diff --git a/src/mpdclient.c b/src/mpdclient.c
index 03c69f30ec2fc2c3e0c70f760d41e137bb3ed571..ffa1a5d84408e86757b82d2dbde1617dbf07c583 100644 (file)
--- a/src/mpdclient.c
+++ b/src/mpdclient.c
-/*
- * $Id$
- *
- * (c) 2004 by Kalle Wallin <kaw@linux.se>
+/* ncmpc (Ncurses MPD Client)
+ * (c) 2004-2017 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
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
+ * 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 <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <time.h>
-#include <string.h>
-#include <glib.h>
-
-#include "config.h"
-#include "ncmpc.h"
-#include "support.h"
#include "mpdclient.h"
-#include "options.h"
+#include "callbacks.h"
+#include "filelist.h"
+#include "config.h"
+#include "gidle.h"
+#include "charset.h"
-#undef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD /* broken with song id's */
-#define ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
-#define ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
-#define ENABLE_SONG_ID
-#define ENABLE_PLCHANGES
+#ifdef ENABLE_ASYNC_CONNECT
+#include "aconnect.h"
+#endif
-#define MPD_ERROR(c) (c==NULL || c->connection==NULL || c->connection->error)
+#include <mpd/client.h>
-/* from utils.c */
-extern GList *string_list_free(GList *string_list);
+#include <assert.h>
-/* Error callbacks */
-static gint
-error_cb(mpdclient_t *c, gint error, gchar *msg)
+static gboolean
+mpdclient_enter_idle_callback(gpointer user_data)
{
- GList *list = c->error_callbacks;
-
- if( list==NULL )
- fprintf(stderr, "error [%d]: %s\n", (error & 0xFF), msg);
+ struct mpdclient *c = user_data;
+ assert(c->enter_idle_source_id != 0);
+ assert(c->source != NULL);
+ assert(!c->idle);
- while(list)
- {
- mpdc_error_cb_t cb = list->data;
- if( cb )
- cb(c, error, msg);
- list=list->next;
- }
- mpd_clearError(c->connection);
- return error;
+ c->enter_idle_source_id = 0;
+ c->idle = mpd_glib_enter(c->source);
+ return false;
}
-#ifdef DEBUG
-#include "strfsong.h"
-
-static gchar *
-get_song_name(mpd_Song *song)
+static void
+mpdclient_schedule_enter_idle(struct mpdclient *c)
{
- static gchar name[256];
+ assert(c != NULL);
+ assert(c->source != NULL);
- strfsong(name, 256, "[%artist% - ]%title%|%file%", song);
- return name;
+ if (c->enter_idle_source_id == 0)
+ /* automatically re-enter MPD "idle" mode */
+ c->enter_idle_source_id =
+ g_idle_add(mpdclient_enter_idle_callback, c);
}
-#endif
-
-/****************************************************************************/
-/*** mpdclient functions ****************************************************/
-/****************************************************************************/
-
-gint
-mpdclient_finish_command(mpdclient_t *c)
+static void
+mpdclient_cancel_enter_idle(struct mpdclient *c)
{
- mpd_finishCommand(c->connection);
-
- if( c->connection->error )
- {
- gchar *msg = locale_to_utf8(c->connection->errorStr);
- gint error = c->connection->error;
-
- if( error == MPD_ERROR_ACK )
- error = error | (c->connection->errorCode << 8);
-
- error_cb(c, error, msg);
- g_free(msg);
- return error;
- }
-
- return 0;
+ if (c->enter_idle_source_id != 0) {
+ g_source_remove(c->enter_idle_source_id);
+ c->enter_idle_source_id = 0;
+ }
}
-mpdclient_t *
-mpdclient_new(void)
+static void
+mpdclient_invoke_error_callback(enum mpd_error error,
+ const char *message)
{
- mpdclient_t *c;
+ char *allocated;
+ if (error == MPD_ERROR_SERVER)
+ /* server errors are UTF-8, the others are locale */
+ message = allocated = utf8_to_locale(message);
+ else
+ allocated = NULL;
- c = g_malloc0(sizeof(mpdclient_t));
-
- return c;
+ mpdclient_error_callback(message);
+ g_free(allocated);
}
-mpdclient_t *
-mpdclient_free(mpdclient_t *c)
+static void
+mpdclient_invoke_error_callback1(struct mpdclient *c)
{
- mpdclient_disconnect(c);
- g_list_free(c->error_callbacks);
- g_list_free(c->playlist_callbacks);
- g_list_free(c->browse_callbacks);
- g_free(c);
+ assert(c != NULL);
+ assert(c->connection != NULL);
+
+ struct mpd_connection *connection = c->connection;
- return NULL;
+ enum mpd_error error = mpd_connection_get_error(connection);
+ assert(error != MPD_ERROR_SUCCESS);
+
+ mpdclient_invoke_error_callback(error,
+ mpd_connection_get_error_message(connection));
}
-gint
-mpdclient_disconnect(mpdclient_t *c)
+static void
+mpdclient_gidle_callback(enum mpd_error error,
+ gcc_unused enum mpd_server_error server_error,
+ const char *message, enum mpd_idle events,
+ void *ctx)
{
- if( c->connection )
- mpd_closeConnection(c->connection);
- c->connection = NULL;
+ struct mpdclient *c = ctx;
- if( c->status )
- mpd_freeStatus(c->status);
- c->status = NULL;
+ c->idle = false;
- if( c->playlist.list )
- mpdclient_playlist_free(&c->playlist);
+ assert(mpdclient_is_connected(c));
- if( c->song )
- c->song = NULL;
-
- return 0;
-}
+ if (error != MPD_ERROR_SUCCESS) {
+ mpdclient_invoke_error_callback(error, message);
+ mpdclient_disconnect(c);
+ mpdclient_lost_callback();
+ return;
+ }
-gint
-mpdclient_connect(mpdclient_t *c,
- gchar *host,
- gint port,
- gfloat timeout,
- gchar *password)
-{
- gint retval = 0;
-
- /* close any open connection */
- if( c->connection )
- mpdclient_disconnect(c);
+ c->events |= events;
+ mpdclient_update(c);
- /* connect to MPD */
- c->connection = mpd_newConnection(host, port, timeout);
- if( c->connection->error )
- return error_cb(c, c->connection->error, c->connection->errorStr);
+ mpdclient_idle_callback(c->events);
- /* send password */
- if( password )
- {
- mpd_sendPasswordCommand(c->connection, password);
- retval = mpdclient_finish_command(c);
- }
- c->need_update = TRUE;
+ c->events = 0;
- return retval;
+ if (c->source != NULL)
+ mpdclient_schedule_enter_idle(c);
}
-gint
-mpdclient_update(mpdclient_t *c)
+/****************************************************************************/
+/*** mpdclient functions ****************************************************/
+/****************************************************************************/
+
+bool
+mpdclient_handle_error(struct mpdclient *c)
{
- gint retval = 0;
+ enum mpd_error error = mpd_connection_get_error(c->connection);
- if( MPD_ERROR(c) )
- return -1;
+ assert(error != MPD_ERROR_SUCCESS);
- /* free the old status */
- if( c->status )
- mpd_freeStatus(c->status);
-
- /* retreive new status */
- mpd_sendStatusCommand(c->connection);
- c->status = mpd_getStatus(c->connection);
- if( (retval=mpdclient_finish_command(c)) )
- return retval;
-#ifdef DEBUG
- if( c->status->error )
- D("status> %s\n", c->status->error);
-#endif
+ if (error == MPD_ERROR_SERVER &&
+ mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_PERMISSION &&
+ mpdclient_auth_callback(c))
+ return true;
+
+ mpdclient_invoke_error_callback(error,
+ mpd_connection_get_error_message(c->connection));
- /* check if the playlist needs an update */
- if( c->playlist.id != c->status->playlist )
- {
- if( c->playlist.list )
- retval = mpdclient_playlist_update_changes(c);
- else
- retval = mpdclient_playlist_update(c);
- }
+ if (!mpd_connection_clear_error(c->connection)) {
+ mpdclient_disconnect(c);
+ mpdclient_lost_callback();
+ }
- /* update the current song */
- if( !c->song || c->status->songid != c->song->id )
- {
- c->song = playlist_get_song(c, c->status->song);
- }
+ return false;
+}
- c->need_update = FALSE;
+#ifdef ENABLE_ASYNC_CONNECT
+#ifndef WIN32
- return retval;
+static bool
+is_local_socket(const char *host)
+{
+ return *host == '/' || *host == '@';
}
+static bool
+settings_is_local_socket(const struct mpd_settings *settings)
+{
+ const char *host = mpd_settings_get_host(settings);
+ return host != NULL && is_local_socket(host);
+}
-/****************************************************************************/
-/*** MPD Commands **********************************************************/
-/****************************************************************************/
+#endif
+#endif
-gint
-mpdclient_cmd_play(mpdclient_t *c, gint index)
+struct mpdclient *
+mpdclient_new(const gchar *host, unsigned port,
+ unsigned timeout_ms, const gchar *password)
{
-#ifdef ENABLE_SONG_ID
- mpd_Song *song = playlist_get_song(c, index);
+ struct mpdclient *c = g_new0(struct mpdclient, 1);
+
+#ifdef ENABLE_ASYNC_CONNECT
+ c->settings = mpd_settings_new(host, port, timeout_ms,
+ NULL, NULL);
+ if (c->settings == NULL)
+ g_error("Out of memory");
+
+#ifndef WIN32
+ c->settings2 = host == NULL && port == 0 &&
+ settings_is_local_socket(c->settings)
+ ? mpd_settings_new(host, 6600, timeout_ms, NULL, NULL)
+ : NULL;
+#endif
- D("Play id:%d\n", song ? song->id : -1);
- if( song )
- mpd_sendPlayIdCommand(c->connection, song->id);
- else
- mpd_sendPlayIdCommand(c->connection, MPD_PLAY_AT_BEGINNING);
#else
- mpd_sendPlayCommand(c->connection, index);
+ c->host = host;
+ c->port = port;
#endif
- c->need_update = TRUE;
- return mpdclient_finish_command(c);
-}
-gint
-mpdclient_cmd_pause(mpdclient_t *c, gint value)
-{
- mpd_sendPauseCommand(c->connection, value);
- return mpdclient_finish_command(c);
-}
+ c->timeout_ms = timeout_ms;
+ c->password = password;
-gint
-mpdclient_cmd_stop(mpdclient_t *c)
-{
- mpd_sendStopCommand(c->connection);
- return mpdclient_finish_command(c);
-}
+ playlist_init(&c->playlist);
+ c->volume = -1;
+ c->events = 0;
+ c->playing = false;
-gint
-mpdclient_cmd_next(mpdclient_t *c)
-{
- mpd_sendNextCommand(c->connection);
- c->need_update = TRUE;
- return mpdclient_finish_command(c);
+ return c;
}
-gint
-mpdclient_cmd_prev(mpdclient_t *c)
+void
+mpdclient_free(struct mpdclient *c)
{
- mpd_sendPrevCommand(c->connection);
- c->need_update = TRUE;
- return mpdclient_finish_command(c);
-}
+ mpdclient_disconnect(c);
-gint
-mpdclient_cmd_seek(mpdclient_t *c, gint id, gint pos)
-{
- D("Seek id:%d\n", id);
- mpd_sendSeekIdCommand(c->connection, id, pos);
- return mpdclient_finish_command(c);
-}
+ mpdclient_playlist_free(&c->playlist);
-gint
-mpdclient_cmd_shuffle(mpdclient_t *c)
-{
- mpd_sendShuffleCommand(c->connection);
- c->need_update = TRUE;
- return mpdclient_finish_command(c);
-}
+#ifdef ENABLE_ASYNC_CONNECT
+ mpd_settings_free(c->settings);
-gint
-mpdclient_cmd_clear(mpdclient_t *c)
-{
- gint retval = 0;
+#ifndef WIN32
+ if (c->settings2 != NULL)
+ mpd_settings_free(c->settings2);
+#endif
+#endif
- mpd_sendClearCommand(c->connection);
- retval = mpdclient_finish_command(c);
- /* call playlist updated callback */
- mpdclient_playlist_callback(c, PLAYLIST_EVENT_CLEAR, NULL);
- c->need_update = TRUE;
- return retval;
+ g_free(c);
}
-gint
-mpdclient_cmd_repeat(mpdclient_t *c, gint value)
+static char *
+settings_name(const struct mpd_settings *settings)
{
- mpd_sendRepeatCommand(c->connection, value);
- return mpdclient_finish_command(c);
-}
+ assert(settings != NULL);
-gint
-mpdclient_cmd_random(mpdclient_t *c, gint value)
-{
- mpd_sendRandomCommand(c->connection, value);
- return mpdclient_finish_command(c);
-}
+ const char *host = mpd_settings_get_host(settings);
+ if (host == NULL)
+ host = "unknown";
-gint
-mpdclient_cmd_crossfade(mpdclient_t *c, gint value)
-{
- mpd_sendCrossfadeCommand(c->connection, value);
- return mpdclient_finish_command(c);
-}
+ if (host[0] == '/')
+ return g_strdup(host);
-gint
-mpdclient_cmd_db_update_utf8(mpdclient_t *c, gchar *path)
-{
- mpd_sendUpdateCommand(c->connection, path ? path : "");
- return mpdclient_finish_command(c);
-}
+ unsigned port = mpd_settings_get_port(settings);
+ if (port == 0 || port == 6600)
+ return g_strdup(host);
-gint
-mpdclient_cmd_volume(mpdclient_t *c, gint value)
-{
- mpd_sendSetvolCommand(c->connection, value);
- return mpdclient_finish_command(c);
+ return g_strdup_printf("%s:%u", host, port);
}
-gint
-mpdclient_cmd_add_path_utf8(mpdclient_t *c, gchar *path_utf8)
+char *
+mpdclient_settings_name(const struct mpdclient *c)
{
- mpd_sendAddCommand(c->connection, path_utf8);
- return mpdclient_finish_command(c);
+ assert(c != NULL);
+
+#ifdef ENABLE_ASYNC_CONNECT
+ return settings_name(c->settings);
+#else
+ struct mpd_settings *settings =
+ mpd_settings_new(c->host, c->port, 0, NULL, NULL);
+ if (settings == NULL)
+ return g_strdup("unknown");
+
+ char *name = settings_name(settings);
+ mpd_settings_free(settings);
+ return name;
+#endif
}
-gint
-mpdclient_cmd_add_path(mpdclient_t *c, gchar *path)
+static void
+mpdclient_status_free(struct mpdclient *c)
{
- gint retval;
- gchar *path_utf8 = locale_to_utf8(path);
+ if (c->status == NULL)
+ return;
- retval=mpdclient_cmd_add_path_utf8(c, path_utf8);
- g_free(path_utf8);
- return retval;
+ mpd_status_free(c->status);
+ c->status = NULL;
+
+ c->volume = -1;
+ c->playing = false;
}
-gint
-mpdclient_cmd_add(mpdclient_t *c, mpd_Song *song)
-{
- gint retval = 0;
+void
+mpdclient_disconnect(struct mpdclient *c)
+{
+#ifdef ENABLE_ASYNC_CONNECT
+ if (c->async_connect != NULL) {
+ aconnect_cancel(c->async_connect);
+ c->async_connect = NULL;
+ }
+#endif
+
+ mpdclient_cancel_enter_idle(c);
- if( !song || !song->file )
- return -1;
+ if (c->source != NULL) {
+ mpd_glib_free(c->source);
+ c->source = NULL;
+ c->idle = false;
+ }
- /* send the add command to mpd */
- mpd_sendAddCommand(c->connection, song->file);
- if( (retval=mpdclient_finish_command(c)) )
- return retval;
+ if (c->connection) {
+ mpd_connection_free(c->connection);
+ ++c->connection_id;
+ }
+ c->connection = NULL;
-#ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD
- /* add the song to playlist */
- c->playlist.list = g_list_append(c->playlist.list, mpd_songDup(song));
- c->playlist.length++;
+ mpdclient_status_free(c);
- /* increment the playlist id, so we dont retrives a new playlist */
- c->playlist.id++;
+ playlist_clear(&c->playlist);
- /* call playlist updated callback */
- mpdclient_playlist_callback(c, PLAYLIST_EVENT_ADD, (gpointer) song);
-#else
- c->need_update = TRUE;
-#endif
+ if (c->song)
+ c->song = NULL;
- return 0;
+ /* everything has changed after a disconnect */
+ c->events |= MPD_IDLE_ALL;
}
-gint
-mpdclient_cmd_delete(mpdclient_t *c, gint index)
+static bool
+mpdclient_connected(struct mpdclient *c,
+ struct mpd_connection *connection)
{
- gint retval = 0;
- mpd_Song *song = playlist_get_song(c, index);
+ c->connection = connection;
- if( !song )
- return -1;
+ if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
+ mpdclient_invoke_error_callback1(c);
+ mpdclient_disconnect(c);
+ mpdclient_failed_callback();
+ return false;
+ }
- /* send the delete command to mpd */
-#ifdef ENABLE_SONG_ID
- D("Delete id:%d\n", song->id);
- mpd_sendDeleteIdCommand(c->connection, song->id);
-#else
- mpd_sendDeleteCommand(c->connection, index);
+#ifdef ENABLE_ASYNC_CONNECT
+ if (c->timeout_ms > 0)
+ mpd_connection_set_timeout(connection, c->timeout_ms);
#endif
- if( (retval=mpdclient_finish_command(c)) )
- return retval;
-
-#ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
- /* increment the playlist id, so we dont retrive a new playlist */
- c->playlist.id++;
- /* remove the song from the playlist */
- c->playlist.list = g_list_remove(c->playlist.list, (gpointer) song);
- c->playlist.length = g_list_length(c->playlist.list);
+ /* send password */
+ if (c->password != NULL &&
+ !mpd_run_password(connection, c->password)) {
+ mpdclient_invoke_error_callback1(c);
+ mpdclient_disconnect(c);
+ mpdclient_failed_callback();
+ return false;
+ }
- /* call playlist updated callback */
- mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
+ c->source = mpd_glib_new(connection,
+ mpdclient_gidle_callback, c);
+ mpdclient_schedule_enter_idle(c);
- /* remove references to the song */
- if( c->song == song )
- {
- c->song = NULL;
- c->need_update = TRUE;
- }
+ ++c->connection_id;
- /* free song */
- mpd_freeSong(song);
+ mpdclient_connected_callback();
+ return true;
+}
-#else
- c->need_update = TRUE;
-#endif
+#ifdef ENABLE_ASYNC_CONNECT
- return 0;
-}
+static void
+mpdclient_aconnect_start(struct mpdclient *c,
+ const struct mpd_settings *settings);
-gint
-mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index)
+static void
+mpdclient_connect_success(struct mpd_connection *connection, void *ctx)
{
- gint n, index1, index2;
- GList *item1, *item2;
- gpointer data1, data2;
- mpd_Song *song1, *song2;
+ struct mpdclient *c = ctx;
+ assert(c->async_connect != NULL);
+ c->async_connect = NULL;
- if( old_index==new_index || new_index<0 || new_index>=c->playlist.length )
- return -1;
+ mpdclient_connected(c, connection);
+}
- song1 = playlist_get_song(c, old_index);
- song2 = playlist_get_song(c, new_index);
+static void
+mpdclient_connect_error(const char *message, void *ctx)
+{
+ struct mpdclient *c = ctx;
+ assert(c->async_connect != NULL);
+ c->async_connect = NULL;
- /* send the move command to mpd */
-#ifdef ENABLE_SONG_ID
- D("Swaping id:%d with id:%d\n", song1->id, song2->id);
- mpd_sendSwapIdCommand(c->connection, song1->id, song2->id);
-#else
- D("Moving index %d to id:%d\n", old_index, new_index);
- mpd_sendMoveCommand(c->connection, old_index, new_index);
+#ifndef WIN32
+ if (!c->connecting2 && c->settings2 != NULL) {
+ c->connecting2 = true;
+ mpdclient_aconnect_start(c, c->settings2);
+ return;
+ }
#endif
- if( (n=mpdclient_finish_command(c)) )
- return n;
-
-#ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
- /* update the songs position field */
- n = song1->pos;
- song1->pos = song2->pos;
- song2->pos = n;
- index1 = MIN(old_index, new_index);
- index2 = MAX(old_index, new_index);
- /* retreive the list items */
- item1 = g_list_nth(c->playlist.list, index1);
- item2 = g_list_nth(c->playlist.list, index2);
- /* retrieve the songs */
- data1 = item1->data;
- data2 = item2->data;
-
- /* move the second item */
- c->playlist.list = g_list_remove(c->playlist.list, data2);
- c->playlist.list = g_list_insert_before(c->playlist.list, item1, data2);
-
- /* move the first item */
- if( index2-index1 >1 )
- {
- item2 = g_list_nth(c->playlist.list, index2);
- c->playlist.list = g_list_remove(c->playlist.list, data1);
- c->playlist.list = g_list_insert_before(c->playlist.list, item2, data1);
- }
-
- /* increment the playlist id, so we dont retrives a new playlist */
- c->playlist.id++;
-#else
- c->need_update = TRUE;
-#endif
+ mpdclient_error_callback(message);
+ mpdclient_failed_callback();
+}
- /* call playlist updated callback */
- D("move> new_index=%d, old_index=%d\n", new_index, old_index);
- mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index);
+static const struct aconnect_handler mpdclient_connect_handler = {
+ .success = mpdclient_connect_success,
+ .error = mpdclient_connect_error,
+};
- return 0;
+static void
+mpdclient_aconnect_start(struct mpdclient *c,
+ const struct mpd_settings *settings)
+{
+ aconnect_start(&c->async_connect,
+ mpd_settings_get_host(settings),
+ mpd_settings_get_port(settings),
+ &mpdclient_connect_handler, c);
}
-gint
-mpdclient_cmd_save_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
+#endif
+
+void
+mpdclient_connect(struct mpdclient *c)
{
- gint retval = 0;
+ /* close any open connection */
+ mpdclient_disconnect(c);
- mpd_sendSaveCommand(c->connection, filename_utf8);
- if( (retval=mpdclient_finish_command(c)) == 0 )
- mpdclient_browse_callback(c, BROWSE_PLAYLIST_SAVED, NULL);
- return retval;
-}
+#ifdef ENABLE_ASYNC_CONNECT
+#ifndef WIN32
+ c->connecting2 = false;
+#endif
+ mpdclient_aconnect_start(c, c->settings);
+#else
+ /* connect to MPD */
+ struct mpd_connection *connection =
+ mpd_connection_new(c->host, c->port, c->timeout_ms);
+ if (connection == NULL)
+ g_error("Out of memory");
-gint
-mpdclient_cmd_save_playlist(mpdclient_t *c, gchar *filename)
-{
- gint retval = 0;
- gchar *filename_utf8 = locale_to_utf8(filename);
-
- retval = mpdclient_cmd_save_playlist_utf8(c, filename);
- g_free(filename_utf8);
- return retval;
+ mpdclient_connected(c, connection);
+#endif
}
-gint
-mpdclient_cmd_load_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
+bool
+mpdclient_update(struct mpdclient *c)
{
- mpd_sendLoadCommand(c->connection, filename_utf8);
- c->need_update = TRUE;
- return mpdclient_finish_command(c);
+ struct mpd_connection *connection = mpdclient_get_connection(c);
+
+ if (connection == NULL)
+ return false;
+
+ /* free the old status */
+ mpdclient_status_free(c);
+
+ /* retrieve new status */
+ c->status = mpd_run_status(connection);
+ if (c->status == NULL)
+ return mpdclient_handle_error(c);
+
+ c->volume = mpd_status_get_volume(c->status);
+ c->playing = mpd_status_get_state(c->status) == MPD_STATE_PLAY;
+
+ /* check if the playlist needs an update */
+ if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
+ bool retval;
+
+ if (!playlist_is_empty(&c->playlist))
+ retval = mpdclient_playlist_update_changes(c);
+ else
+ retval = mpdclient_playlist_update(c);
+ if (!retval)
+ return false;
+ }
+
+ /* update the current song */
+ 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 true;
}
-gint
-mpdclient_cmd_delete_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
+struct mpd_connection *
+mpdclient_get_connection(struct mpdclient *c)
{
- gint retval = 0;
+ if (c->source != NULL && c->idle) {
+ c->idle = false;
+ mpd_glib_leave(c->source);
+
+ mpdclient_schedule_enter_idle(c);
+ }
- mpd_sendRmCommand(c->connection, filename_utf8);
- if( (retval=mpdclient_finish_command(c)) == 0 )
- mpdclient_browse_callback(c, BROWSE_PLAYLIST_DELETED, NULL);
- return retval;
+ return c->connection;
}
-gint
-mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename)
+static struct mpd_status *
+mpdclient_recv_status(struct mpdclient *c)
{
- gint retval = 0;
- gchar *filename_utf8 = locale_to_utf8(filename);
+ assert(c->connection != NULL);
- retval = mpdclient_cmd_delete_playlist_utf8(c, filename_utf8);
- g_free(filename_utf8);
- return retval;
-}
+ struct mpd_status *status = mpd_recv_status(c->connection);
+ if (status == NULL) {
+ mpdclient_handle_error(c);
+ return NULL;
+ }
+ if (c->status != NULL)
+ mpd_status_free(c->status);
+ return c->status = status;
+}
/****************************************************************************/
-/*** Callback managment functions *******************************************/
+/*** MPD Commands **********************************************************/
/****************************************************************************/
-static void
-do_list_callbacks(mpdclient_t *c, GList *list, gint event, gpointer data)
+
+bool
+mpdclient_cmd_crop(struct mpdclient *c)
{
- while(list)
- {
- mpdc_list_cb_t fn = list->data;
+ if (!mpdclient_is_playing(c))
+ return false;
- fn(c, event, data);
- list=list->next;
- }
-}
+ int length = mpd_status_get_queue_length(c->status);
+ int current = mpd_status_get_song_pos(c->status);
+ if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
+ return true;
-void
-mpdclient_playlist_callback(mpdclient_t *c, int event, gpointer data)
-{
- do_list_callbacks(c, c->playlist_callbacks, event, data);
+ struct mpd_connection *connection = mpdclient_get_connection(c);
+ if (connection == NULL)
+ return false;
+
+ mpd_command_list_begin(connection, false);
+
+ if (current < length - 1)
+ mpd_send_delete_range(connection, current + 1, length);
+ if (current > 0)
+ mpd_send_delete_range(connection, 0, current);
+
+ mpd_command_list_end(connection);
+
+ return mpdclient_finish_command(c);
}
-void
-mpdclient_install_playlist_callback(mpdclient_t *c,mpdc_list_cb_t cb)
+bool
+mpdclient_cmd_clear(struct mpdclient *c)
{
- c->playlist_callbacks = g_list_append(c->playlist_callbacks, cb);
+ struct mpd_connection *connection = mpdclient_get_connection(c);
+ if (connection == NULL)
+ return false;
+
+ /* send "clear" and "status" */
+ if (!mpd_command_list_begin(connection, false) ||
+ !mpd_send_clear(connection) ||
+ !mpd_send_status(connection) ||
+ !mpd_command_list_end(connection))
+ return mpdclient_handle_error(c);
+
+ /* receive the new status, store it in the mpdclient struct */
+
+ struct mpd_status *status = mpdclient_recv_status(c);
+ if (status == NULL)
+ return false;
+
+ if (!mpd_response_finish(connection))
+ return mpdclient_handle_error(c);
+
+ /* update mpdclient.playlist */
+
+ if (mpd_status_get_queue_length(status) == 0) {
+ /* after the "clear" command, the queue is really
+ empty - this means we can clear it locally,
+ 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;
+ return true;
}
-void
-mpdclient_remove_playlist_callback(mpdclient_t *c, mpdc_list_cb_t cb)
+bool
+mpdclient_cmd_volume(struct mpdclient *c, gint value)
{
- c->playlist_callbacks = g_list_remove(c->playlist_callbacks, cb);
+ struct mpd_connection *connection = mpdclient_get_connection(c);
+ if (connection == NULL)
+ return false;
+
+ mpd_send_set_volume(connection, value);
+ return mpdclient_finish_command(c);
}
-void
-mpdclient_browse_callback(mpdclient_t *c, int event, gpointer data)
+bool
+mpdclient_cmd_volume_up(struct mpdclient *c)
{
- do_list_callbacks(c, c->browse_callbacks, event, data);
-}
+ if (c->volume < 0 || c->volume >= 100)
+ return true;
+ struct mpd_connection *connection = mpdclient_get_connection(c);
+ if (connection == NULL)
+ return false;
-void
-mpdclient_install_browse_callback(mpdclient_t *c,mpdc_list_cb_t cb)
-{
- c->browse_callbacks = g_list_append(c->browse_callbacks, cb);
+ return mpdclient_cmd_volume(c, ++c->volume);
}
-void
-mpdclient_remove_browse_callback(mpdclient_t *c, mpdc_list_cb_t cb)
+bool
+mpdclient_cmd_volume_down(struct mpdclient *c)
{
- c->browse_callbacks = g_list_remove(c->browse_callbacks, cb);
+ if (c->volume <= 0)
+ return true;
+
+ struct mpd_connection *connection = mpdclient_get_connection(c);
+ if (connection == NULL)
+ return false;
+
+ return mpdclient_cmd_volume(c, --c->volume);
}
-void
-mpdclient_install_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
+bool
+mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
{
- c->error_callbacks = g_list_append(c->error_callbacks, cb);
+ struct mpd_connection *connection = mpdclient_get_connection(c);
+ if (connection == NULL)
+ return false;
+
+ return mpd_send_add(connection, path_utf8)?
+ mpdclient_finish_command(c) : false;
}
-void
-mpdclient_remove_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
+bool
+mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
{
- c->error_callbacks = g_list_remove(c->error_callbacks, cb);
-}
+ assert(c != NULL);
+ assert(song != NULL);
-/****************************************************************************/
-/*** Playlist managment functions *******************************************/
-/****************************************************************************/
+ struct mpd_connection *connection = mpdclient_get_connection(c);
+ if (connection == NULL || c->status == NULL)
+ return false;
-gint
-mpdclient_playlist_free(mpdclient_playlist_t *playlist)
-{
- GList *list = g_list_first(playlist->list);
+ /* send the add command to mpd; at the same time, get the new
+ status (to verify the new playlist id) and the last song
+ (we hope that's the song we just added) */
- while(list)
- {
- mpd_Song *song = (mpd_Song *) list->data;
- mpd_freeSong(song);
- list=list->next;
- }
- g_list_free(playlist->list);
- memset(playlist, 0, sizeof(mpdclient_playlist_t));
- return 0;
-}
+ if (!mpd_command_list_begin(connection, true) ||
+ !mpd_send_add(connection, mpd_song_get_uri(song)) ||
+ !mpd_send_status(connection) ||
+ !mpd_send_get_queue_song_pos(connection,
+ playlist_length(&c->playlist)) ||
+ !mpd_command_list_end(connection) ||
+ !mpd_response_next(connection))
+ return mpdclient_handle_error(c);
-/* update playlist */
-gint
-mpdclient_playlist_update(mpdclient_t *c)
-{
- mpd_InfoEntity *entity;
+ c->events |= MPD_IDLE_QUEUE;
- D("mpdclient_playlist_update() [%lld]\n", c->status->playlist);
+ struct mpd_status *status = mpdclient_recv_status(c);
+ if (status == NULL)
+ return false;
- if( MPD_ERROR(c) )
- return -1;
+ if (!mpd_response_next(connection))
+ return mpdclient_handle_error(c);
- if( c->playlist.list )
- mpdclient_playlist_free(&c->playlist);
+ struct mpd_song *new_song = mpd_recv_song(connection);
+ if (!mpd_response_finish(connection) || new_song == NULL) {
+ if (new_song != NULL)
+ mpd_song_free(new_song);
- mpd_sendPlaylistInfoCommand(c->connection,-1);
- while( (entity=mpd_getNextInfoEntity(c->connection)) )
- {
- if(entity->type==MPD_INFO_ENTITY_TYPE_SONG)
- {
- mpd_Song *song = mpd_songDup(entity->info.song);
+ return mpd_connection_clear_error(connection) ||
+ mpdclient_handle_error(c);
+ }
+
+ if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
+ 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);
- c->playlist.list = g_list_append(c->playlist.list, (gpointer) song);
- c->playlist.length++;
+ /* the song we just received has the correct id;
+ append it to the local playlist */
+ playlist_append(&c->playlist, new_song);
}
- mpd_freeInfoEntity(entity);
- }
- c->playlist.id = c->status->playlist;
- c->song = NULL;
- c->playlist.updated = TRUE;
- /* call playlist updated callbacks */
- mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
+ mpd_song_free(new_song);
- return mpdclient_finish_command(c);
+ return true;
}
-#ifdef ENABLE_PLCHANGES
-
-gint
-mpdclient_compare_songs(gconstpointer a, gconstpointer b)
+bool
+mpdclient_cmd_delete(struct mpdclient *c, gint idx)
{
- mpd_Song *song1 = (mpd_Song *) a;
- mpd_Song *song2 = (mpd_Song *) b;
+ struct mpd_connection *connection = mpdclient_get_connection(c);
- return song1->pos - song2->pos;
-}
+ if (connection == NULL || c->status == NULL)
+ return false;
+ if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
+ return false;
+ const struct mpd_song *song = playlist_get(&c->playlist, idx);
-/* update playlist (plchanges) */
-gint
-mpdclient_playlist_update_changes(mpdclient_t *c)
-{
- mpd_InfoEntity *entity;
+ /* send the delete command to mpd; at the same time, get the
+ new status (to verify the playlist id) */
- D("mpdclient_playlist_update_changes() [%lld -> %lld]\n",
- c->status->playlist, c->playlist.id);
+ if (!mpd_command_list_begin(connection, false) ||
+ !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
+ !mpd_send_status(connection) ||
+ !mpd_command_list_end(connection))
+ return mpdclient_handle_error(c);
- if( MPD_ERROR(c) )
- return -1;
+ c->events |= MPD_IDLE_QUEUE;
- mpd_sendPlChangesCommand(c->connection, c->playlist.id);
+ struct mpd_status *status = mpdclient_recv_status(c);
+ if (status == NULL)
+ return false;
- while( (entity=mpd_getNextInfoEntity(c->connection)) != NULL )
- {
- mpd_Song *song = entity->info.song;
+ if (!mpd_response_finish(connection))
+ return mpdclient_handle_error(c);
- if( song->pos < c->playlist.length )
- {
- GList *item = g_list_nth(c->playlist.list, song->pos);
+ if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
+ 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);
- /* update song */
- D("updating pos:%d, id=%d [%p] - %s\n",
- song->pos, song->id, item, song->file);
- mpd_freeSong((mpd_Song *) item->data);
- item->data = mpd_songDup(song);
- }
- else
- {
- /* add a new song */
- D("adding song at pos %d\n", song->pos);
- c->playlist.list = g_list_append(c->playlist.list,
- (gpointer) mpd_songDup(song));
+ /* remove the song from the local playlist */
+ playlist_remove(&c->playlist, idx);
+
+ /* remove references to the song */
+ if (c->song == song)
+ c->song = NULL;
}
-
- }
- /* remove trailing songs */
- while( c->status->playlistLength < c->playlist.length )
- {
- GList *item = g_list_last(c->playlist.list);
+ return true;
+}
- /* Remove the last playlist entry */
- D("removing song at pos %d\n", ((mpd_Song *) item->data)->pos);
- mpd_freeSong((mpd_Song *) item->data);
- c->playlist.list = g_list_delete_link(c->playlist.list, item);
- c->playlist.length = g_list_length(c->playlist.list);
- }
+bool
+mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
+{
+ if (end == start + 1)
+ /* if that's not really a range, we choose to use the
+ safer "deleteid" version */
+ return mpdclient_cmd_delete(c, start);
- c->song = NULL;
- c->playlist.id = c->status->playlist;
- c->playlist.updated = TRUE;
- c->playlist.length = g_list_length(c->playlist.list);
+ struct mpd_connection *connection = mpdclient_get_connection(c);
+ if (connection == NULL)
+ return false;
- mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
+ /* send the delete command to mpd; at the same time, get the
+ new status (to verify the playlist id) */
- return 0;
-}
+ if (!mpd_command_list_begin(connection, false) ||
+ !mpd_send_delete_range(connection, start, end) ||
+ !mpd_send_status(connection) ||
+ !mpd_command_list_end(connection))
+ return mpdclient_handle_error(c);
-#else
-gint
-mpdclient_playlist_update_changes(mpdclient_t *c)
-{
- return mpdclient_playlist_update(c);
+ c->events |= MPD_IDLE_QUEUE;
+
+ struct mpd_status *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) - (end - start) &&
+ 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);
+
+ /* remove the song from the local playlist */
+ while (end > start) {
+ --end;
+
+ /* remove references to the song */
+ if (c->song == playlist_get(&c->playlist, end))
+ c->song = NULL;
+
+ playlist_remove(&c->playlist, end);
+ }
+ }
+
+ return true;
}
-#endif
-mpd_Song *
-playlist_get_song(mpdclient_t *c, gint index)
+bool
+mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
{
- return (mpd_Song *) g_list_nth_data(c->playlist.list, index);
+ if (dest_pos == src_pos)
+ return true;
+
+ struct mpd_connection *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;
+
+ struct mpd_status *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;
}
-GList *
-playlist_lookup(mpdclient_t *c, int id)
+/* The client-to-client protocol (MPD 0.17.0) */
+
+bool
+mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
{
- GList *list = g_list_first(c->playlist.list);
+ struct mpd_connection *connection = mpdclient_get_connection(c);
+
+ if (connection == NULL)
+ return false;
- while( list )
- {
- mpd_Song *song = (mpd_Song *) list->data;
- if( song->id == id )
- return list;
- list=list->next;
- }
- return NULL;
+ if (!mpd_send_subscribe(connection, channel))
+ return mpdclient_handle_error(c);
+
+ return mpdclient_finish_command(c);
}
-mpd_Song *
-playlist_lookup_song(mpdclient_t *c, gint id)
+bool
+mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
{
- GList *list = c->playlist.list;
+ struct mpd_connection *connection = mpdclient_get_connection(c);
+ if (connection == NULL)
+ return false;
+
+ if (!mpd_send_unsubscribe(connection, channel))
+ return mpdclient_handle_error(c);
- while( list )
- {
- mpd_Song *song = (mpd_Song *) list->data;
- if( song->id == id )
- return song;
- list=list->next;
- }
- return NULL;
+ return mpdclient_finish_command(c);
}
-gint
-playlist_get_index(mpdclient_t *c, mpd_Song *song)
+bool
+mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
+ const char *text)
{
- return g_list_index(c->playlist.list, song);
+ 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);
}
-gint
-playlist_get_index_from_id(mpdclient_t *c, gint id)
+bool
+mpdclient_send_read_messages(struct mpdclient *c)
{
- return g_list_index(c->playlist.list, playlist_lookup_song(c, id));
+ struct mpd_connection *connection = mpdclient_get_connection(c);
+ if (connection == NULL)
+ return false;
+
+ return mpd_send_read_messages(connection)?
+ true : mpdclient_handle_error(c);
}
-gint
-playlist_get_index_from_file(mpdclient_t *c, gchar *filename)
+struct mpd_message *
+mpdclient_recv_message(struct mpdclient *c)
{
- GList *list = c->playlist.list;
- gint i=0;
+ struct mpd_connection *connection = mpdclient_get_connection(c);
+ if (connection == NULL)
+ return false;
- while( list )
- {
- mpd_Song *song = (mpd_Song *) list->data;
- if( strcmp(song->file, filename ) == 0 )
- return i;
- list=list->next;
- i++;
- }
- return -1;
-}
+ 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;
+}
/****************************************************************************/
-/*** Filelist functions *****************************************************/
+/*** Playlist management functions ******************************************/
/****************************************************************************/
-mpdclient_filelist_t *
-mpdclient_filelist_free(mpdclient_filelist_t *filelist)
-{
- GList *list = g_list_first(filelist->list);
-
- D("mpdclient_filelist_free()\n");
- if( list == NULL )
- return NULL;
- while( list!=NULL )
- {
- filelist_entry_t *entry = list->data;
-
- if( entry->entity )
- mpd_freeInfoEntity(entry->entity);
- g_free(entry);
- list=list->next;
- }
- g_list_free(filelist->list);
- g_free(filelist->path);
- filelist->path = NULL;
- filelist->list = NULL;
- filelist->length = 0;
- g_free(filelist);
-
- return NULL;
+/* update playlist */
+bool
+mpdclient_playlist_update(struct mpdclient *c)
+{
+ struct mpd_connection *connection = mpdclient_get_connection(c);
+ if (connection == NULL)
+ return false;
+
+ playlist_clear(&c->playlist);
+
+ mpd_send_list_queue_meta(connection);
+
+ struct mpd_entity *entity;
+ while ((entity = mpd_recv_entity(connection))) {
+ if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
+ playlist_append(&c->playlist, mpd_entity_get_song(entity));
+
+ mpd_entity_free(entity);
+ }
+
+ c->playlist.version = mpd_status_get_queue_version(c->status);
+ c->song = NULL;
+
+ return mpdclient_finish_command(c);
}
-
-mpdclient_filelist_t *
-mpdclient_filelist_get(mpdclient_t *c, gchar *path)
+/* update playlist (plchanges) */
+bool
+mpdclient_playlist_update_changes(struct mpdclient *c)
{
- mpdclient_filelist_t *filelist;
- mpd_InfoEntity *entity;
- gchar *path_utf8 = locale_to_utf8(path);
+ struct mpd_connection *connection = mpdclient_get_connection(c);
- D("mpdclient_filelist_get(%s)\n", path);
- mpd_sendLsInfoCommand(c->connection, path_utf8);
- filelist = g_malloc0(sizeof(mpdclient_filelist_t));
- if( path && path[0] && strcmp(path, "/") )
- {
- /* add a dummy entry for ./.. */
- filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
- entry->entity = NULL;
- filelist->list = g_list_append(filelist->list, (gpointer) entry);
- filelist->length++;
- }
-
- while( (entity=mpd_getNextInfoEntity(c->connection)) )
- {
- filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
-
- entry->entity = entity;
- filelist->list = g_list_append(filelist->list, (gpointer) entry);
- filelist->length++;
- }
-
- if( mpdclient_finish_command(c) )
- {
- g_free(path_utf8);
- return mpdclient_filelist_free(filelist);
- }
-
- g_free(path_utf8);
- filelist->path = g_strdup(path);
- filelist->updated = TRUE;
-
- return filelist;
-}
-
-mpdclient_filelist_t *
-mpdclient_filelist_search_utf8(mpdclient_t *c, int table, gchar *filter_utf8)
-{
- mpdclient_filelist_t *filelist;
- mpd_InfoEntity *entity;
-
- D("mpdclient_filelist_search(%s)\n", filter_utf8);
- mpd_sendSearchCommand(c->connection, table, filter_utf8);
- filelist = g_malloc0(sizeof(mpdclient_filelist_t));
-
- while( (entity=mpd_getNextInfoEntity(c->connection)) )
- {
- filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
-
- entry->entity = entity;
- filelist->list = g_list_append(filelist->list, (gpointer) entry);
- filelist->length++;
- }
-
- if( mpdclient_finish_command(c) )
- return mpdclient_filelist_free(filelist);
-
- filelist->updated = TRUE;
-
- return filelist;
-}
-
-
-mpdclient_filelist_t *
-mpdclient_filelist_search(mpdclient_t *c, int table, gchar *filter)
-{
- mpdclient_filelist_t *filelist;
- gchar *filter_utf8 = locale_to_utf8(filter);
-
- D("mpdclient_filelist_search(%s)\n", filter);
- filelist = mpdclient_filelist_search_utf8(c, table, filter_utf8);
- g_free(filter_utf8);
-
- return filelist;
-}
-
-mpdclient_filelist_t *
-mpdclient_filelist_update(mpdclient_t *c, mpdclient_filelist_t *filelist)
-{
- if( filelist != NULL )
- {
- gchar *path = g_strdup(filelist->path);
+ if (connection == NULL)
+ return false;
- filelist = mpdclient_filelist_free(filelist);
- filelist = mpdclient_filelist_get(c, path);
- g_free(path);
- return filelist;
- }
- return NULL;
-}
+ mpd_send_queue_changes_meta(connection, c->playlist.version);
-filelist_entry_t *
-mpdclient_filelist_find_song(mpdclient_filelist_t *fl, mpd_Song *song)
-{
- GList *list = g_list_first(fl->list);
-
- while( list && song)
- {
- filelist_entry_t *entry = list->data;
- mpd_InfoEntity *entity = entry->entity;
-
- if( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG )
- {
- mpd_Song *song2 = entity->info.song;
-
- if( strcmp(song->file, song2->file) == 0 )
- {
- return entry;
- }
+ struct mpd_song *song;
+ while ((song = mpd_recv_song(connection)) != NULL) {
+ int pos = mpd_song_get_pos(song);
+
+ if (pos >= 0 && (guint)pos < c->playlist.list->len) {
+ /* update song */
+ playlist_replace(&c->playlist, pos, song);
+ } else {
+ /* add a new song */
+ playlist_append(&c->playlist, song);
+ }
+
+ mpd_song_free(song);
}
- list = list->next;
- }
- return NULL;
-}
+ /* remove trailing songs */
-GList *
-mpdclient_get_artists_utf8(mpdclient_t *c)
-{
- gchar *str = NULL;
- GList *list = NULL;
+ unsigned length = mpd_status_get_queue_length(c->status);
+ while (length < c->playlist.list->len) {
+ guint pos = c->playlist.list->len - 1;
- D("mpdclient_get_artists()\n");
- mpd_sendListCommand(c->connection, MPD_TABLE_ARTIST, NULL);
- while( (str=mpd_getNextArtist(c->connection)) )
- {
- list = g_list_append(list, (gpointer) str);
- }
- if( mpdclient_finish_command(c) )
- {
- return string_list_free(list);
- }
+ /* Remove the last playlist entry */
+ playlist_remove(&c->playlist, pos);
+ }
+
+ c->song = NULL;
+ c->playlist.version = mpd_status_get_queue_version(c->status);
- return list;
+ return mpdclient_finish_command(c);
}
-GList *
-mpdclient_get_albums_utf8(mpdclient_t *c, gchar *artist_utf8)
+
+/****************************************************************************/
+/*** Filelist functions *****************************************************/
+/****************************************************************************/
+
+bool
+mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
{
- gchar *str = NULL;
- GList *list = NULL;
+ struct mpd_connection *connection = mpdclient_get_connection(c);
+ if (connection == NULL)
+ return false;
- D("mpdclient_get_albums(%s)\n", artist_utf8);
- mpd_sendListCommand(c->connection, MPD_TABLE_ALBUM, artist_utf8);
- while( (str=mpd_getNextAlbum(c->connection)) )
- {
- list = g_list_append(list, (gpointer) str);
- }
- if( mpdclient_finish_command(c) )
- {
- return string_list_free(list);
- }
-
- return list;
-}
+ if (filelist_is_empty(fl))
+ return true;
+ mpd_command_list_begin(connection, false);
+ for (unsigned i = 0; i < filelist_length(fl); ++i) {
+ struct filelist_entry *entry = filelist_get(fl, i);
+ struct mpd_entity *entity = entry->entity;
+ if (entity != NULL &&
+ mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
+ const struct mpd_song *song =
+ mpd_entity_get_song(entity);
+ mpd_send_add(connection, mpd_song_get_uri(song));
+ }
+ }
+ mpd_command_list_end(connection);
+ return mpdclient_finish_command(c);
+}