Source

libtaginfo / libtaginfo / apetaginfo.cc

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
 * Copyright (C) 2013 Jörn Magens
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This Program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file LICENSE.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth 
 * Floor, Boston, MA  02110-1301  USA
 * https://www.gnu.org/licenses/lgpl-2.1.txt
 *
 * Author:
 * 	Jörn Magens <shuerhaaken@googlemail.com>
 * 	Pavel Vasin <rat4vier@gmail.com>
 */



#include "taginfo.h"
#include "taginfo_internal.h"
#include "taginfo_apetags.h"


#include <apefile.h>
#include <apetag.h>


#define APE_TAG_DISK                "DISC"
#define APE_TAG_DISKNO              "DISCNUMBER"
#define APE_TAG_COMPILATION         "COMPILATION"
#define APE_TAG_ALBUMARTIST         "ALBUM ARTIST" 
#define APE_TAG_ORIGINALARTIST      "ORIGINAL ARTIST" 
#define APE_TAG_TRACK               "TRACK"
#define APE_TAG_COMPOSER            "COMPOSER" 
#define APE_TAG_COMMENT             "COMMENT" 
#define APE_TAG_YEAR                "YEAR" 
#define APE_TAG_LYRICS              "LYRICS" 
#define APE_TAG_LYRICS_UNSYNCED     "UNSYNCED LYRICS"
#define APE_TAG_RATING              "RATING"
#define APE_TAG_PLAYCNT             "PLAY_COUNTER"
#define APE_TAG_COPYRIGHT           "COPYRIGHT"
#define APE_TAG_ENCODER             "ENCODED BY"
#define APE_TAG_HOMEPAGE            "RELATED"

#define TRACK_LABELS                "TRACK_LABELS"
#define ALBUM_LABELS                "ALBUM_LABELS"
#define ARTIST_LABELS               "ARTIST_LABELS"

#define APE_TAG_COVER_ART_OTHER             "COVER ART (OTHER)"
#define APE_TAG_COVER_ART_ICON              "COVER ART (ICON)"
#define APE_TAG_COVER_ART_OTHER_ICON        "COVER ART (OTHER ICON)"
#define APE_TAG_COVER_ART_FRONT             "COVER ART (FRONT)"
#define APE_TAG_COVER_ART_BACK              "COVER ART (BACK)"
#define APE_TAG_COVER_ART_LEAFLET           "COVER ART (LEAFLET)"
#define APE_TAG_COVER_ART_MEDIA             "COVER ART (MEDIA)"
#define APE_TAG_COVER_ART_LEAD              "COVER ART (LEAD)"
#define APE_TAG_COVER_ART_ARTIST            "COVER ART (ARTIST)"
#define APE_TAG_COVER_ART_CONDUCTOR         "COVER ART (CONDUCTOR)"
#define APE_TAG_COVER_ART_BAND              "COVER ART (BAND)"
#define APE_TAG_COVER_ART_COMPOSER          "COVER ART (COMPOSER)"
#define APE_TAG_COVER_ART_LYRICIST          "COVER ART (LYRICIST)"
#define APE_TAG_COVER_ART_STUDIO            "COVER ART (STUDIO)"
#define APE_TAG_COVER_ART_RECORDING         "COVER ART (RECORDING)"
#define APE_TAG_COVER_ART_PERFORMANCE       "COVER ART (PERFORMANCE)"
#define APE_TAG_COVER_ART_MOVIE_SCENE       "COVER ART (MOVIE SCENE)"
#define APE_TAG_COVER_ART_COLORED_FISH      "COVER ART (COLORED FISH)"
#define APE_TAG_COVER_ART_ILLUSTRATION      "COVER ART (ILLUSTRATION)"
#define APE_TAG_COVER_ART_BAND_LOGO         "COVER ART (BAND LOGO)"
#define APE_TAG_COVER_ART_PUBLISHER_LOGO    "COVER ART (PUBLISHER LOGO)"


using namespace TagInfo;
using namespace TagLib;



