Code

mpdclient: convert UTF-8 error message to locale charset
[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 #include <mpd/client.h>
29 #include <assert.h>
31 static void
32 mpdclient_invoke_error_callback(enum mpd_error error,
33                                 const char *message)
34 {
35         char *allocated;
36         if (error == MPD_ERROR_SERVER)
37                 /* server errors are UTF-8, the others are locale */
38                 message = allocated = utf8_to_locale(message);
39         else
40                 allocated = NULL;
42         mpdclient_error_callback(message);
43         g_free(allocated);
44 }
46 /****************************************************************************/
47 /*** mpdclient functions ****************************************************/
48 /****************************************************************************/
50 bool
51 mpdclient_handle_error(struct mpdclient *c)
52 {
53         enum mpd_error error = mpd_connection_get_error(c->connection);
55         assert(error != MPD_ERROR_SUCCESS);
57         if (error == MPD_ERROR_SERVER &&
58             mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_PERMISSION &&
59             mpdclient_auth_callback(c))
60                 return true;
62         mpdclient_invoke_error_callback(error,
63                                         mpd_connection_get_error_message(c->connection));
65         if (!mpd_connection_clear_error(c->connection))
66                 mpdclient_disconnect(c);
68         return false;
69 }
71 struct mpdclient *
72 mpdclient_new(void)
73 {
74         struct mpdclient *c = g_new0(struct mpdclient, 1);
75         playlist_init(&c->playlist);
76         c->volume = -1;
77         c->events = 0;
79         return c;
80 }
82 void
83 mpdclient_free(struct mpdclient *c)
84 {
85         mpdclient_disconnect(c);
87         mpdclient_playlist_free(&c->playlist);
89         g_free(c);
90 }
92 void
93 mpdclient_disconnect(struct mpdclient *c)
94 {
95         if (c->source != NULL) {
96                 mpd_glib_free(c->source);
97                 c->source = NULL;
98                 c->idle = false;
99         }
101         if (c->connection) {
102                 mpd_connection_free(c->connection);
103                 ++c->connection_id;
104         }
105         c->connection = NULL;
107         if (c->status)
108                 mpd_status_free(c->status);
109         c->status = NULL;
111         playlist_clear(&c->playlist);
113         if (c->song)
114                 c->song = NULL;
116         /* everything has changed after a disconnect */
117         c->events |= MPD_IDLE_ALL;
120 bool
121 mpdclient_connect(struct mpdclient *c,
122                   const gchar *host,
123                   gint port,
124                   unsigned timeout_ms,
125                   const gchar *password)
127         /* close any open connection */
128         if (c->connection)
129                 mpdclient_disconnect(c);
131         /* connect to MPD */
132         c->connection = mpd_connection_new(host, port, timeout_ms);
133         if (c->connection == NULL)
134                 g_error("Out of memory");
136         if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) {
137                 mpdclient_handle_error(c);
138                 mpdclient_disconnect(c);
139                 return false;
140         }
142         /* send password */
143         if (password != NULL && !mpd_run_password(c->connection, password)) {
144                 mpdclient_handle_error(c);
145                 mpdclient_disconnect(c);
146                 return false;
147         }
149         c->source = mpd_glib_new(c->connection,
150                                  mpdclient_idle_callback, c);
152         ++c->connection_id;
154         return true;
157 bool
158 mpdclient_update(struct mpdclient *c)
160         struct mpd_connection *connection = mpdclient_get_connection(c);
162         c->volume = -1;
164         if (connection == NULL)
165                 return false;
167         /* free the old status */
168         if (c->status)
169                 mpd_status_free(c->status);
171         /* retrieve new status */
172         c->status = mpd_run_status(connection);
173         if (c->status == NULL)
174                 return mpdclient_handle_error(c);
176         c->update_id = mpd_status_get_update_id(c->status);
178         c->volume = mpd_status_get_volume(c->status);
180         /* check if the playlist needs an update */
181         if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
182                 bool retval;
184                 if (!playlist_is_empty(&c->playlist))
185                         retval = mpdclient_playlist_update_changes(c);
186                 else
187                         retval = mpdclient_playlist_update(c);
188                 if (!retval)
189                         return false;
190         }
192         /* update the current song */
193         if (!c->song || mpd_status_get_song_id(c->status) >= 0) {
194                 c->song = playlist_get_song(&c->playlist,
195                                             mpd_status_get_song_pos(c->status));
196         }
198         return true;
201 struct mpd_connection *
202 mpdclient_get_connection(struct mpdclient *c)
204         if (c->source != NULL && c->idle) {
205                 c->idle = false;
206                 mpd_glib_leave(c->source);
207         }
209         return c->connection;
212 void
213 mpdclient_put_connection(struct mpdclient *c)
215         assert(c->source == NULL || c->connection != NULL);
217         if (c->source != NULL && !c->idle) {
218                 c->idle = mpd_glib_enter(c->source);
219         }
222 static struct mpd_status *
223 mpdclient_recv_status(struct mpdclient *c)
225         assert(c->connection != NULL);
227         struct mpd_status *status = mpd_recv_status(c->connection);
228         if (status == NULL) {
229                 mpdclient_handle_error(c);
230                 return NULL;
231         }
233         if (c->status != NULL)
234                 mpd_status_free(c->status);
235         return c->status = status;
238 /****************************************************************************/
239 /*** MPD Commands  **********************************************************/
240 /****************************************************************************/
242 bool
243 mpdclient_cmd_crop(struct mpdclient *c)
245         if (!mpdclient_is_playing(c))
246                 return false;
248         int length = mpd_status_get_queue_length(c->status);
249         int current = mpd_status_get_song_pos(c->status);
250         if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
251                 return true;
253         struct mpd_connection *connection = mpdclient_get_connection(c);
254         if (connection == NULL)
255                 return false;
257         mpd_command_list_begin(connection, false);
259         if (current < length - 1)
260                 mpd_send_delete_range(connection, current + 1, length);
261         if (current > 0)
262                 mpd_send_delete_range(connection, 0, current);
264         mpd_command_list_end(connection);
266         return mpdclient_finish_command(c);
269 bool
270 mpdclient_cmd_clear(struct mpdclient *c)
272         struct mpd_connection *connection = mpdclient_get_connection(c);
273         if (connection == NULL)
274                 return false;
276         /* send "clear" and "status" */
277         if (!mpd_command_list_begin(connection, false) ||
278             !mpd_send_clear(connection) ||
279             !mpd_send_status(connection) ||
280             !mpd_command_list_end(connection))
281                 return mpdclient_handle_error(c);
283         /* receive the new status, store it in the mpdclient struct */
285         struct mpd_status *status = mpdclient_recv_status(c);
286         if (status == NULL)
287                 return false;
289         if (!mpd_response_finish(connection))
290                 return mpdclient_handle_error(c);
292         /* update mpdclient.playlist */
294         if (mpd_status_get_queue_length(status) == 0) {
295                 /* after the "clear" command, the queue is really
296                    empty - this means we can clear it locally,
297                    reducing the UI latency */
298                 playlist_clear(&c->playlist);
299                 c->playlist.version = mpd_status_get_queue_version(status);
300                 c->song = NULL;
301         }
303         c->events |= MPD_IDLE_QUEUE;
304         return true;
307 bool
308 mpdclient_cmd_volume(struct mpdclient *c, gint value)
310         struct mpd_connection *connection = mpdclient_get_connection(c);
311         if (connection == NULL)
312                 return false;
314         mpd_send_set_volume(connection, value);
315         return mpdclient_finish_command(c);
318 bool
319 mpdclient_cmd_volume_up(struct mpdclient *c)
321         struct mpd_connection *connection = mpdclient_get_connection(c);
322         if (connection == NULL)
323                 return false;
325         if (c->status == NULL ||
326             mpd_status_get_volume(c->status) == -1)
327                 return true;
329         if (c->volume < 0)
330                 c->volume = mpd_status_get_volume(c->status);
332         if (c->volume >= 100)
333                 return true;
335         return mpdclient_cmd_volume(c, ++c->volume);
338 bool
339 mpdclient_cmd_volume_down(struct mpdclient *c)
341         struct mpd_connection *connection = mpdclient_get_connection(c);
342         if (connection == NULL)
343                 return false;
345         if (c->status == NULL || mpd_status_get_volume(c->status) < 0)
346                 return true;
348         if (c->volume < 0)
349                 c->volume = mpd_status_get_volume(c->status);
351         if (c->volume <= 0)
352                 return true;
354         return mpdclient_cmd_volume(c, --c->volume);
357 bool
358 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
360         struct mpd_connection *connection = mpdclient_get_connection(c);
361         if (connection == NULL)
362                 return false;
364         return mpd_send_add(connection, path_utf8)?
365                 mpdclient_finish_command(c) : false;
368 bool
369 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
371         assert(c != NULL);
372         assert(song != NULL);
374         struct mpd_connection *connection = mpdclient_get_connection(c);
375         if (connection == NULL || c->status == NULL)
376                 return false;
378         /* send the add command to mpd; at the same time, get the new
379            status (to verify the new playlist id) and the last song
380            (we hope that's the song we just added) */
382         if (!mpd_command_list_begin(connection, true) ||
383             !mpd_send_add(connection, mpd_song_get_uri(song)) ||
384             !mpd_send_status(connection) ||
385             !mpd_send_get_queue_song_pos(connection,
386                                          playlist_length(&c->playlist)) ||
387             !mpd_command_list_end(connection) ||
388             !mpd_response_next(connection))
389                 return mpdclient_handle_error(c);
391         c->events |= MPD_IDLE_QUEUE;
393         struct mpd_status *status = mpdclient_recv_status(c);
394         if (status == NULL)
395                 return false;
397         if (!mpd_response_next(connection))
398                 return mpdclient_handle_error(c);
400         struct mpd_song *new_song = mpd_recv_song(connection);
401         if (!mpd_response_finish(connection) || new_song == NULL) {
402                 if (new_song != NULL)
403                         mpd_song_free(new_song);
405                 return mpd_connection_clear_error(connection) ||
406                         mpdclient_handle_error(c);
407         }
409         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
410             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
411                 /* the cheap route: match on the new playlist length
412                    and its version, we can keep our local playlist
413                    copy in sync */
414                 c->playlist.version = mpd_status_get_queue_version(status);
416                 /* the song we just received has the correct id;
417                    append it to the local playlist */
418                 playlist_append(&c->playlist, new_song);
419         }
421         mpd_song_free(new_song);
423         return true;
426 bool
427 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
429         struct mpd_connection *connection = mpdclient_get_connection(c);
431         if (connection == NULL || c->status == NULL)
432                 return false;
434         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
435                 return false;
437         const struct mpd_song *song = playlist_get(&c->playlist, idx);
439         /* send the delete command to mpd; at the same time, get the
440            new status (to verify the playlist id) */
442         if (!mpd_command_list_begin(connection, false) ||
443             !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
444             !mpd_send_status(connection) ||
445             !mpd_command_list_end(connection))
446                 return mpdclient_handle_error(c);
448         c->events |= MPD_IDLE_QUEUE;
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         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
458             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
459                 /* the cheap route: match on the new playlist length
460                    and its version, we can keep our local playlist
461                    copy in sync */
462                 c->playlist.version = mpd_status_get_queue_version(status);
464                 /* remove the song from the local playlist */
465                 playlist_remove(&c->playlist, idx);
467                 /* remove references to the song */
468                 if (c->song == song)
469                         c->song = NULL;
470         }
472         return true;
475 bool
476 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
478         if (end == start + 1)
479                 /* if that's not really a range, we choose to use the
480                    safer "deleteid" version */
481                 return mpdclient_cmd_delete(c, start);
483         struct mpd_connection *connection = mpdclient_get_connection(c);
484         if (connection == NULL)
485                 return false;
487         /* send the delete command to mpd; at the same time, get the
488            new status (to verify the playlist id) */
490         if (!mpd_command_list_begin(connection, false) ||
491             !mpd_send_delete_range(connection, start, end) ||
492             !mpd_send_status(connection) ||
493             !mpd_command_list_end(connection))
494                 return mpdclient_handle_error(c);
496         c->events |= MPD_IDLE_QUEUE;
498         struct mpd_status *status = mpdclient_recv_status(c);
499         if (status == NULL)
500                 return false;
502         if (!mpd_response_finish(connection))
503                 return mpdclient_handle_error(c);
505         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
506             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
507                 /* the cheap route: match on the new playlist length
508                    and its version, we can keep our local playlist
509                    copy in sync */
510                 c->playlist.version = mpd_status_get_queue_version(status);
512                 /* remove the song from the local playlist */
513                 while (end > start) {
514                         --end;
516                         /* remove references to the song */
517                         if (c->song == playlist_get(&c->playlist, end))
518                                 c->song = NULL;
520                         playlist_remove(&c->playlist, end);
521                 }
522         }
524         return true;
527 bool
528 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
530         if (dest_pos == src_pos)
531                 return true;
533         struct mpd_connection *connection = mpdclient_get_connection(c);
534         if (connection == NULL)
535                 return false;
537         /* send the "move" command to MPD; at the same time, get the
538            new status (to verify the playlist id) */
540         if (!mpd_command_list_begin(connection, false) ||
541             !mpd_send_move(connection, src_pos, dest_pos) ||
542             !mpd_send_status(connection) ||
543             !mpd_command_list_end(connection))
544                 return mpdclient_handle_error(c);
546         c->events |= MPD_IDLE_QUEUE;
548         struct mpd_status *status = mpdclient_recv_status(c);
549         if (status == NULL)
550                 return false;
552         if (!mpd_response_finish(connection))
553                 return mpdclient_handle_error(c);
555         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
556             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
557                 /* the cheap route: match on the new playlist length
558                    and its version, we can keep our local playlist
559                    copy in sync */
560                 c->playlist.version = mpd_status_get_queue_version(status);
562                 /* swap songs in the local playlist */
563                 playlist_move(&c->playlist, dest_pos, src_pos);
564         }
566         return true;
569 /* The client-to-client protocol (MPD 0.17.0) */
571 bool
572 mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
574         struct mpd_connection *connection = mpdclient_get_connection(c);
576         if (connection == NULL)
577                 return false;
579         if (!mpd_send_subscribe(connection, channel))
580                 return mpdclient_handle_error(c);
582         return mpdclient_finish_command(c);
585 bool
586 mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
588         struct mpd_connection *connection = mpdclient_get_connection(c);
589         if (connection == NULL)
590                 return false;
592         if (!mpd_send_unsubscribe(connection, channel))
593                 return mpdclient_handle_error(c);
595         return mpdclient_finish_command(c);
598 bool
599 mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
600                            const char *text)
602         struct mpd_connection *connection = mpdclient_get_connection(c);
603         if (connection == NULL)
604                 return false;
606         if (!mpd_send_send_message(connection, channel, text))
607                 return mpdclient_handle_error(c);
609         return mpdclient_finish_command(c);
612 bool
613 mpdclient_send_read_messages(struct mpdclient *c)
615         struct mpd_connection *connection = mpdclient_get_connection(c);
616         if (connection == NULL)
617                 return false;
619         return mpd_send_read_messages(connection)?
620                 true : mpdclient_handle_error(c);
623 struct mpd_message *
624 mpdclient_recv_message(struct mpdclient *c)
626         struct mpd_connection *connection = mpdclient_get_connection(c);
627         if (connection == NULL)
628                 return false;
630         struct mpd_message *message = mpd_recv_message(connection);
631         if (message == NULL &&
632             mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS)
633                 mpdclient_handle_error(c);
635         return message;
638 /****************************************************************************/
639 /*** Playlist management functions ******************************************/
640 /****************************************************************************/
642 /* update playlist */
643 bool
644 mpdclient_playlist_update(struct mpdclient *c)
646         struct mpd_connection *connection = mpdclient_get_connection(c);
647         if (connection == NULL)
648                 return false;
650         playlist_clear(&c->playlist);
652         mpd_send_list_queue_meta(connection);
654         struct mpd_entity *entity;
655         while ((entity = mpd_recv_entity(connection))) {
656                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
657                         playlist_append(&c->playlist, mpd_entity_get_song(entity));
659                 mpd_entity_free(entity);
660         }
662         c->playlist.version = mpd_status_get_queue_version(c->status);
663         c->song = NULL;
665         return mpdclient_finish_command(c);
668 /* update playlist (plchanges) */
669 bool
670 mpdclient_playlist_update_changes(struct mpdclient *c)
672         struct mpd_connection *connection = mpdclient_get_connection(c);
674         if (connection == NULL)
675                 return false;
677         mpd_send_queue_changes_meta(connection, c->playlist.version);
679         struct mpd_song *song;
680         while ((song = mpd_recv_song(connection)) != NULL) {
681                 int pos = mpd_song_get_pos(song);
683                 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
684                         /* update song */
685                         playlist_replace(&c->playlist, pos, song);
686                 } else {
687                         /* add a new song */
688                         playlist_append(&c->playlist, song);
689                 }
691                 mpd_song_free(song);
692         }
694         /* remove trailing songs */
696         unsigned length = mpd_status_get_queue_length(c->status);
697         while (length < c->playlist.list->len) {
698                 guint pos = c->playlist.list->len - 1;
700                 /* Remove the last playlist entry */
701                 playlist_remove(&c->playlist, pos);
702         }
704         c->song = NULL;
705         c->playlist.version = mpd_status_get_queue_version(c->status);
707         return mpdclient_finish_command(c);
711 /****************************************************************************/
712 /*** Filelist functions *****************************************************/
713 /****************************************************************************/
715 bool
716 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
718         struct mpd_connection *connection = mpdclient_get_connection(c);
719         if (connection == NULL)
720                 return false;
722         if (filelist_is_empty(fl))
723                 return true;
725         mpd_command_list_begin(connection, false);
727         for (unsigned i = 0; i < filelist_length(fl); ++i) {
728                 struct filelist_entry *entry = filelist_get(fl, i);
729                 struct mpd_entity *entity  = entry->entity;
731                 if (entity != NULL &&
732                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
733                         const struct mpd_song *song =
734                                 mpd_entity_get_song(entity);
736                         mpd_send_add(connection, mpd_song_get_uri(song));
737                 }
738         }
740         mpd_command_list_end(connection);
741         return mpdclient_finish_command(c);