taglibfile.cpp 193 KB
Newer Older
Urs Fleisch's avatar
Urs Fleisch committed
1
2
3
4
5
6
7
/**
 * \file taglibfile.cpp
 * Handling of tagged files using TagLib.
 *
 * \b Project: Kid3
 * \author Urs Fleisch
 * \date 12 Sep 2006
8
 *
9
 * Copyright (C) 2006-2013  Urs Fleisch
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * This file is part of Kid3.
 *
 * Kid3 is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Kid3 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.  If not, see <http://www.gnu.org/licenses/>.
Urs Fleisch's avatar
Urs Fleisch committed
25
26
27
 */

#include "taglibfile.h"
Urs Fleisch's avatar
Urs Fleisch committed
28
29
30
#include <QDir>
#include <QString>
#include <QTextCodec>
31
#include <QByteArray>
32
#include <QImage>
33
#include <QCoreApplication>
34
#include <QVarLengthArray>
Urs Fleisch's avatar
Urs Fleisch committed
35
#include "genres.h"
36
#include "attributedata.h"
37
#include "pictureframe.h"
Urs Fleisch's avatar
Urs Fleisch committed
38
#include <sys/stat.h>
39
#ifdef Q_OS_WIN32
Urs Fleisch's avatar
Urs Fleisch committed
40
41
42
43
44
#include <sys/utime.h>
#else
#include <utime.h>
#endif

Urs Fleisch's avatar
Urs Fleisch committed
45
// Just using include <oggfile.h>, include <flacfile.h> as recommended in the
Urs Fleisch's avatar
Urs Fleisch committed
46
47
// TagLib documentation does not work, as there are files with these names
// in this directory.
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <mpegfile.h>
#include <oggfile.h>
#include <vorbisfile.h>
#include <flacfile.h>
#include <mpcfile.h>
#include <id3v1tag.h>
#include <id3v2tag.h>
#include <id3v2header.h>
#include <apetag.h>
#include <textidentificationframe.h>
#include <commentsframe.h>
#include <attachedpictureframe.h>
#include <uniquefileidentifierframe.h>
61

Urs Fleisch's avatar
Urs Fleisch committed
62
#if TAGLIB_VERSION <= 0x010400
63
#include "taglibext/generalencapsulatedobjectframe.h"
64
65
#include "taglibext/urllinkframe.h"
#include "taglibext/unsynchronizedlyricsframe.h"
66
67
68
69
70
71
#include "taglibext/speex/speexfile.h"
#include "taglibext/speex/taglib_speexfiletyperesolver.h"
#include "taglibext/trueaudio/ttafile.h"
#include "taglibext/trueaudio/taglib_trueaudiofiletyperesolver.h"
#include "taglibext/wavpack/wvfile.h"
#include "taglibext/wavpack/taglib_wavpackfiletyperesolver.h"
Urs Fleisch's avatar
Urs Fleisch committed
72
#else
73
74
75
76
77
78
#include <generalencapsulatedobjectframe.h>
#include <urllinkframe.h>
#include <unsynchronizedlyricsframe.h>
#include <speexfile.h>
#include <trueaudiofile.h>
#include <wavpackfile.h>
Urs Fleisch's avatar
Urs Fleisch committed
79
80
#endif

81
82
83
84
#if TAGLIB_VERSION >= 0x010500
#include <oggflacfile.h>
#endif

85
86
#if TAGLIB_VERSION >= 0x010600
#ifdef TAGLIB_WITH_MP4
87
#include <mp4file.h>
88
89
#endif
#ifdef TAGLIB_WITH_ASF
90
#include <asffile.h>
91
#endif
92
93
94
95
#include <aifffile.h>
#include <wavfile.h>
#include <popularimeterframe.h>
#include <privateframe.h>
96
97
#endif

98
#if TAGLIB_VERSION >= 0x010700
99
#include <apefile.h>
100
101
#endif

102
#if TAGLIB_VERSION >= 0x010800
103
104
105
106
#include <ownershipframe.h>
#include <modfile.h>
#include <s3mfile.h>
#include <itfile.h>
107
#include <tfilestream.h>
108
#ifdef HAVE_TAGLIB_XM_SUPPORT
109
#include <xmfile.h>
110
#endif
111
#endif
112

113
#if TAGLIB_VERSION >= 0x010900
114
#include <opusfile.h>
115
116
#endif

117
118
119
120
121
#if TAGLIB_VERSION >= 0x010901
#include "taglibext/dsf/dsffiletyperesolver.h"
#include "taglibext/dsf/dsffile.h"
#endif

Urs Fleisch's avatar
Urs Fleisch committed
122
123
124
125
#if TAGLIB_VERSION >= 0x010902
#include <synchronizedlyricsframe.h>
#include <eventtimingcodesframe.h>
#else
126
#include "taglibext/synchronizedlyricsframe.h"
127
#include "taglibext/eventtimingcodesframe.h"
Urs Fleisch's avatar
Urs Fleisch committed
128
129
130
131
#endif

#include "taglibext/aac/aacfiletyperesolver.h"
#include "taglibext/mp2/mp2filetyperesolver.h"
132

133
134
namespace {

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/** Convert QString @a s to a TagLib::String. */
TagLib::String toTString(const QString& s)
{
  int len = s.length();
  QVarLengthArray<wchar_t> a(len + 1);
  wchar_t* ws = a.data();
  len = s.toWCharArray(ws);
  ws[len] = 0;
  return TagLib::String(ws);
}

/** Convert TagLib::String @a s to a QString. */
inline QString toQString(const TagLib::String& s)
{
#if TAGLIB_VERSION >= 0x010900
  return QString::fromWCharArray(s.toCWString(), s.size());
#else
  return TStringToQString(s);
#endif
}

156
157
158
159
160
161
162
163
164
#if TAGLIB_VERSION >= 0x010700
/**
 * Set a picture frame from a FLAC picture.
 *
 * @param pic FLAC picture
 * @param frame the picture frame is returned here
 */
void flacPictureToFrame(const TagLib::FLAC::Picture* pic, Frame& frame)
{
Urs Fleisch's avatar
Urs Fleisch committed
165
  TagLib::ByteVector picData(pic->data());
166
167
168
169
  QByteArray ba(picData.data(), picData.size());
  PictureFrame::ImageProperties imgProps(
        pic->width(), pic->height(), pic->colorDepth(),
        pic->numColors(), ba);
Urs Fleisch's avatar
Urs Fleisch committed
170
  PictureFrame::setFields(
171
    frame, Frame::TE_ISO8859_1, QLatin1String("JPG"), toQString(pic->mimeType()),
Urs Fleisch's avatar
Urs Fleisch committed
172
    static_cast<PictureFrame::PictureType>(pic->type()),
173
    toQString(pic->description()),
174
    ba, &imgProps);
175
176
177
178
179
180
181
182
183
184
}

/**
 * Set a FLAC picture from a frame.
 *
 * @param frame picture frame
 * @param pic the FLAC picture to set
 */
void frameToFlacPicture(const Frame& frame, TagLib::FLAC::Picture* pic)
{
185
  Frame::TextEncoding enc;
Urs Fleisch's avatar
Urs Fleisch committed
186
187
188
189
190
  QString imgFormat;
  QString mimeType;
  PictureFrame::PictureType pictureType;
  QString description;
  QByteArray data;
191
  PictureFrame::ImageProperties imgProps;
Urs Fleisch's avatar
Urs Fleisch committed
192
  PictureFrame::getFields(frame, enc, imgFormat, mimeType, pictureType,
193
                          description, data, &imgProps);
Urs Fleisch's avatar
Urs Fleisch committed
194
  pic->setType(static_cast<TagLib::FLAC::Picture::Type>(pictureType));
195
196
  pic->setMimeType(toTString(mimeType));
  pic->setDescription(toTString(description));
Urs Fleisch's avatar
Urs Fleisch committed
197
  pic->setData(TagLib::ByteVector(data.data(), data.size()));
198
199
  if (!imgProps.isValidForImage(data)) {
    imgProps = PictureFrame::ImageProperties(data);
Urs Fleisch's avatar
Urs Fleisch committed
200
  }
201
202
203
204
  pic->setWidth(imgProps.width());
  pic->setHeight(imgProps.height());
  pic->setColorDepth(imgProps.depth());
  pic->setNumColors(imgProps.numColors());
205
206
207
208
}
#endif

}
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
#if TAGLIB_VERSION >= 0x010800
/**
 * Wrapper around TagLib::FileStream which reduces the number of open file
 * descriptors.
 *
 * Using streams, closing the file descriptor is also possible for modified
 * files because the TagLib file does not have to be deleted just to close the
 * file descriptor.
 */
