1 /*
2 * $Id$
3 *
4 * (c) 2004 by Kalle Wallin <kaw@linux.se>
5 * (c) 2008 Max Kellermann <max@duempel.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
22 #ifndef MPDCLIENT_PLAYLIST_H
23 #define MPDCLIENT_PLAYLIST_H
25 #include "libmpdclient.h"
27 #include <assert.h>
28 #include <glib.h>
30 struct mpdclient;
32 typedef struct mpdclient_playlist {
33 /* playlist id */
34 long long id;
36 /* the list */
37 GPtrArray *list;
38 } mpdclient_playlist_t;
40 void
41 playlist_init(struct mpdclient_playlist *playlist);
43 /** remove and free all songs in the playlist */
44 void
45 playlist_clear(struct mpdclient_playlist *playlist);
47 /* free a playlist */
48 gint mpdclient_playlist_free(mpdclient_playlist_t *playlist);
50 static inline guint
51 playlist_length(const struct mpdclient_playlist *playlist)
52 {
53 assert(playlist != NULL);
54 assert(playlist->list != NULL);
56 return playlist->list->len;
57 }
59 static inline gboolean
60 playlist_is_empty(const struct mpdclient_playlist *playlist)
61 {
62 return playlist_length(playlist) == 0;
63 }
65 static inline struct mpd_song *
66 playlist_get(const struct mpdclient_playlist *playlist, guint idx)
67 {
68 assert(idx < playlist_length(playlist));
70 return g_ptr_array_index(playlist->list, idx);
71 }
73 static inline void
74 playlist_append(struct mpdclient_playlist *playlist, const mpd_Song *song)
75 {
76 g_ptr_array_add(playlist->list, mpd_songDup(song));
77 }
79 static inline void
80 playlist_set(const struct mpdclient_playlist *playlist, guint idx,
81 const mpd_Song *song)
82 {
83 assert(idx < playlist_length(playlist));
85 g_ptr_array_index(playlist->list, idx) = mpd_songDup(song);
86 }
88 static inline void
89 playlist_replace(struct mpdclient_playlist *playlist, guint idx,
90 const mpd_Song *song)
91 {
92 mpd_freeSong(playlist_get(playlist, idx));
93 playlist_set(playlist, idx, song);
94 }
96 static inline struct mpd_song *
97 playlist_remove_reuse(struct mpdclient_playlist *playlist, guint idx)
98 {
99 return g_ptr_array_remove_index(playlist->list, idx);
100 }
102 static inline void
103 playlist_remove(struct mpdclient_playlist *playlist, guint idx)
104 {
105 mpd_freeSong(playlist_remove_reuse(playlist, idx));
106 }
108 static inline void
109 playlist_swap(struct mpdclient_playlist *playlist, guint idx1, guint idx2)
110 {
111 mpd_Song *song1 = playlist_get(playlist, idx1);
112 mpd_Song *song2 = playlist_get(playlist, idx2);
113 gint n;
115 /* update the songs position field */
116 n = song1->pos;
117 song1->pos = song2->pos;
118 song2->pos = n;
120 /* update the array */
121 g_ptr_array_index(playlist->list, idx1) = song2;
122 g_ptr_array_index(playlist->list, idx2) = song1;
123 }
125 struct mpd_song *playlist_lookup_song(struct mpdclient *c, gint id);
127 struct mpd_song *playlist_get_song(struct mpdclient *c, gint index);
129 gint playlist_get_index(struct mpdclient *c, struct mpd_song *song);
131 gint playlist_get_index_from_id(struct mpdclient *c, gint id);
133 gint playlist_get_index_from_file(struct mpdclient *c, gchar *filename);
135 #endif