1 /*
2 * $Id$
3 *
4 * (c) 2004 by Kalle Wallin <kaw@linux.se>
5 * (c) 2008 Max Kellermann <max@duempel.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
22 #include "playlist.h"
23 #include "mpdclient.h"
25 #include <string.h>
27 #define ENABLE_PLCHANGES
29 #define MPD_ERROR(c) (c==NULL || c->connection==NULL || c->connection->error)
31 void
32 playlist_init(struct mpdclient_playlist *playlist)
33 {
34 playlist->id = 0;
35 playlist->list = g_ptr_array_sized_new(1024);
36 }
38 void
39 playlist_clear(struct mpdclient_playlist *playlist)
40 {
41 guint i;
43 playlist->id = 0;
45 for (i = 0; i < playlist->list->len; ++i) {
46 struct mpd_song *song = playlist_get(playlist, i);
48 mpd_freeSong(song);
49 }
51 g_ptr_array_set_size(playlist->list, 0);
52 }
54 gint
55 mpdclient_playlist_free(mpdclient_playlist_t *playlist)
56 {
57 if (playlist->list != NULL) {
58 playlist_clear(playlist);
59 g_ptr_array_free(playlist->list, TRUE);
60 }
62 memset(playlist, 0, sizeof(mpdclient_playlist_t));
63 return 0;
64 }
66 struct mpd_song *
67 playlist_get_song(mpdclient_t *c, gint idx)
68 {
69 if (idx < 0 || (guint)idx >= c->playlist.list->len)
70 return NULL;
72 return playlist_get(&c->playlist, idx);
73 }
75 struct mpd_song *
76 playlist_lookup_song(mpdclient_t *c, gint id)
77 {
78 guint i;
80 for (i = 0; i < c->playlist.list->len; ++i) {
81 struct mpd_song *song = playlist_get(&c->playlist, i);
82 if (song->id == id)
83 return song;
84 }
86 return NULL;
87 }
89 gint
90 playlist_get_index(mpdclient_t *c, struct mpd_song *song)
91 {
92 guint i;
94 for (i = 0; i < c->playlist.list->len; ++i) {
95 if (playlist_get(&c->playlist, i) == song)
96 return (gint)i;
97 }
99 return -1;
100 }
102 gint
103 playlist_get_index_from_id(mpdclient_t *c, gint id)
104 {
105 guint i;
107 for (i = 0; i < c->playlist.list->len; ++i) {
108 struct mpd_song *song = playlist_get(&c->playlist, i);
109 if (song->id == id)
110 return (gint)i;
111 }
113 return -1;
114 }
116 gint
117 playlist_get_index_from_file(mpdclient_t *c, gchar *filename)
118 {
119 guint i;
121 for (i = 0; i < c->playlist.list->len; ++i) {
122 struct mpd_song *song = playlist_get(&c->playlist, i);
123 if(strcmp(song->file, filename) == 0)
124 return (gint)i;
125 }
127 return -1;
128 }