class FileIOStream : public TagLib::IOStream {
public:
  /**
   * Constructor.
   * @param fileName path to file
   */
  explicit FileIOStream(const QString& fileName);

  /**
   * Destructor.
   */
  virtual ~FileIOStream();

  /**
   * Close the file handle.
   * The file will automatically be opened again if needed.
   */
  void closeFileHandle();

  // Reimplemented from TagLib::IOStream, delegate to TagLib::FileStream.
  TagLib::FileName name() const;
  TagLib::ByteVector readBlock(ulong length);
  void writeBlock(const TagLib::ByteVector &data);
  void insert(const TagLib::ByteVector &data,
              ulong start = 0, ulong replace = 0);
  void removeBlock(ulong start = 0, ulong length = 0);
  bool readOnly() const;
  bool isOpen() const;
  void seek(long offset, Position p = Beginning);
  void clear();
  long tell() const;
  long length();
  void truncate(long length);

  /**
   * Create a TagLib file for a stream.
   * TagLib::FileRef::create() adapted for IOStream.
   * @param stream stream with name() of which the extension is used to decduce
   * the file type
   * @return file, 0 if not supported.
   */
  static TagLib::File* create(IOStream* stream);

private:
  /**
   * Open file handle, is called by operations which need a file handle.
265
266
   *
   * @return true if file is open.
267
   */
268
  bool openFileHandle() const;
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

  /**
   * Register open files, so that the number of open files can be limited.
   * If the number of open files exceeds a limit, files are closed.
   *
   * @param stream new open file to be registered
   */
  static void registerOpenFile(FileIOStream* stream);

  /**
   * Deregister open file.
   *
   * @param stream file which is no longer open
   */
  static void deregisterOpenFile(FileIOStream* stream);

#ifdef Q_OS_WIN32
  wchar_t* m_fileName;
#else
  char* m_fileName;
#endif
  TagLib::FileStream* m_fileStream;
  long m_offset;

  /** list of file streams with open file descriptor */
  static QList<FileIOStream*> s_openFiles;
};

QList<FileIOStream*> FileIOStream::s_openFiles;

FileIOStream::FileIOStream(const QString& fileName) :
  m_fileStream(0), m_offset(0)
{
#ifdef Q_OS_WIN32
  int fnLen = fileName.length();
  m_fileName = new wchar_t[fnLen + 1];
  m_fileName[fnLen] = 0;
  fileName.toWCharArray(m_fileName);
#else
  QByteArray fn = QFile::encodeName(fileName);
  m_fileName = new char[fn.size() + 1];
  qstrcpy(m_fileName, fn.data());
#endif
}

FileIOStream::~FileIOStream()
{
  deregisterOpenFile(this);
  delete m_fileStream;
  delete [] m_fileName;
}

321
bool FileIOStream::openFileHandle() const
322
323
324
325
326
{
  if (!m_fileStream) {
    FileIOStream* self = const_cast<FileIOStream*>(this);
    self->m_fileStream =
        new TagLib::FileStream(TagLib::FileName(m_fileName));
327
328
329
330
331
    if (!self->m_fileStream->isOpen()) {
      delete self->m_fileStream;
      self->m_fileStream = 0;
      return false;
    }
332
333
334
335
336
    if (m_offset > 0) {
      m_fileStream->seek(m_offset);
    }
    registerOpenFile(self);
  }
337
  return true;
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
}

void FileIOStream::closeFileHandle()
{
  if (m_fileStream) {
    m_offset = m_fileStream->tell();
    delete m_fileStream;
    m_fileStream = 0;
    deregisterOpenFile(this);
  }
}

TagLib::FileName FileIOStream::name() const
{
  if (m_fileStream) {
    return m_fileStream->name();
  }
  return TagLib::FileName(m_fileName);
}

TagLib::ByteVector FileIOStream::readBlock(ulong length)
{
360
361
362
363
  if (openFileHandle()) {
    return m_fileStream->readBlock(length);
  }
  return TagLib::ByteVector();
364
365
366
367
}

void FileIOStream::writeBlock(const TagLib::ByteVector &data)
{
368
369
370
  if (openFileHandle()) {
    m_fileStream->writeBlock(data);
  }
371
372
373
374
375
}

void FileIOStream::insert(const TagLib::ByteVector &data,
                          ulong start, ulong replace)
{
376
377
378
  if (openFileHandle()) {
    m_fileStream->insert(data, start, replace);
  }
379
380
381
382
}

void FileIOStream::removeBlock(ulong start, ulong length)
{
383
384
385
  if (openFileHandle()) {
    m_fileStream->removeBlock(start, length);
  }
386
387
388
389
}

bool FileIOStream::readOnly() const
{
390
391
392
393
  if (openFileHandle()) {
    return m_fileStream->readOnly();
  }
  return true;
394
395
396
397
398
399
400
401
402
403
404
405
}

bool FileIOStream::isOpen() const
{
  if (m_fileStream) {
    return m_fileStream->isOpen();
  }
  return true;
}

void FileIOStream::seek(long offset, Position p)
{
406
407
408
  if (openFileHandle()) {
    m_fileStream->seek(offset, p);
  }
409
410
411
412
}

void FileIOStream::clear()
{
413
414
415
  if (openFileHandle()) {
    m_fileStream->clear();
  }
416
417
418
419
}

