Code

playlist: make functions const
[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 = MPD_STATUS_NO_VOLUME;
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 gint
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;
181         return 0;
184 gint
185 mpdclient_connect(struct mpdclient *c,
186                   const gchar *host,
187                   gint port,
188                   gfloat _timeout,
189                   const gchar *password)
191         gint retval = 0;
193         /* close any open connection */
194         if( c->connection )
195                 mpdclient_disconnect(c);
197         /* connect to MPD */
198         c->connection = mpd_connection_new(host, port, _timeout * 1000);
199         if (c->connection == NULL)
200                 g_error("Out of memory");
202         if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) {
203                 retval = mpdclient_handle_error(c);
204                 if (retval != 0) {
205                         mpd_connection_free(c->connection);
206                         c->connection = NULL;
207                 }
209                 return retval;
210         }
212         /* send password */
213         if( password ) {
214                 mpd_send_password(c->connection, password);
215                 retval = mpdclient_finish_command(c);
216         }
218         return retval;
221 gint
222 mpdclient_update(struct mpdclient *c)
224         gint retval = 0;
226         c->volume = MPD_STATUS_NO_VOLUME;
228         if (MPD_ERROR(c))
229                 return -1;
231         /* free the old status */
232         if (c->status)
233                 mpd_status_free(c->status);
235         /* retrieve new status */
236         c->status = mpd_run_status(c->connection);
237         if (c->status == NULL)
238                 return mpdclient_handle_error(c);
240         if (c->updatingdb &&
241             c->updatingdb != mpd_status_get_update_id(c->status))
242                 mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
244         c->updatingdb = mpd_status_get_update_id(c->status);
245         c->volume = mpd_status_get_volume(c->status);
247         /* check if the playlist needs an update */
248         if (c->playlist.id != mpd_status_get_queue_version(c->status)) {
249                 if (!playlist_is_empty(&c->playlist))
250                         retval = mpdclient_playlist_update_changes(c);
251                 else
252                         retval = mpdclient_playlist_update(c);
253         }
255         /* update the current song */
256         if (!c->song || mpd_status_get_song_id(c->status)) {
257                 c->song = playlist_get_song(&c->playlist,
258                                             mpd_status_get_song_pos(c->status));
259         }
261         return retval;
265 /****************************************************************************/
266 /*** MPD Commands  **********************************************************/
267 /****************************************************************************/
269 gint
270 mpdclient_cmd_play(struct mpdclient *c, gint idx)
272         const struct mpd_song *song = playlist_get_song(&c->playlist, idx);
274         if (MPD_ERROR(c))
275                 return -1;
277         if (song)
278                 mpd_send_play_id(c->connection, mpd_song_get_id(song));
279         else
280                 mpd_send_play(c->connection);
282         return mpdclient_finish_command(c);
285 gint
286 mpdclient_cmd_pause(struct mpdclient *c, gint value)
288         if (MPD_ERROR(c))
289                 return -1;
291         mpd_send_pause(c->connection, value);
292         return mpdclient_finish_command(c);
295 gint
296 mpdclient_cmd_crop(struct mpdclient *c)
298         struct mpd_status *status;
299         bool playing;
300         int length, current;
302         if (MPD_ERROR(c))
303                 return -1;
305         status = mpd_run_status(c->connection);
306         if (status == NULL)
307                 return mpdclient_handle_error(c);
309         playing = mpd_status_get_state(status) == MPD_STATE_PLAY ||
310                 mpd_status_get_state(status) == MPD_STATE_PAUSE;
311         length = mpd_status_get_queue_length(status);
312         current = mpd_status_get_song_pos(status);
314         mpd_status_free(status);
316         if (!playing || length < 2)
317                 return 0;
319         mpd_command_list_begin(c->connection, false);
321         while (--length >= 0)
322                 if (length != current)
323                         mpd_send_delete(c->connection, length);
325         mpd_command_list_end(c->connection);
327         return mpdclient_finish_command(c);
330 gint
331 mpdclient_cmd_stop(struct mpdclient *c)
333         if (MPD_ERROR(c))
334                 return -1;
336         mpd_send_stop(c->connection);
337         return mpdclient_finish_command(c);
340 gint
341 mpdclient_cmd_next(struct mpdclient *c)
343         if (MPD_ERROR(c))
344                 return -1;
346         mpd_send_next(c->connection);
347         return mpdclient_finish_command(c);
350 gint
351 mpdclient_cmd_prev(struct mpdclient *c)
353         if (MPD_ERROR(c))
354                 return -1;
356         mpd_send_previous(c->connection);
357         return mpdclient_finish_command(c);
360 gint
361 mpdclient_cmd_seek(struct mpdclient *c, gint id, gint pos)
363         if (MPD_ERROR(c))
364                 return -1;
366         mpd_send_seek_id(c->connection, id, pos);
367         return mpdclient_finish_command(c);
370 gint
371 mpdclient_cmd_shuffle(struct mpdclient *c)
373         if (MPD_ERROR(c))
374                 return -1;
376         mpd_send_shuffle(c->connection);
377         return mpdclient_finish_command(c);
380 gint
381 mpdclient_cmd_shuffle_range(struct mpdclient *c, guint start, guint end)
383         mpd_send_shuffle_range(c->connection, start, end);
384         return mpdclient_finish_command(c);
387 gint
388 mpdclient_cmd_clear(struct mpdclient *c)
390         gint retval = 0;
392         if (MPD_ERROR(c))
393                 return -1;
395         mpd_send_clear(c->connection);
396         retval = mpdclient_finish_command(c);
397         /* call playlist updated callback */
398         mpdclient_playlist_callback(c, PLAYLIST_EVENT_CLEAR, NULL);
399         return retval;
402 gint
403 mpdclient_cmd_repeat(struct mpdclient *c, gint value)
405         if (MPD_ERROR(c))
406                 return -1;
408         mpd_send_repeat(c->connection, value);
409         return mpdclient_finish_command(c);
412 gint
413 mpdclient_cmd_random(struct mpdclient *c, gint value)
415         if (MPD_ERROR(c))
416                 return -1;
418         mpd_send_random(c->connection, value);
419         return mpdclient_finish_command(c);
422 gint
423 mpdclient_cmd_single(struct mpdclient *c, gint value)
425         if (MPD_ERROR(c))
426                 return -1;
428         mpd_send_single(c->connection, value);
429         return mpdclient_finish_command(c);
432 gint
433 mpdclient_cmd_consume(struct mpdclient *c, gint value)
435         if (MPD_ERROR(c))
436                 return -1;
438         mpd_send_consume(c->connection, value);
439         return mpdclient_finish_command(c);
442 gint
443 mpdclient_cmd_crossfade(struct mpdclient *c, gint value)
445         if (MPD_ERROR(c))
446                 return -1;
448         mpd_send_crossfade(c->connection, value);
449         return mpdclient_finish_command(c);
452 gint
453 mpdclient_cmd_volume(struct mpdclient *c, gint value)
455         if (MPD_ERROR(c))
456                 return -1;
458         mpd_send_set_volume(c->connection, value);
459         return mpdclient_finish_command(c);
462 gint mpdclient_cmd_volume_up(struct mpdclient *c)
464         if (MPD_ERROR(c))
465                 return -1;
467         if (c->status == NULL ||
468             mpd_status_get_volume(c->status) == MPD_STATUS_NO_VOLUME)
469                 return 0;
471         if (c->volume == MPD_STATUS_NO_VOLUME)
472                 c->volume = mpd_status_get_volume(c->status);
474         if (c->volume >= 100)
475                 return 0;
477         return mpdclient_cmd_volume(c, ++c->volume);
480 gint mpdclient_cmd_volume_down(struct mpdclient *c)
482         if (MPD_ERROR(c))
483                 return -1;
485         if (c->status == NULL ||
486             mpd_status_get_volume(c->status) == MPD_STATUS_NO_VOLUME)
487                 return 0;
489         if (c->volume == MPD_STATUS_NO_VOLUME)
490                 c->volume = mpd_status_get_volume(c->status);
492         if (c->volume <= 0)
493                 return 0;
495         return mpdclient_cmd_volume(c, --c->volume);
498 gint
499 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
501         if (MPD_ERROR(c))
502                 return -1;
504         mpd_send_add(c->connection, path_utf8);
505         return mpdclient_finish_command(c);
508 gint
509 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
511         gint retval = 0;
513         if (MPD_ERROR(c))
514                 return -1;
516         if (song == NULL)
517                 return -1;
519         /* send the add command to mpd */
520         mpd_send_add(c->connection, mpd_song_get_uri(song));
521         if( (retval=mpdclient_finish_command(c)) )
522                 return retval;
524 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD
525         /* add the song to playlist */
526         playlist_append(&c->playlist, song);
528         /* increment the playlist id, so we don't retrieve a new playlist */
529         c->playlist.id++;
531         /* call playlist updated callback */
532         mpdclient_playlist_callback(c, PLAYLIST_EVENT_ADD, (gpointer) song);
533 #endif
535         return 0;
538 gint
539 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
541         gint retval = 0;
542         struct mpd_song *song;
544         if (MPD_ERROR(c))
545                 return -1;
547         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
548                 return -1;
550         song = playlist_get(&c->playlist, idx);
552         /* send the delete command to mpd */
553         mpd_send_delete_id(c->connection, mpd_song_get_id(song));
554         if( (retval=mpdclient_finish_command(c)) )
555                 return retval;
557 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
558         /* increment the playlist id, so we don't retrieve a new playlist */
559         c->playlist.id++;
561         /* remove the song from the playlist */
562         playlist_remove_reuse(&c->playlist, idx);
564         /* call playlist updated callback */
565         mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
567         /* remove references to the song */
568         if (c->song == song)
569                 c->song = NULL;
571         mpd_song_free(song);
572 #endif
574         return 0;
577 gint
578 mpdclient_cmd_move(struct mpdclient *c, gint old_index, gint new_index)
580         gint n;
581         struct mpd_song *song1, *song2;
583         if (MPD_ERROR(c))
584                 return -1;
586         if (old_index == new_index || new_index < 0 ||
587             (guint)new_index >= c->playlist.list->len)
588                 return -1;
590         song1 = playlist_get(&c->playlist, old_index);
591         song2 = playlist_get(&c->playlist, new_index);
593         /* send the move command to mpd */
594         mpd_send_swap_id(c->connection,
595                          mpd_song_get_id(song1), mpd_song_get_id(song2));
596         if( (n=mpdclient_finish_command(c)) )
597                 return n;
599 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
600         /* update the playlist */
601         playlist_swap(&c->playlist, old_index, new_index);
603         /* increment the playlist id, so we don't retrieve a new playlist */
604         c->playlist.id++;
605 #endif
607         /* call playlist updated callback */
608         mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index);
610         return 0;
613 gint
614 mpdclient_cmd_save_playlist(struct mpdclient *c, const gchar *filename_utf8)
616         gint retval = 0;
618         if (MPD_ERROR(c))
619                 return -1;
621         mpd_send_save(c->connection, filename_utf8);
622         if ((retval = mpdclient_finish_command(c)) == 0)
623                 mpdclient_browse_callback(c, BROWSE_PLAYLIST_SAVED, NULL);
624         return retval;
627 gint
628 mpdclient_cmd_load_playlist(struct mpdclient *c, const gchar *filename_utf8)
630         if (MPD_ERROR(c))
631                 return -1;
633         mpd_send_load(c->connection, filename_utf8);
634         return mpdclient_finish_command(c);
637 gint
638 mpdclient_cmd_delete_playlist(struct mpdclient *c, const gchar *filename_utf8)
640         gint retval = 0;
642         if (MPD_ERROR(c))
643                 return -1;
645         mpd_send_rm(c->connection, filename_utf8);
646         if ((retval = mpdclient_finish_command(c)) == 0)
647                 mpdclient_browse_callback(c, BROWSE_PLAYLIST_DELETED, NULL);
648         return retval;
652 /****************************************************************************/
653 /*** Callback management functions ******************************************/
654 /****************************************************************************/
656 static void
657 do_list_callbacks(struct mpdclient *c, GList *list, gint event, gpointer data)
659         while (list) {
660                 mpdc_list_cb_t fn = list->data;
662                 fn(c, event, data);
663                 list = list->next;
664         }
667 void
668 mpdclient_playlist_callback(struct mpdclient *c, int event, gpointer data)
670         do_list_callbacks(c, c->playlist_callbacks, event, data);
673 void
674 mpdclient_install_playlist_callback(struct mpdclient *c,mpdc_list_cb_t cb)
676         c->playlist_callbacks = g_list_append(c->playlist_callbacks, cb);
679 void
680 mpdclient_remove_playlist_callback(struct mpdclient *c, mpdc_list_cb_t cb)
682         c->playlist_callbacks = g_list_remove(c->playlist_callbacks, cb);
685 void
686 mpdclient_browse_callback(struct mpdclient *c, int event, gpointer data)
688         do_list_callbacks(c, c->browse_callbacks, event, data);
692 void
693 mpdclient_install_browse_callback(struct mpdclient *c,mpdc_list_cb_t cb)
695         c->browse_callbacks = g_list_append(c->browse_callbacks, cb);
698 void
699 mpdclient_remove_browse_callback(struct mpdclient *c, mpdc_list_cb_t cb)
701         c->browse_callbacks = g_list_remove(c->browse_callbacks, cb);
704 void
705 mpdclient_install_error_callback(struct mpdclient *c, mpdc_error_cb_t cb)
707         c->error_callbacks = g_list_append(c->error_callbacks, cb);
710 void
711 mpdclient_remove_error_callback(struct mpdclient *c, mpdc_error_cb_t cb)
713         c->error_callbacks = g_list_remove(c->error_callbacks, cb);
717 /****************************************************************************/
718 /*** Playlist management functions ******************************************/
719 /****************************************************************************/
721 /* update playlist */
722 gint
723 mpdclient_playlist_update(struct mpdclient *c)
725         struct mpd_entity *entity;
727         if (MPD_ERROR(c))
728                 return -1;
730         playlist_clear(&c->playlist);
732         mpd_send_list_queue_meta(c->connection);
733         while ((entity = mpd_recv_entity(c->connection))) {
734                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
735                         playlist_append(&c->playlist, mpd_entity_get_song(entity));
737                 mpd_entity_free(entity);
738         }
740         c->playlist.id = mpd_status_get_queue_version(c->status);
741         c->song = NULL;
743         /* call playlist updated callbacks */
744         mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
746         return mpdclient_finish_command(c);
749 /* update playlist (plchanges) */
750 gint
751 mpdclient_playlist_update_changes(struct mpdclient *c)
753         struct mpd_song *song;
754         guint length;
756         if (MPD_ERROR(c))
757                 return -1;
759         mpd_send_queue_changes_meta(c->connection, c->playlist.id);
761         while ((song = mpd_recv_song(c->connection)) != NULL) {
762                 int pos = mpd_song_get_pos(song);
764                 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
765                         /* update song */
766                         playlist_replace(&c->playlist, pos, song);
767                 } else {
768                         /* add a new song */
769                         playlist_append(&c->playlist, song);
770                 }
772                 mpd_song_free(song);
773         }
775         /* remove trailing songs */
777         length = mpd_status_get_queue_length(c->status);
778         while (length < c->playlist.list->len) {
779                 guint pos = c->playlist.list->len - 1;
781                 /* Remove the last playlist entry */
782                 playlist_remove(&c->playlist, pos);
783         }
785         c->song = NULL;
786         c->playlist.id = mpd_status_get_queue_version(c->status);
788         mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
790         return 0;
794 /****************************************************************************/
795 /*** Filelist functions *****************************************************/
796 /****************************************************************************/
798 struct filelist *
799 mpdclient_filelist_get(struct mpdclient *c, const gchar *path)
801         struct filelist *filelist;
802         struct mpd_entity *entity;
804         if (MPD_ERROR(c))
805                 return NULL;
807         mpd_send_list_meta(c->connection, path);
808         filelist = filelist_new();
809         if (path && path[0] && strcmp(path, "/"))
810                 /* add a dummy entry for ./.. */
811                 filelist_append(filelist, NULL);
813         while ((entity = mpd_recv_entity(c->connection)) != NULL)
814                 filelist_append(filelist, entity);
816         /* If there's an error, ignore it.  We'll return an empty filelist. */
817         mpdclient_finish_command(c);
819         filelist_sort_dir_play(filelist, compare_filelistentry);
821         return filelist;
824 static struct filelist *
825 mpdclient_recv_filelist_response(struct mpdclient *c)
827         struct filelist *filelist;
828         struct mpd_entity *entity;
830         filelist = filelist_new();
832         while ((entity = mpd_recv_entity(c->connection)) != NULL)
833                 filelist_append(filelist, entity);
835         if (mpdclient_finish_command(c)) {
836                 filelist_free(filelist);
837                 return NULL;
838         }
840         return filelist;
843 struct filelist *
844 mpdclient_filelist_search(struct mpdclient *c,
845                           int exact_match,
846                           enum mpd_tag_type tag,
847                           gchar *filter_utf8)
849         if (MPD_ERROR(c))
850                 return NULL;
852         mpd_search_db_songs(c->connection, exact_match);
853         mpd_search_add_tag_constraint(c->connection, MPD_OPERATOR_DEFAULT,
854                                       tag, filter_utf8);
855         mpd_search_commit(c->connection);
857         return mpdclient_recv_filelist_response(c);
860 int
861 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
863         guint i;
865         if (MPD_ERROR(c))
866                 return -1;
868         if (filelist_is_empty(fl))
869                 return 0;
871         mpd_command_list_begin(c->connection, false);
873         for (i = 0; i < filelist_length(fl); ++i) {
874                 struct filelist_entry *entry = filelist_get(fl, i);
875                 struct mpd_entity *entity  = entry->entity;
877                 if (entity != NULL &&
878                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
879                         const struct mpd_song *song =
880                                 mpd_entity_get_song(entity);
881                         const char *uri = mpd_song_get_uri(song);
883                         if (uri != NULL)
884                                 mpd_send_add(c->connection, uri);
885                 }
886         }
888         mpd_command_list_end(c->connection);
889         return mpdclient_finish_command(c);
892 GList *
893 mpdclient_get_artists(struct mpdclient *c)
895         GList *list = NULL;
896         struct mpd_pair *pair;
898         if (MPD_ERROR(c))
899                return NULL;
901         mpd_search_db_tags(c->connection, MPD_TAG_ARTIST);
902         mpd_search_commit(c->connection);
904         while ((pair = mpd_recv_pair_tag(c->connection,
905                                          MPD_TAG_ARTIST)) != NULL) {
906                 list = g_list_append(list, g_strdup(pair->value));
907                 mpd_return_pair(c->connection, pair);
908         }
910         if (mpdclient_finish_command(c))
911                 return string_list_free(list);
913         return list;
916 GList *
917 mpdclient_get_albums(struct mpdclient *c, const gchar *artist_utf8)
919         GList *list = NULL;
920         struct mpd_pair *pair;
922         if (MPD_ERROR(c))
923                return NULL;
925         mpd_search_db_tags(c->connection, MPD_TAG_ALBUM);
926         if (artist_utf8 != NULL)
927                 mpd_search_add_tag_constraint(c->connection,
928                                               MPD_OPERATOR_DEFAULT,
929                                               MPD_TAG_ARTIST, artist_utf8);
930         mpd_search_commit(c->connection);
932         while ((pair = mpd_recv_pair_tag(c->connection,
933                                          MPD_TAG_ALBUM)) != NULL) {
934                 list = g_list_append(list, g_strdup(pair->value));
935                 mpd_return_pair(c->connection, pair);
936         }
938         if (mpdclient_finish_command(c))
939                 return string_list_free(list);
941         return list;