Code

main: move default_settings_name() to mpdclient.c
[ncmpc.git] / src / mpdclient.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
20 #include "mpdclient.h"
21 #include "callbacks.h"
22 #include "filelist.h"
23 #include "config.h"
24 #include "gidle.h"
25 #include "charset.h"
27 #ifdef ENABLE_ASYNC_CONNECT
28 #include "aconnect.h"
29 #endif
31 #include <mpd/client.h>
33 #include <assert.h>
35 static gboolean
36 mpdclient_enter_idle_callback(gpointer user_data)
37 {
38         struct mpdclient *c = user_data;
39         assert(c->enter_idle_source_id != 0);
40         assert(c->source != NULL);
41         assert(!c->idle);
43         c->enter_idle_source_id = 0;
44         c->idle = mpd_glib_enter(c->source);
45         return false;
46 }
48 static void
49 mpdclient_schedule_enter_idle(struct mpdclient *c)
50 {
51         assert(c != NULL);
52         assert(c->source != NULL);
54         if (c->enter_idle_source_id == 0)
55                 /* automatically re-enter MPD "idle" mode */
56                 c->enter_idle_source_id =
57                         g_idle_add(mpdclient_enter_idle_callback, c);
58 }
60 static void
61 mpdclient_cancel_enter_idle(struct mpdclient *c)
62 {
63         if (c->enter_idle_source_id != 0) {
64                 g_source_remove(c->enter_idle_source_id);
65                 c->enter_idle_source_id = 0;
66         }
67 }
69 static void
70 mpdclient_invoke_error_callback(enum mpd_error error,
71                                 const char *message)
72 {
73         char *allocated;
74         if (error == MPD_ERROR_SERVER)
75                 /* server errors are UTF-8, the others are locale */
76                 message = allocated = utf8_to_locale(message);
77         else
78                 allocated = NULL;
80         mpdclient_error_callback(message);
81         g_free(allocated);
82 }
84 static void
85 mpdclient_invoke_error_callback1(struct mpdclient *c)
86 {
87         assert(c != NULL);
88         assert(c->connection != NULL);
90         struct mpd_connection *connection = c->connection;
92         enum mpd_error error = mpd_connection_get_error(connection);
93         assert(error != MPD_ERROR_SUCCESS);
95         mpdclient_invoke_error_callback(error,
96                                         mpd_connection_get_error_message(connection));
97 }
99 static void
100 mpdclient_gidle_callback(enum mpd_error error,
101                          gcc_unused enum mpd_server_error server_error,
102                          const char *message, enum mpd_idle events,
103                          void *ctx)
105         struct mpdclient *c = ctx;
107         c->idle = false;
109         assert(mpdclient_is_connected(c));
111         if (error != MPD_ERROR_SUCCESS) {
112                 mpdclient_invoke_error_callback(error, message);
113                 mpdclient_disconnect(c);
114                 mpdclient_lost_callback();
115                 return;
116         }
118         c->events |= events;
119         mpdclient_update(c);
121         mpdclient_idle_callback(c->events);
123         c->events = 0;
125         if (c->source != NULL)
126                 mpdclient_schedule_enter_idle(c);
129 /****************************************************************************/
130 /*** mpdclient functions ****************************************************/
131 /****************************************************************************/
133 bool
134 mpdclient_handle_error(struct mpdclient *c)
136         enum mpd_error error = mpd_connection_get_error(c->connection);
138         assert(error != MPD_ERROR_SUCCESS);
140         if (error == MPD_ERROR_SERVER &&
141             mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_PERMISSION &&
142             mpdclient_auth_callback(c))
143                 return true;
145         mpdclient_invoke_error_callback(error,
146                                         mpd_connection_get_error_message(c->connection));
148         if (!mpd_connection_clear_error(c->connection)) {
149                 mpdclient_disconnect(c);
150                 mpdclient_lost_callback();
151         }
153         return false;
156 struct mpdclient *
157 mpdclient_new(const gchar *host, unsigned port,
158               unsigned timeout_ms, const gchar *password)
160         struct mpdclient *c = g_new0(struct mpdclient, 1);
162 #ifdef ENABLE_ASYNC_CONNECT
163         c->settings = mpd_settings_new(host, port, timeout_ms,
164                                        NULL, NULL);
165         if (c->settings == NULL)
166                 g_error("Out of memory");
167 #else
168         c->host = host;
169         c->port = port;
170 #endif
172         c->timeout_ms = timeout_ms;
173         c->password = password;
175         playlist_init(&c->playlist);
176         c->volume = -1;
177         c->events = 0;
178         c->playing = false;
180         return c;
183 void
184 mpdclient_free(struct mpdclient *c)
186         mpdclient_disconnect(c);
188         mpdclient_playlist_free(&c->playlist);
190 #ifdef ENABLE_ASYNC_CONNECT
191         mpd_settings_free(c->settings);
192 #endif
194         g_free(c);
197 static char *
198 settings_name(const struct mpd_settings *settings)
200         assert(settings != NULL);
202         const char *host = mpd_settings_get_host(settings);
203         if (host == NULL)
204                 host = "unknown";
206         if (host[0] == '/')
207                 return g_strdup(host);
209         unsigned port = mpd_settings_get_port(settings);
210         if (port == 0 || port == 6600)
211                 return g_strdup(host);
213         return g_strdup_printf("%s:%u", host, port);
216 char *
217 mpdclient_settings_name(const struct mpdclient *c)
219         assert(c != NULL);
221 #ifdef ENABLE_ASYNC_CONNECT
222         return settings_name(c->settings);
223 #else
224         struct mpd_settings *settings =
225                 mpd_settings_new(c->host, c->port, 0, NULL, NULL);
226         if (settings == NULL)
227                 return g_strdup("unknown");
229         char *name = settings_name(settings);
230         mpd_settings_free(settings);
231         return name;
232 #endif
235 static void
236 mpdclient_status_free(struct mpdclient *c)
238         if (c->status == NULL)
239                 return;
241         mpd_status_free(c->status);
242         c->status = NULL;
244         c->volume = -1;
245         c->playing = false;
248 void
249 mpdclient_disconnect(struct mpdclient *c)
251 #ifdef ENABLE_ASYNC_CONNECT
252         if (c->async_connect != NULL) {
253                 aconnect_cancel(c->async_connect);
254                 c->async_connect = NULL;
255         }
256 #endif
258         mpdclient_cancel_enter_idle(c);
260         if (c->source != NULL) {
261                 mpd_glib_free(c->source);
262                 c->source = NULL;
263                 c->idle = false;
264         }
266         if (c->connection) {
267                 mpd_connection_free(c->connection);
268                 ++c->connection_id;
269         }
270         c->connection = NULL;
272         mpdclient_status_free(c);
274         playlist_clear(&c->playlist);
276         if (c->song)
277                 c->song = NULL;
279         /* everything has changed after a disconnect */
280         c->events |= MPD_IDLE_ALL;
283 static bool
284 mpdclient_connected(struct mpdclient *c,
285                     struct mpd_connection *connection)
287         c->connection = connection;
289         if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
290                 mpdclient_invoke_error_callback1(c);
291                 mpdclient_disconnect(c);
292                 mpdclient_failed_callback();
293                 return false;
294         }
296 #ifdef ENABLE_ASYNC_CONNECT
297         if (c->timeout_ms > 0)
298                 mpd_connection_set_timeout(connection, c->timeout_ms);
299 #endif
301         /* send password */
302         if (c->password != NULL &&
303             !mpd_run_password(connection, c->password)) {
304                 mpdclient_invoke_error_callback1(c);
305                 mpdclient_disconnect(c);
306                 mpdclient_failed_callback();
307                 return false;
308         }
310         c->source = mpd_glib_new(connection,
311                                  mpdclient_gidle_callback, c);
312         mpdclient_schedule_enter_idle(c);
314         ++c->connection_id;
316         mpdclient_connected_callback();
317         return true;
320 #ifdef ENABLE_ASYNC_CONNECT
322 static void
323 mpdclient_connect_success(struct mpd_connection *connection, void *ctx)
325         struct mpdclient *c = ctx;
326         assert(c->async_connect != NULL);
327         c->async_connect = NULL;
329         mpdclient_connected(c, connection);
332 static void
333 mpdclient_connect_error(const char *message, void *ctx)
335         struct mpdclient *c = ctx;
336         assert(c->async_connect != NULL);
337         c->async_connect = NULL;
339         mpdclient_error_callback(message);
340         mpdclient_failed_callback();
343 static const struct aconnect_handler mpdclient_connect_handler = {
344         .success = mpdclient_connect_success,
345         .error = mpdclient_connect_error,
346 };
348 #endif
350 void
351 mpdclient_connect(struct mpdclient *c)
353         /* close any open connection */
354         mpdclient_disconnect(c);
356 #ifdef ENABLE_ASYNC_CONNECT
357         aconnect_start(&c->async_connect,
358                        mpd_settings_get_host(c->settings),
359                        mpd_settings_get_port(c->settings),
360                        &mpdclient_connect_handler, c);
361 #else
362         /* connect to MPD */
363         struct mpd_connection *connection =
364                 mpd_connection_new(c->host, c->port, c->timeout_ms);
365         if (connection == NULL)
366                 g_error("Out of memory");
368         mpdclient_connected(c, connection);
369 #endif
372 bool
373 mpdclient_update(struct mpdclient *c)
375         struct mpd_connection *connection = mpdclient_get_connection(c);
377         if (connection == NULL)
378                 return false;
380         /* free the old status */
381         mpdclient_status_free(c);
383         /* retrieve new status */
384         c->status = mpd_run_status(connection);
385         if (c->status == NULL)
386                 return mpdclient_handle_error(c);
388         c->volume = mpd_status_get_volume(c->status);
389         c->playing = mpd_status_get_state(c->status) == MPD_STATE_PLAY;
391         /* check if the playlist needs an update */
392         if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
393                 bool retval;
395                 if (!playlist_is_empty(&c->playlist))
396                         retval = mpdclient_playlist_update_changes(c);
397                 else
398                         retval = mpdclient_playlist_update(c);
399                 if (!retval)
400                         return false;
401         }
403         /* update the current song */
404         if (!c->song || mpd_status_get_song_id(c->status) >= 0) {
405                 c->song = playlist_get_song(&c->playlist,
406                                             mpd_status_get_song_pos(c->status));
407         }
409         return true;
412 struct mpd_connection *
413 mpdclient_get_connection(struct mpdclient *c)
415         if (c->source != NULL && c->idle) {
416                 c->idle = false;
417                 mpd_glib_leave(c->source);
419                 mpdclient_schedule_enter_idle(c);
420         }
422         return c->connection;
425 static struct mpd_status *
426 mpdclient_recv_status(struct mpdclient *c)
428         assert(c->connection != NULL);
430         struct mpd_status *status = mpd_recv_status(c->connection);
431         if (status == NULL) {
432                 mpdclient_handle_error(c);
433                 return NULL;
434         }
436         if (c->status != NULL)
437                 mpd_status_free(c->status);
438         return c->status = status;
441 /****************************************************************************/
442 /*** MPD Commands  **********************************************************/
443 /****************************************************************************/
445 bool
446 mpdclient_cmd_crop(struct mpdclient *c)
448         if (!mpdclient_is_playing(c))
449                 return false;
451         int length = mpd_status_get_queue_length(c->status);
452         int current = mpd_status_get_song_pos(c->status);
453         if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
454                 return true;
456         struct mpd_connection *connection = mpdclient_get_connection(c);
457         if (connection == NULL)
458                 return false;
460         mpd_command_list_begin(connection, false);
462         if (current < length - 1)
463                 mpd_send_delete_range(connection, current + 1, length);
464         if (current > 0)
465                 mpd_send_delete_range(connection, 0, current);
467         mpd_command_list_end(connection);
469         return mpdclient_finish_command(c);
472 bool
473 mpdclient_cmd_clear(struct mpdclient *c)
475         struct mpd_connection *connection = mpdclient_get_connection(c);
476         if (connection == NULL)
477                 return false;
479         /* send "clear" and "status" */
480         if (!mpd_command_list_begin(connection, false) ||
481             !mpd_send_clear(connection) ||
482             !mpd_send_status(connection) ||
483             !mpd_command_list_end(connection))
484                 return mpdclient_handle_error(c);
486         /* receive the new status, store it in the mpdclient struct */
488         struct mpd_status *status = mpdclient_recv_status(c);
489         if (status == NULL)
490                 return false;
492         if (!mpd_response_finish(connection))
493                 return mpdclient_handle_error(c);
495         /* update mpdclient.playlist */
497         if (mpd_status_get_queue_length(status) == 0) {
498                 /* after the "clear" command, the queue is really
499                    empty - this means we can clear it locally,
500                    reducing the UI latency */
501                 playlist_clear(&c->playlist);
502                 c->playlist.version = mpd_status_get_queue_version(status);
503                 c->song = NULL;
504         }
506         c->events |= MPD_IDLE_QUEUE;
507         return true;
510 bool
511 mpdclient_cmd_volume(struct mpdclient *c, gint value)
513         struct mpd_connection *connection = mpdclient_get_connection(c);
514         if (connection == NULL)
515                 return false;
517         mpd_send_set_volume(connection, value);
518         return mpdclient_finish_command(c);
521 bool
522 mpdclient_cmd_volume_up(struct mpdclient *c)
524         if (c->volume < 0 || c->volume >= 100)
525                 return true;
527         struct mpd_connection *connection = mpdclient_get_connection(c);
528         if (connection == NULL)
529                 return false;
531         return mpdclient_cmd_volume(c, ++c->volume);
534 bool
535 mpdclient_cmd_volume_down(struct mpdclient *c)
537         if (c->volume <= 0)
538                 return true;
540         struct mpd_connection *connection = mpdclient_get_connection(c);
541         if (connection == NULL)
542                 return false;
544         return mpdclient_cmd_volume(c, --c->volume);
547 bool
548 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
550         struct mpd_connection *connection = mpdclient_get_connection(c);
551         if (connection == NULL)
552                 return false;
554         return mpd_send_add(connection, path_utf8)?
555                 mpdclient_finish_command(c) : false;
558 bool
559 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
561         assert(c != NULL);
562         assert(song != NULL);
564         struct mpd_connection *connection = mpdclient_get_connection(c);
565         if (connection == NULL || c->status == NULL)
566                 return false;
568         /* send the add command to mpd; at the same time, get the new
569            status (to verify the new playlist id) and the last song
570            (we hope that's the song we just added) */
572         if (!mpd_command_list_begin(connection, true) ||
573             !mpd_send_add(connection, mpd_song_get_uri(song)) ||
574             !mpd_send_status(connection) ||
575             !mpd_send_get_queue_song_pos(connection,
576                                          playlist_length(&c->playlist)) ||
577             !mpd_command_list_end(connection) ||
578             !mpd_response_next(connection))
579                 return mpdclient_handle_error(c);
581         c->events |= MPD_IDLE_QUEUE;
583         struct mpd_status *status = mpdclient_recv_status(c);
584         if (status == NULL)
585                 return false;
587         if (!mpd_response_next(connection))
588                 return mpdclient_handle_error(c);
590         struct mpd_song *new_song = mpd_recv_song(connection);
591         if (!mpd_response_finish(connection) || new_song == NULL) {
592                 if (new_song != NULL)
593                         mpd_song_free(new_song);
595                 return mpd_connection_clear_error(connection) ||
596                         mpdclient_handle_error(c);
597         }
599         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
600             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
601                 /* the cheap route: match on the new playlist length
602                    and its version, we can keep our local playlist
603                    copy in sync */
604                 c->playlist.version = mpd_status_get_queue_version(status);
606                 /* the song we just received has the correct id;
607                    append it to the local playlist */
608                 playlist_append(&c->playlist, new_song);
609         }
611         mpd_song_free(new_song);
613         return true;
616 bool
617 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
619         struct mpd_connection *connection = mpdclient_get_connection(c);
621         if (connection == NULL || c->status == NULL)
622                 return false;
624         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
625                 return false;
627         const struct mpd_song *song = playlist_get(&c->playlist, idx);
629         /* send the delete command to mpd; at the same time, get the
630            new status (to verify the playlist id) */
632         if (!mpd_command_list_begin(connection, false) ||
633             !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
634             !mpd_send_status(connection) ||
635             !mpd_command_list_end(connection))
636                 return mpdclient_handle_error(c);
638         c->events |= MPD_IDLE_QUEUE;
640         struct mpd_status *status = mpdclient_recv_status(c);
641         if (status == NULL)
642                 return false;
644         if (!mpd_response_finish(connection))
645                 return mpdclient_handle_error(c);
647         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
648             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
649                 /* the cheap route: match on the new playlist length
650                    and its version, we can keep our local playlist
651                    copy in sync */
652                 c->playlist.version = mpd_status_get_queue_version(status);
654                 /* remove the song from the local playlist */
655                 playlist_remove(&c->playlist, idx);
657                 /* remove references to the song */
658                 if (c->song == song)
659                         c->song = NULL;
660         }
662         return true;
665 bool
666 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
668         if (end == start + 1)
669                 /* if that's not really a range, we choose to use the
670                    safer "deleteid" version */
671                 return mpdclient_cmd_delete(c, start);
673         struct mpd_connection *connection = mpdclient_get_connection(c);
674         if (connection == NULL)
675                 return false;
677         /* send the delete command to mpd; at the same time, get the
678            new status (to verify the playlist id) */
680         if (!mpd_command_list_begin(connection, false) ||
681             !mpd_send_delete_range(connection, start, end) ||
682             !mpd_send_status(connection) ||
683             !mpd_command_list_end(connection))
684                 return mpdclient_handle_error(c);
686         c->events |= MPD_IDLE_QUEUE;
688         struct mpd_status *status = mpdclient_recv_status(c);
689         if (status == NULL)
690                 return false;
692         if (!mpd_response_finish(connection))
693                 return mpdclient_handle_error(c);
695         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
696             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
697                 /* the cheap route: match on the new playlist length
698                    and its version, we can keep our local playlist
699                    copy in sync */
700                 c->playlist.version = mpd_status_get_queue_version(status);
702                 /* remove the song from the local playlist */
703                 while (end > start) {
704                         --end;
706                         /* remove references to the song */
707                         if (c->song == playlist_get(&c->playlist, end))
708                                 c->song = NULL;
710                         playlist_remove(&c->playlist, end);
711                 }
712         }
714         return true;
717 bool
718 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
720         if (dest_pos == src_pos)
721                 return true;
723         struct mpd_connection *connection = mpdclient_get_connection(c);
724         if (connection == NULL)
725                 return false;
727         /* send the "move" command to MPD; at the same time, get the
728            new status (to verify the playlist id) */
730         if (!mpd_command_list_begin(connection, false) ||
731             !mpd_send_move(connection, src_pos, dest_pos) ||
732             !mpd_send_status(connection) ||
733             !mpd_command_list_end(connection))
734                 return mpdclient_handle_error(c);
736         c->events |= MPD_IDLE_QUEUE;
738         struct mpd_status *status = mpdclient_recv_status(c);
739         if (status == NULL)
740                 return false;
742         if (!mpd_response_finish(connection))
743                 return mpdclient_handle_error(c);
745         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
746             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
747                 /* the cheap route: match on the new playlist length
748                    and its version, we can keep our local playlist
749                    copy in sync */
750                 c->playlist.version = mpd_status_get_queue_version(status);
752                 /* swap songs in the local playlist */
753                 playlist_move(&c->playlist, dest_pos, src_pos);
754         }
756         return true;
759 /* The client-to-client protocol (MPD 0.17.0) */
761 bool
762 mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
764         struct mpd_connection *connection = mpdclient_get_connection(c);
766         if (connection == NULL)
767                 return false;
769         if (!mpd_send_subscribe(connection, channel))
770                 return mpdclient_handle_error(c);
772         return mpdclient_finish_command(c);
775 bool
776 mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
778         struct mpd_connection *connection = mpdclient_get_connection(c);
779         if (connection == NULL)
780                 return false;
782         if (!mpd_send_unsubscribe(connection, channel))
783                 return mpdclient_handle_error(c);
785         return mpdclient_finish_command(c);
788 bool
789 mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
790                            const char *text)
792         struct mpd_connection *connection = mpdclient_get_connection(c);
793         if (connection == NULL)
794                 return false;
796         if (!mpd_send_send_message(connection, channel, text))
797                 return mpdclient_handle_error(c);
799         return mpdclient_finish_command(c);
802 bool
803 mpdclient_send_read_messages(struct mpdclient *c)
805         struct mpd_connection *connection = mpdclient_get_connection(c);
806         if (connection == NULL)
807                 return false;
809         return mpd_send_read_messages(connection)?
810                 true : mpdclient_handle_error(c);
813 struct mpd_message *
814 mpdclient_recv_message(struct mpdclient *c)
816         struct mpd_connection *connection = mpdclient_get_connection(c);
817         if (connection == NULL)
818                 return false;
820         struct mpd_message *message = mpd_recv_message(connection);
821         if (message == NULL &&
822             mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS)
823                 mpdclient_handle_error(c);
825         return message;
828 /****************************************************************************/
829 /*** Playlist management functions ******************************************/
830 /****************************************************************************/
832 /* update playlist */
833 bool
834 mpdclient_playlist_update(struct mpdclient *c)
836         struct mpd_connection *connection = mpdclient_get_connection(c);
837         if (connection == NULL)
838                 return false;
840         playlist_clear(&c->playlist);
842         mpd_send_list_queue_meta(connection);
844         struct mpd_entity *entity;
845         while ((entity = mpd_recv_entity(connection))) {
846                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
847                         playlist_append(&c->playlist, mpd_entity_get_song(entity));
849                 mpd_entity_free(entity);
850         }
852         c->playlist.version = mpd_status_get_queue_version(c->status);
853         c->song = NULL;
855         return mpdclient_finish_command(c);
858 /* update playlist (plchanges) */
859 bool
860 mpdclient_playlist_update_changes(struct mpdclient *c)
862         struct mpd_connection *connection = mpdclient_get_connection(c);
864         if (connection == NULL)
865                 return false;
867         mpd_send_queue_changes_meta(connection, c->playlist.version);
869         struct mpd_song *song;
870         while ((song = mpd_recv_song(connection)) != NULL) {
871                 int pos = mpd_song_get_pos(song);
873                 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
874                         /* update song */
875                         playlist_replace(&c->playlist, pos, song);
876                 } else {
877                         /* add a new song */
878                         playlist_append(&c->playlist, song);
879                 }
881                 mpd_song_free(song);
882         }
884         /* remove trailing songs */
886         unsigned length = mpd_status_get_queue_length(c->status);
887         while (length < c->playlist.list->len) {
888                 guint pos = c->playlist.list->len - 1;
890                 /* Remove the last playlist entry */
891                 playlist_remove(&c->playlist, pos);
892         }
894         c->song = NULL;
895         c->playlist.version = mpd_status_get_queue_version(c->status);
897         return mpdclient_finish_command(c);
901 /****************************************************************************/
902 /*** Filelist functions *****************************************************/
903 /****************************************************************************/
905 bool
906 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
908         struct mpd_connection *connection = mpdclient_get_connection(c);
909         if (connection == NULL)
910                 return false;
912         if (filelist_is_empty(fl))
913                 return true;
915         mpd_command_list_begin(connection, false);
917         for (unsigned i = 0; i < filelist_length(fl); ++i) {
918                 struct filelist_entry *entry = filelist_get(fl, i);
919                 struct mpd_entity *entity  = entry->entity;
921                 if (entity != NULL &&
922                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
923                         const struct mpd_song *song =
924                                 mpd_entity_get_song(entity);
926                         mpd_send_add(connection, mpd_song_get_uri(song));
927                 }
928         }
930         mpd_command_list_end(connection);
931         return mpdclient_finish_command(c);