diff options
author | Andrzej Rybczak <andrzej@rybczak.net> | 2020-12-20 00:49:21 +0100 |
---|---|---|
committer | Andrzej Rybczak <andrzej@rybczak.net> | 2020-12-20 00:49:21 +0100 |
commit | ab27472b0b4619e84833217b431f23effe9f1b1c (patch) | |
tree | 1794745e3ee40c6dad65ec7537574bdbfce355c9 | |
parent | b68354a2235f6c2fdd8683c841a7c8cecaece3fe (diff) |
Optimize deletion/cropping of main playlist by using ranges
-rw-r--r-- | src/actions.cpp | 6 | ||||
-rw-r--r-- | src/helpers.cpp | 24 | ||||
-rw-r--r-- | src/helpers.h | 2 | ||||
-rw-r--r-- | src/mpdpp.cpp | 11 | ||||
-rw-r--r-- | src/mpdpp.h | 1 |
5 files changed, 41 insertions, 3 deletions
diff --git a/src/actions.cpp b/src/actions.cpp index 228e7f6b..2ea210c9 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -681,8 +681,7 @@ void DeletePlaylistItems::run() if (myScreen == myPlaylist) { Statusbar::print("Deleting items..."); - auto delete_fun = std::bind(&MPD::Connection::Delete, ph::_1, ph::_2); - deleteSelectedSongs(myPlaylist->main(), delete_fun); + deleteSelectedSongsFromPlaylist(myPlaylist->main()); Statusbar::print("Item(s) deleted"); } else if (myScreen->isActiveWindow(myPlaylistEditor->Content)) @@ -1926,7 +1925,8 @@ void CropMainPlaylist::run() confirmAction("Do you really want to crop main playlist?"); Statusbar::print("Cropping playlist..."); selectCurrentIfNoneSelected(w); - cropPlaylist(w, std::bind(&MPD::Connection::Delete, ph::_1, ph::_2)); + reverseSelectionHelper(w.begin(), w.end()); + deleteSelectedSongsFromPlaylist(w); Statusbar::print("Playlist cropped"); } diff --git a/src/helpers.cpp b/src/helpers.cpp index de58ef6e..cdff5517 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -76,6 +76,30 @@ MPD::SongIterator getDatabaseIterator(MPD::Connection &mpd) return result; } +void deleteSelectedSongsFromPlaylist(NC::Menu<MPD::Song> &playlist) +{ + selectCurrentIfNoneSelected(playlist); + boost::optional<int> range_end; + Mpd.StartCommandsList(); + for (auto &s : boost::adaptors::reverse(playlist)) + { + if (s.isSelected()) + { + s.setSelected(false); + if (range_end == boost::none) + range_end = s.value().getPosition() + 1; + } + else if (range_end != boost::none) + { + Mpd.DeleteRange(s.value().getPosition() + 1, *range_end); + range_end.reset(); + } + } + if (range_end != boost::none) + Mpd.DeleteRange(0, *range_end); + Mpd.CommitCommandsList(); +} + void removeSongFromPlaylist(const SongMenu &playlist, const MPD::Song &s) { Mpd.StartCommandsList(); diff --git a/src/helpers.h b/src/helpers.h index 48dff6e3..df05103a 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -528,6 +528,8 @@ inline const char *withErrors(bool success) return success ? "" : " " "(with errors)"; } +void deleteSelectedSongsFromPlaylist(NC::Menu<MPD::Song> &playlist); + bool addSongToPlaylist(const MPD::Song &s, bool play, int position = -1); const MPD::Song *currentSong(const BaseScreen *screen); diff --git a/src/mpdpp.cpp b/src/mpdpp.cpp index 05c127e4..ccdee31d 100644 --- a/src/mpdpp.cpp +++ b/src/mpdpp.cpp @@ -655,6 +655,17 @@ void Connection::Delete(unsigned pos) } } +void Connection::DeleteRange(unsigned begin, unsigned end) +{ + prechecks(); + mpd_send_delete_range(m_connection.get(), begin, end); + if (!m_command_list_active) + { + mpd_response_finish(m_connection.get()); + checkErrors(); + } +} + void Connection::PlaylistDelete(const std::string &playlist, unsigned pos) { prechecks(); diff --git a/src/mpdpp.h b/src/mpdpp.h index 5f56187a..d13eb9c4 100644 --- a/src/mpdpp.h +++ b/src/mpdpp.h @@ -550,6 +550,7 @@ struct Connection bool AddRandomSongs(size_t number, const std::string &random_exclude_pattern, std::mt19937 &rng); bool Add(const std::string &path); void Delete(unsigned int pos); + void DeleteRange(unsigned begin, unsigned end); void PlaylistDelete(const std::string &playlist, unsigned int pos); void StartCommandsList(); void CommitCommandsList(); |