handle possible errors while saving playlist properly

This commit is contained in:
Andrzej Rybczak
2010-08-22 22:04:57 +02:00
parent 6af99edcc1
commit 1735a8c7db
3 changed files with 38 additions and 30 deletions

View File

@@ -1027,7 +1027,7 @@ bool MPD::Connection::DeletePlaylist(const std::string &name)
}
}
bool MPD::Connection::SavePlaylist(const std::string &name)
int MPD::Connection::SavePlaylist(const std::string &name)
{
if (!itsConnection)
return false;
@@ -1035,8 +1035,12 @@ bool MPD::Connection::SavePlaylist(const std::string &name)
GoBusy();
mpd_send_save(itsConnection, name.c_str());
mpd_response_finish(itsConnection);
return !(mpd_connection_get_error(itsConnection) == MPD_ERROR_SERVER
&& mpd_connection_get_server_error(itsConnection) == MPD_SERVER_ERROR_EXIST);
if (mpd_connection_get_error(itsConnection) == MPD_ERROR_SERVER
&& mpd_connection_get_server_error(itsConnection) == MPD_SERVER_ERROR_EXIST)
return MPD_SERVER_ERROR_EXIST;
else
return CheckForErrors();
}
void MPD::Connection::GetPlaylists(TagList &v)
@@ -1295,7 +1299,7 @@ void MPD::Connection::GetTagTypes(TagList &v)
int MPD::Connection::CheckForErrors()
{
int error_code = 0;
int error_code = MPD_ERROR_SUCCESS;
if ((error_code = mpd_connection_get_error(itsConnection)) != MPD_ERROR_SUCCESS)
{
itsErrorMessage = mpd_connection_get_error_message(itsConnection);

View File

@@ -184,7 +184,7 @@ namespace MPD
bool CommitCommandsList();
bool DeletePlaylist(const std::string &);
bool SavePlaylist(const std::string &);
int SavePlaylist(const std::string &);
void ClearPlaylist(const std::string &);
void AddToPlaylist(const std::string &, const Song &);
void AddToPlaylist(const std::string &, const std::string &);

View File

@@ -870,37 +870,41 @@ int main(int argc, char *argv[])
if (Mpd.GetErrorMessage().empty())
ShowMessage("Filtered items added to playlist \"%s\"", playlist_name.c_str());
}
else if (Mpd.SavePlaylist(real_playlist_name))
{
ShowMessage("Playlist saved as: %s", playlist_name.c_str());
if (myPlaylistEditor->Main()) // check if initialized
myPlaylistEditor->Playlists->Clear(); // make playlist's list update itself
}
else
{
LockStatusbar();
Statusbar() << "Playlist already exists, overwrite: " << playlist_name << " ? [" << fmtBold << 'y' << fmtBoldEnd << '/' << fmtBold << 'n' << fmtBoldEnd << "] ";
wFooter->Refresh();
int answer = 0;
while (answer != 'y' && answer != 'n')
int result = Mpd.SavePlaylist(real_playlist_name);
if (result == MPD_ERROR_SUCCESS)
{
TraceMpdStatus();
wFooter->ReadKey(answer);
ShowMessage("Playlist saved as: %s", playlist_name.c_str());
if (myPlaylistEditor->Main()) // check if initialized
myPlaylistEditor->Playlists->Clear(); // make playlist's list update itself
}
UnlockStatusbar();
else if (result == MPD_SERVER_ERROR_EXIST)
{
LockStatusbar();
Statusbar() << "Playlist already exists, overwrite: " << playlist_name << " ? [" << fmtBold << 'y' << fmtBoldEnd << '/' << fmtBold << 'n' << fmtBoldEnd << "] ";
wFooter->Refresh();
int answer = 0;
while (answer != 'y' && answer != 'n')
{
TraceMpdStatus();
wFooter->ReadKey(answer);
}
UnlockStatusbar();
if (answer == 'y')
{
Mpd.DeletePlaylist(real_playlist_name);
if (Mpd.SavePlaylist(real_playlist_name))
ShowMessage("Playlist overwritten!");
if (answer == 'y')
{
Mpd.DeletePlaylist(real_playlist_name);
if (Mpd.SavePlaylist(real_playlist_name) == MPD_ERROR_SUCCESS)
ShowMessage("Playlist overwritten!");
}
else
ShowMessage("Aborted!");
if (myPlaylistEditor->Main()) // check if initialized
myPlaylistEditor->Playlists->Clear(); // make playlist's list update itself
if (myScreen == myPlaylist)
myPlaylist->EnableHighlighting();
}
else
ShowMessage("Aborted!");
if (myPlaylistEditor->Main()) // check if initialized
myPlaylistEditor->Playlists->Clear(); // make playlist's list update itself
if (myScreen == myPlaylist)
myPlaylist->EnableHighlighting();
}
}
if (myBrowser->Main()