void set_item(TagLib::APE::Tag * apetag, const char * key, const String &value, bool replace = true) {
    apetag->removeItem(key);
    if(!value.isEmpty())
        apetag->addValue(key, value, replace);
}

void extract_image(const TagLib::APE::Item &image_item, Image *& image) {
    image->image_file_type = IMAGE_FILE_TYPE_UNKNOWN;
    image->description = NULL; // description not supported in APE tags!
    if(image_item.type() == TagLib::APE::Item::Binary) {
        TagLib::ByteVector CoverData = image_item.binaryData();
        if(CoverData.size() > 0) {
            image->data_length = CoverData.size();
            image->data = new char[image->data_length];
            memcpy(image->data, CoverData.data(), CoverData.size());
            return;
        }
    }
    image->data = NULL;
    image->data_length = 0;
}

String image_type_to_ape_image_name(const ImageType& image_type) {
    switch(image_type) {
        case IMAGE_TYPE_COVER_FRONT :
            return APE_TAG_COVER_ART_FRONT;
        case IMAGE_TYPE_COVER_BACK :
            return APE_TAG_COVER_ART_BACK;
        case IMAGE_TYPE_OTHER :
            return APE_TAG_COVER_ART_OTHER;
        case IMAGE_TYPE_FILE_ICON :
            return APE_TAG_COVER_ART_ICON;
        case IMAGE_TYPE_OTHER_FILE_ICON :
            return APE_TAG_COVER_ART_OTHER_ICON;
        case IMAGE_TYPE_LEAFLET_PAGE :
            return APE_TAG_COVER_ART_LEAFLET;
        case IMAGE_TYPE_MEDIA  :
            return APE_TAG_COVER_ART_MEDIA;
        case IMAGE_TYPE_LEAD_ARTIST :
            return APE_TAG_COVER_ART_LEAD;
        case IMAGE_TYPE_ARTIST :
            return APE_TAG_COVER_ART_ARTIST;
        case IMAGE_TYPE_CONDUCTOR :
            return APE_TAG_COVER_ART_CONDUCTOR;
        case IMAGE_TYPE_BAND :
            return APE_TAG_COVER_ART_BAND;
        case IMAGE_TYPE_COMPOSER :
            return APE_TAG_COVER_ART_COMPOSER;
        case IMAGE_TYPE_LYRICIST :
            return APE_TAG_COVER_ART_LYRICIST;
        case IMAGE_TYPE_RECORDING_LOCATION :
            return APE_TAG_COVER_ART_STUDIO;
        case IMAGE_TYPE_DURING_RECORDING :
            return APE_TAG_COVER_ART_RECORDING;
        case IMAGE_TYPE_DURING_PERFORMANCE :
            return APE_TAG_COVER_ART_PERFORMANCE;;
        case IMAGE_TYPE_MOVIESCREENCAPTURE :
            return APE_TAG_COVER_ART_MOVIE_SCENE;;
        case IMAGE_TYPE_COLOURED_FISH :
            return APE_TAG_COVER_ART_COLORED_FISH;
        case IMAGE_TYPE_ILLUSTRATION :
            return APE_TAG_COVER_ART_ILLUSTRATION;
        case IMAGE_TYPE_ARTIST_LOGO :
            return APE_TAG_COVER_ART_BAND_LOGO;
        case IMAGE_TYPE_PUBLISHER_LOGO :
            return APE_TAG_COVER_ART_PUBLISHER_LOGO;
        default:
            return APE_TAG_COVER_ART_OTHER;
    }
}

