1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 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.
18 */
20 #include "playlist.h"
22 #include <string.h>
24 void
25 playlist_init(struct mpdclient_playlist *playlist)
26 {
27 playlist->version = 0;
28 playlist->list = g_ptr_array_sized_new(1024);
29 }
31 void
32 playlist_clear(struct mpdclient_playlist *playlist)
33 {
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);
42 }
44 g_ptr_array_set_size(playlist->list, 0);
45 }
47 gint
48 mpdclient_playlist_free(struct mpdclient_playlist *playlist)
49 {
50 if (playlist->list != NULL) {
51 playlist_clear(playlist);
52 g_ptr_array_free(playlist->list, TRUE);
53 }
55 memset(playlist, 0, sizeof(*playlist));
56 return 0;
57 }
59 const struct mpd_song *
60 playlist_get_song(const struct mpdclient_playlist *playlist, gint idx)
61 {
62 if (idx < 0 || (guint)idx >= playlist_length(playlist))
63 return NULL;
65 return playlist_get(playlist, idx);
66 }
68 const struct mpd_song *
69 playlist_lookup_song(const struct mpdclient_playlist *playlist, unsigned id)
70 {
71 for (guint i = 0; i < playlist_length(playlist); ++i) {
72 struct mpd_song *song = playlist_get(playlist, i);
73 if (mpd_song_get_id(song) == id)
74 return song;
75 }
77 return NULL;
78 }
80 gint
81 playlist_get_index(const struct mpdclient_playlist *playlist,
82 const struct mpd_song *song)
83 {
84 for (guint i = 0; i < playlist_length(playlist); ++i) {
85 if (playlist_get(playlist, i) == song)
86 return (gint)i;
87 }
89 return -1;
90 }
92 gint
93 playlist_get_index_from_id(const struct mpdclient_playlist *playlist,
94 unsigned id)
95 {
96 for (guint i = 0; i < playlist_length(playlist); ++i) {
97 const struct mpd_song *song = playlist_get(playlist, i);
98 if (mpd_song_get_id(song) == id)
99 return (gint)i;
100 }
102 return -1;
103 }
105 gint
106 playlist_get_index_from_file(const struct mpdclient_playlist *playlist,
107 const gchar *filename)
108 {
109 for (guint i = 0; i < playlist_length(playlist); ++i) {
110 const struct mpd_song *song = playlist_get(playlist, i);
112 if (strcmp(mpd_song_get_uri(song), filename) == 0)
113 return (gint)i;
114 }
116 return -1;
117 }
119 gint
120 playlist_get_id_from_uri(const struct mpdclient_playlist *playlist,
121 const gchar *uri)
122 {
123 for (guint i = 0; i < playlist_length(playlist); ++i) {
124 const struct mpd_song *song = playlist_get(playlist, i);
126 if (strcmp(mpd_song_get_uri(song), uri) == 0)
127 return mpd_song_get_id(song);
128 }
130 return -1;
131 }