MythTV  master
metaiooggvorbis.cpp
Go to the documentation of this file.
1 
2 // libmythmetadata
3 #include "metaiooggvorbis.h"
4 #include "musicmetadata.h"
5 #include "musicutils.h"
6 
7 // Libmyth
8 #include <mythcontext.h>
9 
16 TagLib::Ogg::Vorbis::File *MetaIOOggVorbis::OpenFile(const QString &filename)
17 {
18  QByteArray fname = filename.toLocal8Bit();
19  TagLib::Ogg::Vorbis::File *oggfile =
20  new TagLib::Ogg::Vorbis::File(fname.constData());
21 
22  if (!oggfile->isOpen())
23  {
24  delete oggfile;
25  oggfile = nullptr;
26  }
27 
28  return oggfile;
29 }
30 
31 
35 bool MetaIOOggVorbis::write(const QString &filename, MusicMetadata* mdata)
36 {
37  if (!mdata)
38  return false;
39 
40  m_filename = filename;
41 
42  if (m_filename.isEmpty())
43  return false;
44 
45  TagLib::Ogg::Vorbis::File *oggfile = OpenFile(m_filename);
46 
47  if (!oggfile)
48  return false;
49 
50  TagLib::Ogg::XiphComment *tag = oggfile->tag();
51 
52  if (!tag)
53  {
54  delete oggfile;
55  return false;
56  }
57 
58  WriteGenericMetadata(tag, mdata);
59 
60  // Compilation
61  if (mdata->Compilation())
62  {
63  tag->addField("MUSICBRAINZ_ALBUMARTISTID",
65  tag->addField("COMPILATION_ARTIST",
66  QStringToTString(mdata->CompilationArtist()), true);
67  }
68  else
69  {
70  // Don't remove the musicbrainz field unless it indicated a compilation
71  if (tag->contains("MUSICBRAINZ_ALBUMARTISTID") &&
72  (tag->fieldListMap()["MUSICBRAINZ_ALBUMARTISTID"].toString() ==
74  {
75  tag->removeField("MUSICBRAINZ_ALBUMARTISTID");
76  }
77  tag->removeField("COMPILATION_ARTIST");
78  }
79 
81  bool result = oggfile->save();
83 
84  delete oggfile;
85 
86  return (result);
87 }
88 
92 MusicMetadata* MetaIOOggVorbis::read(const QString &filename)
93 {
94  TagLib::Ogg::Vorbis::File *oggfile = OpenFile(filename);
95 
96  if (!oggfile)
97  return nullptr;
98 
99  TagLib::Ogg::XiphComment *tag = oggfile->tag();
100 
101  if (!tag)
102  {
103  delete oggfile;
104  return nullptr;
105  }
106 
107  MusicMetadata *metadata = new MusicMetadata(filename);
108 
109  ReadGenericMetadata(tag, metadata);
110 
111  bool compilation = false;
112 
113  if (tag->contains("COMPILATION_ARTIST"))
114  {
115  QString compilation_artist = TStringToQString(
116  tag->fieldListMap()["COMPILATION_ARTIST"].toString()).trimmed();
117  if (compilation_artist != metadata->Artist())
118  {
119  metadata->setCompilationArtist(compilation_artist);
120  compilation = true;
121  }
122  }
123 
124  if (!compilation && tag->contains("MUSICBRAINZ_ALBUMARTISTID"))
125  {
126  QString musicbrainzcode = TStringToQString(
127  tag->fieldListMap()["MUSICBRAINZ_ALBUMARTISTID"].toString()).trimmed();
128  if (musicbrainzcode == MYTH_MUSICBRAINZ_ALBUMARTIST_UUID)
129  compilation = true;
130  }
131 
132  metadata->setCompilation(compilation);
133 
134  if (metadata->Length() <= 0)
135  metadata->setLength(getTrackLength(oggfile));
136 
137  delete oggfile;
138 
139  return metadata;
140 }
void setLength(int llength)
int Length() const
#define TStringToQString(s)
Definition: musicutils.h:16
void WriteGenericMetadata(TagLib::Tag *tag, const MusicMetadata *metadata)
Writes metadata common to all tag formats to the tag.
void setCompilation(bool state)
QString Artist() const
void saveTimeStamps(void)
Definition: metaio.cpp:218
void ReadGenericMetadata(TagLib::Tag *tag, MusicMetadata *metadata)
Writes metadata common to all tag formats to the tag.
TagLib::Ogg::Vorbis::File * OpenFile(const QString &filename)
Open the file to read the tag.
bool Compilation() const
int getTrackLength(TagLib::File *file)
Find the length of the track (in seconds)
QString m_filename
Definition: metaio.h:181
bool write(const QString &filename, MusicMetadata *mdata) override
Writes all metadata back to a file.
QString CompilationArtist() const
MusicMetadata * read(const QString &filename) override
Reads MusicMetadata from a file.
#define MYTH_MUSICBRAINZ_ALBUMARTIST_UUID
Definition: metaio.h:15
void setCompilationArtist(const QString &lcompilation_artist, const QString &lcompilation_artist_sort=nullptr)
#define QStringToTString(s)
Definition: musicutils.h:14
void restoreTimeStamps(void)
Definition: metaio.cpp:227