ImageType ape_image_type_to_image_type(const String &nme) {
    if(nme == APE_TAG_COVER_ART_FRONT)
        return IMAGE_TYPE_COVER_FRONT;
    else if(nme == APE_TAG_COVER_ART_OTHER)
        return IMAGE_TYPE_OTHER;
    else if(nme == APE_TAG_COVER_ART_BACK)
        return IMAGE_TYPE_COVER_BACK;
    else if(nme == APE_TAG_COVER_ART_ICON)
        return IMAGE_TYPE_FILE_ICON;
    else if(nme == APE_TAG_COVER_ART_OTHER_ICON)
        return IMAGE_TYPE_OTHER_FILE_ICON;
    else if(nme == APE_TAG_COVER_ART_LEAFLET)
        return IMAGE_TYPE_LEAFLET_PAGE;
    else if(nme == APE_TAG_COVER_ART_MEDIA)
        return IMAGE_TYPE_MEDIA;
    else if(nme == APE_TAG_COVER_ART_LEAD)
        return IMAGE_TYPE_LEAD_ARTIST;
    else if(nme == APE_TAG_COVER_ART_ARTIST)
        return IMAGE_TYPE_ARTIST;
    else if(nme == APE_TAG_COVER_ART_CONDUCTOR)
        return IMAGE_TYPE_CONDUCTOR;
    else if(nme == APE_TAG_COVER_ART_BAND)
        return IMAGE_TYPE_BAND;
    else if(nme == APE_TAG_COVER_ART_COMPOSER)
        return IMAGE_TYPE_COMPOSER;
    else if(nme == APE_TAG_COVER_ART_LYRICIST)
        return IMAGE_TYPE_LYRICIST;
    else if(nme == APE_TAG_COVER_ART_STUDIO)
        return IMAGE_TYPE_RECORDING_LOCATION;
    else if(nme == APE_TAG_COVER_ART_RECORDING)
        return IMAGE_TYPE_DURING_RECORDING;
    else if(nme == APE_TAG_COVER_ART_PERFORMANCE)
        return IMAGE_TYPE_DURING_PERFORMANCE;
    else if(nme == APE_TAG_COVER_ART_MOVIE_SCENE)
        return IMAGE_TYPE_MOVIESCREENCAPTURE;
    else if(nme == APE_TAG_COVER_ART_COLORED_FISH)
        return IMAGE_TYPE_COLOURED_FISH;
    else if(nme == APE_TAG_COVER_ART_ILLUSTRATION)
        return IMAGE_TYPE_ILLUSTRATION;
    else if(nme == APE_TAG_COVER_ART_BAND_LOGO)
        return IMAGE_TYPE_ARTIST_LOGO;
    else if(nme == APE_TAG_COVER_ART_PUBLISHER_LOGO)
        return IMAGE_TYPE_PUBLISHER_LOGO;
    else
        return IMAGE_TYPE_OTHER;
}


ApeTagInfo::ApeTagInfo(const String &filename) : Info(filename) {
    taglib_apetag = NULL;
}


ApeTagInfo::~ApeTagInfo() {
}


