optimize a bit adding playlists/directories to playlist

This commit is contained in:
Andrzej Rybczak
2012-08-14 17:15:18 +02:00
parent bab756c579
commit dad2bba13e
7 changed files with 65 additions and 57 deletions

View File

@@ -134,23 +134,17 @@ void Browser::EnterPressed()
} }
case itSong: case itSong:
{ {
w->Bold(w->Choice(), myPlaylist->Add(*item.song, w->isBold(), 1)); bool res = myPlaylist->Add(*item.song, w->isBold(), 1);
w->Bold(w->Choice(), res);
break; break;
} }
case itPlaylist: case itPlaylist:
{ {
std::string name = item.name; ShowMessage("Loading and playing playlist \"%s\"...", item.name.c_str());
ShowMessage("Loading and playing playlist %s...", name.c_str()); if (Mpd.LoadPlaylist(locale_to_utf_cpy(item.name)))
locale_to_utf(name); ShowMessage("Playlist \"%s\" loaded", item.name.c_str());
if (!Mpd.LoadPlaylist(name))
ShowMessage("Couldn't load playlist.");
else else
{ myPlaylist->PlayNewlyAddedSongs();
size_t old_size = myPlaylist->Items->Size();
Mpd.UpdateStatus();
if (old_size < myPlaylist->Items->Size())
Mpd.Play(old_size);
}
} }
} }
} }
@@ -178,39 +172,38 @@ void Browser::SpacePressed()
if (itsBrowsedDir != "/" && !w->Choice()) if (itsBrowsedDir != "/" && !w->Choice())
break; // do not let add parent dir. break; // do not let add parent dir.
MPD::SongList list; bool result;
# ifndef WIN32 # ifndef WIN32
if (isLocal()) if (isLocal())
{ {
MPD::SongList list;
MPD::ItemList items; MPD::ItemList items;
ShowMessage("Scanning \"%s\"...", item.name.c_str()); ShowMessage("Scanning \"%s\"...", item.name.c_str());
myBrowser->GetLocalDirectory(items, item.name, 1); myBrowser->GetLocalDirectory(items, item.name, 1);
list.reserve(items.size()); list.reserve(items.size());
for (MPD::ItemList::const_iterator it = items.begin(); it != items.end(); ++it) for (MPD::ItemList::const_iterator it = items.begin(); it != items.end(); ++it)
list.push_back(it->song); list.push_back(it->song);
result = myPlaylist->Add(list, 0);
FreeSongList(list);
} }
else else
# endif // !WIN32 # endif // !WIN32
Mpd.GetDirectoryRecursive(locale_to_utf_cpy(item.name), list); result = Mpd.Add(locale_to_utf_cpy(item.name));
if (result)
if (myPlaylist->Add(list, 0))
ShowMessage("Added folder: %s", item.name.c_str()); ShowMessage("Added folder: %s", item.name.c_str());
FreeSongList(list);
break; break;
} }
case itSong: case itSong:
{ {
w->Bold(w->Choice(), myPlaylist->Add(*item.song, w->isBold(), 0)); bool res = myPlaylist->Add(*item.song, w->isBold(), 0);
w->Bold(w->Choice(), res);
break; break;
} }
case itPlaylist: case itPlaylist:
{ {
std::string name = item.name; ShowMessage("Loading playlist \"%s\"...", item.name.c_str());
ShowMessage("Loading playlist %s...", name.c_str()); if (Mpd.LoadPlaylist(locale_to_utf_cpy(item.name)))
locale_to_utf(name); ShowMessage("Playlist \"%s\" loaded", item.name.c_str());
if (!Mpd.LoadPlaylist(name))
ShowMessage("Couldn't load playlist.");
break; break;
} }
} }

View File

