Code

mpdclient: implement password for asynchronous connect
[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 #ifdef ENABLE_ASYNC_CONNECT
157 #ifndef WIN32
159 static bool
160 is_local_socket(const char *host)
162         return *host == '/' || *host == '@';
165 static bool
166 settings_is_local_socket(const struct mpd_settings *settings)
168         const char *host = mpd_settings_get_host(settings);
169         return host != NULL && is_local_socket(host);
172 #endif
173 #endif
175 struct mpdclient *
176 mpdclient_new(const gchar *host, unsigned port,
177               unsigned timeout_ms, const gchar *password)
179         struct mpdclient *c = g_new0(struct mpdclient, 1);
181 #ifdef ENABLE_ASYNC_CONNECT
182         c->settings = mpd_settings_new(host, port, timeout_ms,
183                                        NULL, NULL);
184         if (c->settings == NULL)
185                 g_error("Out of memory");
187 #ifndef WIN32
188         c->settings2 = host == NULL && port == 0 &&
189                 settings_is_local_socket(c->settings)
190                 ? mpd_settings_new(host, 6600, timeout_ms, NULL, NULL)
191                 : NULL;
192 #endif
194 #else
195         c->host = host;
196         c->port = port;
197 #endif
199         c->timeout_ms = timeout_ms;
200         c->password = password;
202         playlist_init(&c->playlist);
203         c->volume = -1;
204         c->events = 0;
205         c->playing = false;
207         return c;
210 void
211 mpdclient_free(struct mpdclient *c)
213         mpdclient_disconnect(c);
215         mpdclient_playlist_free(&c->playlist);
217 #ifdef ENABLE_ASYNC_CONNECT
218         mpd_settings_free(c->settings);
220 #ifndef WIN32
221         if (c->settings2 != NULL)
222                 mpd_settings_free(c->settings2);
223 #endif
224 #endif
226         g_free(c);
229 static char *
230 settings_name(const struct mpd_settings *settings)
232         assert(settings != NULL);
234         const char *host = mpd_settings_get_host(settings);
235         if (host == NULL)
236                 host = "unknown";
238         if (host[0] == '/')
239                 return g_strdup(host);
241         unsigned port = mpd_settings_get_port(settings);
242         if (port == 0 || port == 6600)
243                 return g_strdup(host);
245         return g_strdup_printf("%s:%u", host, port);
248 char *
249 mpdclient_settings_name(const struct mpdclient *c)
251         assert(c != NULL);
253 #ifdef ENABLE_ASYNC_CONNECT
254         return settings_name(c->settings);
255 #else
256         struct mpd_settings *settings =
257                 mpd_settings_new(c->host, c->port, 0, NULL, NULL);
258         if (settings == NULL)
259                 return g_strdup("unknown");
261         char *name = settings_name(settings);
262         mpd_settings_free(settings);
263         return name;
264 #endif
267 static void
268 mpdclient_status_free(struct mpdclient *c)
270         if (c->status == NULL)
271                 return;
273         mpd_status_free(c->status);
274         c->status = NULL;
276         c->volume = -1;
277         c->playing = false;
280 void
281 mpdclient_disconnect(struct mpdclient *c)
283 #ifdef ENABLE_ASYNC_CONNECT
284         if (c->async_connect != NULL) {
285                 aconnect_cancel(c->async_connect);
286                 c->async_connect = NULL;
287         }
288 #endif
290         mpdclient_cancel_enter_idle(c);
292         if (c->source != NULL) {
293                 mpd_glib_free(c->source);
294                 c->source = NULL;
295                 c->idle = false;
296         }
298         if (c->connection) {
299                 mpd_connection_free(c->connection);
300                 ++c->connection_id;
301         }
302         c->connection = NULL;
304         mpdclient_status_free(c);
306         playlist_clear(&c->playlist);
308         if (c->song)
309                 c->song = NULL;
311         /* everything has changed after a disconnect */
312         c->events |= MPD_IDLE_ALL;
315 static bool
316 mpdclient_connected(struct mpdclient *c,
317                     struct mpd_connection *connection)
319         c->connection = connection;
321         if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
322                 mpdclient_invoke_error_callback1(c);
323                 mpdclient_disconnect(c);
324                 mpdclient_failed_callback();
325                 return false;
326         }
328 #ifdef ENABLE_ASYNC_CONNECT
329         if (c->timeout_ms > 0)
330                 mpd_connection_set_timeout(connection, c->timeout_ms);
331 #endif
333         /* send password */
334         if (c->password != NULL &&
335             !mpd_run_password(connection, c->password)) {
336                 mpdclient_invoke_error_callback1(c);
337                 mpdclient_disconnect(c);
338                 mpdclient_failed_callback();
339                 return false;
340         }
342         c->source = mpd_glib_new(connection,
343                                  mpdclient_gidle_callback, c);
344         mpdclient_schedule_enter_idle(c);
346         ++c->connection_id;
348         mpdclient_connected_callback();
349         return true;
352 #ifdef ENABLE_ASYNC_CONNECT
354 static void
355 mpdclient_aconnect_start(struct mpdclient *c,
356                          const struct mpd_settings *settings);
358 static const struct mpd_settings *
359 mpdclient_get_settings(const struct mpdclient *c)
361 #ifndef WIN32
362         if (c->connecting2)
363                 return c->settings2;
364 #endif
366         return c->settings;
369 static void
370 mpdclient_connect_success(struct mpd_connection *connection, void *ctx)
372         struct mpdclient *c = ctx;
373         assert(c->async_connect != NULL);
374         c->async_connect = NULL;
376         const char *password =
377                 mpd_settings_get_password(mpdclient_get_settings(c));
378         if (password != NULL && !mpd_run_password(connection, password)) {
379                 mpdclient_error_callback(mpd_connection_get_error_message(connection));
380                 mpd_connection_free(connection);
381                 mpdclient_failed_callback();
382                 return;
383         }
385         mpdclient_connected(c, connection);
388 static void
389 mpdclient_connect_error(const char *message, void *ctx)
391         struct mpdclient *c = ctx;
392         assert(c->async_connect != NULL);
393         c->async_connect = NULL;
395 #ifndef WIN32
396         if (!c->connecting2 && c->settings2 != NULL) {
397                 c->connecting2 = true;
398                 mpdclient_aconnect_start(c, c->settings2);
399                 return;
400         }
401 #endif
403         mpdclient_error_callback(message);
404         mpdclient_failed_callback();
407 static const struct aconnect_handler mpdclient_connect_handler = {
408         .success = mpdclient_connect_success,
409         .error = mpdclient_connect_error,
410 };
412 static void
413 mpdclient_aconnect_start(struct mpdclient *c,
414                          const struct mpd_settings *settings)
416         aconnect_start(&c->async_connect,
417                        mpd_settings_get_host(settings),
418                        mpd_settings_get_port(settings),
419                        &mpdclient_connect_handler, c);
422 #endif
424 void
425 mpdclient_connect(struct mpdclient *c)
427         /* close any open connection */
428         mpdclient_disconnect(c);
430 #ifdef ENABLE_ASYNC_CONNECT
431 #ifndef WIN32
432         c->connecting2 = false;
433 #endif
434         mpdclient_aconnect_start(c, c->settings);
435 #else
436         /* connect to MPD */
437         struct mpd_connection *connection =
438                 mpd_connection_new(c->host, c->port, c->timeout_ms);
439         if (connection == NULL)
440                 g_error("Out of memory");
442         mpdclient_connected(c, connection);
443 #endif
446 bool
447 mpdclient_update(struct mpdclient *c)
449         struct mpd_connection *connection = mpdclient_get_connection(c);
451         if (connection == NULL)
452                 return false;
454         /* free the old status */
455         mpdclient_status_free(c);
457         /* retrieve new status */
458         c->status = mpd_run_status(connection);
459         if (c->status == NULL)
460                 return mpdclient_handle_error(c);
462         c->volume = mpd_status_get_volume(c->status);
463         c->playing = mpd_status_get_state(c->status) == MPD_STATE_PLAY;
465         /* check if the playlist needs an update */
466         if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
467                 bool retval;
469                 if (!playlist_is_empty(&c->playlist))
470                         retval = mpdclient_playlist_update_changes(c);
471                 else
472                         retval = mpdclient_playlist_update(c);
473                 if (!retval)
474                         return false;
475         }
477         /* update the current song */
478         if (!c->song || mpd_status_get_song_id(c->status) >= 0) {
479                 c->song = playlist_get_song(&c->playlist,
480                                             mpd_status_get_song_pos(c->status));
481         }
483         return true;
486 struct mpd_connection *
487 mpdclient_get_connection(struct mpdclient *c)
489         if (c->source != NULL && c->idle) {
490                 c->idle = false;
491                 mpd_glib_leave(c->source);
493                 mpdclient_schedule_enter_idle(c);
494         }
496         return c->connection;
499 static struct mpd_status *
500 mpdclient_recv_status(struct mpdclient *c)
502         assert(c->connection != NULL);
504         struct mpd_status *status = mpd_recv_status(c->connection);
505         if (status == NULL) {
506                 mpdclient_handle_error(c);
507                 return NULL;
508         }
510         if (c->status != NULL)
511                 mpd_status_free(c->status);
512         return c->status = status;
515 /****************************************************************************/
516 /*** MPD Commands  **********************************************************/
517 /****************************************************************************/
519 bool
520 mpdclient_cmd_crop(struct mpdclient *c)
522         if (!mpdclient_is_playing(c))
523                 return false;
525         int length = mpd_status_get_queue_length(c->status);
526         int current = mpd_status_get_song_pos(c->status);
527         if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
528                 return true;
530         struct mpd_connection *connection = mpdclient_get_connection(c);
531         if (connection == NULL)
532                 return false;
534         mpd_command_list_begin(connection, false);
536         if (current < length - 1)
537                 mpd_send_delete_range(connection, current + 1, length);
538         if (current > 0)
539                 mpd_send_delete_range(connection, 0, current);
541         mpd_command_list_end(connection);
543         return mpdclient_finish_command(c);
546 bool
547 mpdclient_cmd_clear(struct mpdclient *c)
549         struct mpd_connection *connection = mpdclient_get_connection(c);
550         if (connection == NULL)
551                 return false;
553         /* send "clear" and "status" */
554         if (!mpd_command_list_begin(connection, false) ||
555             !mpd_send_clear(connection) ||
556             !mpd_send_status(connection) ||
557             !mpd_command_list_end(connection))
558                 return mpdclient_handle_error(c);
560         /* receive the new status, store it in the mpdclient struct */
562         struct mpd_status *status = mpdclient_recv_status(c);
563         if (status == NULL)
564                 return false;
566         if (!mpd_response_finish(connection))
567                 return mpdclient_handle_error(c);
569         /* update mpdclient.playlist */
571         if (mpd_status_get_queue_length(status) == 0) {
572                 /* after the "clear" command, the queue is really
573                    empty - this means we can clear it locally,
574                    reducing the UI latency */
575                 playlist_clear(&c->playlist);
576                 c->playlist.version = mpd_status_get_queue_version(status);
577                 c->song = NULL;
578         }
580         c->events |= MPD_IDLE_QUEUE;
581         return true;
584 bool
585 mpdclient_cmd_volume(struct mpdclient *c, gint value)
587         struct mpd_connection *connection = mpdclient_get_connection(c);
588         if (connection == NULL)
589                 return false;
591         mpd_send_set_volume(connection, value);
592         return mpdclient_finish_command(c);
595 bool
596 mpdclient_cmd_volume_up(struct mpdclient *c)
598         if (c->volume < 0 || c->volume >= 100)
599                 return true;
601         struct mpd_connection *connection = mpdclient_get_connection(c);
602         if (connection == NULL)
603                 return false;
605         return mpdclient_cmd_volume(c, ++c->volume);
608 bool
609 mpdclient_cmd_volume_down(struct mpdclient *c)
611         if (c->volume <= 0)
612                 return true;
614         struct mpd_connection *connection = mpdclient_get_connection(c);
615         if (connection == NULL)
616                 return false;
618         return mpdclient_cmd_volume(c, --c->volume);
621 bool
622 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
624         struct mpd_connection *connection = mpdclient_get_connection(c);
625         if (connection == NULL)
626                 return false;
628         return mpd_send_add(connection, path_utf8)?
629                 mpdclient_finish_command(c) : false;
632 bool
633 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
635         assert(c != NULL);
636         assert(song != NULL);
638         struct mpd_connection *connection = mpdclient_get_connection(c);
639         if (connection == NULL || c->status == NULL)
640                 return false;
642         /* send the add command to mpd; at the same time, get the new
643            status (to verify the new playlist id) and the last song
644            (we hope that's the song we just added) */
646         if (!mpd_command_list_begin(connection, true) ||
647             !mpd_send_add(connection, mpd_song_get_uri(song)) ||
648             !mpd_send_status(connection) ||
649             !mpd_send_get_queue_song_pos(connection,
650                                          playlist_length(&c->playlist)) ||
651             !mpd_command_list_end(connection) ||
652             !mpd_response_next(connection))
653                 return mpdclient_handle_error(c);
655         c->events |= MPD_IDLE_QUEUE;
657         struct mpd_status *status = mpdclient_recv_status(c);
658         if (status == NULL)
659                 return false;
661         if (!mpd_response_next(connection))
662                 return mpdclient_handle_error(c);
664         struct mpd_song *new_song = mpd_recv_song(connection);
665         if (!mpd_response_finish(connection) || new_song == NULL) {
666                 if (new_song != NULL)
667                         mpd_song_free(new_song);
669                 return mpd_connection_clear_error(connection) ||
670                         mpdclient_handle_error(c);
671         }
673         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
674             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
675                 /* the cheap route: match on the new playlist length
676                    and its version, we can keep our local playlist
677                    copy in sync */
678                 c->playlist.version = mpd_status_get_queue_version(status);
680                 /* the song we just received has the correct id;
681                    append it to the local playlist */
682                 playlist_append(&c->playlist, new_song);
683         }
685         mpd_song_free(new_song);
687         return true;
690 bool
691 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
693         struct mpd_connection *connection = mpdclient_get_connection(c);
695         if (connection == NULL || c->status == NULL)
696                 return false;
698         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
699                 return false;
701         const struct mpd_song *song = playlist_get(&c->playlist, idx);
703         /* send the delete command to mpd; at the same time, get the
704            new status (to verify the playlist id) */
706         if (!mpd_command_list_begin(connection, false) ||
707             !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
708             !mpd_send_status(connection) ||
709             !mpd_command_list_end(connection))
710                 return mpdclient_handle_error(c);
712         c->events |= MPD_IDLE_QUEUE;
714         struct mpd_status *status = mpdclient_recv_status(c);
715         if (status == NULL)
716                 return false;
718         if (!mpd_response_finish(connection))
719                 return mpdclient_handle_error(c);
721         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
722             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
723                 /* the cheap route: match on the new playlist length
724                    and its version, we can keep our local playlist
725                    copy in sync */
726                 c->playlist.version = mpd_status_get_queue_version(status);
728                 /* remove the song from the local playlist */
729                 playlist_remove(&c->playlist, idx);
731                 /* remove references to the song */
732                 if (c->song == song)
733                         c->song = NULL;
734         }
736         return true;
739 bool
740 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
742         if (end == start + 1)
743                 /* if that's not really a range, we choose to use the
744                    safer "deleteid" version */
745                 return mpdclient_cmd_delete(c, start);
747         struct mpd_connection *connection = mpdclient_get_connection(c);
748         if (connection == NULL)
749                 return false;
751         /* send the delete command to mpd; at the same time, get the
752            new status (to verify the playlist id) */
754         if (!mpd_command_list_begin(connection, false) ||
755             !mpd_send_delete_range(connection, start, end) ||
756             !mpd_send_status(connection) ||
757             !mpd_command_list_end(connection))
758                 return mpdclient_handle_error(c);
760         c->events |= MPD_IDLE_QUEUE;
762         struct mpd_status *status = mpdclient_recv_status(c);
763         if (status == NULL)
764                 return false;
766         if (!mpd_response_finish(connection))
767                 return mpdclient_handle_error(c);
769         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
770             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
771                 /* the cheap route: match on the new playlist length
772                    and its version, we can keep our local playlist
773                    copy in sync */
774                 c->playlist.version = mpd_status_get_queue_version(status);
776                 /* remove the song from the local playlist */
777                 while (end > start) {
778                         --end;
780                         /* remove references to the song */
781                         if (c->song == playlist_get(&c->playlist, end))
782                                 c->song = NULL;
784                         playlist_remove(&c->playlist, end);
785                 }
786         }
788         return true;
791 bool
792 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
794         if (dest_pos == src_pos)
795                 return true;
797         struct mpd_connection *connection = mpdclient_get_connection(c);
798         if (connection == NULL)
799                 return false;
801         /* send the "move" command to MPD; at the same time, get the
802            new status (to verify the playlist id) */
804         if (!mpd_command_list_begin(connection, false) ||
805             !mpd_send_move(connection, src_pos, dest_pos) ||
806             !mpd_send_status(connection) ||
807             !mpd_command_list_end(connection))
808                 return mpdclient_handle_error(c);
810         c->events |= MPD_IDLE_QUEUE;
812         struct mpd_status *status = mpdclient_recv_status(c);
813         if (status == NULL)
814                 return false;
816         if (!mpd_response_finish(connection))
817                 return mpdclient_handle_error(c);
819         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
820             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
821                 /* the cheap route: match on the new playlist length
822                    and its version, we can keep our local playlist
823                    copy in sync */
824                 c->playlist.version = mpd_status_get_queue_version(status);
826                 /* swap songs in the local playlist */
827                 playlist_move(&c->playlist, dest_pos, src_pos);
828         }
830         return true;
833 /* The client-to-client protocol (MPD 0.17.0) */
835 bool
836 mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
838         struct mpd_connection *connection = mpdclient_get_connection(c);
840         if (connection == NULL)
841                 return false;
843         if (!mpd_send_subscribe(connection, channel))
844                 return mpdclient_handle_error(c);
846         return mpdclient_finish_command(c);
849 bool
850 mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
852         struct mpd_connection *connection = mpdclient_get_connection(c);
853         if (connection == NULL)
854                 return false;
856         if (!mpd_send_unsubscribe(connection, channel))
857                 return mpdclient_handle_error(c);
859         return mpdclient_finish_command(c);
862 bool
863 mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
864                            const char *text)
866         struct mpd_connection *connection = mpdclient_get_connection(c);
867         if (connection == NULL)
868                 return false;
870         if (!mpd_send_send_message(connection, channel, text))
871                 return mpdclient_handle_error(c);
873         return mpdclient_finish_command(c);
876 bool
877 mpdclient_send_read_messages(struct mpdclient *c)
879         struct mpd_connection *connection = mpdclient_get_connection(c);
880         if (connection == NULL)
881                 return false;
883         return mpd_send_read_messages(connection)?
884                 true : mpdclient_handle_error(c);
887 struct mpd_message *
888 mpdclient_recv_message(struct mpdclient *c)
890         struct mpd_connection *connection = mpdclient_get_connection(c);
891         if (connection == NULL)
892                 return false;
894         struct mpd_message *message = mpd_recv_message(connection);
895         if (message == NULL &&
896             mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS)
897                 mpdclient_handle_error(c);
899         return message;
902 /****************************************************************************/
903 /*** Playlist management functions ******************************************/
904 /****************************************************************************/
906 /* update playlist */
907 bool
908 mpdclient_playlist_update(struct mpdclient *c)
910         struct mpd_connection *connection = mpdclient_get_connection(c);
911         if (connection == NULL)
912                 return false;
914         playlist_clear(&c->playlist);
916         mpd_send_list_queue_meta(connection);
918         struct mpd_entity *entity;
919         while ((entity = mpd_recv_entity(connection))) {
920                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
921                         playlist_append(&c->playlist, mpd_entity_get_song(entity));
923                 mpd_entity_free(entity);
924         }
926         c->playlist.version = mpd_status_get_queue_version(c->status);
927         c->song = NULL;
929         return mpdclient_finish_command(c);
932 /* update playlist (plchanges) */
933 bool
934 mpdclient_playlist_update_changes(struct mpdclient *c)
936         struct mpd_connection *connection = mpdclient_get_connection(c);
938         if (connection == NULL)
939                 return false;
941         mpd_send_queue_changes_meta(connection, c->playlist.version);
943         struct mpd_song *song;
944         while ((song = mpd_recv_song(connection)) != NULL) {
945                 int pos = mpd_song_get_pos(song);
947                 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
948                         /* update song */
949                         playlist_replace(&c->playlist, pos, song);
950                 } else {
951                         /* add a new song */
952                         playlist_append(&c->playlist, song);
953                 }
955                 mpd_song_free(song);
956         }
958         /* remove trailing songs */
960         unsigned length = mpd_status_get_queue_length(c->status);
961         while (length < c->playlist.list->len) {
962                 guint pos = c->playlist.list->len - 1;
964                 /* Remove the last playlist entry */
965                 playlist_remove(&c->playlist, pos);
966         }
968         c->song = NULL;
969         c->playlist.version = mpd_status_get_queue_version(c->status);
971         return mpdclient_finish_command(c);
975 /****************************************************************************/
976 /*** Filelist functions *****************************************************/
977 /****************************************************************************/
979 bool
980 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
982         struct mpd_connection *connection = mpdclient_get_connection(c);
983         if (connection == NULL)
984                 return false;
986         if (filelist_is_empty(fl))
987                 return true;
989         mpd_command_list_begin(connection, false);
991         for (unsigned i = 0; i < filelist_length(fl); ++i) {
992                 struct filelist_entry *entry = filelist_get(fl, i);
993                 struct mpd_entity *entity  = entry->entity;
995                 if (entity != NULL &&
996                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
997                         const struct mpd_song *song =
998                                 mpd_entity_get_song(entity);
1000                         mpd_send_add(connection, mpd_song_get_uri(song));
1001                 }
1002         }
1004         mpd_command_list_end(connection);
1005         return mpdclient_finish_command(c);