From a80a84a9556da85d5c6385e984ff1ed94f1ef1c5 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 24 May 2016 07:33:40 -0400 Subject: [PATCH] core/playlists/generic: Add a playlist_generic_sort() function I set the history playlist to use a noop function, since changing the history doesn't really make sense. Signed-off-by: Anna Schumaker --- core/playlists/artist.c | 13 +++++--- core/playlists/generic.c | 23 +++++++++++++ core/playlists/library.c | 2 +- core/playlists/system.c | 57 ++++++++++++++++++++++++++------- include/core/playlists/system.h | 2 ++ include/core/playlists/type.h | 8 +++++ tests/core/playlists/Sconscript | 3 +- tests/core/playlists/system.c | 4 +++ 8 files changed, 94 insertions(+), 18 deletions(-) create mode 100644 core/playlists/generic.c diff --git a/core/playlists/artist.c b/core/playlists/artist.c index ca5ec4a9..38bb49bd 100644 --- a/core/playlists/artist.c +++ b/core/playlists/artist.c @@ -42,11 +42,16 @@ static bool __artist_pl_load(void *data) return true; } +static struct playlist *__artist_pl_lookup(const gchar *name) +{ + struct artist *artist = artist_lookup(name); + return artist ? artist->ar_playlist : NULL; +} + static struct queue *pl_artist_get_queue(const gchar *name) { - struct artist *artist = artist_lookup(name); - struct playlist *playlist = artist ? artist->ar_playlist : NULL; + struct playlist *playlist = __artist_pl_lookup(name); return playlist ? &playlist->pl_queue : NULL; } @@ -73,8 +78,8 @@ static void pl_artist_set_flag(const gchar *name, enum queue_flags flag, static void pl_artist_sort(const gchar *name, enum compare_t sort, bool reset) { - struct queue *queue = pl_artist_get_queue(name); - queue_sort(queue, sort, reset); + struct playlist *playlist = __artist_pl_lookup(name); + playlist_generic_sort(playlist, sort, reset); } diff --git a/core/playlists/generic.c b/core/playlists/generic.c new file mode 100644 index 00000000..1ee6a424 --- /dev/null +++ b/core/playlists/generic.c @@ -0,0 +1,23 @@ +/* + * Copyright 2016 (c) Anna Schumaker. + */ +#include + + +/* + * Noop playlist operations. + */ +void playlist_noop_sort(struct playlist *playlist, + enum compare_t sort, bool reset) +{ +} + + +/* + * Generic playlist operations. + */ +void playlist_generic_sort(struct playlist *playlist, + enum compare_t sort, bool reset) +{ + queue_sort(&playlist->pl_queue, sort, reset); +} diff --git a/core/playlists/library.c b/core/playlists/library.c index f6dd0593..1c27a2ed 100644 --- a/core/playlists/library.c +++ b/core/playlists/library.c @@ -205,7 +205,7 @@ static void pl_library_set_flag(const gchar *name, enum queue_flags flag, static void pl_library_sort(const gchar *name, enum compare_t sort, bool reset) { struct playlist *playlist = __lib_pl_lookup(name); - queue_sort(&playlist->pl_queue, sort, reset); + playlist_generic_sort(playlist, sort, reset); } diff --git a/core/playlists/system.c b/core/playlists/system.c index cc771df7..7bc9a9ed 100644 --- a/core/playlists/system.c +++ b/core/playlists/system.c @@ -6,17 +6,52 @@ #include static bool __sys_pl_remove(enum sys_playlist_t, struct track *); +static void __sys_pl_save_collection(); static struct file sys_file = FILE_INIT("playlist.db", 0, 0); static struct file sys_collection = FILE_INIT("library.q", 0, 0); + + +/* + * Collection playlist operations. + */ +static void sys_pl_collection_sort(struct playlist *playlist, + enum compare_t sort, bool reset) +{ + playlist_generic_sort(playlist, sort, reset); + __sys_pl_save_collection(); +} + + static struct sys_playlist sys_playlists[SYS_PL_NUM_PLAYLISTS] = { - [SYS_PL_FAVORITES] = { DEFINE_PLAYLIST(PL_SYSTEM, "Favorites") }, - [SYS_PL_HIDDEN] = { DEFINE_PLAYLIST(PL_SYSTEM, "Hidden") }, - [SYS_PL_COLLECTION] = { DEFINE_PLAYLIST(PL_SYSTEM, "Collection") }, - [SYS_PL_HISTORY] = { DEFINE_PLAYLIST(PL_SYSTEM, "History") }, - [SYS_PL_UNPLAYED] = { DEFINE_PLAYLIST(PL_SYSTEM, "Unplayed") }, - [SYS_PL_MOST_PLAYED] = { DEFINE_PLAYLIST(PL_SYSTEM, "Most Played") }, - [SYS_PL_LEAST_PLAYED] = { DEFINE_PLAYLIST(PL_SYSTEM, "Least Played") }, + [SYS_PL_FAVORITES] = { + .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Favorites"), + .spl_sort = playlist_generic_sort, + }, + [SYS_PL_HIDDEN] = { + .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Hidden"), + .spl_sort = playlist_generic_sort, + }, + [SYS_PL_COLLECTION] = { + .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Collection"), + .spl_sort = sys_pl_collection_sort, + }, + [SYS_PL_HISTORY] = { + .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "History"), + .spl_sort = playlist_noop_sort, + }, + [SYS_PL_UNPLAYED] = { + .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Unplayed"), + .spl_sort = playlist_generic_sort, + }, + [SYS_PL_MOST_PLAYED] = { + .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Most Played"), + .spl_sort = playlist_generic_sort, + }, + [SYS_PL_LEAST_PLAYED] = { + .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Least Played"), + .spl_sort = playlist_generic_sort, + }, }; @@ -257,11 +292,9 @@ static void pl_system_set_flag(const gchar *name, enum queue_flags flag, static void pl_system_sort(const gchar *name, enum compare_t sort, bool reset) { - enum sys_playlist_t plist = __sys_pl_convert(name); - - queue_sort(__sys_pl_queue(plist), sort, reset); - if (plist == SYS_PL_COLLECTION) - __sys_pl_save_collection(); + struct sys_playlist *sys_pl = __sys_pl_lookup(name); + if (sys_pl) + sys_pl->spl_sort(&sys_pl->spl_playlist, sort, reset); } diff --git a/include/core/playlists/system.h b/include/core/playlists/system.h index 49a89ffd..ba9ce821 100644 --- a/include/core/playlists/system.h +++ b/include/core/playlists/system.h @@ -19,6 +19,8 @@ enum sys_playlist_t { struct sys_playlist { struct playlist spl_playlist; + + void (*spl_sort)(struct playlist *, enum compare_t, bool); }; diff --git a/include/core/playlists/type.h b/include/core/playlists/type.h index 5a9bbf5a..787a230d 100644 --- a/include/core/playlists/type.h +++ b/include/core/playlists/type.h @@ -54,4 +54,12 @@ struct playlist_type { }; +/* Noop playlist sorting operation. */ +void playlist_noop_sort(struct playlist *, enum compare_t, bool); + + +/* Generic playlist sorting operation. */ +void playlist_generic_sort(struct playlist *, enum compare_t, bool); + + #endif /* OCARINA_CORE_PLAYLISTS_TYPE_H */ diff --git a/tests/core/playlists/Sconscript b/tests/core/playlists/Sconscript index 6aba4498..844d7c19 100644 --- a/tests/core/playlists/Sconscript +++ b/tests/core/playlists/Sconscript @@ -1,5 +1,5 @@ #!/env/bin/python -Import("env", "CoreTest", "testing_group") +Import("env", "CoreTest", "testing_group", "core_objs") res = [] @@ -10,6 +10,7 @@ def PlaylistTest(name): Depends(run, res[-1]) return run +core_objs += [ env.Object("../../../core/playlists/generic.c") ] res += [ PlaylistTest("system") ] res += [ PlaylistTest("artist") ] res += [ PlaylistTest("library") ] diff --git a/tests/core/playlists/system.c b/tests/core/playlists/system.c index 89a1c8ca..e12460e8 100644 --- a/tests/core/playlists/system.c +++ b/tests/core/playlists/system.c @@ -141,6 +141,10 @@ static void test_history() test_equal(queue_has_flag(queue, Q_ADD_FRONT), (bool)true); test_equal(queue_has_flag(queue, Q_NO_SORT), (bool)true); + test_equal(g_slist_length(queue->q_sort), 0); + pl_system.pl_sort("History", COMPARE_TRACK, true); + test_equal(g_slist_length(queue->q_sort), 0); + test_equal(queue_has_flag(queue, Q_RANDOM), (bool)false); pl_system.pl_set_flag("History", Q_RANDOM, true); test_equal(queue_has_flag(queue, Q_RANDOM), (bool)false);