Code

screen_search: fix crash when disconnected
[ncmpc.git] / src / mpdclient.c
index 0d8c2baa3bed341708e043edf4a2fc97b89582ab..e55c1362482ece91917b22875f9dc9f671c339d3 100644 (file)
@@ -1,21 +1,21 @@
 /* ncmpc (Ncurses MPD Client)
- * (c) 2004-2009 The Music Player Daemon Project
+ * (c) 2004-2010 The Music Player Daemon Project
  * Project homepage: http://musicpd.org
-
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
-
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
-
+ *
  * You should have received a copy of the GNU General Public License along
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-*/
+ */
 
 #include "mpdclient.h"
 #include "filelist.h"
@@ -83,13 +83,6 @@ mpdclient_handle_error(struct mpdclient *c)
        return false;
 }
 
-static bool
-mpdclient_finish_command(struct mpdclient *c)
-{
-       return mpd_response_finish(c->connection)
-               ? true : mpdclient_handle_error(c);
-}
-
 struct mpdclient *
 mpdclient_new(void)
 {
@@ -122,8 +115,10 @@ mpdclient_disconnect(struct mpdclient *c)
                c->idle = false;
        }
 
-       if (c->connection)
+       if (c->connection) {
                mpd_connection_free(c->connection);
+               ++c->connection_id;
+       }
        c->connection = NULL;
 
        if (c->status)
@@ -136,24 +131,22 @@ mpdclient_disconnect(struct mpdclient *c)
                c->song = NULL;
 
        /* everything has changed after a disconnect */
-       c->events |= MPD_IDLE_DATABASE|MPD_IDLE_STORED_PLAYLIST|
-               MPD_IDLE_QUEUE|MPD_IDLE_PLAYER|MPD_IDLE_MIXER|MPD_IDLE_OUTPUT|
-               MPD_IDLE_OPTIONS|MPD_IDLE_UPDATE;
+       c->events |= MPD_IDLE_ALL;
 }
 
 bool
 mpdclient_connect(struct mpdclient *c,
                  const gchar *host,
                  gint port,
-                 gfloat _timeout,
+                 unsigned timeout_ms,
                  const gchar *password)
 {
        /* close any open connection */
-       if( c->connection )
+       if (c->connection)
                mpdclient_disconnect(c);
 
        /* connect to MPD */
-       c->connection = mpd_connection_new(host, port, _timeout * 1000);
+       c->connection = mpd_connection_new(host, port, timeout_ms);
        if (c->connection == NULL)
                g_error("Out of memory");
 
@@ -170,6 +163,8 @@ mpdclient_connect(struct mpdclient *c,
                return false;
        }
 
+       ++c->connection_id;
+
        return true;
 }
 
@@ -177,7 +172,6 @@ bool
 mpdclient_update(struct mpdclient *c)
 {
        struct mpd_connection *connection = mpdclient_get_connection(c);
-       bool retval;
 
        c->volume = -1;
 
@@ -216,23 +210,26 @@ mpdclient_update(struct mpdclient *c)
 
        /* check if the playlist needs an update */
        if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
+               bool retval;
+
                if (c->source == NULL)
-                       c->events |= MPD_IDLE_PLAYLIST;
+                       c->events |= MPD_IDLE_QUEUE;
 
                if (!playlist_is_empty(&c->playlist))
                        retval = mpdclient_playlist_update_changes(c);
                else
                        retval = mpdclient_playlist_update(c);
-       } else
-               retval = true;
+               if (!retval)
+                       return false;
+       }
 
        /* update the current song */
-       if (!c->song || mpd_status_get_song_id(c->status)) {
+       if (!c->song || mpd_status_get_song_id(c->status) >= 0) {
                c->song = playlist_get_song(&c->playlist,
                                            mpd_status_get_song_pos(c->status));
        }
 
-       return retval;
+       return true;
 }
 
 struct mpd_connection *
@@ -284,15 +281,12 @@ mpdclient_cmd_crop(struct mpdclient *c)
        struct mpd_connection *connection;
        int length, current;
 
-       if (c->status == NULL)
+       if (!mpdclient_is_playing(c))
                return false;
 
        length = mpd_status_get_queue_length(c->status);
        current = mpd_status_get_song_pos(c->status);
-       if (current < 0 ||
-           (mpd_status_get_state(c->status) != MPD_STATE_PLAY &&
-            mpd_status_get_state(c->status) != MPD_STATE_PAUSE) ||
-           mpd_status_get_queue_length(c->status) < 2)
+       if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
                return true;
 
        connection = mpdclient_get_connection(c);
@@ -338,10 +332,6 @@ mpdclient_cmd_clear(struct mpdclient *c)
        if (status == NULL)
                return false;
 
-       if (c->status != NULL)
-               mpd_status_free(c->status);
-       c->status = status;
-
        if (!mpd_response_finish(connection))
                return mpdclient_handle_error(c);
 