bool ApeTagInfo::load(void) {
    if(Info::load()) {
        if(taglib_apetag && !taglib_apetag->isEmpty()) {
            if(!taglib_tag)
                load_base_tags((TagLib::Tag *)taglib_apetag);
            if(taglib_apetag->itemListMap().contains(APE_TAG_COMPOSER)) {
                composer = taglib_apetag->itemListMap()[APE_TAG_COMPOSER].toStringList().front();
            }
            if(taglib_apetag->itemListMap().contains(APE_TAG_COPYRIGHT)) {
                copyright = taglib_apetag->itemListMap()[APE_TAG_COPYRIGHT].toStringList().front();
            }
            if(taglib_apetag->itemListMap().contains(APE_TAG_ORIGINALARTIST)) {
                original_artist = taglib_apetag->itemListMap()[APE_TAG_ORIGINALARTIST].toStringList().front();
            }
            if(taglib_apetag->itemListMap().contains(APE_TAG_ENCODER)) {
                encoder = taglib_apetag->itemListMap()[APE_TAG_ENCODER].toStringList().front();
            }
            if(taglib_apetag->itemListMap().contains(APE_TAG_HOMEPAGE)) {
                homepage = taglib_apetag->itemListMap()[APE_TAG_HOMEPAGE].toStringList().front();
            }
            if(taglib_apetag->itemListMap().contains(APE_TAG_TRACK) && !taglib_apetag->itemListMap()[APE_TAG_TRACK].isEmpty()) {
                StringList list = taglib_apetag->itemListMap()[APE_TAG_TRACK].toStringList().front().split("/");
                if(list.size() == 2) {
                    track_number = atol(list.front().toCString(false)); 
                    track_count  = atol(list.back().toCString(false));
                }
                else if(list.size() == 1) {
                    track_number = atol(list.front().toCString(false)); 
                }
            }
            if(taglib_apetag->itemListMap().contains(APE_TAG_DISK) && !taglib_apetag->itemListMap()[APE_TAG_DISK].isEmpty()) {
                StringList list = taglib_apetag->itemListMap()[APE_TAG_DISK].toStringList().front().split("/");
                if(list.size() == 2) {
                    volume_number = atol(list.front().toCString(false)); 
                    volume_count  = atol(list.back().toCString(false));
                }
                else if(list.size() == 1) {
                    track_number = atol(list.front().toCString(false)); 
                }
            }
            else if(taglib_apetag->itemListMap().contains(APE_TAG_DISKNO)) {
                volume_number = 
                  atol(taglib_apetag->itemListMap()[APE_TAG_DISKNO].toStringList().front().toCString(false));
            }
            if(taglib_apetag->itemListMap().contains(APE_TAG_COMPILATION)) {
                is_compilation = 
                    taglib_apetag->itemListMap()[APE_TAG_COMPILATION].toStringList().front() == String("1");
            }
            if(taglib_apetag->itemListMap().contains(APE_TAG_ALBUMARTIST)) {
                album_artist = taglib_apetag->itemListMap()[APE_TAG_ALBUMARTIST].toStringList().front();
            }
            else if(taglib_apetag->itemListMap().contains("ALBUMARTIST")) {
                album_artist = taglib_apetag->itemListMap()["ALBUMARTIST"].toStringList().front();
            }
            
            // Rating 
            if(taglib_apetag->itemListMap().contains(APE_TAG_RATING)) {
                long rat = 0;
                rat = atol(taglib_apetag->itemListMap()[APE_TAG_RATING].toStringList().front().toCString(true));
                if(rat) {
                    if(rat > 5) {
                        rating = popularity_to_rating(rat);
                    }
                    else {
                        rating = rat;
                    }
                }
            }
            if(taglib_apetag->itemListMap().contains(APE_TAG_PLAYCNT)) {
                playcount = 
                    atol(taglib_apetag->itemListMap()[APE_TAG_PLAYCNT].toStringList().front().toCString(false));
                if(playcount < 0)
                    playcount = 0;
            }
            // Labels
            if(track_labels.size() == 0) {
                if(taglib_apetag->itemListMap().contains(TRACK_LABELS)) {
                    track_labels_string = taglib_apetag->itemListMap()[TRACK_LABELS].toStringList().front();
                    track_labels = split(track_labels_string, "|");
                }
            }
            if(artist_labels.size() == 0) {
                if(taglib_apetag->itemListMap().contains(ARTIST_LABELS)) {
                    artist_labels_string = taglib_apetag->itemListMap()[ARTIST_LABELS].toStringList().front();
                    artist_labels = split(artist_labels_string, "|");
                }
            }
            if(album_labels.size() == 0) {
                if(taglib_apetag->itemListMap().contains(ALBUM_LABELS)) {
                    album_labels_string = taglib_apetag->itemListMap()[ALBUM_LABELS].toStringList().front();
                    album_labels = split(album_labels_string, "|");
                }
            }
            APE::ItemListMap map = taglib_apetag->itemListMap();
            for(APE::ItemListMap::ConstIterator it = map.begin(); it != map.end(); ++it) {
                if(it->first == APE_TAG_COVER_ART_OTHER          ||
                   it->first == APE_TAG_COVER_ART_ICON	         ||
                   it->first == APE_TAG_COVER_ART_OTHER_ICON     ||
                   it->first == APE_TAG_COVER_ART_FRONT          ||
                   it->first == APE_TAG_COVER_ART_BACK           ||
                   it->first == APE_TAG_COVER_ART_LEAFLET        ||
                   it->first == APE_TAG_COVER_ART_MEDIA          ||
                   it->first == APE_TAG_COVER_ART_LEAD           ||
                   it->first == APE_TAG_COVER_ART_ARTIST         ||
                   it->first == APE_TAG_COVER_ART_CONDUCTOR      ||
                   it->first == APE_TAG_COVER_ART_BAND           ||
                   it->first == APE_TAG_COVER_ART_COMPOSER       ||
                   it->first == APE_TAG_COVER_ART_LYRICIST       ||
                   it->first == APE_TAG_COVER_ART_STUDIO         ||
                   it->first == APE_TAG_COVER_ART_RECORDING      ||
                   it->first == APE_TAG_COVER_ART_PERFORMANCE    ||
                   it->first == APE_TAG_COVER_ART_MOVIE_SCENE    ||
                   it->first == APE_TAG_COVER_ART_COLORED_FISH   ||
                   it->first == APE_TAG_COVER_ART_ILLUSTRATION   ||
                   it->first == APE_TAG_COVER_ART_BAND_LOGO      ||
                   it->first == APE_TAG_COVER_ART_PUBLISHER_LOGO   ) {
                    has_image = true;
                }
            }
            return true;
        }
    }
    return false;
}


