Code

use libmpdclient2
[ncmpc.git] / src / playlist.h
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 #ifndef MPDCLIENT_PLAYLIST_H
21 #define MPDCLIENT_PLAYLIST_H
23 #include <mpd/client.h>
25 #include <assert.h>
26 #include <glib.h>
28 struct mpdclient;
30 typedef struct mpdclient_playlist {
31         /* playlist id */
32         unsigned id;
34         /* the list */
35         GPtrArray *list;
36 } mpdclient_playlist_t;
38 void
39 playlist_init(struct mpdclient_playlist *playlist);
41 /** remove and free all songs in the playlist */
42 void
43 playlist_clear(struct mpdclient_playlist *playlist);
45 /* free a playlist */
46 gint mpdclient_playlist_free(mpdclient_playlist_t *playlist);
48 static inline guint
49 playlist_length(const struct mpdclient_playlist *playlist)
50 {
51         assert(playlist != NULL);
52         assert(playlist->list != NULL);
54         return playlist->list->len;
55 }
57 static inline gboolean
58 playlist_is_empty(const struct mpdclient_playlist *playlist)
59 {
60         return playlist_length(playlist) == 0;
61 }
63 static inline struct mpd_song *
64 playlist_get(const struct mpdclient_playlist *playlist, guint idx)
65 {
66         assert(idx < playlist_length(playlist));
68         return g_ptr_array_index(playlist->list, idx);
69 }
71 static inline void
72 playlist_append(struct mpdclient_playlist *playlist, const struct mpd_song *song)
73 {
74         g_ptr_array_add(playlist->list, mpd_song_dup(song));
75 }
77 static inline void
78 playlist_set(const struct mpdclient_playlist *playlist, guint idx,
79              const struct mpd_song *song)
80 {
81         assert(idx < playlist_length(playlist));
83         g_ptr_array_index(playlist->list, idx) = mpd_song_dup(song);
84 }
86 static inline void
87 playlist_replace(struct mpdclient_playlist *playlist, guint idx,
88                  const struct mpd_song *song)
89 {
90         mpd_song_free(playlist_get(playlist, idx));
91         playlist_set(playlist, idx, song);
92 }
94 static inline struct mpd_song *
95 playlist_remove_reuse(struct mpdclient_playlist *playlist, guint idx)
96 {
97         return g_ptr_array_remove_index(playlist->list, idx);
98 }
100 static inline void
101 playlist_remove(struct mpdclient_playlist *playlist, guint idx)
103         mpd_song_free(playlist_remove_reuse(playlist, idx));
106 static inline void
107 playlist_swap(struct mpdclient_playlist *playlist, guint idx1, guint idx2)
109         struct mpd_song *song1 = playlist_get(playlist, idx1);
110         struct mpd_song *song2 = playlist_get(playlist, idx2);
111         int n;
113         /* update the songs position field */
114         n = mpd_song_get_pos(song1);
115         mpd_song_set_pos(song1, mpd_song_get_pos(song2));
116         mpd_song_set_pos(song2, n);
118         /* update the array */
119         g_ptr_array_index(playlist->list, idx1) = song2;
120         g_ptr_array_index(playlist->list, idx2) = song1;
123 struct mpd_song *playlist_lookup_song(struct mpdclient *c, unsigned id);
125 struct mpd_song *playlist_get_song(struct mpdclient *c, gint index);
127 gint
128 playlist_get_index(const struct mpdclient *c, const struct mpd_song *song);
130 gint
131 playlist_get_index_from_id(const struct mpdclient *c, unsigned id);
133 gint
134 playlist_get_index_from_file(const struct mpdclient *c, const gchar *filename);
136 static inline gint
137 playlist_get_index_from_same_song(const struct mpdclient *c,
138                                   const struct mpd_song *song)
140         return playlist_get_index_from_file(c, mpd_song_get_uri(song));
143 #endif