1 /*
2 * (c) 2004 by Kalle Wallin <kaw@linux.se>
3 * (c) 2008 Max Kellermann <max@duempel.org>
4 *
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.
9 *
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 */
20 #include "playlist.h"
21 #include "mpdclient.h"
23 #include <string.h>
25 #define ENABLE_PLCHANGES
27 #define MPD_ERROR(c) (c==NULL || c->connection==NULL || c->connection->error)
29 void
30 playlist_init(struct mpdclient_playlist *playlist)
31 {
32 playlist->id = 0;
33 playlist->list = g_ptr_array_sized_new(1024);
34 }
36 void
37 playlist_clear(struct mpdclient_playlist *playlist)
38 {
39 guint i;
41 playlist->id = 0;
43 for (i = 0; i < playlist->list->len; ++i) {
44 struct mpd_song *song = playlist_get(playlist, i);
46 mpd_freeSong(song);
47 }
49 g_ptr_array_set_size(playlist->list, 0);
50 }
52 gint
53 mpdclient_playlist_free(mpdclient_playlist_t *playlist)
54 {
55 if (playlist->list != NULL) {
56 playlist_clear(playlist);
57 g_ptr_array_free(playlist->list, TRUE);
58 }
60 memset(playlist, 0, sizeof(mpdclient_playlist_t));
61 return 0;
62 }
64 struct mpd_song *
65 playlist_get_song(mpdclient_t *c, gint idx)
66 {
67 if (idx < 0 || (guint)idx >= c->playlist.list->len)
68 return NULL;
70 return playlist_get(&c->playlist, idx);
71 }
73 struct mpd_song *
74 playlist_lookup_song(mpdclient_t *c, gint id)
75 {
76 guint i;
78 for (i = 0; i < c->playlist.list->len; ++i) {
79 struct mpd_song *song = playlist_get(&c->playlist, i);
80 if (song->id == id)
81 return song;
82 }
84 return NULL;
85 }
87 gint
88 playlist_get_index(mpdclient_t *c, struct mpd_song *song)
89 {
90 guint i;
92 for (i = 0; i < c->playlist.list->len; ++i) {
93 if (playlist_get(&c->playlist, i) == song)
94 return (gint)i;
95 }
97 return -1;
98 }
100 gint
101 playlist_get_index_from_id(mpdclient_t *c, gint id)
102 {
103 guint i;
105 for (i = 0; i < c->playlist.list->len; ++i) {
106 struct mpd_song *song = playlist_get(&c->playlist, i);
107 if (song->id == id)
108 return (gint)i;
109 }
111 return -1;
112 }
114 gint
115 playlist_get_index_from_file(mpdclient_t *c, gchar *filename)
116 {
117 guint i;
119 for (i = 0; i < c->playlist.list->len; ++i) {
120 struct mpd_song *song = playlist_get(&c->playlist, i);
121 if(strcmp(song->file, filename) == 0)
122 return (gint)i;
123 }
125 return -1;
126 }