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_crop(mpdclient_t *c)
304 {
305 mpd_Status *status;
306 int length;
308 mpd_sendStatusCommand(c->connection);
309 status = mpd_getStatus(c->connection);
310 length = status->playlistLength - 1;
312 if (length <= 0) {
313 mpd_freeStatus(status);
314 } else if (status->state == 3 || status->state == 2) {
315 /* If playing or paused */
317 mpd_sendCommandListBegin( c->connection );
319 while (length >= 0) {
320 if (length != status->song)
321 mpd_sendDeleteCommand(c->connection, length);
323 length--;
324 }
326 mpd_sendCommandListEnd(c->connection);
327 mpd_freeStatus(status);
328 } else {
329 mpd_freeStatus(status);
330 }
332 return mpdclient_finish_command(c);
333 }
335 gint
336 mpdclient_cmd_stop(mpdclient_t *c)
337 {
338 mpd_sendStopCommand(c->connection);
339 return mpdclient_finish_command(c);
340 }
342 gint
343 mpdclient_cmd_next(mpdclient_t *c)
344 {
345 mpd_sendNextCommand(c->connection);
346 c->need_update = TRUE;
347 return mpdclient_finish_command(c);
348 }
350 gint
351 mpdclient_cmd_prev(mpdclient_t *c)
352 {
353 mpd_sendPrevCommand(c->connection);
354 c->need_update = TRUE;
355 return mpdclient_finish_command(c);
356 }
358 gint
359 mpdclient_cmd_seek(mpdclient_t *c, gint id, gint pos)
360 {
361 D("Seek id:%d\n", id);
362 mpd_sendSeekIdCommand(c->connection, id, pos);
363 return mpdclient_finish_command(c);
364 }
366 gint
367 mpdclient_cmd_shuffle(mpdclient_t *c)
368 {
369 mpd_sendShuffleCommand(c->connection);
370 c->need_update = TRUE;
371 return mpdclient_finish_command(c);
372 }
374 gint
375 mpdclient_cmd_clear(mpdclient_t *c)
376 {
377 gint retval = 0;
379 mpd_sendClearCommand(c->connection);
380 retval = mpdclient_finish_command(c);
381 /* call playlist updated callback */
382 mpdclient_playlist_callback(c, PLAYLIST_EVENT_CLEAR, NULL);
383 c->need_update = TRUE;
384 return retval;
385 }
387 gint
388 mpdclient_cmd_repeat(mpdclient_t *c, gint value)
389 {
390 mpd_sendRepeatCommand(c->connection, value);
391 return mpdclient_finish_command(c);
392 }
394 gint
395 mpdclient_cmd_random(mpdclient_t *c, gint value)
396 {
397 mpd_sendRandomCommand(c->connection, value);
398 return mpdclient_finish_command(c);
399 }
401 gint
402 mpdclient_cmd_crossfade(mpdclient_t *c, gint value)
403 {
404 mpd_sendCrossfadeCommand(c->connection, value);
405 return mpdclient_finish_command(c);
406 }
408 gint
409 mpdclient_cmd_db_update_utf8(mpdclient_t *c, gchar *path)
410 {
411 mpd_sendUpdateCommand(c->connection, path ? path : "");
412 return mpdclient_finish_command(c);
413 }
415 gint
416 mpdclient_cmd_volume(mpdclient_t *c, gint value)
417 {
418 mpd_sendSetvolCommand(c->connection, value);
419 return mpdclient_finish_command(c);
420 }
422 gint
423 mpdclient_cmd_add_path_utf8(mpdclient_t *c, gchar *path_utf8)
424 {
425 mpd_sendAddCommand(c->connection, path_utf8);
426 return mpdclient_finish_command(c);
427 }
429 gint
430 mpdclient_cmd_add_path(mpdclient_t *c, gchar *path)
431 {
432 gint retval;
433 gchar *path_utf8 = locale_to_utf8(path);
435 retval=mpdclient_cmd_add_path_utf8(c, path_utf8);
436 g_free(path_utf8);
437 return retval;
438 }
440 gint
441 mpdclient_cmd_add(mpdclient_t *c, struct mpd_song *song)
442 {
443 gint retval = 0;
445 if( !song || !song->file )
446 return -1;
448 /* send the add command to mpd */
449 mpd_sendAddCommand(c->connection, song->file);
450 if( (retval=mpdclient_finish_command(c)) )
451 return retval;
453 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD
454 /* add the song to playlist */
455 playlist_append(&c->playlist, song);
457 /* increment the playlist id, so we dont retrives a new playlist */
458 c->playlist.id++;
460 /* call playlist updated callback */
461 mpdclient_playlist_callback(c, PLAYLIST_EVENT_ADD, (gpointer) song);
462 #else
463 c->need_update = TRUE;
464 #endif
466 return 0;
467 }
469 gint
470 mpdclient_cmd_delete(mpdclient_t *c, gint idx)
471 {
472 gint retval = 0;
473 struct mpd_song *song;
475 if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
476 return -1;
478 song = playlist_get(&c->playlist, idx);
480 /* send the delete command to mpd */
481 #ifdef ENABLE_SONG_ID
482 D("Delete id:%d\n", song->id);
483 mpd_sendDeleteIdCommand(c->connection, song->id);
484 #else
485 mpd_sendDeleteCommand(c->connection, idx);
486 #endif
487 if( (retval=mpdclient_finish_command(c)) )
488 return retval;
490 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
491 /* increment the playlist id, so we dont retrive a new playlist */
492 c->playlist.id++;
494 /* remove the song from the playlist */
495 playlist_remove_reuse(&c->playlist, idx);
497 /* call playlist updated callback */
498 mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
500 /* remove references to the song */
501 if (c->song == song) {
502 c->song = NULL;
503 c->need_update = TRUE;
504 }
506 mpd_freeSong(song);
508 #else
509 c->need_update = TRUE;
510 #endif
512 return 0;
513 }
515 gint
516 mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index)
517 {
518 gint n;
519 struct mpd_song *song1, *song2;
521 if (old_index == new_index || new_index < 0 ||
522 (guint)new_index >= c->playlist.list->len)
523 return -1;
525 song1 = playlist_get(&c->playlist, old_index);
526 song2 = playlist_get(&c->playlist, new_index);
528 /* send the move command to mpd */
529 #ifdef ENABLE_SONG_ID
530 D("Swapping id:%d with id:%d\n", song1->id, song2->id);
531 mpd_sendSwapIdCommand(c->connection, song1->id, song2->id);
532 #else
533 D("Moving index %d to id:%d\n", old_index, new_index);
534 mpd_sendMoveCommand(c->connection, old_index, new_index);
535 #endif
536 if( (n=mpdclient_finish_command(c)) )
537 return n;
539 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
540 /* update the playlist */
541 playlist_swap(&c->playlist, old_index, new_index);
543 /* increment the playlist id, so we dont retrives a new playlist */
544 c->playlist.id++;
546 #else
547 c->need_update = TRUE;
548 #endif
550 /* call playlist updated callback */
551 D("move> new_index=%d, old_index=%d\n", new_index, old_index);
552 mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index);
554 return 0;
555 }
557 gint
558 mpdclient_cmd_save_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
559 {
560 gint retval = 0;
562 mpd_sendSaveCommand(c->connection, filename_utf8);
563 if( (retval=mpdclient_finish_command(c)) == 0 )
564 mpdclient_browse_callback(c, BROWSE_PLAYLIST_SAVED, NULL);
565 return retval;
566 }
568 gint
569 mpdclient_cmd_save_playlist(mpdclient_t *c, gchar *filename)
570 {
571 gint retval = 0;
572 gchar *filename_utf8 = locale_to_utf8(filename);
574 retval = mpdclient_cmd_save_playlist_utf8(c, filename);
575 g_free(filename_utf8);
576 return retval;
577 }
579 gint
580 mpdclient_cmd_load_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
581 {
582 mpd_sendLoadCommand(c->connection, filename_utf8);
583 c->need_update = TRUE;
584 return mpdclient_finish_command(c);
585 }
587 gint
588 mpdclient_cmd_delete_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
589 {
590 gint retval = 0;
592 mpd_sendRmCommand(c->connection, filename_utf8);
593 if( (retval=mpdclient_finish_command(c)) == 0 )
594 mpdclient_browse_callback(c, BROWSE_PLAYLIST_DELETED, NULL);
595 return retval;
596 }
598 gint
599 mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename)
600 {
601 gint retval = 0;
602 gchar *filename_utf8 = locale_to_utf8(filename);
604 retval = mpdclient_cmd_delete_playlist_utf8(c, filename_utf8);
605 g_free(filename_utf8);
606 return retval;
607 }
610 /****************************************************************************/
611 /*** Callback managment functions *******************************************/
612 /****************************************************************************/
613 static void
614 do_list_callbacks(mpdclient_t *c, GList *list, gint event, gpointer data)
615 {
616 while(list)
617 {
618 mpdc_list_cb_t fn = list->data;
620 fn(c, event, data);
621 list=list->next;
622 }
623 }
625 void
626 mpdclient_playlist_callback(mpdclient_t *c, int event, gpointer data)
627 {
628 do_list_callbacks(c, c->playlist_callbacks, event, data);
629 }
631 void
632 mpdclient_install_playlist_callback(mpdclient_t *c,mpdc_list_cb_t cb)
633 {
634 c->playlist_callbacks = g_list_append(c->playlist_callbacks, cb);
635 }
637 void
638 mpdclient_remove_playlist_callback(mpdclient_t *c, mpdc_list_cb_t cb)
639 {
640 c->playlist_callbacks = g_list_remove(c->playlist_callbacks, cb);
641 }
643 void
644 mpdclient_browse_callback(mpdclient_t *c, int event, gpointer data)
645 {
646 do_list_callbacks(c, c->browse_callbacks, event, data);
647 }
650 void
651 mpdclient_install_browse_callback(mpdclient_t *c,mpdc_list_cb_t cb)
652 {
653 c->browse_callbacks = g_list_append(c->browse_callbacks, cb);
654 }
656 void
657 mpdclient_remove_browse_callback(mpdclient_t *c, mpdc_list_cb_t cb)
658 {
659 c->browse_callbacks = g_list_remove(c->browse_callbacks, cb);
660 }
662 void
663 mpdclient_install_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
664 {
665 c->error_callbacks = g_list_append(c->error_callbacks, cb);
666 }
668 void
669 mpdclient_remove_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
670 {
671 c->error_callbacks = g_list_remove(c->error_callbacks, cb);
672 }
674 /****************************************************************************/
675 /*** Playlist managment functions *******************************************/
676 /****************************************************************************/
679 /* update playlist */
680 gint
681 mpdclient_playlist_update(mpdclient_t *c)
682 {
683 mpd_InfoEntity *entity;
685 D("mpdclient_playlist_update() [%lld]\n", c->status->playlist);
687 if (MPD_ERROR(c))
688 return -1;
690 playlist_clear(&c->playlist);
692 mpd_sendPlaylistInfoCommand(c->connection,-1);
693 while ((entity = mpd_getNextInfoEntity(c->connection))) {
694 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
695 playlist_append(&c->playlist, entity->info.song);
697 mpd_freeInfoEntity(entity);
698 }
700 c->playlist.id = c->status->playlist;
701 c->song = NULL;
703 /* call playlist updated callbacks */
704 mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
706 return mpdclient_finish_command(c);
707 }
709 #ifdef ENABLE_PLCHANGES
711 /* update playlist (plchanges) */
712 gint
713 mpdclient_playlist_update_changes(mpdclient_t *c)
714 {
715 mpd_InfoEntity *entity;
717 D("mpdclient_playlist_update_changes() [%lld -> %lld]\n",
718 c->status->playlist, c->playlist.id);
720 if (MPD_ERROR(c))
721 return -1;
723 mpd_sendPlChangesCommand(c->connection, c->playlist.id);
725 while ((entity = mpd_getNextInfoEntity(c->connection)) != NULL) {
726 struct mpd_song *song = entity->info.song;
728 if (song->pos >= 0 && (guint)song->pos < c->playlist.list->len) {
729 /* update song */
730 D("updating pos:%d, id=%d - %s\n",
731 song->pos, song->id, song->file);
732 playlist_replace(&c->playlist, song->pos, song);
733 } else {
734 /* add a new song */
735 D("adding song at pos %d\n", song->pos);
736 playlist_append(&c->playlist, song);
737 }
739 mpd_freeInfoEntity(entity);
740 }
742 /* remove trailing songs */
743 while ((guint)c->status->playlistLength < c->playlist.list->len) {
744 guint pos = c->playlist.list->len - 1;
746 /* Remove the last playlist entry */
747 D("removing song at pos %d\n", pos);
748 playlist_remove(&c->playlist, pos);
749 }
751 c->song = NULL;
752 c->playlist.id = c->status->playlist;
754 mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
756 return 0;
757 }
759 #else
760 gint
761 mpdclient_playlist_update_changes(mpdclient_t *c)
762 {
763 return mpdclient_playlist_update(c);
764 }
765 #endif
768 /****************************************************************************/
769 /*** Filelist functions *****************************************************/
770 /****************************************************************************/
772 mpdclient_filelist_t *
773 mpdclient_filelist_get(mpdclient_t *c, const gchar *path)
774 {
775 mpdclient_filelist_t *filelist;
776 mpd_InfoEntity *entity;
777 gchar *path_utf8 = locale_to_utf8(path);
778 gboolean has_dirs_only = TRUE;
780 D("mpdclient_filelist_get(%s)\n", path);
781 mpd_sendLsInfoCommand(c->connection, path_utf8);
782 filelist = filelist_new(path);
783 if (path && path[0] && strcmp(path, "/"))
784 /* add a dummy entry for ./.. */
785 filelist_append(filelist, NULL);
787 while ((entity=mpd_getNextInfoEntity(c->connection))) {
788 filelist_append(filelist, entity);
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->updated = TRUE;
801 // If there are only directory entities in the filelist, we sort it
802 if (has_dirs_only) {
803 D("mpdclient_filelist_get: only dirs; sorting!\n");
804 filelist_sort(filelist, compare_filelistentry_dir);
805 }
807 return filelist;
808 }
810 mpdclient_filelist_t *
811 mpdclient_filelist_search_utf8(mpdclient_t *c,
812 int exact_match,
813 int table,
814 gchar *filter_utf8)
815 {
816 mpdclient_filelist_t *filelist;
817 mpd_InfoEntity *entity;
819 D("mpdclient_filelist_search(%s)\n", filter_utf8);
820 if (exact_match)
821 mpd_sendFindCommand(c->connection, table, filter_utf8);
822 else
823 mpd_sendSearchCommand(c->connection, table, filter_utf8);
824 filelist = filelist_new(NULL);
826 while ((entity=mpd_getNextInfoEntity(c->connection)))
827 filelist_append(filelist, entity);
829 if (mpdclient_finish_command(c)) {
830 filelist_free(filelist);
831 return NULL;
832 }
834 filelist->updated = TRUE;
835 return filelist;
836 }
839 mpdclient_filelist_t *
840 mpdclient_filelist_search(mpdclient_t *c,
841 int exact_match,
842 int table,
843 gchar *_filter)
844 {
845 mpdclient_filelist_t *filelist;
846 gchar *filter_utf8 = locale_to_utf8(_filter);
848 D("mpdclient_filelist_search(%s)\n", _filter);
849 filelist = mpdclient_filelist_search_utf8(c, exact_match, table,
850 filter_utf8);
851 g_free(filter_utf8);
853 return filelist;
854 }
856 mpdclient_filelist_t *
857 mpdclient_filelist_update(mpdclient_t *c, mpdclient_filelist_t *filelist)
858 {
859 if( filelist != NULL )
860 {
861 gchar *path = g_strdup(filelist->path);
863 filelist_free(filelist);
864 filelist = mpdclient_filelist_get(c, path);
865 g_free(path);
866 return filelist;
867 }
868 return NULL;
869 }
871 int
872 mpdclient_filelist_add_all(mpdclient_t *c, mpdclient_filelist_t *fl)
873 {
874 guint i;
876 if (filelist_is_empty(fl))
877 return 0;
879 mpd_sendCommandListBegin(c->connection);
881 for (i = 0; i < filelist_length(fl); ++i) {
882 filelist_entry_t *entry = filelist_get(fl, i);
883 mpd_InfoEntity *entity = entry->entity;
885 if (entity && entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
886 struct mpd_song *song = entity->info.song;
888 mpd_sendAddCommand(c->connection, song->file);
889 }
890 }
892 mpd_sendCommandListEnd(c->connection);
893 return mpdclient_finish_command(c);
894 }
896 GList *
897 mpdclient_get_artists_utf8(mpdclient_t *c)
898 {
899 gchar *str = NULL;
900 GList *list = NULL;
902 D("mpdclient_get_artists()\n");
903 mpd_sendListCommand(c->connection, MPD_TABLE_ARTIST, NULL);
904 while( (str=mpd_getNextArtist(c->connection)) )
905 {
906 list = g_list_append(list, (gpointer) str);
907 }
908 if( mpdclient_finish_command(c) )
909 {
910 return string_list_free(list);
911 }
913 return list;
914 }
916 GList *
917 mpdclient_get_albums_utf8(mpdclient_t *c, gchar *artist_utf8)
918 {
919 gchar *str = NULL;
920 GList *list = NULL;
922 D("mpdclient_get_albums(%s)\n", artist_utf8);
923 mpd_sendListCommand(c->connection, MPD_TABLE_ALBUM, artist_utf8);
924 while( (str=mpd_getNextAlbum(c->connection)) )
925 {
926 list = g_list_append(list, (gpointer) str);
927 }
928 if( mpdclient_finish_command(c) )
929 {
930 return string_list_free(list);
931 }
933 return list;
934 }