bool ApeTagInfo::save(void) {
    if(taglib_apetag) {
        if(changedflag) {
            if(changedflag & CHANGED_COMPOSER_TAG)
                set_item(taglib_apetag, APE_TAG_COMPOSER, composer, true);
            if(changedflag & CHANGED_COPYRIGHT_TAG)
                set_item(taglib_apetag, APE_TAG_COPYRIGHT, copyright, true);
            if(changedflag & CHANGED_ORIGINALARTIST_TAG)
                set_item(taglib_apetag, APE_TAG_ORIGINALARTIST, original_artist, true);
            if(changedflag & CHANGED_ENCODER_TAG)
                set_item(taglib_apetag, APE_TAG_ENCODER, encoder, true);
            if(changedflag & CHANGED_HOMEPAGE_TAG)
                set_item(taglib_apetag, APE_TAG_HOMEPAGE, homepage, true);
            if(changedflag & CHANGED_TRACK_NUMBER) {
                if(!(changedflag & CHANGED_TRACK_COUNT)) {
                    // prevent overwrite in save_base_tags()
                    changedflag &= ~(CHANGED_TRACK_NUMBER);
                    taglib_apetag->setTrack(track_number);
                }
                else {
                    // prevent overwrite in save_base_tags()
                    changedflag &= ~(CHANGED_TRACK_NUMBER);
                    changedflag &= ~(CHANGED_TRACK_COUNT);
                    taglib_apetag->addValue(APE_TAG_TRACK, format("%d/%d", track_number, track_count), true);
                }
            }
            if(changedflag & CHANGED_TRACK_COUNT) {
                // prevent overwrite in save_base_tags()
                changedflag &= ~(CHANGED_TRACK_NUMBER);
                changedflag &= ~(CHANGED_TRACK_COUNT);
                taglib_apetag->addValue(APE_TAG_TRACK, format("%d/%d", track_number, track_count), true);
            }
            if(changedflag & CHANGED_DATA_VOL_NUM) {
                if(!(changedflag & CHANGED_DATA_VOL_CNT)) {
                    // prevent overwrite in save_base_tags()
                    changedflag &= ~(CHANGED_DATA_VOL_NUM);
                    taglib_apetag->addValue(APE_TAG_DISK, format("%u", volume_number), true);
                    taglib_apetag->addValue(APE_TAG_DISKNO, format("%u", volume_number), true);
                }
                else {
                    // prevent overwrite in save_base_tags()
                    changedflag &= ~(CHANGED_DATA_VOL_NUM);
                    changedflag &= ~(CHANGED_DATA_VOL_CNT);
                    taglib_apetag->addValue(APE_TAG_DISK, format("%u/%u", volume_number, volume_count), true);
                }
            }
            if(changedflag & CHANGED_DATA_VOL_CNT) {
                // prevent overwrite in save_base_tags()
                changedflag &= ~(CHANGED_DATA_VOL_NUM);
                changedflag &= ~(CHANGED_DATA_VOL_CNT);
                taglib_apetag->addValue(APE_TAG_DISK, format("%d/%d", volume_number, volume_count), true);
            }
            
            if(changedflag & CHANGED_IS_COMPILATION_TAG) {
                if(is_compilation) {
                    taglib_apetag->addValue(APE_TAG_COMPILATION, "1", true);
                }
                else {
                    taglib_apetag->addValue(APE_TAG_COMPILATION, "0", true);
                }
            }
            if(changedflag & CHANGED_DATA_ALBUMARTIST)
                set_item(taglib_apetag, APE_TAG_ALBUMARTIST, album_artist, true);
            
            if(changedflag & CHANGED_DATA_RATING)
                taglib_apetag->addValue(APE_TAG_RATING, format("%u", rating_to_popularity(rating)), true);
            
            if(changedflag & CHANGED_DATA_PLAYCOUNT)
                taglib_apetag->addValue(APE_TAG_PLAYCNT, format("%u", playcount), true);
            
            if(changedflag & CHANGED_TRACK_LABELS)
                set_item(taglib_apetag, TRACK_LABELS, track_labels_string, true);
            if(changedflag & CHANGED_ARTIST_LABELS)
                set_item(taglib_apetag, ARTIST_LABELS, artist_labels_string, true);
            if(changedflag & CHANGED_ALBUM_LABELS)
                set_item(taglib_apetag, ALBUM_LABELS, album_labels_string, true);
            save_base_tags((TagLib::Tag *)taglib_apetag);
        }
    }
    return Info::save();
}

