Code

playlist: renamed "id" to "version"
[ncmpc.git] / src / playlist.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 The Music Player Daemon Project
3  * Project homepage: http://musicpd.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.
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.
18 */
20 #include "playlist.h"
22 #include <string.h>
24 #define ENABLE_PLCHANGES
26 #define MPD_ERROR(c) (c==NULL || c->connection==NULL || c->connection->error)
28 void
29 playlist_init(struct mpdclient_playlist *playlist)
30 {
31         playlist->version = 0;
32         playlist->list = g_ptr_array_sized_new(1024);
33 }
35 void
36 playlist_clear(struct mpdclient_playlist *playlist)
37 {
38         guint i;
40         playlist->version = 0;
42         for (i = 0; i < playlist->list->len; ++i) {
43                 struct mpd_song *song = playlist_get(playlist, i);
45                 mpd_song_free(song);
46         }
48         g_ptr_array_set_size(playlist->list, 0);
49 }
51 gint
52 mpdclient_playlist_free(struct mpdclient_playlist *playlist)
53 {
54         if (playlist->list != NULL) {
55                 playlist_clear(playlist);
56                 g_ptr_array_free(playlist->list, TRUE);
57         }
59         memset(playlist, 0, sizeof(*playlist));
60         return 0;
61 }
63 const struct mpd_song *
64 playlist_get_song(const struct mpdclient_playlist *playlist, gint idx)
65 {
66         if (idx < 0 || (guint)idx >= playlist_length(playlist))
67                 return NULL;
69         return playlist_get(playlist, idx);
70 }
72 const struct mpd_song *
73 playlist_lookup_song(const struct mpdclient_playlist *playlist, unsigned id)
74 {
75         for (guint i = 0; i < playlist_length(playlist); ++i) {
76                 struct mpd_song *song = playlist_get(playlist, i);
77                 if (mpd_song_get_id(song) == id)
78                         return song;
79         }
81         return NULL;
82 }
84 gint
85 playlist_get_index(const struct mpdclient_playlist *playlist,
86                    const struct mpd_song *song)
87 {
88         for (guint i = 0; i < playlist_length(playlist); ++i) {
89                 if (playlist_get(playlist, i) == song)
90                         return (gint)i;
91         }
93         return -1;
94 }
96 gint
97 playlist_get_index_from_id(const struct mpdclient_playlist *playlist,
98                            unsigned id)
99 {
100         for (guint i = 0; i < playlist_length(playlist); ++i) {
101                 const struct mpd_song *song = playlist_get(playlist, i);
102                 if (mpd_song_get_id(song) == id)
103                         return (gint)i;
104         }
106         return -1;
109 gint
110 playlist_get_index_from_file(const struct mpdclient_playlist *playlist,
111                              const gchar *filename)
113         for (guint i = 0; i < playlist_length(playlist); ++i) {
114                 const struct mpd_song *song = playlist_get(playlist, i);
116                 if (strcmp(mpd_song_get_uri(song), filename) == 0)
117                         return (gint)i;
118         }
120         return -1;
123 gint
124 playlist_get_id_from_uri(const struct mpdclient_playlist *playlist,
125                          const gchar *uri)
127         for (guint i = 0; i < playlist_length(playlist); ++i) {
128                 const struct mpd_song *song = playlist_get(playlist, i);
130                 if (strcmp(mpd_song_get_uri(song), uri) == 0)
131                         return mpd_song_get_id(song);
132         }
134         return -1;