configure.ac: require GLib 2.14
[ncmpc.git] / src / playlist.c
blob8ec007be47d27b2ff61e0dc735aae805eba1e767
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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.
20 #include "playlist.h"
22 #include <string.h>
24 void
25 playlist_init(struct mpdclient_playlist *playlist)
27 playlist->version = 0;
28 playlist->list = g_ptr_array_sized_new(1024);
31 void
32 playlist_clear(struct mpdclient_playlist *playlist)
34 guint i;
36 playlist->version = 0;
38 for (i = 0; i < playlist->list->len; ++i) {
39 struct mpd_song *song = playlist_get(playlist, i);
41 mpd_song_free(song);
44 g_ptr_array_set_size(playlist->list, 0);
47 gint
48 mpdclient_playlist_free(struct mpdclient_playlist *playlist)
50 if (playlist->list != NULL) {
51 playlist_clear(playlist);
52 g_ptr_array_free(playlist->list, TRUE);
55 memset(playlist, 0, sizeof(*playlist));
56 return 0;
59 const struct mpd_song *
60 playlist_get_song(const struct mpdclient_playlist *playlist, gint idx)
62 if (idx < 0 || (guint)idx >= playlist_length(playlist))
63 return NULL;
65 return playlist_get(playlist, idx);
68 void
69 playlist_move(struct mpdclient_playlist *playlist,
70 unsigned dest, unsigned src)
72 struct mpd_song *song;
74 assert(playlist != NULL);
75 assert(src < playlist_length(playlist));
76 assert(dest < playlist_length(playlist));
77 assert(src != dest);
79 song = playlist_get(playlist, src);
81 if (src < dest) {
82 memmove(&playlist->list->pdata[src],
83 &playlist->list->pdata[src + 1],
84 sizeof(playlist->list->pdata[0]) * (dest - src));
85 playlist->list->pdata[dest] = song;
86 } else {
87 memmove(&playlist->list->pdata[dest + 1],
88 &playlist->list->pdata[dest],
89 sizeof(playlist->list->pdata[0]) * (src - dest));
90 playlist->list->pdata[dest] = song;
94 const struct mpd_song *
95 playlist_lookup_song(const struct mpdclient_playlist *playlist, unsigned id)
97 for (guint i = 0; i < playlist_length(playlist); ++i) {
98 struct mpd_song *song = playlist_get(playlist, i);
99 if (mpd_song_get_id(song) == id)
100 return song;
103 return NULL;
106 gint
107 playlist_get_index(const struct mpdclient_playlist *playlist,
108 const struct mpd_song *song)
110 for (guint i = 0; i < playlist_length(playlist); ++i) {
111 if (playlist_get(playlist, i) == song)
112 return (gint)i;
115 return -1;
118 gint
119 playlist_get_index_from_id(const struct mpdclient_playlist *playlist,
120 unsigned id)
122 for (guint i = 0; i < playlist_length(playlist); ++i) {
123 const struct mpd_song *song = playlist_get(playlist, i);
124 if (mpd_song_get_id(song) == id)
125 return (gint)i;
128 return -1;
131 gint
132 playlist_get_index_from_file(const struct mpdclient_playlist *playlist,
133 const gchar *filename)
135 for (guint i = 0; i < playlist_length(playlist); ++i) {
136 const struct mpd_song *song = playlist_get(playlist, i);
138 if (strcmp(mpd_song_get_uri(song), filename) == 0)
139 return (gint)i;
142 return -1;
145 gint
146 playlist_get_id_from_uri(const struct mpdclient_playlist *playlist,
147 const gchar *uri)
149 for (guint i = 0; i < playlist_length(playlist); ++i) {
150 const struct mpd_song *song = playlist_get(playlist, i);
152 if (strcmp(mpd_song_get_uri(song), uri) == 0)
153 return mpd_song_get_id(song);
156 return -1;