From 58d0563df78e78eed419e2245684cb4a50abfa52 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 18 Oct 2009 02:20:17 +0200 Subject: [PATCH] mpdclient: added function mpdclient_cmd_move() --- src/mpdclient.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/mpdclient.h | 3 +++ 2 files changed, 48 insertions(+) diff --git a/src/mpdclient.c b/src/mpdclient.c index ab2a79b..f627936 100644 --- a/src/mpdclient.c +++ b/src/mpdclient.c @@ -668,6 +668,51 @@ mpdclient_cmd_swap(struct mpdclient *c, gint old_index, gint new_index) return true; } +bool +mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos) +{ + struct mpd_connection *connection; + struct mpd_status *status; + + if (dest_pos == src_pos) + return true; + + connection = mpdclient_get_connection(c); + if (connection == NULL) + return false; + + /* send the "move" command to MPD; at the same time, get the + new status (to verify the playlist id) */ + + if (!mpd_command_list_begin(connection, false) || + !mpd_send_move(connection, src_pos, dest_pos) || + !mpd_send_status(connection) || + !mpd_command_list_end(connection)) + return mpdclient_handle_error(c); + + c->events |= MPD_IDLE_QUEUE; + + status = mpdclient_recv_status(c); + if (status == NULL) + return false; + + if (!mpd_response_finish(connection)) + return mpdclient_handle_error(c); + + if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) && + mpd_status_get_queue_version(status) == c->playlist.version + 1) { + /* the cheap route: match on the new playlist length + and its version, we can keep our local playlist + copy in sync */ + c->playlist.version = mpd_status_get_queue_version(status); + + /* swap songs in the local playlist */ + playlist_move(&c->playlist, dest_pos, src_pos); + } + + return true; +} + /****************************************************************************/ /*** Playlist management functions ******************************************/ diff --git a/src/mpdclient.h b/src/mpdclient.h index e73bd8a..940b3eb 100644 --- a/src/mpdclient.h +++ b/src/mpdclient.h @@ -122,6 +122,9 @@ mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end); bool mpdclient_cmd_swap(struct mpdclient *c, gint old_index, gint new_index); +bool +mpdclient_cmd_move(struct mpdclient *c, unsigned dest, unsigned src); + /*** playlist functions **************************************************/ /* update the complete playlist */ -- 2.30.2