MythTV  0.27pre
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
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 
11  : MetaIOTagLib()
12 {
13 }
14 
16 {
17 }
18 
25 TagLib::Ogg::Vorbis::File *MetaIOOggVorbis::OpenFile(const QString &filename)
26 {
27  QByteArray fname = filename.toLocal8Bit();
28  TagLib::Ogg::Vorbis::File *oggfile =
29  new TagLib::Ogg::Vorbis::File(fname.constData());
30 
31  if (!oggfile->isOpen())
32  {
33  delete oggfile;
34  oggfile = NULL;
35  }
36 
37  return oggfile;
38 }
39 
40 
45 {
46  if (!mdata)
47  return false;
48 
49  TagLib::Ogg::Vorbis::File *oggfile = OpenFile(mdata->Filename());
50 
51  if (!oggfile)
52  return false;
53 
54  TagLib::Ogg::XiphComment *tag = oggfile->tag();
55 
56  if (!tag)
57  {
58  delete oggfile;
59  return false;
60  }
61 
62  WriteGenericMetadata(tag, mdata);
63 
64  // Compilation
65  if (mdata->Compilation())
66  {
67  tag->addField("MUSICBRAINZ_ALBUMARTISTID",
68  MYTH_MUSICBRAINZ_ALBUMARTIST_UUID, true);
69  tag->addField("COMPILATION_ARTIST",
70  QStringToTString(mdata->CompilationArtist()), true);
71  }
72  else
73  {
74  // Don't remove the musicbrainz field unless it indicated a compilation
75  if (tag->contains("MUSICBRAINZ_ALBUMARTISTID") &&
76  (tag->fieldListMap()["MUSICBRAINZ_ALBUMARTISTID"].toString() ==
77  MYTH_MUSICBRAINZ_ALBUMARTIST_UUID))
78  {
79  tag->removeField("MUSICBRAINZ_ALBUMARTISTID");
80  }
81  tag->removeField("COMPILATION_ARTIST");
82  }
83 
84  bool result = oggfile->save();
85 
86  if (oggfile)
87  delete oggfile;
88 
89  return (result);
90 }
91 
95 MusicMetadata* MetaIOOggVorbis::read(const QString &filename)
96 {
97  TagLib::Ogg::Vorbis::File *oggfile = OpenFile(filename);
98 
99  if (!oggfile)
100  return NULL;
101 
102  TagLib::Ogg::XiphComment *tag = oggfile->tag();
103 
104  if (!tag)
105  {
106  delete oggfile;
107  return NULL;
108  }
109 
110  MusicMetadata *metadata = new MusicMetadata(filename);
111 
112  ReadGenericMetadata(tag, metadata);
113 
114  bool compilation = false;
115 
116  if (tag->contains("COMPILATION_ARTIST"))
117  {
118  QString compilation_artist = TStringToQString(
119  tag->fieldListMap()["COMPILATION_ARTIST"].toString()).trimmed();
120  if (compilation_artist != metadata->Artist())
121  {
122  metadata->setCompilationArtist(compilation_artist);
123  compilation = true;
124  }
125  }
126 
127  if (!compilation && tag->contains("MUSICBRAINZ_ALBUMARTISTID"))
128  {
129  QString musicbrainzcode = TStringToQString(
130  tag->fieldListMap()["MUSICBRAINZ_ALBUMARTISTID"].toString()).trimmed();
131  if (musicbrainzcode == MYTH_MUSICBRAINZ_ALBUMARTIST_UUID)
132  compilation = true;
133  }
134 
135  metadata->setCompilation(compilation);
136 
137  if (metadata->Length() <= 0)
138  metadata->setLength(getTrackLength(oggfile));
139 
140  delete oggfile;
141 
142  return metadata;
143 }