long FileIOStream::tell() const
{
420
421
422
423
  if (openFileHandle()) {
    return m_fileStream->tell();
  }
  return 0;
424
425
426
427
}

long FileIOStream::length()
{
428
429
430
431
  if (openFileHandle()) {
    return m_fileStream->length();
  }
  return 0;
432
433
434
435
}

void FileIOStream::truncate(long length)
{
436
437
438
  if (openFileHandle()) {
    m_fileStream->truncate(length);
  }
439
440
441
442
443
444
445
446
447
448
449
450
}

TagLib::File* FileIOStream::create(TagLib::IOStream* stream)
{
#ifdef Q_OS_WIN32
  TagLib::String fn = stream->name().toString();
#else
  TagLib::String fn = stream->name();
#endif
  const int extPos = fn.rfind(".");
  if (extPos != -1) {
    TagLib::String ext = fn.substr(extPos + 1).upper();
Urs Fleisch's avatar
Urs Fleisch committed
451
    if (ext == "MP3" || ext == "MP2" || ext == "AAC")
452
453
      return new TagLib::MPEG::File(stream,
                                    TagLib::ID3v2::FrameFactory::instance());
454
455
456
457
458
459
460
461
    if (ext == "OGG") {
      TagLib::File* file = new TagLib::Vorbis::File(stream);
      if (!file->isValid()) {
        delete file;
        file = new TagLib::Ogg::FLAC::File(stream);
      }
      return file;
    }
462
    if (ext == "OGA") {
463
      TagLib::File* file = new TagLib::Ogg::FLAC::File(stream);
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
      if (!file->isValid()) {
        delete file;
        file = new TagLib::Vorbis::File(stream);
      }
      return file;
    }
    if (ext == "FLAC")
      return new TagLib::FLAC::File(stream,
                                    TagLib::ID3v2::FrameFactory::instance());
    if (ext == "MPC")
      return new TagLib::MPC::File(stream);
    if (ext == "WV")
      return new TagLib::WavPack::File(stream);
    if (ext == "SPX")
      return new TagLib::Ogg::Speex::File(stream);
Urs Fleisch's avatar
Urs Fleisch committed
479
#if TAGLIB_VERSION >= 0x010900
480
481
    if (ext == "OPUS")
      return new TagLib::Ogg::Opus::File(stream);
Urs Fleisch's avatar
Urs Fleisch committed
482
#endif
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
    if (ext == "TTA")
      return new TagLib::TrueAudio::File(stream);
    if (ext == "M4A" || ext == "M4R" || ext == "M4B" || ext == "M4P" ||
        ext == "MP4" || ext == "3G2")
      return new TagLib::MP4::File(stream);
    if (ext == "WMA" || ext == "ASF")
      return new TagLib::ASF::File(stream);
    if (ext == "AIF" || ext == "AIFF")
      return new TagLib::RIFF::AIFF::File(stream);
    if (ext == "WAV")
      return new TagLib::RIFF::WAV::File(stream);
    if (ext == "APE")
      return new TagLib::APE::File(stream);
    if (ext == "MOD" || ext == "MODULE" || ext == "NST" || ext == "WOW")
      return new TagLib::Mod::File(stream);
    if (ext == "S3M")
      return new TagLib::S3M::File(stream);
    if (ext == "IT")
      return new TagLib::IT::File(stream);
#ifdef HAVE_TAGLIB_XM_SUPPORT
    if (ext == "XM")
      return new TagLib::XM::File(stream);
505
506
507
508
#endif
#if TAGLIB_VERSION >= 0x010901
    if (ext == "DSF")
      return new DSFFile(stream, TagLib::ID3v2::FrameFactory::instance());
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
537
538
539
540
541
542
543
#endif
  }
  return 0;
}

void FileIOStream::registerOpenFile(FileIOStream* stream)
{
  if (s_openFiles.contains(stream))
    return;

  int numberOfFilesToClose = s_openFiles.size() - 15;
  if (numberOfFilesToClose > 5) {
    for (QList<FileIOStream*>::iterator it = s_openFiles.begin();
         it != s_openFiles.end();
         ++it) {
      (*it)->closeFileHandle();
      if (--numberOfFilesToClose <= 0) {
        break;
      }
    }
  }
  s_openFiles.append(stream);
}

/**
 * Deregister open file.
 *
 * @param stream file which is no longer open
 */
void FileIOStream::deregisterOpenFile(FileIOStream* stream)
{
  s_openFiles.removeAll(stream);
}
#endif

544
545
546
547
548
/**
 * Data encoding in ID3v1 tags.
 */
class TextCodecStringHandler : public TagLib::ID3v1::StringHandler {
public:
Urs Fleisch's avatar
Urs Fleisch committed
549
550
551
  /**
   * Constructor.
   */
552
553
  TextCodecStringHandler() {}

Urs Fleisch's avatar
Urs Fleisch committed
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
  /**
   * Destructor.
   */
  virtual ~TextCodecStringHandler() {}

  /**
   * Decode a string from data.
   *
   * @param data data to decode
   */
  virtual TagLib::String parse(const TagLib::ByteVector& data) const;

  /**
   * Encode a byte vector with the data from a string.
   *
   * @param s string to encode
   */
  virtual TagLib::ByteVector render(const TagLib::String& s) const;

  /**
   * Set text codec.
   * @param codec text codec, 0 for default behavior (ISO 8859-1)
   */
  static void setTextCodec(const QTextCodec* codec) { s_codec = codec; }
578
579
580
581
582
583
584
585
586
587
588
589
590
591

private:
  static const QTextCodec* s_codec;
};

const QTextCodec* TextCodecStringHandler::s_codec = 0;

/**
 * Decode a string from data.
 *
 * @param data data to decode
 */
TagLib::String TextCodecStringHandler::parse(const TagLib::ByteVector& data) const
{
Urs Fleisch's avatar
Urs Fleisch committed
592
  return s_codec ?
593
    toTString(s_codec->toUnicode(data.data(), data.size())).stripWhiteSpace() :
Urs Fleisch's avatar
Urs Fleisch committed
594
    TagLib::String(data, TagLib::String::Latin1).stripWhiteSpace();
595
596
597
598
599
600
601
602
603
}

/**
 * Encode a byte vector with the data from a string.
 *
 * @param s string to encode
 */
TagLib::ByteVector TextCodecStringHandler::render(const TagLib::String& s) const
{
Urs Fleisch's avatar
Urs Fleisch committed
604
  if (s_codec) {
605
    QByteArray ba(s_codec->fromUnicode(toQString(s)));
Urs Fleisch's avatar
Urs Fleisch committed
606
607
608
609
    return TagLib::ByteVector(ba.data(), ba.size());
  } else {
    return s.data(TagLib::String::Latin1);
  }
610
611
612
}


613
614
615
/** Default text encoding */
TagLib::String::Type TagLibFile::s_defaultTextEncoding = TagLib::String::Latin1;

