From b74aabfaa119d243ff1421cf1bd46b6c338c0818 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Sat, 3 Oct 2009 02:46:32 +0200 Subject: [PATCH] tag editor: performer, composer and disc tags support for ogg and flac files --- src/tag_editor.cpp | 36 +++++++++++++++++++++++++++++++++--- src/tag_editor.h | 2 ++ src/tiny_tag_editor.cpp | 14 +++++++++++++- src/tiny_tag_editor.h | 5 +++++ 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/tag_editor.cpp b/src/tag_editor.cpp index a028332f..bde35653 100644 --- a/src/tag_editor.cpp +++ b/src/tag_editor.cpp @@ -25,9 +25,12 @@ #include #include +// taglib includes #include "id3v2tag.h" #include "textidentificationframe.h" #include "mpegfile.h" +#include "vorbisfile.h" +#include "flacfile.h" #include "charset.h" #include "display.h" @@ -920,6 +923,23 @@ namespace } } +void TagEditor::WriteXiphComments(const MPD::Song &s, TagLib::Ogg::XiphComment *tag) +{ + TagLib::StringList list; + + tag->addField("DISCNUMBER", ToWString(s.GetDisc())); // disc + + tag->removeField("COMPOSER"); // composer + GetTagList(list, s.GetComposer()); + for (TagLib::StringList::ConstIterator it = list.begin(); it != list.end(); ++it) + tag->addField("COMPOSER", *it, 0); + + tag->removeField("PERFORMER"); // performer + GetTagList(list, s.GetPerformer()); + for (TagLib::StringList::ConstIterator it = list.begin(); it != list.end(); ++it) + tag->addField("PERFORMER", *it, 0); +} + bool TagEditor::WriteTags(MPD::Song &s) { std::string path_to_file; @@ -938,9 +958,9 @@ bool TagEditor::WriteTags(MPD::Song &s) f.tag()->setTrack(StrToInt(s.GetTrack())); f.tag()->setGenre(ToWString(s.GetGenre())); f.tag()->setComment(ToWString(s.GetComment())); - if (TagLib::MPEG::File *file = dynamic_cast(f.file())) + if (TagLib::MPEG::File *mp3_file = dynamic_cast(f.file())) { - TagLib::ID3v2::Tag *tag = file->ID3v2Tag(); + TagLib::ID3v2::Tag *tag = mp3_file->ID3v2Tag(1); TagLib::StringList list; WriteID3v2("TIT2", tag, ToWString(s.GetTitle())); // title @@ -957,6 +977,14 @@ bool TagEditor::WriteTags(MPD::Song &s) GetTagList(list, s.GetPerformer()); WriteID3v2("TOPE", tag, list); // performer } + else if (TagLib::Ogg::Vorbis::File *ogg_file = dynamic_cast(f.file())) + { + WriteXiphComments(s, ogg_file->tag()); + } + else if (TagLib::FLAC::File *flac_file = dynamic_cast(f.file())) + { + WriteXiphComments(s, flac_file->xiphComment(1)); + } if (!f.save()) return false; @@ -1062,7 +1090,9 @@ void TagEditor::GetTagList(TagLib::StringList &list, const std::string &s) for (size_t i = 0; i != std::string::npos; i = s.find(",", i)) { if (i) - i++; + ++i; + while (s[i] == ' ') + ++i; size_t j = s.find(",", i); list.append(TagLib::String(s.substr(i, j-i), TagLib::String::UTF8)); } diff --git a/src/tag_editor.h b/src/tag_editor.h index fbc70393..b535ae6e 100644 --- a/src/tag_editor.h +++ b/src/tag_editor.h @@ -30,6 +30,7 @@ #include // taglib headers +#include "xiphcomment.h" #include "fileref.h" #include "tag.h" @@ -96,6 +97,7 @@ class TagEditor : public Screen static void CapitalizeFirstLetters(MPD::Song &); static void LowerAllLetters(MPD::Song &); static void GetTagList(TagLib::StringList &, const std::string &); + static void WriteXiphComments(const MPD::Song &, TagLib::Ogg::XiphComment *); static void GetPatternList(); static void SavePatternList(); diff --git a/src/tiny_tag_editor.cpp b/src/tiny_tag_editor.cpp index dc207df1..3fbe9a31 100644 --- a/src/tiny_tag_editor.cpp +++ b/src/tiny_tag_editor.cpp @@ -22,6 +22,11 @@ #ifdef HAVE_TAGLIB_H +// taglib includes +#include "mpegfile.h" +#include "vorbisfile.h" +#include "flacfile.h" + #include "browser.h" #include "charset.h" #include "display.h" @@ -281,7 +286,7 @@ bool TinyTagEditor::GetTags() w->IntoSeparator(18); w->IntoSeparator(20); - if (ext != "mp3") + if (!extendedTagsSupported(f.file())) for (size_t i = 14; i <= 16; ++i) w->Static(i, 1); @@ -324,5 +329,12 @@ bool TinyTagEditor::GetTags() return true; } +bool TinyTagEditor::extendedTagsSupported(TagLib::File *f) +{ + return dynamic_cast(f) + || dynamic_cast(f) + || dynamic_cast(f); +} + #endif // HAVE_TAGLIB_H diff --git a/src/tiny_tag_editor.h b/src/tiny_tag_editor.h index 61bc4e02..140dd286 100644 --- a/src/tiny_tag_editor.h +++ b/src/tiny_tag_editor.h @@ -27,6 +27,9 @@ #ifdef HAVE_TAGLIB_H +// taglib includes +#include "tfile.h" + #include "screen.h" class TinyTagEditor : public Screen< Menu > @@ -53,6 +56,8 @@ class TinyTagEditor : public Screen< Menu > private: bool GetTags(); MPD::Song itsEdited; + + static bool extendedTagsSupported(TagLib::File *); }; extern TinyTagEditor *myTinyTagEditor;