do not delete items from playlist if sending command delete failed

This commit is contained in:
Andrzej Rybczak
2009-11-05 22:16:54 +01:00
parent 60d932ef68
commit 5b2dbe026c
3 changed files with 41 additions and 36 deletions

View File

@@ -36,7 +36,6 @@ const char *MPD::Message::FunctionDisabledFilteringEnabled = "Function disabled
Connection::Connection() : itsConnection(0), Connection::Connection() : itsConnection(0),
isCommandsListEnabled(0), isCommandsListEnabled(0),
itsErrorCode(0),
itsMaxPlaylistLength(-1), itsMaxPlaylistLength(-1),
isIdle(0), isIdle(0),
supportsIdle(0), supportsIdle(0),
@@ -893,45 +892,47 @@ bool Connection::AddRandomSongs(size_t number)
return true; return true;
} }
void Connection::Delete(unsigned pos) bool Connection::Delete(unsigned pos)
{ {
if (!itsConnection) if (!itsConnection)
return; return false;
if (!isCommandsListEnabled) if (!isCommandsListEnabled)
GoBusy(); GoBusy();
else else
assert(!isIdle); assert(!isIdle);
mpd_send_delete(itsConnection, pos); bool result = mpd_send_delete(itsConnection, pos);
if (!isCommandsListEnabled) 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) if (!itsConnection)
return; return false;
if (!isCommandsListEnabled) if (!isCommandsListEnabled)
GoBusy(); GoBusy();
else else
assert(!isIdle); assert(!isIdle);
mpd_send_delete_id(itsConnection, id); bool result = mpd_send_delete_id(itsConnection, id);
if (!isCommandsListEnabled) 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) if (!itsConnection)
return; return false;
if (!isCommandsListEnabled) if (!isCommandsListEnabled)
{ {
GoBusy(); GoBusy();
mpd_run_playlist_delete(itsConnection, playlist.c_str(), pos); return mpd_run_playlist_delete(itsConnection, playlist.c_str(), pos);
} }
else else
{ {
assert(!isIdle); 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) if (GetPlaylistLength() == itsMaxPlaylistLength && itsErrorHandler)
itsErrorHandler(this, MPD_SERVER_ERROR_PLAYLIST_MAX, Message::FullPlaylist, itsErrorHandlerUserdata); itsErrorHandler(this, MPD_SERVER_ERROR_PLAYLIST_MAX, Message::FullPlaylist, itsErrorHandlerUserdata);
isCommandsListEnabled = 0; isCommandsListEnabled = 0;
bool result = !CheckForErrors();
UpdateStatus(); UpdateStatus();
return !CheckForErrors(); return result;
} }
bool Connection::DeletePlaylist(const std::string &name) bool Connection::DeletePlaylist(const std::string &name)
@@ -1241,23 +1243,24 @@ void Connection::GetTagTypes(TagList &v)
int Connection::CheckForErrors() 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); 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 // 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() // setting real max playlist length is in UpdateStatus()
itsErrorCode = mpd_connection_get_server_error(itsConnection); error_code = mpd_connection_get_server_error(itsConnection);
if (itsErrorCode == MPD_SERVER_ERROR_PLAYLIST_MAX && itsMaxPlaylistLength == size_t(-1)) if (error_code == MPD_SERVER_ERROR_PLAYLIST_MAX && itsMaxPlaylistLength == size_t(-1))
itsMaxPlaylistLength = 0; itsMaxPlaylistLength = 0;
} }
if (!mpd_connection_clear_error(itsConnection)) if (!mpd_connection_clear_error(itsConnection))
Disconnect(); Disconnect();
if (itsErrorHandler) 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) void MPD::FreeSongList(SongList &l)

View File

@@ -154,7 +154,6 @@ namespace MPD
void GetPlaylistChanges(unsigned, SongList &); void GetPlaylistChanges(unsigned, SongList &);
const std::string & GetErrorMessage() const { return itsErrorMessage; } const std::string & GetErrorMessage() const { return itsErrorMessage; }
int GetErrorCode() const { return itsErrorCode; }
Song GetCurrentSong(); Song GetCurrentSong();
int GetCurrentSongPos() const; int GetCurrentSongPos() const;
@@ -175,9 +174,9 @@ namespace MPD
int AddSong(const Song &, int = -1); // returns id of added song int AddSong(const Song &, int = -1); // returns id of added song
bool AddRandomSongs(size_t); bool AddRandomSongs(size_t);
void Add(const std::string &path); void Add(const std::string &path);
void Delete(unsigned); bool Delete(unsigned);
void DeleteID(unsigned); bool DeleteID(unsigned);
void Delete(const std::string &, unsigned); bool Delete(const std::string &, unsigned);
void StartCommandsList(); void StartCommandsList();
bool CommitCommandsList(); bool CommitCommandsList();
@@ -220,7 +219,6 @@ namespace MPD
bool isCommandsListEnabled; bool isCommandsListEnabled;
std::string itsErrorMessage; std::string itsErrorMessage;
int itsErrorCode;
size_t itsMaxPlaylistLength; size_t itsMaxPlaylistLength;
int itsFD; int itsFD;

View File

@@ -586,20 +586,20 @@ int main(int argc, char *argv[])
{ {
if (!myPlaylist->Items->Empty() && myScreen == myPlaylist) if (!myPlaylist->Items->Empty() && myScreen == myPlaylist)
{ {
Playlist::BlockUpdate = 1;
if (myPlaylist->Items->hasSelected()) if (myPlaylist->Items->hasSelected())
{ {
std::vector<size_t> list; std::vector<size_t> list;
myPlaylist->Items->GetSelected(list); myPlaylist->Items->GetSelected(list);
Mpd.StartCommandsList(); Mpd.StartCommandsList();
for (std::vector<size_t>::reverse_iterator it = list.rbegin(); it != list.rend(); ++it) for (std::vector<size_t>::reverse_iterator it = list.rbegin(); it != list.rend(); ++it)
{
Mpd.DeleteID((*myPlaylist->Items)[*it].GetID()); 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 else
{ {
@@ -615,11 +615,15 @@ int main(int argc, char *argv[])
// needed for keeping proper position of now playing song. // needed for keeping proper position of now playing song.
if (myPlaylist->NowPlaying > int(myPlaylist->CurrentSong()->GetPosition())-del_counter) if (myPlaylist->NowPlaying > int(myPlaylist->CurrentSong()->GetPosition())-del_counter)
--myPlaylist->NowPlaying; --myPlaylist->NowPlaying;
Mpd.DeleteID(myPlaylist->CurrentSong()->GetID()); if (Mpd.DeleteID(myPlaylist->CurrentSong()->GetID()))
myPlaylist->Items->DeleteOption(id); {
myPlaylist->Items->Refresh(); myPlaylist->Items->DeleteOption(id);
wFooter->ReadKey(input); myPlaylist->Items->Refresh();
++del_counter; wFooter->ReadKey(input);
++del_counter;
}
else
break;
} }
myPlaylist->FixPositions(myPlaylist->Items->Choice()); myPlaylist->FixPositions(myPlaylist->Items->Choice());
wFooter->SetTimeout(ncmpcpp_window_timeout); wFooter->SetTimeout(ncmpcpp_window_timeout);