use common interface for adding item(s) to playlist

this also removes a few code duplications.
This commit is contained in:
Andrzej Rybczak
2009-08-09 15:51:24 +02:00
parent f5fc2dbe8a
commit ea1edd4d4a
6 changed files with 107 additions and 313 deletions

View File

@@ -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;
}

View File

@@ -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)
{

View File

@@ -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;
}

View File

@@ -62,6 +62,9 @@ class Playlist : public Screen< Menu<MPD::Song> >
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 *);

View File

@@ -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);

View File

@@ -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);
}