933a14874772a6890e7cf8b44dde5407c4ebe6c4
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "mpdclient.h"
21 #include "filelist.h"
22 #include "screen_client.h"
23 #include "config.h"
24 #include "options.h"
25 #include "strfsong.h"
26 #include "utils.h"
28 #include <mpd/client.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <time.h>
33 #include <string.h>
35 #undef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD /* broken with song id's */
36 #define ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
37 #define ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
39 #define BUFSIZE 1024
41 static bool
42 MPD_ERROR(const struct mpdclient *client)
43 {
44 return client->connection == NULL ||
45 mpd_connection_get_error(client->connection) != MPD_ERROR_SUCCESS;
46 }
48 /* filelist sorting functions */
49 static gint
50 compare_filelistentry(gconstpointer filelist_entry1,
51 gconstpointer filelist_entry2)
52 {
53 const struct mpd_entity *e1, *e2;
54 int n = 0;
56 e1 = ((const struct filelist_entry *)filelist_entry1)->entity;
57 e2 = ((const struct filelist_entry *)filelist_entry2)->entity;
59 if (e1 != NULL && e2 != NULL &&
60 mpd_entity_get_type(e1) == mpd_entity_get_type(e2)) {
61 switch (mpd_entity_get_type(e1)) {
62 case MPD_ENTITY_TYPE_UNKNOWN:
63 break;
64 case MPD_ENTITY_TYPE_DIRECTORY:
65 n = g_utf8_collate(mpd_directory_get_path(mpd_entity_get_directory(e1)),
66 mpd_directory_get_path(mpd_entity_get_directory(e2)));
67 break;
68 case MPD_ENTITY_TYPE_SONG:
69 break;
70 case MPD_ENTITY_TYPE_PLAYLIST:
71 n = g_utf8_collate(mpd_playlist_get_path(mpd_entity_get_playlist(e1)),
72 mpd_playlist_get_path(mpd_entity_get_playlist(e2)));
73 }
74 }
75 return n;
76 }
78 /* sort by list-format */
79 gint
80 compare_filelistentry_format(gconstpointer filelist_entry1,
81 gconstpointer filelist_entry2)
82 {
83 const struct mpd_entity *e1, *e2;
84 char key1[BUFSIZE], key2[BUFSIZE];
85 int n = 0;
87 e1 = ((const struct filelist_entry *)filelist_entry1)->entity;
88 e2 = ((const struct filelist_entry *)filelist_entry2)->entity;
90 if (e1 && e2 &&
91 mpd_entity_get_type(e1) == MPD_ENTITY_TYPE_SONG &&
92 mpd_entity_get_type(e2) == MPD_ENTITY_TYPE_SONG) {
93 strfsong(key1, BUFSIZE, options.list_format, mpd_entity_get_song(e1));
94 strfsong(key2, BUFSIZE, options.list_format, mpd_entity_get_song(e2));
95 n = strcmp(key1,key2);
96 }
98 return n;
99 }
102 /****************************************************************************/
103 /*** mpdclient functions ****************************************************/
104 /****************************************************************************/
106 gint
107 mpdclient_handle_error(struct mpdclient *c)
108 {
109 enum mpd_error error = mpd_connection_get_error(c->connection);
111 assert(error != MPD_ERROR_SUCCESS);
113 if (error == MPD_ERROR_SERVER &&
114 mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_PERMISSION &&
115 screen_auth(c))
116 return 0;
118 if (error == MPD_ERROR_SERVER)
119 error = error | (mpd_connection_get_server_error(c->connection) << 8);
121 for (GList *list = c->error_callbacks; list != NULL;
122 list = list->next) {
123 mpdc_error_cb_t cb = list->data;
124 cb(c, error, mpd_connection_get_error_message(c->connection));
125 }
127 if (!mpd_connection_clear_error(c->connection))
128 mpdclient_disconnect(c);
130 return error;
131 }
133 static gint
134 mpdclient_finish_command(struct mpdclient *c)
135 {
136 return mpd_response_finish(c->connection)
137 ? 0 : mpdclient_handle_error(c);
138 }
140 struct mpdclient *
141 mpdclient_new(void)
142 {
143 struct mpdclient *c;
145 c = g_new0(struct mpdclient, 1);
146 playlist_init(&c->playlist);
147 c->volume = MPD_STATUS_NO_VOLUME;
149 return c;
150 }
152 void
153 mpdclient_free(struct mpdclient *c)
154 {
155 mpdclient_disconnect(c);
157 mpdclient_playlist_free(&c->playlist);
159 g_list_free(c->error_callbacks);
160 g_list_free(c->playlist_callbacks);
161 g_list_free(c->browse_callbacks);
162 g_free(c);
163 }
165 gint
166 mpdclient_disconnect(struct mpdclient *c)
167 {
168 if (c->connection)
169 mpd_connection_free(c->connection);
170 c->connection = NULL;
172 if (c->status)
173 mpd_status_free(c->status);
174 c->status = NULL;
176 playlist_clear(&c->playlist);
178 if (c->song)
179 c->song = NULL;
181 return 0;
182 }
184 gint
185 mpdclient_connect(struct mpdclient *c,
186 const gchar *host,
187 gint port,
188 gfloat _timeout,
189 const gchar *password)
190 {
191 gint retval = 0;
193 /* close any open connection */
194 if( c->connection )
195 mpdclient_disconnect(c);
197 /* connect to MPD */
198 c->connection = mpd_connection_new(host, port, _timeout * 1000);
199 if (c->connection == NULL)
200 g_error("Out of memory");
202 if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) {
203 retval = mpdclient_handle_error(c);
204 if (retval != 0) {
205 mpd_connection_free(c->connection);
206 c->connection = NULL;
207 }
209 return retval;
210 }
212 /* send password */
213 if( password ) {
214 mpd_send_password(c->connection, password);
215 retval = mpdclient_finish_command(c);
216 }
218 return retval;
219 }
221 gint
222 mpdclient_update(struct mpdclient *c)
223 {
224 gint retval = 0;
226 c->volume = MPD_STATUS_NO_VOLUME;
228 if (MPD_ERROR(c))
229 return -1;
231 /* free the old status */
232 if (c->status)
233 mpd_status_free(c->status);
235 /* retrieve new status */
236 c->status = mpd_run_status(c->connection);
237 if (c->status == NULL)
238 return mpdclient_handle_error(c);
240 if (c->updatingdb &&
241 c->updatingdb != mpd_status_get_update_id(c->status))
242 mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
244 c->updatingdb = mpd_status_get_update_id(c->status);
245 c->volume = mpd_status_get_volume(c->status);
247 /* check if the playlist needs an update */
248 if (c->playlist.id != mpd_status_get_queue_version(c->status)) {
249 if (!playlist_is_empty(&c->playlist))
250 retval = mpdclient_playlist_update_changes(c);
251 else
252 retval = mpdclient_playlist_update(c);
253 }
255 /* update the current song */
256 if (!c->song || mpd_status_get_song_id(c->status)) {
257 c->song = playlist_get_song(c, mpd_status_get_song_pos(c->status));
258 }
260 return retval;
261 }
264 /****************************************************************************/
265 /*** MPD Commands **********************************************************/
266 /****************************************************************************/
268 gint
269 mpdclient_cmd_play(struct mpdclient *c, gint idx)
270 {
271 struct mpd_song *song = playlist_get_song(c, idx);
273 if (MPD_ERROR(c))
274 return -1;
276 if (song)
277 mpd_send_play_id(c->connection, mpd_song_get_id(song));
278 else
279 mpd_send_play(c->connection);
281 return mpdclient_finish_command(c);
282 }
284 gint
285 mpdclient_cmd_pause(struct mpdclient *c, gint value)
286 {
287 if (MPD_ERROR(c))
288 return -1;
290 mpd_send_pause(c->connection, value);
291 return mpdclient_finish_command(c);
292 }
294 gint
295 mpdclient_cmd_crop(struct mpdclient *c)
296 {
297 struct mpd_status *status;
298 bool playing;
299 int length, current;
301 if (MPD_ERROR(c))
302 return -1;
304 status = mpd_run_status(c->connection);
305 if (status == NULL)
306 return mpdclient_handle_error(c);
308 playing = mpd_status_get_state(status) == MPD_STATE_PLAY ||
309 mpd_status_get_state(status) == MPD_STATE_PAUSE;
310 length = mpd_status_get_queue_length(status);
311 current = mpd_status_get_song_pos(status);
313 mpd_status_free(status);
315 if (!playing || length < 2)
316 return 0;
318 mpd_command_list_begin(c->connection, false);
320 while (--length >= 0)
321 if (length != current)
322 mpd_send_delete(c->connection, length);
324 mpd_command_list_end(c->connection);
326 return mpdclient_finish_command(c);
327 }
329 gint
330 mpdclient_cmd_stop(struct mpdclient *c)
331 {
332 if (MPD_ERROR(c))
333 return -1;
335 mpd_send_stop(c->connection);
336 return mpdclient_finish_command(c);
337 }
339 gint
340 mpdclient_cmd_next(struct mpdclient *c)
341 {
342 if (MPD_ERROR(c))
343 return -1;
345 mpd_send_next(c->connection);
346 return mpdclient_finish_command(c);
347 }
349 gint
350 mpdclient_cmd_prev(struct mpdclient *c)
351 {
352 if (MPD_ERROR(c))
353 return -1;
355 mpd_send_previous(c->connection);
356 return mpdclient_finish_command(c);
357 }
359 gint
360 mpdclient_cmd_seek(struct mpdclient *c, gint id, gint pos)
361 {
362 if (MPD_ERROR(c))
363 return -1;
365 mpd_send_seek_id(c->connection, id, pos);
366 return mpdclient_finish_command(c);
367 }
369 gint
370 mpdclient_cmd_shuffle(struct mpdclient *c)
371 {
372 if (MPD_ERROR(c))
373 return -1;
375 mpd_send_shuffle(c->connection);
376 return mpdclient_finish_command(c);
377 }
379 gint
380 mpdclient_cmd_shuffle_range(struct mpdclient *c, guint start, guint end)
381 {
382 mpd_send_shuffle_range(c->connection, start, end);
383 return mpdclient_finish_command(c);
384 }
386 gint
387 mpdclient_cmd_clear(struct mpdclient *c)
388 {
389 gint retval = 0;
391 if (MPD_ERROR(c))
392 return -1;
394 mpd_send_clear(c->connection);
395 retval = mpdclient_finish_command(c);
396 /* call playlist updated callback */
397 mpdclient_playlist_callback(c, PLAYLIST_EVENT_CLEAR, NULL);
398 return retval;
399 }
401 gint
402 mpdclient_cmd_repeat(struct mpdclient *c, gint value)
403 {
404 if (MPD_ERROR(c))
405 return -1;
407 mpd_send_repeat(c->connection, value);
408 return mpdclient_finish_command(c);
409 }
411 gint
412 mpdclient_cmd_random(struct mpdclient *c, gint value)
413 {
414 if (MPD_ERROR(c))
415 return -1;
417 mpd_send_random(c->connection, value);
418 return mpdclient_finish_command(c);
419 }
421 gint
422 mpdclient_cmd_single(struct mpdclient *c, gint value)
423 {
424 if (MPD_ERROR(c))
425 return -1;
427 mpd_send_single(c->connection, value);
428 return mpdclient_finish_command(c);
429 }
431 gint
432 mpdclient_cmd_consume(struct mpdclient *c, gint value)
433 {
434 if (MPD_ERROR(c))
435 return -1;
437 mpd_send_consume(c->connection, value);
438 return mpdclient_finish_command(c);
439 }
441 gint
442 mpdclient_cmd_crossfade(struct mpdclient *c, gint value)
443 {
444 if (MPD_ERROR(c))
445 return -1;
447 mpd_send_crossfade(c->connection, value);
448 return mpdclient_finish_command(c);
449 }
451 gint
452 mpdclient_cmd_volume(struct mpdclient *c, gint value)
453 {
454 if (MPD_ERROR(c))
455 return -1;
457 mpd_send_set_volume(c->connection, value);
458 return mpdclient_finish_command(c);
459 }
461 gint mpdclient_cmd_volume_up(struct mpdclient *c)
462 {
463 if (MPD_ERROR(c))
464 return -1;
466 if (c->status == NULL ||
467 mpd_status_get_volume(c->status) == MPD_STATUS_NO_VOLUME)
468 return 0;
470 if (c->volume == MPD_STATUS_NO_VOLUME)
471 c->volume = mpd_status_get_volume(c->status);
473 if (c->volume >= 100)
474 return 0;
476 return mpdclient_cmd_volume(c, ++c->volume);
477 }
479 gint mpdclient_cmd_volume_down(struct mpdclient *c)
480 {
481 if (MPD_ERROR(c))
482 return -1;
484 if (c->status == NULL ||
485 mpd_status_get_volume(c->status) == MPD_STATUS_NO_VOLUME)
486 return 0;
488 if (c->volume == MPD_STATUS_NO_VOLUME)
489 c->volume = mpd_status_get_volume(c->status);
491 if (c->volume <= 0)
492 return 0;
494 return mpdclient_cmd_volume(c, --c->volume);
495 }
497 gint
498 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
499 {
500 if (MPD_ERROR(c))
501 return -1;
503 mpd_send_add(c->connection, path_utf8);
504 return mpdclient_finish_command(c);
505 }
507 gint
508 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
509 {
510 gint retval = 0;
512 if (MPD_ERROR(c))
513 return -1;
515 if (song == NULL)
516 return -1;
518 /* send the add command to mpd */
519 mpd_send_add(c->connection, mpd_song_get_uri(song));
520 if( (retval=mpdclient_finish_command(c)) )
521 return retval;
523 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD
524 /* add the song to playlist */
525 playlist_append(&c->playlist, song);
527 /* increment the playlist id, so we don't retrieve a new playlist */
528 c->playlist.id++;
530 /* call playlist updated callback */
531 mpdclient_playlist_callback(c, PLAYLIST_EVENT_ADD, (gpointer) song);
532 #endif
534 return 0;
535 }
537 gint
538 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
539 {
540 gint retval = 0;
541 struct mpd_song *song;
543 if (MPD_ERROR(c))
544 return -1;
546 if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
547 return -1;
549 song = playlist_get(&c->playlist, idx);
551 /* send the delete command to mpd */
552 mpd_send_delete_id(c->connection, mpd_song_get_id(song));
553 if( (retval=mpdclient_finish_command(c)) )
554 return retval;
556 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
557 /* increment the playlist id, so we don't retrieve a new playlist */
558 c->playlist.id++;
560 /* remove the song from the playlist */
561 playlist_remove_reuse(&c->playlist, idx);
563 /* call playlist updated callback */
564 mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
566 /* remove references to the song */
567 if (c->song == song)
568 c->song = NULL;
570 mpd_song_free(song);
571 #endif
573 return 0;
574 }
576 gint
577 mpdclient_cmd_move(struct mpdclient *c, gint old_index, gint new_index)
578 {
579 gint n;
580 struct mpd_song *song1, *song2;
582 if (MPD_ERROR(c))
583 return -1;
585 if (old_index == new_index || new_index < 0 ||
586 (guint)new_index >= c->playlist.list->len)
587 return -1;
589 song1 = playlist_get(&c->playlist, old_index);
590 song2 = playlist_get(&c->playlist, new_index);
592 /* send the move command to mpd */
593 mpd_send_swap_id(c->connection,
594 mpd_song_get_id(song1), mpd_song_get_id(song2));
595 if( (n=mpdclient_finish_command(c)) )
596 return n;
598 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
599 /* update the playlist */
600 playlist_swap(&c->playlist, old_index, new_index);
602 /* increment the playlist id, so we don't retrieve a new playlist */
603 c->playlist.id++;
604 #endif
606 /* call playlist updated callback */
607 mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index);
609 return 0;
610 }
612 gint
613 mpdclient_cmd_save_playlist(struct mpdclient *c, const gchar *filename_utf8)
614 {
615 gint retval = 0;
617 if (MPD_ERROR(c))
618 return -1;
620 mpd_send_save(c->connection, filename_utf8);
621 if ((retval = mpdclient_finish_command(c)) == 0)
622 mpdclient_browse_callback(c, BROWSE_PLAYLIST_SAVED, NULL);
623 return retval;
624 }
626 gint
627 mpdclient_cmd_load_playlist(struct mpdclient *c, const gchar *filename_utf8)
628 {
629 if (MPD_ERROR(c))
630 return -1;
632 mpd_send_load(c->connection, filename_utf8);
633 return mpdclient_finish_command(c);
634 }
636 gint
637 mpdclient_cmd_delete_playlist(struct mpdclient *c, const gchar *filename_utf8)
638 {
639 gint retval = 0;
641 if (MPD_ERROR(c))
642 return -1;
644 mpd_send_rm(c->connection, filename_utf8);
645 if ((retval = mpdclient_finish_command(c)) == 0)
646 mpdclient_browse_callback(c, BROWSE_PLAYLIST_DELETED, NULL);
647 return retval;
648 }
651 /****************************************************************************/
652 /*** Callback management functions ******************************************/
653 /****************************************************************************/
655 static void
656 do_list_callbacks(struct mpdclient *c, GList *list, gint event, gpointer data)
657 {
658 while (list) {
659 mpdc_list_cb_t fn = list->data;
661 fn(c, event, data);
662 list = list->next;
663 }
664 }
666 void
667 mpdclient_playlist_callback(struct mpdclient *c, int event, gpointer data)
668 {
669 do_list_callbacks(c, c->playlist_callbacks, event, data);
670 }
672 void
673 mpdclient_install_playlist_callback(struct mpdclient *c,mpdc_list_cb_t cb)
674 {
675 c->playlist_callbacks = g_list_append(c->playlist_callbacks, cb);
676 }
678 void
679 mpdclient_remove_playlist_callback(struct mpdclient *c, mpdc_list_cb_t cb)
680 {
681 c->playlist_callbacks = g_list_remove(c->playlist_callbacks, cb);
682 }
684 void
685 mpdclient_browse_callback(struct mpdclient *c, int event, gpointer data)
686 {
687 do_list_callbacks(c, c->browse_callbacks, event, data);
688 }
691 void
692 mpdclient_install_browse_callback(struct mpdclient *c,mpdc_list_cb_t cb)
693 {
694 c->browse_callbacks = g_list_append(c->browse_callbacks, cb);
695 }
697 void
698 mpdclient_remove_browse_callback(struct mpdclient *c, mpdc_list_cb_t cb)
699 {
700 c->browse_callbacks = g_list_remove(c->browse_callbacks, cb);
701 }
703 void
704 mpdclient_install_error_callback(struct mpdclient *c, mpdc_error_cb_t cb)
705 {
706 c->error_callbacks = g_list_append(c->error_callbacks, cb);
707 }
709 void
710 mpdclient_remove_error_callback(struct mpdclient *c, mpdc_error_cb_t cb)
711 {
712 c->error_callbacks = g_list_remove(c->error_callbacks, cb);
713 }
716 /****************************************************************************/
717 /*** Playlist management functions ******************************************/
718 /****************************************************************************/
720 /* update playlist */
721 gint
722 mpdclient_playlist_update(struct mpdclient *c)
723 {
724 struct mpd_entity *entity;
726 if (MPD_ERROR(c))
727 return -1;
729 playlist_clear(&c->playlist);
731 mpd_send_list_queue_meta(c->connection);
732 while ((entity = mpd_recv_entity(c->connection))) {
733 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
734 playlist_append(&c->playlist, mpd_entity_get_song(entity));
736 mpd_entity_free(entity);
737 }
739 c->playlist.id = mpd_status_get_queue_version(c->status);
740 c->song = NULL;
742 /* call playlist updated callbacks */
743 mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
745 return mpdclient_finish_command(c);
746 }
748 /* update playlist (plchanges) */
749 gint
750 mpdclient_playlist_update_changes(struct mpdclient *c)
751 {
752 struct mpd_song *song;
753 guint length;
755 if (MPD_ERROR(c))
756 return -1;
758 mpd_send_queue_changes_meta(c->connection, c->playlist.id);
760 while ((song = mpd_recv_song(c->connection)) != NULL) {
761 int pos = mpd_song_get_pos(song);
763 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
764 /* update song */
765 playlist_replace(&c->playlist, pos, song);
766 } else {
767 /* add a new song */
768 playlist_append(&c->playlist, song);
769 }
771 mpd_song_free(song);
772 }
774 /* remove trailing songs */
776 length = mpd_status_get_queue_length(c->status);
777 while (length < c->playlist.list->len) {
778 guint pos = c->playlist.list->len - 1;
780 /* Remove the last playlist entry */
781 playlist_remove(&c->playlist, pos);
782 }
784 c->song = NULL;
785 c->playlist.id = mpd_status_get_queue_version(c->status);
787 mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
789 return 0;
790 }
793 /****************************************************************************/
794 /*** Filelist functions *****************************************************/
795 /****************************************************************************/
797 struct filelist *
798 mpdclient_filelist_get(struct mpdclient *c, const gchar *path)
799 {
800 struct filelist *filelist;
801 struct mpd_entity *entity;
803 if (MPD_ERROR(c))
804 return NULL;
806 mpd_send_list_meta(c->connection, path);
807 filelist = filelist_new();
808 if (path && path[0] && strcmp(path, "/"))
809 /* add a dummy entry for ./.. */
810 filelist_append(filelist, NULL);
812 while ((entity = mpd_recv_entity(c->connection)) != NULL)
813 filelist_append(filelist, entity);
815 /* If there's an error, ignore it. We'll return an empty filelist. */
816 mpdclient_finish_command(c);
818 filelist_sort_dir_play(filelist, compare_filelistentry);
820 return filelist;
821 }
823 static struct filelist *
824 mpdclient_recv_filelist_response(struct mpdclient *c)
825 {
826 struct filelist *filelist;
827 struct mpd_entity *entity;
829 filelist = filelist_new();
831 while ((entity = mpd_recv_entity(c->connection)) != NULL)
832 filelist_append(filelist, entity);
834 if (mpdclient_finish_command(c)) {
835 filelist_free(filelist);
836 return NULL;
837 }
839 return filelist;
840 }
842 struct filelist *
843 mpdclient_filelist_search(struct mpdclient *c,
844 int exact_match,
845 enum mpd_tag_type tag,
846 gchar *filter_utf8)
847 {
848 if (MPD_ERROR(c))
849 return NULL;
851 mpd_search_db_songs(c->connection, exact_match);
852 mpd_search_add_tag_constraint(c->connection, MPD_OPERATOR_DEFAULT,
853 tag, filter_utf8);
854 mpd_search_commit(c->connection);
856 return mpdclient_recv_filelist_response(c);
857 }
859 int
860 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
861 {
862 guint i;
864 if (MPD_ERROR(c))
865 return -1;
867 if (filelist_is_empty(fl))
868 return 0;
870 mpd_command_list_begin(c->connection, false);
872 for (i = 0; i < filelist_length(fl); ++i) {
873 struct filelist_entry *entry = filelist_get(fl, i);
874 struct mpd_entity *entity = entry->entity;
876 if (entity != NULL &&
877 mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
878 const struct mpd_song *song =
879 mpd_entity_get_song(entity);
880 const char *uri = mpd_song_get_uri(song);
882 if (uri != NULL)
883 mpd_send_add(c->connection, uri);
884 }
885 }
887 mpd_command_list_end(c->connection);
888 return mpdclient_finish_command(c);
889 }
891 GList *
892 mpdclient_get_artists(struct mpdclient *c)
893 {
894 GList *list = NULL;
895 struct mpd_pair *pair;
897 if (MPD_ERROR(c))
898 return NULL;
900 mpd_search_db_tags(c->connection, MPD_TAG_ARTIST);
901 mpd_search_commit(c->connection);
903 while ((pair = mpd_recv_pair_tag(c->connection,
904 MPD_TAG_ARTIST)) != NULL) {
905 list = g_list_append(list, g_strdup(pair->value));
906 mpd_return_pair(c->connection, pair);
907 }
909 if (mpdclient_finish_command(c))
910 return string_list_free(list);
912 return list;
913 }
915 GList *
916 mpdclient_get_albums(struct mpdclient *c, const gchar *artist_utf8)
917 {
918 GList *list = NULL;
919 struct mpd_pair *pair;
921 if (MPD_ERROR(c))
922 return NULL;
924 mpd_search_db_tags(c->connection, MPD_TAG_ALBUM);
925 if (artist_utf8 != NULL)
926 mpd_search_add_tag_constraint(c->connection,
927 MPD_OPERATOR_DEFAULT,
928 MPD_TAG_ARTIST, artist_utf8);
929 mpd_search_commit(c->connection);
931 while ((pair = mpd_recv_pair_tag(c->connection,
932 MPD_TAG_ALBUM)) != NULL) {
933 list = g_list_append(list, g_strdup(pair->value));
934 mpd_return_pair(c->connection, pair);
935 }
937 if (mpdclient_finish_command(c))
938 return string_list_free(list);
940 return list;
941 }