@@ -353,6 +343,7 @@ mpdclient_cmd_clear(struct mpdclient *c)
                   reducing the UI latency */
                playlist_clear(&c->playlist);
                c->playlist.version = mpd_status_get_queue_version(status);
+               c->song = NULL;
        }
 
        c->events |= MPD_IDLE_QUEUE;
@@ -416,8 +407,8 @@ mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
        if (connection == NULL)
                return false;
 
-       mpd_send_add(connection, path_utf8);
-       return mpdclient_finish_command(c);
+       return mpd_send_add(connection, path_utf8)?
+               mpdclient_finish_command(c) : false;
 }
 
 bool
@@ -446,7 +437,7 @@ mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
            !mpd_response_next(connection))
                return mpdclient_handle_error(c);
 
-       c->events |= MPD_IDLE_PLAYLIST;
+       c->events |= MPD_IDLE_QUEUE;
 
        status = mpdclient_recv_status(c);
        if (status == NULL)
@@ -505,7 +496,7 @@ mpdclient_cmd_delete(struct mpdclient *c, gint idx)
            !mpd_command_list_end(connection))
                return mpdclient_handle_error(c);
 
-       c->events |= MPD_IDLE_PLAYLIST;
+       c->events |= MPD_IDLE_QUEUE;
 
        status = mpdclient_recv_status(c);
        if (status == NULL)
@@ -587,7 +578,7 @@ mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
            !mpd_command_list_end(connection))
                return mpdclient_handle_error(c);
 
-       c->events |= MPD_IDLE_PLAYLIST;
+       c->events |= MPD_IDLE_QUEUE;
 
        status = mpdclient_recv_status(c);
        if (status == NULL)
@@ -619,33 +610,28 @@ mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
 }
 
 bool
-mpdclient_cmd_move(struct mpdclient *c, gint old_index, gint new_index)
+mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
 {
-       struct mpd_connection *connection = mpdclient_get_connection(c);
-       const struct mpd_song *song1, *song2;
+       struct mpd_connection *connection;
        struct mpd_status *status;
 
-       if (connection == NULL)
-               return false;
+       if (dest_pos == src_pos)
+               return true;
 
-       if (old_index == new_index || new_index < 0 ||
-           (guint)new_index >= c->playlist.list->len)
+       connection = mpdclient_get_connection(c);
+       if (connection == NULL)
                return false;
 
-       song1 = playlist_get(&c->playlist, old_index);
-       song2 = playlist_get(&c->playlist, new_index);
-
-       /* send the delete command to mpd; at the same time, get the
+       /* 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_swap_id(connection, mpd_song_get_id(song1),
-                             mpd_song_get_id(song2)) ||
+           !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_PLAYLIST;
+       c->events |= MPD_IDLE_QUEUE;
 
        status = mpdclient_recv_status(c);
        if (status == NULL)
@@ -662,12 +648,82 @@ mpdclient_cmd_move(struct mpdclient *c, gint old_index, gint new_index)
                c->playlist.version = mpd_status_get_queue_version(status);
 
                /* swap songs in the local playlist */
-               playlist_swap(&c->playlist, old_index, new_index);
+               playlist_move(&c->playlist, dest_pos, src_pos);
        }
 
        return true;
 }
 
+#if LIBMPDCLIENT_CHECK_VERSION(2,5,0)
+/* The client-to-client protocol (MPD 0.17.0) */
+
+bool
+mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
+{
+       struct mpd_connection *connection = mpdclient_get_connection(c);
+
+       if (connection == NULL)
+               return false;
+
+       if (!mpd_send_subscribe(connection, channel))
+               return mpdclient_handle_error(c);
+
+       return mpdclient_finish_command(c);
+}
+
+bool
+mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
+{
+       struct mpd_connection *connection = mpdclient_get_connection(c);
+       if (connection == NULL)
+               return false;
+
+       if (!mpd_send_unsubscribe(connection, channel))
+               return mpdclient_handle_error(c);
+
+       return mpdclient_finish_command(c);
+}
+
+bool
+mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
+                          const char *text)
+{
+       struct mpd_connection *connection = mpdclient_get_connection(c);
+       if (connection == NULL)
+               return false;
+
+       if (!mpd_send_send_message(connection, channel, text))
+               return mpdclient_handle_error(c);
+
+       return mpdclient_finish_command(c);
+}
+
+bool
+mpdclient_send_read_messages(struct mpdclient *c)
+{
+       struct mpd_connection *connection = mpdclient_get_connection(c);
+       if (connection == NULL)
+               return false;
+
+       return mpd_send_read_messages(connection)?
+               true : mpdclient_handle_error(c);
+}
+
+struct mpd_message *
+mpdclient_recv_message(struct mpdclient *c)
+{
+       struct mpd_connection *connection = mpdclient_get_connection(c);
+       if (connection == NULL)
+               return false;
+
+       struct mpd_message *message = mpd_recv_message(connection);
+       if (message == NULL &&
+           mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS)
+               mpdclient_handle_error(c);
+
+       return message;
+}
+#endif
 
 /****************************************************************************/
 /*** Playlist management functions ******************************************/