Code

free info entity while iterating
[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));
167   return c;
170 mpdclient_t *
171 mpdclient_free(mpdclient_t *c)
173   mpdclient_disconnect(c);
174   g_list_free(c->error_callbacks);
175   g_list_free(c->playlist_callbacks);
176   g_list_free(c->browse_callbacks);
177   g_free(c);
179   return NULL;
182 gint
183 mpdclient_disconnect(mpdclient_t *c)
185   if( c->connection )
186     mpd_closeConnection(c->connection);
187   c->connection = NULL;
189   if( c->status )
190     mpd_freeStatus(c->status);
191   c->status = NULL;
193   if( c->playlist.list )
194     mpdclient_playlist_free(&c->playlist);
196   if( c->song )
197     c->song = NULL;
198   
199   return 0;
202 gint
203 mpdclient_connect(mpdclient_t *c,
204                   gchar *host,
205                   gint port,
206                   gfloat _timeout,
207                   gchar *password)
209         gint retval = 0;
211         /* close any open connection */
212         if( c->connection )
213                 mpdclient_disconnect(c);
215         /* connect to MPD */
216         c->connection = mpd_newConnection(host, port, _timeout);
217         if( c->connection->error )
218                 return error_cb(c, c->connection->error,
219                                 c->connection->errorStr);
221         /* send password */
222         if( password ) {
223                 mpd_sendPasswordCommand(c->connection, password);
224                 retval = mpdclient_finish_command(c);
225         }
226         c->need_update = TRUE;
228         return retval;
231 gint
232 mpdclient_update(mpdclient_t *c)
234   gint retval = 0;
236   if( MPD_ERROR(c) )
237     return -1;
239   /* free the old status */
240   if( c->status )
241     mpd_freeStatus(c->status);
242   
243   /* retreive new status */
244   mpd_sendStatusCommand(c->connection);
245   c->status = mpd_getStatus(c->connection);
246   if( (retval=mpdclient_finish_command(c)) )
247     return retval;
248 #ifndef NDEBUG
249   if( c->status->error )
250     D("status> %s\n", c->status->error);
251 #endif
253   /* check if the playlist needs an update */
254   if( c->playlist.id != c->status->playlist )
255     {
256       if( c->playlist.list )
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     {
265       c->song = playlist_get_song(c, c->status->song);
266     }
268   c->need_update = FALSE;
270   return retval;
274 /****************************************************************************/
275 /*** MPD Commands  **********************************************************/
276 /****************************************************************************/
278 gint
279 mpdclient_cmd_play(mpdclient_t *c, gint idx)
281 #ifdef ENABLE_SONG_ID
282         mpd_Song *song = playlist_get_song(c, idx);
284         D("Play id:%d\n", song ? song->id : -1);
285         if (song)
286                 mpd_sendPlayIdCommand(c->connection, song->id);
287         else
288                 mpd_sendPlayIdCommand(c->connection, MPD_PLAY_AT_BEGINNING);
289 #else
290         mpd_sendPlayCommand(c->connection, idx);
291 #endif
292         c->need_update = TRUE;
293         return mpdclient_finish_command(c);
296 gint
297 mpdclient_cmd_pause(mpdclient_t *c, gint value)
299         mpd_sendPauseCommand(c->connection, value);
300         return mpdclient_finish_command(c);
303 gint
304 mpdclient_cmd_stop(mpdclient_t *c)
306         mpd_sendStopCommand(c->connection);
307         return mpdclient_finish_command(c);
310 gint
311 mpdclient_cmd_next(mpdclient_t *c)
313         mpd_sendNextCommand(c->connection);
314         c->need_update = TRUE;
315         return mpdclient_finish_command(c);
318 gint
319 mpdclient_cmd_prev(mpdclient_t *c)
321         mpd_sendPrevCommand(c->connection);
322         c->need_update = TRUE;
323         return mpdclient_finish_command(c);
326 gint 
327 mpdclient_cmd_seek(mpdclient_t *c, gint id, gint pos)
329   D("Seek id:%d\n", id);
330   mpd_sendSeekIdCommand(c->connection, id, pos);
331   return mpdclient_finish_command(c);
334 gint 
335 mpdclient_cmd_shuffle(mpdclient_t *c)
337   mpd_sendShuffleCommand(c->connection);
338   c->need_update = TRUE;
339   return mpdclient_finish_command(c);
342 gint 
343 mpdclient_cmd_clear(mpdclient_t *c)
345   gint retval = 0;
347   mpd_sendClearCommand(c->connection);
348   retval = mpdclient_finish_command(c);
349   /* call playlist updated callback */
350   mpdclient_playlist_callback(c, PLAYLIST_EVENT_CLEAR, NULL);
351   c->need_update = TRUE;
352   return retval;
355 gint 
356 mpdclient_cmd_repeat(mpdclient_t *c, gint value)
358   mpd_sendRepeatCommand(c->connection, value);
359   return mpdclient_finish_command(c);
362 gint 
363 mpdclient_cmd_random(mpdclient_t *c, gint value)
365   mpd_sendRandomCommand(c->connection, value);
366   return mpdclient_finish_command(c);
369 gint 
370 mpdclient_cmd_crossfade(mpdclient_t *c, gint value)
372   mpd_sendCrossfadeCommand(c->connection, value);
373   return mpdclient_finish_command(c);
376 gint 
377 mpdclient_cmd_db_update_utf8(mpdclient_t *c, gchar *path)
379   mpd_sendUpdateCommand(c->connection, path ? path : "");
380   return mpdclient_finish_command(c);
383 gint 
384 mpdclient_cmd_volume(mpdclient_t *c, gint value)
386   mpd_sendSetvolCommand(c->connection, value);
387   return mpdclient_finish_command(c);
390 gint 
391 mpdclient_cmd_add_path_utf8(mpdclient_t *c, gchar *path_utf8)
393   mpd_sendAddCommand(c->connection, path_utf8);
394   return mpdclient_finish_command(c);
397 gint 
398 mpdclient_cmd_add_path(mpdclient_t *c, gchar *path)
400   gint retval;
401   gchar *path_utf8 = locale_to_utf8(path);
403   retval=mpdclient_cmd_add_path_utf8(c, path_utf8);
404   g_free(path_utf8);
405   return retval;
408 gint
409 mpdclient_cmd_add(mpdclient_t *c, mpd_Song *song)
411         gint retval = 0;
413         if( !song || !song->file )
414                 return -1;
416         /* send the add command to mpd */
417         mpd_sendAddCommand(c->connection, song->file);
418         if( (retval=mpdclient_finish_command(c)) )
419                 return retval;
421 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD
422         /* add the song to playlist */
423         c->playlist.list = g_list_append(c->playlist.list, mpd_songDup(song));
424         c->playlist.length++;
426         /* increment the playlist id, so we dont retrives a new playlist */
427         c->playlist.id++;
429         /* call playlist updated callback */
430         mpdclient_playlist_callback(c, PLAYLIST_EVENT_ADD, (gpointer) song);
431 #else
432         c->need_update = TRUE;
433 #endif
435         return 0;
438 gint
439 mpdclient_cmd_delete(mpdclient_t *c, gint idx)
441         gint retval = 0;
442         mpd_Song *song = playlist_get_song(c, idx);
444         if( !song )
445                 return -1;
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         c->playlist.list = g_list_remove(c->playlist.list, (gpointer) song);
463         c->playlist.length = g_list_length(c->playlist.list);
465         /* call playlist updated callback */
466         mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
468         /* remove references to the song */
469         if (c->song == song) {
470                 c->song = NULL;
471                 c->need_update = TRUE;
472         }
474         /* free song */
475         mpd_freeSong(song);
477 #else
478         c->need_update = TRUE;
479 #endif
481         return 0;
484 gint
485 mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index)
487         gint n, index1, index2;
488         GList *item1, *item2;
489         gpointer data1, data2;
490         mpd_Song *song1, *song2;
492         if (old_index == new_index || new_index < 0 ||
493             (guint)new_index >= c->playlist.length)
494                 return -1;
496         song1 = playlist_get_song(c, old_index);
497         song2 = playlist_get_song(c, new_index);
499         /* send the move command to mpd */
500 #ifdef ENABLE_SONG_ID
501         D("Swapping id:%d with id:%d\n", song1->id, song2->id);
502         mpd_sendSwapIdCommand(c->connection, song1->id, song2->id);
503 #else
504         D("Moving index %d to id:%d\n", old_index, new_index);
505         mpd_sendMoveCommand(c->connection, old_index, new_index);
506 #endif
507         if( (n=mpdclient_finish_command(c)) )
508                 return n;
510 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
511         /* update the songs position field */
512         n = song1->pos;
513         song1->pos = song2->pos;
514         song2->pos = n;
515         index1 = MIN(old_index, new_index);
516         index2 = MAX(old_index, new_index);
517         /* retreive the list items */
518         item1 = g_list_nth(c->playlist.list, index1);
519         item2 = g_list_nth(c->playlist.list, index2);
520         /* retrieve the songs */
521         data1 = item1->data;
522         data2 = item2->data;
524         /* move the second item */
525         c->playlist.list = g_list_remove(c->playlist.list, data2);
526         c->playlist.list = g_list_insert_before(c->playlist.list, item1, data2);
528         /* move the first item */
529         if (index2-index1 > 1) {
530                 item2 = g_list_nth(c->playlist.list, index2);
531                 c->playlist.list = g_list_remove(c->playlist.list, data1);
532                 c->playlist.list = g_list_insert_before(c->playlist.list,
533                                                         item2, data1);
534         }
536         /* increment the playlist id, so we dont retrives a new playlist */
537         c->playlist.id++;
539 #else
540         c->need_update = TRUE;
541 #endif
543         /* call playlist updated callback */
544         D("move> new_index=%d, old_index=%d\n", new_index, old_index);
545         mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index);
547         return 0;
550 gint 
551 mpdclient_cmd_save_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
553   gint retval = 0;
555   mpd_sendSaveCommand(c->connection, filename_utf8);
556   if( (retval=mpdclient_finish_command(c)) == 0 )
557     mpdclient_browse_callback(c, BROWSE_PLAYLIST_SAVED, NULL);
558   return retval;
561 gint 
562 mpdclient_cmd_save_playlist(mpdclient_t *c, gchar *filename)
564   gint retval = 0;
565   gchar *filename_utf8 = locale_to_utf8(filename);
566   
567   retval = mpdclient_cmd_save_playlist_utf8(c, filename);
568   g_free(filename_utf8);
569   return retval;
572 gint 
573 mpdclient_cmd_load_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
575   mpd_sendLoadCommand(c->connection, filename_utf8);
576   c->need_update = TRUE;
577   return mpdclient_finish_command(c);
580 gint 
581 mpdclient_cmd_delete_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
583   gint retval = 0;
585   mpd_sendRmCommand(c->connection, filename_utf8);
586   if( (retval=mpdclient_finish_command(c)) == 0 )
587     mpdclient_browse_callback(c, BROWSE_PLAYLIST_DELETED, NULL);
588   return retval;
591 gint 
592 mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename)
594   gint retval = 0;
595   gchar *filename_utf8 = locale_to_utf8(filename);
597   retval = mpdclient_cmd_delete_playlist_utf8(c, filename_utf8);
598   g_free(filename_utf8);
599   return retval;
603 /****************************************************************************/
604 /*** Callback managment functions *******************************************/
605 /****************************************************************************/
606 static void
607 do_list_callbacks(mpdclient_t *c, GList *list, gint event, gpointer data)
609   while(list)
610     {
611       mpdc_list_cb_t fn = list->data;
613       fn(c, event, data);
614       list=list->next;
615     }
618 void
619 mpdclient_playlist_callback(mpdclient_t *c, int event, gpointer data)
621   do_list_callbacks(c, c->playlist_callbacks, event, data);
624 void
625 mpdclient_install_playlist_callback(mpdclient_t *c,mpdc_list_cb_t cb)
627   c->playlist_callbacks = g_list_append(c->playlist_callbacks, cb);
630 void
631 mpdclient_remove_playlist_callback(mpdclient_t *c, mpdc_list_cb_t cb)
633   c->playlist_callbacks = g_list_remove(c->playlist_callbacks, cb);
636 void
637 mpdclient_browse_callback(mpdclient_t *c, int event, gpointer data)
639   do_list_callbacks(c, c->browse_callbacks, event, data);
643 void
644 mpdclient_install_browse_callback(mpdclient_t *c,mpdc_list_cb_t cb)
646   c->browse_callbacks = g_list_append(c->browse_callbacks, cb);
649 void
650 mpdclient_remove_browse_callback(mpdclient_t *c, mpdc_list_cb_t cb)
652   c->browse_callbacks = g_list_remove(c->browse_callbacks, cb);
655 void
656 mpdclient_install_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
658   c->error_callbacks = g_list_append(c->error_callbacks, cb);
661 void
662 mpdclient_remove_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
664   c->error_callbacks = g_list_remove(c->error_callbacks, cb);
667 /****************************************************************************/
668 /*** Playlist managment functions *******************************************/
669 /****************************************************************************/
671 gint
672 mpdclient_playlist_free(mpdclient_playlist_t *playlist)
674   GList *list = g_list_first(playlist->list);
676   while(list)
677     {
678       mpd_Song *song = (mpd_Song *) list->data;
679       mpd_freeSong(song);
680       list=list->next;
681     }
682   g_list_free(playlist->list);
683         memset(playlist, 0, sizeof(mpdclient_playlist_t));
684         return 0;
687 /* update playlist */
688 gint
689 mpdclient_playlist_update(mpdclient_t *c)
691         mpd_InfoEntity *entity;
693         D("mpdclient_playlist_update() [%lld]\n", c->status->playlist);
695         if (MPD_ERROR(c))
696                 return -1;
698         if (c->playlist.list)
699                 mpdclient_playlist_free(&c->playlist);
701         mpd_sendPlaylistInfoCommand(c->connection,-1);
702         while ((entity = mpd_getNextInfoEntity(c->connection))) {
703                 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
704                         mpd_Song *song = mpd_songDup(entity->info.song);
706                         c->playlist.list = g_list_append(c->playlist.list,
707                                                          (gpointer)song);
708                         c->playlist.length++;
709                 }
710                 mpd_freeInfoEntity(entity);
711         }
713         c->playlist.id = c->status->playlist;
714         c->song = NULL;
715         c->playlist.updated = TRUE;
717         /* call playlist updated callbacks */
718         mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
720         return mpdclient_finish_command(c);
723 #ifdef ENABLE_PLCHANGES
725 /* update playlist (plchanges) */
726 gint
727 mpdclient_playlist_update_changes(mpdclient_t *c)
729         mpd_InfoEntity *entity;
731         D("mpdclient_playlist_update_changes() [%lld -> %lld]\n",
732           c->status->playlist, c->playlist.id);
734         if (MPD_ERROR(c))
735                 return -1;
737         mpd_sendPlChangesCommand(c->connection, c->playlist.id);
739         while ((entity = mpd_getNextInfoEntity(c->connection)) != NULL) {
740                 mpd_Song *song = entity->info.song;
742                 if (song->pos >= 0 && (guint)song->pos < c->playlist.length) {
743                         GList *item = g_list_nth(c->playlist.list, song->pos);
745                         /* update song */
746                         D("updating pos:%d, id=%d [%p] - %s\n",
747                           song->pos, song->id, item, song->file);
748                         mpd_freeSong((mpd_Song *) item->data);
749                         item->data = mpd_songDup(song);
750                 } else {
751                         /* add a new song */
752                         D("adding song at pos %d\n", song->pos);
753                         c->playlist.list = g_list_append(c->playlist.list,
754                                                          (gpointer)mpd_songDup(song));
755                 }
757                 mpd_freeInfoEntity(entity);
758         }
760         /* remove trailing songs */
761         while ((guint)c->status->playlistLength < c->playlist.length) {
762                 GList *item = g_list_last(c->playlist.list);
764                 /* Remove the last playlist entry */
765                 D("removing song at pos %d\n", ((mpd_Song *) item->data)->pos);
766                 mpd_freeSong((mpd_Song *) item->data);
767                 c->playlist.list = g_list_delete_link(c->playlist.list, item);
768                 c->playlist.length = g_list_length(c->playlist.list);
769         }
771         c->song = NULL;
772         c->playlist.id = c->status->playlist;
773         c->playlist.updated = TRUE;
774         c->playlist.length = g_list_length(c->playlist.list);
776         mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
778         return 0;
781 #else
782 gint
783 mpdclient_playlist_update_changes(mpdclient_t *c)
785         return mpdclient_playlist_update(c);
787 #endif
789 mpd_Song *
790 playlist_get_song(mpdclient_t *c, gint idx)
792         return (mpd_Song *) g_list_nth_data(c->playlist.list, idx);
795 GList *
796 playlist_lookup(mpdclient_t *c, int id)
798         GList *list = g_list_first(c->playlist.list);
800         while (list) {
801                 mpd_Song *song = (mpd_Song *) list->data;
802                 if( song->id == id )
803                         return list;
804                 list=list->next;
805         }
807         return NULL;
810 mpd_Song *
811 playlist_lookup_song(mpdclient_t *c, gint id)
813         GList *list = c->playlist.list;
815         while (list) {
816                 mpd_Song *song = (mpd_Song *) list->data;
817                 if (song->id == id)
818                         return song;
819                 list=list->next;
820         }
822         return NULL;
825 gint
826 playlist_get_index(mpdclient_t *c, mpd_Song *song)
828         return g_list_index(c->playlist.list, song);
831 gint
832 playlist_get_index_from_id(mpdclient_t *c, gint id)
834         return g_list_index(c->playlist.list, playlist_lookup_song(c, id));
837 gint
838 playlist_get_index_from_file(mpdclient_t *c, gchar *filename)
840   GList *list = c->playlist.list;
841   gint i=0;
843   while( list )
844     {
845       mpd_Song *song = (mpd_Song *) list->data;
846       if( strcmp(song->file, filename ) == 0 )  
847         return i;
848       list=list->next;
849       i++;
850     }
851   return -1;
855 /****************************************************************************/
856 /*** Filelist functions *****************************************************/
857 /****************************************************************************/
859 mpdclient_filelist_t *
860 mpdclient_filelist_free(mpdclient_filelist_t *filelist)
862   GList *list = g_list_first(filelist->list);
864   D("mpdclient_filelist_free()\n");
865   if( list == NULL )
866     return NULL;
867   while( list!=NULL )
868     {
869       filelist_entry_t *entry = list->data;
871       if( entry->entity )
872         mpd_freeInfoEntity(entry->entity);
873       g_free(entry);
874       list=list->next;
875     }
876   g_list_free(filelist->list);
877   g_free(filelist->path);
878   filelist->path = NULL;
879   filelist->list = NULL;
880   filelist->length = 0;
881   g_free(filelist);
883   return NULL;
887 mpdclient_filelist_t *
888 mpdclient_filelist_get(mpdclient_t *c, const gchar *path)
890   mpdclient_filelist_t *filelist;
891   mpd_InfoEntity *entity;
892   gchar *path_utf8 = locale_to_utf8(path);
893   gboolean has_dirs_only = TRUE;
895   D("mpdclient_filelist_get(%s)\n", path);
896   mpd_sendLsInfoCommand(c->connection, path_utf8);
897   filelist = g_malloc0(sizeof(mpdclient_filelist_t));
898   if( path && path[0] && strcmp(path, "/") )
899     {
900       /* add a dummy entry for ./.. */
901       filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
902       entry->entity = NULL;
903       filelist->list = g_list_append(filelist->list, (gpointer) entry);
904       filelist->length++;
905     }
907   while( (entity=mpd_getNextInfoEntity(c->connection)) ) 
908     {
909       filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
910       
911       entry->entity = entity;
912       filelist->list = g_list_append(filelist->list, (gpointer) entry);
913       filelist->length++;
915       if (has_dirs_only && entity->type != MPD_INFO_ENTITY_TYPE_DIRECTORY)
916         {
917           has_dirs_only = FALSE;
918         }
919     }
920   
921    /* If there's an error, ignore it.  We'll return an empty filelist. */
922    mpdclient_finish_command(c);
923   
924   g_free(path_utf8);
925   filelist->path = g_strdup(path);
926   filelist->updated = TRUE;
928   // If there are only directory entities in the filelist, we sort it
929   if (has_dirs_only)
930     {
931       D("mpdclient_filelist_get: only dirs; sorting!\n");
932       filelist->list = g_list_sort(filelist->list, compare_filelistentry_dir);
933     }
935   return filelist;
938 mpdclient_filelist_t *
939 mpdclient_filelist_search_utf8(mpdclient_t *c, 
940                                int exact_match,
941                                int table, 
942                                gchar *filter_utf8)
944   mpdclient_filelist_t *filelist;
945   mpd_InfoEntity *entity;
947   D("mpdclient_filelist_search(%s)\n", filter_utf8);
948   if( exact_match )
949     mpd_sendFindCommand(c->connection, table, filter_utf8);
950   else
951     mpd_sendSearchCommand(c->connection, table, filter_utf8);
952   filelist = g_malloc0(sizeof(mpdclient_filelist_t));
954   while( (entity=mpd_getNextInfoEntity(c->connection)) ) 
955     {
956       filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
957       
958       entry->entity = entity;
959       filelist->list = g_list_append(filelist->list, (gpointer) entry);
960       filelist->length++;
961     }
962   
963   if( mpdclient_finish_command(c) )
964     return mpdclient_filelist_free(filelist);
966   filelist->updated = TRUE;
968   return filelist;
972 mpdclient_filelist_t *
973 mpdclient_filelist_search(mpdclient_t *c,
974                           int exact_match,
975                           int table,
976                           gchar *_filter)
978         mpdclient_filelist_t *filelist;
979         gchar *filter_utf8 = locale_to_utf8(_filter);
981         D("mpdclient_filelist_search(%s)\n", _filter);
982         filelist = mpdclient_filelist_search_utf8(c, exact_match, table,
983                                                   filter_utf8);
984         g_free(filter_utf8);
986         return filelist;
989 mpdclient_filelist_t *
990 mpdclient_filelist_update(mpdclient_t *c, mpdclient_filelist_t *filelist)
992   if( filelist != NULL )
993     {    
994       gchar *path = g_strdup(filelist->path);
996       filelist = mpdclient_filelist_free(filelist);
997       filelist = mpdclient_filelist_get(c, path);
998       g_free(path);
999       return filelist;
1000     }
1001   return NULL;
1004 filelist_entry_t *
1005 mpdclient_filelist_find_song(mpdclient_filelist_t *fl, mpd_Song *song)
1007   GList *list = g_list_first(fl->list);
1009   while( list && song)
1010     {
1011       filelist_entry_t *entry = list->data;
1012       mpd_InfoEntity *entity  = entry->entity;
1014       if( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG )
1015         {
1016           mpd_Song *song2 = entity->info.song;
1018           if( strcmp(song->file, song2->file) == 0 )
1019             {
1020               return entry;
1021             }
1022         }
1023       list = list->next;
1024     }
1025   return NULL;
1028 int
1029 mpdclient_filelist_add_all(mpdclient_t *c, mpdclient_filelist_t *fl)
1031   GList *list = g_list_first(fl->list);
1033   if( fl->list==NULL || fl->length<1 )
1034     return 0;
1036   mpd_sendCommandListBegin(c->connection);
1037   while( list )
1038     {
1039       filelist_entry_t *entry = list->data;
1040       mpd_InfoEntity *entity  = entry->entity;
1042       if( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG )
1043         {
1044           mpd_Song *song = entity->info.song;
1046           mpd_sendAddCommand(c->connection, song->file);
1047         }
1048       list = list->next;
1049     }
1050   mpd_sendCommandListEnd(c->connection);
1051   return mpdclient_finish_command(c);
1061 GList *
1062 mpdclient_get_artists_utf8(mpdclient_t *c)
1064   gchar *str = NULL; 
1065   GList *list = NULL;
1067   D("mpdclient_get_artists()\n");
1068   mpd_sendListCommand(c->connection, MPD_TABLE_ARTIST, NULL);
1069   while( (str=mpd_getNextArtist(c->connection)) )
1070     {
1071       list = g_list_append(list, (gpointer) str);
1072     }
1073   if( mpdclient_finish_command(c) )
1074     {
1075       return string_list_free(list);
1076     }  
1078   return list;
1081 GList *
1082 mpdclient_get_albums_utf8(mpdclient_t *c, gchar *artist_utf8)
1084   gchar *str = NULL; 
1085   GList *list = NULL;
1087   D("mpdclient_get_albums(%s)\n", artist_utf8);
1088   mpd_sendListCommand(c->connection, MPD_TABLE_ALBUM, artist_utf8);
1089   while( (str=mpd_getNextAlbum(c->connection)) )
1090     {
1091       list = g_list_append(list, (gpointer) str);
1092     }
1093   if( mpdclient_finish_command(c) )
1094     {
1095       return string_list_free(list);
1096     }  
1097   
1098   return list;