From 5b2dbe026c1b94fc4a43e5bac493095c9222a0cc Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Thu, 5 Nov 2009 22:16:54 +0100 Subject: [PATCH] do not delete items from playlist if sending command delete failed --- src/mpdpp.cpp | 43 +++++++++++++++++++++++-------------------- src/mpdpp.h | 8 +++----- src/ncmpcpp.cpp | 26 +++++++++++++++----------- 3 files changed, 41 insertions(+), 36 deletions(-) diff --git a/src/mpdpp.cpp b/src/mpdpp.cpp index 749f8e9d..70ab90eb 100644 --- a/src/mpdpp.cpp +++ b/src/mpdpp.cpp @@ -36,7 +36,6 @@ const char *MPD::Message::FunctionDisabledFilteringEnabled = "Function disabled Connection::Connection() : itsConnection(0), isCommandsListEnabled(0), - itsErrorCode(0), itsMaxPlaylistLength(-1), isIdle(0), supportsIdle(0), @@ -893,45 +892,47 @@ bool Connection::AddRandomSongs(size_t number) return true; } -void Connection::Delete(unsigned pos) +bool Connection::Delete(unsigned pos) { if (!itsConnection) - return; + return false; if (!isCommandsListEnabled) GoBusy(); else assert(!isIdle); - mpd_send_delete(itsConnection, pos); + bool result = mpd_send_delete(itsConnection, pos); if (!isCommandsListEnabled) - mpd_response_finish(itsConnection); + result = mpd_response_finish(itsConnection); + return result; } -void Connection::DeleteID(unsigned id) +bool Connection::DeleteID(unsigned id) { if (!itsConnection) - return; + return false; if (!isCommandsListEnabled) GoBusy(); else assert(!isIdle); - mpd_send_delete_id(itsConnection, id); + bool result = mpd_send_delete_id(itsConnection, id); if (!isCommandsListEnabled) - mpd_response_finish(itsConnection); + result = mpd_response_finish(itsConnection); + return result; } -void Connection::Delete(const std::string &playlist, unsigned pos) +bool Connection::Delete(const std::string &playlist, unsigned pos) { if (!itsConnection) - return; + return false; if (!isCommandsListEnabled) { GoBusy(); - mpd_run_playlist_delete(itsConnection, playlist.c_str(), pos); + return mpd_run_playlist_delete(itsConnection, playlist.c_str(), pos); } else { assert(!isIdle); - mpd_send_playlist_delete(itsConnection, playlist.c_str(), pos); + return mpd_send_playlist_delete(itsConnection, playlist.c_str(), pos); } } @@ -956,8 +957,9 @@ bool Connection::CommitCommandsList() if (GetPlaylistLength() == itsMaxPlaylistLength && itsErrorHandler) itsErrorHandler(this, MPD_SERVER_ERROR_PLAYLIST_MAX, Message::FullPlaylist, itsErrorHandlerUserdata); isCommandsListEnabled = 0; + bool result = !CheckForErrors(); UpdateStatus(); - return !CheckForErrors(); + return result; } bool Connection::DeletePlaylist(const std::string &name) @@ -1241,23 +1243,24 @@ void Connection::GetTagTypes(TagList &v) int Connection::CheckForErrors() { - if ((itsErrorCode = mpd_connection_get_error(itsConnection)) != MPD_ERROR_SUCCESS) + int error_code = 0; + if ((error_code = mpd_connection_get_error(itsConnection)) != MPD_ERROR_SUCCESS) { itsErrorMessage = mpd_connection_get_error_message(itsConnection); - if (itsErrorCode == MPD_ERROR_SERVER) + if (error_code == MPD_ERROR_SERVER) { // this is to avoid setting too small max size as we check it before fetching current status // setting real max playlist length is in UpdateStatus() - itsErrorCode = mpd_connection_get_server_error(itsConnection); - if (itsErrorCode == MPD_SERVER_ERROR_PLAYLIST_MAX && itsMaxPlaylistLength == size_t(-1)) + error_code = mpd_connection_get_server_error(itsConnection); + if (error_code == MPD_SERVER_ERROR_PLAYLIST_MAX && itsMaxPlaylistLength == size_t(-1)) itsMaxPlaylistLength = 0; } if (!mpd_connection_clear_error(itsConnection)) Disconnect(); if (itsErrorHandler) - itsErrorHandler(this, itsErrorCode, itsErrorMessage.c_str(), itsErrorHandlerUserdata); + itsErrorHandler(this, error_code, itsErrorMessage.c_str(), itsErrorHandlerUserdata); } - return itsErrorCode; + return error_code; } void MPD::FreeSongList(SongList &l) diff --git a/src/mpdpp.h b/src/mpdpp.h index a3850dc0..4cda1e7b 100644 --- a/src/mpdpp.h +++ b/src/mpdpp.h @@ -154,7 +154,6 @@ namespace MPD void GetPlaylistChanges(unsigned, SongList &); const std::string & GetErrorMessage() const { return itsErrorMessage; } - int GetErrorCode() const { return itsErrorCode; } Song GetCurrentSong(); int GetCurrentSongPos() const; @@ -175,9 +174,9 @@ namespace MPD int AddSong(const Song &, int = -1); // returns id of added song bool AddRandomSongs(size_t); void Add(const std::string &path); - void Delete(unsigned); - void DeleteID(unsigned); - void Delete(const std::string &, unsigned); + bool Delete(unsigned); + bool DeleteID(unsigned); + bool Delete(const std::string &, unsigned); void StartCommandsList(); bool CommitCommandsList(); @@ -220,7 +219,6 @@ namespace MPD bool isCommandsListEnabled; std::string itsErrorMessage; - int itsErrorCode; size_t itsMaxPlaylistLength; int itsFD; diff --git a/src/ncmpcpp.cpp b/src/ncmpcpp.cpp index be7cd5f1..4ebd3836 100644 --- a/src/ncmpcpp.cpp +++ b/src/ncmpcpp.cpp @@ -586,20 +586,20 @@ int main(int argc, char *argv[]) { if (!myPlaylist->Items->Empty() && myScreen == myPlaylist) { - Playlist::BlockUpdate = 1; if (myPlaylist->Items->hasSelected()) { std::vector list; myPlaylist->Items->GetSelected(list); Mpd.StartCommandsList(); for (std::vector::reverse_iterator it = list.rbegin(); it != list.rend(); ++it) - { Mpd.DeleteID((*myPlaylist->Items)[*it].GetID()); - myPlaylist->Items->DeleteOption(*it); + if (Mpd.CommitCommandsList()) + { + for (size_t i = 0; i < myPlaylist->Items->Size(); ++i) + myPlaylist->Items->Select(i, 0); + myPlaylist->FixPositions(list.front()); + ShowMessage("Selected items deleted!"); } - Mpd.CommitCommandsList(); - myPlaylist->FixPositions(list.front()); - ShowMessage("Selected items deleted!"); } else { @@ -615,11 +615,15 @@ int main(int argc, char *argv[]) // needed for keeping proper position of now playing song. if (myPlaylist->NowPlaying > int(myPlaylist->CurrentSong()->GetPosition())-del_counter) --myPlaylist->NowPlaying; - Mpd.DeleteID(myPlaylist->CurrentSong()->GetID()); - myPlaylist->Items->DeleteOption(id); - myPlaylist->Items->Refresh(); - wFooter->ReadKey(input); - ++del_counter; + if (Mpd.DeleteID(myPlaylist->CurrentSong()->GetID())) + { + myPlaylist->Items->DeleteOption(id); + myPlaylist->Items->Refresh(); + wFooter->ReadKey(input); + ++del_counter; + } + else + break; } myPlaylist->FixPositions(myPlaylist->Items->Choice()); wFooter->SetTimeout(ncmpcpp_window_timeout);