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)
100 {
101 GList *list = c->error_callbacks;
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;
115 }
117 #ifndef NDEBUG
118 // Unused ath the moment
119 /*
120 #include "strfsong.h"
122 static gchar *
123 get_song_name(mpd_Song *song)
124 {
125 static gchar name[256];
127 strfsong(name, 256, "[%artist% - ]%title%|%file%", song);
128 return name;
129 }
130 */
131 #endif
133 /****************************************************************************/
134 /*** mpdclient functions ****************************************************/
135 /****************************************************************************/
137 gint
138 mpdclient_finish_command(mpdclient_t *c)
139 {
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;
158 }
160 mpdclient_t *
161 mpdclient_new(void)
162 {
163 mpdclient_t *c;
165 c = g_malloc0(sizeof(mpdclient_t));
166 c->playlist.list = g_array_sized_new(FALSE, FALSE, sizeof(struct mpd_song *), 1024);
168 return c;
169 }
171 mpdclient_t *
172 mpdclient_free(mpdclient_t *c)
173 {
174 mpdclient_disconnect(c);
175 g_list_free(c->error_callbacks);
176 g_list_free(c->playlist_callbacks);
177 g_list_free(c->browse_callbacks);
178 g_free(c);
180 return NULL;
181 }
183 gint
184 mpdclient_disconnect(mpdclient_t *c)
185 {
186 if (c->connection)
187 mpd_closeConnection(c->connection);
188 c->connection = NULL;
190 if (c->status)
191 mpd_freeStatus(c->status);
192 c->status = NULL;
194 if (c->playlist.list)
195 mpdclient_playlist_free(&c->playlist);
197 if (c->song)
198 c->song = NULL;
200 return 0;
201 }
203 gint
204 mpdclient_connect(mpdclient_t *c,
205 gchar *host,
206 gint port,
207 gfloat _timeout,
208 gchar *password)
209 {
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;
230 }
232 gint
233 mpdclient_update(mpdclient_t *c)
234 {
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 (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 c->song = playlist_get_song(c, c->status->song);
265 }
267 c->need_update = FALSE;
269 return retval;
270 }
273 /****************************************************************************/
274 /*** MPD Commands **********************************************************/
275 /****************************************************************************/
277 gint
278 mpdclient_cmd_play(mpdclient_t *c, gint idx)
279 {
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);
293 }
295 gint
296 mpdclient_cmd_pause(mpdclient_t *c, gint value)
297 {
298 mpd_sendPauseCommand(c->connection, value);
299 return mpdclient_finish_command(c);
300 }
302 gint
303 mpdclient_cmd_stop(mpdclient_t *c)
304 {
305 mpd_sendStopCommand(c->connection);
306 return mpdclient_finish_command(c);
307 }
309 gint
310 mpdclient_cmd_next(mpdclient_t *c)
311 {
312 mpd_sendNextCommand(c->connection);
313 c->need_update = TRUE;
314 return mpdclient_finish_command(c);
315 }
317 gint
318 mpdclient_cmd_prev(mpdclient_t *c)
319 {
320 mpd_sendPrevCommand(c->connection);
321 c->need_update = TRUE;
322 return mpdclient_finish_command(c);
323 }
325 gint
326 mpdclient_cmd_seek(mpdclient_t *c, gint id, gint pos)
327 {
328 D("Seek id:%d\n", id);
329 mpd_sendSeekIdCommand(c->connection, id, pos);
330 return mpdclient_finish_command(c);
331 }
333 gint
334 mpdclient_cmd_shuffle(mpdclient_t *c)
335 {
336 mpd_sendShuffleCommand(c->connection);
337 c->need_update = TRUE;
338 return mpdclient_finish_command(c);
339 }
341 gint
342 mpdclient_cmd_clear(mpdclient_t *c)
343 {
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;
352 }
354 gint
355 mpdclient_cmd_repeat(mpdclient_t *c, gint value)
356 {
357 mpd_sendRepeatCommand(c->connection, value);
358 return mpdclient_finish_command(c);
359 }
361 gint
362 mpdclient_cmd_random(mpdclient_t *c, gint value)
363 {
364 mpd_sendRandomCommand(c->connection, value);
365 return mpdclient_finish_command(c);
366 }
368 gint
369 mpdclient_cmd_crossfade(mpdclient_t *c, gint value)
370 {
371 mpd_sendCrossfadeCommand(c->connection, value);
372 return mpdclient_finish_command(c);
373 }
375 gint
376 mpdclient_cmd_db_update_utf8(mpdclient_t *c, gchar *path)
377 {
378 mpd_sendUpdateCommand(c->connection, path ? path : "");
379 return mpdclient_finish_command(c);
380 }
382 gint
383 mpdclient_cmd_volume(mpdclient_t *c, gint value)
384 {
385 mpd_sendSetvolCommand(c->connection, value);
386 return mpdclient_finish_command(c);
387 }
389 gint
390 mpdclient_cmd_add_path_utf8(mpdclient_t *c, gchar *path_utf8)
391 {
392 mpd_sendAddCommand(c->connection, path_utf8);
393 return mpdclient_finish_command(c);
394 }
396 gint
397 mpdclient_cmd_add_path(mpdclient_t *c, gchar *path)
398 {
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;
405 }
407 gint
408 mpdclient_cmd_add(mpdclient_t *c, struct mpd_song *song)
409 {
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 c->playlist.list = g_list_append(c->playlist.list, mpd_songDup(song));
423 c->playlist.length++;
425 /* increment the playlist id, so we dont retrives a new playlist */
426 c->playlist.id++;
428 /* call playlist updated callback */
429 mpdclient_playlist_callback(c, PLAYLIST_EVENT_ADD, (gpointer) song);
430 #else
431 c->need_update = TRUE;
432 #endif
434 return 0;
435 }
437 gint
438 mpdclient_cmd_delete(mpdclient_t *c, gint idx)
439 {
440 gint retval = 0;
441 struct mpd_song *song = playlist_get_song(c, idx);
443 if( !song )
444 return -1;
446 /* send the delete command to mpd */
447 #ifdef ENABLE_SONG_ID
448 D("Delete id:%d\n", song->id);
449 mpd_sendDeleteIdCommand(c->connection, song->id);
450 #else
451 mpd_sendDeleteCommand(c->connection, idx);
452 #endif
453 if( (retval=mpdclient_finish_command(c)) )
454 return retval;
456 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
457 /* increment the playlist id, so we dont retrive a new playlist */
458 c->playlist.id++;
460 /* remove the song from the playlist */
461 g_array_remove_index(c->playlist.list, idx);
463 /* call playlist updated callback */
464 mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
466 /* remove references to the song */
467 if (c->song == song) {
468 c->song = NULL;
469 c->need_update = TRUE;
470 }
472 /* free song */
473 mpd_freeSong(song);
475 #else
476 c->need_update = TRUE;
477 #endif
479 return 0;
480 }
482 gint
483 mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index)
484 {
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_song(c, old_index);
493 song2 = playlist_get_song(c, 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 songs position field */
508 n = song1->pos;
509 song1->pos = song2->pos;
510 song2->pos = n;
511 /* update the array */
512 g_array_index(c->playlist.list, struct mpd_song *, old_index) = song2;
513 g_array_index(c->playlist.list, struct mpd_song *, new_index) = song1;
515 /* increment the playlist id, so we dont retrives a new playlist */
516 c->playlist.id++;
518 #else
519 c->need_update = TRUE;
520 #endif
522 /* call playlist updated callback */
523 D("move> new_index=%d, old_index=%d\n", new_index, old_index);
524 mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index);
526 return 0;
527 }
529 gint
530 mpdclient_cmd_save_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
531 {
532 gint retval = 0;
534 mpd_sendSaveCommand(c->connection, filename_utf8);
535 if( (retval=mpdclient_finish_command(c)) == 0 )
536 mpdclient_browse_callback(c, BROWSE_PLAYLIST_SAVED, NULL);
537 return retval;
538 }
540 gint
541 mpdclient_cmd_save_playlist(mpdclient_t *c, gchar *filename)
542 {
543 gint retval = 0;
544 gchar *filename_utf8 = locale_to_utf8(filename);
546 retval = mpdclient_cmd_save_playlist_utf8(c, filename);
547 g_free(filename_utf8);
548 return retval;
549 }
551 gint
552 mpdclient_cmd_load_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
553 {
554 mpd_sendLoadCommand(c->connection, filename_utf8);
555 c->need_update = TRUE;
556 return mpdclient_finish_command(c);
557 }
559 gint
560 mpdclient_cmd_delete_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
561 {
562 gint retval = 0;
564 mpd_sendRmCommand(c->connection, filename_utf8);
565 if( (retval=mpdclient_finish_command(c)) == 0 )
566 mpdclient_browse_callback(c, BROWSE_PLAYLIST_DELETED, NULL);
567 return retval;
568 }
570 gint
571 mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename)
572 {
573 gint retval = 0;
574 gchar *filename_utf8 = locale_to_utf8(filename);
576 retval = mpdclient_cmd_delete_playlist_utf8(c, filename_utf8);
577 g_free(filename_utf8);
578 return retval;
579 }
582 /****************************************************************************/
583 /*** Callback managment functions *******************************************/
584 /****************************************************************************/
585 static void
586 do_list_callbacks(mpdclient_t *c, GList *list, gint event, gpointer data)
587 {
588 while(list)
589 {
590 mpdc_list_cb_t fn = list->data;
592 fn(c, event, data);
593 list=list->next;
594 }
595 }
597 void
598 mpdclient_playlist_callback(mpdclient_t *c, int event, gpointer data)
599 {
600 do_list_callbacks(c, c->playlist_callbacks, event, data);
601 }
603 void
604 mpdclient_install_playlist_callback(mpdclient_t *c,mpdc_list_cb_t cb)
605 {
606 c->playlist_callbacks = g_list_append(c->playlist_callbacks, cb);
607 }
609 void
610 mpdclient_remove_playlist_callback(mpdclient_t *c, mpdc_list_cb_t cb)
611 {
612 c->playlist_callbacks = g_list_remove(c->playlist_callbacks, cb);
613 }
615 void
616 mpdclient_browse_callback(mpdclient_t *c, int event, gpointer data)
617 {
618 do_list_callbacks(c, c->browse_callbacks, event, data);
619 }
622 void
623 mpdclient_install_browse_callback(mpdclient_t *c,mpdc_list_cb_t cb)
624 {
625 c->browse_callbacks = g_list_append(c->browse_callbacks, cb);
626 }
628 void
629 mpdclient_remove_browse_callback(mpdclient_t *c, mpdc_list_cb_t cb)
630 {
631 c->browse_callbacks = g_list_remove(c->browse_callbacks, cb);
632 }
634 void
635 mpdclient_install_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
636 {
637 c->error_callbacks = g_list_append(c->error_callbacks, cb);
638 }
640 void
641 mpdclient_remove_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
642 {
643 c->error_callbacks = g_list_remove(c->error_callbacks, cb);
644 }
646 /****************************************************************************/
647 /*** Playlist managment functions *******************************************/
648 /****************************************************************************/
651 /* update playlist */
652 gint
653 mpdclient_playlist_update(mpdclient_t *c)
654 {
655 mpd_InfoEntity *entity;
657 D("mpdclient_playlist_update() [%lld]\n", c->status->playlist);
659 if (MPD_ERROR(c))
660 return -1;
662 if (c->playlist.list)
663 mpdclient_playlist_free(&c->playlist);
665 mpd_sendPlaylistInfoCommand(c->connection,-1);
666 while ((entity = mpd_getNextInfoEntity(c->connection))) {
667 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
668 struct mpd_song *song = mpd_songDup(entity->info.song);
669 g_array_append_val(c->playlist.list, song);
670 }
671 mpd_freeInfoEntity(entity);
672 }
674 c->playlist.id = c->status->playlist;
675 c->song = NULL;
676 c->playlist.updated = TRUE;
678 /* call playlist updated callbacks */
679 mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
681 return mpdclient_finish_command(c);
682 }
684 #ifdef ENABLE_PLCHANGES
686 /* update playlist (plchanges) */
687 gint
688 mpdclient_playlist_update_changes(mpdclient_t *c)
689 {
690 mpd_InfoEntity *entity;
692 D("mpdclient_playlist_update_changes() [%lld -> %lld]\n",
693 c->status->playlist, c->playlist.id);
695 if (MPD_ERROR(c))
696 return -1;
698 mpd_sendPlChangesCommand(c->connection, c->playlist.id);
700 while ((entity = mpd_getNextInfoEntity(c->connection)) != NULL) {
701 struct mpd_song *song = mpd_songDup(entity->info.song);
703 if (song->pos >= 0 && (guint)song->pos < c->playlist.list->len) {
704 /* update song */
705 D("updating pos:%d, id=%d - %s\n",
706 song->pos, song->id, song->file);
707 mpd_freeSong(g_array_index(c->playlist.list,
708 struct mpd_song *, song->pos));
709 g_array_index(c->playlist.list,
710 struct mpd_song *, song->pos) = song;
711 } else {
712 /* add a new song */
713 D("adding song at pos %d\n", song->pos);
714 g_array_append_val(c->playlist.list, song);
715 }
717 mpd_freeInfoEntity(entity);
718 }
720 /* remove trailing songs */
721 while ((guint)c->status->playlistLength < c->playlist.list->len) {
722 guint pos = c->playlist.list->len - 1;
723 struct mpd_song *song = g_array_index(c->playlist.list, struct mpd_song *, pos);
725 /* Remove the last playlist entry */
726 D("removing song at pos %d\n", pos);
727 mpd_freeSong(song);
728 g_array_remove_index(c->playlist.list, pos);
729 }
731 c->song = NULL;
732 c->playlist.id = c->status->playlist;
733 c->playlist.updated = TRUE;
735 mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
737 return 0;
738 }
740 #else
741 gint
742 mpdclient_playlist_update_changes(mpdclient_t *c)
743 {
744 return mpdclient_playlist_update(c);
745 }
746 #endif
749 /****************************************************************************/
750 /*** Filelist functions *****************************************************/
751 /****************************************************************************/
753 mpdclient_filelist_t *
754 mpdclient_filelist_free(mpdclient_filelist_t *filelist)
755 {
756 GList *list = g_list_first(filelist->list);
758 D("mpdclient_filelist_free()\n");
759 if( list == NULL )
760 return NULL;
761 while( list!=NULL )
762 {
763 filelist_entry_t *entry = list->data;
765 if( entry->entity )
766 mpd_freeInfoEntity(entry->entity);
767 g_free(entry);
768 list=list->next;
769 }
770 g_list_free(filelist->list);
771 g_free(filelist->path);
772 filelist->path = NULL;
773 filelist->list = NULL;
774 filelist->length = 0;
775 g_free(filelist);
777 return NULL;
778 }
781 mpdclient_filelist_t *
782 mpdclient_filelist_get(mpdclient_t *c, const gchar *path)
783 {
784 mpdclient_filelist_t *filelist;
785 mpd_InfoEntity *entity;
786 gchar *path_utf8 = locale_to_utf8(path);
787 gboolean has_dirs_only = TRUE;
789 D("mpdclient_filelist_get(%s)\n", path);
790 mpd_sendLsInfoCommand(c->connection, path_utf8);
791 filelist = g_malloc0(sizeof(mpdclient_filelist_t));
792 if( path && path[0] && strcmp(path, "/") )
793 {
794 /* add a dummy entry for ./.. */
795 filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
796 entry->entity = NULL;
797 filelist->list = g_list_append(filelist->list, (gpointer) entry);
798 filelist->length++;
799 }
801 while( (entity=mpd_getNextInfoEntity(c->connection)) )
802 {
803 filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
805 entry->entity = entity;
806 filelist->list = g_list_append(filelist->list, (gpointer) entry);
807 filelist->length++;
809 if (has_dirs_only && entity->type != MPD_INFO_ENTITY_TYPE_DIRECTORY)
810 {
811 has_dirs_only = FALSE;
812 }
813 }
815 /* If there's an error, ignore it. We'll return an empty filelist. */
816 mpdclient_finish_command(c);
818 g_free(path_utf8);
819 filelist->path = g_strdup(path);
820 filelist->updated = TRUE;
822 // If there are only directory entities in the filelist, we sort it
823 if (has_dirs_only)
824 {
825 D("mpdclient_filelist_get: only dirs; sorting!\n");
826 filelist->list = g_list_sort(filelist->list, compare_filelistentry_dir);
827 }
829 return filelist;
830 }
832 mpdclient_filelist_t *
833 mpdclient_filelist_search_utf8(mpdclient_t *c,
834 int exact_match,
835 int table,
836 gchar *filter_utf8)
837 {
838 mpdclient_filelist_t *filelist;
839 mpd_InfoEntity *entity;
841 D("mpdclient_filelist_search(%s)\n", filter_utf8);
842 if( exact_match )
843 mpd_sendFindCommand(c->connection, table, filter_utf8);
844 else
845 mpd_sendSearchCommand(c->connection, table, filter_utf8);
846 filelist = g_malloc0(sizeof(mpdclient_filelist_t));
848 while( (entity=mpd_getNextInfoEntity(c->connection)) )
849 {
850 filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
852 entry->entity = entity;
853 filelist->list = g_list_append(filelist->list, (gpointer) entry);
854 filelist->length++;
855 }
857 if( mpdclient_finish_command(c) )
858 return mpdclient_filelist_free(filelist);
860 filelist->updated = TRUE;
862 return filelist;
863 }
866 mpdclient_filelist_t *
867 mpdclient_filelist_search(mpdclient_t *c,
868 int exact_match,
869 int table,
870 gchar *_filter)
871 {
872 mpdclient_filelist_t *filelist;
873 gchar *filter_utf8 = locale_to_utf8(_filter);
875 D("mpdclient_filelist_search(%s)\n", _filter);
876 filelist = mpdclient_filelist_search_utf8(c, exact_match, table,
877 filter_utf8);
878 g_free(filter_utf8);
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, struct 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 struct 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 }
922 int
923 mpdclient_filelist_add_all(mpdclient_t *c, mpdclient_filelist_t *fl)
924 {
925 GList *list = g_list_first(fl->list);
927 if( fl->list==NULL || fl->length<1 )
928 return 0;
930 mpd_sendCommandListBegin(c->connection);
931 while( list )
932 {
933 filelist_entry_t *entry = list->data;
934 mpd_InfoEntity *entity = entry->entity;
936 if( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG )
937 {
938 struct mpd_song *song = entity->info.song;
940 mpd_sendAddCommand(c->connection, song->file);
941 }
942 list = list->next;
943 }
944 mpd_sendCommandListEnd(c->connection);
945 return mpdclient_finish_command(c);
946 }
955 GList *
956 mpdclient_get_artists_utf8(mpdclient_t *c)
957 {
958 gchar *str = NULL;
959 GList *list = NULL;
961 D("mpdclient_get_artists()\n");
962 mpd_sendListCommand(c->connection, MPD_TABLE_ARTIST, NULL);
963 while( (str=mpd_getNextArtist(c->connection)) )
964 {
965 list = g_list_append(list, (gpointer) str);
966 }
967 if( mpdclient_finish_command(c) )
968 {
969 return string_list_free(list);
970 }
972 return list;
973 }
975 GList *
976 mpdclient_get_albums_utf8(mpdclient_t *c, gchar *artist_utf8)
977 {
978 gchar *str = NULL;
979 GList *list = NULL;
981 D("mpdclient_get_albums(%s)\n", artist_utf8);
982 mpd_sendListCommand(c->connection, MPD_TABLE_ALBUM, artist_utf8);
983 while( (str=mpd_getNextAlbum(c->connection)) )
984 {
985 list = g_list_append(list, (gpointer) str);
986 }
987 if( mpdclient_finish_command(c) )
988 {
989 return string_list_free(list);
990 }
992 return list;
993 }