summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrzej Rybczak <electricityispower@gmail.com>2012-10-01 20:44:22 +0200
committerAndrzej Rybczak <electricityispower@gmail.com>2012-10-01 20:44:22 +0200
commit9a292ca20d5b7c5f37efb4cd793c7c3fe4140442 (patch)
tree57abbc93f56e79ed4c0ccc675f6bfebc2986d6c9
parent60faa154627ed667a7e40cebc6475d3c30b3aab9 (diff)
mpd: make functions take item consumer instead of returning vector of items
-rw-r--r--src/actions.cpp28
-rw-r--r--src/browser.cpp14
-rw-r--r--src/media_library.cpp37
-rw-r--r--src/mpdpp.cpp128
-rw-r--r--src/mpdpp.h35
-rw-r--r--src/outputs.cpp6
-rw-r--r--src/playlist_editor.cpp53
-rw-r--r--src/search_engine.cpp16
-rw-r--r--src/sel_items_adder.cpp17
-rw-r--r--src/server_info.cpp8
-rw-r--r--src/status.cpp14
-rw-r--r--src/tag_editor.cpp42
-rw-r--r--src/utility/comparators.h6
-rw-r--r--src/visualizer.cpp11
14 files changed, 206 insertions, 209 deletions
diff --git a/src/actions.cpp b/src/actions.cpp
index c181a5c6..b23d2b19 100644
--- a/src/actions.cpp
+++ b/src/actions.cpp
@@ -1407,24 +1407,28 @@ void EditLibraryTag::Run()
MPD::MutableSong::SetFunction set = tagTypeToSetFunction(Config.media_lib_primary_tag);
assert(set);
bool success = true;
- MPD::SongList songs = Mpd.CommitSearchSongs();
- for (auto s = songs.begin(); s != songs.end(); ++s)
- {
- MPD::MutableSong es = *s;
- es.setTags(set, new_tag, Config.tags_separator);
- Statusbar::msg("Updating tags in \"%s\"...", es.getName().c_str());
- std::string path = Config.mpd_music_dir + es.getURI();
- if (!Tags::write(es))
+ std::string dir_to_update;
+ Mpd.CommitSearchSongs([set, &dir_to_update, &new_tag, &success](MPD::Song &&s) {
+ if (!success)
+ return;
+ MPD::MutableSong ms = s;
+ ms.setTags(set, new_tag, Config.tags_separator);
+ Statusbar::msg("Updating tags in \"%s\"...", ms.getName().c_str());
+ std::string path = Config.mpd_music_dir + ms.getURI();
+ if (!Tags::write(ms))
{
const char msg[] = "Error while updating tags in \"%ls\"";
- Statusbar::msg(msg, wideShorten(ToWString(es.getURI()), COLS-const_strlen(msg)).c_str());
+ Statusbar::msg(msg, wideShorten(ToWString(ms.getURI()), COLS-const_strlen(msg)).c_str());
success = false;
- break;
}
- }
+ if (dir_to_update.empty())
+ dir_to_update = s.getURI();
+ else
+ dir_to_update = getSharedDirectory(dir_to_update, s.getURI());
+ });
if (success)
{
- Mpd.UpdateDirectory(getSharedDirectory(songs.begin(), songs.end()));
+ Mpd.UpdateDirectory(dir_to_update);
Statusbar::msg("Tags updated successfully");
}
}
diff --git a/src/browser.cpp b/src/browser.cpp
index 015372e5..6cc17881 100644
--- a/src/browser.cpp
+++ b/src/browser.cpp
@@ -325,16 +325,18 @@ MPD::SongList Browser::getSelectedSongs()
else
# endif // !WIN32
{
- auto list = Mpd.GetDirectoryRecursive(item.name);
- result.insert(result.end(), list.begin(), list.end());
+ Mpd.GetDirectoryRecursive(item.name, [&result](MPD::Song &&s) {
+ result.push_back(s);
+ });
}
}
else if (item.type == itSong)
result.push_back(*item.song);
else if (item.type == itPlaylist)
{
- auto list = Mpd.GetPlaylistContent(item.name);
- result.insert(result.end(), list.begin(), list.end());
+ Mpd.GetPlaylistContent(item.name, [&result](MPD::Song &&s) {
+ result.push_back(s);
+ });
}
};
for (auto it = w.begin(); it != w.end(); ++it)
@@ -395,7 +397,9 @@ void Browser::GetDirectory(std::string dir, std::string subdir)
if (isLocal())
GetLocalDirectory(list);
else
- list = Mpd.GetDirectory(dir);
+ Mpd.GetDirectory(dir, [&list](MPD::Item &&item) {
+ list.push_back(item);
+ });
# else
list = Mpd.GetDirectory(dir);
# endif // !WIN32
diff --git a/src/media_library.cpp b/src/media_library.cpp
index a18e028e..141a56ac 100644
--- a/src/media_library.cpp
+++ b/src/media_library.cpp
@@ -324,9 +324,9 @@ void MediaLibrary::update()
Mpd.AddSearch(Config.media_lib_primary_tag,
Tags.current().value().tag());
Mpd.AddSearch(MPD_TAG_ALBUM, album);
- auto dates = Mpd.CommitSearchTags();
- for (auto date = dates.begin(); date != dates.end(); ++date)
- Albums.addItem(SearchConstraints(album, *date, mtime));
+ Mpd.CommitSearchTags([this, &album, &mtime](std::string &&date) {
+ Albums.addItem(SearchConstraints(album, date, mtime));
+ });
}
else
Albums.addItem(SearchConstraints(album, "", mtime));
@@ -347,7 +347,10 @@ void MediaLibrary::update()
Albums << NC::XY(0, 0) << "Fetching albums...";
Albums.Window::refresh();
Mpd.BlockIdle(true);
- auto artists = Mpd.GetList(Config.media_lib_primary_tag);
+ MPD::StringList artists;
+ Mpd.GetList(Config.media_lib_primary_tag, [&artists](std::string &&artist) {
+ artists.push_back(artist);
+ });
for (auto artist = artists.begin(); artist != artists.end(); ++artist)
{
Mpd.StartFieldSearchMTime(MPD_TAG_ALBUM, Config.media_library_sort_by_mtime);
@@ -364,9 +367,9 @@ void MediaLibrary::update()
Mpd.StartFieldSearch(MPD_TAG_DATE);
Mpd.AddSearch(Config.media_lib_primary_tag, *artist);
Mpd.AddSearch(MPD_TAG_ALBUM, album);
- auto dates = Mpd.CommitSearchTags();
- for (auto date = dates.begin(); date != dates.end(); ++date)
- Albums.addItem(SearchConstraints(*artist, album, *date, mtime));
+ Mpd.CommitSearchTags([this, &artist, &album, &mtime](std::string &&date) {
+ Albums.addItem(SearchConstraints(*artist, album, date, mtime));
+ });
}
else
Albums.addItem(SearchConstraints(*artist, album, *artist, mtime));
@@ -403,9 +406,9 @@ void MediaLibrary::update()
if (Config.media_library_display_date)
Mpd.AddSearch(MPD_TAG_DATE, Albums.current().value().Date);
}
- auto songs = Mpd.CommitSearchSongs();
- for (auto s = songs.begin(); s != songs.end(); ++s)
- Songs.addItem(*s, myPlaylist->checkForSong(*s));
+ Mpd.CommitSearchSongs([this](MPD::Song &&s) {
+ Songs.addItem(s, myPlaylist->checkForSong(s));
+ });
if (Albums.current().value().Date == AllTracksMarker)
std::sort(Songs.beginV(), Songs.endV(), SortAllTracks());
@@ -675,9 +678,9 @@ MPD::SongList MediaLibrary::getSelectedSongs()
auto tag_handler = [&result](const std::string &tag) {
Mpd.StartSearch(true);
Mpd.AddSearch(Config.media_lib_primary_tag, tag);
- auto songs = Mpd.CommitSearchSongs();
- std::sort(songs.begin(), songs.end(), SortAllTracks());
- result.insert(result.end(), songs.begin(), songs.end());
+ Mpd.CommitSearchSongs([&result](MPD::Song &&s) {
+ result.push_back(s);
+ });
};
for (auto it = Tags.begin(); it != Tags.end(); ++it)
if (it->isSelected())
@@ -701,9 +704,11 @@ MPD::SongList MediaLibrary::getSelectedSongs()
Tags.current().value().tag());
Mpd.AddSearch(MPD_TAG_ALBUM, sc.Album);
Mpd.AddSearch(MPD_TAG_DATE, sc.Date);
- auto songs = Mpd.CommitSearchSongs();
- std::sort(songs.begin(), songs.end(), SortSongsByTrack);
- result.insert(result.end(), songs.begin(), songs.end());
+ size_t begin = result.size();
+ Mpd.CommitSearchSongs([&result](MPD::Song &&s) {
+ result.push_back(s);
+ });
+ std::sort(result.begin()+begin, result.end(), SortSongsByTrack);
}
}
// if no item is selected, add songs from right column
diff --git a/src/mpdpp.cpp b/src/mpdpp.cpp
index aa099277..3d1b5ddc 100644
--- a/src/mpdpp.cpp
+++ b/src/mpdpp.cpp
@@ -663,19 +663,17 @@ bool Connection::Rename(const std::string &from, const std::string &to)
}
}
-SongList Connection::GetPlaylistChanges(unsigned version)
+void Connection::GetPlaylistChanges(unsigned version, SongConsumer f)
{
- SongList result;
if (!itsConnection)
- return result;
+ return;
assert(!isCommandsListEnabled);
GoBusy();
mpd_send_queue_changes_meta(itsConnection, version);
while (mpd_song *s = mpd_recv_song(itsConnection))
- result.push_back(Song(s));
+ f(Song(s));
mpd_response_finish(itsConnection);
GoIdle();
- return result;
}
Song Connection::GetSong(const std::string &path)
@@ -706,19 +704,17 @@ Song Connection::GetCurrentlyPlayingSong()
return result;
}
-SongList Connection::GetPlaylistContent(const std::string &path)
+void Connection::GetPlaylistContent(const std::string &path, SongConsumer f)
{
- SongList result;
if (!itsConnection)
- return result;
+ return;
assert(!isCommandsListEnabled);
GoBusy();
mpd_send_list_playlist_meta(itsConnection, path.c_str());
while (mpd_song *s = mpd_recv_song(itsConnection))
- result.push_back(Song(s));
+ f(Song(s));
mpd_response_finish(itsConnection);
GoIdle();
- return result;
}
void Connection::GetSupportedExtensions(std::set<std::string> &acc)
@@ -944,7 +940,10 @@ bool Connection::AddRandomTag(mpd_tag_type tag, size_t number)
return false;
assert(!isCommandsListEnabled);
- auto tags = GetList(tag);
+ StringList tags;
+ GetList(tag, [&tags](std::string &&tag_name) {
+ tags.push_back(tag_name);
+ });
if (number > tags.size())
{
if (itsErrorHandler)
@@ -959,7 +958,10 @@ bool Connection::AddRandomTag(mpd_tag_type tag, size_t number)
{
StartSearch(1);
AddSearch(tag, *it++);
- auto songs = CommitSearchSongs();
+ SongList songs;
+ CommitSearchSongs([&songs](MPD::Song &&s) {
+ songs.push_back(s);
+ });
StartCommandsList();
for (auto s = songs.begin(); s != songs.end(); ++s)
AddSong(*s);
@@ -1110,35 +1112,31 @@ int Connection::SavePlaylist(const std::string &name)
return CheckForErrors();
}
-StringList Connection::GetPlaylists()
+void Connection::GetPlaylists(StringConsumer f)
{
- StringList result;
if (!itsConnection)
- return result;
- auto items = GetDirectory("/");
- for (auto it = items.begin(); it != items.end(); ++it)
- if (it->type == itPlaylist)
- result.push_back(it->name);
- return result;
+ return;
+ GetDirectory("/", [&f](Item &&item) {
+ if (item.type == itPlaylist)
+ f(std::move(item.name));
+ });
}
-StringList Connection::GetList(mpd_tag_type type)
+void Connection::GetList(mpd_tag_type type, StringConsumer f)
{
- StringList result;
if (!itsConnection)
- return result;
+ return;
assert(!isCommandsListEnabled);
GoBusy();
mpd_search_db_tags(itsConnection, type);
mpd_search_commit(itsConnection);
while (mpd_pair *item = mpd_recv_pair_tag(itsConnection, type))
{
- result.push_back(item->value);
+ f(std::string(item->value));
mpd_return_pair(itsConnection, item);
}
mpd_response_finish(itsConnection);
GoIdle();
- return result;
}
TagMTimeList Connection::GetListMTime(mpd_tag_type type, bool get_mtime)
@@ -1237,37 +1235,33 @@ void Connection::AddSearchURI(const std::string &str) const
mpd_search_add_uri_constraint(itsConnection, MPD_OPERATOR_DEFAULT, str.c_str());
}
-SongList Connection::CommitSearchSongs()
+void Connection::CommitSearchSongs(SongConsumer f)
{
- SongList result;
if (!itsConnection)
- return result;
+ return;
assert(!isCommandsListEnabled);
GoBusy();
mpd_search_commit(itsConnection);
while (mpd_song *s = mpd_recv_song(itsConnection))
- result.push_back(Song(s));
+ f(Song(s));
mpd_response_finish(itsConnection);
GoIdle();
- return result;
}
-StringList Connection::CommitSearchTags()
+void Connection::CommitSearchTags(StringConsumer f)
{
- StringList result;
if (!itsConnection)
- return result;
+ return;
assert(!isCommandsListEnabled);
GoBusy();
mpd_search_commit(itsConnection);
while (mpd_pair *tag = mpd_recv_pair_tag(itsConnection, itsSearchedField))
{
- result.push_back(tag->value);
+ f(std::string(tag->value));
mpd_return_pair(itsConnection, tag);
}
mpd_response_finish(itsConnection);
GoIdle();
- return result;
}
TagMTimeList Connection::CommitSearchTagsMTime()
@@ -1314,14 +1308,13 @@ TagMTimeList Connection::CommitSearchTagsMTime()
return result;
}
-ItemList Connection::GetDirectory(const std::string &path)
+void Connection::GetDirectory(const std::string &directory, ItemConsumer f)
{
- ItemList result;
if (!itsConnection)
- return result;
+ return;
assert(!isCommandsListEnabled);
GoBusy();
- mpd_send_list_meta(itsConnection, path.c_str());
+ mpd_send_list_meta(itsConnection, directory.c_str());
while (mpd_entity *item = mpd_recv_entity(itsConnection))
{
Item it;
@@ -1343,77 +1336,68 @@ ItemList Connection::GetDirectory(const std::string &path)
assert(false);
}
mpd_entity_free(item);
- result.push_back(std::move(it));
+ f(std::move(it));
}
mpd_response_finish(itsConnection);
GoIdle();
- return result;
}
-SongList Connection::GetDirectoryRecursive(const std::string &path)
+void Connection::GetDirectoryRecursive(const std::string &directory, SongConsumer f)
{
- SongList result;
if (!itsConnection)
- return result;
+ return;
assert(!isCommandsListEnabled);
GoBusy();
- mpd_send_list_all_meta(itsConnection, path.c_str());
+ mpd_send_list_all_meta(itsConnection, directory.c_str());
while (mpd_song *s = mpd_recv_song(itsConnection))
- result.push_back(Song(s));
+ f(Song(s));
mpd_response_finish(itsConnection);
GoIdle();
- return result;
}
-StringList Connection::GetDirectories(const std::string &path)
+void Connection::GetDirectories(const std::string &directory, StringConsumer f)
{
- StringList result;
if (!itsConnection)
- return result;
+ return;
assert(!isCommandsListEnabled);
GoBusy();
- mpd_send_list_meta(itsConnection, path.c_str());
+ mpd_send_list_meta(itsConnection, directory.c_str());
while (mpd_directory *dir = mpd_recv_directory(itsConnection))
{
- result.push_back(mpd_directory_get_path(dir));
+ f(std::string(mpd_directory_get_path(dir)));
mpd_directory_free(dir);
}
mpd_response_finish(itsConnection);
GoIdle();
- return result;
}
-SongList Connection::GetSongs(const std::string &path)
+void Connection::GetSongs(const std::string &directory, SongConsumer f)
{
- SongList result;
if (!itsConnection)
- return result;
+ return;
assert(!isCommandsListEnabled);
GoBusy();
- mpd_send_list_meta(itsConnection, path.c_str());
+ mpd_send_list_meta(itsConnection, directory.c_str());
while (mpd_song *s = mpd_recv_song(itsConnection))
- result.push_back(Song(s));
+ f(Song(s));
mpd_response_finish(itsConnection);
GoIdle();
- return result;
}
-OutputList Connection::GetOutputs()
+void Connection::GetOutputs(OutputConsumer f)
{
- OutputList result;
if (!itsConnection)
- return result;
+ return;
assert(!isCommandsListEnabled);
GoBusy();
mpd_send_outputs(itsConnection);
while (mpd_output *output = mpd_recv_output(itsConnection))
{
- result.push_back(Output(mpd_output_get_name(output), mpd_output_get_enabled(output)));
+ f(Output(mpd_output_get_name(output), mpd_output_get_enabled(output)));
mpd_output_free(output);
}
mpd_response_finish(itsConnection);
GoIdle();
- return result;
}
bool Connection::EnableOutput(int id)
@@ -1448,40 +1432,36 @@ bool Connection::DisableOutput(int id)
}
}
-StringList Connection::GetURLHandlers()
+void Connection::GetURLHandlers(StringConsumer f)
{
- StringList result;
if (!itsConnection)
- return result;
+ return;
assert(!isCommandsListEnabled);
GoBusy();
mpd_send_list_url_schemes(itsConnection);
while (mpd_pair *handler = mpd_recv_pair_named(itsConnection, "handler"))
{
- result.push_back(handler->value);
+ f(std::string(handler->value));
mpd_return_pair(itsConnection, handler);
}
mpd_response_finish(itsConnection);
GoIdle();
- return result;
}
-StringList Connection::GetTagTypes()
+void Connection::GetTagTypes(StringConsumer f)
{
- StringList result;
if (!itsConnection)
- return result;
+ return;
assert(!isCommandsListEnabled);
GoBusy();
mpd_send_list_tag_types(itsConnection);
while (mpd_pair *tag_type = mpd_recv_pair_named(itsConnection, "tagtype"))
{
- result.push_back(tag_type->value);
+ f(std::string(tag_type->value));
mpd_return_pair(itsConnection, tag_type);
}
mpd_response_finish(itsConnection);
GoIdle();
- return result;
}
int Connection::CheckForErrors()
diff --git a/src/mpdpp.h b/src/mpdpp.h
index c099140d..f312a294 100644
--- a/src/mpdpp.h
+++ b/src/mpdpp.h
@@ -128,11 +128,14 @@ typedef std::vector<Output> OutputList;
class Connection
{
- friend struct Statistics;
-
typedef void (*StatusUpdater) (Connection *, StatusChanges, void *);
typedef void (*ErrorHandler) (Connection *, int, const char *, void *);
+ typedef std::function<void(Item &&)> ItemConsumer;
+ typedef std::function<void(Output &&)> OutputConsumer;
+ typedef std::function<void(Song &&)> SongConsumer;
+ typedef std::function<void(std::string &&)> StringConsumer;
+
public:
Connection();
~Connection();
@@ -197,14 +200,14 @@ public:
unsigned GetBitrate() const { return itsCurrentStatus ? mpd_status_get_kbit_rate(itsCurrentStatus) : 0; }
size_t GetPlaylistLength() const { return itsCurrentStatus ? mpd_status_get_queue_length(itsCurrentStatus) : 0; }
- SongList GetPlaylistChanges(unsigned);
+ void GetPlaylistChanges(unsigned, SongConsumer f);
const std::string &GetErrorMessage() const { return itsErrorMessage; }
Song GetCurrentlyPlayingSong();
int GetCurrentSongPos() const;
Song GetSong(const std::string &);
- SongList GetPlaylistContent(const std::string &);
+ void GetPlaylistContent(const std::string &name, SongConsumer f);
void GetSupportedExtensions(std::set<std::string> &);
@@ -246,28 +249,26 @@ public:
void AddSearch(mpd_tag_type, const std::string &) const;
void AddSearchAny(const std::string &str) const;
void AddSearchURI(const std::string &str) const;
- SongList CommitSearchSongs();
- StringList CommitSearchTags();
+ void CommitSearchSongs(SongConsumer f);
+ void CommitSearchTags(StringConsumer f);
TagMTimeList CommitSearchTagsMTime();
- StringList GetPlaylists();
- StringList GetList(mpd_tag_type);
+ void GetPlaylists(StringConsumer f);
+ void GetList(mpd_tag_type type, StringConsumer f);
TagMTimeList GetListMTime(mpd_tag_type, bool);
- ItemList GetDirectory(const std::string &);
- SongList GetDirectoryRecursive(const std::string &);
- SongList GetSongs(const std::string &);
- StringList GetDirectories(const std::string &);
+ void GetDirectory(const std::string &directory, ItemConsumer f);
+ void GetDirectoryRecursive(const std::string &directory, SongConsumer f);
+ void GetSongs(const std::string &directory, SongConsumer f);
+ void GetDirectories(const std::string &directory, StringConsumer f);
- OutputList GetOutputs();
+ void GetOutputs(OutputConsumer f);
bool EnableOutput(int);
bool DisableOutput(int);
- StringList GetURLHandlers();
- StringList GetTagTypes();
+ void GetURLHandlers(StringConsumer f);
+ void GetTagTypes(StringConsumer f);
private:
- //void check
-
void GoIdle();
int GoBusy();
diff --git a/src/outputs.cpp b/src/outputs.cpp
index 287885ba..18741c2e 100644
--- a/src/outputs.cpp
+++ b/src/outputs.cpp
@@ -99,9 +99,9 @@ void Outputs::mouseButtonPressed(MEVENT me)
void Outputs::FetchList()
{
w.clear();
- auto outputs = Mpd.GetOutputs();
- for (auto o = outputs.begin(); o != outputs.end(); ++o)
- w.addItem(*o, o->isEnabled());
+ Mpd.GetOutputs([this](MPD::Output &&output) {
+ w.addItem(output, output.isEnabled());
+ });
if (myScreen == this)
w.refresh();
}
diff --git a/src/playlist_editor.cpp b/src/playlist_editor.cpp
index c125dbb0..690edc50 100644
--- a/src/playlist_editor.cpp
+++ b/src/playlist_editor.cpp
@@ -130,16 +130,18 @@ void PlaylistEditor::update()
playlistsUpdateRequested = false;
Playlists.clearSearchResults();
withUnfilteredMenuReapplyFilter(Playlists, [this]() {
- auto list = Mpd.GetPlaylists();
- std::sort(list.begin(), list.end(),
+ size_t idx = 0;
+ Mpd.GetPlaylists([this, &idx](std::string &&playlist) {
+ if (idx < Playlists.size())
+ Playlists[idx].value() = playlist;
+ else
+ Playlists.addItem(playlist);
+ ++idx;
+ });
+ if (idx < Playlists.size())
+ Playlists.resizeList(idx);
+ std::sort(Playlists.beginV(), Playlists.endV(),
LocaleBasedSorting(std::locale(), Config.ignore_leading_the));
- auto playlist = list.begin();
- if (Playlists.size() > list.size())
- Playlists.resizeList(list.size());
- for (auto it = Playlists.begin(); it != Playlists.end(); ++it, ++playlist)
- it->value() = *playlist;
- for (; playlist != list.end(); ++playlist)
- Playlists.addItem(*playlist);
});
Playlists.refresh();
}
@@ -149,25 +151,27 @@ void PlaylistEditor::update()
contentUpdateRequested = false;
Content.clearSearchResults();
withUnfilteredMenuReapplyFilter(Content, [this]() {
- auto list = Mpd.GetPlaylistContent(Playlists.current().value());
- auto song = list.begin();
- if (Content.size() > list.size())
- Content.resizeList(list.size());
- for (auto it = Content.begin(); it != Content.end(); ++it, ++song)
- {
- it->value() = *song;
- it->setBold(myPlaylist->checkForSong(*song));
- }
- for (; song != list.end(); ++song)
- Content.addItem(*song, myPlaylist->checkForSong(*song));
+ size_t idx = 0;
+ Mpd.GetPlaylistContent(Playlists.current().value(), [this, &idx](MPD::Song &&s) {
+ if (idx < Content.size())
+ {
+ Content[idx].value() = s;
+ Content[idx].setBold(myPlaylist->checkForSong(s));
+ }
+ else
+ Content.addItem(s, myPlaylist->checkForSong(s));
+ ++idx;
+ });
+ if (idx < Content.size())
+ Content.resizeList(idx);
std::string title;
if (Config.titles_visibility)
{
title = "Playlist content";
title += " (";
- title += unsignedLongIntTo<std::string>::apply(list.size());
+ title += unsignedLongIntTo<std::string>::apply(Content.size());
title += " item";
- if (list.size() == 1)
+ if (Content.size() == 1)
title += ")";
else
title += "s)";
@@ -418,8 +422,9 @@ MPD::SongList PlaylistEditor::getSelectedSongs()
if (it->isSelected())
{
any_selected = true;
- auto songs = Mpd.GetPlaylistContent(it->value());
- result.insert(result.end(), songs.begin(), songs.end());
+ Mpd.GetPlaylistContent(it->value(), [&result](MPD::Song &&s) {
+ result.push_back(s);
+ });
}
}
// we don't check for empty result here as it's possible that
diff --git a/src/search_engine.cpp b/src/search_engine.cpp
index 12327a27..16b608d5 100644
--- a/src/search_engine.cpp
+++ b/src/search_engine.cpp
@@ -410,21 +410,19 @@ void SearchEngine::Search()
Mpd.AddSearch(MPD_TAG_DATE, itsConstraints[9]);
if (!itsConstraints[10].empty())
Mpd.AddSearch(MPD_TAG_COMMENT, itsConstraints[10]);
- auto songs = Mpd.CommitSearchSongs();
- for (auto s = songs.begin(); s != songs.end(); ++s)
- w.addItem(*s);
+ Mpd.CommitSearchSongs([this](MPD::Song &&s) {
+ w.addItem(s);
+ });
return;
}
MPD::SongList list;
if (Config.search_in_db)
- list = Mpd.GetDirectoryRecursive("/");
+ Mpd.GetDirectoryRecursive("/", [&list](MPD::Song &&s) {
+ list.push_back(s);
+ });
else
- {
- list.reserve(myPlaylist->main().size());
- for (auto s = myPlaylist->main().beginV(); s != myPlaylist->main().endV(); ++s)
- list.push_back(*s);
- }
+ list.insert(list.end(), myPlaylist->main().beginV(), myPlaylist->main().endV());
bool any_found = 1;
bool found = 1;
diff --git a/src/sel_items_adder.cpp b/src/sel_items_adder.cpp
index a667e390..ec275992 100644
--- a/src/sel_items_adder.cpp
+++ b/src/sel_items_adder.cpp
@@ -198,16 +198,15 @@ void SelectedItemsAdder::populatePlaylistSelector(BaseScreen *old_screen)
// stored playlists don't support songs from outside of mpd database
if (old_screen != myBrowser || !myBrowser->isLocal())
{
- auto playlists = Mpd.GetPlaylists();
- std::sort(playlists.begin(), playlists.end(),
- LocaleBasedSorting(std::locale(), Config.ignore_leading_the));
- for (auto pl = playlists.begin(); pl != playlists.end(); ++pl)
- {
- m_playlist_selector.addItem(Component::Item::Type(*pl,
- std::bind(&Self::addToExistingPlaylist, this, *pl)
+ size_t begin = m_playlist_selector.size();
+ Mpd.GetPlaylists([this](std::string &&playlist) {
+ m_playlist_selector.addItem(Component::Item::Type(playlist,
+ std::bind(&Self::addToExistingPlaylist, this, playlist)
));
- }
- if (!playlists.empty())
+ });
+ std::sort(m_playlist_selector.beginV()+begin, m_playlist_selector.endV(),
+ LocaleBasedSorting(std::locale(), Config.ignore_leading_the));
+ if (begin < m_playlist_selector.size())
m_playlist_selector.addSeparator();
}
m_playlist_selector.addItem(Component::Item::Type("Cancel",
diff --git a/src/server_info.cpp b/src/server_info.cpp
index a49e5362..762cb228 100644
--- a/src/server_info.cpp
+++ b/src/server_info.cpp
@@ -36,8 +36,12 @@ ServerInfo::ServerInfo()
{
SetDimensions();
w = NC::Scrollpad((COLS-itsWidth)/2, (MainHeight-itsHeight)/2+MainStartY, itsWidth, itsHeight, "MPD server info", Config.main_color, Config.window_border);
- itsURLHandlers = Mpd.GetURLHandlers();
- itsTagTypes = Mpd.GetTagTypes();
+ Mpd.GetURLHandlers([this](std::string &&handler) {
+ itsURLHandlers.push_back(handler);
+ });
+ Mpd.GetTagTypes([this](std::string &&tag_type) {
+ itsTagTypes.push_back(tag_type);
+ });
}
void ServerInfo::switchTo()
diff --git a/src/status.cpp b/src/status.cpp
index 53f1e3be..db81dfec 100644
--- a/src/status.cpp
+++ b/src/status.cpp
@@ -141,21 +141,19 @@ void Status::Changes::playlist()
myPlaylist->main().resizeList(playlist_length);
}
- auto songs = Mpd.GetPlaylistChanges(Mpd.GetOldPlaylistID());
- for (auto s = songs.begin(); s != songs.end(); ++s)
- {
- size_t pos = s->getPosition();
+ Mpd.GetPlaylistChanges(Mpd.GetOldPlaylistID(), [](MPD::Song &&s) {
+ size_t pos = s.getPosition();
if (pos < myPlaylist->main().size())
{
// if song's already in playlist, replace it with a new one
MPD::Song &old_s = myPlaylist->main()[pos].value();
myPlaylist->unregisterHash(old_s.getHash());
- old_s = *s;
+ old_s = s;
}
else // otherwise just add it to playlist
- myPlaylist->main().addItem(*s);
- myPlaylist->registerHash(s->getHash());
- }
+ myPlaylist->main().addItem(s);
+ myPlaylist->registerHash(s.getHash());
+ });
});
if (Mpd.isPlaying())
diff --git a/src/tag_editor.cpp b/src/tag_editor.cpp
index 2e5b7cb6..2fea1ed0 100644
--- a/src/tag_editor.cpp
+++ b/src/tag_editor.cpp
@@ -226,39 +226,28 @@ void TagEditor::update()
Dirs->Window::clear();
Tags->clear();
- int highlightme = -1;
- auto dirs = Mpd.GetDirectories(itsBrowsedDir);
- std::sort(dirs.begin(), dirs.end(), LocaleBasedSorting(std::locale(), Config.ignore_leading_the));
if (itsBrowsedDir != "/")
- {
- size_t slash = itsBrowsedDir.rfind("/");
- std::string parent = slash != std::string::npos ? itsBrowsedDir.substr(0, slash) : "/";
- Dirs->addItem(make_pair("..", parent));
- }
+ Dirs->addItem(std::make_pair("..", getParentDirectory(itsBrowsedDir)));
else
Dirs->addItem(std::make_pair(".", "/"));
- for (auto dir = dirs.begin(); dir != dirs.end(); ++dir)
- {
- size_t slash = dir->rfind("/");
- std::string to_display = slash != std::string::npos ? dir->substr(slash+1) : *dir;
- Dirs->addItem(make_pair(to_display, *dir));
- if (*dir == itsHighlightedDir)
- highlightme = Dirs->size()-1;
- }
- if (highlightme != -1)
- Dirs->highlight(highlightme);
-
+ Mpd.GetDirectories(itsBrowsedDir, [this](std::string &&directory) {
+ Dirs->addItem(std::make_pair(getBasename(directory), directory));
+ if (directory == itsHighlightedDir)
+ Dirs->highlight(Dirs->size()-1);
+ });
+ std::sort(Dirs->beginV()+1, Dirs->endV(),
+ LocaleBasedSorting(std::locale(), Config.ignore_leading_the));
Dirs->display();
}
if (Tags->reallyEmpty())
{
Tags->reset();
- auto songs = Mpd.GetSongs(Dirs->current().value().second);
- std::sort(songs.begin(), songs.end(),
+ Mpd.GetSongs(Dirs->current().value().second, [this](MPD::Song &&s) {
+ Tags->addItem(s);
+ });
+ std::sort(Tags->beginV(), Tags->endV(),
LocaleBasedSorting(std::locale(), Config.ignore_leading_the));
- for (auto s = songs.begin(); s != songs.end(); ++s)
- Tags->addItem(*s);
Tags->refresh();
}
@@ -279,8 +268,11 @@ void TagEditor::enterPressed()
if (w == Dirs)
{
- auto dirs = Mpd.GetDirectories(Dirs->current().value().second);
- if (!dirs.empty())
+ bool has_subdirs = false;
+ Mpd.GetDirectories(Dirs->current().value().second, [&has_subdirs](std::string &&) {
+ has_subdirs = true;
+ });
+ if (has_subdirs)
{
itsHighlightedDir = itsBrowsedDir;
itsBrowsedDir = Dirs->current().value().second;
diff --git a/src/utility/comparators.h b/src/utility/comparators.h
index b5d0e2c9..5c8fccd7 100644
--- a/src/utility/comparators.h
+++ b/src/utility/comparators.h
@@ -22,6 +22,7 @@
#define _UTILITY_COMPARATORS
#include <string>
+#include "exec_item.h"
#include "mpdpp.h"
#include "settings.h"
#include "menu.h"
@@ -57,6 +58,11 @@ public:
bool operator()(const std::pair<A, B> &a, const std::pair<A, B> &b) const {
return m_cmp(a.first, b.first) < 0;
}
+
+ template <typename ItemT, typename FunT>
+ bool operator()(const ExecItem<ItemT, FunT> &a, const ExecItem<ItemT, FunT> &b) const {
+ return m_cmp(a.item(), b.item());
+ }
};
class LocaleBasedItemSorting
diff --git a/src/visualizer.cpp b/src/visualizer.cpp
index 8ebd2004..2c1e3611 100644
--- a/src/visualizer.cpp
+++ b/src/visualizer.cpp
@@ -217,11 +217,12 @@ void Visualizer::FindOutputID()
m_output_id = -1;
if (!Config.visualizer_output_name.empty())
{
- size_t i = 0;
- auto outputs = Mpd.GetOutputs();
- for (auto o = outputs.begin(); o != outputs.end(); ++o, ++i)
- if (o->name() == Config.visualizer_output_name)
- m_output_id = i;
+ size_t idx = 0;
+ Mpd.GetOutputs([this, &idx](MPD::Output &&output) {
+ if (output.name() == Config.visualizer_output_name)
+ m_output_id = idx;
+ ++idx;
+ });
if (m_output_id == -1)
Statusbar::msg("There is no output named \"%s\"", Config.visualizer_output_name.c_str());
}