@@ -883,19 +883,19 @@ int MPD::Connection::AddSong(const Song &s, int pos)
return !s.Empty() ? (AddSong((!s.isFromDB() ? "file://" : "") + (s.Localized() ? locale_to_utf_cpy(s.GetFile()) : s.GetFile()), pos)) : -1; return !s.Empty() ? (AddSong((!s.isFromDB() ? "file://" : "") + (s.Localized() ? locale_to_utf_cpy(s.GetFile()) : s.GetFile()), pos)) : -1;
} }
void MPD::Connection::Add(const std::string &path) bool MPD::Connection::Add(const std::string &path)
{ {
if (!itsConnection) if (!itsConnection)
return; return false;
if (!isCommandsListEnabled) if (!isCommandsListEnabled)
{ {
GoBusy(); GoBusy();
mpd_run_add(itsConnection, path.c_str()); return mpd_run_add(itsConnection, path.c_str());
} }
else else
{ {
assert(!isIdle); assert(!isIdle);
mpd_send_add(itsConnection, path.c_str()); return mpd_send_add(itsConnection, path.c_str());
} }
} }

View File

@@ -180,7 +180,7 @@ namespace MPD
int AddSong(const Song &, int = -1); // returns id of added song int AddSong(const Song &, int = -1); // returns id of added song
bool AddRandomTag(mpd_tag_type, size_t); bool AddRandomTag(mpd_tag_type, size_t);
bool AddRandomSongs(size_t); bool AddRandomSongs(size_t);
void Add(const std::string &path); bool Add(const std::string &path);
bool Delete(unsigned); bool Delete(unsigned);
bool DeleteID(unsigned); bool DeleteID(unsigned);
bool Delete(const std::string &, unsigned); bool Delete(const std::string &, unsigned);

View File

@@ -581,8 +581,6 @@ bool Playlist::Add(const MPD::SongList &l, bool play, int position)
if (l.empty()) if (l.empty())
return false; return false;
size_t old_playlist_size = Items->Size();
Mpd.StartCommandsList(); Mpd.StartCommandsList();
MPD::SongList::const_iterator it = l.begin(); MPD::SongList::const_iterator it = l.begin();
if (position < 0) if (position < 0)
@@ -598,16 +596,25 @@ bool Playlist::Add(const MPD::SongList &l, bool play, int position)
if (Mpd.AddSong(**j, position) < 0) if (Mpd.AddSong(**j, position) < 0)
break; break;
} }
if (!Mpd.CommitCommandsList()) if (!Mpd.CommitCommandsList())
return false; return false;
if (play)
if (play && old_playlist_size < Items->Size()) PlayNewlyAddedSongs();
Mpd.Play(old_playlist_size);
return true; return true;
} }
void Playlist::PlayNewlyAddedSongs()
{
bool is_filtered = Items->isFiltered();
Items->ShowAll();
size_t old_size = Items->Size();
Mpd.UpdateStatus();
if (old_size < Items->Size())
Mpd.Play(old_size);
if (is_filtered)
Items->ShowFiltered();
}
void Playlist::SetSelectedItemsPriority(int prio) void Playlist::SetSelectedItemsPriority(int prio)
{ {
std::vector<size_t> list; std::vector<size_t> list;

View File

@@ -77,6 +77,7 @@ class Playlist : public Screen<Window>
bool Add(const MPD::Song &s, bool in_playlist, bool play, int position = -1); bool Add(const MPD::Song &s, bool in_playlist, bool play, int position = -1);
bool Add(const MPD::SongList &l, bool play, int position = -1); bool Add(const MPD::SongList &l, bool play, int position = -1);
void PlayNewlyAddedSongs();
void SetSelectedItemsPriority(int prio); void SetSelectedItemsPriority(int prio);

View File

@@ -18,6 +18,7 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/ ***************************************************************************/
#include <cassert>
#include <algorithm> #include <algorithm>
#include "charset.h" #include "charset.h"
@@ -324,9 +325,18 @@ void PlaylistEditor::AddToPlaylist(bool add_n_play)
if (w == Playlists && !Playlists->Empty()) if (w == Playlists && !Playlists->Empty())
{ {
Mpd.GetPlaylistContent(locale_to_utf_cpy(Playlists->Current()), list); const char *msg;
if (myPlaylist->Add(list, add_n_play)) if (add_n_play)
ShowMessage("Loading playlist %s...", Playlists->Current().c_str()); msg = "Loading and playing playlist \"%s\"...";
else
msg = "Loading playlist \"%s\"...";
ShowMessage(msg, Playlists->Current().c_str());
if (Mpd.LoadPlaylist(utf_to_locale_cpy(Playlists->Current())))
{
ShowMessage("Playlist \"%s\" loaded", Playlists->Current().c_str());
if (add_n_play)
myPlaylist->PlayNewlyAddedSongs();
}
} }
else if (w == Content && !Content->Empty()) else if (w == Content && !Content->Empty())
Content->Bold(Content->Choice(), myPlaylist->Add(Content->Current(), Content->isBold(), add_n_play)); Content->Bold(Content->Choice(), myPlaylist->Add(Content->Current(), Content->isBold(), add_n_play));
@@ -402,9 +412,7 @@ void PlaylistEditor::GetSelectedSongs(MPD::SongList &v)
if (selected.empty()) if (selected.empty())
selected.push_back(Content->Choice()); selected.push_back(Content->Choice());
for (std::vector<size_t>::const_iterator it = selected.begin(); it != selected.end(); ++it) for (std::vector<size_t>::const_iterator it = selected.begin(); it != selected.end(); ++it)
{
v.push_back(new MPD::Song(Content->at(*it))); v.push_back(new MPD::Song(Content->at(*it)));
}
} }
void PlaylistEditor::ApplyFilter(const std::string &s) void PlaylistEditor::ApplyFilter(const std::string &s)
@@ -436,6 +444,5 @@ List *PlaylistEditor::GetList()
else if (w == Content) else if (w == Content)
return Content; return Content;
else // silence compiler else // silence compiler
return 0; assert(false);
} }

