bool MPDConnection::finishCommand() { if (d->connection->error) { QString errormsg = QString::fromUtf8(d->connection->errorStr).replace("\n", ""); switch (d->connection->error) { case MPD_ERROR_TIMEOUT: case MPD_ERROR_SYSTEM: case MPD_ERROR_UNKHOST: case MPD_ERROR_CONNPORT: case MPD_ERROR_NOTMPD: case MPD_ERROR_NORESPONSE: case MPD_ERROR_SENDING: case MPD_ERROR_CONNCLOSED: // These are the serious errors. Set errormsg. qWarning("Error: `%s (error code %d). Disconnecting.", qPrintable(errormsg), d->connection->error); disconnectFromMPD(errormsg); return false; default: // Just warnings. We ignore them. qWarning("Warning: `%s (error code %d). Ignoring.", qPrintable(errormsg), d->connection->error); } qWarning("Problem occured while executing command: %s, called from: %s", qPrintable(d->command), qPrintable(d->caller)); d->caller = d->command = QString(); mpd_clearError(d->connection); return false; } else mpd_finishCommand(d->connection); // Clear error code, just in case. mpd_clearError(d->connection); return true; }
/* 0 - connected * 1 - was error, connection restored * -1 - unable to connect to MPD */ int mpdc_tickle() { char err = 0; if (mpdz == NULL) { mpdc_init(); } else if (mpdz->error) { pz_error(mpdz->errorStr); mpd_clearError(mpdz); err = 1; } if (mpdc_status(mpdz) == -1) { mpdc_destroy(); mpdc_init(); if (mpdc_status(mpdz) < 0) { if (!err) { /* already have an error */ pz_error(_("Unable to determine MPD status.")); } mpd_active = 0; return -1; } mpd_active = 1; return 1; } return (mpdz == NULL) ? -1 : err; }
int mpd_database_save_playlist(MpdObj *mi, char *name) { if(name == NULL || !strlen(name)) { debug_printf(DEBUG_WARNING, "mpd_playlist_save: name != NULL and strlen(name) > 0 failed"); return MPD_ARGS_ERROR; } if(!mpd_check_connected(mi)) { debug_printf(DEBUG_WARNING,"mpd_playlist_save: not connected\n"); return MPD_NOT_CONNECTED; } if(mpd_lock_conn(mi)) { debug_printf(DEBUG_ERROR,"mpd_playlist_save: lock failed\n"); return MPD_LOCK_FAILED; } mpd_sendSaveCommand(mi->connection,name); mpd_finishCommand(mi->connection); if(mi->connection->error == MPD_ERROR_ACK && mi->connection->errorCode == MPD_ACK_ERROR_EXIST) { mpd_clearError(mi->connection); mpd_unlock_conn(mi); return MPD_DATABASE_PLAYLIST_EXIST; } /* unlock */ mpd_unlock_conn(mi); return MPD_OK; }
int mpd_playlist_load(MpdObj *mi, const char *path) { int retv = MPD_OK; if(!mpd_check_connected(mi)) { debug_printf(DEBUG_WARNING,"mpd_playlist_load: not connected\n"); return MPD_NOT_CONNECTED; } if(mpd_lock_conn(mi)) { debug_printf(DEBUG_ERROR,"lock failed\n"); return MPD_LOCK_FAILED; } mpd_sendLoadCommand(mi->connection,path); mpd_finishCommand(mi->connection); if(mi->connection->errorCode == MPD_ACK_ERROR_NO_EXIST) { debug_printf(DEBUG_WARNING, "mpd_playlist_load: failed to load playlist\n"); mpd_clearError(mi->connection); retv = MPD_PLAYLIST_LOAD_FAILED; } if(mpd_unlock_conn(mi)) { debug_printf(DEBUG_ERROR, "Failed to unlock connection"); return MPD_LOCK_FAILED; } return retv; }
int mpd_check_error(MpdObj *mi) { if(mi == NULL) { debug_printf(DEBUG_ERROR, "mi == NULL?"); return MPD_ARGS_ERROR; } /* this shouldn't happen, ever */ if(mi->connection == NULL) { debug_printf(DEBUG_ERROR, "mi->connection == NULL?"); return MPD_FATAL_ERROR; } /* TODO: map these errors in the future */ mi->error = mi->connection->error; mi->error_mpd_code = mi->connection->errorCode; /*TODO: do I need to strdup this? */ mi->error_msg = strdup(mi->connection->errorStr); /* Check for permission */ /* First check for an error reported by MPD * Then check what type of error mpd reported */ if(mi->error == MPD_ERROR_ACK) { debug_printf(DEBUG_ERROR,"clearing errors in mpd_Connection: %i-%s", mi->connection->errorCode, mi->connection->errorStr); mpd_clearError(mi->connection); if (mi->the_error_callback) { debug_printf(DEBUG_ERROR, "Error callback 1 (ACK)"); mi->the_error_callback(mi, mi->error_mpd_code, mi->error_msg, mi->the_error_signal_userdata ); } free(mi->error_msg); mi->error_msg = NULL; return TRUE; } if(mi->error) { debug_printf(DEBUG_ERROR, "Following error occured: %i: code: %i msg: %s", mi->error,mi->connection->errorCode, mi->error_msg); if (mi->the_error_callback) { debug_printf(DEBUG_ERROR, "Error callback 2"); mi->the_error_callback(mi, mi->error, mi->error_msg, mi->the_error_signal_userdata ); } mpd_disconnect(mi); free(mi->error_msg); mi->error_msg = NULL; return MPD_SERVER_ERROR; } free(mi->error_msg); mi->error_msg = NULL; return MPD_OK; }
/* note: it's safe to use/reuse command == connection->buffer or even * connection->errorStr (it's only reset on error) */ void mpd_executeCommand(mpd_Connection * connection, char * command) { int ret; struct timeval tv; fd_set fds; char * commandPtr = command; int commandLen = strlen(command); if(!connection->doneProcessing && !connection->commandList) { strcpy(connection->errorStr,"not done processing current command"); connection->error = 1; return; } mpd_clearError(connection); FD_ZERO(&fds); FD_SET(connection->sock,&fds); tv.tv_sec = connection->timeout.tv_sec; tv.tv_usec = connection->timeout.tv_usec; while((ret = select(connection->sock+1,NULL,&fds,NULL,&tv)==1) || (ret==-1 && errno==EINTR)) { ret = send(connection->sock,commandPtr,commandLen, #ifdef WIN32 ioctlsocket(connection->sock, commandLen, commandPtr)); #endif #ifndef WIN32 MSG_DONTWAIT); #endif if(ret<=0) { if(ret==EAGAIN || ret==EINTR) continue; snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, "problems giving command \"%s\"",command); connection->error = MPD_ERROR_SENDING; return; } else { commandPtr+=ret; commandLen-=ret; } if(commandLen<=0) break; } if(commandLen>0) { perror(""); snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, "timeout sending command \"%s\"",command); connection->error = MPD_ERROR_TIMEOUT; return; } if(!connection->commandList) connection->doneProcessing = 0; else if(connection->commandList == COMMAND_LIST_OK) { connection->listOks++; } }
char * mpd_sticker_song_get(MpdObj *mi, const char *path, const char *tag) { char *retv_value = NULL; char *retv = NULL; if(!mpd_check_connected(mi)) { debug_printf(DEBUG_INFO,"not connected\n"); return NULL; } if(mpd_server_check_command_allowed(mi, "sticker") != MPD_SERVER_COMMAND_ALLOWED) { debug_printf(DEBUG_WARNING, "Command not allowed\n"); return NULL; } if(mpd_lock_conn(mi)) { debug_printf(DEBUG_ERROR,"lock failed\n"); return NULL; } mpd_sendGetSongSticker(mi->connection,path, tag); retv_value = mpd_getNextSticker(mi->connection); mpd_finishCommand(mi->connection); if(retv_value && strlen(retv_value) > strlen(tag)){ retv = g_strdup(&retv_value[strlen(tag)]+1); } free(retv_value); if(mi->connection->error == MPD_ERROR_ACK && mi->connection->errorCode == MPD_ACK_ERROR_NO_EXIST) { mpd_clearError(mi->connection); g_free(retv); retv = NULL; } if(mpd_unlock_conn(mi)) { debug_printf(DEBUG_ERROR, "Failed to unlock"); g_free(retv); return NULL; } return retv; }