616
#if TAGLIB_VERSION < 0x010800
617
618
/** List of TagLib files with open file descriptor */
QList<TagLibFile*> TagLibFile::s_openFiles;
619
#endif
620
621


Urs Fleisch's avatar
Urs Fleisch committed
622
623
624
/**
 * Constructor.
 *
625
 * @param idx index in file proxy model
Urs Fleisch's avatar
Urs Fleisch committed
626
 */
627
628
TagLibFile::TagLibFile(const QPersistentModelIndex& idx) :
  TaggedFile(idx),
629
630
631
  m_tagInformationRead(false), m_hasTagV1(false), m_hasTagV2(false),
  m_isTagV1Supported(false), m_fileRead(false),
  m_tagV1(0), m_tagV2(0),
632
#if TAGLIB_VERSION >= 0x010800
633
  m_stream(0),
634
635
  m_id3v2Version(0),
#endif
636
  m_activatedFeatures(0), m_duration(0),
637
  m_tagTypeV1(TT_Unknown), m_tagTypeV2(TT_Unknown)
Urs Fleisch's avatar
Urs Fleisch committed
638
639
640
641
642
643
644
645
{
}

/**
 * Destructor.
 */
TagLibFile::~TagLibFile()
{
646
  closeFile(true);
Urs Fleisch's avatar
Urs Fleisch committed
647
648
}

649
650
651
652
653
654
655
656
657
/**
 * Get key of tagged file format.
 * @return "TaglibMetadata".
 */
QString TagLibFile::taggedFileKey() const
{
  return QLatin1String("TaglibMetadata");
}

658
659
660
661
662
663
664
/**
 * Get features supported.
 * @return bit mask with Feature flags set.
 */
int TagLibFile::taggedFileFeatures() const
{
  return TF_ID3v11 | TF_ID3v22 |
665
666
667
668
669
670
#if TAGLIB_VERSION >= 0x010500
      TF_OggFlac |
#endif
#if TAGLIB_VERSION >= 0x010700
      TF_OggPictures |
#endif
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
#if TAGLIB_VERSION >= 0x010800
      TF_ID3v23 |
#endif
      TF_ID3v24;
}

/**
 * Get currently active tagged file features.
 * @return active tagged file features (TF_ID3v23, TF_ID3v24, or 0).
 * @see setActiveTaggedFileFeatures()
 */
int TagLibFile::activeTaggedFileFeatures() const
{
  return m_activatedFeatures;
}

/**
 * Activate some features provided by the tagged file.
 * TagLibFile provides the TF_ID3v23 and TF_ID3v24 features, which determine
 * the ID3v2 version used in writeTags() (the overload without id3v2Version).
 * If 0 is set, the default behavior applies, i.e. for new files,
 * TagConfig::id3v2Version() is used, else the existing version.
 *
 * @param features TF_ID3v23, TF_ID3v24, or 0
 */
void TagLibFile::setActiveTaggedFileFeatures(int features)
{
  m_activatedFeatures = features;
}

Urs Fleisch's avatar
Urs Fleisch committed
701
702
703
704
705
706
707
/**
 * Read tags from file.
 *
 * @param force true to force reading even if tags were already read.
 */
