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_reuse(&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 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(&c->playlist, old_index);
493 song2 = playlist_get(&c->playlist, 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 playlist */
508 playlist_swap(&c->playlist, old_index, new_index);
510 /* increment the playlist id, so we dont retrives a new playlist */
511 c->playlist.id++;
513 #else
514 c->need_update = TRUE;
515 #endif
517 /* call playlist updated callback */
518 D("move> new_index=%d, old_index=%d\n", new_index, old_index);
519 mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index);
521 return 0;
522 }
524 gint
525 mpdclient_cmd_save_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
526 {
527 gint retval = 0;
529 mpd_sendSaveCommand(c->connection, filename_utf8);
530 if( (retval=mpdclient_finish_command(c)) == 0 )
531 mpdclient_browse_callback(c, BROWSE_PLAYLIST_SAVED, NULL);
532 return retval;
533 }
535 gint
536 mpdclient_cmd_save_playlist(mpdclient_t *c, gchar *filename)
537 {
538 gint retval = 0;
539 gchar *filename_utf8 = locale_to_utf8(filename);
541 retval = mpdclient_cmd_save_playlist_utf8(c, filename);
542 g_free(filename_utf8);
543 return retval;
544 }
546 gint
547 mpdclient_cmd_load_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
548 {
549 mpd_sendLoadCommand(c->connection, filename_utf8);
550 c->need_update = TRUE;
551 return mpdclient_finish_command(c);
552 }
554 gint
555 mpdclient_cmd_delete_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
556 {
557 gint retval = 0;
559 mpd_sendRmCommand(c->connection, filename_utf8);
560 if( (retval=mpdclient_finish_command(c)) == 0 )
561 mpdclient_browse_callback(c, BROWSE_PLAYLIST_DELETED, NULL);
562 return retval;
563 }
565 gint
566 mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename)
567 {
568 gint retval = 0;
569 gchar *filename_utf8 = locale_to_utf8(filename);
571 retval = mpdclient_cmd_delete_playlist_utf8(c, filename_utf8);
572 g_free(filename_utf8);
573 return retval;
574 }
577 /****************************************************************************/
578 /*** Callback managment functions *******************************************/
579 /****************************************************************************/
580 static void
581 do_list_callbacks(mpdclient_t *c, GList *list, gint event, gpointer data)
582 {
583 while(list)
584 {
585 mpdc_list_cb_t fn = list->data;
587 fn(c, event, data);
588 list=list->next;
589 }
590 }
592 void
593 mpdclient_playlist_callback(mpdclient_t *c, int event, gpointer data)
594 {
595 do_list_callbacks(c, c->playlist_callbacks, event, data);
596 }
598 void
599 mpdclient_install_playlist_callback(mpdclient_t *c,mpdc_list_cb_t cb)
600 {
601 c->playlist_callbacks = g_list_append(c->playlist_callbacks, cb);
602 }
604 void
605 mpdclient_remove_playlist_callback(mpdclient_t *c, mpdc_list_cb_t cb)
606 {
607 c->playlist_callbacks = g_list_remove(c->playlist_callbacks, cb);
608 }
610 void
611 mpdclient_browse_callback(mpdclient_t *c, int event, gpointer data)
612 {
613 do_list_callbacks(c, c->browse_callbacks, event, data);
614 }
617 void
618 mpdclient_install_browse_callback(mpdclient_t *c,mpdc_list_cb_t cb)
619 {
620 c->browse_callbacks = g_list_append(c->browse_callbacks, cb);
621 }
623 void
624 mpdclient_remove_browse_callback(mpdclient_t *c, mpdc_list_cb_t cb)
625 {
626 c->browse_callbacks = g_list_remove(c->browse_callbacks, cb);
627 }
629 void
630 mpdclient_install_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
631 {
632 c->error_callbacks = g_list_append(c->error_callbacks, cb);
633 }
635 void
636 mpdclient_remove_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
637 {
638 c->error_callbacks = g_list_remove(c->error_callbacks, cb);
639 }
641 /****************************************************************************/
642 /*** Playlist managment functions *******************************************/
643 /****************************************************************************/
646 /* update playlist */
647 gint
648 mpdclient_playlist_update(mpdclient_t *c)
649 {
650 mpd_InfoEntity *entity;
652 D("mpdclient_playlist_update() [%lld]\n", c->status->playlist);
654 if (MPD_ERROR(c))
655 return -1;
657 playlist_clear(&c->playlist);
659 mpd_sendPlaylistInfoCommand(c->connection,-1);
660 while ((entity = mpd_getNextInfoEntity(c->connection))) {
661 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
662 playlist_append(&c->playlist, entity->info.song);
664 mpd_freeInfoEntity(entity);
665 }
667 c->playlist.id = c->status->playlist;
668 c->song = NULL;
670 /* call playlist updated callbacks */
671 mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
673 return mpdclient_finish_command(c);
674 }
676 #ifdef ENABLE_PLCHANGES
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 struct mpd_song *song = entity->info.song;
695 if (song->pos >= 0 && (guint)song->pos < c->playlist.list->len) {
696 /* update song */
697 D("updating pos:%d, id=%d - %s\n",
698 song->pos, song->id, song->file);
699 playlist_replace(&c->playlist, song->pos, song);
700 } else {
701 /* add a new song */
702 D("adding song at pos %d\n", song->pos);
703 playlist_append(&c->playlist, song);
704 }
706 mpd_freeInfoEntity(entity);
707 }
709 /* remove trailing songs */
710 while ((guint)c->status->playlistLength < c->playlist.list->len) {
711 guint pos = c->playlist.list->len - 1;
713 /* Remove the last playlist entry */
714 D("removing song at pos %d\n", pos);
715 playlist_remove(&c->playlist, pos);
716 }
718 c->song = NULL;
719 c->playlist.id = c->status->playlist;
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 filelist_entry_t *entry = list->data;
912 mpd_InfoEntity *entity = entry->entity;
914 if (entity && entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
915 struct mpd_song *song = entity->info.song;
917 mpd_sendAddCommand(c->connection, song->file);
918 }
920 list = list->next;
921 }
923 mpd_sendCommandListEnd(c->connection);
924 return mpdclient_finish_command(c);
925 }
927 GList *
928 mpdclient_get_artists_utf8(mpdclient_t *c)
929 {
930 gchar *str = NULL;
931 GList *list = NULL;
933 D("mpdclient_get_artists()\n");
934 mpd_sendListCommand(c->connection, MPD_TABLE_ARTIST, NULL);
935 while( (str=mpd_getNextArtist(c->connection)) )
936 {
937 list = g_list_append(list, (gpointer) str);
938 }
939 if( mpdclient_finish_command(c) )
940 {
941 return string_list_free(list);
942 }
944 return list;
945 }
947 GList *
948 mpdclient_get_albums_utf8(mpdclient_t *c, gchar *artist_utf8)
949 {
950 gchar *str = NULL;
951 GList *list = NULL;
953 D("mpdclient_get_albums(%s)\n", artist_utf8);
954 mpd_sendListCommand(c->connection, MPD_TABLE_ALBUM, artist_utf8);
955 while( (str=mpd_getNextAlbum(c->connection)) )
956 {
957 list = g_list_append(list, (gpointer) str);
958 }
959 if( mpdclient_finish_command(c) )
960 {
961 return string_list_free(list);
962 }
964 return list;
965 }