Code

mpdclient, screen: don't use MPD_STATUS_NO_VOLUME
[ncmpc.git] / src / mpdclient.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
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.
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.
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 "filelist.h"
22 #include "screen_client.h"
23 #include "config.h"
24 #include "options.h"
25 #include "strfsong.h"
26 #include "utils.h"
28 #include <mpd/client.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <time.h>
33 #include <string.h>
35 #undef  ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD /* broken with song id's */
36 #define ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
37 #define ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
39 #define BUFSIZE 1024
41 static bool
42 MPD_ERROR(const struct mpdclient *client)
43 {
44         return client->connection == NULL ||
45                 mpd_connection_get_error(client->connection) != MPD_ERROR_SUCCESS;
46 }
48 /* filelist sorting functions */
49 static gint
50 compare_filelistentry(gconstpointer filelist_entry1,
51                           gconstpointer filelist_entry2)
52 {
53         const struct mpd_entity *e1, *e2;
54         int n = 0;
56         e1 = ((const struct filelist_entry *)filelist_entry1)->entity;
57         e2 = ((const struct filelist_entry *)filelist_entry2)->entity;
59         if (e1 != NULL && e2 != NULL &&
60             mpd_entity_get_type(e1) == mpd_entity_get_type(e2)) {
61                 switch (mpd_entity_get_type(e1)) {
62                 case MPD_ENTITY_TYPE_UNKNOWN:
63                         break;
64                 case MPD_ENTITY_TYPE_DIRECTORY:
65                         n = g_utf8_collate(mpd_directory_get_path(mpd_entity_get_directory(e1)),
66                                            mpd_directory_get_path(mpd_entity_get_directory(e2)));
67                         break;
68                 case MPD_ENTITY_TYPE_SONG:
69                         break;
70                 case MPD_ENTITY_TYPE_PLAYLIST:
71                         n = g_utf8_collate(mpd_playlist_get_path(mpd_entity_get_playlist(e1)),
72                                            mpd_playlist_get_path(mpd_entity_get_playlist(e2)));
73                 }
74         }
75         return n;
76 }
78 /* sort by list-format */
79 gint
80 compare_filelistentry_format(gconstpointer filelist_entry1,
81                              gconstpointer filelist_entry2)
82 {
83         const struct mpd_entity *e1, *e2;
84         char key1[BUFSIZE], key2[BUFSIZE];
85         int n = 0;
87         e1 = ((const struct filelist_entry *)filelist_entry1)->entity;
88         e2 = ((const struct filelist_entry *)filelist_entry2)->entity;
90         if (e1 && e2 &&
91             mpd_entity_get_type(e1) == MPD_ENTITY_TYPE_SONG &&
92             mpd_entity_get_type(e2) == MPD_ENTITY_TYPE_SONG) {
93                 strfsong(key1, BUFSIZE, options.list_format, mpd_entity_get_song(e1));
94                 strfsong(key2, BUFSIZE, options.list_format, mpd_entity_get_song(e2));
95                 n = strcmp(key1,key2);
96         }
98         return n;
99 }
102 /****************************************************************************/
103 /*** mpdclient functions ****************************************************/
104 /****************************************************************************/
106 gint
107 mpdclient_handle_error(struct mpdclient *c)
109         enum mpd_error error = mpd_connection_get_error(c->connection);
111         assert(error != MPD_ERROR_SUCCESS);
113         if (error == MPD_ERROR_SERVER &&
114             mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_PERMISSION &&
115             screen_auth(c))
116                 return 0;
118         if (error == MPD_ERROR_SERVER)
119                 error = error | (mpd_connection_get_server_error(c->connection) << 8);
121         for (GList *list = c->error_callbacks; list != NULL;
122              list = list->next) {
123                 mpdc_error_cb_t cb = list->data;
124                 cb(c, error, mpd_connection_get_error_message(c->connection));
125         }
127         if (!mpd_connection_clear_error(c->connection))
128                 mpdclient_disconnect(c);
130         return error;
133 static gint
134 mpdclient_finish_command(struct mpdclient *c)
136         return mpd_response_finish(c->connection)
137                 ? 0 : mpdclient_handle_error(c);
140 struct mpdclient *
141 mpdclient_new(void)
143         struct mpdclient *c;
145         c = g_new0(struct mpdclient, 1);
146         playlist_init(&c->playlist);
147         c->volume = -1;
149         return c;
152 void
153 mpdclient_free(struct mpdclient *c)
155         mpdclient_disconnect(c);
157         mpdclient_playlist_free(&c->playlist);
159         g_list_free(c->error_callbacks);
160         g_list_free(c->playlist_callbacks);
161         g_list_free(c->browse_callbacks);
162         g_free(c);
165 void
166 mpdclient_disconnect(struct mpdclient *c)
168         if (c->connection)
169                 mpd_connection_free(c->connection);
170         c->connection = NULL;
172         if (c->status)
173                 mpd_status_free(c->status);
174         c->status = NULL;
176         playlist_clear(&c->playlist);
178         if (c->song)
179                 c->song = NULL;
182 bool
183 mpdclient_connect(struct mpdclient *c,
184                   const gchar *host,
185                   gint port,
186                   gfloat _timeout,
187                   const gchar *password)
189         /* close any open connection */
190         if( c->connection )
191                 mpdclient_disconnect(c);
193         /* connect to MPD */
194         c->connection = mpd_connection_new(host, port, _timeout * 1000);
195         if (c->connection == NULL)
196                 g_error("Out of memory");
198         if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) {
199                 mpdclient_handle_error(c);
200                 mpdclient_disconnect(c);
201                 return false;
202         }
204         /* send password */
205         if (password != NULL && !mpd_run_password(c->connection, password)) {
206                 mpdclient_handle_error(c);
207                 mpdclient_disconnect(c);
208                 return false;
209         }
211         return true;
214 bool
215 mpdclient_update(struct mpdclient *c)
217         bool retval;
219         c->volume = -1;
221         if (MPD_ERROR(c))
222                 return false;
224         /* free the old status */
225         if (c->status)
226                 mpd_status_free(c->status);
228         /* retrieve new status */
229         c->status = mpd_run_status(c->connection);
230         if (c->status == NULL)
231                 return mpdclient_handle_error(c) == 0;
233         if (c->updatingdb &&
234             c->updatingdb != mpd_status_get_update_id(c->status))
235                 mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
237         c->updatingdb = mpd_status_get_update_id(c->status);
238         c->volume = mpd_status_get_volume(c->status);
240         /* check if the playlist needs an update */
241         if (c->playlist.id != mpd_status_get_queue_version(c->status)) {
242                 if (!playlist_is_empty(&c->playlist))
243                         retval = mpdclient_playlist_update_changes(c);
244                 else
245                         retval = mpdclient_playlist_update(c);
246         }
248         /* update the current song */
249         if (!c->song || mpd_status_get_song_id(c->status)) {
250                 c->song = playlist_get_song(&c->playlist,
251                                             mpd_status_get_song_pos(c->status));
252         }
254         return retval;
258 /****************************************************************************/
259 /*** MPD Commands  **********************************************************/
260 /****************************************************************************/
262 gint
263 mpdclient_cmd_play(struct mpdclient *c, gint idx)
265         const struct mpd_song *song = playlist_get_song(&c->playlist, idx);
267         if (MPD_ERROR(c))
268                 return -1;
270         if (song)
271                 mpd_send_play_id(c->connection, mpd_song_get_id(song));
272         else
273                 mpd_send_play(c->connection);
275         return mpdclient_finish_command(c);
278 gint
279 mpdclient_cmd_pause(struct mpdclient *c, gint value)
281         if (MPD_ERROR(c))
282                 return -1;
284         mpd_send_pause(c->connection, value);
285         return mpdclient_finish_command(c);
288 gint
289 mpdclient_cmd_crop(struct mpdclient *c)
291         struct mpd_status *status;
292         bool playing;
293         int length, current;
295         if (MPD_ERROR(c))
296                 return -1;
298         status = mpd_run_status(c->connection);
299         if (status == NULL)
300                 return mpdclient_handle_error(c);
302         playing = mpd_status_get_state(status) == MPD_STATE_PLAY ||
303                 mpd_status_get_state(status) == MPD_STATE_PAUSE;
304         length = mpd_status_get_queue_length(status);
305         current = mpd_status_get_song_pos(status);
307         mpd_status_free(status);
309         if (!playing || length < 2)
310                 return 0;
312         mpd_command_list_begin(c->connection, false);
314         while (--length >= 0)
315                 if (length != current)
316                         mpd_send_delete(c->connection, length);
318         mpd_command_list_end(c->connection);
320         return mpdclient_finish_command(c);
323 gint
324 mpdclient_cmd_stop(struct mpdclient *c)
326         if (MPD_ERROR(c))
327                 return -1;
329         mpd_send_stop(c->connection);
330         return mpdclient_finish_command(c);
333 gint
334 mpdclient_cmd_next(struct mpdclient *c)
336         if (MPD_ERROR(c))
337                 return -1;
339         mpd_send_next(c->connection);
340         return mpdclient_finish_command(c);
343 gint
344 mpdclient_cmd_prev(struct mpdclient *c)
346         if (MPD_ERROR(c))
347                 return -1;
349         mpd_send_previous(c->connection);
350         return mpdclient_finish_command(c);
353 gint
354 mpdclient_cmd_seek(struct mpdclient *c, gint id, gint pos)
356         if (MPD_ERROR(c))
357                 return -1;
359         mpd_send_seek_id(c->connection, id, pos);
360         return mpdclient_finish_command(c);
363 gint
364 mpdclient_cmd_shuffle(struct mpdclient *c)
366         if (MPD_ERROR(c))
367                 return -1;
369         mpd_send_shuffle(c->connection);
370         return mpdclient_finish_command(c);
373 gint
374 mpdclient_cmd_shuffle_range(struct mpdclient *c, guint start, guint end)
376         mpd_send_shuffle_range(c->connection, start, end);
377         return mpdclient_finish_command(c);
380 gint
381 mpdclient_cmd_clear(struct mpdclient *c)
383         gint retval = 0;
385         if (MPD_ERROR(c))
386                 return -1;
388         mpd_send_clear(c->connection);
389         retval = mpdclient_finish_command(c);
390         /* call playlist updated callback */
391         mpdclient_playlist_callback(c, PLAYLIST_EVENT_CLEAR, NULL);
392         return retval;
395 gint
396 mpdclient_cmd_repeat(struct mpdclient *c, gint value)
398         if (MPD_ERROR(c))
399                 return -1;
401         mpd_send_repeat(c->connection, value);
402         return mpdclient_finish_command(c);
405 gint
406 mpdclient_cmd_random(struct mpdclient *c, gint value)
408         if (MPD_ERROR(c))
409                 return -1;
411         mpd_send_random(c->connection, value);
412         return mpdclient_finish_command(c);
415 gint
416 mpdclient_cmd_single(struct mpdclient *c, gint value)
418         if (MPD_ERROR(c))
419                 return -1;
421         mpd_send_single(c->connection, value);
422         return mpdclient_finish_command(c);
425 gint
426 mpdclient_cmd_consume(struct mpdclient *c, gint value)
428         if (MPD_ERROR(c))
429                 return -1;
431         mpd_send_consume(c->connection, value);
432         return mpdclient_finish_command(c);
435 gint
436 mpdclient_cmd_crossfade(struct mpdclient *c, gint value)
438         if (MPD_ERROR(c))
439                 return -1;
441         mpd_send_crossfade(c->connection, value);
442         return mpdclient_finish_command(c);
445 gint
446 mpdclient_cmd_volume(struct mpdclient *c, gint value)
448         if (MPD_ERROR(c))
449                 return -1;
451         mpd_send_set_volume(c->connection, value);
452         return mpdclient_finish_command(c);
455 gint mpdclient_cmd_volume_up(struct mpdclient *c)
457         if (MPD_ERROR(c))
458                 return -1;
460         if (c->status == NULL ||
461             mpd_status_get_volume(c->status) == -1)
462                 return 0;
464         if (c->volume < 0)
465                 c->volume = mpd_status_get_volume(c->status);
467         if (c->volume >= 100)
468                 return 0;
470         return mpdclient_cmd_volume(c, ++c->volume);
473 gint mpdclient_cmd_volume_down(struct mpdclient *c)
475         if (MPD_ERROR(c))
476                 return -1;
478         if (c->status == NULL || mpd_status_get_volume(c->status) < 0)
479                 return 0;
481         if (c->volume < 0)
482                 c->volume = mpd_status_get_volume(c->status);
484         if (c->volume <= 0)
485                 return 0;
487         return mpdclient_cmd_volume(c, --c->volume);
490 gint
491 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
493         if (MPD_ERROR(c))
494                 return -1;
496         mpd_send_add(c->connection, path_utf8);
497         return mpdclient_finish_command(c);
500 gint
501 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
503         gint retval = 0;
505         if (MPD_ERROR(c))
506                 return -1;
508         if (song == NULL)
509                 return -1;
511         /* send the add command to mpd */
512         mpd_send_add(c->connection, mpd_song_get_uri(song));
513         if( (retval=mpdclient_finish_command(c)) )
514                 return retval;
516 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD
517         /* add the song to playlist */
518         playlist_append(&c->playlist, song);
520         /* increment the playlist id, so we don't retrieve a new playlist */
521         c->playlist.id++;
523         /* call playlist updated callback */
524         mpdclient_playlist_callback(c, PLAYLIST_EVENT_ADD, (gpointer) song);
525 #endif
527         return 0;
530 gint
531 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
533         gint retval = 0;
534         struct mpd_song *song;
536         if (MPD_ERROR(c))
537                 return -1;
539         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
540                 return -1;
542         song = playlist_get(&c->playlist, idx);
544         /* send the delete command to mpd */
545         mpd_send_delete_id(c->connection, mpd_song_get_id(song));
546         if( (retval=mpdclient_finish_command(c)) )
547                 return retval;
549 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
550         /* increment the playlist id, so we don't retrieve a new playlist */
551         c->playlist.id++;
553         /* remove the song from the playlist */
554         playlist_remove_reuse(&c->playlist, idx);
556         /* call playlist updated callback */
557         mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
559         /* remove references to the song */
560         if (c->song == song)
561                 c->song = NULL;
563         mpd_song_free(song);
564 #endif
566         return 0;
569 gint
570 mpdclient_cmd_move(struct mpdclient *c, gint old_index, gint new_index)
572         gint n;
573         struct mpd_song *song1, *song2;
575         if (MPD_ERROR(c))
576                 return -1;
578         if (old_index == new_index || new_index < 0 ||
579             (guint)new_index >= c->playlist.list->len)
580                 return -1;
582         song1 = playlist_get(&c->playlist, old_index);
583         song2 = playlist_get(&c->playlist, new_index);
585         /* send the move command to mpd */
586         mpd_send_swap_id(c->connection,
587                          mpd_song_get_id(song1), mpd_song_get_id(song2));
588         if( (n=mpdclient_finish_command(c)) )
589                 return n;
591 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
592         /* update the playlist */
593         playlist_swap(&c->playlist, old_index, new_index);
595         /* increment the playlist id, so we don't retrieve a new playlist */
596         c->playlist.id++;
597 #endif
599         /* call playlist updated callback */
600         mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index);
602         return 0;
605 gint
606 mpdclient_cmd_save_playlist(struct mpdclient *c, const gchar *filename_utf8)
608         gint retval = 0;
610         if (MPD_ERROR(c))
611                 return -1;
613         mpd_send_save(c->connection, filename_utf8);
614         if ((retval = mpdclient_finish_command(c)) == 0)
615                 mpdclient_browse_callback(c, BROWSE_PLAYLIST_SAVED, NULL);
616         return retval;
619 gint
620 mpdclient_cmd_load_playlist(struct mpdclient *c, const gchar *filename_utf8)
622         if (MPD_ERROR(c))
623                 return -1;
625         mpd_send_load(c->connection, filename_utf8);
626         return mpdclient_finish_command(c);
629 gint
630 mpdclient_cmd_delete_playlist(struct mpdclient *c, const gchar *filename_utf8)
632         gint retval = 0;
634         if (MPD_ERROR(c))
635                 return -1;
637         mpd_send_rm(c->connection, filename_utf8);
638         if ((retval = mpdclient_finish_command(c)) == 0)
639                 mpdclient_browse_callback(c, BROWSE_PLAYLIST_DELETED, NULL);
640         return retval;
644 /****************************************************************************/
645 /*** Callback management functions ******************************************/
646 /****************************************************************************/
648 static void
649 do_list_callbacks(struct mpdclient *c, GList *list, gint event, gpointer data)
651         while (list) {
652                 mpdc_list_cb_t fn = list->data;
654                 fn(c, event, data);
655                 list = list->next;
656         }
659 void
660 mpdclient_playlist_callback(struct mpdclient *c, int event, gpointer data)
662         do_list_callbacks(c, c->playlist_callbacks, event, data);
665 void
666 mpdclient_install_playlist_callback(struct mpdclient *c,mpdc_list_cb_t cb)
668         c->playlist_callbacks = g_list_append(c->playlist_callbacks, cb);
671 void
672 mpdclient_remove_playlist_callback(struct mpdclient *c, mpdc_list_cb_t cb)
674         c->playlist_callbacks = g_list_remove(c->playlist_callbacks, cb);
677 void
678 mpdclient_browse_callback(struct mpdclient *c, int event, gpointer data)
680         do_list_callbacks(c, c->browse_callbacks, event, data);
684 void
685 mpdclient_install_browse_callback(struct mpdclient *c,mpdc_list_cb_t cb)
687         c->browse_callbacks = g_list_append(c->browse_callbacks, cb);
690 void
691 mpdclient_remove_browse_callback(struct mpdclient *c, mpdc_list_cb_t cb)
693         c->browse_callbacks = g_list_remove(c->browse_callbacks, cb);
696 void
697 mpdclient_install_error_callback(struct mpdclient *c, mpdc_error_cb_t cb)
699         c->error_callbacks = g_list_append(c->error_callbacks, cb);
702 void
703 mpdclient_remove_error_callback(struct mpdclient *c, mpdc_error_cb_t cb)
705         c->error_callbacks = g_list_remove(c->error_callbacks, cb);
709 /****************************************************************************/
710 /*** Playlist management functions ******************************************/
711 /****************************************************************************/
713 /* update playlist */
714 bool
715 mpdclient_playlist_update(struct mpdclient *c)
717         struct mpd_entity *entity;
719         if (MPD_ERROR(c))
720                 return false;
722         playlist_clear(&c->playlist);
724         mpd_send_list_queue_meta(c->connection);
725         while ((entity = mpd_recv_entity(c->connection))) {
726                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
727                         playlist_append(&c->playlist, mpd_entity_get_song(entity));
729                 mpd_entity_free(entity);
730         }
732         c->playlist.id = mpd_status_get_queue_version(c->status);
733         c->song = NULL;
735         /* call playlist updated callbacks */
736         mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
738         return mpdclient_finish_command(c) == 0;
741 /* update playlist (plchanges) */
742 bool
743 mpdclient_playlist_update_changes(struct mpdclient *c)
745         struct mpd_song *song;
746         guint length;
748         if (MPD_ERROR(c))
749                 return false;
751         mpd_send_queue_changes_meta(c->connection, c->playlist.id);
753         while ((song = mpd_recv_song(c->connection)) != NULL) {
754                 int pos = mpd_song_get_pos(song);
756                 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
757                         /* update song */
758                         playlist_replace(&c->playlist, pos, song);
759                 } else {
760                         /* add a new song */
761                         playlist_append(&c->playlist, song);
762                 }
764                 mpd_song_free(song);
765         }
767         /* remove trailing songs */
769         length = mpd_status_get_queue_length(c->status);
770         while (length < c->playlist.list->len) {
771                 guint pos = c->playlist.list->len - 1;
773                 /* Remove the last playlist entry */
774                 playlist_remove(&c->playlist, pos);
775         }
777         c->song = NULL;
778         c->playlist.id = mpd_status_get_queue_version(c->status);
780         mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
782         return mpdclient_finish_command(c) == 0;
786 /****************************************************************************/
787 /*** Filelist functions *****************************************************/
788 /****************************************************************************/
790 struct filelist *
791 mpdclient_filelist_get(struct mpdclient *c, const gchar *path)
793         struct filelist *filelist;
794         struct mpd_entity *entity;
796         if (MPD_ERROR(c))
797                 return NULL;
799         mpd_send_list_meta(c->connection, path);
800         filelist = filelist_new();
801         if (path && path[0] && strcmp(path, "/"))
802                 /* add a dummy entry for ./.. */
803                 filelist_append(filelist, NULL);
805         while ((entity = mpd_recv_entity(c->connection)) != NULL)
806                 filelist_append(filelist, entity);
808         /* If there's an error, ignore it.  We'll return an empty filelist. */
809         mpdclient_finish_command(c);
811         filelist_sort_dir_play(filelist, compare_filelistentry);
813         return filelist;
816 static struct filelist *
817 mpdclient_recv_filelist_response(struct mpdclient *c)
819         struct filelist *filelist;
820         struct mpd_entity *entity;
822         filelist = filelist_new();
824         while ((entity = mpd_recv_entity(c->connection)) != NULL)
825                 filelist_append(filelist, entity);
827         if (mpdclient_finish_command(c)) {
828                 filelist_free(filelist);
829                 return NULL;
830         }
832         return filelist;
835 struct filelist *
836 mpdclient_filelist_search(struct mpdclient *c,
837                           int exact_match,
838                           enum mpd_tag_type tag,
839                           gchar *filter_utf8)
841         if (MPD_ERROR(c))
842                 return NULL;
844         mpd_search_db_songs(c->connection, exact_match);
845         mpd_search_add_tag_constraint(c->connection, MPD_OPERATOR_DEFAULT,
846                                       tag, filter_utf8);
847         mpd_search_commit(c->connection);
849         return mpdclient_recv_filelist_response(c);
852 int
853 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
855         guint i;
857         if (MPD_ERROR(c))
858                 return -1;
860         if (filelist_is_empty(fl))
861                 return 0;
863         mpd_command_list_begin(c->connection, false);
865         for (i = 0; i < filelist_length(fl); ++i) {
866                 struct filelist_entry *entry = filelist_get(fl, i);
867                 struct mpd_entity *entity  = entry->entity;
869                 if (entity != NULL &&
870                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
871                         const struct mpd_song *song =
872                                 mpd_entity_get_song(entity);
873                         const char *uri = mpd_song_get_uri(song);
875                         if (uri != NULL)
876                                 mpd_send_add(c->connection, uri);
877                 }
878         }
880         mpd_command_list_end(c->connection);
881         return mpdclient_finish_command(c);
884 GList *
885 mpdclient_get_artists(struct mpdclient *c)
887         GList *list = NULL;
888         struct mpd_pair *pair;
890         if (MPD_ERROR(c))
891                return NULL;
893         mpd_search_db_tags(c->connection, MPD_TAG_ARTIST);
894         mpd_search_commit(c->connection);
896         while ((pair = mpd_recv_pair_tag(c->connection,
897                                          MPD_TAG_ARTIST)) != NULL) {
898                 list = g_list_append(list, g_strdup(pair->value));
899                 mpd_return_pair(c->connection, pair);
900         }
902         if (mpdclient_finish_command(c))
903                 return string_list_free(list);
905         return list;
908 GList *
909 mpdclient_get_albums(struct mpdclient *c, const gchar *artist_utf8)
911         GList *list = NULL;
912         struct mpd_pair *pair;
914         if (MPD_ERROR(c))
915                return NULL;
917         mpd_search_db_tags(c->connection, MPD_TAG_ALBUM);
918         if (artist_utf8 != NULL)
919                 mpd_search_add_tag_constraint(c->connection,
920                                               MPD_OPERATOR_DEFAULT,
921                                               MPD_TAG_ARTIST, artist_utf8);
922         mpd_search_commit(c->connection);
924         while ((pair = mpd_recv_pair_tag(c->connection,
925                                          MPD_TAG_ALBUM)) != NULL) {
926                 list = g_list_append(list, g_strdup(pair->value));
927                 mpd_return_pair(c->connection, pair);
928         }
930         if (mpdclient_finish_command(c))
931                 return string_list_free(list);
933         return list;