From c7bc2062d2805223fa97c25858e77edd7a5be1f5 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sun, 1 May 2016 19:55:47 -0400 Subject: [PATCH] core/playlist: Add playlist_{get,set}_random() I'll need this to toggle the random flag for the Collection playlist to avoid cycling through the GUI in order to save. Signed-off-by: Anna Schumaker --- core/playlist.c | 10 ++++++++++ core/playlists/system.c | 11 +++++++++++ include/core/playlist.h | 7 +++++++ include/core/playlists/type.h | 3 +++ tests/core/playlists/system.c | 15 ++++++++++++--- 5 files changed, 43 insertions(+), 3 deletions(-) diff --git a/core/playlist.c b/core/playlist.c index a20152c8..d8c3422e 100644 --- a/core/playlist.c +++ b/core/playlist.c @@ -48,6 +48,16 @@ unsigned int playlist_size(const gchar *name) return queue ? queue_size(queue) : 0; } +void playlist_set_random(const gchar *name, bool enabled) +{ + pl_system.pl_set_flag(name, Q_RANDOM, enabled); +} + +bool playlist_get_random(const gchar *name) +{ + return queue_has_flag(playlist_get_queue(name), Q_RANDOM); +} + struct queue *playlist_get_queue(const gchar *name) { return pl_system.pl_get_queue(name); diff --git a/core/playlists/system.c b/core/playlists/system.c index d1140216..08043524 100644 --- a/core/playlists/system.c +++ b/core/playlists/system.c @@ -187,12 +187,23 @@ static void pl_system_update(const gchar *name) GUINT_TO_POINTER(plist)); } +static void pl_system_set_flag(const gchar *name, enum queue_flags flag, + bool enabled) +{ + enum sys_playlist_t plist = __sys_pl_convert(name); + if (enabled) + queue_set_flag(__sys_pl_queue(plist), flag); + else + queue_unset_flag(__sys_pl_queue(plist), flag); +} + struct playlist_type pl_system = { .pl_get_queue = pl_system_get_queue, .pl_add_track = pl_system_add_track, .pl_remove_track = pl_system_remove_track, .pl_update = pl_system_update, + .pl_set_flag = pl_system_set_flag, }; diff --git a/include/core/playlist.h b/include/core/playlist.h index b7cf0fa5..a8ea0e9a 100644 --- a/include/core/playlist.h +++ b/include/core/playlist.h @@ -35,6 +35,13 @@ bool playlist_has(const gchar *, struct track *); unsigned int playlist_size(const gchar *); +/* Called to set the playlist's random flag. */ +void playlist_set_random(const gchar *, bool); + +/* Called to check the playlist's random flag. */ +bool playlist_get_random(const gchar *); + + /* Called to access the playlist queue. */ struct queue *playlist_get_queue(const gchar *); diff --git a/include/core/playlists/type.h b/include/core/playlists/type.h index 276bb5e6..ea1ce5e3 100644 --- a/include/core/playlists/type.h +++ b/include/core/playlists/type.h @@ -30,6 +30,9 @@ struct playlist_type { /* Called to update a playlist. */ void (*pl_update)(const gchar *); + + /* Called to set a playlist flag. */ + void (*pl_set_flag)(const gchar *, enum queue_flags, bool); }; diff --git a/tests/core/playlists/system.c b/tests/core/playlists/system.c index b92e96d1..461c4f37 100644 --- a/tests/core/playlists/system.c +++ b/tests/core/playlists/system.c @@ -12,6 +12,13 @@ test_equal(queue_has(queue, track_get(0)), (bool)ex_track0); \ test_equal(queue_has(queue, track_get(1)), (bool)ex_track1) +#define __test_playlist_random(name, queue) \ + test_equal(queue_has_flag(queue, Q_RANDOM), (bool)false); \ + pl_system.pl_set_flag(name, Q_RANDOM, true); \ + test_equal(queue_has_flag(queue, Q_RANDOM), (bool)true); \ + pl_system.pl_set_flag(name, Q_RANDOM, false); \ + test_equal(queue_has_flag(queue, Q_RANDOM), (bool)false); \ + #define __test_playlist_add(name, queue) \ __test_playlist_state(queue, 0, false, false); \ test_equal(pl_system.pl_add_track(name, track_get(0)), (bool)true); \ @@ -83,6 +90,7 @@ static void test_favorites() test_not_equal((void *)queue, NULL); test_equal(queue_has_flag(queue, Q_ADD_FRONT), (bool)false); + __test_playlist_random("Favorites", queue); __test_playlist_add("Favorites", queue); __test_playlist_reinit(queue, 2, true, true); __test_playlist_update("Favorites", queue, 2, true, true); @@ -97,6 +105,7 @@ static void test_hidden() test_equal((void *)pl_system.pl_get_queue("Banned"), (void *)queue); test_equal(queue_has_flag(queue, Q_ADD_FRONT), (bool)false); + __test_playlist_random("Hidden", queue); __test_playlist_add("Hidden", queue); __test_playlist_reinit(queue, 2, true, true); __test_playlist_update("Hidden", queue, 2, true, true); @@ -113,6 +122,7 @@ static void test_unplayed() test_not_equal((void *)queue, NULL); test_equal(queue_has_flag(queue, Q_ADD_FRONT), (bool)true); + __test_playlist_random("Unplayed", queue); __test_playlist_reinit(queue, 2, true, true); __test_playlist_remove("Unplayed", queue); @@ -147,9 +157,7 @@ static void test_most_played() pl_system_deinit(); pl_system_init(NULL); - test_not_equal((void *)most, NULL); - test_equal(queue_has_flag(most, Q_ADD_FRONT), (bool)true); - + __test_playlist_random("Most Played", most); __test_playlist_reinit(most, 1, false, true); test_equal(pl_system.pl_remove_track("Most Played", track_get(0)), (bool)false); @@ -186,6 +194,7 @@ static void test_least_played() test_not_equal((void *)least, NULL); test_equal(queue_has_flag(least, Q_ADD_FRONT), (bool)true); + __test_playlist_random("Least Played", least); __test_playlist_reinit(least, 1, false, true); test_equal(pl_system.pl_remove_track("Least Played", track_get(0)), (bool)false);