1 /*
2 * (c) 2004 by Kalle Wallin <kaw@linux.se>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 */
19 #include "mpdclient.h"
20 #include "screen_utils.h"
21 #include "config.h"
22 #include "ncmpc.h"
23 #include "support.h"
24 #include "options.h"
25 #include "strfsong.h"
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <time.h>
30 #include <string.h>
32 #undef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD /* broken with song id's */
33 #define ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
34 #define ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
35 #define ENABLE_SONG_ID
36 #define ENABLE_PLCHANGES
38 #define BUFSIZE 1024
40 #define MPD_ERROR(c) (c==NULL || c->connection==NULL || c->connection->error)
42 /* from utils.c */
43 extern GList *string_list_free(GList *string_list);
46 /* filelist sorting functions */
47 static gint
48 compare_filelistentry_dir(gconstpointer filelist_entry1,
49 gconstpointer filelist_entry2)
50 {
51 const mpd_InfoEntity *e1, *e2;
52 char *key1, *key2;
53 int n = 0;
55 e1 = ((const filelist_entry_t *)filelist_entry1)->entity;
56 e2 = ((const filelist_entry_t *)filelist_entry2)->entity;
58 if (e1 && e2 &&
59 e1->type == MPD_INFO_ENTITY_TYPE_DIRECTORY &&
60 e2->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
61 key1 = g_utf8_collate_key(e1->info.directory->path,-1);
62 key2 = g_utf8_collate_key(e2->info.directory->path,-1);
63 n = strcmp(key1,key2);
64 g_free(key1);
65 g_free(key2);
66 }
68 return n;
69 }
71 /* sort by list-format */
72 gint
73 compare_filelistentry_format(gconstpointer filelist_entry1,
74 gconstpointer filelist_entry2)
75 {
76 const mpd_InfoEntity *e1, *e2;
77 char key1[BUFSIZE], key2[BUFSIZE];
78 int n = 0;
80 e1 = ((const filelist_entry_t *)filelist_entry1)->entity;
81 e2 = ((const filelist_entry_t *)filelist_entry2)->entity;
83 if (e1 && e2 &&
84 e1->type == MPD_INFO_ENTITY_TYPE_SONG &&
85 e2->type == MPD_INFO_ENTITY_TYPE_SONG) {
86 strfsong(key1, BUFSIZE, LIST_FORMAT, e1->info.song);
87 strfsong(key2, BUFSIZE, LIST_FORMAT, e2->info.song);
88 n = strcmp(key1,key2);
89 }
91 return n;
92 }
95 /* Error callbacks */
96 static gint
97 error_cb(mpdclient_t *c, gint error, gchar *msg)
98 {
99 GList *list = c->error_callbacks;
101 if (list == NULL)
102 fprintf(stderr, "error [%d]: %s\n", (error & 0xFF), msg);
104 while (list) {
105 mpdc_error_cb_t cb = list->data;
106 if (cb)
107 cb(c, error, msg);
108 list = list->next;
109 }
111 mpd_clearError(c->connection);
112 return error;
113 }
116 /****************************************************************************/
117 /*** mpdclient functions ****************************************************/
118 /****************************************************************************/
120 gint
121 mpdclient_finish_command(mpdclient_t *c)
122 {
123 mpd_finishCommand(c->connection);
125 if (c->connection->error) {
126 gint error = c->connection->error;
127 gchar *msg;
129 if (error == MPD_ERROR_ACK &&
130 c->connection->errorCode == MPD_ACK_ERROR_PERMISSION &&
131 screen_auth(c) == 0)
132 return 0;
134 if (error == MPD_ERROR_ACK)
135 error = error | (c->connection->errorCode << 8);
137 msg = locale_to_utf8(c->connection->errorStr);
138 error_cb(c, error, msg);
139 g_free(msg);
140 return error;
141 }
143 return 0;
144 }
146 mpdclient_t *
147 mpdclient_new(void)
148 {
149 mpdclient_t *c;
151 c = g_malloc0(sizeof(mpdclient_t));
152 playlist_init(&c->playlist);
154 return c;
155 }
157 void
158 mpdclient_free(mpdclient_t *c)
159 {
160 mpdclient_disconnect(c);
162 mpdclient_playlist_free(&c->playlist);
164 g_list_free(c->error_callbacks);
165 g_list_free(c->playlist_callbacks);
166 g_list_free(c->browse_callbacks);
167 g_free(c);
168 }
170 gint
171 mpdclient_disconnect(mpdclient_t *c)
172 {
173 if (c->connection)
174 mpd_closeConnection(c->connection);
175 c->connection = NULL;
177 if (c->status)
178 mpd_freeStatus(c->status);
179 c->status = NULL;
181 playlist_clear(&c->playlist);
183 if (c->song)
184 c->song = NULL;
186 return 0;
187 }
189 gint
190 mpdclient_connect(mpdclient_t *c,
191 gchar *host,
192 gint port,
193 gfloat _timeout,
194 gchar *password)
195 {
196 gint retval = 0;
198 /* close any open connection */
199 if( c->connection )
200 mpdclient_disconnect(c);
202 /* connect to MPD */
203 c->connection = mpd_newConnection(host, port, _timeout);
204 if( c->connection->error )
205 return error_cb(c, c->connection->error,
206 c->connection->errorStr);
208 /* send password */
209 if( password ) {
210 mpd_sendPasswordCommand(c->connection, password);
211 retval = mpdclient_finish_command(c);
212 }
213 c->need_update = TRUE;
215 return retval;
216 }
218 gint
219 mpdclient_update(mpdclient_t *c)
220 {
221 gint retval = 0;
223 if (MPD_ERROR(c))
224 return -1;
226 /* free the old status */
227 if (c->status)
228 mpd_freeStatus(c->status);
230 /* retreive new status */
231 mpd_sendStatusCommand(c->connection);
232 c->status = mpd_getStatus(c->connection);
233 if ((retval=mpdclient_finish_command(c)))
234 return retval;
235 #ifndef NDEBUG
236 if (c->status->error)
237 D("status> %s\n", c->status->error);
238 #endif
240 /* check if the playlist needs an update */
241 if (c->playlist.id != c->status->playlist) {
242 if (playlist_is_empty(&c->playlist))
243 retval = mpdclient_playlist_update_changes(c);
244 else
245 retval = mpdclient_playlist_update(c);
246 }
248 /* update the current song */
249 if (!c->song || c->status->songid != c->song->id) {
250 c->song = playlist_get_song(c, c->status->song);
251 }
253 c->need_update = FALSE;
255 return retval;
256 }
259 /****************************************************************************/
260 /*** MPD Commands **********************************************************/
261 /****************************************************************************/
263 gint
264 mpdclient_cmd_play(mpdclient_t *c, gint idx)
265 {
266 #ifdef ENABLE_SONG_ID
267 struct mpd_song *song = playlist_get_song(c, idx);
269 D("Play id:%d\n", song ? song->id : -1);
270 if (song)
271 mpd_sendPlayIdCommand(c->connection, song->id);
272 else
273 mpd_sendPlayIdCommand(c->connection, MPD_PLAY_AT_BEGINNING);
274 #else
275 mpd_sendPlayCommand(c->connection, idx);
276 #endif
277 c->need_update = TRUE;
278 return mpdclient_finish_command(c);
279 }
281 gint
282 mpdclient_cmd_pause(mpdclient_t *c, gint value)
283 {
284 mpd_sendPauseCommand(c->connection, value);
285 return mpdclient_finish_command(c);
286 }
288 gint
289 mpdclient_cmd_crop(mpdclient_t *c)
290 {
291 mpd_Status *status;
292 int length;
294 mpd_sendStatusCommand(c->connection);
295 status = mpd_getStatus(c->connection);
296 length = status->playlistLength - 1;
298 if (length <= 0) {
299 mpd_freeStatus(status);
300 } else if (status->state == 3 || status->state == 2) {
301 /* If playing or paused */
303 mpd_sendCommandListBegin( c->connection );
305 while (length >= 0) {
306 if (length != status->song)
307 mpd_sendDeleteCommand(c->connection, length);
309 length--;
310 }
312 mpd_sendCommandListEnd(c->connection);
313 mpd_freeStatus(status);
314 } else {
315 mpd_freeStatus(status);
316 }
318 return mpdclient_finish_command(c);
319 }
321 gint
322 mpdclient_cmd_stop(mpdclient_t *c)
323 {
324 mpd_sendStopCommand(c->connection);
325 return mpdclient_finish_command(c);
326 }
328 gint
329 mpdclient_cmd_next(mpdclient_t *c)
330 {
331 mpd_sendNextCommand(c->connection);
332 c->need_update = TRUE;
333 return mpdclient_finish_command(c);
334 }
336 gint
337 mpdclient_cmd_prev(mpdclient_t *c)
338 {
339 mpd_sendPrevCommand(c->connection);
340 c->need_update = TRUE;
341 return mpdclient_finish_command(c);
342 }
344 gint
345 mpdclient_cmd_seek(mpdclient_t *c, gint id, gint pos)
346 {
347 D("Seek id:%d\n", id);
348 mpd_sendSeekIdCommand(c->connection, id, pos);
349 return mpdclient_finish_command(c);
350 }
352 gint
353 mpdclient_cmd_shuffle(mpdclient_t *c)
354 {
355 mpd_sendShuffleCommand(c->connection);
356 c->need_update = TRUE;
357 return mpdclient_finish_command(c);
358 }
360 gint
361 mpdclient_cmd_clear(mpdclient_t *c)
362 {
363 gint retval = 0;
365 mpd_sendClearCommand(c->connection);
366 retval = mpdclient_finish_command(c);
367 /* call playlist updated callback */
368 mpdclient_playlist_callback(c, PLAYLIST_EVENT_CLEAR, NULL);
369 c->need_update = TRUE;
370 return retval;
371 }
373 gint
374 mpdclient_cmd_repeat(mpdclient_t *c, gint value)
375 {
376 mpd_sendRepeatCommand(c->connection, value);
377 return mpdclient_finish_command(c);
378 }
380 gint
381 mpdclient_cmd_random(mpdclient_t *c, gint value)
382 {
383 mpd_sendRandomCommand(c->connection, value);
384 return mpdclient_finish_command(c);
385 }
387 gint
388 mpdclient_cmd_crossfade(mpdclient_t *c, gint value)
389 {
390 mpd_sendCrossfadeCommand(c->connection, value);
391 return mpdclient_finish_command(c);
392 }
394 gint
395 mpdclient_cmd_db_update_utf8(mpdclient_t *c, gchar *path)
396 {
397 mpd_sendUpdateCommand(c->connection, path ? path : "");
398 return mpdclient_finish_command(c);
399 }
401 gint
402 mpdclient_cmd_volume(mpdclient_t *c, gint value)
403 {
404 mpd_sendSetvolCommand(c->connection, value);
405 return mpdclient_finish_command(c);
406 }
408 gint
409 mpdclient_cmd_add_path_utf8(mpdclient_t *c, gchar *path_utf8)
410 {
411 mpd_sendAddCommand(c->connection, path_utf8);
412 return mpdclient_finish_command(c);
413 }
415 gint
416 mpdclient_cmd_add_path(mpdclient_t *c, gchar *path)
417 {
418 gint retval;
419 gchar *path_utf8 = locale_to_utf8(path);
421 retval=mpdclient_cmd_add_path_utf8(c, path_utf8);
422 g_free(path_utf8);
423 return retval;
424 }
426 gint
427 mpdclient_cmd_add(mpdclient_t *c, struct mpd_song *song)
428 {
429 gint retval = 0;
431 if( !song || !song->file )
432 return -1;
434 /* send the add command to mpd */
435 mpd_sendAddCommand(c->connection, song->file);
436 if( (retval=mpdclient_finish_command(c)) )
437 return retval;
439 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD
440 /* add the song to playlist */
441 playlist_append(&c->playlist, song);
443 /* increment the playlist id, so we dont retrives a new playlist */
444 c->playlist.id++;
446 /* call playlist updated callback */
447 mpdclient_playlist_callback(c, PLAYLIST_EVENT_ADD, (gpointer) song);
448 #else
449 c->need_update = TRUE;
450 #endif
452 return 0;
453 }
455 gint
456 mpdclient_cmd_delete(mpdclient_t *c, gint idx)
457 {
458 gint retval = 0;
459 struct mpd_song *song;
461 if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
462 return -1;
464 song = playlist_get(&c->playlist, idx);
466 /* send the delete command to mpd */
467 #ifdef ENABLE_SONG_ID
468 D("Delete id:%d\n", song->id);
469 mpd_sendDeleteIdCommand(c->connection, song->id);
470 #else
471 mpd_sendDeleteCommand(c->connection, idx);
472 #endif
473 if( (retval=mpdclient_finish_command(c)) )
474 return retval;
476 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
477 /* increment the playlist id, so we dont retrive a new playlist */
478 c->playlist.id++;
480 /* remove the song from the playlist */
481 playlist_remove_reuse(&c->playlist, idx);
483 /* call playlist updated callback */
484 mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
486 /* remove references to the song */
487 if (c->song == song) {
488 c->song = NULL;
489 c->need_update = TRUE;
490 }
492 mpd_freeSong(song);
494 #else
495 c->need_update = TRUE;
496 #endif
498 return 0;
499 }
501 gint
502 mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index)
503 {
504 gint n;
505 struct mpd_song *song1, *song2;
507 if (old_index == new_index || new_index < 0 ||
508 (guint)new_index >= c->playlist.list->len)
509 return -1;
511 song1 = playlist_get(&c->playlist, old_index);
512 song2 = playlist_get(&c->playlist, new_index);
514 /* send the move command to mpd */
515 #ifdef ENABLE_SONG_ID
516 D("Swapping id:%d with id:%d\n", song1->id, song2->id);
517 mpd_sendSwapIdCommand(c->connection, song1->id, song2->id);
518 #else
519 D("Moving index %d to id:%d\n", old_index, new_index);
520 mpd_sendMoveCommand(c->connection, old_index, new_index);
521 #endif
522 if( (n=mpdclient_finish_command(c)) )
523 return n;
525 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
526 /* update the playlist */
527 playlist_swap(&c->playlist, old_index, new_index);
529 /* increment the playlist id, so we dont retrives a new playlist */
530 c->playlist.id++;
532 #else
533 c->need_update = TRUE;
534 #endif
536 /* call playlist updated callback */
537 D("move> new_index=%d, old_index=%d\n", new_index, old_index);
538 mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index);
540 return 0;
541 }
543 gint
544 mpdclient_cmd_save_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
545 {
546 gint retval = 0;
548 mpd_sendSaveCommand(c->connection, filename_utf8);
549 if ((retval = mpdclient_finish_command(c)) == 0)
550 mpdclient_browse_callback(c, BROWSE_PLAYLIST_SAVED, NULL);
551 return retval;
552 }
554 gint
555 mpdclient_cmd_save_playlist(mpdclient_t *c, gchar *filename)
556 {
557 gint retval = 0;
558 gchar *filename_utf8 = locale_to_utf8(filename);
560 retval = mpdclient_cmd_save_playlist_utf8(c, filename);
561 g_free(filename_utf8);
562 return retval;
563 }
565 gint
566 mpdclient_cmd_load_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
567 {
568 mpd_sendLoadCommand(c->connection, filename_utf8);
569 c->need_update = TRUE;
570 return mpdclient_finish_command(c);
571 }
573 gint
574 mpdclient_cmd_delete_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
575 {
576 gint retval = 0;
578 mpd_sendRmCommand(c->connection, filename_utf8);
579 if ((retval = mpdclient_finish_command(c)) == 0)
580 mpdclient_browse_callback(c, BROWSE_PLAYLIST_DELETED, NULL);
581 return retval;
582 }
584 gint
585 mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename)
586 {
587 gint retval = 0;
588 gchar *filename_utf8 = locale_to_utf8(filename);
590 retval = mpdclient_cmd_delete_playlist_utf8(c, filename_utf8);
591 g_free(filename_utf8);
592 return retval;
593 }
596 /****************************************************************************/
597 /*** Callback managment functions *******************************************/
598 /****************************************************************************/
600 static void
601 do_list_callbacks(mpdclient_t *c, GList *list, gint event, gpointer data)
602 {
603 while (list) {
604 mpdc_list_cb_t fn = list->data;
606 fn(c, event, data);
607 list = list->next;
608 }
609 }
611 void
612 mpdclient_playlist_callback(mpdclient_t *c, int event, gpointer data)
613 {
614 do_list_callbacks(c, c->playlist_callbacks, event, data);
615 }
617 void
618 mpdclient_install_playlist_callback(mpdclient_t *c,mpdc_list_cb_t cb)
619 {
620 c->playlist_callbacks = g_list_append(c->playlist_callbacks, cb);
621 }
623 void
624 mpdclient_remove_playlist_callback(mpdclient_t *c, mpdc_list_cb_t cb)
625 {
626 c->playlist_callbacks = g_list_remove(c->playlist_callbacks, cb);
627 }
629 void
630 mpdclient_browse_callback(mpdclient_t *c, int event, gpointer data)
631 {
632 do_list_callbacks(c, c->browse_callbacks, event, data);
633 }
636 void
637 mpdclient_install_browse_callback(mpdclient_t *c,mpdc_list_cb_t cb)
638 {
639 c->browse_callbacks = g_list_append(c->browse_callbacks, cb);
640 }
642 void
643 mpdclient_remove_browse_callback(mpdclient_t *c, mpdc_list_cb_t cb)
644 {
645 c->browse_callbacks = g_list_remove(c->browse_callbacks, cb);
646 }
648 void
649 mpdclient_install_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
650 {
651 c->error_callbacks = g_list_append(c->error_callbacks, cb);
652 }
654 void
655 mpdclient_remove_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
656 {
657 c->error_callbacks = g_list_remove(c->error_callbacks, cb);
658 }
661 /****************************************************************************/
662 /*** Playlist managment functions *******************************************/
663 /****************************************************************************/
665 /* update playlist */
666 gint
667 mpdclient_playlist_update(mpdclient_t *c)
668 {
669 mpd_InfoEntity *entity;
671 D("mpdclient_playlist_update() [%lld]\n", c->status->playlist);
673 if (MPD_ERROR(c))
674 return -1;
676 playlist_clear(&c->playlist);
678 mpd_sendPlaylistInfoCommand(c->connection,-1);
679 while ((entity = mpd_getNextInfoEntity(c->connection))) {
680 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
681 playlist_append(&c->playlist, entity->info.song);
683 mpd_freeInfoEntity(entity);
684 }
686 c->playlist.id = c->status->playlist;
687 c->song = NULL;
689 /* call playlist updated callbacks */
690 mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
692 return mpdclient_finish_command(c);
693 }
695 #ifdef ENABLE_PLCHANGES
697 /* update playlist (plchanges) */
698 gint
699 mpdclient_playlist_update_changes(mpdclient_t *c)
700 {
701 mpd_InfoEntity *entity;
703 D("mpdclient_playlist_update_changes() [%lld -> %lld]\n",
704 c->status->playlist, c->playlist.id);
706 if (MPD_ERROR(c))
707 return -1;
709 mpd_sendPlChangesCommand(c->connection, c->playlist.id);
711 while ((entity = mpd_getNextInfoEntity(c->connection)) != NULL) {
712 struct mpd_song *song = entity->info.song;
714 if (song->pos >= 0 && (guint)song->pos < c->playlist.list->len) {
715 /* update song */
716 D("updating pos:%d, id=%d - %s\n",
717 song->pos, song->id, song->file);
718 playlist_replace(&c->playlist, song->pos, song);
719 } else {
720 /* add a new song */
721 D("adding song at pos %d\n", song->pos);
722 playlist_append(&c->playlist, song);
723 }
725 mpd_freeInfoEntity(entity);
726 }
728 /* remove trailing songs */
729 while ((guint)c->status->playlistLength < c->playlist.list->len) {
730 guint pos = c->playlist.list->len - 1;
732 /* Remove the last playlist entry */
733 D("removing song at pos %d\n", pos);
734 playlist_remove(&c->playlist, pos);
735 }
737 c->song = NULL;
738 c->playlist.id = c->status->playlist;
740 mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
742 return 0;
743 }
745 #else
746 gint
747 mpdclient_playlist_update_changes(mpdclient_t *c)
748 {
749 return mpdclient_playlist_update(c);
750 }
751 #endif
754 /****************************************************************************/
755 /*** Filelist functions *****************************************************/
756 /****************************************************************************/
758 mpdclient_filelist_t *
759 mpdclient_filelist_get(mpdclient_t *c, const gchar *path)
760 {
761 mpdclient_filelist_t *filelist;
762 mpd_InfoEntity *entity;
763 gchar *path_utf8 = locale_to_utf8(path);
764 gboolean has_dirs_only = TRUE;
766 D("mpdclient_filelist_get(%s)\n", path);
767 mpd_sendLsInfoCommand(c->connection, path_utf8);
768 filelist = filelist_new(path);
769 if (path && path[0] && strcmp(path, "/"))
770 /* add a dummy entry for ./.. */
771 filelist_append(filelist, NULL);
773 while ((entity=mpd_getNextInfoEntity(c->connection))) {
774 filelist_append(filelist, entity);
776 if (has_dirs_only && entity->type != MPD_INFO_ENTITY_TYPE_DIRECTORY) {
777 has_dirs_only = FALSE;
778 }
779 }
781 /* If there's an error, ignore it. We'll return an empty filelist. */
782 mpdclient_finish_command(c);
784 g_free(path_utf8);
785 filelist->updated = TRUE;
787 // If there are only directory entities in the filelist, we sort it
788 if (has_dirs_only) {
789 D("mpdclient_filelist_get: only dirs; sorting!\n");
790 filelist_sort(filelist, compare_filelistentry_dir);
791 }
793 return filelist;
794 }
796 mpdclient_filelist_t *
797 mpdclient_filelist_search_utf8(mpdclient_t *c,
798 int exact_match,
799 int table,
800 gchar *filter_utf8)
801 {
802 mpdclient_filelist_t *filelist;
803 mpd_InfoEntity *entity;
805 D("mpdclient_filelist_search(%s)\n", filter_utf8);
806 if (exact_match)
807 mpd_sendFindCommand(c->connection, table, filter_utf8);
808 else
809 mpd_sendSearchCommand(c->connection, table, filter_utf8);
810 filelist = filelist_new(NULL);
812 while ((entity=mpd_getNextInfoEntity(c->connection)))
813 filelist_append(filelist, entity);
815 if (mpdclient_finish_command(c)) {
816 filelist_free(filelist);
817 return NULL;
818 }
820 filelist->updated = TRUE;
821 return filelist;
822 }
825 mpdclient_filelist_t *
826 mpdclient_filelist_search(mpdclient_t *c,
827 int exact_match,
828 int table,
829 gchar *_filter)
830 {
831 mpdclient_filelist_t *filelist;
832 gchar *filter_utf8 = locale_to_utf8(_filter);
834 D("mpdclient_filelist_search(%s)\n", _filter);
835 filelist = mpdclient_filelist_search_utf8(c, exact_match, table,
836 filter_utf8);
837 g_free(filter_utf8);
839 return filelist;
840 }
842 mpdclient_filelist_t *
843 mpdclient_filelist_update(mpdclient_t *c, mpdclient_filelist_t *filelist)
844 {
845 if (filelist != NULL) {
846 gchar *path = g_strdup(filelist->path);
848 filelist_free(filelist);
849 filelist = mpdclient_filelist_get(c, path);
850 g_free(path);
851 return filelist;
852 }
853 return NULL;
854 }
856 int
857 mpdclient_filelist_add_all(mpdclient_t *c, mpdclient_filelist_t *fl)
858 {
859 guint i;
861 if (filelist_is_empty(fl))
862 return 0;
864 mpd_sendCommandListBegin(c->connection);
866 for (i = 0; i < filelist_length(fl); ++i) {
867 filelist_entry_t *entry = filelist_get(fl, i);
868 mpd_InfoEntity *entity = entry->entity;
870 if (entity && entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
871 struct mpd_song *song = entity->info.song;
873 mpd_sendAddCommand(c->connection, song->file);
874 }
875 }
877 mpd_sendCommandListEnd(c->connection);
878 return mpdclient_finish_command(c);
879 }
881 GList *
882 mpdclient_get_artists_utf8(mpdclient_t *c)
883 {
884 gchar *str = NULL;
885 GList *list = NULL;
887 D("mpdclient_get_artists()\n");
888 mpd_sendListCommand(c->connection, MPD_TABLE_ARTIST, NULL);
889 while ((str = mpd_getNextArtist(c->connection)))
890 list = g_list_append(list, (gpointer) str);
892 if (mpdclient_finish_command(c))
893 return string_list_free(list);
895 return list;
896 }
898 GList *
899 mpdclient_get_albums_utf8(mpdclient_t *c, gchar *artist_utf8)
900 {
901 gchar *str = NULL;
902 GList *list = NULL;
904 D("mpdclient_get_albums(%s)\n", artist_utf8);
905 mpd_sendListCommand(c->connection, MPD_TABLE_ALBUM, artist_utf8);
906 while ((str = mpd_getNextAlbum(c->connection)))
907 list = g_list_append(list, (gpointer) str);
909 if (mpdclient_finish_command(c))
910 return string_list_free(list);
912 return list;
913 }