summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: a6858ac)
raw | patch | inline | side by side (parent: a6858ac)
author | Max Kellermann <max@duempel.org> | |
Tue, 16 Sep 2008 17:11:39 +0000 (19:11 +0200) | ||
committer | Max Kellermann <max@duempel.org> | |
Tue, 16 Sep 2008 17:11:39 +0000 (19:11 +0200) |
Added functions like playlist_length(), playlist_get(),
playlist_replace(), playlist_remove(). Don't access the
mpdclient_playlist struct directly.
playlist_replace(), playlist_remove(). Don't access the
mpdclient_playlist struct directly.
src/mpdclient.c | patch | blob | history | |
src/playlist.c | patch | blob | history | |
src/playlist.h | patch | blob | history |
diff --git a/src/mpdclient.c b/src/mpdclient.c
index 8449ca42b464be4bdf675cb7b9174dd34a144405..7c2cd32f76e3062e1e374124a6cdc991a65f600e 100644 (file)
--- a/src/mpdclient.c
+++ b/src/mpdclient.c
/* check if the playlist needs an update */
if (c->playlist.id != c->status->playlist) {
- if (c->playlist.list)
+ if (playlist_is_empty(&c->playlist))
retval = mpdclient_playlist_update_changes(c);
else
retval = mpdclient_playlist_update(c);
#ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD
/* add the song to playlist */
- c->playlist.list = g_list_append(c->playlist.list, mpd_songDup(song));
- c->playlist.length++;
+ playlist_append(&c->playlist, song);
/* increment the playlist id, so we dont retrives a new playlist */
c->playlist.id++;
c->playlist.id++;
/* remove the song from the playlist */
- g_array_remove_index(c->playlist.list, idx);
+ playlist_remove(&c->playlist, idx);
/* call playlist updated callback */
mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
c->need_update = TRUE;
}
- /* free song */
- mpd_freeSong(song);
-
#else
c->need_update = TRUE;
#endif
return n;
#ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
- /* update the songs position field */
- n = song1->pos;
- song1->pos = song2->pos;
- song2->pos = n;
- /* update the array */
- g_array_index(c->playlist.list, struct mpd_song *, old_index) = song2;
- g_array_index(c->playlist.list, struct mpd_song *, new_index) = song1;
+ /* update the playlist */
+ playlist_swap(&c->playlist, old_index, new_index);
/* increment the playlist id, so we dont retrives a new playlist */
c->playlist.id++;
mpd_sendPlaylistInfoCommand(c->connection,-1);
while ((entity = mpd_getNextInfoEntity(c->connection))) {
- if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
- struct mpd_song *song = mpd_songDup(entity->info.song);
- g_array_append_val(c->playlist.list, song);
- }
+ if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
+ playlist_append(&c->playlist, entity->info.song);
+
mpd_freeInfoEntity(entity);
}
mpd_sendPlChangesCommand(c->connection, c->playlist.id);
while ((entity = mpd_getNextInfoEntity(c->connection)) != NULL) {
- struct mpd_song *song = mpd_songDup(entity->info.song);
+ struct mpd_song *song = entity->info.song;
if (song->pos >= 0 && (guint)song->pos < c->playlist.list->len) {
/* update song */
D("updating pos:%d, id=%d - %s\n",
song->pos, song->id, song->file);
- mpd_freeSong(g_array_index(c->playlist.list,
- struct mpd_song *, song->pos));
- g_array_index(c->playlist.list,
- struct mpd_song *, song->pos) = song;
+ playlist_replace(&c->playlist, song->pos, song);
} else {
/* add a new song */
D("adding song at pos %d\n", song->pos);
- g_array_append_val(c->playlist.list, song);
+ playlist_append(&c->playlist, song);
}
mpd_freeInfoEntity(entity);
/* remove trailing songs */
while ((guint)c->status->playlistLength < c->playlist.list->len) {
guint pos = c->playlist.list->len - 1;
- struct mpd_song *song = g_array_index(c->playlist.list, struct mpd_song *, pos);
/* Remove the last playlist entry */
D("removing song at pos %d\n", pos);
- mpd_freeSong(song);
- g_array_remove_index(c->playlist.list, pos);
+ playlist_remove(&c->playlist, pos);
}
c->song = NULL;
diff --git a/src/playlist.c b/src/playlist.c
index b600343d720ab23fba3b1663b1ba539eedc554f4..d14f870b0902c0ef655a98b8e5216b29a50445f3 100644 (file)
--- a/src/playlist.c
+++ b/src/playlist.c
guint i;
for (i = 0; i < playlist->list->len; ++i) {
- struct mpd_song *song = g_array_index(playlist->list, struct mpd_song *, i);
+ struct mpd_song *song = playlist_get(playlist, i);
+
mpd_freeSong(song);
}
if (idx < 0 || (guint)idx >= c->playlist.list->len)
return NULL;
- return g_array_index(c->playlist.list, struct mpd_song *, idx);
+ return playlist_get(&c->playlist, idx);
}
struct mpd_song *
guint i;
for (i = 0; i < c->playlist.list->len; ++i) {
- struct mpd_song *song = g_array_index(c->playlist.list,
- struct mpd_song *, i);
+ struct mpd_song *song = playlist_get(&c->playlist, i);
if (song->id == id)
return song;
}
guint i;
for (i = 0; i < c->playlist.list->len; ++i) {
- if (g_array_index(c->playlist.list, struct mpd_song *, i)
- == song)
+ if (playlist_get(&c->playlist, i) == song)
return (gint)i;
}
guint i;
for (i = 0; i < c->playlist.list->len; ++i) {
- struct mpd_song *song = g_array_index(c->playlist.list,
- struct mpd_song *, i);
+ struct mpd_song *song = playlist_get(&c->playlist, i);
if (song->id == id)
return (gint)i;
}
guint i;
for (i = 0; i < c->playlist.list->len; ++i) {
- struct mpd_song *song = g_array_index(c->playlist.list,
- struct mpd_song *, i);
+ struct mpd_song *song = playlist_get(&c->playlist, i);
if(strcmp(song->file, filename) == 0)
return (gint)i;
}
diff --git a/src/playlist.h b/src/playlist.h
index 946ca631c44b6138ec78d911f90510a9fb55894c..377ed84a2ee91e94a743e53a37c7f2cd5bf9ee7c 100644 (file)
--- a/src/playlist.h
+++ b/src/playlist.h
#include "libmpdclient.h"
+#include <assert.h>
#include <glib.h>
struct mpdclient;
/* free a playlist */
gint mpdclient_playlist_free(mpdclient_playlist_t *playlist);
+static inline guint
+playlist_length(const struct mpdclient_playlist *playlist)
+{
+ assert(playlist != NULL);
+ assert(playlist->list != NULL);
+
+ return playlist->list->len;
+}
+
+static inline gboolean
+playlist_is_empty(const struct mpdclient_playlist *playlist)
+{
+ return playlist_length(playlist) == 0;
+}
+
+static inline struct mpd_song *
+playlist_get(const struct mpdclient_playlist *playlist, guint idx)
+{
+ assert(idx < playlist_length(playlist));
+
+ return g_array_index(playlist->list, struct mpd_song *, idx);
+}
+
+static inline void
+playlist_append(struct mpdclient_playlist *playlist, const mpd_Song *song)
+{
+ mpd_Song *song2 = mpd_songDup(song);
+ g_array_append_val(playlist->list, song2);
+}
+
+static inline void
+playlist_set(const struct mpdclient_playlist *playlist, guint idx,
+ const mpd_Song *song)
+{
+ assert(idx < playlist_length(playlist));
+
+ g_array_index(playlist->list, mpd_Song *, idx) = mpd_songDup(song);
+}
+
+static inline void
+playlist_replace(struct mpdclient_playlist *playlist, guint idx,
+ const mpd_Song *song)
+{
+ mpd_freeSong(playlist_get(playlist, idx));
+ playlist_set(playlist, idx, song);
+}
+
+static inline void
+playlist_remove(struct mpdclient_playlist *playlist, guint idx)
+{
+ mpd_Song *song = playlist_get(playlist, idx);
+ mpd_freeSong(song);
+ g_array_remove_index(playlist->list, idx);
+}
+
+static inline void
+playlist_swap(struct mpdclient_playlist *playlist, guint idx1, guint idx2)
+{
+ mpd_Song *song1 = playlist_get(playlist, idx1);
+ mpd_Song *song2 = playlist_get(playlist, idx2);
+ gint n;
+
+ /* update the songs position field */
+ n = song1->pos;
+ song1->pos = song2->pos;
+ song2->pos = n;
+
+ /* update the array */
+ g_array_index(playlist->list, struct mpd_song *, idx1) = song2;
+ g_array_index(playlist->list, struct mpd_song *, idx2) = song1;
+}
+
struct mpd_song *playlist_lookup_song(struct mpdclient *c, gint id);
struct mpd_song *playlist_get_song(struct mpdclient *c, gint index);