MythTV  0.28pre
 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 
44 bool MetaIOOggVorbis::write(const QString &filename, MusicMetadata* mdata)
45 {
46  if (!mdata)
47  return false;
48 
50 
51  if (m_filename.isEmpty())
52  return false;
53 
54  TagLib::Ogg::Vorbis::File *oggfile = OpenFile(m_filename);
55 
56  if (!oggfile)
57  return false;
58 
59  TagLib::Ogg::XiphComment *tag = oggfile->tag();
60 
61  if (!tag)
62  {
63  delete oggfile;
64  return false;
65  }
66 
67  WriteGenericMetadata(tag, mdata);
68 
69  // Compilation
70  if (mdata->Compilation())
71  {
72  tag->addField("MUSICBRAINZ_ALBUMARTISTID",
73  MYTH_MUSICBRAINZ_ALBUMARTIST_UUID, true);
74  tag->addField("COMPILATION_ARTIST",
75  QStringToTString(mdata->CompilationArtist()), true);
76  }
77  else
78  {
79  // Don't remove the musicbrainz field unless it indicated a compilation
80  if (tag->contains("MUSICBRAINZ_ALBUMARTISTID") &&
81  (tag->fieldListMap()["MUSICBRAINZ_ALBUMARTISTID"].toString() ==
82  MYTH_MUSICBRAINZ_ALBUMARTIST_UUID))
83  {
84  tag->removeField("MUSICBRAINZ_ALBUMARTISTID");
85  }
86  tag->removeField("COMPILATION_ARTIST");
87  }
88 
90  bool result = oggfile->save();
92 
93  if (oggfile)
94  delete oggfile;
95 
96  return (result);
97 }
98 
103 {
104  TagLib::Ogg::Vorbis::File *oggfile = OpenFile(filename);
105 
106  if (!oggfile)
107  return NULL;
108 
109  TagLib::Ogg::XiphComment *tag = oggfile->tag();
110 
111  if (!tag)
112  {
113  delete oggfile;
114  return NULL;
115  }
116 
117  MusicMetadata *metadata = new MusicMetadata(filename);
118 
119  ReadGenericMetadata(tag, metadata);
120 
121  bool compilation = false;
122 
123  if (tag->contains("COMPILATION_ARTIST"))
124  {
125  QString compilation_artist = TStringToQString(
126  tag->fieldListMap()["COMPILATION_ARTIST"].toString()).trimmed();
127  if (compilation_artist != metadata->Artist())
128  {
129  metadata->setCompilationArtist(compilation_artist);
130  compilation = true;
131  }
132  }
133 
134  if (!compilation && tag->contains("MUSICBRAINZ_ALBUMARTISTID"))
135  {
136  QString musicbrainzcode = TStringToQString(
137  tag->fieldListMap()["MUSICBRAINZ_ALBUMARTISTID"].toString()).trimmed();
138  if (musicbrainzcode == MYTH_MUSICBRAINZ_ALBUMARTIST_UUID)
139  compilation = true;
140  }
141 
142  metadata->setCompilation(compilation);
143 
144  if (metadata->Length() <= 0)
145  metadata->setLength(getTrackLength(oggfile));
146 
147  delete oggfile;
148 
149  return metadata;
150 }