Code

2c46d8e9069b09201069a9a32221d10605cd18a4
[ncmpc.git] / src / mpdclient.c
1 /* 
2  * $Id$
3  *
4  * (c) 2004 by Kalle Wallin <kaw@linux.se>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
21 #include "mpdclient.h"
22 #include "screen_utils.h"
23 #include "config.h"
24 #include "ncmpc.h"
25 #include "support.h"
26 #include "options.h"
27 #include "strfsong.h"
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <time.h>
32 #include <string.h>
34 #undef  ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD /* broken with song id's */
35 #define ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
36 #define ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
37 #define ENABLE_SONG_ID
38 #define ENABLE_PLCHANGES 
40 #define BUFSIZE 1024
42 #define MPD_ERROR(c) (c==NULL || c->connection==NULL || c->connection->error)
44 /* from utils.c */
45 extern GList *string_list_free(GList *string_list);
48 /* filelist sorting functions */
49 static gint
50 compare_filelistentry_dir(gconstpointer filelist_entry1,
51                           gconstpointer filelist_entry2)
52 {
53         const mpd_InfoEntity *e1, *e2;
54         char *key1, *key2;
55         int n = 0;
57         e1 = ((const filelist_entry_t *)filelist_entry1)->entity;
58         e2 = ((const filelist_entry_t *)filelist_entry2)->entity;
60         if (e1 && e2 &&
61             e1->type == MPD_INFO_ENTITY_TYPE_DIRECTORY &&
62             e2->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
63                 key1 = g_utf8_collate_key(e1->info.directory->path,-1);
64                 key2 = g_utf8_collate_key(e2->info.directory->path,-1);
65                 n = strcmp(key1,key2);
66                 g_free(key1);
67                 g_free(key2);
68         }
70         return n;
71 }
73 /* sort by list-format */
74 gint
75 compare_filelistentry_format(gconstpointer filelist_entry1,
76                              gconstpointer filelist_entry2)
77 {
78         const mpd_InfoEntity *e1, *e2;
79         char key1[BUFSIZE], key2[BUFSIZE];
80         int n = 0;
82         e1 = ((const filelist_entry_t *)filelist_entry1)->entity;
83         e2 = ((const filelist_entry_t *)filelist_entry2)->entity;
85         if (e1 && e2 &&
86             e1->type == MPD_INFO_ENTITY_TYPE_SONG &&
87             e2->type == MPD_INFO_ENTITY_TYPE_SONG) {
88                 strfsong(key1, BUFSIZE, LIST_FORMAT, e1->info.song);
89                 strfsong(key2, BUFSIZE, LIST_FORMAT, e2->info.song);
90                 n = strcmp(key1,key2);
91         }
93         return n;
94 }
97 /* Error callbacks */
98 static gint
99 error_cb(mpdclient_t *c, gint error, gchar *msg)
101   GList *list = c->error_callbacks;
102   
103   if( list==NULL )
104     fprintf(stderr, "error [%d]: %s\n", (error & 0xFF), msg);
106   while(list)
107     {
108       mpdc_error_cb_t cb = list->data;
109       if( cb )
110         cb(c, error, msg);
111       list=list->next;
112     }
113   mpd_clearError(c->connection);
114   return error;
117 #ifndef NDEBUG
118 // Unused ath the moment
119 /*
120 #include "strfsong.h"
122 static gchar *
123 get_song_name(mpd_Song *song)
125   static gchar name[256];
127   strfsong(name, 256, "[%artist% - ]%title%|%file%", song);
128   return name;
130 */
131 #endif
133 /****************************************************************************/
134 /*** mpdclient functions ****************************************************/
135 /****************************************************************************/
137 gint
138 mpdclient_finish_command(mpdclient_t *c) 
140   mpd_finishCommand(c->connection);
142   if( c->connection->error )
143     {
144       gchar *msg = locale_to_utf8(c->connection->errorStr);
145       gint error = c->connection->error;
146       if( error == MPD_ERROR_ACK )
147         error = error | (c->connection->errorCode << 8);
148       if(  c->connection->errorCode == MPD_ACK_ERROR_PERMISSION )
149         {
150           if(screen_auth(c) == 0) return 0;
151         }
152       error_cb(c, error, msg);
153       g_free(msg);
154       return error;
155     }
157   return 0;
160 mpdclient_t *
161 mpdclient_new(void)
163         mpdclient_t *c;
165         c = g_malloc0(sizeof(mpdclient_t));
166         playlist_init(&c->playlist);
168         return c;
171 void
172 mpdclient_free(mpdclient_t *c)
174         mpdclient_disconnect(c);
176         mpdclient_playlist_free(&c->playlist);
178         g_list_free(c->error_callbacks);
179         g_list_free(c->playlist_callbacks);
180         g_list_free(c->browse_callbacks);
181         g_free(c);
184 gint
185 mpdclient_disconnect(mpdclient_t *c)
187         if (c->connection)
188                 mpd_closeConnection(c->connection);
189         c->connection = NULL;
191         if (c->status)
192                 mpd_freeStatus(c->status);
193         c->status = NULL;
195         playlist_clear(&c->playlist);
197         if (c->song)
198                 c->song = NULL;
200         return 0;
203 gint
204 mpdclient_connect(mpdclient_t *c,
205                   gchar *host,
206                   gint port,
207                   gfloat _timeout,
208                   gchar *password)
210         gint retval = 0;
212         /* close any open connection */
213         if( c->connection )
214                 mpdclient_disconnect(c);
216         /* connect to MPD */
217         c->connection = mpd_newConnection(host, port, _timeout);
218         if( c->connection->error )
219                 return error_cb(c, c->connection->error,
220                                 c->connection->errorStr);
222         /* send password */
223         if( password ) {
224                 mpd_sendPasswordCommand(c->connection, password);
225                 retval = mpdclient_finish_command(c);
226         }
227         c->need_update = TRUE;
229         return retval;
232 gint
233 mpdclient_update(mpdclient_t *c)
235         gint retval = 0;
237         if (MPD_ERROR(c))
238                 return -1;
240         /* free the old status */
241         if (c->status)
242                 mpd_freeStatus(c->status);
244         /* retreive new status */
245         mpd_sendStatusCommand(c->connection);
246         c->status = mpd_getStatus(c->connection);
247         if ((retval=mpdclient_finish_command(c)))
248                 return retval;
249 #ifndef NDEBUG
250         if (c->status->error)
251                 D("status> %s\n", c->status->error);
252 #endif
254         /* check if the playlist needs an update */
255         if (c->playlist.id != c->status->playlist) {
256                 if (playlist_is_empty(&c->playlist))
257                         retval = mpdclient_playlist_update_changes(c);
258                 else
259                         retval = mpdclient_playlist_update(c);
260         }
262         /* update the current song */
263         if (!c->song || c->status->songid != c->song->id) {
264                 c->song = playlist_get_song(c, c->status->song);
265         }
267         c->need_update = FALSE;
269         return retval;
273 /****************************************************************************/
274 /*** MPD Commands  **********************************************************/
275 /****************************************************************************/
277 gint
278 mpdclient_cmd_play(mpdclient_t *c, gint idx)
280 #ifdef ENABLE_SONG_ID
281         struct mpd_song *song = playlist_get_song(c, idx);
283         D("Play id:%d\n", song ? song->id : -1);
284         if (song)
285                 mpd_sendPlayIdCommand(c->connection, song->id);
286         else
287                 mpd_sendPlayIdCommand(c->connection, MPD_PLAY_AT_BEGINNING);
288 #else
289         mpd_sendPlayCommand(c->connection, idx);
290 #endif
291         c->need_update = TRUE;
292         return mpdclient_finish_command(c);
295 gint
296 mpdclient_cmd_pause(mpdclient_t *c, gint value)
298         mpd_sendPauseCommand(c->connection, value);
299         return mpdclient_finish_command(c);
302 gint
303 mpdclient_cmd_stop(mpdclient_t *c)
305         mpd_sendStopCommand(c->connection);
306         return mpdclient_finish_command(c);
309 gint
310 mpdclient_cmd_next(mpdclient_t *c)
312         mpd_sendNextCommand(c->connection);
313         c->need_update = TRUE;
314         return mpdclient_finish_command(c);
317 gint
318 mpdclient_cmd_prev(mpdclient_t *c)
320         mpd_sendPrevCommand(c->connection);
321         c->need_update = TRUE;
322         return mpdclient_finish_command(c);
325 gint 
326 mpdclient_cmd_seek(mpdclient_t *c, gint id, gint pos)
328   D("Seek id:%d\n", id);
329   mpd_sendSeekIdCommand(c->connection, id, pos);
330   return mpdclient_finish_command(c);
333 gint 
334 mpdclient_cmd_shuffle(mpdclient_t *c)
336   mpd_sendShuffleCommand(c->connection);
337   c->need_update = TRUE;
338   return mpdclient_finish_command(c);
341 gint 
342 mpdclient_cmd_clear(mpdclient_t *c)
344   gint retval = 0;
346   mpd_sendClearCommand(c->connection);
347   retval = mpdclient_finish_command(c);
348   /* call playlist updated callback */
349   mpdclient_playlist_callback(c, PLAYLIST_EVENT_CLEAR, NULL);
350   c->need_update = TRUE;
351   return retval;
354 gint 
355 mpdclient_cmd_repeat(mpdclient_t *c, gint value)
357   mpd_sendRepeatCommand(c->connection, value);
358   return mpdclient_finish_command(c);
361 gint 
362 mpdclient_cmd_random(mpdclient_t *c, gint value)
364   mpd_sendRandomCommand(c->connection, value);
365   return mpdclient_finish_command(c);
368 gint 
369 mpdclient_cmd_crossfade(mpdclient_t *c, gint value)
371   mpd_sendCrossfadeCommand(c->connection, value);
372   return mpdclient_finish_command(c);
375 gint 
376 mpdclient_cmd_db_update_utf8(mpdclient_t *c, gchar *path)
378   mpd_sendUpdateCommand(c->connection, path ? path : "");
379   return mpdclient_finish_command(c);
382 gint 
383 mpdclient_cmd_volume(mpdclient_t *c, gint value)
385   mpd_sendSetvolCommand(c->connection, value);
386   return mpdclient_finish_command(c);
389 gint 
390 mpdclient_cmd_add_path_utf8(mpdclient_t *c, gchar *path_utf8)
392   mpd_sendAddCommand(c->connection, path_utf8);
393   return mpdclient_finish_command(c);
396 gint 
397 mpdclient_cmd_add_path(mpdclient_t *c, gchar *path)
399   gint retval;
400   gchar *path_utf8 = locale_to_utf8(path);
402   retval=mpdclient_cmd_add_path_utf8(c, path_utf8);
403   g_free(path_utf8);
404   return retval;
407 gint
408 mpdclient_cmd_add(mpdclient_t *c, struct mpd_song *song)
410         gint retval = 0;
412         if( !song || !song->file )
413                 return -1;
415         /* send the add command to mpd */
416         mpd_sendAddCommand(c->connection, song->file);
417         if( (retval=mpdclient_finish_command(c)) )
418                 return retval;
420 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD
421         /* add the song to playlist */
422         playlist_append(&c->playlist, song);
424         /* increment the playlist id, so we dont retrives a new playlist */
425         c->playlist.id++;
427         /* call playlist updated callback */
428         mpdclient_playlist_callback(c, PLAYLIST_EVENT_ADD, (gpointer) song);
429 #else
430         c->need_update = TRUE;
431 #endif
433         return 0;
436 gint
437 mpdclient_cmd_delete(mpdclient_t *c, gint idx)
439         gint retval = 0;
440         struct mpd_song *song;
442         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
443                 return -1;
445         song = playlist_get(&c->playlist, idx);
447         /* send the delete command to mpd */
448 #ifdef ENABLE_SONG_ID
449         D("Delete id:%d\n", song->id);
450         mpd_sendDeleteIdCommand(c->connection, song->id);
451 #else
452         mpd_sendDeleteCommand(c->connection, idx);
453 #endif
454         if( (retval=mpdclient_finish_command(c)) )
455                 return retval;
457 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
458         /* increment the playlist id, so we dont retrive a new playlist */
459         c->playlist.id++;
461         /* remove the song from the playlist */
462         playlist_remove_reuse(&c->playlist, idx);
464         /* call playlist updated callback */
465         mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
467         /* remove references to the song */
468         if (c->song == song) {
469                 c->song = NULL;
470                 c->need_update = TRUE;
471         }
473         mpd_freeSong(song);
475 #else
476         c->need_update = TRUE;
477 #endif
479         return 0;
482 gint
483 mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index)
485         gint n;
486         struct mpd_song *song1, *song2;
488         if (old_index == new_index || new_index < 0 ||
489             (guint)new_index >= c->playlist.list->len)
490                 return -1;
492         song1 = playlist_get(&c->playlist, old_index);
493         song2 = playlist_get(&c->playlist, new_index);
495         /* send the move command to mpd */
496 #ifdef ENABLE_SONG_ID
497         D("Swapping id:%d with id:%d\n", song1->id, song2->id);
498         mpd_sendSwapIdCommand(c->connection, song1->id, song2->id);
499 #else
500         D("Moving index %d to id:%d\n", old_index, new_index);
501         mpd_sendMoveCommand(c->connection, old_index, new_index);
502 #endif
503         if( (n=mpdclient_finish_command(c)) )
504                 return n;
506 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
507         /* update the playlist */
508         playlist_swap(&c->playlist, old_index, new_index);
510         /* increment the playlist id, so we dont retrives a new playlist */
511         c->playlist.id++;
513 #else
514         c->need_update = TRUE;
515 #endif
517         /* call playlist updated callback */
518         D("move> new_index=%d, old_index=%d\n", new_index, old_index);
519         mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index);
521         return 0;
524 gint 
525 mpdclient_cmd_save_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
527   gint retval = 0;
529   mpd_sendSaveCommand(c->connection, filename_utf8);
530   if( (retval=mpdclient_finish_command(c)) == 0 )
531     mpdclient_browse_callback(c, BROWSE_PLAYLIST_SAVED, NULL);
532   return retval;
535 gint 
536 mpdclient_cmd_save_playlist(mpdclient_t *c, gchar *filename)
538   gint retval = 0;
539   gchar *filename_utf8 = locale_to_utf8(filename);
540   
541   retval = mpdclient_cmd_save_playlist_utf8(c, filename);
542   g_free(filename_utf8);
543   return retval;
546 gint 
547 mpdclient_cmd_load_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
549   mpd_sendLoadCommand(c->connection, filename_utf8);
550   c->need_update = TRUE;
551   return mpdclient_finish_command(c);
554 gint 
555 mpdclient_cmd_delete_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
557   gint retval = 0;
559   mpd_sendRmCommand(c->connection, filename_utf8);
560   if( (retval=mpdclient_finish_command(c)) == 0 )
561     mpdclient_browse_callback(c, BROWSE_PLAYLIST_DELETED, NULL);
562   return retval;
565 gint 
566 mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename)
568   gint retval = 0;
569   gchar *filename_utf8 = locale_to_utf8(filename);
571   retval = mpdclient_cmd_delete_playlist_utf8(c, filename_utf8);
572   g_free(filename_utf8);
573   return retval;
577 /****************************************************************************/
578 /*** Callback managment functions *******************************************/
579 /****************************************************************************/
580 static void
581 do_list_callbacks(mpdclient_t *c, GList *list, gint event, gpointer data)
583   while(list)
584     {
585       mpdc_list_cb_t fn = list->data;
587       fn(c, event, data);
588       list=list->next;
589     }
592 void
593 mpdclient_playlist_callback(mpdclient_t *c, int event, gpointer data)
595   do_list_callbacks(c, c->playlist_callbacks, event, data);
598 void
599 mpdclient_install_playlist_callback(mpdclient_t *c,mpdc_list_cb_t cb)
601   c->playlist_callbacks = g_list_append(c->playlist_callbacks, cb);
604 void
605 mpdclient_remove_playlist_callback(mpdclient_t *c, mpdc_list_cb_t cb)
607   c->playlist_callbacks = g_list_remove(c->playlist_callbacks, cb);
610 void
611 mpdclient_browse_callback(mpdclient_t *c, int event, gpointer data)
613   do_list_callbacks(c, c->browse_callbacks, event, data);
617 void
618 mpdclient_install_browse_callback(mpdclient_t *c,mpdc_list_cb_t cb)
620   c->browse_callbacks = g_list_append(c->browse_callbacks, cb);
623 void
624 mpdclient_remove_browse_callback(mpdclient_t *c, mpdc_list_cb_t cb)
626   c->browse_callbacks = g_list_remove(c->browse_callbacks, cb);
629 void
630 mpdclient_install_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
632   c->error_callbacks = g_list_append(c->error_callbacks, cb);
635 void
636 mpdclient_remove_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
638   c->error_callbacks = g_list_remove(c->error_callbacks, cb);
641 /****************************************************************************/
642 /*** Playlist managment functions *******************************************/
643 /****************************************************************************/
646 /* update playlist */
647 gint
648 mpdclient_playlist_update(mpdclient_t *c)
650         mpd_InfoEntity *entity;
652         D("mpdclient_playlist_update() [%lld]\n", c->status->playlist);
654         if (MPD_ERROR(c))
655                 return -1;
657         playlist_clear(&c->playlist);
659         mpd_sendPlaylistInfoCommand(c->connection,-1);
660         while ((entity = mpd_getNextInfoEntity(c->connection))) {
661                 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
662                         playlist_append(&c->playlist, entity->info.song);
664                 mpd_freeInfoEntity(entity);
665         }
667         c->playlist.id = c->status->playlist;
668         c->song = NULL;
670         /* call playlist updated callbacks */
671         mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
673         return mpdclient_finish_command(c);
676 #ifdef ENABLE_PLCHANGES
678 /* update playlist (plchanges) */
679 gint
680 mpdclient_playlist_update_changes(mpdclient_t *c)
682         mpd_InfoEntity *entity;
684         D("mpdclient_playlist_update_changes() [%lld -> %lld]\n",
685           c->status->playlist, c->playlist.id);
687         if (MPD_ERROR(c))
688                 return -1;
690         mpd_sendPlChangesCommand(c->connection, c->playlist.id);
692         while ((entity = mpd_getNextInfoEntity(c->connection)) != NULL) {
693                 struct mpd_song *song = entity->info.song;
695                 if (song->pos >= 0 && (guint)song->pos < c->playlist.list->len) {
696                         /* update song */
697                         D("updating pos:%d, id=%d - %s\n",
698                           song->pos, song->id, song->file);
699                         playlist_replace(&c->playlist, song->pos, song);
700                 } else {
701                         /* add a new song */
702                         D("adding song at pos %d\n", song->pos);
703                         playlist_append(&c->playlist, song);
704                 }
706                 mpd_freeInfoEntity(entity);
707         }
709         /* remove trailing songs */
710         while ((guint)c->status->playlistLength < c->playlist.list->len) {
711                 guint pos = c->playlist.list->len - 1;
713                 /* Remove the last playlist entry */
714                 D("removing song at pos %d\n", pos);
715                 playlist_remove(&c->playlist, pos);
716         }
718         c->song = NULL;
719         c->playlist.id = c->status->playlist;
721         mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
723         return 0;
726 #else
727 gint
728 mpdclient_playlist_update_changes(mpdclient_t *c)
730         return mpdclient_playlist_update(c);
732 #endif
735 /****************************************************************************/
736 /*** Filelist functions *****************************************************/
737 /****************************************************************************/
739 mpdclient_filelist_t *
740 mpdclient_filelist_get(mpdclient_t *c, const gchar *path)
742         mpdclient_filelist_t *filelist;
743         mpd_InfoEntity *entity;
744         gchar *path_utf8 = locale_to_utf8(path);
745         gboolean has_dirs_only = TRUE;
747         D("mpdclient_filelist_get(%s)\n", path);
748         mpd_sendLsInfoCommand(c->connection, path_utf8);
749         filelist = filelist_new(path);
750         if (path && path[0] && strcmp(path, "/"))
751                 /* add a dummy entry for ./.. */
752                 filelist_append(filelist, NULL);
754         while ((entity=mpd_getNextInfoEntity(c->connection))) {
755                 filelist_append(filelist, entity);
757                 if (has_dirs_only && entity->type != MPD_INFO_ENTITY_TYPE_DIRECTORY) {
758                         has_dirs_only = FALSE;
759                 }
760         }
762         /* If there's an error, ignore it.  We'll return an empty filelist. */
763         mpdclient_finish_command(c);
765         g_free(path_utf8);
766         filelist->updated = TRUE;
768         // If there are only directory entities in the filelist, we sort it
769         if (has_dirs_only) {
770                 D("mpdclient_filelist_get: only dirs; sorting!\n");
771                 filelist_sort(filelist, compare_filelistentry_dir);
772         }
774         return filelist;
777 mpdclient_filelist_t *
778 mpdclient_filelist_search_utf8(mpdclient_t *c,
779                                int exact_match,
780                                int table,
781                                gchar *filter_utf8)
783         mpdclient_filelist_t *filelist;
784         mpd_InfoEntity *entity;
786         D("mpdclient_filelist_search(%s)\n", filter_utf8);
787         if (exact_match)
788                 mpd_sendFindCommand(c->connection, table, filter_utf8);
789         else
790                 mpd_sendSearchCommand(c->connection, table, filter_utf8);
791         filelist = filelist_new(NULL);
793         while ((entity=mpd_getNextInfoEntity(c->connection)))
794                 filelist_append(filelist, entity);
796         if (mpdclient_finish_command(c)) {
797                 filelist_free(filelist);
798                 return NULL;
799         }
801         filelist->updated = TRUE;
802         return filelist;
806 mpdclient_filelist_t *
807 mpdclient_filelist_search(mpdclient_t *c,
808                           int exact_match,
809                           int table,
810                           gchar *_filter)
812         mpdclient_filelist_t *filelist;
813         gchar *filter_utf8 = locale_to_utf8(_filter);
815         D("mpdclient_filelist_search(%s)\n", _filter);
816         filelist = mpdclient_filelist_search_utf8(c, exact_match, table,
817                                                   filter_utf8);
818         g_free(filter_utf8);
820         return filelist;
823 mpdclient_filelist_t *
824 mpdclient_filelist_update(mpdclient_t *c, mpdclient_filelist_t *filelist)
826   if( filelist != NULL )
827     {    
828       gchar *path = g_strdup(filelist->path);
830       filelist_free(filelist);
831       filelist = mpdclient_filelist_get(c, path);
832       g_free(path);
833       return filelist;
834     }
835   return NULL;
838 int
839 mpdclient_filelist_add_all(mpdclient_t *c, mpdclient_filelist_t *fl)
841         guint i;
843         if (filelist_is_empty(fl))
844                 return 0;
846         mpd_sendCommandListBegin(c->connection);
848         for (i = 0; i < filelist_length(fl); ++i) {
849                 filelist_entry_t *entry = filelist_get(fl, i);
850                 mpd_InfoEntity *entity  = entry->entity;
852                 if (entity && entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
853                         struct mpd_song *song = entity->info.song;
855                         mpd_sendAddCommand(c->connection, song->file);
856                 }
857         }
859         mpd_sendCommandListEnd(c->connection);
860         return mpdclient_finish_command(c);
863 GList *
864 mpdclient_get_artists_utf8(mpdclient_t *c)
866   gchar *str = NULL; 
867   GList *list = NULL;
869   D("mpdclient_get_artists()\n");
870   mpd_sendListCommand(c->connection, MPD_TABLE_ARTIST, NULL);
871   while( (str=mpd_getNextArtist(c->connection)) )
872     {
873       list = g_list_append(list, (gpointer) str);
874     }
875   if( mpdclient_finish_command(c) )
876     {
877       return string_list_free(list);
878     }  
880   return list;
883 GList *
884 mpdclient_get_albums_utf8(mpdclient_t *c, gchar *artist_utf8)
886   gchar *str = NULL; 
887   GList *list = NULL;
889   D("mpdclient_get_albums(%s)\n", artist_utf8);
890   mpd_sendListCommand(c->connection, MPD_TABLE_ALBUM, artist_utf8);
891   while( (str=mpd_getNextAlbum(c->connection)) )
892     {
893       list = g_list_append(list, (gpointer) str);
894     }
895   if( mpdclient_finish_command(c) )
896     {
897       return string_list_free(list);
898     }  
899   
900   return list;