Code

870839fd27dbb69ee8abd1d4b65e79d8c8567ee4
[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 static void
349 mpdclient_aconnect_start(struct mpdclient *c,
350                          const struct mpd_settings *settings)
352         aconnect_start(&c->async_connect,
353                        mpd_settings_get_host(settings),
354                        mpd_settings_get_port(settings),
355                        &mpdclient_connect_handler, c);
358 #endif
360 void
361 mpdclient_connect(struct mpdclient *c)
363         /* close any open connection */
364         mpdclient_disconnect(c);
366 #ifdef ENABLE_ASYNC_CONNECT
367         mpdclient_aconnect_start(c, c->settings);
368 #else
369         /* connect to MPD */
370         struct mpd_connection *connection =
371                 mpd_connection_new(c->host, c->port, c->timeout_ms);
372         if (connection == NULL)
373                 g_error("Out of memory");
375         mpdclient_connected(c, connection);
376 #endif
379 bool
380 mpdclient_update(struct mpdclient *c)
382         struct mpd_connection *connection = mpdclient_get_connection(c);
384         if (connection == NULL)
385                 return false;
387         /* free the old status */
388         mpdclient_status_free(c);
390         /* retrieve new status */
391         c->status = mpd_run_status(connection);
392         if (c->status == NULL)
393                 return mpdclient_handle_error(c);
395         c->volume = mpd_status_get_volume(c->status);
396         c->playing = mpd_status_get_state(c->status) == MPD_STATE_PLAY;
398         /* check if the playlist needs an update */
399         if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
400                 bool retval;
402                 if (!playlist_is_empty(&c->playlist))
403                         retval = mpdclient_playlist_update_changes(c);
404                 else
405                         retval = mpdclient_playlist_update(c);
406                 if (!retval)
407                         return false;
408         }
410         /* update the current song */
411         if (!c->song || mpd_status_get_song_id(c->status) >= 0) {
412                 c->song = playlist_get_song(&c->playlist,
413                                             mpd_status_get_song_pos(c->status));
414         }
416         return true;
419 struct mpd_connection *
420 mpdclient_get_connection(struct mpdclient *c)
422         if (c->source != NULL && c->idle) {
423                 c->idle = false;
424                 mpd_glib_leave(c->source);
426                 mpdclient_schedule_enter_idle(c);
427         }
429         return c->connection;
432 static struct mpd_status *
433 mpdclient_recv_status(struct mpdclient *c)
435         assert(c->connection != NULL);
437         struct mpd_status *status = mpd_recv_status(c->connection);
438         if (status == NULL) {
439                 mpdclient_handle_error(c);
440                 return NULL;
441         }
443         if (c->status != NULL)
444                 mpd_status_free(c->status);
445         return c->status = status;
448 /****************************************************************************/
449 /*** MPD Commands  **********************************************************/
450 /****************************************************************************/
452 bool
453 mpdclient_cmd_crop(struct mpdclient *c)
455         if (!mpdclient_is_playing(c))
456                 return false;
458         int length = mpd_status_get_queue_length(c->status);
459         int current = mpd_status_get_song_pos(c->status);
460         if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
461                 return true;
463         struct mpd_connection *connection = mpdclient_get_connection(c);
464         if (connection == NULL)
465                 return false;
467         mpd_command_list_begin(connection, false);
469         if (current < length - 1)
470                 mpd_send_delete_range(connection, current + 1, length);
471         if (current > 0)
472                 mpd_send_delete_range(connection, 0, current);
474         mpd_command_list_end(connection);
476         return mpdclient_finish_command(c);
479 bool
480 mpdclient_cmd_clear(struct mpdclient *c)
482         struct mpd_connection *connection = mpdclient_get_connection(c);
483         if (connection == NULL)
484                 return false;
486         /* send "clear" and "status" */
487         if (!mpd_command_list_begin(connection, false) ||
488             !mpd_send_clear(connection) ||
489             !mpd_send_status(connection) ||
490             !mpd_command_list_end(connection))
491                 return mpdclient_handle_error(c);
493         /* receive the new status, store it in the mpdclient struct */
495         struct mpd_status *status = mpdclient_recv_status(c);
496         if (status == NULL)
497                 return false;
499         if (!mpd_response_finish(connection))
500                 return mpdclient_handle_error(c);
502         /* update mpdclient.playlist */
504         if (mpd_status_get_queue_length(status) == 0) {
505                 /* after the "clear" command, the queue is really
506                    empty - this means we can clear it locally,
507                    reducing the UI latency */
508                 playlist_clear(&c->playlist);
509                 c->playlist.version = mpd_status_get_queue_version(status);
510                 c->song = NULL;
511         }
513         c->events |= MPD_IDLE_QUEUE;
514         return true;
517 bool
518 mpdclient_cmd_volume(struct mpdclient *c, gint value)
520         struct mpd_connection *connection = mpdclient_get_connection(c);
521         if (connection == NULL)
522                 return false;
524         mpd_send_set_volume(connection, value);
525         return mpdclient_finish_command(c);
528 bool
529 mpdclient_cmd_volume_up(struct mpdclient *c)
531         if (c->volume < 0 || c->volume >= 100)
532                 return true;
534         struct mpd_connection *connection = mpdclient_get_connection(c);
535         if (connection == NULL)
536                 return false;
538         return mpdclient_cmd_volume(c, ++c->volume);
541 bool
542 mpdclient_cmd_volume_down(struct mpdclient *c)
544         if (c->volume <= 0)
545                 return true;
547         struct mpd_connection *connection = mpdclient_get_connection(c);
548         if (connection == NULL)
549                 return false;
551         return mpdclient_cmd_volume(c, --c->volume);
554 bool
555 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
557         struct mpd_connection *connection = mpdclient_get_connection(c);
558         if (connection == NULL)
559                 return false;
561         return mpd_send_add(connection, path_utf8)?
562                 mpdclient_finish_command(c) : false;
565 bool
566 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
568         assert(c != NULL);
569         assert(song != NULL);
571         struct mpd_connection *connection = mpdclient_get_connection(c);
572         if (connection == NULL || c->status == NULL)
573                 return false;
575         /* send the add command to mpd; at the same time, get the new
576            status (to verify the new playlist id) and the last song
577            (we hope that's the song we just added) */
579         if (!mpd_command_list_begin(connection, true) ||
580             !mpd_send_add(connection, mpd_song_get_uri(song)) ||
581             !mpd_send_status(connection) ||
582             !mpd_send_get_queue_song_pos(connection,
583                                          playlist_length(&c->playlist)) ||
584             !mpd_command_list_end(connection) ||
585             !mpd_response_next(connection))
586                 return mpdclient_handle_error(c);
588         c->events |= MPD_IDLE_QUEUE;
590         struct mpd_status *status = mpdclient_recv_status(c);
591         if (status == NULL)
592                 return false;
594         if (!mpd_response_next(connection))
595                 return mpdclient_handle_error(c);
597         struct mpd_song *new_song = mpd_recv_song(connection);
598         if (!mpd_response_finish(connection) || new_song == NULL) {
599                 if (new_song != NULL)
600                         mpd_song_free(new_song);
602                 return mpd_connection_clear_error(connection) ||
603                         mpdclient_handle_error(c);
604         }
606         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
607             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
608                 /* the cheap route: match on the new playlist length
609                    and its version, we can keep our local playlist
610                    copy in sync */
611                 c->playlist.version = mpd_status_get_queue_version(status);
613                 /* the song we just received has the correct id;
614                    append it to the local playlist */
615                 playlist_append(&c->playlist, new_song);
616         }
618         mpd_song_free(new_song);
620         return true;
623 bool
624 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
626         struct mpd_connection *connection = mpdclient_get_connection(c);
628         if (connection == NULL || c->status == NULL)
629                 return false;
631         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
632                 return false;
634         const struct mpd_song *song = playlist_get(&c->playlist, idx);
636         /* send the delete command to mpd; at the same time, get the
637            new status (to verify the playlist id) */
639         if (!mpd_command_list_begin(connection, false) ||
640             !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
641             !mpd_send_status(connection) ||
642             !mpd_command_list_end(connection))
643                 return mpdclient_handle_error(c);
645         c->events |= MPD_IDLE_QUEUE;
647         struct mpd_status *status = mpdclient_recv_status(c);
648         if (status == NULL)
649                 return false;
651         if (!mpd_response_finish(connection))
652                 return mpdclient_handle_error(c);
654         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
655             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
656                 /* the cheap route: match on the new playlist length
657                    and its version, we can keep our local playlist
658                    copy in sync */
659                 c->playlist.version = mpd_status_get_queue_version(status);
661                 /* remove the song from the local playlist */
662                 playlist_remove(&c->playlist, idx);
664                 /* remove references to the song */
665                 if (c->song == song)
666                         c->song = NULL;
667         }
669         return true;
672 bool
673 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
675         if (end == start + 1)
676                 /* if that's not really a range, we choose to use the
677                    safer "deleteid" version */
678                 return mpdclient_cmd_delete(c, start);
680         struct mpd_connection *connection = mpdclient_get_connection(c);
681         if (connection == NULL)
682                 return false;
684         /* send the delete command to mpd; at the same time, get the
685            new status (to verify the playlist id) */
687         if (!mpd_command_list_begin(connection, false) ||
688             !mpd_send_delete_range(connection, start, end) ||
689             !mpd_send_status(connection) ||
690             !mpd_command_list_end(connection))
691                 return mpdclient_handle_error(c);
693         c->events |= MPD_IDLE_QUEUE;
695         struct mpd_status *status = mpdclient_recv_status(c);
696         if (status == NULL)
697                 return false;
699         if (!mpd_response_finish(connection))
700                 return mpdclient_handle_error(c);
702         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
703             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
704                 /* the cheap route: match on the new playlist length
705                    and its version, we can keep our local playlist
706                    copy in sync */
707                 c->playlist.version = mpd_status_get_queue_version(status);
709                 /* remove the song from the local playlist */
710                 while (end > start) {
711                         --end;
713                         /* remove references to the song */
714                         if (c->song == playlist_get(&c->playlist, end))
715                                 c->song = NULL;
717                         playlist_remove(&c->playlist, end);
718                 }
719         }
721         return true;
724 bool
725 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
727         if (dest_pos == src_pos)
728                 return true;
730         struct mpd_connection *connection = mpdclient_get_connection(c);
731         if (connection == NULL)
732                 return false;
734         /* send the "move" command to MPD; at the same time, get the
735            new status (to verify the playlist id) */
737         if (!mpd_command_list_begin(connection, false) ||
738             !mpd_send_move(connection, src_pos, dest_pos) ||
739             !mpd_send_status(connection) ||
740             !mpd_command_list_end(connection))
741                 return mpdclient_handle_error(c);
743         c->events |= MPD_IDLE_QUEUE;
745         struct mpd_status *status = mpdclient_recv_status(c);
746         if (status == NULL)
747                 return false;
749         if (!mpd_response_finish(connection))
750                 return mpdclient_handle_error(c);
752         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
753             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
754                 /* the cheap route: match on the new playlist length
755                    and its version, we can keep our local playlist
756                    copy in sync */
757                 c->playlist.version = mpd_status_get_queue_version(status);
759                 /* swap songs in the local playlist */
760                 playlist_move(&c->playlist, dest_pos, src_pos);
761         }
763         return true;
766 /* The client-to-client protocol (MPD 0.17.0) */
768 bool
769 mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
771         struct mpd_connection *connection = mpdclient_get_connection(c);
773         if (connection == NULL)
774                 return false;
776         if (!mpd_send_subscribe(connection, channel))
777                 return mpdclient_handle_error(c);
779         return mpdclient_finish_command(c);
782 bool
783 mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
785         struct mpd_connection *connection = mpdclient_get_connection(c);
786         if (connection == NULL)
787                 return false;
789         if (!mpd_send_unsubscribe(connection, channel))
790                 return mpdclient_handle_error(c);
792         return mpdclient_finish_command(c);
795 bool
796 mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
797                            const char *text)
799         struct mpd_connection *connection = mpdclient_get_connection(c);
800         if (connection == NULL)
801                 return false;
803         if (!mpd_send_send_message(connection, channel, text))
804                 return mpdclient_handle_error(c);
806         return mpdclient_finish_command(c);
809 bool
810 mpdclient_send_read_messages(struct mpdclient *c)
812         struct mpd_connection *connection = mpdclient_get_connection(c);
813         if (connection == NULL)
814                 return false;
816         return mpd_send_read_messages(connection)?
817                 true : mpdclient_handle_error(c);
820 struct mpd_message *
821 mpdclient_recv_message(struct mpdclient *c)
823         struct mpd_connection *connection = mpdclient_get_connection(c);
824         if (connection == NULL)
825                 return false;
827         struct mpd_message *message = mpd_recv_message(connection);
828         if (message == NULL &&
829             mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS)
830                 mpdclient_handle_error(c);
832         return message;
835 /****************************************************************************/
836 /*** Playlist management functions ******************************************/
837 /****************************************************************************/
839 /* update playlist */
840 bool
841 mpdclient_playlist_update(struct mpdclient *c)
843         struct mpd_connection *connection = mpdclient_get_connection(c);
844         if (connection == NULL)
845                 return false;
847         playlist_clear(&c->playlist);
849         mpd_send_list_queue_meta(connection);
851         struct mpd_entity *entity;
852         while ((entity = mpd_recv_entity(connection))) {
853                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
854                         playlist_append(&c->playlist, mpd_entity_get_song(entity));
856                 mpd_entity_free(entity);
857         }
859         c->playlist.version = mpd_status_get_queue_version(c->status);
860         c->song = NULL;
862         return mpdclient_finish_command(c);
865 /* update playlist (plchanges) */
866 bool
867 mpdclient_playlist_update_changes(struct mpdclient *c)
869         struct mpd_connection *connection = mpdclient_get_connection(c);
871         if (connection == NULL)
872                 return false;
874         mpd_send_queue_changes_meta(connection, c->playlist.version);
876         struct mpd_song *song;
877         while ((song = mpd_recv_song(connection)) != NULL) {
878                 int pos = mpd_song_get_pos(song);
880                 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
881                         /* update song */
882                         playlist_replace(&c->playlist, pos, song);
883                 } else {
884                         /* add a new song */
885                         playlist_append(&c->playlist, song);
886                 }
888                 mpd_song_free(song);
889         }
891         /* remove trailing songs */
893         unsigned length = mpd_status_get_queue_length(c->status);
894         while (length < c->playlist.list->len) {
895                 guint pos = c->playlist.list->len - 1;
897                 /* Remove the last playlist entry */
898                 playlist_remove(&c->playlist, pos);
899         }
901         c->song = NULL;
902         c->playlist.version = mpd_status_get_queue_version(c->status);
904         return mpdclient_finish_command(c);
908 /****************************************************************************/
909 /*** Filelist functions *****************************************************/
910 /****************************************************************************/
912 bool
913 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
915         struct mpd_connection *connection = mpdclient_get_connection(c);
916         if (connection == NULL)
917                 return false;
919         if (filelist_is_empty(fl))
920                 return true;
922         mpd_command_list_begin(connection, false);
924         for (unsigned i = 0; i < filelist_length(fl); ++i) {
925                 struct filelist_entry *entry = filelist_get(fl, i);
926                 struct mpd_entity *entity  = entry->entity;
928                 if (entity != NULL &&
929                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
930                         const struct mpd_song *song =
931                                 mpd_entity_get_song(entity);
933                         mpd_send_add(connection, mpd_song_get_uri(song));
934                 }
935         }
937         mpd_command_list_end(connection);
938         return mpdclient_finish_command(c);