Code

45046880869d3c545f42158c767c8dd32b3f8ad9
[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_playlist {
29         /* queue version number (obtained from mpd_status) */
30         unsigned version;
32         /* the list */
33         GPtrArray *list;
34 };
36 void
37 playlist_init(struct mpdclient_playlist *playlist);
39 /** remove and free all songs in the playlist */
40 void
41 playlist_clear(struct mpdclient_playlist *playlist);
43 /* free a playlist */
44 gint
45 mpdclient_playlist_free(struct mpdclient_playlist *playlist);
47 static inline guint
48 playlist_length(const struct mpdclient_playlist *playlist)
49 {
50         assert(playlist != NULL);
51         assert(playlist->list != NULL);
53         return playlist->list->len;
54 }
56 static inline gboolean
57 playlist_is_empty(const struct mpdclient_playlist *playlist)
58 {
59         return playlist_length(playlist) == 0;
60 }
62 static inline struct mpd_song *
63 playlist_get(const struct mpdclient_playlist *playlist, guint idx)
64 {
65         assert(idx < playlist_length(playlist));
67         return g_ptr_array_index(playlist->list, idx);
68 }
70 static inline void
71 playlist_append(struct mpdclient_playlist *playlist, const struct mpd_song *song)
72 {
73         g_ptr_array_add(playlist->list, mpd_song_dup(song));
74 }
76 static inline void
77 playlist_set(const struct mpdclient_playlist *playlist, guint idx,
78              const struct mpd_song *song)
79 {
80         assert(idx < playlist_length(playlist));
82         g_ptr_array_index(playlist->list, idx) = mpd_song_dup(song);
83 }
85 static inline void
86 playlist_replace(struct mpdclient_playlist *playlist, guint idx,
87                  const struct mpd_song *song)
88 {
89         mpd_song_free(playlist_get(playlist, idx));
90         playlist_set(playlist, idx, song);
91 }
93 static inline struct mpd_song *
94 playlist_remove_reuse(struct mpdclient_playlist *playlist, guint idx)
95 {
96         return g_ptr_array_remove_index(playlist->list, idx);
97 }
99 static inline void
100 playlist_remove(struct mpdclient_playlist *playlist, guint idx)
102         mpd_song_free(playlist_remove_reuse(playlist, idx));
105 static inline void
106 playlist_swap(struct mpdclient_playlist *playlist, guint idx1, guint idx2)
108         struct mpd_song *song1 = playlist_get(playlist, idx1);
109         struct mpd_song *song2 = playlist_get(playlist, idx2);
111         /* update the array */
112         g_ptr_array_index(playlist->list, idx1) = song2;
113         g_ptr_array_index(playlist->list, idx2) = song1;
116 const struct mpd_song *
117 playlist_lookup_song(const struct mpdclient_playlist *playlist, unsigned id);
119 const struct mpd_song *
120 playlist_get_song(const struct mpdclient_playlist *playlist, gint index);
122 gint
123 playlist_get_index(const struct mpdclient_playlist *playlist,
124                    const struct mpd_song *song);
126 gint
127 playlist_get_index_from_id(const struct mpdclient_playlist *playlist,
128                            unsigned id);
130 gint
131 playlist_get_index_from_file(const struct mpdclient_playlist *playlist,
132                              const gchar *filename);
134 static inline gint
135 playlist_get_index_from_same_song(const struct mpdclient_playlist *playlist,
136                                   const struct mpd_song *song)
138         return playlist_get_index_from_file(playlist, mpd_song_get_uri(song));
141 gint
142 playlist_get_id_from_uri(const struct mpdclient_playlist *playlist,
143                          const gchar *uri);
145 static inline gint
146 playlist_get_id_from_same_song(const struct mpdclient_playlist *playlist,
147                                const struct mpd_song *song)
149         return playlist_get_id_from_uri(playlist, mpd_song_get_uri(song));
152 #endif