Code

screen: make cols and rows local
[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 void
198 mpdclient_status_free(struct mpdclient *c)
200         if (c->status == NULL)
201                 return;
203         mpd_status_free(c->status);
204         c->status = NULL;
206         c->volume = -1;
207         c->playing = false;
210 void
211 mpdclient_disconnect(struct mpdclient *c)
213 #ifdef ENABLE_ASYNC_CONNECT
214         if (c->async_connect != NULL) {
215                 aconnect_cancel(c->async_connect);
216                 c->async_connect = NULL;
217         }
218 #endif
220         mpdclient_cancel_enter_idle(c);
222         if (c->source != NULL) {
223                 mpd_glib_free(c->source);
224                 c->source = NULL;
225                 c->idle = false;
226         }
228         if (c->connection) {
229                 mpd_connection_free(c->connection);
230                 ++c->connection_id;
231         }
232         c->connection = NULL;
234         mpdclient_status_free(c);
236         playlist_clear(&c->playlist);
238         if (c->song)
239                 c->song = NULL;
241         /* everything has changed after a disconnect */
242         c->events |= MPD_IDLE_ALL;
245 static bool
246 mpdclient_connected(struct mpdclient *c,
247                     struct mpd_connection *connection)
249         c->connection = connection;
251         if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
252                 mpdclient_invoke_error_callback1(c);
253                 mpdclient_disconnect(c);
254                 mpdclient_failed_callback();
255                 return false;
256         }
258 #ifdef ENABLE_ASYNC_CONNECT
259         if (c->timeout_ms > 0)
260                 mpd_connection_set_timeout(connection, c->timeout_ms);
261 #endif
263         /* send password */
264         if (c->password != NULL &&
265             !mpd_run_password(connection, c->password)) {
266                 mpdclient_invoke_error_callback1(c);
267                 mpdclient_disconnect(c);
268                 mpdclient_failed_callback();
269                 return false;
270         }
272         c->source = mpd_glib_new(connection,
273                                  mpdclient_gidle_callback, c);
274         mpdclient_schedule_enter_idle(c);
276         ++c->connection_id;
278         mpdclient_connected_callback();
279         return true;
282 #ifdef ENABLE_ASYNC_CONNECT
284 static void
285 mpdclient_connect_success(struct mpd_connection *connection, void *ctx)
287         struct mpdclient *c = ctx;
288         assert(c->async_connect != NULL);
289         c->async_connect = NULL;
291         mpdclient_connected(c, connection);
294 static void
295 mpdclient_connect_error(const char *message, void *ctx)
297         struct mpdclient *c = ctx;
298         assert(c->async_connect != NULL);
299         c->async_connect = NULL;
301         mpdclient_error_callback(message);
302         mpdclient_failed_callback();
305 static const struct aconnect_handler mpdclient_connect_handler = {
306         .success = mpdclient_connect_success,
307         .error = mpdclient_connect_error,
308 };
310 #endif
312 void
313 mpdclient_connect(struct mpdclient *c)
315         /* close any open connection */
316         mpdclient_disconnect(c);
318 #ifdef ENABLE_ASYNC_CONNECT
319         aconnect_start(&c->async_connect,
320                        mpd_settings_get_host(c->settings),
321                        mpd_settings_get_port(c->settings),
322                        &mpdclient_connect_handler, c);
323 #else
324         /* connect to MPD */
325         struct mpd_connection *connection =
326                 mpd_connection_new(c->host, c->port, c->timeout_ms);
327         if (connection == NULL)
328                 g_error("Out of memory");
330         mpdclient_connected(c, connection);
331 #endif
334 bool
335 mpdclient_update(struct mpdclient *c)
337         struct mpd_connection *connection = mpdclient_get_connection(c);
339         if (connection == NULL)
340                 return false;
342         /* free the old status */
343         mpdclient_status_free(c);
345         /* retrieve new status */
346         c->status = mpd_run_status(connection);
347         if (c->status == NULL)
348                 return mpdclient_handle_error(c);
350         c->volume = mpd_status_get_volume(c->status);
351         c->playing = mpd_status_get_state(c->status) == MPD_STATE_PLAY;
353         /* check if the playlist needs an update */
354         if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
355                 bool retval;
357                 if (!playlist_is_empty(&c->playlist))
358                         retval = mpdclient_playlist_update_changes(c);
359                 else
360                         retval = mpdclient_playlist_update(c);
361                 if (!retval)
362                         return false;
363         }
365         /* update the current song */
366         if (!c->song || mpd_status_get_song_id(c->status) >= 0) {
367                 c->song = playlist_get_song(&c->playlist,
368                                             mpd_status_get_song_pos(c->status));
369         }
371         return true;
374 struct mpd_connection *
375 mpdclient_get_connection(struct mpdclient *c)
377         if (c->source != NULL && c->idle) {
378                 c->idle = false;
379                 mpd_glib_leave(c->source);
381                 mpdclient_schedule_enter_idle(c);
382         }
384         return c->connection;
387 static struct mpd_status *
388 mpdclient_recv_status(struct mpdclient *c)
390         assert(c->connection != NULL);
392         struct mpd_status *status = mpd_recv_status(c->connection);
393         if (status == NULL) {
394                 mpdclient_handle_error(c);
395                 return NULL;
396         }
398         if (c->status != NULL)
399                 mpd_status_free(c->status);
400         return c->status = status;
403 /****************************************************************************/
404 /*** MPD Commands  **********************************************************/
405 /****************************************************************************/
407 bool
408 mpdclient_cmd_crop(struct mpdclient *c)
410         if (!mpdclient_is_playing(c))
411                 return false;
413         int length = mpd_status_get_queue_length(c->status);
414         int current = mpd_status_get_song_pos(c->status);
415         if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
416                 return true;
418         struct mpd_connection *connection = mpdclient_get_connection(c);
419         if (connection == NULL)
420                 return false;
422         mpd_command_list_begin(connection, false);
424         if (current < length - 1)
425                 mpd_send_delete_range(connection, current + 1, length);
426         if (current > 0)
427                 mpd_send_delete_range(connection, 0, current);
429         mpd_command_list_end(connection);
431         return mpdclient_finish_command(c);
434 bool
435 mpdclient_cmd_clear(struct mpdclient *c)
437         struct mpd_connection *connection = mpdclient_get_connection(c);
438         if (connection == NULL)
439                 return false;
441         /* send "clear" and "status" */
442         if (!mpd_command_list_begin(connection, false) ||
443             !mpd_send_clear(connection) ||
444             !mpd_send_status(connection) ||
445             !mpd_command_list_end(connection))
446                 return mpdclient_handle_error(c);
448         /* receive the new status, store it in the mpdclient struct */
450         struct mpd_status *status = mpdclient_recv_status(c);
451         if (status == NULL)
452                 return false;
454         if (!mpd_response_finish(connection))
455                 return mpdclient_handle_error(c);
457         /* update mpdclient.playlist */
459         if (mpd_status_get_queue_length(status) == 0) {
460                 /* after the "clear" command, the queue is really
461                    empty - this means we can clear it locally,
462                    reducing the UI latency */
463                 playlist_clear(&c->playlist);
464                 c->playlist.version = mpd_status_get_queue_version(status);
465                 c->song = NULL;
466         }
468         c->events |= MPD_IDLE_QUEUE;
469         return true;
472 bool
473 mpdclient_cmd_volume(struct mpdclient *c, gint value)
475         struct mpd_connection *connection = mpdclient_get_connection(c);
476         if (connection == NULL)
477                 return false;
479         mpd_send_set_volume(connection, value);
480         return mpdclient_finish_command(c);
483 bool
484 mpdclient_cmd_volume_up(struct mpdclient *c)
486         if (c->volume < 0 || c->volume >= 100)
487                 return true;
489         struct mpd_connection *connection = mpdclient_get_connection(c);
490         if (connection == NULL)
491                 return false;
493         return mpdclient_cmd_volume(c, ++c->volume);
496 bool
497 mpdclient_cmd_volume_down(struct mpdclient *c)
499         if (c->volume <= 0)
500                 return true;
502         struct mpd_connection *connection = mpdclient_get_connection(c);
503         if (connection == NULL)
504                 return false;
506         return mpdclient_cmd_volume(c, --c->volume);
509 bool
510 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
512         struct mpd_connection *connection = mpdclient_get_connection(c);
513         if (connection == NULL)
514                 return false;
516         return mpd_send_add(connection, path_utf8)?
517                 mpdclient_finish_command(c) : false;
520 bool
521 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
523         assert(c != NULL);
524         assert(song != NULL);
526         struct mpd_connection *connection = mpdclient_get_connection(c);
527         if (connection == NULL || c->status == NULL)
528                 return false;
530         /* send the add command to mpd; at the same time, get the new
531            status (to verify the new playlist id) and the last song
532            (we hope that's the song we just added) */
534         if (!mpd_command_list_begin(connection, true) ||
535             !mpd_send_add(connection, mpd_song_get_uri(song)) ||
536             !mpd_send_status(connection) ||
537             !mpd_send_get_queue_song_pos(connection,
538                                          playlist_length(&c->playlist)) ||
539             !mpd_command_list_end(connection) ||
540             !mpd_response_next(connection))
541                 return mpdclient_handle_error(c);
543         c->events |= MPD_IDLE_QUEUE;
545         struct mpd_status *status = mpdclient_recv_status(c);
546         if (status == NULL)
547                 return false;
549         if (!mpd_response_next(connection))
550                 return mpdclient_handle_error(c);
552         struct mpd_song *new_song = mpd_recv_song(connection);
553         if (!mpd_response_finish(connection) || new_song == NULL) {
554                 if (new_song != NULL)
555                         mpd_song_free(new_song);
557                 return mpd_connection_clear_error(connection) ||
558                         mpdclient_handle_error(c);
559         }
561         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
562             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
563                 /* the cheap route: match on the new playlist length
564                    and its version, we can keep our local playlist
565                    copy in sync */
566                 c->playlist.version = mpd_status_get_queue_version(status);
568                 /* the song we just received has the correct id;
569                    append it to the local playlist */
570                 playlist_append(&c->playlist, new_song);
571         }
573         mpd_song_free(new_song);
575         return true;
578 bool
579 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
581         struct mpd_connection *connection = mpdclient_get_connection(c);
583         if (connection == NULL || c->status == NULL)
584                 return false;
586         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
587                 return false;
589         const struct mpd_song *song = playlist_get(&c->playlist, idx);
591         /* send the delete command to mpd; at the same time, get the
592            new status (to verify the playlist id) */
594         if (!mpd_command_list_begin(connection, false) ||
595             !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
596             !mpd_send_status(connection) ||
597             !mpd_command_list_end(connection))
598                 return mpdclient_handle_error(c);
600         c->events |= MPD_IDLE_QUEUE;
602         struct mpd_status *status = mpdclient_recv_status(c);
603         if (status == NULL)
604                 return false;
606         if (!mpd_response_finish(connection))
607                 return mpdclient_handle_error(c);
609         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
610             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
611                 /* the cheap route: match on the new playlist length
612                    and its version, we can keep our local playlist
613                    copy in sync */
614                 c->playlist.version = mpd_status_get_queue_version(status);
616                 /* remove the song from the local playlist */
617                 playlist_remove(&c->playlist, idx);
619                 /* remove references to the song */
620                 if (c->song == song)
621                         c->song = NULL;
622         }
624         return true;
627 bool
628 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
630         if (end == start + 1)
631                 /* if that's not really a range, we choose to use the
632                    safer "deleteid" version */
633                 return mpdclient_cmd_delete(c, start);
635         struct mpd_connection *connection = mpdclient_get_connection(c);
636         if (connection == NULL)
637                 return false;
639         /* send the delete command to mpd; at the same time, get the
640            new status (to verify the playlist id) */
642         if (!mpd_command_list_begin(connection, false) ||
643             !mpd_send_delete_range(connection, start, end) ||
644             !mpd_send_status(connection) ||
645             !mpd_command_list_end(connection))
646                 return mpdclient_handle_error(c);
648         c->events |= MPD_IDLE_QUEUE;
650         struct mpd_status *status = mpdclient_recv_status(c);
651         if (status == NULL)
652                 return false;
654         if (!mpd_response_finish(connection))
655                 return mpdclient_handle_error(c);
657         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
658             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
659                 /* the cheap route: match on the new playlist length
660                    and its version, we can keep our local playlist
661                    copy in sync */
662                 c->playlist.version = mpd_status_get_queue_version(status);
664                 /* remove the song from the local playlist */
665                 while (end > start) {
666                         --end;
668                         /* remove references to the song */
669                         if (c->song == playlist_get(&c->playlist, end))
670                                 c->song = NULL;
672                         playlist_remove(&c->playlist, end);
673                 }
674         }
676         return true;
679 bool
680 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
682         if (dest_pos == src_pos)
683                 return true;
685         struct mpd_connection *connection = mpdclient_get_connection(c);
686         if (connection == NULL)
687                 return false;
689         /* send the "move" command to MPD; at the same time, get the
690            new status (to verify the playlist id) */
692         if (!mpd_command_list_begin(connection, false) ||
693             !mpd_send_move(connection, src_pos, dest_pos) ||
694             !mpd_send_status(connection) ||
695             !mpd_command_list_end(connection))
696                 return mpdclient_handle_error(c);
698         c->events |= MPD_IDLE_QUEUE;
700         struct mpd_status *status = mpdclient_recv_status(c);
701         if (status == NULL)
702                 return false;
704         if (!mpd_response_finish(connection))
705                 return mpdclient_handle_error(c);
707         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
708             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
709                 /* the cheap route: match on the new playlist length
710                    and its version, we can keep our local playlist
711                    copy in sync */
712                 c->playlist.version = mpd_status_get_queue_version(status);
714                 /* swap songs in the local playlist */
715                 playlist_move(&c->playlist, dest_pos, src_pos);
716         }
718         return true;
721 /* The client-to-client protocol (MPD 0.17.0) */
723 bool
724 mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
726         struct mpd_connection *connection = mpdclient_get_connection(c);
728         if (connection == NULL)
729                 return false;
731         if (!mpd_send_subscribe(connection, channel))
732                 return mpdclient_handle_error(c);
734         return mpdclient_finish_command(c);
737 bool
738 mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
740         struct mpd_connection *connection = mpdclient_get_connection(c);
741         if (connection == NULL)
742                 return false;
744         if (!mpd_send_unsubscribe(connection, channel))
745                 return mpdclient_handle_error(c);
747         return mpdclient_finish_command(c);
750 bool
751 mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
752                            const char *text)
754         struct mpd_connection *connection = mpdclient_get_connection(c);
755         if (connection == NULL)
756                 return false;
758         if (!mpd_send_send_message(connection, channel, text))
759                 return mpdclient_handle_error(c);
761         return mpdclient_finish_command(c);
764 bool
765 mpdclient_send_read_messages(struct mpdclient *c)
767         struct mpd_connection *connection = mpdclient_get_connection(c);
768         if (connection == NULL)
769                 return false;
771         return mpd_send_read_messages(connection)?
772                 true : mpdclient_handle_error(c);
775 struct mpd_message *
776 mpdclient_recv_message(struct mpdclient *c)
778         struct mpd_connection *connection = mpdclient_get_connection(c);
779         if (connection == NULL)
780                 return false;
782         struct mpd_message *message = mpd_recv_message(connection);
783         if (message == NULL &&
784             mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS)
785                 mpdclient_handle_error(c);
787         return message;
790 /****************************************************************************/
791 /*** Playlist management functions ******************************************/
792 /****************************************************************************/
794 /* update playlist */
795 bool
796 mpdclient_playlist_update(struct mpdclient *c)
798         struct mpd_connection *connection = mpdclient_get_connection(c);
799         if (connection == NULL)
800                 return false;
802         playlist_clear(&c->playlist);
804         mpd_send_list_queue_meta(connection);
806         struct mpd_entity *entity;
807         while ((entity = mpd_recv_entity(connection))) {
808                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
809                         playlist_append(&c->playlist, mpd_entity_get_song(entity));
811                 mpd_entity_free(entity);
812         }
814         c->playlist.version = mpd_status_get_queue_version(c->status);
815         c->song = NULL;
817         return mpdclient_finish_command(c);
820 /* update playlist (plchanges) */
821 bool
822 mpdclient_playlist_update_changes(struct mpdclient *c)
824         struct mpd_connection *connection = mpdclient_get_connection(c);
826         if (connection == NULL)
827                 return false;
829         mpd_send_queue_changes_meta(connection, c->playlist.version);
831         struct mpd_song *song;
832         while ((song = mpd_recv_song(connection)) != NULL) {
833                 int pos = mpd_song_get_pos(song);
835                 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
836                         /* update song */
837                         playlist_replace(&c->playlist, pos, song);
838                 } else {
839                         /* add a new song */
840                         playlist_append(&c->playlist, song);
841                 }
843                 mpd_song_free(song);
844         }
846         /* remove trailing songs */
848         unsigned length = mpd_status_get_queue_length(c->status);
849         while (length < c->playlist.list->len) {
850                 guint pos = c->playlist.list->len - 1;
852                 /* Remove the last playlist entry */
853                 playlist_remove(&c->playlist, pos);
854         }
856         c->song = NULL;
857         c->playlist.version = mpd_status_get_queue_version(c->status);
859         return mpdclient_finish_command(c);
863 /****************************************************************************/
864 /*** Filelist functions *****************************************************/
865 /****************************************************************************/
867 bool
868 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
870         struct mpd_connection *connection = mpdclient_get_connection(c);
871         if (connection == NULL)
872                 return false;
874         if (filelist_is_empty(fl))
875                 return true;
877         mpd_command_list_begin(connection, false);
879         for (unsigned i = 0; i < filelist_length(fl); ++i) {
880                 struct filelist_entry *entry = filelist_get(fl, i);
881                 struct mpd_entity *entity  = entry->entity;
883                 if (entity != NULL &&
884                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
885                         const struct mpd_song *song =
886                                 mpd_entity_get_song(entity);
888                         mpd_send_add(connection, mpd_song_get_uri(song));
889                 }
890         }
892         mpd_command_list_end(connection);
893         return mpdclient_finish_command(c);