Image ** ApeTagInfo::get_images(int &image_count) const {
    if(taglib_apetag && !taglib_apetag->isEmpty()) {
        image_count = 0;
        Image** images;
        List<Image*> imageList;
        if(taglib_apetag) {
            Image * image = NULL;
            int i = 0;
            APE::ItemListMap map = taglib_apetag->itemListMap();
            for(APE::ItemListMap::ConstIterator it = map.begin(); it != map.end(); ++it) {
                if(it->first == APE_TAG_COVER_ART_OTHER          ||
                   it->first == APE_TAG_COVER_ART_ICON	         ||
                   it->first == APE_TAG_COVER_ART_OTHER_ICON     ||
                   it->first == APE_TAG_COVER_ART_FRONT          ||
                   it->first == APE_TAG_COVER_ART_BACK           ||
                   it->first == APE_TAG_COVER_ART_LEAFLET        ||
                   it->first == APE_TAG_COVER_ART_MEDIA          ||
                   it->first == APE_TAG_COVER_ART_LEAD           ||
                   it->first == APE_TAG_COVER_ART_ARTIST         ||
                   it->first == APE_TAG_COVER_ART_CONDUCTOR      ||
                   it->first == APE_TAG_COVER_ART_BAND           ||
                   it->first == APE_TAG_COVER_ART_COMPOSER       ||
                   it->first == APE_TAG_COVER_ART_LYRICIST       ||
                   it->first == APE_TAG_COVER_ART_STUDIO         ||
                   it->first == APE_TAG_COVER_ART_RECORDING      ||
                   it->first == APE_TAG_COVER_ART_PERFORMANCE    ||
                   it->first == APE_TAG_COVER_ART_MOVIE_SCENE    ||
                   it->first == APE_TAG_COVER_ART_COLORED_FISH   ||
                   it->first == APE_TAG_COVER_ART_ILLUSTRATION   ||
                   it->first == APE_TAG_COVER_ART_BAND_LOGO      ||
                   it->first == APE_TAG_COVER_ART_PUBLISHER_LOGO   ) {
                    image = new Image();
                    extract_image(it->second, image);
                    if(image->data && image->data_length > 0) {
                        String apetype = String(it->first);
                        image->image_type = ape_image_type_to_image_type(apetype);
                        imageList.prepend(image);
                        i++;
                    }
                    else {
                        delete image;
                    }
               }
            }
            if(0 < imageList.size()) {
                images = new Image*[imageList.size()];
                int ii = 0;
                for(List<Image *>::Iterator it = imageList.begin(); it != imageList.end(); ++it) {
                    images[ii] = (*it);
                    ii++;
                }
                image_count = imageList.size();
                return images;
            }
        }
        
    }
    return NULL;
}

