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"
24 #include "ncmpc.h"
26 #include <string.h>
28 #define ENABLE_PLCHANGES
30 #define MPD_ERROR(c) (c==NULL || c->connection==NULL || c->connection->error)
32 void
33 playlist_init(struct mpdclient_playlist *playlist)
34 {
35 playlist->id = 0;
36 playlist->list = g_ptr_array_sized_new(1024);
37 }
39 void
40 playlist_clear(struct mpdclient_playlist *playlist)
41 {
42 guint i;
44 playlist->id = 0;
46 for (i = 0; i < playlist->list->len; ++i) {
47 struct mpd_song *song = playlist_get(playlist, i);
49 mpd_freeSong(song);
50 }
52 g_ptr_array_set_size(playlist->list, 0);
53 }
55 gint
56 mpdclient_playlist_free(mpdclient_playlist_t *playlist)
57 {
58 if (playlist->list != NULL) {
59 playlist_clear(playlist);
60 g_ptr_array_free(playlist->list, TRUE);
61 }
63 memset(playlist, 0, sizeof(mpdclient_playlist_t));
64 return 0;
65 }
67 struct mpd_song *
68 playlist_get_song(mpdclient_t *c, gint idx)
69 {
70 if (idx < 0 || (guint)idx >= c->playlist.list->len)
71 return NULL;
73 return playlist_get(&c->playlist, idx);
74 }
76 struct mpd_song *
77 playlist_lookup_song(mpdclient_t *c, gint id)
78 {
79 guint i;
81 for (i = 0; i < c->playlist.list->len; ++i) {
82 struct mpd_song *song = playlist_get(&c->playlist, i);
83 if (song->id == id)
84 return song;
85 }
87 return NULL;
88 }
90 gint
91 playlist_get_index(mpdclient_t *c, struct mpd_song *song)
92 {
93 guint i;
95 for (i = 0; i < c->playlist.list->len; ++i) {
96 if (playlist_get(&c->playlist, i) == song)
97 return (gint)i;
98 }
100 return -1;
101 }
103 gint
104 playlist_get_index_from_id(mpdclient_t *c, gint id)
105 {
106 guint i;
108 for (i = 0; i < c->playlist.list->len; ++i) {
109 struct mpd_song *song = playlist_get(&c->playlist, i);
110 if (song->id == id)
111 return (gint)i;
112 }
114 return -1;
115 }
117 gint
118 playlist_get_index_from_file(mpdclient_t *c, gchar *filename)
119 {
120 guint i;
122 for (i = 0; i < c->playlist.list->len; ++i) {
123 struct mpd_song *song = playlist_get(&c->playlist, i);
124 if(strcmp(song->file, filename) == 0)
125 return (gint)i;
126 }
128 return -1;
129 }