Remove mtime from mpd_Song
[libmpd.git] / src / libmpd-playlist.c
bloba8feef456f218dfe3d3a3ff1643cef2edf3b264d
1 /* libmpd (high level libmpdclient library)
2 * Copyright (C) 2004-2009 Qball Cow <qball@sarine.nl>
3 * Project homepage: http://gmpcwiki.sarine.nl/
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.
20 #include <stdio.h>
21 #include <stdlib.h>
22 #define __USE_GNU
24 #include <string.h>
25 #include <stdarg.h>
26 #include <config.h>
27 #include "debug_printf.h"
28 #include "libmpd.h"
29 #include "libmpd-internal.h"
32 int mpd_playlist_get_playlist_length(MpdObj *mi)
34 if(!mpd_check_connected(mi))
36 debug_printf(DEBUG_WARNING,"not connected\n");
37 return MPD_NOT_CONNECTED;
39 if(mpd_status_check(mi) != MPD_OK)
41 debug_printf(DEBUG_ERROR,"Failed grabbing status\n");
42 return MPD_STATUS_FAILED;
44 return mi->status->playlistLength;
47 long long mpd_playlist_get_old_playlist_id(MpdObj *mi)
49 return mi->OldState.playlistid;
52 long long mpd_playlist_get_playlist_id(MpdObj *mi)
54 if(!mpd_check_connected(mi))
56 debug_printf(DEBUG_WARNING,"not connected\n");
57 return MPD_NOT_CONNECTED;
59 if(mpd_status_check(mi) != MPD_OK)
61 debug_printf(DEBUG_WARNING,"Failed grabbing status\n");
62 return MPD_STATUS_FAILED;
64 return mi->status->playlist;
66 int mpd_playlist_add(MpdObj *mi,const char *path)
68 int retv = mpd_playlist_queue_add(mi, path);
69 if(retv != MPD_OK) return retv;
70 return mpd_playlist_queue_commit(mi);
73 int mpd_playlist_delete_id(MpdObj *mi, int songid)
75 int retv = mpd_playlist_queue_delete_id(mi, songid);
76 if(retv != MPD_OK) return retv;
77 return mpd_playlist_queue_commit(mi);
80 int mpd_playlist_delete_pos(MpdObj *mi, int songpos)
82 int retv = mpd_playlist_queue_delete_pos(mi, songpos);
83 if(retv != MPD_OK) return retv;
84 return mpd_playlist_queue_commit(mi);
86 /*******************************************************************************
87 * PLAYLIST
89 mpd_Song * mpd_playlist_get_song(MpdObj *mi, int songid)
91 mpd_Song *song = NULL;
92 mpd_InfoEntity *ent = NULL;
93 if(songid < 0){
94 debug_printf(DEBUG_ERROR, "songid < 0 Failed");
95 return NULL;
97 if(!mpd_check_connected(mi))
99 debug_printf(DEBUG_ERROR, "Not Connected\n");
100 return NULL;
103 if(mpd_lock_conn(mi))
105 return NULL;
107 debug_printf(DEBUG_INFO, "Trying to grab song with id: %i\n", songid);
108 mpd_sendPlaylistIdCommand(mi->connection, songid);
109 ent = mpd_getNextInfoEntity(mi->connection);
110 mpd_finishCommand(mi->connection);
112 if(mpd_unlock_conn(mi))
114 if(ent) mpd_freeInfoEntity(ent);
115 return NULL;
118 if(ent == NULL)
120 debug_printf(DEBUG_ERROR, "Failed to grab song from mpd\n");
121 return NULL;
124 if(ent->type != MPD_INFO_ENTITY_TYPE_SONG)
126 mpd_freeInfoEntity(ent);
127 debug_printf(DEBUG_ERROR, "Failed to grab correct song type from mpd\n");
128 return NULL;
130 song = ent->info.song;
131 ent->info.song = NULL;
133 mpd_freeInfoEntity(ent);
135 return song;
138 mpd_Song * mpd_playlist_get_song_from_pos(MpdObj *mi, int songpos)
140 mpd_Song *song = NULL;
141 mpd_InfoEntity *ent = NULL;
142 if(songpos < 0){
143 debug_printf(DEBUG_ERROR, "songpos < 0 Failed");
144 return NULL;
146 if(!mpd_check_connected(mi))
148 debug_printf(DEBUG_ERROR, "Not Connected\n");
149 return NULL;
152 if(mpd_lock_conn(mi))
154 return NULL;
156 debug_printf(DEBUG_INFO, "Trying to grab song with id: %i\n", songpos);
157 mpd_sendPlaylistInfoCommand(mi->connection, songpos);
158 ent = mpd_getNextInfoEntity(mi->connection);
159 mpd_finishCommand(mi->connection);
161 if(mpd_unlock_conn(mi))
163 /*TODO free entity. for now this can never happen */
164 return NULL;
167 if(ent == NULL)
169 debug_printf(DEBUG_ERROR, "Failed to grab song from mpd\n");
170 return NULL;
173 if(ent->type != MPD_INFO_ENTITY_TYPE_SONG)
175 mpd_freeInfoEntity(ent);
176 debug_printf(DEBUG_ERROR, "Failed to grab corect song type from mpd\n");
177 return NULL;
179 song = ent->info.song;
180 ent->info.song = NULL;
182 mpd_freeInfoEntity(ent);
184 return song;
187 MpdData * mpd_playlist_get_song_from_pos_range(MpdObj *mi, int start, int stop)
189 MpdData *data = NULL;
190 int i;
191 mpd_InfoEntity *ent = NULL;
192 if(!mpd_check_connected(mi))
194 debug_printf(DEBUG_ERROR, "Not Connected\n");
195 return NULL;
197 if(mpd_status_check(mi) != MPD_OK)
199 debug_printf(DEBUG_ERROR,"Failed grabbing status\n");
200 return NULL;
203 if(mpd_lock_conn(mi))
205 return NULL;
207 /* Don't check outside playlist length */
208 if(!(stop < mi->status->playlistLength)) {
209 stop = mi->status->playlistLength -1;
211 mpd_sendCommandListBegin(mi->connection);
212 for(i=start; i <= stop; i++){
213 mpd_sendPlaylistInfoCommand(mi->connection, i);
215 mpd_sendCommandListEnd(mi->connection);
216 while (( ent = mpd_getNextInfoEntity(mi->connection)) != NULL)
218 if(ent->type == MPD_INFO_ENTITY_TYPE_SONG)
220 data = mpd_new_data_struct_append(data);
221 data->type = MPD_DATA_TYPE_SONG;
222 data->song = ent->info.song;
223 ent->info.song = NULL;
225 mpd_freeInfoEntity(ent);
227 mpd_finishCommand(mi->connection);
229 if(mpd_unlock_conn(mi))
231 /*TODO free entity. for now this can never happen */
232 return NULL;
234 return data;
237 mpd_Song * mpd_playlist_get_current_song(MpdObj *mi)
239 if(!mpd_check_connected(mi))
241 debug_printf(DEBUG_WARNING, "Not Connected\n");
242 return NULL;
245 if(mpd_status_check(mi) != MPD_OK)
247 debug_printf(DEBUG_ERROR, "Failed to check status\n");
248 return NULL;
251 if(mi->CurrentSong != NULL && mi->CurrentSong->id != mi->status->songid)
253 debug_printf(DEBUG_WARNING, "Current song not up2date, updating\n");
254 mpd_freeSong(mi->CurrentSong);
255 mi->CurrentSong = NULL;
257 /* only update song when playing/pasing */
258 if(mi->CurrentSong == NULL &&
259 (mpd_player_get_state(mi) != MPD_PLAYER_STOP && mpd_player_get_state(mi) != MPD_PLAYER_UNKNOWN))
261 /* TODO: this to use the geT_current_song_id function */
262 mi->CurrentSong = mpd_playlist_get_song(mi, mpd_player_get_current_song_id(mi));
263 if(mi->CurrentSong == NULL)
265 debug_printf(DEBUG_ERROR, "Failed to grab song\n");
266 return NULL;
269 return mi->CurrentSong;
272 int mpd_playlist_clear(MpdObj *mi)
274 if(!mpd_check_connected(mi))
276 debug_printf(DEBUG_WARNING,"not connected\n");
277 return MPD_NOT_CONNECTED;
279 if(mpd_lock_conn(mi))
281 debug_printf(DEBUG_WARNING,"lock failed\n");
282 return MPD_LOCK_FAILED;
285 mpd_sendClearCommand(mi->connection);
286 mpd_finishCommand(mi->connection);
287 /* hack to make it update correctly when replacing 1 song */
288 mi->CurrentState.songid = -1;
289 /* unlock */
290 mpd_unlock_conn(mi);
291 mpd_status_update(mi);
292 return FALSE;
295 int mpd_playlist_shuffle(MpdObj *mi)
297 if(!mpd_check_connected(mi))
299 debug_printf(DEBUG_WARNING,"not connected\n");
300 return MPD_NOT_CONNECTED;
302 if(mpd_lock_conn(mi))
304 debug_printf(DEBUG_ERROR,"lock failed\n");
305 return MPD_LOCK_FAILED;
308 mpd_sendShuffleCommand(mi->connection);
309 mpd_finishCommand(mi->connection);
311 /* unlock */
312 mpd_unlock_conn(mi);
313 return FALSE;
318 int mpd_playlist_move_id(MpdObj *mi, int old_id, int new_id)
320 if(!mpd_check_connected(mi))
322 debug_printf(DEBUG_WARNING,"not connected\n");
323 return MPD_NOT_CONNECTED;
325 if(mpd_lock_conn(mi))
327 debug_printf(DEBUG_ERROR,"lock failed\n");
328 return MPD_LOCK_FAILED;
331 mpd_sendMoveIdCommand(mi->connection,old_id, new_id);
332 mpd_finishCommand(mi->connection);
334 /* unlock */
335 mpd_unlock_conn(mi);
336 return MPD_OK;
339 int mpd_playlist_move_pos(MpdObj *mi, int old_pos, int new_pos)
341 if(!mpd_check_connected(mi))
343 debug_printf(DEBUG_WARNING,"not connected\n");
344 return MPD_NOT_CONNECTED;
346 if(mpd_lock_conn(mi))
348 debug_printf(DEBUG_ERROR,"lock failed\n");
349 return MPD_LOCK_FAILED;
352 mpd_sendMoveCommand(mi->connection,old_pos, new_pos);
353 mpd_finishCommand(mi->connection);
355 /* unlock */
356 mpd_unlock_conn(mi);
357 return MPD_OK;
360 MpdData * mpd_playlist_get_changes(MpdObj *mi,int old_playlist_id)
362 MpdData *data = NULL;
363 mpd_InfoEntity *ent = NULL;
364 if(!mpd_check_connected(mi))
366 debug_printf(DEBUG_WARNING,"not connected\n");
367 return NULL;
369 if(mpd_lock_conn(mi))
371 debug_printf(DEBUG_WARNING,"lock failed\n");
372 return NULL;
375 if(old_playlist_id == -1)
377 debug_printf(DEBUG_INFO,"get fresh playlist\n");
378 mpd_sendPlChangesCommand (mi->connection, 0);
379 /* mpd_sendPlaylistIdCommand(mi->connection, -1); */
381 else
383 mpd_sendPlChangesCommand (mi->connection, old_playlist_id);
386 while (( ent = mpd_getNextInfoEntity(mi->connection)) != NULL)
388 if(ent->type == MPD_INFO_ENTITY_TYPE_SONG)
390 data = mpd_new_data_struct_append(data);
391 data->type = MPD_DATA_TYPE_SONG;
392 data->song = ent->info.song;
393 ent->info.song = NULL;
395 mpd_freeInfoEntity(ent);
397 mpd_finishCommand(mi->connection);
399 /* unlock */
400 if(mpd_unlock_conn(mi))
402 debug_printf(DEBUG_WARNING,"mpd_playlist_get_changes: unlock failed.\n");
403 mpd_data_free(data);
404 return NULL;
406 if(data == NULL)
408 return NULL;
410 return mpd_data_get_first(data);
415 MpdData * mpd_playlist_get_changes_posid(MpdObj *mi,int old_playlist_id)
417 MpdData *data = NULL;
418 mpd_InfoEntity *ent = NULL;
419 debug_printf(DEBUG_INFO, "Fetching using new plchangesposid command");
420 if(!mpd_check_connected(mi))
422 debug_printf(DEBUG_WARNING,"not connected\n");
423 return NULL;
425 if(mpd_lock_conn(mi))
427 debug_printf(DEBUG_WARNING,"lock failed\n");
428 return NULL;
431 if(old_playlist_id == -1)
433 debug_printf(DEBUG_INFO,"get fresh playlist\n");
434 mpd_sendPlChangesPosIdCommand (mi->connection, 0);
435 /* mpd_sendPlaylistIdCommand(mi->connection, -1); */
437 else
439 mpd_sendPlChangesPosIdCommand (mi->connection, old_playlist_id);
442 while (( ent = mpd_getNextInfoEntity(mi->connection)) != NULL)
444 if(ent->type == MPD_INFO_ENTITY_TYPE_SONG)
446 data = mpd_new_data_struct_append(data);
447 data->type = MPD_DATA_TYPE_SONG;
448 data->song = ent->info.song;
449 ent->info.song = NULL;
451 mpd_freeInfoEntity(ent);
453 mpd_finishCommand(mi->connection);
455 /* unlock */
456 if(mpd_unlock_conn(mi))
458 debug_printf(DEBUG_WARNING,"mpd_playlist_get_changes: unlock failed.\n");
459 mpd_data_free(data);
460 return NULL;
462 if(data == NULL)
464 return NULL;
466 return mpd_data_get_first(data);
469 int mpd_playlist_queue_add(MpdObj *mi,const char *path)
471 if(!mpd_check_connected(mi))
473 debug_printf(DEBUG_WARNING,"not connected\n");
474 return MPD_NOT_CONNECTED;
476 if(path == NULL)
478 debug_printf(DEBUG_ERROR, "path != NULL Failed");
479 return MPD_ARGS_ERROR;
482 if(mi->queue == NULL)
484 mi->queue = mpd_new_queue_struct();
485 mi->queue->first = mi->queue;
486 mi->queue->next = NULL;
487 mi->queue->prev = NULL;
489 else
491 mi->queue->next = mpd_new_queue_struct();
492 mi->queue->next->first = mi->queue->first;
493 mi->queue->next->prev = mi->queue;
494 mi->queue = mi->queue->next;
495 mi->queue->next = NULL;
497 mi->queue->type = MPD_QUEUE_ADD;
498 mi->queue->path = strdup(path);
499 return MPD_OK;
502 int mpd_playlist_queue_load(MpdObj *mi,const char *path)
504 if(!mpd_check_connected(mi))
506 debug_printf(DEBUG_WARNING,"not connected\n");
507 return MPD_NOT_CONNECTED;
509 if(path == NULL)
511 debug_printf(DEBUG_ERROR, "path != NULL Failed");
512 return MPD_ARGS_ERROR;
515 if(mi->queue == NULL)
517 mi->queue = mpd_new_queue_struct();
518 mi->queue->first = mi->queue;
519 mi->queue->next = NULL;
520 mi->queue->prev = NULL;
522 else
524 mi->queue->next = mpd_new_queue_struct();
525 mi->queue->next->first = mi->queue->first;
526 mi->queue->next->prev = mi->queue;
527 mi->queue = mi->queue->next;
528 mi->queue->next = NULL;
530 mi->queue->type = MPD_QUEUE_LOAD;
531 mi->queue->path = strdup(path);
532 return MPD_OK;
536 int mpd_playlist_queue_commit(MpdObj *mi)
538 if(!mpd_check_connected(mi))
540 debug_printf(DEBUG_WARNING,"not connected\n");
541 return MPD_NOT_CONNECTED;
543 if(mi->queue == NULL)
545 debug_printf(DEBUG_WARNING,"mi->queue is empty");
546 return MPD_PLAYLIST_QUEUE_EMPTY;
548 if(mpd_lock_conn(mi))
550 debug_printf(DEBUG_WARNING,"lock failed\n");
551 return MPD_LOCK_FAILED;
553 mpd_sendCommandListBegin(mi->connection);
554 /* get first item */
555 mi->queue = mi->queue->first;
556 while(mi->queue != NULL)
558 if(mi->queue->type == MPD_QUEUE_ADD)
560 if(mi->queue->path != NULL)
562 mpd_sendAddCommand(mi->connection, mi->queue->path);
565 else if(mi->queue->type == MPD_QUEUE_LOAD)
567 if(mi->queue->path != NULL)
569 mpd_sendLoadCommand(mi->connection, mi->queue->path);
572 else if (mi->queue->type == MPD_QUEUE_DELETE_ID)
574 if(mi->queue->id >= 0)
576 mpd_sendDeleteIdCommand(mi->connection, mi->queue->id);
579 else if (mi->queue->type == MPD_QUEUE_DELETE_POS)
581 if(mi->queue->id >= 0)
583 mpd_sendDeleteCommand(mi->connection, mi->queue->id);
587 mpd_queue_get_next(mi);
589 mpd_sendCommandListEnd(mi->connection);
590 mpd_finishCommand(mi->connection);
591 mpd_unlock_conn(mi);
592 mpd_status_update(mi);
593 return MPD_OK;
595 int mpd_playlist_queue_delete_id(MpdObj *mi,int id)
597 if(!mpd_check_connected(mi))
599 debug_printf(DEBUG_WARNING,"not connected\n");
600 return MPD_NOT_CONNECTED;
603 if(mi->queue == NULL)
605 mi->queue = mpd_new_queue_struct();
606 mi->queue->first = mi->queue;
607 mi->queue->next = NULL;
608 mi->queue->prev = NULL;
610 else
612 mi->queue->next = mpd_new_queue_struct();
613 mi->queue->next->first = mi->queue->first;
614 mi->queue->next->prev = mi->queue;
615 mi->queue = mi->queue->next;
616 mi->queue->next = NULL;
618 mi->queue->type = MPD_QUEUE_DELETE_ID;
619 mi->queue->id = id;
620 mi->queue->path = NULL;
621 return MPD_OK;
624 int mpd_playlist_queue_delete_pos(MpdObj *mi,int songpos)
626 if(!mpd_check_connected(mi))
628 debug_printf(DEBUG_WARNING,"mpd_playlist_add: not connected\n");
629 return MPD_NOT_CONNECTED;
632 if(mi->queue == NULL)
634 mi->queue = mpd_new_queue_struct();
635 mi->queue->first = mi->queue;
636 mi->queue->next = NULL;
637 mi->queue->prev = NULL;
639 else
641 mi->queue->next = mpd_new_queue_struct();
642 mi->queue->next->first = mi->queue->first;
643 mi->queue->next->prev = mi->queue;
644 mi->queue = mi->queue->next;
645 mi->queue->next = NULL;
647 mi->queue->type = MPD_QUEUE_DELETE_POS;
648 mi->queue->id = songpos;
649 mi->queue->path = NULL;
650 return MPD_OK;
653 int mpd_playlist_add_get_id(MpdObj *mi,const char *path)
655 int songid = -1;
656 if(mi == NULL || path == NULL)
658 debug_printf(DEBUG_ERROR, "mi == NULL || path == NULL failed");
659 return MPD_ARGS_ERROR;
661 if(!mpd_check_connected(mi))
663 debug_printf(DEBUG_WARNING,"mpd_playlist_add: not connected\n");
664 return MPD_NOT_CONNECTED;
666 if(mpd_lock_conn(mi))
668 debug_printf(DEBUG_WARNING,"lock failed\n");
669 return MPD_LOCK_FAILED;
671 songid = mpd_sendAddIdCommand(mi->connection, path);
672 mpd_finishCommand(mi->connection);
674 mpd_unlock_conn(mi);
675 return songid;
678 void mpd_playlist_search_start(MpdObj *mi, int exact)
681 * Check argument
683 if(mi == NULL || exact > 1 || exact < 0)
685 debug_printf(DEBUG_ERROR, "Argument error");
686 return ;
688 if(!mpd_check_connected(mi))
690 debug_printf(DEBUG_ERROR, "Not Connected\n");
691 return ;
693 if(!mpd_server_check_version(mi, 0,12,1))
695 debug_printf(DEBUG_ERROR, "Advanced search requires mpd 0.12.2 or higher");
696 return ;
698 /* lock, so we can work on mi->connection */
699 if(mpd_lock_conn(mi) != MPD_OK)
701 debug_printf(DEBUG_ERROR, "Failed to lock connection");
702 return ;
704 mpd_startPlaylistSearch(mi->connection, exact);
705 /* Set search type */
706 mi->search_type = (exact)? MPD_SEARCH_TYPE_PLAYLIST_FIND:MPD_SEARCH_TYPE_PLAYLIST_SEARCH;
707 /* unlock, let the error handler handle any possible error.
709 mpd_unlock_conn(mi);
710 return;
713 void mpd_playlist_search_add_constraint(MpdObj *mi, mpd_TagItems field,const char *value)
715 mpd_database_search_add_constraint(mi, field, value);
717 MpdData * mpd_playlist_search_commit(MpdObj *mi)
719 mpd_InfoEntity *ent = NULL;
720 MpdData *data = NULL;
721 if(!mpd_check_connected(mi))
723 debug_printf(DEBUG_WARNING,"not connected\n");
724 return NULL;
726 if(mi->search_type < MPD_SEARCH_TYPE_PLAYLIST_FIND )
728 debug_printf(DEBUG_ERROR, "no or wrong search in progress to commit");
729 return NULL;
731 if(mpd_lock_conn(mi))
733 debug_printf(DEBUG_ERROR,"lock failed\n");
734 return NULL;
736 mpd_commitSearch(mi->connection);
737 while (( ent = mpd_getNextInfoEntity(mi->connection)) != NULL)
739 if(ent->type == MPD_INFO_ENTITY_TYPE_SONG)
741 data = mpd_new_data_struct_append(data);
742 data->type = MPD_DATA_TYPE_SONG;
743 data->song = ent->info.song;
744 ent->info.song = NULL;
746 mpd_freeInfoEntity(ent);
748 mpd_finishCommand(mi->connection);
750 * reset search type
752 mi->search_type = MPD_SEARCH_TYPE_NONE;
753 mi->search_field = MPD_TAG_ITEM_ARTIST;
754 /* unlock */
755 if(mpd_unlock_conn(mi))
757 debug_printf(DEBUG_ERROR, "Failed to unlock connection");
758 if(data)mpd_data_free(data);
759 return NULL;
761 if(data == NULL)
763 return NULL;
765 return mpd_data_get_first(data);