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.
25 playlist_init(struct mpdclient_playlist
*playlist
)
27 playlist
->version
= 0;
28 playlist
->list
= g_ptr_array_sized_new(1024);
32 playlist_clear(struct mpdclient_playlist
*playlist
)
36 playlist
->version
= 0;
38 for (i
= 0; i
< playlist
->list
->len
; ++i
) {
39 struct mpd_song
*song
= playlist_get(playlist
, i
);
44 g_ptr_array_set_size(playlist
->list
, 0);
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
));
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
))
65 return playlist_get(playlist
, idx
);
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
));
79 song
= playlist_get(playlist
, src
);
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
;
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
)
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
)
119 playlist_get_index_from_id(const struct mpdclient_playlist
*playlist
,
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
)
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)
146 playlist_get_id_from_uri(const struct mpdclient_playlist
*playlist
,
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
);