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 "screen_utils.h"
22 #include "config.h"
23 #include "options.h"
24 #include "strfsong.h"
25 #include "utils.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 static bool
41 MPD_ERROR(const struct mpdclient *client)
42 {
43 return client == NULL || client->connection==NULL ||
44 client->connection->error != MPD_ERROR_SUCCESS;
45 }
47 /* filelist sorting functions */
48 static gint
49 compare_filelistentry(gconstpointer filelist_entry1,
50 gconstpointer filelist_entry2)
51 {
52 const mpd_InfoEntity *e1, *e2;
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 && e1->type == e2->type) {
59 switch (e1->type) {
60 case MPD_INFO_ENTITY_TYPE_DIRECTORY:
61 n = g_utf8_collate(e1->info.directory->path,
62 e2->info.directory->path);
63 break;
64 case MPD_INFO_ENTITY_TYPE_SONG:
65 break;
66 case MPD_INFO_ENTITY_TYPE_PLAYLISTFILE:
67 n = g_utf8_collate(e1->info.playlistFile->path,
68 e2->info.playlistFile->path);
69 }
70 }
71 return n;
72 }
74 /* sort by list-format */
75 gint
76 compare_filelistentry_format(gconstpointer filelist_entry1,
77 gconstpointer filelist_entry2)
78 {
79 const mpd_InfoEntity *e1, *e2;
80 char key1[BUFSIZE], key2[BUFSIZE];
81 int n = 0;
83 e1 = ((const filelist_entry_t *)filelist_entry1)->entity;
84 e2 = ((const filelist_entry_t *)filelist_entry2)->entity;
86 if (e1 && e2 &&
87 e1->type == MPD_INFO_ENTITY_TYPE_SONG &&
88 e2->type == MPD_INFO_ENTITY_TYPE_SONG) {
89 strfsong(key1, BUFSIZE, options.list_format, e1->info.song);
90 strfsong(key2, BUFSIZE, options.list_format, e2->info.song);
91 n = strcmp(key1,key2);
92 }
94 return n;
95 }
98 /* Error callbacks */
99 static gint
100 error_cb(mpdclient_t *c, gint error, const gchar *msg)
101 {
102 GList *list = c->error_callbacks;
104 if (list == NULL)
105 fprintf(stderr, "error [%d]: %s\n", (error & 0xFF), msg);
107 while (list) {
108 mpdc_error_cb_t cb = list->data;
109 if (cb)
110 cb(c, error, msg);
111 list = list->next;
112 }
114 mpd_clearError(c->connection);
115 return error;
116 }
119 /****************************************************************************/
120 /*** mpdclient functions ****************************************************/
121 /****************************************************************************/
123 gint
124 mpdclient_finish_command(mpdclient_t *c)
125 {
126 mpd_finishCommand(c->connection);
128 if (c->connection->error) {
129 gint error = c->connection->error;
131 if (error == MPD_ERROR_ACK &&
132 c->connection->errorCode == MPD_ACK_ERROR_PERMISSION &&
133 screen_auth(c) == 0)
134 return 0;
136 if (error == MPD_ERROR_ACK)
137 error = error | (c->connection->errorCode << 8);
139 error_cb(c, error, c->connection->errorStr);
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);
153 c->volume = MPD_STATUS_NO_VOLUME;
155 return c;
156 }
158 void
159 mpdclient_free(mpdclient_t *c)
160 {
161 mpdclient_disconnect(c);
163 mpdclient_playlist_free(&c->playlist);
165 g_list_free(c->error_callbacks);
166 g_list_free(c->playlist_callbacks);
167 g_list_free(c->browse_callbacks);
168 g_free(c);
169 }
171 gint
172 mpdclient_disconnect(mpdclient_t *c)
173 {
174 if (c->connection)
175 mpd_closeConnection(c->connection);
176 c->connection = NULL;
178 if (c->status)
179 mpd_freeStatus(c->status);
180 c->status = NULL;
182 playlist_clear(&c->playlist);
184 if (c->song)
185 c->song = NULL;
187 return 0;
188 }
190 gint
191 mpdclient_connect(mpdclient_t *c,
192 gchar *host,
193 gint port,
194 gfloat _timeout,
195 gchar *password)
196 {
197 gint retval = 0;
199 /* close any open connection */
200 if( c->connection )
201 mpdclient_disconnect(c);
203 /* connect to MPD */
204 c->connection = mpd_newConnection(host, port, _timeout);
205 if( c->connection->error )
206 return error_cb(c, c->connection->error,
207 c->connection->errorStr);
209 /* send password */
210 if( password ) {
211 mpd_sendPasswordCommand(c->connection, password);
212 retval = mpdclient_finish_command(c);
213 }
214 c->need_update = TRUE;
216 return retval;
217 }
219 gint
220 mpdclient_update(mpdclient_t *c)
221 {
222 gint retval = 0;
224 c->volume = MPD_STATUS_NO_VOLUME;
226 if (MPD_ERROR(c))
227 return -1;
229 /* free the old status */
230 if (c->status)
231 mpd_freeStatus(c->status);
233 /* retrieve new status */
234 mpd_sendStatusCommand(c->connection);
235 c->status = mpd_getStatus(c->connection);
236 if ((retval=mpdclient_finish_command(c)))
237 return retval;
239 if (c->updatingdb && c->updatingdb != c->status->updatingDb)
240 mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
242 c->updatingdb = c->status->updatingDb;
243 c->volume = c->status->volume;
245 /* check if the playlist needs an update */
246 if (c->playlist.id != c->status->playlist) {
247 if (playlist_is_empty(&c->playlist))
248 retval = mpdclient_playlist_update_changes(c);
249 else
250 retval = mpdclient_playlist_update(c);
251 }
253 /* update the current song */
254 if (!c->song || c->status->songid != c->song->id) {
255 c->song = playlist_get_song(c, c->status->song);
256 }
258 c->need_update = FALSE;
260 return retval;
261 }
264 /****************************************************************************/
265 /*** MPD Commands **********************************************************/
266 /****************************************************************************/
268 gint
269 mpdclient_cmd_play(mpdclient_t *c, gint idx)
270 {
271 #ifdef ENABLE_SONG_ID
272 struct mpd_song *song = playlist_get_song(c, idx);
274 if (song)
275 mpd_sendPlayIdCommand(c->connection, song->id);
276 else
277 mpd_sendPlayIdCommand(c->connection, MPD_PLAY_AT_BEGINNING);
278 #else
279 mpd_sendPlayCommand(c->connection, idx);
280 #endif
281 c->need_update = TRUE;
282 return mpdclient_finish_command(c);
283 }
285 gint
286 mpdclient_cmd_pause(mpdclient_t *c, gint value)
287 {
288 mpd_sendPauseCommand(c->connection, value);
289 return mpdclient_finish_command(c);
290 }
292 gint
293 mpdclient_cmd_crop(mpdclient_t *c)
294 {
295 gint error;
296 mpd_Status *status;
297 bool playing;
298 int length, current;
300 mpd_sendStatusCommand(c->connection);
301 status = mpd_getStatus(c->connection);
302 error = mpdclient_finish_command(c);
303 if (error)
304 return error;
306 playing = status->state == MPD_STATUS_STATE_PLAY ||
307 status->state == MPD_STATUS_STATE_PAUSE;
308 length = status->playlistLength;
309 current = status->song;
311 mpd_freeStatus(status);
313 if (!playing || length < 2)
314 return 0;
316 mpd_sendCommandListBegin( c->connection );
318 while (--length >= 0)
319 if (length != current)
320 mpd_sendDeleteCommand(c->connection, length);
322 mpd_sendCommandListEnd(c->connection);
324 return mpdclient_finish_command(c);
325 }
327 gint
328 mpdclient_cmd_stop(mpdclient_t *c)
329 {
330 mpd_sendStopCommand(c->connection);
331 return mpdclient_finish_command(c);
332 }
334 gint
335 mpdclient_cmd_next(mpdclient_t *c)
336 {
337 mpd_sendNextCommand(c->connection);
338 c->need_update = TRUE;
339 return mpdclient_finish_command(c);
340 }
342 gint
343 mpdclient_cmd_prev(mpdclient_t *c)
344 {
345 mpd_sendPrevCommand(c->connection);
346 c->need_update = TRUE;
347 return mpdclient_finish_command(c);
348 }
350 gint
351 mpdclient_cmd_seek(mpdclient_t *c, gint id, gint pos)
352 {
353 mpd_sendSeekIdCommand(c->connection, id, pos);
354 return mpdclient_finish_command(c);
355 }
357 gint
358 mpdclient_cmd_shuffle(mpdclient_t *c)
359 {
360 mpd_sendShuffleCommand(c->connection);
361 c->need_update = TRUE;
362 return mpdclient_finish_command(c);
363 }
365 gint
366 mpdclient_cmd_shuffle_range(mpdclient_t *c, guint start, guint end)
367 {
368 mpd_sendShuffleRangeCommand(c->connection, start, end);
369 c->need_update = TRUE;
370 return mpdclient_finish_command(c);
371 }
373 gint
374 mpdclient_cmd_clear(mpdclient_t *c)
375 {
376 gint retval = 0;
378 mpd_sendClearCommand(c->connection);
379 retval = mpdclient_finish_command(c);
380 /* call playlist updated callback */
381 mpdclient_playlist_callback(c, PLAYLIST_EVENT_CLEAR, NULL);
382 c->need_update = TRUE;
383 return retval;
384 }
386 gint
387 mpdclient_cmd_repeat(mpdclient_t *c, gint value)
388 {
389 mpd_sendRepeatCommand(c->connection, value);
390 return mpdclient_finish_command(c);
391 }
393 gint
394 mpdclient_cmd_random(mpdclient_t *c, gint value)
395 {
396 mpd_sendRandomCommand(c->connection, value);
397 return mpdclient_finish_command(c);
398 }
400 gint
401 mpdclient_cmd_single(mpdclient_t *c, gint value)
402 {
403 mpd_sendSingleCommand(c->connection, value);
404 return mpdclient_finish_command(c);
405 }
407 gint
408 mpdclient_cmd_consume(mpdclient_t *c, gint value)
409 {
410 mpd_sendConsumeCommand(c->connection, value);
411 return mpdclient_finish_command(c);
412 }
414 gint
415 mpdclient_cmd_crossfade(mpdclient_t *c, gint value)
416 {
417 mpd_sendCrossfadeCommand(c->connection, value);
418 return mpdclient_finish_command(c);
419 }
421 gint
422 mpdclient_cmd_db_update(mpdclient_t *c, gchar *path)
423 {
424 gint ret;
426 mpd_sendUpdateCommand(c->connection, path ? path : "");
427 ret = mpdclient_finish_command(c);
429 if (ret == 0)
430 /* set updatingDb to make sure the browse callback
431 gets called even if the update has finished before
432 status is updated */
433 c->updatingdb = 1;
435 return ret;
436 }
438 gint
439 mpdclient_cmd_volume(mpdclient_t *c, gint value)
440 {
441 mpd_sendSetvolCommand(c->connection, value);
442 return mpdclient_finish_command(c);
443 }
445 gint mpdclient_cmd_volume_up(struct mpdclient *c)
446 {
447 if (c->status == NULL || c->status->volume == MPD_STATUS_NO_VOLUME)
448 return 0;
450 if (c->volume == MPD_STATUS_NO_VOLUME)
451 c->volume = c->status->volume;
453 if (c->volume >= 100)
454 return 0;
456 return mpdclient_cmd_volume(c, ++c->volume);
457 }
459 gint mpdclient_cmd_volume_down(struct mpdclient *c)
460 {
461 if (c->status == NULL || c->status->volume == MPD_STATUS_NO_VOLUME)
462 return 0;
464 if (c->volume == MPD_STATUS_NO_VOLUME)
465 c->volume = c->status->volume;
467 if (c->volume <= 0)
468 return 0;
470 return mpdclient_cmd_volume(c, --c->volume);
471 }
473 gint
474 mpdclient_cmd_add_path(mpdclient_t *c, gchar *path_utf8)
475 {
476 mpd_sendAddCommand(c->connection, path_utf8);
477 return mpdclient_finish_command(c);
478 }
480 gint
481 mpdclient_cmd_add(mpdclient_t *c, struct mpd_song *song)
482 {
483 gint retval = 0;
485 if( !song || !song->file )
486 return -1;
488 /* send the add command to mpd */
489 mpd_sendAddCommand(c->connection, song->file);
490 if( (retval=mpdclient_finish_command(c)) )
491 return retval;
493 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD
494 /* add the song to playlist */
495 playlist_append(&c->playlist, song);
497 /* increment the playlist id, so we don't retrieve a new playlist */
498 c->playlist.id++;
500 /* call playlist updated callback */
501 mpdclient_playlist_callback(c, PLAYLIST_EVENT_ADD, (gpointer) song);
502 #else
503 c->need_update = TRUE;
504 #endif
506 return 0;
507 }
509 gint
510 mpdclient_cmd_delete(mpdclient_t *c, gint idx)
511 {
512 gint retval = 0;
513 struct mpd_song *song;
515 if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
516 return -1;
518 song = playlist_get(&c->playlist, idx);
520 /* send the delete command to mpd */
521 #ifdef ENABLE_SONG_ID
522 mpd_sendDeleteIdCommand(c->connection, song->id);
523 #else
524 mpd_sendDeleteCommand(c->connection, idx);
525 #endif
526 if( (retval=mpdclient_finish_command(c)) )
527 return retval;
529 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
530 /* increment the playlist id, so we don't retrieve a new playlist */
531 c->playlist.id++;
533 /* remove the song from the playlist */
534 playlist_remove_reuse(&c->playlist, idx);
536 /* call playlist updated callback */
537 mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
539 /* remove references to the song */
540 if (c->song == song) {
541 c->song = NULL;
542 c->need_update = TRUE;
543 }
545 mpd_freeSong(song);
547 #else
548 c->need_update = TRUE;
549 #endif
551 return 0;
552 }
554 gint
555 mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index)
556 {
557 gint n;
558 struct mpd_song *song1, *song2;
560 if (old_index == new_index || new_index < 0 ||
561 (guint)new_index >= c->playlist.list->len)
562 return -1;
564 song1 = playlist_get(&c->playlist, old_index);
565 song2 = playlist_get(&c->playlist, new_index);
567 /* send the move command to mpd */
568 #ifdef ENABLE_SONG_ID
569 mpd_sendSwapIdCommand(c->connection, song1->id, song2->id);
570 #else
571 mpd_sendMoveCommand(c->connection, old_index, new_index);
572 #endif
573 if( (n=mpdclient_finish_command(c)) )
574 return n;
576 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
577 /* update the playlist */
578 playlist_swap(&c->playlist, old_index, new_index);
580 /* increment the playlist id, so we don't retrieve a new playlist */
581 c->playlist.id++;
583 #else
584 c->need_update = TRUE;
585 #endif
587 /* call playlist updated callback */
588 mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index);
590 return 0;
591 }
593 gint
594 mpdclient_cmd_save_playlist(mpdclient_t *c, gchar *filename_utf8)
595 {
596 gint retval = 0;
598 mpd_sendSaveCommand(c->connection, filename_utf8);
599 if ((retval = mpdclient_finish_command(c)) == 0)
600 mpdclient_browse_callback(c, BROWSE_PLAYLIST_SAVED, NULL);
601 return retval;
602 }
604 gint
605 mpdclient_cmd_load_playlist(mpdclient_t *c, gchar *filename_utf8)
606 {
607 mpd_sendLoadCommand(c->connection, filename_utf8);
608 c->need_update = TRUE;
609 return mpdclient_finish_command(c);
610 }
612 gint
613 mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename_utf8)
614 {
615 gint retval = 0;
617 mpd_sendRmCommand(c->connection, filename_utf8);
618 if ((retval = mpdclient_finish_command(c)) == 0)
619 mpdclient_browse_callback(c, BROWSE_PLAYLIST_DELETED, NULL);
620 return retval;
621 }
624 /****************************************************************************/
625 /*** Callback management functions ******************************************/
626 /****************************************************************************/
628 static void
629 do_list_callbacks(mpdclient_t *c, GList *list, gint event, gpointer data)
630 {
631 while (list) {
632 mpdc_list_cb_t fn = list->data;
634 fn(c, event, data);
635 list = list->next;
636 }
637 }
639 void
640 mpdclient_playlist_callback(mpdclient_t *c, int event, gpointer data)
641 {
642 do_list_callbacks(c, c->playlist_callbacks, event, data);
643 }
645 void
646 mpdclient_install_playlist_callback(mpdclient_t *c,mpdc_list_cb_t cb)
647 {
648 c->playlist_callbacks = g_list_append(c->playlist_callbacks, cb);
649 }
651 void
652 mpdclient_remove_playlist_callback(mpdclient_t *c, mpdc_list_cb_t cb)
653 {
654 c->playlist_callbacks = g_list_remove(c->playlist_callbacks, cb);
655 }
657 void
658 mpdclient_browse_callback(mpdclient_t *c, int event, gpointer data)
659 {
660 do_list_callbacks(c, c->browse_callbacks, event, data);
661 }
664 void
665 mpdclient_install_browse_callback(mpdclient_t *c,mpdc_list_cb_t cb)
666 {
667 c->browse_callbacks = g_list_append(c->browse_callbacks, cb);
668 }
670 void
671 mpdclient_remove_browse_callback(mpdclient_t *c, mpdc_list_cb_t cb)
672 {
673 c->browse_callbacks = g_list_remove(c->browse_callbacks, cb);
674 }
676 void
677 mpdclient_install_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
678 {
679 c->error_callbacks = g_list_append(c->error_callbacks, cb);
680 }
682 void
683 mpdclient_remove_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
684 {
685 c->error_callbacks = g_list_remove(c->error_callbacks, cb);
686 }
689 /****************************************************************************/
690 /*** Playlist management functions ******************************************/
691 /****************************************************************************/
693 /* update playlist */
694 gint
695 mpdclient_playlist_update(mpdclient_t *c)
696 {
697 mpd_InfoEntity *entity;
699 if (MPD_ERROR(c))
700 return -1;
702 playlist_clear(&c->playlist);
704 mpd_sendPlaylistInfoCommand(c->connection,-1);
705 while ((entity = mpd_getNextInfoEntity(c->connection))) {
706 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
707 playlist_append(&c->playlist, entity->info.song);
709 mpd_freeInfoEntity(entity);
710 }
712 c->playlist.id = c->status->playlist;
713 c->song = NULL;
715 /* call playlist updated callbacks */
716 mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
718 return mpdclient_finish_command(c);
719 }
721 #ifdef ENABLE_PLCHANGES
723 /* update playlist (plchanges) */
724 gint
725 mpdclient_playlist_update_changes(mpdclient_t *c)
726 {
727 mpd_InfoEntity *entity;
729 if (MPD_ERROR(c))
730 return -1;
732 mpd_sendPlChangesCommand(c->connection, c->playlist.id);
734 while ((entity = mpd_getNextInfoEntity(c->connection)) != NULL) {
735 struct mpd_song *song = entity->info.song;
737 if (song->pos >= 0 && (guint)song->pos < c->playlist.list->len) {
738 /* update song */
739 playlist_replace(&c->playlist, song->pos, song);
740 } else {
741 /* add a new song */
742 playlist_append(&c->playlist, song);
743 }
745 mpd_freeInfoEntity(entity);
746 }
748 /* remove trailing songs */
749 while ((guint)c->status->playlistLength < c->playlist.list->len) {
750 guint pos = c->playlist.list->len - 1;
752 /* Remove the last playlist entry */
753 playlist_remove(&c->playlist, pos);
754 }
756 c->song = NULL;
757 c->playlist.id = c->status->playlist;
759 mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
761 return 0;
762 }
764 #else
765 gint
766 mpdclient_playlist_update_changes(mpdclient_t *c)
767 {
768 return mpdclient_playlist_update(c);
769 }
770 #endif
773 /****************************************************************************/
774 /*** Filelist functions *****************************************************/
775 /****************************************************************************/
777 mpdclient_filelist_t *
778 mpdclient_filelist_get(mpdclient_t *c, const gchar *path)
779 {
780 mpdclient_filelist_t *filelist;
781 mpd_InfoEntity *entity;
783 mpd_sendLsInfoCommand(c->connection, path);
784 filelist = filelist_new(path);
785 if (path && path[0] && strcmp(path, "/"))
786 /* add a dummy entry for ./.. */
787 filelist_append(filelist, NULL);
789 while ((entity=mpd_getNextInfoEntity(c->connection))) {
790 filelist_append(filelist, entity);
791 }
793 /* If there's an error, ignore it. We'll return an empty filelist. */
794 mpdclient_finish_command(c);
796 filelist_sort_dir_play(filelist, compare_filelistentry);
798 return filelist;
799 }
801 mpdclient_filelist_t *
802 mpdclient_filelist_search(mpdclient_t *c,
803 int exact_match,
804 int table,
805 gchar *filter_utf8)
806 {
807 mpdclient_filelist_t *filelist;
808 mpd_InfoEntity *entity;
810 if (exact_match)
811 mpd_sendFindCommand(c->connection, table, filter_utf8);
812 else
813 mpd_sendSearchCommand(c->connection, table, filter_utf8);
814 filelist = filelist_new(NULL);
816 while ((entity=mpd_getNextInfoEntity(c->connection)))
817 filelist_append(filelist, entity);
819 if (mpdclient_finish_command(c)) {
820 filelist_free(filelist);
821 return NULL;
822 }
824 return filelist;
825 }
827 mpdclient_filelist_t *
828 mpdclient_filelist_update(mpdclient_t *c, mpdclient_filelist_t *filelist)
829 {
830 if (filelist != NULL) {
831 gchar *path = g_strdup(filelist->path);
833 filelist_free(filelist);
834 filelist = mpdclient_filelist_get(c, path);
835 g_free(path);
836 return filelist;
837 }
838 return NULL;
839 }
841 int
842 mpdclient_filelist_add_all(mpdclient_t *c, mpdclient_filelist_t *fl)
843 {
844 guint i;
846 if (filelist_is_empty(fl))
847 return 0;
849 mpd_sendCommandListBegin(c->connection);
851 for (i = 0; i < filelist_length(fl); ++i) {
852 filelist_entry_t *entry = filelist_get(fl, i);
853 mpd_InfoEntity *entity = entry->entity;
855 if (entity && entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
856 struct mpd_song *song = entity->info.song;
858 mpd_sendAddCommand(c->connection, song->file);
859 }
860 }
862 mpd_sendCommandListEnd(c->connection);
863 return mpdclient_finish_command(c);
864 }
866 GList *
867 mpdclient_get_artists(mpdclient_t *c)
868 {
869 gchar *str = NULL;
870 GList *list = NULL;
872 mpd_sendListCommand(c->connection, MPD_TABLE_ARTIST, NULL);
873 while ((str = mpd_getNextArtist(c->connection)))
874 list = g_list_append(list, (gpointer) str);
876 if (mpdclient_finish_command(c))
877 return string_list_free(list);
879 return list;
880 }
882 GList *
883 mpdclient_get_albums(mpdclient_t *c, gchar *artist_utf8)
884 {
885 gchar *str = NULL;
886 GList *list = NULL;
888 mpd_sendListCommand(c->connection, MPD_TABLE_ALBUM, artist_utf8);
889 while ((str = mpd_getNextAlbum(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 }