View File

@@ -35,17 +35,17 @@ SearchEngine *mySearcher = new SearchEngine;
const char *SearchEngine::ConstraintsNames[] = const char *SearchEngine::ConstraintsNames[] =
{ {
"Any:", "Any",
"Artist:", "Artist",
"Album Artist:", "Album Artist",
"Title:", "Title",
"Album:", "Album",
"Filename:", "Filename",
"Composer:", "Composer",
"Performer:", "Performer",
"Genre:", "Genre",
"Date:", "Date",
"Comment:" "Comment"
}; };
const char *SearchEngine::SearchModes[] = const char *SearchEngine::SearchModes[] =
@@ -137,10 +137,10 @@ void SearchEngine::EnterPressed()
if (option < ConstraintsNumber) if (option < ConstraintsNumber)
{ {
Statusbar() << fmtBold << ConstraintsNames[option] << fmtBoldEnd << ' '; Statusbar() << fmtBold << ConstraintsNames[option] << fmtBoldEnd << ": ";
itsConstraints[option] = Global::wFooter->GetString(itsConstraints[option]); itsConstraints[option] = Global::wFooter->GetString(itsConstraints[option]);
w->Current().first->Clear(); w->Current().first->Clear();
*w->Current().first << fmtBold << std::setw(13) << std::left << ConstraintsNames[option] << fmtBoldEnd << ' '; *w->Current().first << fmtBold << std::setw(13) << std::left << ConstraintsNames[option] << fmtBoldEnd << ": ";
ShowTag(*w->Current().first, itsConstraints[option]); ShowTag(*w->Current().first, itsConstraints[option]);
} }
else if (option == ConstraintsNumber+1) else if (option == ConstraintsNumber+1)
@@ -333,7 +333,7 @@ void SearchEngine::Prepare()
for (size_t i = 0; i < ConstraintsNumber; ++i) for (size_t i = 0; i < ConstraintsNumber; ++i)
{ {
*(*w)[i].first << fmtBold << std::setw(13) << std::left << ConstraintsNames[i] << fmtBoldEnd << ' '; *(*w)[i].first << fmtBold << std::setw(13) << std::left << ConstraintsNames[i] << fmtBoldEnd << ": ";
ShowTag(*(*w)[i].first, itsConstraints[i]); ShowTag(*(*w)[i].first, itsConstraints[i]);
} }