void TagLibFile::readTags(bool force)
{
708
  bool priorIsTagInformationRead = isTagInformationRead();
709
  QString fileName = currentFilePath();
Urs Fleisch's avatar
Urs Fleisch committed
710
  QByteArray fn = QFile::encodeName(fileName);
Urs Fleisch's avatar
Urs Fleisch committed
711

Urs Fleisch's avatar
Urs Fleisch committed
712
  if (force || m_fileRef.isNull()) {
713
714
715
716
717
#if TAGLIB_VERSION >= 0x010800
    delete m_stream;
    m_stream = new FileIOStream(fileName);
    m_fileRef = TagLib::FileRef(FileIOStream::create(m_stream));
#else
718
#if TAGLIB_VERSION > 0x010400 && defined Q_OS_WIN32
Urs Fleisch's avatar
Urs Fleisch committed
719
720
721
722
723
724
    int fnLen = fileName.length();
    wchar_t* fnWs = new wchar_t[fnLen + 1];
    fnWs[fnLen] = 0;
    fileName.toWCharArray(fnWs);
    m_fileRef = TagLib::FileRef(TagLib::FileName(fnWs));
    delete [] fnWs;
725
#else
Urs Fleisch's avatar
Urs Fleisch committed
726
    m_fileRef = TagLib::FileRef(fn);
727
728
#endif
    registerOpenFile(this);
729
#endif
Urs Fleisch's avatar
Urs Fleisch committed
730
731
732
733
734
    m_tagV1 = 0;
    m_tagV2 = 0;
    markTag1Unchanged();
    markTag2Unchanged();
    m_fileRead = true;
735
736

#if TAGLIB_VERSION >= 0x010700
Urs Fleisch's avatar
Urs Fleisch committed
737
738
    m_pictures.clear();
    m_pictures.setRead(false);
739
#endif
Urs Fleisch's avatar
Urs Fleisch committed
740
  }
Urs Fleisch's avatar
Urs Fleisch committed
741

Urs Fleisch's avatar
Urs Fleisch committed
742
743
744
745
  TagLib::File* file;
  if (!m_fileRef.isNull() && (file = m_fileRef.file()) != 0) {
    TagLib::MPEG::File* mpegFile;
    TagLib::FLAC::File* flacFile;
Urs Fleisch's avatar
Urs Fleisch committed
746
#ifdef MPC_ID3V1
Urs Fleisch's avatar
Urs Fleisch committed
747
748
    TagLib::MPC::File* mpcFile;
    TagLib::WavPack::File* wvFile;
Urs Fleisch's avatar
Urs Fleisch committed
749
#endif
Urs Fleisch's avatar
Urs Fleisch committed
750
    TagLib::TrueAudio::File* ttaFile;
751
#if TAGLIB_VERSION >= 0x010700
Urs Fleisch's avatar
Urs Fleisch committed
752
753
    TagLib::APE::File* apeFile;
#endif
754
    m_fileExtension = QLatin1String(".mp3");
755
    m_isTagV1Supported = false;
Urs Fleisch's avatar
Urs Fleisch committed
756
    if ((mpegFile = dynamic_cast<TagLib::MPEG::File*>(file)) != 0) {
757
      m_fileExtension = QLatin1String(".mp3");
758
      m_isTagV1Supported = true;
Urs Fleisch's avatar
Urs Fleisch committed
759
760
761
762
763
      if (!m_tagV1) {
        m_tagV1 = mpegFile->ID3v1Tag();
        markTag1Unchanged();
      }
      if (!m_tagV2) {
764
765
766
767
768
769
770
771
772
773
774
#if TAGLIB_VERSION >= 0x010800
        m_id3v2Version = 0;
        TagLib::ID3v2::Tag* id3v2Tag = mpegFile->ID3v2Tag();
        if (id3v2Tag && !id3v2Tag->isEmpty()) {
          TagLib::ID3v2::Header* header = id3v2Tag->header();
          if (header) {
            m_id3v2Version = header->majorVersion();
          }
        }
        m_tagV2 = id3v2Tag;
#else
Urs Fleisch's avatar
Urs Fleisch committed
775
        m_tagV2 = mpegFile->ID3v2Tag();
776
#endif
Urs Fleisch's avatar
Urs Fleisch committed
777
778
779
        markTag2Unchanged();
      }
    } else if ((flacFile = dynamic_cast<TagLib::FLAC::File*>(file)) != 0) {
780
      m_fileExtension = QLatin1String(".flac");
781
      m_isTagV1Supported = true;
Urs Fleisch's avatar
Urs Fleisch committed
782
783
784
785
786
787
788
789
      if (!m_tagV1) {
        m_tagV1 = flacFile->ID3v1Tag();
        markTag1Unchanged();
      }
      if (!m_tagV2) {
        m_tagV2 = flacFile->xiphComment();
        markTag2Unchanged();
      }
790
#if TAGLIB_VERSION >= 0x010700
Urs Fleisch's avatar
Urs Fleisch committed
791
792
793
794
795
796
797
798
799
800
801
802
      if (!m_pictures.isRead()) {
        TagLib::List<TagLib::FLAC::Picture*> pics(flacFile->pictureList());
        int i = 0;
        for (TagLib::List<TagLib::FLAC::Picture*>::ConstIterator it =
             pics.begin(); it != pics.end(); ++it) {
          PictureFrame frame;
          flacPictureToFrame(*it, frame);
          frame.setIndex(i++);
          m_pictures.append(frame);
        }
        m_pictures.setRead(true);
      }
803
#endif
Urs Fleisch's avatar
Urs Fleisch committed
804
#ifdef MPC_ID3V1
Urs Fleisch's avatar
Urs Fleisch committed
805
    } else if ((mpcFile = dynamic_cast<TagLib::MPC::File*>(file)) != 0) {
806
      m_fileExtension = QLatin1String(".mpc");
807
      m_isTagV1Supported = true;
Urs Fleisch's avatar
Urs Fleisch committed
808
809
810
811
812
813
814
815
816
      if (!m_tagV1) {
        m_tagV1 = mpcFile->ID3v1Tag();
        markTag1Unchanged();
      }
      if (!m_tagV2) {
        m_tagV2 = mpcFile->APETag();
        markTag2Unchanged();
      }
    } else if ((wvFile = dynamic_cast<TagLib::WavPack::File*>(file)) != 0) {
817
      m_fileExtension = QLatin1String(".wv");
818
      m_isTagV1Supported = true;
Urs Fleisch's avatar
Urs Fleisch committed
819
820
821
822
823
824
825
826
827
828
      if (!m_tagV1) {
        m_tagV1 = wvFile->ID3v1Tag();
        markTag1Unchanged();
      }
      if (!m_tagV2) {
        m_tagV2 = wvFile->APETag();
        markTag2Unchanged();
      }
#endif
    } else if ((ttaFile = dynamic_cast<TagLib::TrueAudio::File*>(file)) != 0) {
829
      m_fileExtension = QLatin1String(".tta");
830
      m_isTagV1Supported = true;
Urs Fleisch's avatar
Urs Fleisch committed
831
832
833
834
835
836
837
838
      if (!m_tagV1) {
        m_tagV1 = ttaFile->ID3v1Tag();
        markTag1Unchanged();
      }
      if (!m_tagV2) {
        m_tagV2 = ttaFile->ID3v2Tag();
        markTag2Unchanged();
      }
839
#if TAGLIB_VERSION >= 0x010700
Urs Fleisch's avatar
Urs Fleisch committed
840
    } else if ((apeFile = dynamic_cast<TagLib::APE::File*>(file)) != 0) {
841
      m_fileExtension = QLatin1String(".ape");
842
      m_isTagV1Supported = true;
Urs Fleisch's avatar
Urs Fleisch committed
843
844
845
846
847
848
849
850
851
852
      if (!m_tagV1) {
        m_tagV1 = apeFile->ID3v1Tag();
        markTag1Unchanged();
      }
      if (!m_tagV2) {
        m_tagV2 = apeFile->APETag();
        markTag2Unchanged();
      }
#endif
    } else {
853
      if (dynamic_cast<TagLib::Vorbis::File*>(file) != 0) {
854
        m_fileExtension = QLatin1String(".ogg");
855
      } else if (dynamic_cast<TagLib::Ogg::Speex::File*>(file) != 0) {
856
        m_fileExtension = QLatin1String(".spx");
857
858
#ifndef MPC_ID3V1
      } else if (dynamic_cast<TagLib::MPC::File*>(file) != 0) {
859
        m_fileExtension = QLatin1String(".mpc");
860
      } else if (dynamic_cast<TagLib::WavPack::File*>(file) != 0) {
861
        m_fileExtension = QLatin1String(".wv");
862
863
864
865
#endif
#if TAGLIB_VERSION >= 0x010600
#ifdef TAGLIB_WITH_MP4
      } else if (dynamic_cast<TagLib::MP4::File*>(file) != 0) {
866
        m_fileExtension = QLatin1String(".m4a");
867
868
869
#endif
#ifdef TAGLIB_WITH_ASF
      } else if (dynamic_cast<TagLib::ASF::File*>(file) != 0) {
870
        m_fileExtension = QLatin1String(".wma");
871
872
#endif
      } else if (dynamic_cast<TagLib::RIFF::AIFF::File*>(file) != 0) {
873
        m_fileExtension = QLatin1String(".aiff");
874
      } else if (dynamic_cast<TagLib::RIFF::WAV::File*>(file) != 0) {
875
        m_fileExtension = QLatin1String(".wav");
876
877
878
#endif
#if TAGLIB_VERSION >= 0x010800
      } else if (dynamic_cast<TagLib::Mod::File*>(file) != 0) {
879
        m_fileExtension = QLatin1String(".mod");
880
      } else if (dynamic_cast<TagLib::S3M::File*>(file) != 0) {
881
        m_fileExtension = QLatin1String(".s3m");
882
      } else if (dynamic_cast<TagLib::IT::File*>(file) != 0) {
883
        m_fileExtension = QLatin1String(".it");
884
885
#ifdef HAVE_TAGLIB_XM_SUPPORT
      } else if (dynamic_cast<TagLib::XM::File*>(file) != 0) {
886
        m_fileExtension = QLatin1String(".xm");
887
#endif
888
889
890
891
#endif
#if TAGLIB_VERSION >= 0x010900
      } else if (dynamic_cast<TagLib::Ogg::Opus::File*>(file) != 0) {
        m_fileExtension = QLatin1String(".opus");
892
893
894
895
#endif
#if TAGLIB_VERSION >= 0x010901
      } else if (dynamic_cast<DSFFile*>(file) != 0) {
        m_fileExtension = QLatin1String(".dsf");
896
897
#endif
      }
Urs Fleisch's avatar
Urs Fleisch committed
898
899
900
901
902
903
904
905
906
      m_tagV1 = 0;
      markTag1Unchanged();
      if (!m_tagV2) {
        m_tagV2 = m_fileRef.tag();
        markTag2Unchanged();
      }
    }
  }

907
908
909
910
911
912
  // Cache information, so that it is available after file is closed.
  m_tagInformationRead = true;
  m_hasTagV1 = m_tagV1 && !m_tagV1->isEmpty();
  m_hasTagV2 = m_tagV2 && !m_tagV2->isEmpty();
  m_tagFormatV1 = getTagFormat(m_tagV1, m_tagTypeV1);
  m_tagFormatV2 = getTagFormat(m_tagV2, m_tagTypeV2);
913
  readAudioProperties();
914

Urs Fleisch's avatar
Urs Fleisch committed
915
916
917
  if (force) {
    setFilename(currentFilename());
  }
918
919

  notifyModelDataChanged(priorIsTagInformationRead);
Urs Fleisch's avatar
Urs Fleisch committed
920
921
}

