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 <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <time.h>
25 #include <string.h>
26 #include <glib.h>
28 #include "config.h"
29 #include "ncmpc.h"
30 #include "support.h"
31 #include "mpdclient.h"
32 #include "options.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 MPD_ERROR(c) (c==NULL || c->connection==NULL || c->connection->error)
43 /* Error callbacks */
44 static gint
45 error_cb(mpdclient_t *c, gint error, gchar *msg)
46 {
47 GList *list = c->error_callbacks;
49 if( list==NULL )
50 fprintf(stderr, "error [%d]: %s\n", (error & 0xFF), msg);
52 while(list)
53 {
54 mpdc_error_cb_t cb = list->data;
55 if( cb )
56 cb(c, error, msg);
57 list=list->next;
58 }
59 mpd_clearError(c->connection);
60 return error;
61 }
63 #ifdef DEBUG
64 #include "strfsong.h"
66 static gchar *
67 get_song_name(mpd_Song *song)
68 {
69 static gchar name[256];
71 strfsong(name, 256, "[%artist% - ]%title%|%file%", song);
72 return name;
73 }
75 #endif
77 /****************************************************************************/
78 /*** mpdclient functions ****************************************************/
79 /****************************************************************************/
81 gint
82 mpdclient_finish_command(mpdclient_t *c)
83 {
84 mpd_finishCommand(c->connection);
86 if( c->connection->error )
87 {
88 gchar *msg = locale_to_utf8(c->connection->errorStr);
89 gint error = c->connection->error;
91 if( error == MPD_ERROR_ACK )
92 error = error | (c->connection->errorCode << 8);
94 error_cb(c, error, msg);
95 g_free(msg);
96 return error;
97 }
99 return 0;
100 }
102 mpdclient_t *
103 mpdclient_new(void)
104 {
105 mpdclient_t *c;
107 c = g_malloc0(sizeof(mpdclient_t));
109 return c;
110 }
112 mpdclient_t *
113 mpdclient_free(mpdclient_t *c)
114 {
115 mpdclient_disconnect(c);
116 g_list_free(c->error_callbacks);
117 g_list_free(c->playlist_callbacks);
118 g_list_free(c->browse_callbacks);
119 g_free(c);
121 return NULL;
122 }
124 gint
125 mpdclient_disconnect(mpdclient_t *c)
126 {
127 if( c->connection )
128 mpd_closeConnection(c->connection);
129 c->connection = NULL;
131 if( c->status )
132 mpd_freeStatus(c->status);
133 c->status = NULL;
135 if( c->playlist.list )
136 mpdclient_playlist_free(&c->playlist);
138 if( c->song )
139 c->song = NULL;
141 return 0;
142 }
144 gint
145 mpdclient_connect(mpdclient_t *c,
146 gchar *host,
147 gint port,
148 gfloat timeout,
149 gchar *password)
150 {
151 gint retval = 0;
153 /* close any open connection */
154 if( c->connection )
155 mpdclient_disconnect(c);
157 /* connect to MPD */
158 c->connection = mpd_newConnection(host, port, timeout);
159 if( c->connection->error )
160 return error_cb(c, c->connection->error, c->connection->errorStr);
162 /* send password */
163 if( password )
164 {
165 mpd_sendPasswordCommand(c->connection, password);
166 retval = mpdclient_finish_command(c);
167 }
168 c->need_update = TRUE;
170 return retval;
171 }
173 gint
174 mpdclient_update(mpdclient_t *c)
175 {
176 gint retval = 0;
178 if( MPD_ERROR(c) )
179 return -1;
181 /* free the old status */
182 if( c->status )
183 mpd_freeStatus(c->status);
185 /* retreive new status */
186 mpd_sendStatusCommand(c->connection);
187 c->status = mpd_getStatus(c->connection);
188 if( (retval=mpdclient_finish_command(c)) )
189 return retval;
190 #ifdef DEBUG
191 if( c->status->error )
192 D("status> %s\n", c->status->error);
193 #endif
195 /* check if the playlist needs an update */
196 if( c->playlist.id != c->status->playlist )
197 {
198 if( c->playlist.list )
199 retval = mpdclient_playlist_update_changes(c);
200 else
201 retval = mpdclient_playlist_update(c);
202 }
204 /* update the current song */
205 if( !c->song || c->status->songid != c->song->id )
206 {
207 c->song = playlist_get_song(c, c->status->song);
208 }
210 c->need_update = FALSE;
212 return retval;
213 }
216 /****************************************************************************/
217 /*** MPD Commands **********************************************************/
218 /****************************************************************************/
220 gint
221 mpdclient_cmd_play(mpdclient_t *c, gint index)
222 {
223 #ifdef ENABLE_SONG_ID
224 mpd_Song *song = playlist_get_song(c, index);
226 D("Play id:%d\n", song ? song->id : -1);
227 if( song )
228 mpd_sendPlayIdCommand(c->connection, song->id);
229 else
230 mpd_sendPlayIdCommand(c->connection, MPD_PLAY_AT_BEGINNING);
231 #else
232 mpd_sendPlayCommand(c->connection, index);
233 #endif
234 c->need_update = TRUE;
235 return mpdclient_finish_command(c);
236 }
238 gint
239 mpdclient_cmd_pause(mpdclient_t *c, gint value)
240 {
241 mpd_sendPauseCommand(c->connection, value);
242 return mpdclient_finish_command(c);
243 }
245 gint
246 mpdclient_cmd_stop(mpdclient_t *c)
247 {
248 mpd_sendStopCommand(c->connection);
249 return mpdclient_finish_command(c);
250 }
252 gint
253 mpdclient_cmd_next(mpdclient_t *c)
254 {
255 mpd_sendNextCommand(c->connection);
256 c->need_update = TRUE;
257 return mpdclient_finish_command(c);
258 }
260 gint
261 mpdclient_cmd_prev(mpdclient_t *c)
262 {
263 mpd_sendPrevCommand(c->connection);
264 c->need_update = TRUE;
265 return mpdclient_finish_command(c);
266 }
268 gint
269 mpdclient_cmd_seek(mpdclient_t *c, gint id, gint pos)
270 {
271 D("Seek id:%d\n", id);
272 mpd_sendSeekIdCommand(c->connection, id, pos);
273 return mpdclient_finish_command(c);
274 }
276 gint
277 mpdclient_cmd_shuffle(mpdclient_t *c)
278 {
279 mpd_sendShuffleCommand(c->connection);
280 c->need_update = TRUE;
281 return mpdclient_finish_command(c);
282 }
284 gint
285 mpdclient_cmd_clear(mpdclient_t *c)
286 {
287 gint retval = 0;
289 mpd_sendClearCommand(c->connection);
290 retval = mpdclient_finish_command(c);
291 /* call playlist updated callback */
292 mpdclient_playlist_callback(c, PLAYLIST_EVENT_CLEAR, NULL);
293 c->need_update = TRUE;
294 return retval;
295 }
297 gint
298 mpdclient_cmd_repeat(mpdclient_t *c, gint value)
299 {
300 mpd_sendRepeatCommand(c->connection, value);
301 return mpdclient_finish_command(c);
302 }
304 gint
305 mpdclient_cmd_random(mpdclient_t *c, gint value)
306 {
307 mpd_sendRandomCommand(c->connection, value);
308 return mpdclient_finish_command(c);
309 }
311 gint
312 mpdclient_cmd_crossfade(mpdclient_t *c, gint value)
313 {
314 mpd_sendCrossfadeCommand(c->connection, value);
315 return mpdclient_finish_command(c);
316 }
318 gint
319 mpdclient_cmd_db_update_utf8(mpdclient_t *c, gchar *path)
320 {
321 mpd_sendUpdateCommand(c->connection, path ? path : "");
322 return mpdclient_finish_command(c);
323 }
325 gint
326 mpdclient_cmd_volume(mpdclient_t *c, gint value)
327 {
328 mpd_sendSetvolCommand(c->connection, value);
329 return mpdclient_finish_command(c);
330 }
332 gint
333 mpdclient_cmd_add_path_utf8(mpdclient_t *c, gchar *path_utf8)
334 {
335 mpd_sendAddCommand(c->connection, path_utf8);
336 return mpdclient_finish_command(c);
337 }
339 gint
340 mpdclient_cmd_add_path(mpdclient_t *c, gchar *path)
341 {
342 gint retval;
343 gchar *path_utf8 = locale_to_utf8(path);
345 retval=mpdclient_cmd_add_path_utf8(c, path_utf8);
346 g_free(path_utf8);
347 return retval;
348 }
350 gint
351 mpdclient_cmd_add(mpdclient_t *c, mpd_Song *song)
352 {
353 gint retval = 0;
355 if( !song || !song->file )
356 return -1;
358 /* send the add command to mpd */
359 mpd_sendAddCommand(c->connection, song->file);
360 if( (retval=mpdclient_finish_command(c)) )
361 return retval;
363 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD
364 /* add the song to playlist */
365 c->playlist.list = g_list_append(c->playlist.list, mpd_songDup(song));
366 c->playlist.length++;
368 /* increment the playlist id, so we dont retrives a new playlist */
369 c->playlist.id++;
371 /* call playlist updated callback */
372 mpdclient_playlist_callback(c, PLAYLIST_EVENT_ADD, (gpointer) song);
373 #else
374 c->need_update = TRUE;
375 #endif
377 return 0;
378 }
380 gint
381 mpdclient_cmd_delete(mpdclient_t *c, gint index)
382 {
383 gint retval = 0;
384 mpd_Song *song = playlist_get_song(c, index);
386 if( !song )
387 return -1;
389 /* send the delete command to mpd */
390 #ifdef ENABLE_SONG_ID
391 D("Delete id:%d\n", song->id);
392 mpd_sendDeleteIdCommand(c->connection, song->id);
393 #else
394 mpd_sendDeleteCommand(c->connection, index);
395 #endif
396 if( (retval=mpdclient_finish_command(c)) )
397 return retval;
399 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
400 /* increment the playlist id, so we dont retrive a new playlist */
401 c->playlist.id++;
403 /* remove the song from the playlist */
404 c->playlist.list = g_list_remove(c->playlist.list, (gpointer) song);
405 c->playlist.length = g_list_length(c->playlist.list);
407 /* call playlist updated callback */
408 mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
410 /* remove references to the song */
411 if( c->song == song )
412 {
413 c->song = NULL;
414 c->need_update = TRUE;
415 }
417 /* free song */
418 mpd_freeSong(song);
420 #else
421 c->need_update = TRUE;
422 #endif
424 return 0;
425 }
427 gint
428 mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index)
429 {
430 gint n, index1, index2;
431 GList *item1, *item2;
432 gpointer data1, data2;
433 mpd_Song *song1, *song2;
435 if( old_index==new_index || new_index<0 || new_index>=c->playlist.length )
436 return -1;
438 song1 = playlist_get_song(c, old_index);
439 song2 = playlist_get_song(c, new_index);
441 /* send the move command to mpd */
442 #ifdef ENABLE_SONG_ID
443 D("Swaping id:%d with id:%d\n", song1->id, song2->id);
444 mpd_sendSwapIdCommand(c->connection, song1->id, song2->id);
445 #else
446 D("Moving index %d to id:%d\n", old_index, new_index);
447 mpd_sendMoveCommand(c->connection, old_index, new_index);
448 #endif
449 if( (n=mpdclient_finish_command(c)) )
450 return n;
452 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
453 /* update the songs position field */
454 n = song1->pos;
455 song1->pos = song2->pos;
456 song2->pos = n;
457 index1 = MIN(old_index, new_index);
458 index2 = MAX(old_index, new_index);
459 /* retreive the list items */
460 item1 = g_list_nth(c->playlist.list, index1);
461 item2 = g_list_nth(c->playlist.list, index2);
462 /* retrieve the songs */
463 data1 = item1->data;
464 data2 = item2->data;
466 /* move the second item */
467 c->playlist.list = g_list_remove(c->playlist.list, data2);
468 c->playlist.list = g_list_insert_before(c->playlist.list, item1, data2);
470 /* move the first item */
471 if( index2-index1 >1 )
472 {
473 item2 = g_list_nth(c->playlist.list, index2);
474 c->playlist.list = g_list_remove(c->playlist.list, data1);
475 c->playlist.list = g_list_insert_before(c->playlist.list, item2, data1);
476 }
478 /* increment the playlist id, so we dont retrives a new playlist */
479 c->playlist.id++;
481 #else
482 c->need_update = TRUE;
483 #endif
485 /* call playlist updated callback */
486 D("move> new_index=%d, old_index=%d\n", new_index, old_index);
487 mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index);
489 return 0;
490 }
492 gint
493 mpdclient_cmd_save_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
494 {
495 gint retval = 0;
497 mpd_sendSaveCommand(c->connection, filename_utf8);
498 if( (retval=mpdclient_finish_command(c)) == 0 )
499 mpdclient_browse_callback(c, BROWSE_PLAYLIST_SAVED, NULL);
500 return retval;
501 }
503 gint
504 mpdclient_cmd_save_playlist(mpdclient_t *c, gchar *filename)
505 {
506 gint retval = 0;
507 gchar *filename_utf8 = locale_to_utf8(filename);
509 retval = mpdclient_cmd_save_playlist_utf8(c, filename);
510 g_free(filename_utf8);
511 return retval;
512 }
514 gint
515 mpdclient_cmd_load_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
516 {
517 mpd_sendLoadCommand(c->connection, filename_utf8);
518 c->need_update = TRUE;
519 return mpdclient_finish_command(c);
520 }
522 gint
523 mpdclient_cmd_delete_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
524 {
525 gint retval = 0;
527 mpd_sendRmCommand(c->connection, filename_utf8);
528 if( (retval=mpdclient_finish_command(c)) == 0 )
529 mpdclient_browse_callback(c, BROWSE_PLAYLIST_DELETED, NULL);
530 return retval;
531 }
533 gint
534 mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename)
535 {
536 gint retval = 0;
537 gchar *filename_utf8 = locale_to_utf8(filename);
539 retval = mpdclient_cmd_delete_playlist_utf8(c, filename_utf8);
540 g_free(filename_utf8);
541 return retval;
542 }
545 /****************************************************************************/
546 /*** Callback managment functions *******************************************/
547 /****************************************************************************/
548 static void
549 do_list_callbacks(mpdclient_t *c, GList *list, gint event, gpointer data)
550 {
551 while(list)
552 {
553 mpdc_list_cb_t fn = list->data;
555 fn(c, event, data);
556 list=list->next;
557 }
558 }
560 void
561 mpdclient_playlist_callback(mpdclient_t *c, int event, gpointer data)
562 {
563 do_list_callbacks(c, c->playlist_callbacks, event, data);
564 }
566 void
567 mpdclient_install_playlist_callback(mpdclient_t *c,mpdc_list_cb_t cb)
568 {
569 c->playlist_callbacks = g_list_append(c->playlist_callbacks, cb);
570 }
572 void
573 mpdclient_remove_playlist_callback(mpdclient_t *c, mpdc_list_cb_t cb)
574 {
575 c->playlist_callbacks = g_list_remove(c->playlist_callbacks, cb);
576 }
578 void
579 mpdclient_browse_callback(mpdclient_t *c, int event, gpointer data)
580 {
581 do_list_callbacks(c, c->browse_callbacks, event, data);
582 }
585 void
586 mpdclient_install_browse_callback(mpdclient_t *c,mpdc_list_cb_t cb)
587 {
588 c->browse_callbacks = g_list_append(c->browse_callbacks, cb);
589 }
591 void
592 mpdclient_remove_browse_callback(mpdclient_t *c, mpdc_list_cb_t cb)
593 {
594 c->browse_callbacks = g_list_remove(c->browse_callbacks, cb);
595 }
597 void
598 mpdclient_install_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
599 {
600 c->error_callbacks = g_list_append(c->error_callbacks, cb);
601 }
603 void
604 mpdclient_remove_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
605 {
606 c->error_callbacks = g_list_remove(c->error_callbacks, cb);
607 }
609 /****************************************************************************/
610 /*** Playlist managment functions *******************************************/
611 /****************************************************************************/
613 gint
614 mpdclient_playlist_free(mpdclient_playlist_t *playlist)
615 {
616 GList *list = g_list_first(playlist->list);
618 while(list)
619 {
620 mpd_Song *song = (mpd_Song *) list->data;
621 mpd_freeSong(song);
622 list=list->next;
623 }
624 g_list_free(playlist->list);
625 memset(playlist, 0, sizeof(mpdclient_playlist_t));
626 return 0;
627 }
629 /* update playlist */
630 gint
631 mpdclient_playlist_update(mpdclient_t *c)
632 {
633 mpd_InfoEntity *entity;
635 D("mpdclient_playlist_update() [%lld]\n", c->status->playlist);
637 if( MPD_ERROR(c) )
638 return -1;
640 if( c->playlist.list )
641 mpdclient_playlist_free(&c->playlist);
643 mpd_sendPlaylistInfoCommand(c->connection,-1);
644 while( (entity=mpd_getNextInfoEntity(c->connection)) )
645 {
646 if(entity->type==MPD_INFO_ENTITY_TYPE_SONG)
647 {
648 mpd_Song *song = mpd_songDup(entity->info.song);
650 c->playlist.list = g_list_append(c->playlist.list, (gpointer) song);
651 c->playlist.length++;
652 }
653 mpd_freeInfoEntity(entity);
654 }
655 c->playlist.id = c->status->playlist;
656 c->song = NULL;
657 c->playlist.updated = TRUE;
659 /* call playlist updated callbacks */
660 mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
662 return mpdclient_finish_command(c);
663 }
665 #ifdef ENABLE_PLCHANGES
667 gint
668 mpdclient_compare_songs(gconstpointer a, gconstpointer b)
669 {
670 mpd_Song *song1 = (mpd_Song *) a;
671 mpd_Song *song2 = (mpd_Song *) b;
673 return song1->pos - song2->pos;
674 }
678 /* update playlist (plchanges) */
679 gint
680 mpdclient_playlist_update_changes(mpdclient_t *c)
681 {
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 {
694 mpd_Song *song = entity->info.song;
696 if( song->pos < c->playlist.length )
697 {
698 GList *item = g_list_nth(c->playlist.list, song->pos);
700 /* update song */
701 D("updating pos:%d, id=%d [%p] - %s\n",
702 song->pos, song->id, item, song->file);
703 mpd_freeSong((mpd_Song *) item->data);
704 item->data = mpd_songDup(song);
705 }
706 else
707 {
708 /* add a new song */
709 D("adding song at pos %d\n", song->pos);
710 c->playlist.list = g_list_append(c->playlist.list,
711 (gpointer) mpd_songDup(song));
712 }
714 }
716 /* remove trailing songs */
717 while( c->status->playlistLength < c->playlist.length )
718 {
719 GList *item = g_list_last(c->playlist.list);
721 /* Remove the last playlist entry */
722 D("removing song at pos %d\n", ((mpd_Song *) item->data)->pos);
723 mpd_freeSong((mpd_Song *) item->data);
724 c->playlist.list = g_list_delete_link(c->playlist.list, item);
725 c->playlist.length = g_list_length(c->playlist.list);
726 }
728 c->song = NULL;
729 c->playlist.id = c->status->playlist;
730 c->playlist.updated = TRUE;
731 c->playlist.length = g_list_length(c->playlist.list);
733 mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
735 return 0;
736 }
738 #else
739 gint
740 mpdclient_playlist_update_changes(mpdclient_t *c)
741 {
742 return mpdclient_playlist_update(c);
743 }
744 #endif
746 mpd_Song *
747 playlist_get_song(mpdclient_t *c, gint index)
748 {
749 return (mpd_Song *) g_list_nth_data(c->playlist.list, index);
750 }
752 GList *
753 playlist_lookup(mpdclient_t *c, int id)
754 {
755 GList *list = g_list_first(c->playlist.list);
757 while( list )
758 {
759 mpd_Song *song = (mpd_Song *) list->data;
760 if( song->id == id )
761 return list;
762 list=list->next;
763 }
764 return NULL;
765 }
767 mpd_Song *
768 playlist_lookup_song(mpdclient_t *c, gint id)
769 {
770 GList *list = c->playlist.list;
772 while( list )
773 {
774 mpd_Song *song = (mpd_Song *) list->data;
775 if( song->id == id )
776 return song;
777 list=list->next;
778 }
779 return NULL;
780 }
782 gint
783 playlist_get_index(mpdclient_t *c, mpd_Song *song)
784 {
785 return g_list_index(c->playlist.list, song);
786 }
788 gint
789 playlist_get_index_from_id(mpdclient_t *c, gint id)
790 {
791 return g_list_index(c->playlist.list, playlist_lookup_song(c, id));
792 }
794 gint
795 playlist_get_index_from_file(mpdclient_t *c, gchar *filename)
796 {
797 GList *list = c->playlist.list;
798 gint i=0;
800 while( list )
801 {
802 mpd_Song *song = (mpd_Song *) list->data;
803 if( strcmp(song->file, filename ) == 0 )
804 return i;
805 list=list->next;
806 i++;
807 }
808 return -1;
809 }
812 /****************************************************************************/
813 /*** Filelist functions *****************************************************/
814 /****************************************************************************/
816 mpdclient_filelist_t *
817 mpdclient_filelist_free(mpdclient_filelist_t *filelist)
818 {
819 GList *list = g_list_first(filelist->list);
821 D("mpdclient_filelist_free()\n");
822 while( list!=NULL )
823 {
824 filelist_entry_t *entry = list->data;
826 if( entry->entity )
827 mpd_freeInfoEntity(entry->entity);
828 g_free(entry);
829 list=list->next;
830 }
831 g_list_free(filelist->list);
832 g_free(filelist->path);
833 filelist->path = NULL;
834 filelist->list = NULL;
835 filelist->length = 0;
836 g_free(filelist);
838 return NULL;
839 }
842 mpdclient_filelist_t *
843 mpdclient_filelist_get(mpdclient_t *c, gchar *path)
844 {
845 mpdclient_filelist_t *filelist;
846 mpd_InfoEntity *entity;
847 gchar *path_utf8 = locale_to_utf8(path);
849 D("mpdclient_filelist_get(%s)\n", path);
850 mpd_sendLsInfoCommand(c->connection, path_utf8);
851 filelist = g_malloc0(sizeof(mpdclient_filelist_t));
852 if( path && path[0] && strcmp(path, "/") )
853 {
854 /* add a dummy entry for ./.. */
855 filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
856 entry->entity = NULL;
857 filelist->list = g_list_append(filelist->list, (gpointer) entry);
858 filelist->length++;
859 }
861 while( (entity=mpd_getNextInfoEntity(c->connection)) )
862 {
863 filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
865 entry->entity = entity;
866 filelist->list = g_list_append(filelist->list, (gpointer) entry);
867 filelist->length++;
868 }
870 if( mpdclient_finish_command(c) )
871 {
872 g_free(path_utf8);
873 return mpdclient_filelist_free(filelist);
874 }
876 g_free(path_utf8);
877 filelist->path = g_strdup(path);
878 filelist->updated = TRUE;
880 return filelist;
881 }
883 mpdclient_filelist_t *
884 mpdclient_filelist_update(mpdclient_t *c, mpdclient_filelist_t *filelist)
885 {
886 if( filelist != NULL )
887 {
888 gchar *path = g_strdup(filelist->path);
890 filelist = mpdclient_filelist_free(filelist);
891 filelist = mpdclient_filelist_get(c, path);
892 g_free(path);
893 return filelist;
894 }
895 return NULL;
896 }
898 filelist_entry_t *
899 mpdclient_filelist_find_song(mpdclient_filelist_t *fl, mpd_Song *song)
900 {
901 GList *list = g_list_first(fl->list);
903 while( list && song)
904 {
905 filelist_entry_t *entry = list->data;
906 mpd_InfoEntity *entity = entry->entity;
908 if( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG )
909 {
910 mpd_Song *song2 = entity->info.song;
912 if( strcmp(song->file, song2->file) == 0 )
913 {
914 return entry;
915 }
916 }
917 list = list->next;
918 }
919 return NULL;
920 }