void ApeTagInfo::set_images(const Image ** images, const int image_count) {
    if(taglib_apetag) {
        taglib_apetag->removeItem(APE_TAG_COVER_ART_OTHER);
        taglib_apetag->removeItem(APE_TAG_COVER_ART_ICON);
        taglib_apetag->removeItem(APE_TAG_COVER_ART_OTHER_ICON);
        taglib_apetag->removeItem(APE_TAG_COVER_ART_FRONT);
        taglib_apetag->removeItem(APE_TAG_COVER_ART_BACK);
        taglib_apetag->removeItem(APE_TAG_COVER_ART_LEAFLET);
        taglib_apetag->removeItem(APE_TAG_COVER_ART_MEDIA);
        taglib_apetag->removeItem(APE_TAG_COVER_ART_LEAD);
        taglib_apetag->removeItem(APE_TAG_COVER_ART_ARTIST);
        taglib_apetag->removeItem(APE_TAG_COVER_ART_CONDUCTOR);
        taglib_apetag->removeItem(APE_TAG_COVER_ART_BAND);
        taglib_apetag->removeItem(APE_TAG_COVER_ART_COMPOSER);
        taglib_apetag->removeItem(APE_TAG_COVER_ART_LYRICIST);
        taglib_apetag->removeItem(APE_TAG_COVER_ART_STUDIO);
        taglib_apetag->removeItem(APE_TAG_COVER_ART_RECORDING);
        taglib_apetag->removeItem(APE_TAG_COVER_ART_PERFORMANCE);
        taglib_apetag->removeItem(APE_TAG_COVER_ART_MOVIE_SCENE);
        taglib_apetag->removeItem(APE_TAG_COVER_ART_COLORED_FISH);
        taglib_apetag->removeItem(APE_TAG_COVER_ART_ILLUSTRATION);
        taglib_apetag->removeItem(APE_TAG_COVER_ART_BAND_LOGO);
        taglib_apetag->removeItem(APE_TAG_COVER_ART_PUBLISHER_LOGO);
        
        for(int p = 0; p < image_count; p++) {
            if(!images[p]->data || images[p]->data_length == 0)
                continue;
            
            ByteVector ImgData = ByteVector((TagLib::uint) images[p]->data_length);
            memcpy(ImgData.data(), images[p]->data, images[p]->data_length);
            
            APE::Item imageItem = APE::Item();
            imageItem.setType(TagLib::APE::Item::Binary);
            String key = image_type_to_ape_image_name(images[p]->image_type);
            imageItem.setKey(key);
            imageItem.setBinaryData(ImgData);
            taglib_apetag->setItem(key, imageItem);
        }
    }
}


String ApeTagInfo::get_lyrics(void) const {
    if(taglib_apetag->itemListMap().contains(APE_TAG_LYRICS))
        return taglib_apetag->itemListMap()[APE_TAG_LYRICS].toStringList().front();
    return String();
}


bool ApeTagInfo::set_lyrics(const String &lyrics) {
    if(taglib_apetag) {
        taglib_apetag->addValue(APE_TAG_LYRICS, lyrics);
        return true;
    }
    return false;
}