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:
{
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;
}
case itPlaylist:
{
std::string name = item.name;
ShowMessage("Loading and playing playlist %s...", name.c_str());
locale_to_utf(name);
if (!Mpd.LoadPlaylist(name))
ShowMessage("Couldn't load playlist.");
ShowMessage("Loading and playing playlist \"%s\"...", item.name.c_str());
if (Mpd.LoadPlaylist(locale_to_utf_cpy(item.name)))
ShowMessage("Playlist \"%s\" loaded", item.name.c_str());
else
{
size_t old_size = myPlaylist->Items->Size();
Mpd.UpdateStatus();
if (old_size < myPlaylist->Items->Size())
Mpd.Play(old_size);
}
myPlaylist->PlayNewlyAddedSongs();
}
}
}
@@ -178,39 +172,38 @@ void Browser::SpacePressed()
if (itsBrowsedDir != "/" && !w->Choice())
break; // do not let add parent dir.
MPD::SongList list;
bool result;
# ifndef WIN32
if (isLocal())
{
MPD::SongList list;
MPD::ItemList items;
ShowMessage("Scanning \"%s\"...", item.name.c_str());
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);
result = myPlaylist->Add(list, 0);
FreeSongList(list);
}
else
# endif // !WIN32
Mpd.GetDirectoryRecursive(locale_to_utf_cpy(item.name), list);
if (myPlaylist->Add(list, 0))
result = Mpd.Add(locale_to_utf_cpy(item.name));
if (result)
ShowMessage("Added folder: %s", item.name.c_str());
FreeSongList(list);
break;
}
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;
}
case itPlaylist:
{
std::string name = item.name;
ShowMessage("Loading playlist %s...", name.c_str());
locale_to_utf(name);
if (!Mpd.LoadPlaylist(name))
ShowMessage("Couldn't load playlist.");
ShowMessage("Loading playlist \"%s\"...", item.name.c_str());
if (Mpd.LoadPlaylist(locale_to_utf_cpy(item.name)))
ShowMessage("Playlist \"%s\" loaded", item.name.c_str());
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;
}
void MPD::Connection::Add(const std::string &path)
bool MPD::Connection::Add(const std::string &path)
{
if (!itsConnection)
return;
return false;
if (!isCommandsListEnabled)
{
GoBusy();
mpd_run_add(itsConnection, path.c_str());
return mpd_run_add(itsConnection, path.c_str());
}
else
{
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
bool AddRandomTag(mpd_tag_type, size_t);
bool AddRandomSongs(size_t);
void Add(const std::string &path);
bool Add(const std::string &path);
bool Delete(unsigned);
bool DeleteID(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())
return false;
size_t old_playlist_size = Items->Size();
Mpd.StartCommandsList();
MPD::SongList::const_iterator it = l.begin();
if (position < 0)
@@ -598,16 +596,25 @@ bool Playlist::Add(const MPD::SongList &l, bool play, int position)
if (Mpd.AddSong(**j, position) < 0)
break;
}
if (!Mpd.CommitCommandsList())
return false;
if (play && old_playlist_size < Items->Size())
Mpd.Play(old_playlist_size);
if (play)
PlayNewlyAddedSongs();
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)
{
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::SongList &l, bool play, int position = -1);
void PlayNewlyAddedSongs();
void SetSelectedItemsPriority(int prio);

View File

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

View File

@@ -35,17 +35,17 @@ SearchEngine *mySearcher = new SearchEngine;
const char *SearchEngine::ConstraintsNames[] =
{
"Any:",
"Artist:",
"Album Artist:",
"Title:",
"Album:",
"Filename:",
"Composer:",
"Performer:",
"Genre:",
"Date:",
"Comment:"
"Any",
"Artist",
"Album Artist",
"Title",
"Album",
"Filename",
"Composer",
"Performer",
"Genre",
"Date",
"Comment"
};
const char *SearchEngine::SearchModes[] =
@@ -137,10 +137,10 @@ void SearchEngine::EnterPressed()
if (option < ConstraintsNumber)
{
Statusbar() << fmtBold << ConstraintsNames[option] << fmtBoldEnd << ' ';
Statusbar() << fmtBold << ConstraintsNames[option] << fmtBoldEnd << ": ";
itsConstraints[option] = Global::wFooter->GetString(itsConstraints[option]);
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]);
}
else if (option == ConstraintsNumber+1)
@@ -333,7 +333,7 @@ void SearchEngine::Prepare()
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]);
}