93c034f5a1361926090224d3025a3dfaa0ef7d8b
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 /* true if the list is updated */
37 gboolean updated;
39 /* the list */
40 GPtrArray *list;
41 } mpdclient_playlist_t;
43 void
44 playlist_init(struct mpdclient_playlist *playlist);
46 /** remove and free all songs in the playlist */
47 void
48 playlist_clear(struct mpdclient_playlist *playlist);
50 /* free a playlist */
51 gint mpdclient_playlist_free(mpdclient_playlist_t *playlist);
53 static inline guint
54 playlist_length(const struct mpdclient_playlist *playlist)
55 {
56 assert(playlist != NULL);
57 assert(playlist->list != NULL);
59 return playlist->list->len;
60 }
62 static inline gboolean
63 playlist_is_empty(const struct mpdclient_playlist *playlist)
64 {
65 return playlist_length(playlist) == 0;
66 }
68 static inline struct mpd_song *
69 playlist_get(const struct mpdclient_playlist *playlist, guint idx)
70 {
71 assert(idx < playlist_length(playlist));
73 return g_ptr_array_index(playlist->list, idx);
74 }
76 static inline void
77 playlist_append(struct mpdclient_playlist *playlist, const mpd_Song *song)
78 {
79 g_ptr_array_add(playlist->list, mpd_songDup(song));
80 }
82 static inline void
83 playlist_set(const struct mpdclient_playlist *playlist, guint idx,
84 const mpd_Song *song)
85 {
86 assert(idx < playlist_length(playlist));
88 g_ptr_array_index(playlist->list, idx) = mpd_songDup(song);
89 }
91 static inline void
92 playlist_replace(struct mpdclient_playlist *playlist, guint idx,
93 const mpd_Song *song)
94 {
95 mpd_freeSong(playlist_get(playlist, idx));
96 playlist_set(playlist, idx, song);
97 }
99 static inline struct mpd_song *
100 playlist_remove_reuse(struct mpdclient_playlist *playlist, guint idx)
101 {
102 return g_ptr_array_remove_index(playlist->list, idx);
103 }
105 static inline void
106 playlist_remove(struct mpdclient_playlist *playlist, guint idx)
107 {
108 mpd_freeSong(playlist_remove_reuse(playlist, idx));
109 }
111 static inline void
112 playlist_swap(struct mpdclient_playlist *playlist, guint idx1, guint idx2)
113 {
114 mpd_Song *song1 = playlist_get(playlist, idx1);
115 mpd_Song *song2 = playlist_get(playlist, idx2);
116 gint n;
118 /* update the songs position field */
119 n = song1->pos;
120 song1->pos = song2->pos;
121 song2->pos = n;
123 /* update the array */
124 g_ptr_array_index(playlist->list, idx1) = song2;
125 g_ptr_array_index(playlist->list, idx2) = song1;
126 }
128 struct mpd_song *playlist_lookup_song(struct mpdclient *c, gint id);
130 struct mpd_song *playlist_get_song(struct mpdclient *c, gint index);
132 gint playlist_get_index(struct mpdclient *c, struct mpd_song *song);
134 gint playlist_get_index_from_id(struct mpdclient *c, gint id);
136 gint playlist_get_index_from_file(struct mpdclient *c, gchar *filename);
138 #endif