From ea1edd4d4a923446f700155080b2ea2e53d2aff0 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Sun, 9 Aug 2009 15:51:24 +0200 Subject: [PATCH] use common interface for adding item(s) to playlist this also removes a few code duplications. --- src/browser.cpp | 121 ++++------------------------------------ src/media_library.cpp | 91 +++--------------------------- src/playlist.cpp | 78 ++++++++++++++++++++++++++ src/playlist.h | 3 + src/playlist_editor.cpp | 73 ++---------------------- src/search_engine.cpp | 54 +----------------- 6 files changed, 107 insertions(+), 313 deletions(-) diff --git a/src/browser.cpp b/src/browser.cpp index c85fcdaa..562816db 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -107,53 +107,15 @@ void Browser::EnterPressed() } case itSong: { - BlockItemListUpdate = 1; - if (Config.ncmpc_like_songs_adding && w->isBold()) - { - bool found = 0; - unsigned hash = w->Current().song->GetHash(); - for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i) - { - if (myPlaylist->Main()->at(i).GetHash() == hash) - { - Mpd.Play(i); - found = 1; - break; - } - } - if (found) - break; - } - Song &s = *item.song; - int id = Mpd.AddSong(s); - if (id >= 0) - { - Mpd.PlayID(id); - ShowMessage("Added to playlist: %s", s.toString(Config.song_status_format).c_str()); - w->BoldOption(w->Choice(), 1); - } + w->BoldOption(w->Choice(), myPlaylist->Add(*item.song, w->isBold(), 1)); break; } case itPlaylist: { SongList list; Mpd.GetPlaylistContent(locale_to_utf_cpy(item.name), list); - Mpd.StartCommandsList(); - SongList::const_iterator it = list.begin(); - for (; it != list.end(); ++it) - if (Mpd.AddSong(**it) < 0) - break; - Mpd.CommitCommandsList(); - - if (it != list.begin()) - { + if (myPlaylist->Add(list, 1)) ShowMessage("Loading and playing playlist %s...", item.name.c_str()); - Song *s = &myPlaylist->Main()->at(myPlaylist->Main()->Size()-list.size()); - if (s->GetHash() == list[0]->GetHash()) - Mpd.PlayID(s->GetID()); - else - ShowMessage("%s", MPD::Message::PartOfSongsAdded); - } FreeSongList(list); break; } @@ -180,95 +142,36 @@ void Browser::SpacePressed() if (itsBrowsedDir != "/" && !w->Choice()) break; // do not let add parent dir. - bool everything_was_added = 1; + SongList list; if (Config.local_browser) { - ItemList list; - + ItemList items; ShowMessage("Scanning \"%s\"...", item.name.c_str()); - myBrowser->GetLocalDirectory(list, item.name, 1); - - Mpd.StartCommandsList(); - for (ItemList::const_iterator it = list.begin(); it != list.end(); ++it) - { - if (everything_was_added && Mpd.AddSong(*it->song) < 0) - everything_was_added = 0; - delete it->song; - } - Mpd.CommitCommandsList(); + myBrowser->GetLocalDirectory(items, item.name, 1); + list.reserve(items.size()); + for (MPD::ItemList::const_iterator it = items.begin(); it != items.end(); ++it) + list.push_back(it->song); } else - { - SongList list; Mpd.GetDirectoryRecursive(locale_to_utf_cpy(item.name), list); - Mpd.StartCommandsList(); - for (SongList::const_iterator it = list.begin(); it != list.end(); ++it) - { - if (Mpd.AddSong(**it) < 0) - { - everything_was_added = 0; - break; - } - } - Mpd.CommitCommandsList(); - FreeSongList(list); - } - if (everything_was_added) + if (myPlaylist->Add(list, 0)) ShowMessage("Added folder: %s", item.name.c_str()); + FreeSongList(list); break; } case itSong: { - BlockItemListUpdate = 1; - if (Config.ncmpc_like_songs_adding && w->isBold()) - { - Playlist::BlockUpdate = 1; - unsigned hash = w->Current().song->GetHash(); - Mpd.StartCommandsList(); - for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i) - { - if (myPlaylist->Main()->at(i).GetHash() == hash) - { - Mpd.Delete(i); - myPlaylist->Main()->DeleteOption(i); - i--; - } - } - Mpd.CommitCommandsList(); - w->BoldOption(w->Choice(), 0); - Playlist::BlockUpdate = 0; - } - else - { - Song &s = *item.song; - if (Mpd.AddSong(s) != -1) - { - ShowMessage("Added to playlist: %s", s.toString(Config.song_status_format).c_str()); - w->BoldOption(w->Choice(), 1); - } - } + w->BoldOption(w->Choice(), myPlaylist->Add(*item.song, w->isBold(), 0)); break; } case itPlaylist: { SongList list; Mpd.GetPlaylistContent(locale_to_utf_cpy(item.name), list); - Mpd.StartCommandsList(); - SongList::const_iterator it = list.begin(); - for (; it != list.end(); ++it) - if (Mpd.AddSong(**it) < 0) - break; - Mpd.CommitCommandsList(); - - if (it != list.begin()) - { + if (myPlaylist->Add(list, 0)) ShowMessage("Loading playlist %s...", item.name.c_str()); - Song &s = myPlaylist->Main()->at(myPlaylist->Main()->Size()-list.size()); - if (s.GetHash() != list[0]->GetHash()) - ShowMessage("%s", MPD::Message::PartOfSongsAdded); - } FreeSongList(list); break; } diff --git a/src/media_library.cpp b/src/media_library.cpp index 6c7de6d8..ef046e82 100644 --- a/src/media_library.cpp +++ b/src/media_library.cpp @@ -482,101 +482,26 @@ void MediaLibrary::AddToPlaylist(bool add_n_play) Mpd.StartSearch(1); Mpd.AddSearch(Config.media_lib_primary_tag, locale_to_utf_cpy(Artists->Current())); Mpd.CommitSearch(list); - Mpd.StartCommandsList(); - SongList::const_iterator it = list.begin(); - for (; it != list.end(); ++it) - if (Mpd.AddSong(**it) < 0) - break; - Mpd.CommitCommandsList(); - if (it != list.begin()) + if (myPlaylist->Add(list, add_n_play)) { std::string tag_type = IntoStr(Config.media_lib_primary_tag); ToLower(tag_type); ShowMessage("Adding songs of %s \"%s\"", tag_type.c_str(), Artists->Current().c_str()); - Song *s = &myPlaylist->Main()->at(myPlaylist->Main()->Size()-list.size()); - if (s->GetHash() == list[0]->GetHash()) - { - if (add_n_play) - Mpd.PlayID(s->GetID()); - } - else - ShowMessage("%s", MPD::Message::PartOfSongsAdded); } } else if (w == Albums) { - Mpd.StartCommandsList(); - size_t i = 0; - for (; i < Songs->Size(); ++i) - if (Mpd.AddSong(Songs->at(i)) < 0) - break; - Mpd.CommitCommandsList(); + MPD::SongList l; + l.reserve(Songs->Size()); + for (size_t i = 0; i < Songs->Size(); ++i) + l.push_back(&(*Songs)[i]); - if (i) - { + if (myPlaylist->Add(l, add_n_play)) ShowMessage("Adding songs from album \"%s\"", Albums->Current().second.Album.c_str()); - Song *s = &myPlaylist->Main()->at(myPlaylist->Main()->Size()-Songs->Size()); - if (s->GetHash() == Songs->at(0).GetHash()) - { - if (add_n_play) - Mpd.PlayID(s->GetID()); - } - else - ShowMessage("%s", MPD::Message::PartOfSongsAdded); - } - } - else if (w == Songs) - { - if (!Songs->Empty()) - { - BlockItemListUpdate = 1; - if (Config.ncmpc_like_songs_adding && Songs->isBold()) - { - unsigned hash = Songs->Current().GetHash(); - if (add_n_play) - { - for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i) - { - if (myPlaylist->Main()->at(i).GetHash() == hash) - { - Mpd.Play(i); - break; - } - } - } - else - { - Playlist::BlockUpdate = 1; - Mpd.StartCommandsList(); - for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i) - { - if (myPlaylist->Main()->at(i).GetHash() == hash) - { - Mpd.Delete(i); - myPlaylist->Main()->DeleteOption(i); - i--; - } - } - Mpd.CommitCommandsList(); - Songs->BoldOption(Songs->Choice(), 0); - Playlist::BlockUpdate = 0; - } - } - else - { - Song &s = Songs->Current(); - int id = Mpd.AddSong(s); - if (id >= 0) - { - ShowMessage("Added to playlist: %s", s.toString(Config.song_status_format).c_str()); - if (add_n_play) - Mpd.PlayID(id); - Songs->BoldOption(Songs->Choice(), 1); - } - } - } } + else if (w == Songs && !Songs->Empty()) + Songs->BoldOption(Songs->Choice(), myPlaylist->Add(Songs->Current(), Songs->isBold(), add_n_play)); FreeSongList(list); if (!add_n_play) { diff --git a/src/playlist.cpp b/src/playlist.cpp index f0039416..5194e7fc 100644 --- a/src/playlist.cpp +++ b/src/playlist.cpp @@ -467,3 +467,81 @@ std::string Playlist::SongInColumnsToString(const MPD::Song &s, void *) return s.toString(result); } +bool Playlist::Add(const MPD::Song &s, bool in_playlist, bool play) +{ + BlockItemListUpdate = 1; + if (Config.ncmpc_like_songs_adding && in_playlist) + { + unsigned hash = s.GetHash(); + if (play) + { + for (size_t i = 0; i < w->Size(); ++i) + { + if (w->at(i).GetHash() == hash) + { + Mpd.Play(i); + break; + } + } + return true; + } + else + { + Playlist::BlockUpdate = 1; + Mpd.StartCommandsList(); + for (size_t i = 0; i < w->Size(); ++i) + { + if ((*w)[i].GetHash() == hash) + { + Mpd.Delete(i); + w->DeleteOption(i); + i--; + } + } + Mpd.CommitCommandsList(); + Playlist::BlockUpdate = 0; + return false; + } + } + else + { + int id = Mpd.AddSong(s); + if (id >= 0) + { + ShowMessage("Added to playlist: %s", s.toString(Config.song_status_format).c_str()); + if (play) + Mpd.PlayID(id); + return true; + } + else + return false; + } +} + +bool Playlist::Add(const MPD::SongList &l, bool play) +{ + if (l.empty()) + return false; + + size_t old_playlist_size = w->Size(); + + Mpd.StartCommandsList(); + MPD::SongList::const_iterator it = l.begin(); + for (; it != l.end(); ++it) + if (Mpd.AddSong(**it) < 0) + break; + Mpd.CommitCommandsList(); + + if (play && old_playlist_size < w->Size()) + Mpd.Play(old_playlist_size); + + if (w->Back().GetHash() != l.back()->GetHash()) + { + if (it != l.begin()) + ShowMessage("%s", MPD::Message::PartOfSongsAdded); + return false; + } + else + return true; +} + diff --git a/src/playlist.h b/src/playlist.h index 05654794..4b6f2d3c 100644 --- a/src/playlist.h +++ b/src/playlist.h @@ -62,6 +62,9 @@ class Playlist : public Screen< Menu > void UpdateTimer() { time(&itsTimer); } time_t Timer() const { return itsTimer; } + bool Add(const MPD::Song &s, bool in_playlist, bool play); + bool Add(const MPD::SongList &l, bool play); + static std::string SongToString(const MPD::Song &, void *); static std::string SongInColumnsToString(const MPD::Song &, void *); diff --git a/src/playlist_editor.cpp b/src/playlist_editor.cpp index 66670a3c..d5636e78 100644 --- a/src/playlist_editor.cpp +++ b/src/playlist_editor.cpp @@ -197,77 +197,12 @@ void PlaylistEditor::AddToPlaylist(bool add_n_play) if (w == Playlists && !Playlists->Empty()) { Mpd.GetPlaylistContent(locale_to_utf_cpy(Playlists->Current()), list); - Mpd.StartCommandsList(); - SongList::const_iterator it = list.begin(); - for (; it != list.end(); ++it) - if (Mpd.AddSong(**it) < 0) - break; - Mpd.CommitCommandsList(); - - if (it != list.begin()) - { + if (myPlaylist->Add(list, add_n_play)) ShowMessage("Loading playlist %s...", Playlists->Current().c_str()); - Song &s = myPlaylist->Main()->at(myPlaylist->Main()->Size()-list.size()); - if (s.GetHash() == list[0]->GetHash()) - { - if (add_n_play) - Mpd.PlayID(s.GetID()); - } - else - ShowMessage("%s", MPD::Message::PartOfSongsAdded); - } - } - else if (w == Content) - { - if (!Content->Empty()) - { - BlockItemListUpdate = 1; - if (Config.ncmpc_like_songs_adding && Content->isBold()) - { - unsigned hash = Content->Current().GetHash(); - if (add_n_play) - { - for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i) - { - if (myPlaylist->Main()->at(i).GetHash() == hash) - { - Mpd.Play(i); - break; - } - } - } - else - { - Playlist::BlockUpdate = 1; - Mpd.StartCommandsList(); - for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i) - { - if (myPlaylist->Main()->at(i).GetHash() == hash) - { - Mpd.Delete(i); - myPlaylist->Main()->DeleteOption(i); - i--; - } - } - Mpd.CommitCommandsList(); - Content->BoldOption(Content->Choice(), 0); - Playlist::BlockUpdate = 0; - } - } - else - { - const Song &s = Content->Current(); - int id = Mpd.AddSong(s); - if (id >= 0) - { - ShowMessage("Added to playlist: %s", s.toString(Config.song_status_format).c_str()); - if (add_n_play) - Mpd.PlayID(id); - Content->BoldOption(Content->Choice(), 1); - } - } - } } + else if (w == Content && !Content->Empty()) + Content->BoldOption(Content->Choice(), myPlaylist->Add(Content->Current(), Content->isBold(), add_n_play)); + FreeSongList(list); if (!add_n_play) w->Scroll(wDown); diff --git a/src/search_engine.cpp b/src/search_engine.cpp index 2313e985..de9be245 100644 --- a/src/search_engine.cpp +++ b/src/search_engine.cpp @@ -225,30 +225,7 @@ void SearchEngine::EnterPressed() } default: { - BlockItemListUpdate = 1; - if (Config.ncmpc_like_songs_adding && w->isBold()) - { - unsigned hash = w->Current().second->GetHash(); - for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i) - { - if (myPlaylist->Main()->at(i).GetHash() == hash) - { - Mpd.Play(i); - break; - } - } - } - else - { - const Song &s = *w->Current().second; - int id = Mpd.AddSong(s); - if (id >= 0) - { - Mpd.PlayID(id); - ShowMessage("Added to playlist: %s", s.toString(Config.song_status_format).c_str()); - w->BoldOption(w->Choice(), 1); - } - } + w->BoldOption(w->Choice(), myPlaylist->Add(*w->Current().second, w->isBold(), 1)); break; } } @@ -268,34 +245,7 @@ void SearchEngine::SpacePressed() return; } - BlockItemListUpdate = 1; - if (Config.ncmpc_like_songs_adding && w->isBold()) - { - Playlist::BlockUpdate = 1; - unsigned hash = w->Current().second->GetHash(); - Mpd.StartCommandsList(); - for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i) - { - if (myPlaylist->Main()->at(i).GetHash() == hash) - { - Mpd.Delete(i); - myPlaylist->Main()->DeleteOption(i); - i--; - } - } - Mpd.CommitCommandsList(); - w->BoldOption(w->Choice(), 0); - Playlist::BlockUpdate = 0; - } - else - { - const Song &s = *w->Current().second; - if (Mpd.AddSong(s) != -1) - { - ShowMessage("Added to playlist: %s", s.toString(Config.song_status_format).c_str()); - w->BoldOption(w->Choice(), 1); - } - } + w->BoldOption(w->Choice(), myPlaylist->Add(*w->Current().second, w->isBold(), 0)); w->Scroll(wDown); }