922
923
924
925
926
927
928
929
930
931
932
933
934
/**
 * Close file handle.
 * TagLib keeps the file handle open until the FileRef is destroyed.
 * This causes problems when the operating system has a limited number of
 * open file handles. This method closes the file by assigning a new file
 * reference. Note that this will also invalidate the tag pointers.
 * The file is only closed if there are no unsaved tag changes or if the
 * @a force parameter is set.
 *
 * @param force true to close the file even if tags are changed
 */
void TagLibFile::closeFile(bool force)
{
935
936
937
938
939
940
941
942
943
944
945
946
#if TAGLIB_VERSION >= 0x010800
  if (force) {
    m_fileRef = TagLib::FileRef();
    delete m_stream;
    m_stream = 0;
    m_tagV1 = 0;
    m_tagV2 = 0;
    m_fileRead = false;
  } else if (m_stream) {
    m_stream->closeFileHandle();
  }
#else
947
948
949
950
951
952
953
  if (force || (!isTag1Changed() && !isTag2Changed())) {
    m_fileRef = TagLib::FileRef();
    m_tagV1 = 0;
    m_tagV2 = 0;
    m_fileRead = false;
    deregisterOpenFile(this);
  }
954
#endif
955
956
957
958
959
960
961
962
}

/**
 * Make sure that file is open.
 * This method should be called before accessing m_fileRef, m_tagV1, m_tagV2.
 *
 * @param force true to force reopening of file even if it is already open
 */
Urs Fleisch's avatar
Urs Fleisch committed
963
void TagLibFile::makeFileOpen(bool force) const
964
965
{
  if (!m_fileRead || force) {
Urs Fleisch's avatar
Urs Fleisch committed
966
    const_cast<TagLibFile*>(this)->readTags(force);
967
968
969
  }
}

Urs Fleisch's avatar
Urs Fleisch committed
970
971
972
973
974
975
976
977
978
979
980
/**
 * Write tags to file and rename it if necessary.
 *
 * @param force    true to force writing even if file was not changed.
 * @param renamed  will be set to true if the file was renamed,
 *                 i.e. the file name is no longer valid, else *renamed
 *                 is left unchanged
 * @param preserve true to preserve file time stamps
 *
 * @return true if ok, false if the file could not be written or renamed.
 */
Urs Fleisch's avatar
Urs Fleisch committed
981
bool TagLibFile::writeTags(bool force, bool* renamed, bool preserve)
982
{
983
984
985
986
987
988
989
990
  int id3v2Version;
  if (m_activatedFeatures & TF_ID3v24)
    id3v2Version = 4;
  else if (m_activatedFeatures & TF_ID3v23)
    id3v2Version = 3;
  else
    id3v2Version = 0;
  return writeTags(force, renamed, preserve, id3v2Version);
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
}

/**
 * Write tags to file and rename it if necessary.
 *
 * @param force    true to force writing even if file was not changed.
 * @param renamed  will be set to true if the file was renamed,
 *                 i.e. the file name is no longer valid, else *renamed
 *                 is left unchanged
 * @param preserve true to preserve file time stamps
 * @param id3v2Version ID3v2 version to use, 0 to use existing or preferred,
 *                     3 to force ID3v2.3.0, 4 to force ID3v2.4.0. Is ignored
 *                     if TagLib version is less than 1.8.0.
 *
 * @return true if ok, false if the file could not be written or renamed.
 */
bool TagLibFile::writeTags(bool force, bool* renamed, bool preserve,
                           int id3v2Version)
