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 playlist_init(&c->playlist);
168 return c;
169 }
171 void
172 mpdclient_free(mpdclient_t *c)
173 {
174 mpdclient_disconnect(c);
176 mpdclient_playlist_free(&c->playlist);
178 g_list_free(c->error_callbacks);
179 g_list_free(c->playlist_callbacks);
180 g_list_free(c->browse_callbacks);
181 g_free(c);
182 }
184 gint
185 mpdclient_disconnect(mpdclient_t *c)
186 {
187 if (c->connection)
188 mpd_closeConnection(c->connection);
189 c->connection = NULL;
191 if (c->status)
192 mpd_freeStatus(c->status);
193 c->status = NULL;
195 playlist_clear(&c->playlist);
197 if (c->song)
198 c->song = NULL;
200 return 0;
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 (playlist_is_empty(&c->playlist))
257 retval = mpdclient_playlist_update_changes(c);
258 else
259 retval = mpdclient_playlist_update(c);
260 }
262 /* update the current song */
263 if (!c->song || c->status->songid != c->song->id) {
264 c->song = playlist_get_song(c, c->status->song);
265 }
267 c->need_update = FALSE;
269 return retval;
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 playlist_append(&c->playlist, song);
424 /* increment the playlist id, so we dont retrives a new playlist */
425 c->playlist.id++;
427 /* call playlist updated callback */
428 mpdclient_playlist_callback(c, PLAYLIST_EVENT_ADD, (gpointer) song);
429 #else
430 c->need_update = TRUE;
431 #endif
433 return 0;
434 }
436 gint
437 mpdclient_cmd_delete(mpdclient_t *c, gint idx)
438 {
439 gint retval = 0;
440 struct mpd_song *song;
442 if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
443 return -1;
445 song = playlist_get(&c->playlist, idx);
447 /* send the delete command to mpd */
448 #ifdef ENABLE_SONG_ID
449 D("Delete id:%d\n", song->id);
450 mpd_sendDeleteIdCommand(c->connection, song->id);
451 #else
452 mpd_sendDeleteCommand(c->connection, idx);
453 #endif
454 if( (retval=mpdclient_finish_command(c)) )
455 return retval;
457 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
458 /* increment the playlist id, so we dont retrive a new playlist */
459 c->playlist.id++;
461 /* remove the song from the playlist */
462 playlist_remove(&c->playlist, idx);
464 /* call playlist updated callback */
465 mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
467 /* remove references to the song */
468 if (c->song == song) {
469 c->song = NULL;
470 c->need_update = TRUE;
471 }
473 #else
474 c->need_update = TRUE;
475 #endif
477 return 0;
478 }
480 gint
481 mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index)
482 {
483 gint n;
484 struct mpd_song *song1, *song2;
486 if (old_index == new_index || new_index < 0 ||
487 (guint)new_index >= c->playlist.list->len)
488 return -1;
490 song1 = playlist_get(&c->playlist, old_index);
491 song2 = playlist_get(&c->playlist, new_index);
493 /* send the move command to mpd */
494 #ifdef ENABLE_SONG_ID
495 D("Swapping id:%d with id:%d\n", song1->id, song2->id);
496 mpd_sendSwapIdCommand(c->connection, song1->id, song2->id);
497 #else
498 D("Moving index %d to id:%d\n", old_index, new_index);
499 mpd_sendMoveCommand(c->connection, old_index, new_index);
500 #endif
501 if( (n=mpdclient_finish_command(c)) )
502 return n;
504 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
505 /* update the playlist */
506 playlist_swap(&c->playlist, old_index, new_index);
508 /* increment the playlist id, so we dont retrives a new playlist */
509 c->playlist.id++;
511 #else
512 c->need_update = TRUE;
513 #endif
515 /* call playlist updated callback */
516 D("move> new_index=%d, old_index=%d\n", new_index, old_index);
517 mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index);
519 return 0;
520 }
522 gint
523 mpdclient_cmd_save_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
524 {
525 gint retval = 0;
527 mpd_sendSaveCommand(c->connection, filename_utf8);
528 if( (retval=mpdclient_finish_command(c)) == 0 )
529 mpdclient_browse_callback(c, BROWSE_PLAYLIST_SAVED, NULL);
530 return retval;
531 }
533 gint
534 mpdclient_cmd_save_playlist(mpdclient_t *c, gchar *filename)
535 {
536 gint retval = 0;
537 gchar *filename_utf8 = locale_to_utf8(filename);
539 retval = mpdclient_cmd_save_playlist_utf8(c, filename);
540 g_free(filename_utf8);
541 return retval;
542 }
544 gint
545 mpdclient_cmd_load_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
546 {
547 mpd_sendLoadCommand(c->connection, filename_utf8);
548 c->need_update = TRUE;
549 return mpdclient_finish_command(c);
550 }
552 gint
553 mpdclient_cmd_delete_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
554 {
555 gint retval = 0;
557 mpd_sendRmCommand(c->connection, filename_utf8);
558 if( (retval=mpdclient_finish_command(c)) == 0 )
559 mpdclient_browse_callback(c, BROWSE_PLAYLIST_DELETED, NULL);
560 return retval;
561 }
563 gint
564 mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename)
565 {
566 gint retval = 0;
567 gchar *filename_utf8 = locale_to_utf8(filename);
569 retval = mpdclient_cmd_delete_playlist_utf8(c, filename_utf8);
570 g_free(filename_utf8);
571 return retval;
572 }
575 /****************************************************************************/
576 /*** Callback managment functions *******************************************/
577 /****************************************************************************/
578 static void
579 do_list_callbacks(mpdclient_t *c, GList *list, gint event, gpointer data)
580 {
581 while(list)
582 {
583 mpdc_list_cb_t fn = list->data;
585 fn(c, event, data);
586 list=list->next;
587 }
588 }
590 void
591 mpdclient_playlist_callback(mpdclient_t *c, int event, gpointer data)
592 {
593 do_list_callbacks(c, c->playlist_callbacks, event, data);
594 }
596 void
597 mpdclient_install_playlist_callback(mpdclient_t *c,mpdc_list_cb_t cb)
598 {
599 c->playlist_callbacks = g_list_append(c->playlist_callbacks, cb);
600 }
602 void
603 mpdclient_remove_playlist_callback(mpdclient_t *c, mpdc_list_cb_t cb)
604 {
605 c->playlist_callbacks = g_list_remove(c->playlist_callbacks, cb);
606 }
608 void
609 mpdclient_browse_callback(mpdclient_t *c, int event, gpointer data)
610 {
611 do_list_callbacks(c, c->browse_callbacks, event, data);
612 }
615 void
616 mpdclient_install_browse_callback(mpdclient_t *c,mpdc_list_cb_t cb)
617 {
618 c->browse_callbacks = g_list_append(c->browse_callbacks, cb);
619 }
621 void
622 mpdclient_remove_browse_callback(mpdclient_t *c, mpdc_list_cb_t cb)
623 {
624 c->browse_callbacks = g_list_remove(c->browse_callbacks, cb);
625 }
627 void
628 mpdclient_install_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
629 {
630 c->error_callbacks = g_list_append(c->error_callbacks, cb);
631 }
633 void
634 mpdclient_remove_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
635 {
636 c->error_callbacks = g_list_remove(c->error_callbacks, cb);
637 }
639 /****************************************************************************/
640 /*** Playlist managment functions *******************************************/
641 /****************************************************************************/
644 /* update playlist */
645 gint
646 mpdclient_playlist_update(mpdclient_t *c)
647 {
648 mpd_InfoEntity *entity;
650 D("mpdclient_playlist_update() [%lld]\n", c->status->playlist);
652 if (MPD_ERROR(c))
653 return -1;
655 playlist_clear(&c->playlist);
657 mpd_sendPlaylistInfoCommand(c->connection,-1);
658 while ((entity = mpd_getNextInfoEntity(c->connection))) {
659 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
660 playlist_append(&c->playlist, entity->info.song);
662 mpd_freeInfoEntity(entity);
663 }
665 c->playlist.id = c->status->playlist;
666 c->song = NULL;
667 c->playlist.updated = TRUE;
669 /* call playlist updated callbacks */
670 mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
672 return mpdclient_finish_command(c);
673 }
675 #ifdef ENABLE_PLCHANGES
677 /* update playlist (plchanges) */
678 gint
679 mpdclient_playlist_update_changes(mpdclient_t *c)
680 {
681 mpd_InfoEntity *entity;
683 D("mpdclient_playlist_update_changes() [%lld -> %lld]\n",
684 c->status->playlist, c->playlist.id);
686 if (MPD_ERROR(c))
687 return -1;
689 mpd_sendPlChangesCommand(c->connection, c->playlist.id);
691 while ((entity = mpd_getNextInfoEntity(c->connection)) != NULL) {
692 struct mpd_song *song = entity->info.song;
694 if (song->pos >= 0 && (guint)song->pos < c->playlist.list->len) {
695 /* update song */
696 D("updating pos:%d, id=%d - %s\n",
697 song->pos, song->id, song->file);
698 playlist_replace(&c->playlist, song->pos, song);
699 } else {
700 /* add a new song */
701 D("adding song at pos %d\n", song->pos);
702 playlist_append(&c->playlist, song);
703 }
705 mpd_freeInfoEntity(entity);
706 }
708 /* remove trailing songs */
709 while ((guint)c->status->playlistLength < c->playlist.list->len) {
710 guint pos = c->playlist.list->len - 1;
712 /* Remove the last playlist entry */
713 D("removing song at pos %d\n", pos);
714 playlist_remove(&c->playlist, pos);
715 }
717 c->song = NULL;
718 c->playlist.id = c->status->playlist;
719 c->playlist.updated = TRUE;
721 mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
723 return 0;
724 }
726 #else
727 gint
728 mpdclient_playlist_update_changes(mpdclient_t *c)
729 {
730 return mpdclient_playlist_update(c);
731 }
732 #endif
735 /****************************************************************************/
736 /*** Filelist functions *****************************************************/
737 /****************************************************************************/
739 void
740 mpdclient_filelist_free(mpdclient_filelist_t *filelist)
741 {
742 GList *list = g_list_first(filelist->list);
744 D("mpdclient_filelist_free()\n");
745 if (list == NULL)
746 return;
747 while (list != NULL) {
748 filelist_entry_t *entry = list->data;
750 if (entry->entity)
751 mpd_freeInfoEntity(entry->entity);
752 g_free(entry);
753 list=list->next;
754 }
755 g_list_free(filelist->list);
756 g_free(filelist->path);
757 filelist->path = NULL;
758 filelist->list = NULL;
759 filelist->length = 0;
760 g_free(filelist);
761 }
764 mpdclient_filelist_t *
765 mpdclient_filelist_get(mpdclient_t *c, const gchar *path)
766 {
767 mpdclient_filelist_t *filelist;
768 mpd_InfoEntity *entity;
769 gchar *path_utf8 = locale_to_utf8(path);
770 gboolean has_dirs_only = TRUE;
772 D("mpdclient_filelist_get(%s)\n", path);
773 mpd_sendLsInfoCommand(c->connection, path_utf8);
774 filelist = g_malloc0(sizeof(mpdclient_filelist_t));
775 if (path && path[0] && strcmp(path, "/")) {
776 /* add a dummy entry for ./.. */
777 filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
778 entry->entity = NULL;
779 filelist->list = g_list_append(filelist->list, entry);
780 filelist->length++;
781 }
783 while ((entity=mpd_getNextInfoEntity(c->connection))) {
784 filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
786 entry->entity = entity;
787 filelist->list = g_list_append(filelist->list, entry);
788 filelist->length++;
790 if (has_dirs_only && entity->type != MPD_INFO_ENTITY_TYPE_DIRECTORY) {
791 has_dirs_only = FALSE;
792 }
793 }
795 /* If there's an error, ignore it. We'll return an empty filelist. */
796 mpdclient_finish_command(c);
798 g_free(path_utf8);
799 filelist->path = g_strdup(path);
800 filelist->updated = TRUE;
802 // If there are only directory entities in the filelist, we sort it
803 if (has_dirs_only) {
804 D("mpdclient_filelist_get: only dirs; sorting!\n");
805 filelist->list = g_list_sort(filelist->list, compare_filelistentry_dir);
806 }
808 return filelist;
809 }
811 mpdclient_filelist_t *
812 mpdclient_filelist_search_utf8(mpdclient_t *c,
813 int exact_match,
814 int table,
815 gchar *filter_utf8)
816 {
817 mpdclient_filelist_t *filelist;
818 mpd_InfoEntity *entity;
820 D("mpdclient_filelist_search(%s)\n", filter_utf8);
821 if (exact_match)
822 mpd_sendFindCommand(c->connection, table, filter_utf8);
823 else
824 mpd_sendSearchCommand(c->connection, table, filter_utf8);
825 filelist = g_malloc0(sizeof(mpdclient_filelist_t));
827 while ((entity=mpd_getNextInfoEntity(c->connection))) {
828 filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
830 entry->entity = entity;
831 filelist->list = g_list_append(filelist->list, entry);
832 filelist->length++;
833 }
835 if (mpdclient_finish_command(c)) {
836 mpdclient_filelist_free(filelist);
837 return NULL;
838 }
840 filelist->updated = TRUE;
841 return filelist;
842 }
845 mpdclient_filelist_t *
846 mpdclient_filelist_search(mpdclient_t *c,
847 int exact_match,
848 int table,
849 gchar *_filter)
850 {
851 mpdclient_filelist_t *filelist;
852 gchar *filter_utf8 = locale_to_utf8(_filter);
854 D("mpdclient_filelist_search(%s)\n", _filter);
855 filelist = mpdclient_filelist_search_utf8(c, exact_match, table,
856 filter_utf8);
857 g_free(filter_utf8);
859 return filelist;
860 }
862 mpdclient_filelist_t *
863 mpdclient_filelist_update(mpdclient_t *c, mpdclient_filelist_t *filelist)
864 {
865 if( filelist != NULL )
866 {
867 gchar *path = g_strdup(filelist->path);
869 mpdclient_filelist_free(filelist);
870 filelist = mpdclient_filelist_get(c, path);
871 g_free(path);
872 return filelist;
873 }
874 return NULL;
875 }
877 filelist_entry_t *
878 mpdclient_filelist_find_song(mpdclient_filelist_t *fl, struct mpd_song *song)
879 {
880 GList *list = g_list_first(fl->list);
882 while( list && song)
883 {
884 filelist_entry_t *entry = list->data;
885 mpd_InfoEntity *entity = entry->entity;
887 if( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG )
888 {
889 struct mpd_song *song2 = entity->info.song;
891 if( strcmp(song->file, song2->file) == 0 )
892 {
893 return entry;
894 }
895 }
896 list = list->next;
897 }
898 return NULL;
899 }
901 int
902 mpdclient_filelist_add_all(mpdclient_t *c, mpdclient_filelist_t *fl)
903 {
904 GList *list = g_list_first(fl->list);
906 if( fl->list==NULL || fl->length<1 )
907 return 0;
909 mpd_sendCommandListBegin(c->connection);
910 while( list )
911 {
912 filelist_entry_t *entry = list->data;
913 mpd_InfoEntity *entity = entry->entity;
915 if( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG )
916 {
917 struct mpd_song *song = entity->info.song;
919 mpd_sendAddCommand(c->connection, song->file);
920 }
921 list = list->next;
922 }
923 mpd_sendCommandListEnd(c->connection);
924 return mpdclient_finish_command(c);
925 }
934 GList *
935 mpdclient_get_artists_utf8(mpdclient_t *c)
936 {
937 gchar *str = NULL;
938 GList *list = NULL;
940 D("mpdclient_get_artists()\n");
941 mpd_sendListCommand(c->connection, MPD_TABLE_ARTIST, NULL);
942 while( (str=mpd_getNextArtist(c->connection)) )
943 {
944 list = g_list_append(list, (gpointer) str);
945 }
946 if( mpdclient_finish_command(c) )
947 {
948 return string_list_free(list);
949 }
951 return list;
952 }
954 GList *
955 mpdclient_get_albums_utf8(mpdclient_t *c, gchar *artist_utf8)
956 {
957 gchar *str = NULL;
958 GList *list = NULL;
960 D("mpdclient_get_albums(%s)\n", artist_utf8);
961 mpd_sendListCommand(c->connection, MPD_TABLE_ALBUM, artist_utf8);
962 while( (str=mpd_getNextAlbum(c->connection)) )
963 {
964 list = g_list_append(list, (gpointer) str);
965 }
966 if( mpdclient_finish_command(c) )
967 {
968 return string_list_free(list);
969 }
971 return list;
972 }