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 for (i = 0; i < playlist->list->len; ++i) {
45 struct mpd_song *song = playlist_get(playlist, i);
47 mpd_freeSong(song);
48 }
50 g_ptr_array_set_size(playlist->list, 0);
51 }
53 gint
54 mpdclient_playlist_free(mpdclient_playlist_t *playlist)
55 {
56 if (playlist->list != NULL) {
57 playlist_clear(playlist);
58 g_ptr_array_free(playlist->list, TRUE);
59 }
61 memset(playlist, 0, sizeof(mpdclient_playlist_t));
62 return 0;
63 }
65 struct mpd_song *
66 playlist_get_song(mpdclient_t *c, gint idx)
67 {
68 if (idx < 0 || (guint)idx >= c->playlist.list->len)
69 return NULL;
71 return playlist_get(&c->playlist, idx);
72 }
74 struct mpd_song *
75 playlist_lookup_song(mpdclient_t *c, gint id)
76 {
77 guint i;
79 for (i = 0; i < c->playlist.list->len; ++i) {
80 struct mpd_song *song = playlist_get(&c->playlist, i);
81 if (song->id == id)
82 return song;
83 }
85 return NULL;
86 }
88 gint
89 playlist_get_index(mpdclient_t *c, struct mpd_song *song)
90 {
91 guint i;
93 for (i = 0; i < c->playlist.list->len; ++i) {
94 if (playlist_get(&c->playlist, i) == song)
95 return (gint)i;
96 }
98 return -1;
99 }
101 gint
102 playlist_get_index_from_id(mpdclient_t *c, gint id)
103 {
104 guint i;
106 for (i = 0; i < c->playlist.list->len; ++i) {
107 struct mpd_song *song = playlist_get(&c->playlist, i);
108 if (song->id == id)
109 return (gint)i;
110 }
112 return -1;
113 }
115 gint
116 playlist_get_index_from_file(mpdclient_t *c, gchar *filename)
117 {
118 guint i;
120 for (i = 0; i < c->playlist.list->len; ++i) {
121 struct mpd_song *song = playlist_get(&c->playlist, i);
122 if(strcmp(song->file, filename) == 0)
123 return (gint)i;
124 }
126 return -1;
127 }