bool mpd_run_delete(struct mpd_connection *connection, unsigned pos) { return mpd_run_check(connection) && mpd_send_delete(connection, pos) && mpd_response_finish(connection); }
void Connection::Delete(unsigned pos) { prechecks(); mpd_send_delete(m_connection.get(), pos); if (!m_command_list_active) { mpd_response_finish(m_connection.get()); checkErrors(); } }
int cmd_crop(mpd_unused int argc, mpd_unused char **argv, struct mpd_connection *conn) { struct mpd_status *status = getStatus( conn ); int length = mpd_status_get_queue_length(status) - 1; if (length < 0) { mpd_status_free(status); DIE( "A playlist longer than 1 song in length is required to crop.\n" ); } else if (mpd_status_get_state(status) == MPD_STATE_PLAY || mpd_status_get_state(status) == MPD_STATE_PAUSE) { if (!mpd_command_list_begin(conn, false)) printErrorAndExit(conn); while( length >= 0 ) { if (length != mpd_status_get_song_pos(status)) { mpd_send_delete(conn, length); } length--; } mpd_status_free(status); if (!mpd_command_list_end(conn) || !mpd_response_finish(conn)) printErrorAndExit(conn); return ( 0 ); } else { mpd_status_free(status); DIE( "You need to be playing to crop the playlist\n" ); } }
int mpd_crop() { struct mpd_status *status = mpd_run_status(mpd.conn); if (status == 0) return 0; int length = mpd_status_get_queue_length(status) - 1; if (length < 0) { mpd_status_free(status); syslog(LOG_INFO, "%s: A playlist longer than 1 song in length is required to crop.\n", __func__); } else if (mpd_status_get_state(status) == MPD_STATE_PLAY || mpd_status_get_state(status) == MPD_STATE_PAUSE) { if (!mpd_command_list_begin(mpd.conn, false)) { syslog(LOG_ERR, "%s: mpd_command_list_begin failed\n", __func__); return 0; } for (; length >= 0; --length) if (length != mpd_status_get_song_pos(status)) mpd_send_delete(mpd.conn, length); mpd_status_free(status); if (!mpd_command_list_end(mpd.conn) || !mpd_response_finish(mpd.conn)) { syslog(LOG_ERR, "%s: mpd_command_list_end || mpd_response_finish failed\n", __func__); return 0; } return 0; } else { mpd_status_free(status); syslog(LOG_INFO, "%s: You need to be playing to crop the playlist\n", __func__); return 0; } return 1; }
/* Execute the given command. In fact, it just will ask MPD to do it. * You will have to check the next "status" or "playlist" update (using the corresponding slot) * to know if the command has really been executed. */ void Player::executeCmd(EMSPlayerCmd cmd) { bool waitResponse = true; /* Wait the responase by default */ bool error = false; if (conn == NULL) { return; } /* Dispatch execution depending on the action */ switch (cmd.action) { case ACTION_ADD: { if(searchTrackInPlaylist(cmd.track) >= 0) { qDebug() << "Do not add track in the playlist as it already exist"; return; } QString filename = getMPDFilename(cmd.track); mpd_send_add(conn, filename.toStdString().c_str()); break; } case ACTION_DEL: { int pos = searchTrackInPlaylist(cmd.track); if (pos >= 0) { mpd_send_delete(conn, pos); } break; } case ACTION_DEL_ALL: { mpd_send_clear(conn); break; } case ACTION_PLAY_POS: { mutex.lock(); int size = playlist.tracks.size(); mutex.unlock(); if (size <= 0 || cmd.uintValue >= (unsigned int)size) { qDebug() << "Asked to play a track after the end of the current playlist"; error = true; break; } mpd_send_play_pos(conn, cmd.uintValue); break; } case ACTION_PLAY_TRACK: { int position = searchTrackInPlaylist(cmd.track); if (position < 0) { qDebug() << "Asked to play a track which is not in the current playlist"; error = true; break; } mpd_send_play_pos(conn, position); break; } case ACTION_PLAY: { mpd_send_play(conn); break; } case ACTION_SEEK: { /* Get current position */ int songId = getCurrentPos(); if (songId >= 0) { mpd_send_seek_pos(conn, songId, cmd.uintValue); } break; } case ACTION_PAUSE: { mpd_send_pause(conn, true); break; } case ACTION_TOGGLE: { mpd_send_toggle_pause(conn); break; } case ACTION_STOP: { mpd_send_stop(conn); break; } case ACTION_NEXT: { mpd_send_next(conn); break; } case ACTION_PREV: { mpd_send_previous(conn); break; } case ACTION_REPEAT: { mutex.lock(); bool repeatTmp = status.repeat; mutex.unlock(); if (cmd.boolValue != repeatTmp) { mpd_send_repeat(conn, cmd.boolValue); } break; } case ACTION_RANDOM: { mutex.lock(); bool randomTmp = status.random; mutex.unlock(); if (cmd.boolValue != randomTmp) { mpd_send_random(conn, cmd.boolValue); } break; } case ACTION_ENABLE_OUTPUT: { if (cmd.uintValue >= 1) { mpd_send_enable_output(conn, cmd.uintValue-1); } break; } case ACTION_DISABLE_OUTPUT: { if (cmd.uintValue >= 1) { mpd_send_disable_output(conn, cmd.uintValue-1); } break; } default: { qCritical() << "Unhandled action in the current command."; waitResponse = false; break; } } if (waitResponse && !mpd_response_finish(conn)) { error = true; qCritical() << "MPD could not execute the current command."; enum mpd_error errorMpd = mpd_connection_get_error(conn); if (errorMpd == MPD_ERROR_SERVER || errorMpd == MPD_ERROR_ARGUMENT) /* Problem with the command */ { QString errorMessage = QString::fromUtf8(mpd_connection_get_error_message(conn)); qCritical() << "Command error : " << errorMessage; if (!mpd_connection_clear_error(conn)) { qCritical() << "This error cannot be cleared, reconnecting..."; connectToMpd(); } } else if (errorMpd == MPD_ERROR_TIMEOUT || errorMpd == MPD_ERROR_RESOLVER || errorMpd == MPD_ERROR_MALFORMED || errorMpd == MPD_ERROR_CLOSED ) /* Assume there is a connection problem, try to reconnect... */ { QString errorMessage = QString::fromUtf8(mpd_connection_get_error_message(conn)); qCritical() << "Connexion error : " << errorMessage; qCritical() << "Reconnecting..."; connectToMpd(); mutex.lock(); queue.push_front(cmd); mutex.unlock(); cmdAvailable.release(1); } } /* Post-action depending on the command * Do the minimum here as the whole status will be * retrieve here. But for playlist ADD/DEL, we need to * store the EMSTrack structure. */ if(!error) { switch (cmd.action) { case ACTION_ADD: { EMSPlaylist newPlaylist; mutex.lock(); playlist.tracks.append(cmd.track); newPlaylist = playlist; mutex.unlock(); emit playlistChanged(newPlaylist); break; } case ACTION_DEL: { int pos = searchTrackInPlaylist(cmd.track); if (pos >= 0) { EMSPlaylist newPlaylist; mutex.lock(); playlist.tracks.removeAt(pos); newPlaylist = playlist; mutex.unlock(); emit playlistChanged(newPlaylist); } break; } case ACTION_DEL_ALL: { EMSPlaylist newPlaylist; mutex.lock(); playlist.tracks.clear(); newPlaylist = playlist; mutex.unlock(); emit playlistChanged(newPlaylist); break; } case ACTION_ENABLE_OUTPUT: { mutex.lock(); for(int i=0; i<outputs.size(); i++) { if (outputs.at(i).id_mpd == cmd.uintValue) { EMSSndCard card = outputs.at(i); card.enabled = true; outputs.replace(i, card); } } emit outputsChanged(outputs); mutex.unlock(); break; } case ACTION_DISABLE_OUTPUT: { mutex.lock(); for(int i=0; i<outputs.size(); i++) { if (outputs.at(i).id_mpd == cmd.uintValue) { EMSSndCard card = outputs.at(i); card.enabled = false; outputs.replace(i, card); } } emit outputsChanged(outputs); mutex.unlock(); break; } default: break; } } }
int cmd_del ( int argc, char ** argv, struct mpd_connection *conn ) { int i,j; char * s; char * t; char * t2; int range[2]; int songsDeleted = 0; int plLength = 0; char * songsToDel; struct mpd_status *status; status = getStatus(conn); plLength = mpd_status_get_queue_length(status); songsToDel = malloc(plLength); memset(songsToDel,0,plLength); for(i=0;i<argc;i++) { if(argv[i][0]=='#') s = &(argv[i][1]); else s = argv[i]; range[0] = strtol(s,&t,10); /* If argument is 0 current song and we're not stopped */ if(range[0] == 0 && strlen(s) == 1 && \ (mpd_status_get_state(status) == MPD_STATE_PLAY || mpd_status_get_state(status) == MPD_STATE_PAUSE)) range[0] = mpd_status_get_song_pos(status) + 1; if(s==t) DIE("error parsing song numbers from: %s\n",argv[i]); else if(*t=='-') { range[1] = strtol(t+1,&t2,10); if(t+1==t2 || *t2!='\0') DIE("error parsing range from: %s\n",argv[i]); } else if(*t==')' || *t=='\0') range[1] = range[0]; else DIE("error parsing song numbers from: %s\n",argv[i]); if(range[0]<=0 || range[1]<=0) { if (range[0]==range[1]) DIE("song number must be positive: %i\n",range[0]); else DIE("song numbers must be positive: %i to %i\n",range[0],range[1]); } if(range[1]<range[0]) DIE("song range must be from low to high: %i to %i\n",range[0],range[1]); if(range[1]>plLength) DIE("song number does not exist: %i\n",range[1]); for(j=range[0];j<=range[1];j++) songsToDel[j-1] = 1; } if (!mpd_command_list_begin(conn, false)) printErrorAndExit(conn); for(i=0;i<plLength;i++) { if(songsToDel[i]) { mpd_send_delete(conn, i - songsDeleted); songsDeleted++; } } mpd_status_free(status); free(songsToDel); if (!mpd_command_list_end(conn) || !mpd_response_finish(conn)) printErrorAndExit(conn); return 0; }