Code

mpdclient: added function mpdclient_cmd_move()
authorMax Kellermann <max@duempel.org>
Sun, 18 Oct 2009 00:20:17 +0000 (02:20 +0200)
committerMax Kellermann <max@duempel.org>
Sun, 18 Oct 2009 00:20:17 +0000 (02:20 +0200)
src/mpdclient.c
src/mpdclient.h

index ab2a79b56492d28cf554c5b5bf3697c4bdad8edf..f62793613cdc504e81651f253e59a697dd3f0e6c 100644 (file)
@@ -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 ******************************************/
index e73bd8a8f498e9739e3e45c19b98f76bb5996950..940b3eb1d256227fc6d6b8991b9905969ecfb7c8 100644 (file)
@@ -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 */