Urs Fleisch's avatar
Urs Fleisch committed
1009
{
1010
  QString fnStr(currentFilePath());
Urs Fleisch's avatar
Urs Fleisch committed
1011
  if (isChanged() && !QFileInfo(fnStr).isWritable()) {
1012
1013
1014
#if TAGLIB_VERSION >= 0x010800
    closeFile(false);
#endif
Urs Fleisch's avatar
Urs Fleisch committed
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
    return false;
  }

  // store time stamp if it has to be preserved
  QByteArray fn;
  bool setUtime = false;
  struct utimbuf times;
  if (preserve) {
    fn = QFile::encodeName(fnStr);
    struct stat fileStat;
    if (::stat(fn, &fileStat) == 0) {
      times.actime  = fileStat.st_atime;
      times.modtime = fileStat.st_mtime;
      setUtime = true;
    }
  }

  bool fileChanged = false;
  TagLib::File* file;
  if (!m_fileRef.isNull() && (file = m_fileRef.file()) != 0) {
    TagLib::MPEG::File* mpegFile = dynamic_cast<TagLib::MPEG::File*>(file);
    if (mpegFile) {
      if (m_tagV1 && (force || isTag1Changed()) && m_tagV1->isEmpty()) {
        mpegFile->strip(TagLib::MPEG::File::ID3v1);
        fileChanged = true;
        markTag1Unchanged();
        m_tagV1 = 0;
      }
      if (m_tagV2 && (force || isTag2Changed()) && m_tagV2->isEmpty()) {
        mpegFile->strip(TagLib::MPEG::File::ID3v2);
        fileChanged = true;
        markTag2Unchanged();
        m_tagV2 = 0;
      }
      int saveMask = 0;
      if (m_tagV1 && (force || isTag1Changed()) && !m_tagV1->isEmpty()) {
        saveMask |= TagLib::MPEG::File::ID3v1;
      }
      if (m_tagV2 && (force || isTag2Changed()) && !m_tagV2->isEmpty()) {
        saveMask |= TagLib::MPEG::File::ID3v2;
      }
      if (saveMask != 0) {
1057
1058
1059
1060
1061
#if TAGLIB_VERSION >= 0x010800
        if (id3v2Version == 3 || id3v2Version == 4) {
          m_id3v2Version = id3v2Version;
        }
        if (m_id3v2Version != 3 && m_id3v2Version != 4) {
1062
          m_id3v2Version = TagConfig::instance().id3v2Version() ==
1063
              TagConfig::ID3v2_3_0 ? 3 : 4;
1064
1065
1066
1067
1068
1069
1070
        }
#else
        Q_UNUSED(id3v2Version);
#endif
        if (mpegFile->save(saveMask, false
#if TAGLIB_VERSION >= 0x010800
                           , m_id3v2Version
1071
1072
1073
#endif
#if TAGLIB_VERSION >= 0x010900
                           , false
1074
1075
#endif
                           )) {
Urs Fleisch's avatar
Urs Fleisch committed
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
          fileChanged = true;
          if (saveMask & TagLib::MPEG::File::ID3v1) {
            markTag1Unchanged();
          }
          if (saveMask & TagLib::MPEG::File::ID3v2) {
            markTag2Unchanged();
          }
        }
      }
    } else {
      if ((m_tagV2 && (force || isTag2Changed())) ||
          (m_tagV1 && (force || isTag1Changed()))) {
        TagLib::TrueAudio::File* ttaFile = dynamic_cast<TagLib::TrueAudio::File*>(file);
1089
#if TAGLIB_VERSION >= 0x010700
Urs Fleisch's avatar
Urs Fleisch committed
1090
        TagLib::APE::File* apeFile = dynamic_cast<TagLib::APE::File*>(file);
1091
#endif
Urs Fleisch's avatar
Urs Fleisch committed
1092
#ifndef MPC_ID3V1
Urs Fleisch's avatar
Urs Fleisch committed
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
        // it does not work if there is also an ID3 tag (bug in TagLib?)
        TagLib::MPC::File* mpcFile = dynamic_cast<TagLib::MPC::File*>(file);
        TagLib::WavPack::File* wvFile = dynamic_cast<TagLib::WavPack::File*>(file);
        if (mpcFile) {
          mpcFile->remove(TagLib::MPC::File::ID3v1 | TagLib::MPC::File::ID3v2);
          fileChanged = true;
        } else if (wvFile) {
          wvFile->strip(TagLib::WavPack::File::ID3v1);
          fileChanged = true;
        } else
#endif
        if (ttaFile) {
          if (m_tagV1 && (force || isTag1Changed()) && m_tagV1->isEmpty()) {
            ttaFile->strip(TagLib::TrueAudio::File::ID3v1);
            fileChanged = true;
            markTag1Unchanged();
            m_tagV1 = 0;
          }
          if (m_tagV2 && (force || isTag2Changed()) && m_tagV2->isEmpty()) {
            ttaFile->strip(TagLib::TrueAudio::File::ID3v2);
            fileChanged = true;
            markTag2Unchanged();
            m_tagV2 = 0;
          }
        }
1118
#if TAGLIB_VERSION >= 0x010700
Urs Fleisch's avatar
Urs Fleisch committed
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
        if (apeFile) {
          if (m_tagV1 && (force || isTag1Changed()) && m_tagV1->isEmpty()) {
            apeFile->strip(TagLib::APE::File::ID3v1);
            fileChanged = true;
            markTag1Unchanged();
            m_tagV1 = 0;
          }
          if (m_tagV2 && (force || isTag2Changed()) && m_tagV2->isEmpty()) {
            apeFile->strip(TagLib::APE::File::APE);
            fileChanged = true;
            markTag2Unchanged();
            m_tagV2 = 0;
          }
        }
1133
1134
#endif
#if TAGLIB_VERSION >= 0x010700
Urs Fleisch's avatar
Urs Fleisch committed
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
        if (TagLib::FLAC::File* flacFile =
            dynamic_cast<TagLib::FLAC::File*>(file)) {
          flacFile->removePictures();
          foreach (const Frame& frame, m_pictures) {
            TagLib::FLAC::Picture* pic = new TagLib::FLAC::Picture;
            frameToFlacPicture(frame, pic);
            flacFile->addPicture(pic);
          }
        }
#endif
        if (m_fileRef.save()) {
          fileChanged = true;
          markTag1Unchanged();
          markTag2Unchanged();
        }
      }
    }
  }

  // If the file was changed, make sure it is written to disk.
1155
  // This is done when the file is closed. Later the file is opened again.
Urs Fleisch's avatar
Urs Fleisch committed
1156
1157
1158
1159
1160
1161
1162
  // If the file is not properly closed, doubled tags can be
  // written if the file is finally closed!
  // This can be reproduced with an untagged MP3 file, then add
  // an ID3v2 title, save, add an ID3v2 artist, save, reload
  // => double ID3v2 tags.
  // On Windows it is necessary to close the file before renaming it,
  // so it is done even if the file is not changed.
1163
#ifndef Q_OS_WIN32
Urs Fleisch's avatar
Urs Fleisch committed
1164
  if (fileChanged)
1165
#endif
1166
    closeFile(true);
1167

Urs Fleisch's avatar
Urs Fleisch committed
1168
1169
1170
1171
  // restore time stamp
  if (setUtime) {
    ::utime(fn, &times);
  }
Urs Fleisch's avatar
Urs Fleisch committed
1172

1173
  if (isFilenameChanged()) {
Urs Fleisch's avatar
Urs Fleisch committed
1174
1175
1176
    if (!renameFile(currentFilename(), getFilename())) {
      return false;
    }
1177
    markFilenameUnchanged();
Urs Fleisch's avatar
Urs Fleisch committed
1178
1179
    *renamed = true;
  }
1180

1181
#ifndef Q_OS_WIN32
Urs Fleisch's avatar
Urs Fleisch committed
1182
  if (fileChanged)
1183
#endif
1184
    makeFileOpen(true);
Urs Fleisch's avatar
Urs Fleisch committed
1185
  return true;
Urs Fleisch's avatar
Urs Fleisch committed
1186
1187
1188
}

/**
1189
 * Remove ID3v1 frames.
Urs Fleisch's avatar
Urs Fleisch committed
1190
 *
1191
 * @param flt filter specifying which frames to remove
Urs Fleisch's avatar
Urs Fleisch committed
1192
 */
1193
void TagLibFile::deleteFramesV1(const FrameFilter& flt)
Urs Fleisch's avatar
Urs Fleisch committed
1194
{
1195
  makeFileOpen();
Urs Fleisch's avatar
Urs Fleisch committed
1196
1197
1198
  if (m_tagV1) {
    TaggedFile::deleteFramesV1(flt);
  }
Urs Fleisch's avatar
Urs Fleisch committed
1199
1200
1201
1202
1203
1204
1205
1206
1207
}

/**
 * Get ID3v1 title.
 *
 * @return string,
 *         "" if the field does not exist,
 *         QString::null if the tags do not exist.
 */
Urs Fleisch's avatar
Urs Fleisch committed
1208
QString TagLibFile::getTitleV1() const
Urs Fleisch's avatar
Urs Fleisch committed
1209
{
1210
  makeFileOpen();
Urs Fleisch's avatar
Urs Fleisch committed
1211
1212
  if (m_tagV1) {
    TagLib::String str = m_tagV1->title();
1213
    return str.isNull() ? QLatin1String("") : toQString(str);
Urs Fleisch's avatar
Urs Fleisch committed
1214
  } else {
1215
    return QString();
Urs Fleisch's avatar
Urs Fleisch committed
1216
  }
Urs Fleisch's avatar
Urs Fleisch committed
1217
1218
1219
1220
1221
1222
1223
1224
1225
}

