4114a311c238d58d067313bdf22305151feaf8a6
[ncmpc-debian.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  */
19
20 #include "mpdclient.h"
21 #include "callbacks.h"
22 #include "filelist.h"
23 #include "config.h"
24 #include "gidle.h"
25 #include "charset.h"
26
27 #ifdef ENABLE_ASYNC_CONNECT
28 #include "aconnect.h"
29 #endif
30
31 #include <mpd/client.h>
32
33 #include <assert.h>
34
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);
42
43         c->enter_idle_source_id = 0;
44         c->idle = mpd_glib_enter(c->source);
45         return false;
46 }
47
48 static void
49 mpdclient_schedule_enter_idle(struct mpdclient *c)
50 {
51         assert(c != NULL);
52         assert(c->source != NULL);
53
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 }
59
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 }
68
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;
79
80         mpdclient_error_callback(message);
81         g_free(allocated);
82 }
83
84 static void
85 mpdclient_invoke_error_callback1(struct mpdclient *c)
86 {
87         assert(c != NULL);
88         assert(c->connection != NULL);
89
90         struct mpd_connection *connection = c->connection;
91
92         enum mpd_error error = mpd_connection_get_error(connection);
93         assert(error != MPD_ERROR_SUCCESS);
94
95         mpdclient_invoke_error_callback(error,
96                                         mpd_connection_get_error_message(connection));
97 }
98
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)
104 {
105         struct mpdclient *c = ctx;
106
107         c->idle = false;
108
109         assert(mpdclient_is_connected(c));
110
111         if (error != MPD_ERROR_SUCCESS) {
112                 mpdclient_invoke_error_callback(error, message);
113                 mpdclient_disconnect(c);
114                 mpdclient_lost_callback();
115                 return;
116         }
117
118         c->events |= events;
119         mpdclient_update(c);
120
121         mpdclient_idle_callback(c->events);
122
123         c->events = 0;
124
125         if (c->source != NULL)
126                 mpdclient_schedule_enter_idle(c);
127 }
128
129 /****************************************************************************/
130 /*** mpdclient functions ****************************************************/
131 /****************************************************************************/
132
133 bool
134 mpdclient_handle_error(struct mpdclient *c)
135 {
136         enum mpd_error error = mpd_connection_get_error(c->connection);
137
138         assert(error != MPD_ERROR_SUCCESS);
139
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;
144
145         mpdclient_invoke_error_callback(error,
146                                         mpd_connection_get_error_message(c->connection));
147
148         if (!mpd_connection_clear_error(c->connection)) {
149                 mpdclient_disconnect(c);
150                 mpdclient_lost_callback();
151         }
152
153         return false;
154 }
155
156 #ifdef ENABLE_ASYNC_CONNECT
157 #ifndef WIN32
158
159 static bool
160 is_local_socket(const char *host)
161 {
162         return *host == '/' || *host == '@';
163 }
164
165 static bool
166 settings_is_local_socket(const struct mpd_settings *settings)
167 {
168         const char *host = mpd_settings_get_host(settings);
169         return host != NULL && is_local_socket(host);
170 }
171
172 #endif
173 #endif
174
175 struct mpdclient *
176 mpdclient_new(const gchar *host, unsigned port,
177               unsigned timeout_ms, const gchar *password)
178 {
179         struct mpdclient *c = g_new0(struct mpdclient, 1);
180
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");
186
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
193
194 #else
195         c->host = host;
196         c->port = port;
197 #endif
198
199         c->timeout_ms = timeout_ms;
200         c->password = password;
201
202         playlist_init(&c->playlist);
203         c->volume = -1;
204         c->events = 0;
205         c->playing = false;
206
207         return c;
208 }
209
210 void
211 mpdclient_free(struct mpdclient *c)
212 {
213         mpdclient_disconnect(c);
214
215         mpdclient_playlist_free(&c->playlist);
216
217 #ifdef ENABLE_ASYNC_CONNECT
218         mpd_settings_free(c->settings);
219
220 #ifndef WIN32
221         if (c->settings2 != NULL)
222                 mpd_settings_free(c->settings2);
223 #endif
224 #endif
225
226         g_free(c);
227 }
228
229 static char *
230 settings_name(const struct mpd_settings *settings)
231 {
232         assert(settings != NULL);
233
234         const char *host = mpd_settings_get_host(settings);
235         if (host == NULL)
236                 host = "unknown";
237
238         if (host[0] == '/')
239                 return g_strdup(host);
240
241         unsigned port = mpd_settings_get_port(settings);
242         if (port == 0 || port == 6600)
243                 return g_strdup(host);
244
245         return g_strdup_printf("%s:%u", host, port);
246 }
247
248 char *
249 mpdclient_settings_name(const struct mpdclient *c)
250 {
251         assert(c != NULL);
252
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");
260
261         char *name = settings_name(settings);
262         mpd_settings_free(settings);
263         return name;
264 #endif
265 }
266
267 static void
268 mpdclient_status_free(struct mpdclient *c)
269 {
270         if (c->status == NULL)
271                 return;
272
273         mpd_status_free(c->status);
274         c->status = NULL;
275
276         c->volume = -1;
277         c->playing = false;
278 }
279
280 void
281 mpdclient_disconnect(struct mpdclient *c)
282 {
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
289
290         mpdclient_cancel_enter_idle(c);
291
292         if (c->source != NULL) {
293                 mpd_glib_free(c->source);
294                 c->source = NULL;
295                 c->idle = false;
296         }
297
298         if (c->connection) {
299                 mpd_connection_free(c->connection);
300                 ++c->connection_id;
301         }
302         c->connection = NULL;
303
304         mpdclient_status_free(c);
305
306         playlist_clear(&c->playlist);
307
308         if (c->song)
309                 c->song = NULL;
310
311         /* everything has changed after a disconnect */
312         c->events |= MPD_IDLE_ALL;
313 }
314
315 static bool
316 mpdclient_connected(struct mpdclient *c,
317                     struct mpd_connection *connection)
318 {
319         c->connection = connection;
320
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         }
327
328 #ifdef ENABLE_ASYNC_CONNECT
329         if (c->timeout_ms > 0)
330                 mpd_connection_set_timeout(connection, c->timeout_ms);
331 #endif
332
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         }
341
342         c->source = mpd_glib_new(connection,
343                                  mpdclient_gidle_callback, c);
344         mpdclient_schedule_enter_idle(c);
345
346         ++c->connection_id;
347
348         mpdclient_connected_callback();
349         return true;
350 }
351
352 #ifdef ENABLE_ASYNC_CONNECT
353
354 static void
355 mpdclient_aconnect_start(struct mpdclient *c,
356                          const struct mpd_settings *settings);
357
358 static const struct mpd_settings *
359 mpdclient_get_settings(const struct mpdclient *c)
360 {
361 #ifndef WIN32
362         if (c->connecting2)
363                 return c->settings2;
364 #endif
365
366         return c->settings;
367 }
368
369 static void
370 mpdclient_connect_success(struct mpd_connection *connection, void *ctx)
371 {
372         struct mpdclient *c = ctx;
373         assert(c->async_connect != NULL);
374         c->async_connect = NULL;
375
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         }
384
385         mpdclient_connected(c, connection);
386 }
387
388 static void
389 mpdclient_connect_error(const char *message, void *ctx)
390 {
391         struct mpdclient *c = ctx;
392         assert(c->async_connect != NULL);
393         c->async_connect = NULL;
394
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
402
403         mpdclient_error_callback(message);
404         mpdclient_failed_callback();
405 }
406
407 static const struct aconnect_handler mpdclient_connect_handler = {
408         .success = mpdclient_connect_success,
409         .error = mpdclient_connect_error,
410 };
411
412 static void
413 mpdclient_aconnect_start(struct mpdclient *c,
414                          const struct mpd_settings *settings)
415 {
416         aconnect_start(&c->async_connect,
417                        mpd_settings_get_host(settings),
418                        mpd_settings_get_port(settings),
419                        &mpdclient_connect_handler, c);
420 }
421
422 #endif
423
424 void
425 mpdclient_connect(struct mpdclient *c)
426 {
427         /* close any open connection */
428         mpdclient_disconnect(c);
429
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");
441
442         mpdclient_connected(c, connection);
443 #endif
444 }
445
446 bool
447 mpdclient_update(struct mpdclient *c)
448 {
449         struct mpd_connection *connection = mpdclient_get_connection(c);
450
451         if (connection == NULL)
452                 return false;
453
454         /* free the old status */
455         mpdclient_status_free(c);
456
457         /* retrieve new status */
458         c->status = mpd_run_status(connection);
459         if (c->status == NULL)
460                 return mpdclient_handle_error(c);
461
462         c->volume = mpd_status_get_volume(c->status);
463         c->playing = mpd_status_get_state(c->status) == MPD_STATE_PLAY;
464
465         /* check if the playlist needs an update */
466         if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
467                 bool retval;
468
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         }
476
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         }
482
483         return true;
484 }
485
486 struct mpd_connection *
487 mpdclient_get_connection(struct mpdclient *c)
488 {
489         if (c->source != NULL && c->idle) {
490                 c->idle = false;
491                 mpd_glib_leave(c->source);
492
493                 if (c->source != NULL)
494                         mpdclient_schedule_enter_idle(c);
495         }
496
497         return c->connection;
498 }
499
500 static struct mpd_status *
501 mpdclient_recv_status(struct mpdclient *c)
502 {
503         assert(c->connection != NULL);
504
505         struct mpd_status *status = mpd_recv_status(c->connection);
506         if (status == NULL) {
507                 mpdclient_handle_error(c);
508                 return NULL;
509         }
510
511         if (c->status != NULL)
512                 mpd_status_free(c->status);
513         return c->status = status;
514 }
515
516 /****************************************************************************/
517 /*** MPD Commands  **********************************************************/
518 /****************************************************************************/
519
520 bool
521 mpdclient_cmd_crop(struct mpdclient *c)
522 {
523         if (!mpdclient_is_playing(c))
524                 return false;
525
526         int length = mpd_status_get_queue_length(c->status);
527         int current = mpd_status_get_song_pos(c->status);
528         if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
529                 return true;
530
531         struct mpd_connection *connection = mpdclient_get_connection(c);
532         if (connection == NULL)
533                 return false;
534
535         mpd_command_list_begin(connection, false);
536
537         if (current < length - 1)
538                 mpd_send_delete_range(connection, current + 1, length);
539         if (current > 0)
540                 mpd_send_delete_range(connection, 0, current);
541
542         mpd_command_list_end(connection);
543
544         return mpdclient_finish_command(c);
545 }
546
547 bool
548 mpdclient_cmd_clear(struct mpdclient *c)
549 {
550         struct mpd_connection *connection = mpdclient_get_connection(c);
551         if (connection == NULL)
552                 return false;
553
554         /* send "clear" and "status" */
555         if (!mpd_command_list_begin(connection, false) ||
556             !mpd_send_clear(connection) ||
557             !mpd_send_status(connection) ||
558             !mpd_command_list_end(connection))
559                 return mpdclient_handle_error(c);
560
561         /* receive the new status, store it in the mpdclient struct */
562
563         struct mpd_status *status = mpdclient_recv_status(c);
564         if (status == NULL)
565                 return false;
566
567         if (!mpd_response_finish(connection))
568                 return mpdclient_handle_error(c);
569
570         /* update mpdclient.playlist */
571
572         if (mpd_status_get_queue_length(status) == 0) {
573                 /* after the "clear" command, the queue is really
574                    empty - this means we can clear it locally,
575                    reducing the UI latency */
576                 playlist_clear(&c->playlist);
577                 c->playlist.version = mpd_status_get_queue_version(status);
578                 c->song = NULL;
579         }
580
581         c->events |= MPD_IDLE_QUEUE;
582         return true;
583 }
584
585 bool
586 mpdclient_cmd_volume(struct mpdclient *c, gint value)
587 {
588         struct mpd_connection *connection = mpdclient_get_connection(c);
589         if (connection == NULL)
590                 return false;
591
592         mpd_send_set_volume(connection, value);
593         return mpdclient_finish_command(c);
594 }
595
596 bool
597 mpdclient_cmd_volume_up(struct mpdclient *c)
598 {
599         if (c->volume < 0 || c->volume >= 100)
600                 return true;
601
602         struct mpd_connection *connection = mpdclient_get_connection(c);
603         if (connection == NULL)
604                 return false;
605
606         return mpdclient_cmd_volume(c, ++c->volume);
607 }
608
609 bool
610 mpdclient_cmd_volume_down(struct mpdclient *c)
611 {
612         if (c->volume <= 0)
613                 return true;
614
615         struct mpd_connection *connection = mpdclient_get_connection(c);
616         if (connection == NULL)
617                 return false;
618
619         return mpdclient_cmd_volume(c, --c->volume);
620 }
621
622 bool
623 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
624 {
625         struct mpd_connection *connection = mpdclient_get_connection(c);
626         if (connection == NULL)
627                 return false;
628
629         return mpd_send_add(connection, path_utf8)?
630                 mpdclient_finish_command(c) : false;
631 }
632
633 bool
634 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
635 {
636         assert(c != NULL);
637         assert(song != NULL);
638
639         struct mpd_connection *connection = mpdclient_get_connection(c);
640         if (connection == NULL || c->status == NULL)
641                 return false;
642
643         /* send the add command to mpd; at the same time, get the new
644            status (to verify the new playlist id) and the last song
645            (we hope that's the song we just added) */
646
647         if (!mpd_command_list_begin(connection, true) ||
648             !mpd_send_add(connection, mpd_song_get_uri(song)) ||
649             !mpd_send_status(connection) ||
650             !mpd_send_get_queue_song_pos(connection,
651                                          playlist_length(&c->playlist)) ||
652             !mpd_command_list_end(connection) ||
653             !mpd_response_next(connection))
654                 return mpdclient_handle_error(c);
655
656         c->events |= MPD_IDLE_QUEUE;
657
658         struct mpd_status *status = mpdclient_recv_status(c);
659         if (status == NULL)
660                 return false;
661
662         if (!mpd_response_next(connection))
663                 return mpdclient_handle_error(c);
664
665         struct mpd_song *new_song = mpd_recv_song(connection);
666         if (!mpd_response_finish(connection) || new_song == NULL) {
667                 if (new_song != NULL)
668                         mpd_song_free(new_song);
669
670                 return mpd_connection_clear_error(connection) ||
671                         mpdclient_handle_error(c);
672         }
673
674         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
675             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
676                 /* the cheap route: match on the new playlist length
677                    and its version, we can keep our local playlist
678                    copy in sync */
679                 c->playlist.version = mpd_status_get_queue_version(status);
680
681                 /* the song we just received has the correct id;
682                    append it to the local playlist */
683                 playlist_append(&c->playlist, new_song);
684         }
685
686         mpd_song_free(new_song);
687
688         return true;
689 }
690
691 bool
692 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
693 {
694         struct mpd_connection *connection = mpdclient_get_connection(c);
695
696         if (connection == NULL || c->status == NULL)
697                 return false;
698
699         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
700                 return false;
701
702         const struct mpd_song *song = playlist_get(&c->playlist, idx);
703
704         /* send the delete command to mpd; at the same time, get the
705            new status (to verify the playlist id) */
706
707         if (!mpd_command_list_begin(connection, false) ||
708             !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
709             !mpd_send_status(connection) ||
710             !mpd_command_list_end(connection))
711                 return mpdclient_handle_error(c);
712
713         c->events |= MPD_IDLE_QUEUE;
714
715         struct mpd_status *status = mpdclient_recv_status(c);
716         if (status == NULL)
717                 return false;
718
719         if (!mpd_response_finish(connection))
720                 return mpdclient_handle_error(c);
721
722         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
723             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
724                 /* the cheap route: match on the new playlist length
725                    and its version, we can keep our local playlist
726                    copy in sync */
727                 c->playlist.version = mpd_status_get_queue_version(status);
728
729                 /* remove the song from the local playlist */
730                 playlist_remove(&c->playlist, idx);
731
732                 /* remove references to the song */
733                 if (c->song == song)
734                         c->song = NULL;
735         }
736
737         return true;
738 }
739
740 bool
741 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
742 {
743         if (end == start + 1)
744                 /* if that's not really a range, we choose to use the
745                    safer "deleteid" version */
746                 return mpdclient_cmd_delete(c, start);
747
748         struct mpd_connection *connection = mpdclient_get_connection(c);
749         if (connection == NULL)
750                 return false;
751
752         /* send the delete command to mpd; at the same time, get the
753            new status (to verify the playlist id) */
754
755         if (!mpd_command_list_begin(connection, false) ||
756             !mpd_send_delete_range(connection, start, end) ||
757             !mpd_send_status(connection) ||
758             !mpd_command_list_end(connection))
759                 return mpdclient_handle_error(c);
760
761         c->events |= MPD_IDLE_QUEUE;
762
763         struct mpd_status *status = mpdclient_recv_status(c);
764         if (status == NULL)
765                 return false;
766
767         if (!mpd_response_finish(connection))
768                 return mpdclient_handle_error(c);
769
770         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
771             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
772                 /* the cheap route: match on the new playlist length
773                    and its version, we can keep our local playlist
774                    copy in sync */
775                 c->playlist.version = mpd_status_get_queue_version(status);
776
777                 /* remove the song from the local playlist */
778                 while (end > start) {
779                         --end;
780
781                         /* remove references to the song */
782                         if (c->song == playlist_get(&c->playlist, end))
783                                 c->song = NULL;
784
785                         playlist_remove(&c->playlist, end);
786                 }
787         }
788
789         return true;
790 }
791
792 bool
793 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
794 {
795         if (dest_pos == src_pos)
796                 return true;
797
798         struct mpd_connection *connection = mpdclient_get_connection(c);
799         if (connection == NULL)
800                 return false;
801
802         /* send the "move" command to MPD; at the same time, get the
803            new status (to verify the playlist id) */
804
805         if (!mpd_command_list_begin(connection, false) ||
806             !mpd_send_move(connection, src_pos, dest_pos) ||
807             !mpd_send_status(connection) ||
808             !mpd_command_list_end(connection))
809                 return mpdclient_handle_error(c);
810
811         c->events |= MPD_IDLE_QUEUE;
812
813         struct mpd_status *status = mpdclient_recv_status(c);
814         if (status == NULL)
815                 return false;
816
817         if (!mpd_response_finish(connection))
818                 return mpdclient_handle_error(c);
819
820         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
821             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
822                 /* the cheap route: match on the new playlist length
823                    and its version, we can keep our local playlist
824                    copy in sync */
825                 c->playlist.version = mpd_status_get_queue_version(status);
826
827                 /* swap songs in the local playlist */
828                 playlist_move(&c->playlist, dest_pos, src_pos);
829         }
830
831         return true;
832 }
833
834 /* The client-to-client protocol (MPD 0.17.0) */
835
836 bool
837 mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
838 {
839         struct mpd_connection *connection = mpdclient_get_connection(c);
840
841         if (connection == NULL)
842                 return false;
843
844         if (!mpd_send_subscribe(connection, channel))
845                 return mpdclient_handle_error(c);
846
847         return mpdclient_finish_command(c);
848 }
849
850 bool
851 mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
852 {
853         struct mpd_connection *connection = mpdclient_get_connection(c);
854         if (connection == NULL)
855                 return false;
856
857         if (!mpd_send_unsubscribe(connection, channel))
858                 return mpdclient_handle_error(c);
859
860         return mpdclient_finish_command(c);
861 }
862
863 bool
864 mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
865                            const char *text)
866 {
867         struct mpd_connection *connection = mpdclient_get_connection(c);
868         if (connection == NULL)
869                 return false;
870
871         if (!mpd_send_send_message(connection, channel, text))
872                 return mpdclient_handle_error(c);
873
874         return mpdclient_finish_command(c);
875 }
876
877 bool
878 mpdclient_send_read_messages(struct mpdclient *c)
879 {
880         struct mpd_connection *connection = mpdclient_get_connection(c);
881         if (connection == NULL)
882                 return false;
883
884         return mpd_send_read_messages(connection)?
885                 true : mpdclient_handle_error(c);
886 }
887
888 struct mpd_message *
889 mpdclient_recv_message(struct mpdclient *c)
890 {
891         struct mpd_connection *connection = mpdclient_get_connection(c);
892         if (connection == NULL)
893                 return NULL;
894
895         struct mpd_message *message = mpd_recv_message(connection);
896         if (message == NULL &&
897             mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS)
898                 mpdclient_handle_error(c);
899
900         return message;
901 }
902
903 /****************************************************************************/
904 /*** Playlist management functions ******************************************/
905 /****************************************************************************/
906
907 /* update playlist */
908 bool
909 mpdclient_playlist_update(struct mpdclient *c)
910 {
911         struct mpd_connection *connection = mpdclient_get_connection(c);
912         if (connection == NULL)
913                 return false;
914
915         playlist_clear(&c->playlist);
916
917         mpd_send_list_queue_meta(connection);
918
919         struct mpd_entity *entity;
920         while ((entity = mpd_recv_entity(connection))) {
921                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
922                         playlist_append(&c->playlist, mpd_entity_get_song(entity));
923
924                 mpd_entity_free(entity);
925         }
926
927         c->playlist.version = mpd_status_get_queue_version(c->status);
928         c->song = NULL;
929
930         return mpdclient_finish_command(c);
931 }
932
933 /* update playlist (plchanges) */
934 bool
935 mpdclient_playlist_update_changes(struct mpdclient *c)
936 {
937         struct mpd_connection *connection = mpdclient_get_connection(c);
938
939         if (connection == NULL)
940                 return false;
941
942         mpd_send_queue_changes_meta(connection, c->playlist.version);
943
944         struct mpd_song *song;
945         while ((song = mpd_recv_song(connection)) != NULL) {
946                 int pos = mpd_song_get_pos(song);
947
948                 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
949                         /* update song */
950                         playlist_replace(&c->playlist, pos, song);
951                 } else {
952                         /* add a new song */
953                         playlist_append(&c->playlist, song);
954                 }
955
956                 mpd_song_free(song);
957         }
958
959         /* remove trailing songs */
960
961         unsigned length = mpd_status_get_queue_length(c->status);
962         while (length < c->playlist.list->len) {
963                 guint pos = c->playlist.list->len - 1;
964
965                 /* Remove the last playlist entry */
966                 playlist_remove(&c->playlist, pos);
967         }
968
969         c->song = NULL;
970         c->playlist.version = mpd_status_get_queue_version(c->status);
971
972         return mpdclient_finish_command(c);
973 }
974
975
976 /****************************************************************************/
977 /*** Filelist functions *****************************************************/
978 /****************************************************************************/
979
980 bool
981 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
982 {
983         struct mpd_connection *connection = mpdclient_get_connection(c);
984         if (connection == NULL)
985                 return false;
986
987         if (filelist_is_empty(fl))
988                 return true;
989
990         mpd_command_list_begin(connection, false);
991
992         for (unsigned i = 0; i < filelist_length(fl); ++i) {
993                 struct filelist_entry *entry = filelist_get(fl, i);
994                 struct mpd_entity *entity  = entry->entity;
995
996                 if (entity != NULL &&
997                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
998                         const struct mpd_song *song =
999                                 mpd_entity_get_song(entity);
1000
1001                         mpd_send_add(connection, mpd_song_get_uri(song));
1002                 }
1003         }
1004
1005         mpd_command_list_end(connection);
1006         return mpdclient_finish_command(c);
1007 }