Code

display songs time in playlist
[ncmpc.git] / src / mpdclient.c
index 289633880d7869e99477c5f0cb2c5f77e315c3cd..4a12515752b9037366d99f1662022ea4269289e3 100644 (file)
@@ -22,6 +22,7 @@
 #include "config.h"
 #include "options.h"
 #include "strfsong.h"
+#include "utils.h"
 
 #include <stdlib.h>
 #include <unistd.h>
 
 #define BUFSIZE 1024
 
-#define MPD_ERROR(c) (c==NULL || c->connection==NULL || c->connection->error)
-
-/* from utils.c */
-extern GList *string_list_free(GList *string_list);
-
+static bool
+MPD_ERROR(const struct mpdclient *client)
+{
+       return client == NULL || client->connection==NULL ||
+               client->connection->error != MPD_ERROR_SUCCESS;
+}
 
 /* filelist sorting functions */
 static gint
@@ -148,6 +150,7 @@ mpdclient_new(void)
 
        c = g_malloc0(sizeof(mpdclient_t));
        playlist_init(&c->playlist);
+       c->volume = MPD_STATUS_NO_VOLUME;
 
        return c;
 }
@@ -218,6 +221,8 @@ mpdclient_update(mpdclient_t *c)
 {
        gint retval = 0;
 
+       c->volume = MPD_STATUS_NO_VOLUME;
+
        if (MPD_ERROR(c))
                return -1;
 
@@ -231,6 +236,12 @@ mpdclient_update(mpdclient_t *c)
        if ((retval=mpdclient_finish_command(c)))
                return retval;
 
+       if (c->updatingdb && c->updatingdb != c->status->updatingDb)
+               mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
+
+       c->updatingdb = c->status->updatingDb;
+       c->volume = c->status->volume;
+
        /* check if the playlist needs an update */
        if (c->playlist.id != c->status->playlist) {
                if (playlist_is_empty(&c->playlist))
@@ -281,32 +292,34 @@ mpdclient_cmd_pause(mpdclient_t *c, gint value)
 gint
 mpdclient_cmd_crop(mpdclient_t *c)
 {
+       gint error;
        mpd_Status *status;
-       int length;
+       bool playing;
+       int length, current;
 
        mpd_sendStatusCommand(c->connection);
        status = mpd_getStatus(c->connection);
-       length = status->playlistLength - 1;
+       error = mpdclient_finish_command(c);
+       if (error)
+               return error;
 
-       if (length <= 0) {
-               mpd_freeStatus(status);
-       } else if (status->state == 3 || status->state == 2) {
-               /* If playing or paused */
+       playing = status->state == MPD_STATUS_STATE_PLAY ||
+               status->state == MPD_STATUS_STATE_PAUSE;
+       length = status->playlistLength;
+       current = status->song;
 
-               mpd_sendCommandListBegin( c->connection );
+       mpd_freeStatus(status);
 
-               while (length >= 0) {
-                       if (length != status->song)
-                               mpd_sendDeleteCommand(c->connection, length);
+       if (!playing || length < 2)
+               return 0;
 
-                       length--;
-               }
+       mpd_sendCommandListBegin( c->connection );
 
-               mpd_sendCommandListEnd(c->connection);
-               mpd_freeStatus(status);
-       } else {
-               mpd_freeStatus(status);
-       }
+       while (--length >= 0)
+               if (length != current)
+                       mpd_sendDeleteCommand(c->connection, length);
+
+       mpd_sendCommandListEnd(c->connection);
 
        return mpdclient_finish_command(c);
 }
@@ -408,8 +421,18 @@ mpdclient_cmd_crossfade(mpdclient_t *c, gint value)
 gint
 mpdclient_cmd_db_update(mpdclient_t *c, gchar *path)
 {
+       gint ret;
+
        mpd_sendUpdateCommand(c->connection, path ? path : "");
-       return mpdclient_finish_command(c);
+       ret = mpdclient_finish_command(c);
+
+       if (ret == 0)
+               /* set updatingDb to make sure the browse callback
+                  gets called even if the update has finished before
+                  status is updated */
+               c->updatingdb = 1;
+
+       return ret;
 }
 
 gint
@@ -419,6 +442,34 @@ mpdclient_cmd_volume(mpdclient_t *c, gint value)
        return mpdclient_finish_command(c);
 }
 
+gint mpdclient_cmd_volume_up(struct mpdclient *c)
+{
+       if (c->status == NULL || c->status->volume == MPD_STATUS_NO_VOLUME)
+               return 0;
+
+       if (c->volume == MPD_STATUS_NO_VOLUME)
+               c->volume = c->status->volume;
+
+       if (c->volume >= 100)
+               return 0;
+
+       return mpdclient_cmd_volume(c, ++c->volume);
+}
+
+gint mpdclient_cmd_volume_down(struct mpdclient *c)
+{
+       if (c->status == NULL || c->status->volume == MPD_STATUS_NO_VOLUME)
+               return 0;
+
+       if (c->volume == MPD_STATUS_NO_VOLUME)
+               c->volume = c->status->volume;
+
+       if (c->volume <= 0)
+               return 0;
+
+       return mpdclient_cmd_volume(c, --c->volume);
+}
+
 gint
 mpdclient_cmd_add_path(mpdclient_t *c, gchar *path_utf8)
 {