Code

fix shadow warnings
[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             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 < 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                 }
756         }
758         /* remove trailing songs */
759         while (c->status->playlistLength < c->playlist.length) {
760                 GList *item = g_list_last(c->playlist.list);
762                 /* Remove the last playlist entry */
763                 D("removing song at pos %d\n", ((mpd_Song *) item->data)->pos);
764                 mpd_freeSong((mpd_Song *) item->data);
765                 c->playlist.list = g_list_delete_link(c->playlist.list, item);
766                 c->playlist.length = g_list_length(c->playlist.list);
767         }
769         c->song = NULL;
770         c->playlist.id = c->status->playlist;
771         c->playlist.updated = TRUE;
772         c->playlist.length = g_list_length(c->playlist.list);
774         mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
776         return 0;
779 #else
780 gint
781 mpdclient_playlist_update_changes(mpdclient_t *c)
783         return mpdclient_playlist_update(c);
785 #endif
787 mpd_Song *
788 playlist_get_song(mpdclient_t *c, gint idx)
790         return (mpd_Song *) g_list_nth_data(c->playlist.list, idx);
793 GList *
794 playlist_lookup(mpdclient_t *c, int id)
796         GList *list = g_list_first(c->playlist.list);
798         while (list) {
799                 mpd_Song *song = (mpd_Song *) list->data;
800                 if( song->id == id )
801                         return list;
802                 list=list->next;
803         }
805         return NULL;
808 mpd_Song *
809 playlist_lookup_song(mpdclient_t *c, gint id)
811         GList *list = c->playlist.list;
813         while (list) {
814                 mpd_Song *song = (mpd_Song *) list->data;
815                 if (song->id == id)
816                         return song;
817                 list=list->next;
818         }
820         return NULL;
823 gint
824 playlist_get_index(mpdclient_t *c, mpd_Song *song)
826         return g_list_index(c->playlist.list, song);
829 gint
830 playlist_get_index_from_id(mpdclient_t *c, gint id)
832         return g_list_index(c->playlist.list, playlist_lookup_song(c, id));
835 gint
836 playlist_get_index_from_file(mpdclient_t *c, gchar *filename)
838   GList *list = c->playlist.list;
839   gint i=0;
841   while( list )
842     {
843       mpd_Song *song = (mpd_Song *) list->data;
844       if( strcmp(song->file, filename ) == 0 )  
845         return i;
846       list=list->next;
847       i++;
848     }
849   return -1;
853 /****************************************************************************/
854 /*** Filelist functions *****************************************************/
855 /****************************************************************************/
857 mpdclient_filelist_t *
858 mpdclient_filelist_free(mpdclient_filelist_t *filelist)
860   GList *list = g_list_first(filelist->list);
862   D("mpdclient_filelist_free()\n");
863   if( list == NULL )
864     return NULL;
865   while( list!=NULL )
866     {
867       filelist_entry_t *entry = list->data;
869       if( entry->entity )
870         mpd_freeInfoEntity(entry->entity);
871       g_free(entry);
872       list=list->next;
873     }
874   g_list_free(filelist->list);
875   g_free(filelist->path);
876   filelist->path = NULL;
877   filelist->list = NULL;
878   filelist->length = 0;
879   g_free(filelist);
881   return NULL;
885 mpdclient_filelist_t *
886 mpdclient_filelist_get(mpdclient_t *c, const gchar *path)
888   mpdclient_filelist_t *filelist;
889   mpd_InfoEntity *entity;
890   gchar *path_utf8 = locale_to_utf8(path);
891   gboolean has_dirs_only = TRUE;
893   D("mpdclient_filelist_get(%s)\n", path);
894   mpd_sendLsInfoCommand(c->connection, path_utf8);
895   filelist = g_malloc0(sizeof(mpdclient_filelist_t));
896   if( path && path[0] && strcmp(path, "/") )
897     {
898       /* add a dummy entry for ./.. */
899       filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
900       entry->entity = NULL;
901       filelist->list = g_list_append(filelist->list, (gpointer) entry);
902       filelist->length++;
903     }
905   while( (entity=mpd_getNextInfoEntity(c->connection)) ) 
906     {
907       filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
908       
909       entry->entity = entity;
910       filelist->list = g_list_append(filelist->list, (gpointer) entry);
911       filelist->length++;
913       if (has_dirs_only && entity->type != MPD_INFO_ENTITY_TYPE_DIRECTORY)
914         {
915           has_dirs_only = FALSE;
916         }
917     }
918   
919    /* If there's an error, ignore it.  We'll return an empty filelist. */
920    mpdclient_finish_command(c);
921   
922   g_free(path_utf8);
923   filelist->path = g_strdup(path);
924   filelist->updated = TRUE;
926   // If there are only directory entities in the filelist, we sort it
927   if (has_dirs_only)
928     {
929       D("mpdclient_filelist_get: only dirs; sorting!\n");
930       filelist->list = g_list_sort(filelist->list, compare_filelistentry_dir);
931     }
933   return filelist;
936 mpdclient_filelist_t *
937 mpdclient_filelist_search_utf8(mpdclient_t *c, 
938                                int exact_match,
939                                int table, 
940                                gchar *filter_utf8)
942   mpdclient_filelist_t *filelist;
943   mpd_InfoEntity *entity;
945   D("mpdclient_filelist_search(%s)\n", filter_utf8);
946   if( exact_match )
947     mpd_sendFindCommand(c->connection, table, filter_utf8);
948   else
949     mpd_sendSearchCommand(c->connection, table, filter_utf8);
950   filelist = g_malloc0(sizeof(mpdclient_filelist_t));
952   while( (entity=mpd_getNextInfoEntity(c->connection)) ) 
953     {
954       filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
955       
956       entry->entity = entity;
957       filelist->list = g_list_append(filelist->list, (gpointer) entry);
958       filelist->length++;
959     }
960   
961   if( mpdclient_finish_command(c) )
962     return mpdclient_filelist_free(filelist);
964   filelist->updated = TRUE;
966   return filelist;
970 mpdclient_filelist_t *
971 mpdclient_filelist_search(mpdclient_t *c,
972                           int exact_match,
973                           int table,
974                           gchar *_filter)
976         mpdclient_filelist_t *filelist;
977         gchar *filter_utf8 = locale_to_utf8(_filter);
979         D("mpdclient_filelist_search(%s)\n", _filter);
980         filelist = mpdclient_filelist_search_utf8(c, exact_match, table,
981                                                   filter_utf8);
982         g_free(filter_utf8);
984         return filelist;
987 mpdclient_filelist_t *
988 mpdclient_filelist_update(mpdclient_t *c, mpdclient_filelist_t *filelist)
990   if( filelist != NULL )
991     {    
992       gchar *path = g_strdup(filelist->path);
994       filelist = mpdclient_filelist_free(filelist);
995       filelist = mpdclient_filelist_get(c, path);
996       g_free(path);
997       return filelist;
998     }
999   return NULL;
1002 filelist_entry_t *
1003 mpdclient_filelist_find_song(mpdclient_filelist_t *fl, mpd_Song *song)
1005   GList *list = g_list_first(fl->list);
1007   while( list && song)
1008     {
1009       filelist_entry_t *entry = list->data;
1010       mpd_InfoEntity *entity  = entry->entity;
1012       if( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG )
1013         {
1014           mpd_Song *song2 = entity->info.song;
1016           if( strcmp(song->file, song2->file) == 0 )
1017             {
1018               return entry;
1019             }
1020         }
1021       list = list->next;
1022     }
1023   return NULL;
1026 int
1027 mpdclient_filelist_add_all(mpdclient_t *c, mpdclient_filelist_t *fl)
1029   GList *list = g_list_first(fl->list);
1031   if( fl->list==NULL || fl->length<1 )
1032     return 0;
1034   mpd_sendCommandListBegin(c->connection);
1035   while( list )
1036     {
1037       filelist_entry_t *entry = list->data;
1038       mpd_InfoEntity *entity  = entry->entity;
1040       if( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG )
1041         {
1042           mpd_Song *song = entity->info.song;
1044           mpd_sendAddCommand(c->connection, song->file);
1045         }
1046       list = list->next;
1047     }
1048   mpd_sendCommandListEnd(c->connection);
1049   return mpdclient_finish_command(c);
1059 GList *
1060 mpdclient_get_artists_utf8(mpdclient_t *c)
1062   gchar *str = NULL; 
1063   GList *list = NULL;
1065   D("mpdclient_get_artists()\n");
1066   mpd_sendListCommand(c->connection, MPD_TABLE_ARTIST, NULL);
1067   while( (str=mpd_getNextArtist(c->connection)) )
1068     {
1069       list = g_list_append(list, (gpointer) str);
1070     }
1071   if( mpdclient_finish_command(c) )
1072     {
1073       return string_list_free(list);
1074     }  
1076   return list;
1079 GList *
1080 mpdclient_get_albums_utf8(mpdclient_t *c, gchar *artist_utf8)
1082   gchar *str = NULL; 
1083   GList *list = NULL;
1085   D("mpdclient_get_albums(%s)\n", artist_utf8);
1086   mpd_sendListCommand(c->connection, MPD_TABLE_ALBUM, artist_utf8);
1087   while( (str=mpd_getNextAlbum(c->connection)) )
1088     {
1089       list = g_list_append(list, (gpointer) str);
1090     }
1091   if( mpdclient_finish_command(c) )
1092     {
1093       return string_list_free(list);
1094     }  
1095   
1096   return list;