mpd: make GetRecursiveDirectory return SongIterator

This commit is contained in:
Andrzej Rybczak
2014-11-02 23:08:21 +01:00
parent afa8a34340
commit e5b0c7a357
5 changed files with 44 additions and 33 deletions

View File

@@ -346,16 +346,14 @@ MPD::SongList Browser::getSelectedSongs()
{
case MPD::Item::Type::Directory:
if (m_local_browser)
{
MPD::ItemList items;
getLocalDirectoryRecursively(songs, item.directory().path());
}
else
{
MPD::ItemIterator it = Mpd.GetDirectoryRecursive(item.directory().path()), end;
for (; it != end; ++it)
if (it->type() == MPD::Item::Type::Song)
songs.push_back(std::move(it->song()));
std::copy(
std::make_move_iterator(Mpd.GetDirectoryRecursive(item.directory().path())),
std::make_move_iterator(MPD::SongIterator()),
std::back_inserter(songs)
);
}
break;
case MPD::Item::Type::Song:

View File

@@ -259,24 +259,20 @@ void MediaLibrary::update()
Albums.clearSearchResults();
m_albums_update_request = false;
std::map<std::tuple<std::string, std::string, std::string>, time_t> albums;
MPD::ItemIterator item = Mpd.GetDirectoryRecursive("/"), end;
for (; item != end; ++item)
for (MPD::SongIterator s = Mpd.GetDirectoryRecursive("/"), end; s != end; ++s)
{
if (item->type() != MPD::Item::Type::Song)
continue;
unsigned idx = 0;
const MPD::Song &s = item->song();
std::string tag = s.get(Config.media_lib_primary_tag, idx);
std::string tag = s->get(Config.media_lib_primary_tag, idx);
do
{
auto key = std::make_tuple(tag, s.getAlbum(), s.getDate());
auto key = std::make_tuple(tag, s->getAlbum(), s->getDate());
auto it = albums.find(key);
if (it == albums.end())
albums[key] = s.getMTime();
albums[key] = s->getMTime();
else
it->second = s.getMTime();
it->second = s->getMTime();
}
while (!(tag = s.get(Config.media_lib_primary_tag, ++idx)).empty());
while (!(tag = s->get(Config.media_lib_primary_tag, ++idx)).empty());
}
withUnfilteredMenuReapplyFilter(Albums, [this, &albums]() {
size_t idx = 0;
@@ -308,23 +304,19 @@ void MediaLibrary::update()
std::map<std::string, time_t> tags;
if (Config.media_library_sort_by_mtime)
{
MPD::ItemIterator item = Mpd.GetDirectoryRecursive("/"), end;
for (; item != end; ++item)
for (MPD::SongIterator s = Mpd.GetDirectoryRecursive("/"), end; s != end; ++s)
{
if (item->type() != MPD::Item::Type::Song)
continue;
unsigned idx = 0;
const MPD::Song &s = item->song();
std::string tag = s.get(Config.media_lib_primary_tag, idx);
std::string tag = s->get(Config.media_lib_primary_tag, idx);
do
{
auto it = tags.find(tag);
if (it == tags.end())
tags[tag] = s.getMTime();
tags[tag] = s->getMTime();
else
it->second = std::max(it->second, s.getMTime());
it->second = std::max(it->second, s->getMTime());
}
while (!(tag = s.get(Config.media_lib_primary_tag, ++idx)).empty());
while (!(tag = s->get(Config.media_lib_primary_tag, ++idx)).empty());
}
}
else

View File

@@ -43,6 +43,24 @@ bool fetchItem(MPD::ItemIterator::State &state)
return false;
}
bool fetchItemSong(MPD::SongIterator::State &state)
{
auto src = mpd_recv_entity(state.connection());
while (src != nullptr && mpd_entity_get_type(src) != MPD_ENTITY_TYPE_SONG)
{
mpd_entity_free(src);
src = mpd_recv_entity(state.connection());
}
if (src != nullptr)
{
state.setObject(mpd_song_dup(mpd_entity_get_song(src)));
mpd_entity_free(src);
return true;
}
else
return false;
}
bool fetchPlaylist(MPD::PlaylistIterator::State &state)
{
auto src = mpd_recv_playlist(state.connection());
@@ -737,12 +755,12 @@ ItemIterator Connection::GetDirectory(const std::string &directory)
return ItemIterator(m_connection.get(), fetchItem);
}
ItemIterator Connection::GetDirectoryRecursive(const std::string &directory)
SongIterator Connection::GetDirectoryRecursive(const std::string &directory)
{
prechecksNoCommandsList();
mpd_send_list_all_meta(m_connection.get(), directory.c_str());
checkErrors();
return ItemIterator(m_connection.get(), fetchItem);
return SongIterator(m_connection.get(), fetchItemSong);
}
void Connection::GetDirectories(const std::string &directory, StringConsumer f)

View File

@@ -547,7 +547,7 @@ public:
PlaylistIterator GetPlaylists();
void GetList(mpd_tag_type type, StringConsumer f);
ItemIterator GetDirectory(const std::string &directory);
ItemIterator GetDirectoryRecursive(const std::string &directory);
SongIterator GetDirectoryRecursive(const std::string &directory);
SongIterator GetSongs(const std::string &directory);
void GetDirectories(const std::string &directory, StringConsumer f);

View File

@@ -447,17 +447,20 @@ void SearchEngine::Search()
MPD::SongList list;
if (Config.search_in_db)
{
MPD::ItemIterator item = Mpd.GetDirectoryRecursive("/"), end;
for (; item != end; ++item)
if (item->type() != MPD::Item::Type::Song)
list.push_back(std::move(item->song()));
std::copy(
std::make_move_iterator(Mpd.GetDirectoryRecursive("/")),
std::make_move_iterator(MPD::SongIterator()),
std::back_inserter(list)
);
}
else
{
std::copy(
myPlaylist->main().beginV(),
myPlaylist->main().endV(),
std::back_inserter(list)
);
}
bool any_found = 1;
bool found = 1;