diff --git a/src/charset.cpp b/src/charset.cpp index 91206812..cd9f42ce 100644 --- a/src/charset.cpp +++ b/src/charset.cpp @@ -82,8 +82,7 @@ namespace *outbuf = 0; if (delete_old) delete [] instart; - inbuf = strdup(outstart); - delete [] outstart; + inbuf = outstart; } } diff --git a/src/conv.cpp b/src/conv.cpp index 6852aebf..b814ec68 100644 --- a/src/conv.cpp +++ b/src/conv.cpp @@ -69,8 +69,6 @@ std::string IntoStr(mpd_tag_type tag) // this is only for left column's title in return "Comment"; case MPD_TAG_DISC: return "Disc"; - case MPD_TAG_FILE: - return "Filename"; default: return ""; } @@ -170,8 +168,6 @@ MPD::Song::SetFunction IntoSetFunction(mpd_tag_type tag) return &MPD::Song::SetComment; case MPD_TAG_DISC: return &MPD::Song::SetDisc; - case MPD_TAG_FILE: - return &MPD::Song::SetNewName; default: return 0; } diff --git a/src/media_library.cpp b/src/media_library.cpp index 0ea535b4..e9a8cfce 100644 --- a/src/media_library.cpp +++ b/src/media_library.cpp @@ -197,13 +197,14 @@ void MediaLibrary::Update() // 13) { - TagList noalbum_list; - Mpd.StartFieldSearch(MPD_TAG_FILE); + SongList noalbum_list; + Mpd.StartSearch(1); Mpd.AddSearch(Config.media_lib_primary_tag, Artists->Current()); Mpd.AddSearch(MPD_TAG_ALBUM, ""); Mpd.CommitSearch(noalbum_list); if (!noalbum_list.empty()) Albums->AddOption(std::make_pair("", SearchConstraints("", ""))); + FreeSongList(noalbum_list); } for (TagList::const_iterator it = list.begin(); it != list.end(); ++it) diff --git a/src/mpdpp.cpp b/src/mpdpp.cpp index 6286fa90..54e761fb 100644 --- a/src/mpdpp.cpp +++ b/src/mpdpp.cpp @@ -467,7 +467,7 @@ bool Connection::AddRandomSongs(size_t number) TagList files; mpd_send_list_all(itsConnection, "/"); - while (mpd_pair *item = mpd_recv_pair_tag(itsConnection, MPD_TAG_FILE)) + while (mpd_pair *item = mpd_recv_pair_named(itsConnection, "file")) { files.push_back(item->value); mpd_return_pair(itsConnection, item); @@ -598,7 +598,7 @@ void Connection::GetAlbums(const std::string &artist, TagList &v) const { mpd_search_db_tags(itsConnection, MPD_TAG_ALBUM); if (!artist.empty()) - mpd_search_add_constraint(itsConnection, MPD_TAG_ARTIST, artist.c_str()); + mpd_search_add_tag_constraint(itsConnection, MPD_OPERATOR_DEFAULT, MPD_TAG_ARTIST, artist.c_str()); mpd_search_commit(itsConnection); while (mpd_pair *item = mpd_recv_pair_tag(itsConnection, MPD_TAG_ALBUM)) { @@ -631,7 +631,7 @@ void Connection::AddSearch(mpd_tag_type item, const std::string &str) const if (Version() < 14 && str.empty()) return; if (isConnected) - mpd_search_add_constraint(itsConnection, item, str.c_str()); + mpd_search_add_tag_constraint(itsConnection, MPD_OPERATOR_DEFAULT, item, str.c_str()); } void Connection::CommitSearch(SongList &v) const diff --git a/src/song.cpp b/src/song.cpp index baae66e3..b6bedc06 100644 --- a/src/song.cpp +++ b/src/song.cpp @@ -45,6 +45,7 @@ namespace MPD::Song::Song(mpd_song *s, bool copy_ptr) : itsSong(s), itsFile(0), + itsTags(0), itsSlash(std::string::npos), itsHash(0), copyPtr(copy_ptr), @@ -54,15 +55,15 @@ MPD::Song::Song(mpd_song *s, bool copy_ptr) : itsSong(s), SetHashAndSlash(); } -MPD::Song::Song(const Song &s) : itsSong(0), +MPD::Song::Song(const Song &s) : itsSong(s.copyPtr ? s.itsSong : mpd_song_dup(s.itsSong)), itsFile(s.itsFile ? strdup(s.itsFile) : 0), + itsTags(s.itsTags ? new TagMap(*s.itsTags) : 0), itsNewName(s.itsNewName), itsSlash(s.itsSlash), itsHash(s.itsHash), copyPtr(s.copyPtr), isLocalised(s.isLocalised) { - itsSong = s.copyPtr ? s.itsSong : mpd_song_dup(s.itsSong); } MPD::Song::~Song() @@ -70,6 +71,7 @@ MPD::Song::~Song() if (itsSong) mpd_song_free(itsSong); delete [] itsFile; + delete itsTags; } std::string MPD::Song::GetLength() const @@ -93,13 +95,16 @@ void MPD::Song::Localize() } for (unsigned t = MPD_TAG_ARTIST; t <= MPD_TAG_DISC; ++t) { - tag = conv_tag = mpd_song_get_tag(itsSong, mpd_tag_type(t), 0); - utf_to_locale(conv_tag, 0); - if (tag != conv_tag) // tag has been converted + unsigned pos = 0; + for (; (tag = mpd_song_get_tag(itsSong, mpd_tag_type(t), pos)); ++pos) { - mpd_song_clear_tag(itsSong, mpd_tag_type(t)); - mpd_song_add_tag(itsSong, mpd_tag_type(t), conv_tag); - delete [] conv_tag; + conv_tag = tag; + utf_to_locale(conv_tag, 0); + if (tag != conv_tag) // tag has been converted + { + SetTag(mpd_tag_type(t), pos, conv_tag); + delete [] conv_tag; + } } } isLocalised = 1; @@ -110,9 +115,14 @@ void MPD::Song::Clear() { if (itsSong) mpd_song_free(itsSong); + itsSong = 0; + delete [] itsFile; itsFile = 0; - itsSong = 0; + + delete itsTags; + itsTags = 0; + itsNewName.clear(); itsSlash = std::string::npos; itsHash = 0; @@ -142,7 +152,8 @@ std::string MPD::Song::GetFile() const std::string MPD::Song::GetName() const { - if (const char *name = mpd_song_get_tag(itsSong, MPD_TAG_NAME, 0)) + std::string name = GetTag(MPD_TAG_NAME, 0); + if (!name.empty()) return name; else if (itsSlash != std::string::npos) return MyFilename()+itsSlash+1; @@ -162,184 +173,126 @@ std::string MPD::Song::GetDirectory() const std::string MPD::Song::GetArtist() const { - if (const char *artist = mpd_song_get_tag(itsSong, MPD_TAG_ARTIST, 0)) - return artist; - else - return ""; + return GetTag(MPD_TAG_ARTIST, 0); } std::string MPD::Song::GetTitle() const { - if (const char *title = mpd_song_get_tag(itsSong, MPD_TAG_TITLE, 0)) - return title; - else - return ""; + return GetTag(MPD_TAG_TITLE, 0); } std::string MPD::Song::GetAlbum() const { - if (const char *album = mpd_song_get_tag(itsSong, MPD_TAG_ALBUM, 0)) - return album; - else - return ""; + return GetTag(MPD_TAG_ALBUM, 0); } std::string MPD::Song::GetTrack() const { - const char *track = mpd_song_get_tag(itsSong, MPD_TAG_TRACK, 0); - if (!track) - return ""; - else if (track[0] != '0' && !track[1]) - return "0"+std::string(track); - else - return track; + std::string track = GetTag(MPD_TAG_TRACK, 0); + return track.length() == 1 && track[0] != '0' ? "0"+track : track; } std::string MPD::Song::GetTrackNumber() const { - const char *track = mpd_song_get_tag(itsSong, MPD_TAG_TRACK, 0); - if (!track) - return ""; - const char *slash = strrchr(track, '/'); - if (slash) + std::string track = GetTag(MPD_TAG_TRACK, 0); + size_t slash = track.find('/'); + if (slash != std::string::npos) { - std::string result(track, slash-track); - return result[0] != '0' && result.length() == 1 ? "0"+result : result; + track = track.substr(slash+1); + return track.length() == 1 && track[0] != '0' ? "0"+track : track; } else - return GetTrack(); + return track; } std::string MPD::Song::GetDate() const { - if (const char *date = mpd_song_get_tag(itsSong, MPD_TAG_DATE, 0)) - return date; - else - return ""; + return GetTag(MPD_TAG_DATE, 0); } std::string MPD::Song::GetGenre() const { - if (const char *genre = mpd_song_get_tag(itsSong, MPD_TAG_GENRE, 0)) - return genre; - else - return ""; + return GetTag(MPD_TAG_GENRE, 0); } std::string MPD::Song::GetComposer() const { - if (const char *composer = mpd_song_get_tag(itsSong, MPD_TAG_COMPOSER, 0)) - return composer; - else - return ""; + return GetTag(MPD_TAG_COMPOSER, 0); } std::string MPD::Song::GetPerformer() const { - if (const char *performer = mpd_song_get_tag(itsSong, MPD_TAG_PERFORMER, 0)) - return performer; - else - return ""; + return GetTag(MPD_TAG_PERFORMER, 0); } std::string MPD::Song::GetDisc() const { - if (const char *disc = mpd_song_get_tag(itsSong, MPD_TAG_DISC, 0)) - return disc; - else - return ""; + return GetTag(MPD_TAG_DISC, 0); } std::string MPD::Song::GetComment() const { - if (const char *comment = mpd_song_get_tag(itsSong, MPD_TAG_COMMENT, 0)) - return comment; - else - return ""; + return GetTag(MPD_TAG_COMMENT, 0); } void MPD::Song::SetArtist(const std::string &str) { - mpd_song_clear_tag(itsSong, MPD_TAG_ARTIST); - if (!str.empty()) - mpd_song_add_tag(itsSong, MPD_TAG_ARTIST, str.c_str()); + SetTag(MPD_TAG_ARTIST, 0, str); } void MPD::Song::SetTitle(const std::string &str) { - mpd_song_clear_tag(itsSong, MPD_TAG_TITLE); - if (!str.empty()) - mpd_song_add_tag(itsSong, MPD_TAG_TITLE, str.c_str()); + SetTag(MPD_TAG_TITLE, 0, str); } void MPD::Song::SetAlbum(const std::string &str) { - mpd_song_clear_tag(itsSong, MPD_TAG_ALBUM); - if (!str.empty()) - mpd_song_add_tag(itsSong, MPD_TAG_ALBUM, str.c_str()); + SetTag(MPD_TAG_ALBUM, 0, str); } void MPD::Song::SetTrack(const std::string &str) { - mpd_song_clear_tag(itsSong, MPD_TAG_TRACK); - if (!str.empty()) - mpd_song_add_tag(itsSong, MPD_TAG_TRACK, str.c_str()); + SetTag(MPD_TAG_TRACK, 0, str); } void MPD::Song::SetTrack(unsigned track) { - mpd_song_clear_tag(itsSong, MPD_TAG_TRACK); - if (track) - mpd_song_add_tag(itsSong, MPD_TAG_ARTIST, IntoStr(track).c_str()); + SetTag(MPD_TAG_ARTIST, 0, IntoStr(track)); } void MPD::Song::SetDate(const std::string &str) { - mpd_song_clear_tag(itsSong, MPD_TAG_DATE); - if (!str.empty()) - mpd_song_add_tag(itsSong, MPD_TAG_DATE, str.c_str()); + SetTag(MPD_TAG_DATE, 0, str); } void MPD::Song::SetDate(unsigned year) { - mpd_song_clear_tag(itsSong, MPD_TAG_DATE); - if (year) - mpd_song_add_tag(itsSong, MPD_TAG_DATE, IntoStr(year).c_str()); + SetTag(MPD_TAG_TRACK, 0, IntoStr(year)); } void MPD::Song::SetGenre(const std::string &str) { - mpd_song_clear_tag(itsSong, MPD_TAG_GENRE); - if (!str.empty()) - mpd_song_add_tag(itsSong, MPD_TAG_GENRE, str.c_str()); + SetTag(MPD_TAG_GENRE, 0, str); } void MPD::Song::SetComposer(const std::string &str) { - mpd_song_clear_tag(itsSong, MPD_TAG_COMPOSER); - if (!str.empty()) - mpd_song_add_tag(itsSong, MPD_TAG_COMPOSER, str.c_str()); + SetTag(MPD_TAG_COMPOSER, 0, str); } void MPD::Song::SetPerformer(const std::string &str) { - mpd_song_clear_tag(itsSong, MPD_TAG_PERFORMER); - if (!str.empty()) - mpd_song_add_tag(itsSong, MPD_TAG_PERFORMER, str.c_str()); + SetTag(MPD_TAG_PERFORMER, 0, str); } void MPD::Song::SetDisc(const std::string &str) { - mpd_song_clear_tag(itsSong, MPD_TAG_DISC); - if (!str.empty()) - mpd_song_add_tag(itsSong, MPD_TAG_DISC, str.c_str()); + SetTag(MPD_TAG_DISC, 0, str); } void MPD::Song::SetComment(const std::string &str) { - mpd_song_clear_tag(itsSong, MPD_TAG_COMMENT); - if (!str.empty()) - mpd_song_add_tag(itsSong, MPD_TAG_COMMENT, str.c_str()); + SetTag(MPD_TAG_COMMENT, 0, str); } void MPD::Song::SetPosition(unsigned pos) @@ -347,11 +300,6 @@ void MPD::Song::SetPosition(unsigned pos) mpd_song_set_pos(itsSong, pos); } -void MPD::Song::SetLength(unsigned len) -{ - mpd_song_set_duration(itsSong, len); -} - std::string MPD::Song::ParseFormat(std::string::const_iterator &it) const { std::string result; @@ -482,10 +430,11 @@ MPD::Song &MPD::Song::operator=(const MPD::Song &s) return *this; if (itsSong) mpd_song_free(itsSong); - if (itsFile) - delete [] itsFile; + delete [] itsFile; + delete itsTags; itsSong = s.copyPtr ? s.itsSong : (s.itsSong ? mpd_song_dup(s.itsSong) : 0); itsFile = s.itsFile ? strdup(s.itsFile) : 0; + itsTags = s.itsTags ? new TagMap(*s.itsTags) : 0; itsNewName = s.itsNewName; itsSlash = s.itsSlash; itsHash = s.itsHash; @@ -549,3 +498,24 @@ const char *MPD::Song::MyFilename() const return itsFile ? itsFile : mpd_song_get_uri(itsSong); } +void MPD::Song::SetTag(mpd_tag_type type, unsigned pos, const std::string &value) +{ + if (value.empty()) + return; + if (!itsTags) + itsTags = new TagMap; + (*itsTags)[std::make_pair(type, pos)] = value; +} + +std::string MPD::Song::GetTag(mpd_tag_type type, unsigned pos) const +{ + if (itsTags) + { + TagMap::const_iterator it = itsTags->find(std::make_pair(type, pos)); + if (it != itsTags->end()) + return it->second; + } + const char *tag = mpd_song_get_tag(itsSong, type, pos); + return tag ? tag : ""; +} + diff --git a/src/song.h b/src/song.h index 3b7266b5..88389fbf 100644 --- a/src/song.h +++ b/src/song.h @@ -21,6 +21,7 @@ #ifndef _SONG_H #define _SONG_H +#include #include #include @@ -29,13 +30,14 @@ namespace MPD { class Song { + typedef std::map, std::string> TagMap; + public: typedef void (Song::*SetFunction)(const std::string &); typedef std::string (Song::*GetFunction)() const; - Song() : itsSong(0), itsFile(0), itsSlash(std::string::npos), itsHash(0), copyPtr(0), isLocalised(0) { } - Song(mpd_song *, bool = 0); + Song(mpd_song * = 0, bool = 0); Song(const Song &); ~Song(); @@ -73,7 +75,6 @@ namespace MPD void SetDisc(const std::string &); void SetComment(const std::string &); void SetPosition(unsigned); - void SetLength(unsigned); void SetNewName(const std::string &name) { itsNewName = name == GetName() ? "" : name; } std::string GetNewName() const { return itsNewName; } @@ -99,6 +100,9 @@ namespace MPD void SetHashAndSlash(); std::string ParseFormat(std::string::const_iterator &it) const; + void SetTag(mpd_tag_type, unsigned, const std::string &); + std::string GetTag(mpd_tag_type, unsigned) const; + /// Used internally for handling filename, since we don't have /// write access to file string in mpd_song, manage our own if /// localization was done and there is localized filename that @@ -112,6 +116,9 @@ namespace MPD /// localized version of filename const char *itsFile; + /// map that contains localized tags or these set by tag editor + TagMap *itsTags; + std::string itsNewName; size_t itsSlash; diff --git a/src/tag_editor.cpp b/src/tag_editor.cpp index 5c6d3ca5..eec93d4e 100644 --- a/src/tag_editor.cpp +++ b/src/tag_editor.cpp @@ -895,7 +895,6 @@ void TagEditor::ReadTags(MPD::Song &s) s.SetDisc(!mpegf->ID3v2Tag()->frameListMap()["TPOS"].isEmpty() ? mpegf->ID3v2Tag()->frameListMap()["TPOS"].front()->toString().to8Bit(1) : ""); } s.SetComment(f.tag()->comment().to8Bit(1)); - s.SetLength(f.audioProperties()->length()); } namespace