/**
 * Get ID3v1 artist.
 *
 * @return string,
 *         "" if the field does not exist,
 *         QString::null if the tags do not exist.
 */
Urs Fleisch's avatar
Urs Fleisch committed
1226
QString TagLibFile::getArtistV1() const
Urs Fleisch's avatar
Urs Fleisch committed
1227
{
1228
  makeFileOpen();
Urs Fleisch's avatar
Urs Fleisch committed
1229
1230
  if (m_tagV1) {
    TagLib::String str = m_tagV1->artist();
1231
    return str.isNull() ? QLatin1String("") : toQString(str);
Urs Fleisch's avatar
Urs Fleisch committed
1232
  } else {
1233
    return QString();
Urs Fleisch's avatar
Urs Fleisch committed
1234
  }
Urs Fleisch's avatar
Urs Fleisch committed
1235
1236
1237
1238
1239
1240
1241
1242
1243
}

/**
 * Get ID3v1 album.
 *
 * @return string,
 *         "" if the field does not exist,
 *         QString::null if the tags do not exist.
 */
Urs Fleisch's avatar
Urs Fleisch committed
1244
QString TagLibFile::getAlbumV1() const
Urs Fleisch's avatar
Urs Fleisch committed
1245
{
1246
  makeFileOpen();
Urs Fleisch's avatar
Urs Fleisch committed
1247
1248
  if (m_tagV1) {
    TagLib::String str = m_tagV1->album();
1249
    return str.isNull() ? QLatin1String("") : toQString(str);
Urs Fleisch's avatar
Urs Fleisch committed
1250
  } else {
1251
    return QString();
Urs Fleisch's avatar
Urs Fleisch committed
1252
  }
Urs Fleisch's avatar
Urs Fleisch committed
1253
1254
1255
1256
1257
1258
1259
1260
1261
}

/**
 * Get ID3v1 comment.
 *
 * @return string,
 *         "" if the field does not exist,
 *         QString::null if the tags do not exist.
 */
Urs Fleisch's avatar
Urs Fleisch committed
1262
QString TagLibFile::getCommentV1() const
Urs Fleisch's avatar
Urs Fleisch committed
1263
{
1264
  makeFileOpen();
Urs Fleisch's avatar
Urs Fleisch committed
1265
1266
1267
  if (m_tagV1) {
    TagLib::String str = m_tagV1->comment();
    if (str.isNull()) {
1268
      return QLatin1String("");
Urs Fleisch's avatar
Urs Fleisch committed
1269
    } else {
1270
      QString qstr(toQString(str));
Urs Fleisch's avatar
Urs Fleisch committed
1271
1272
1273
1274
      qstr.truncate(28);
      return qstr;
    }
  } else {
1275
    return QString();
Urs Fleisch's avatar
Urs Fleisch committed
1276
  }
Urs Fleisch's avatar
Urs Fleisch committed
1277
1278
1279
1280
1281
1282
1283
1284
1285
}

/**
 * Get ID3v1 year.
 *
 * @return number,
 *         0 if the field does not exist,
 *         -1 if the tags do not exist.
 */
Urs Fleisch's avatar
Urs Fleisch committed
1286
int TagLibFile::getYearV1() const
Urs Fleisch's avatar
Urs Fleisch committed
1287
{
1288
  makeFileOpen();
Urs Fleisch's avatar
Urs Fleisch committed
1289
1290
1291
1292
1293
  if (m_tagV1) {
    return m_tagV1->year();
  } else {
    return -1;
  }
Urs Fleisch's avatar
Urs Fleisch committed
1294
1295
1296
1297
1298
1299
1300
1301
1302
}

/**
 * Get ID3v1 track.
 *
 * @return number,
 *         0 if the field does not exist,
 *         -1 if the tags do not exist.
 */
Urs Fleisch's avatar
Urs Fleisch committed
1303
int TagLibFile::getTrackNumV1() const
Urs Fleisch's avatar
Urs Fleisch committed
1304
{
1305
  makeFileOpen();
Urs Fleisch's avatar
Urs Fleisch committed
1306
1307
1308
1309
1310
  if (m_tagV1) {
    return m_tagV1->track();
  } else {
    return -1;
  }
Urs Fleisch's avatar
Urs Fleisch committed
1311
1312
1313
1314
1315
}

/**
 * Get ID3v1 genre.
 *
1316
1317
1318
 * @return string,
 *         "" if the field does not exist,
 *         QString::null if the tags do not exist.
Urs Fleisch's avatar
Urs Fleisch committed
1319
 */
Urs Fleisch's avatar
Urs Fleisch committed
1320
QString TagLibFile::getGenreV1() const
Urs Fleisch's avatar
Urs Fleisch committed
1321
{
1322
  makeFileOpen();
Urs Fleisch's avatar
Urs Fleisch committed
1323
1324
  if (m_tagV1) {
    TagLib::String str = m_tagV1->genre();
1325
    return str.isNull() ? QLatin1String("") : toQString(str);
Urs Fleisch's avatar
Urs Fleisch committed
1326
  } else {
1327
    return QString();
Urs Fleisch's avatar
Urs Fleisch committed
1328
  }
Urs Fleisch's avatar
Urs Fleisch committed
1329
1330
1331
1332
1333
1334
1335
1336
1337
}

/**
 * Get ID3v2 title.
 *
 * @return string,
 *         "" if the field does not exist,
 *         QString::null if the tags do not exist.
 */
Urs Fleisch's avatar
Urs Fleisch committed
1338
QString TagLibFile::getTitleV2() const
Urs Fleisch's avatar
Urs Fleisch committed
1339
{
1340
  makeFileOpen();
Urs Fleisch's avatar
Urs Fleisch committed
1341
1342
  if (m_tagV2) {
    TagLib::String str = m_tagV2->title();
1343
    return str.isNull() ? QLatin1String("") : toQString(str);
Urs Fleisch's avatar
Urs Fleisch committed
1344
  } else {
1345
    return QString();
Urs Fleisch's avatar
Urs Fleisch committed
1346
  }
Urs Fleisch's avatar
Urs Fleisch committed
1347
1348
1349
1350
1351
1352
1353
1354
1355
}

/**
 * Get ID3v2 artist.
 *
 * @return string,
 *         "" if the field does not exist,
 *         QString::null if the tags do not exist.
 */
Urs Fleisch's avatar
Urs Fleisch committed
1356
QString TagLibFile::getArtistV2() const
Urs Fleisch's avatar
Urs Fleisch committed
1357
{
1358
  makeFileOpen();
Urs Fleisch's avatar
Urs Fleisch committed
1359
1360
  if