core/playlist: playlist_sort() takes a playlist pointer

Additionally, it also uses a playlist-level function pointer to decide
what to do.  In most cases this calls the generic sort function, but the
history playlist should never be sorted.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-09-12 09:49:35 -04:00
parent 61e3137131
commit bef0c70e5a
13 changed files with 70 additions and 67 deletions

View File

@ -143,10 +143,14 @@ bool playlist_get_random(struct playlist *playlist)
return playlist ? queue_has_flag(&playlist->pl_queue, Q_RANDOM) : false;
}
void playlist_sort(enum playlist_type_t type, const gchar *name,
enum compare_t sort, bool reset)
bool playlist_sort(struct playlist *playlist, enum compare_t sort, bool reset)
{
playlist_types[type]->pl_sort(name, sort, reset);
if (!playlist || !playlist->pl_ops->pl_sort)
return false;
playlist->pl_ops->pl_sort(playlist, sort, reset);
playlist_types[playlist->pl_type]->pl_save();
return g_slist_length(playlist->pl_queue.q_sort) > 0;
}
struct track *playlist_next(void)

View File

@ -10,6 +10,7 @@ static struct file artist_file = FILE_INIT("playlist.artist", 0);
static struct playlist_ops pl_artist_ops = {
.pl_set_flag = playlist_generic_set_flag,
.pl_sort = playlist_generic_sort,
};
@ -126,13 +127,6 @@ static void pl_artist_update(const gchar *name)
{
}
static void pl_artist_sort(const gchar *name, enum compare_t sort, bool reset)
{
struct playlist *playlist = __artist_pl_lookup(name);
playlist_generic_sort(playlist, sort, reset);
pl_artist_save();
}
static struct track *pl_artist_next(const gchar *name)
{
struct playlist *playlist = __artist_pl_lookup(name);
@ -149,7 +143,6 @@ struct playlist_type pl_artist = {
.pl_get_name = pl_artist_get_name,
.pl_can_select = pl_artist_can_select,
.pl_update = pl_artist_update,
.pl_sort = pl_artist_sort,
.pl_next = pl_artist_next,
};

View File

@ -13,11 +13,6 @@ bool playlist_noop_can_select(struct playlist *playlist)
return false;
}
void playlist_noop_sort(struct playlist *playlist,
enum compare_t sort, bool reset)
{
}
/*
* Generic playlist operations.

View File

@ -195,6 +195,7 @@ static bool pl_library_delete(struct playlist *playlist)
static struct playlist_ops pl_library_ops = {
.pl_delete = pl_library_delete,
.pl_set_flag = playlist_generic_set_flag,
.pl_sort = playlist_generic_sort,
};
@ -260,13 +261,6 @@ static void pl_library_update(const gchar *name)
idle_schedule(IDLE_SYNC, __lib_pl_update, playlist);
}
static void pl_library_sort(const gchar *name, enum compare_t sort, bool reset)
{
struct playlist *playlist = __lib_pl_lookup(name);
playlist_generic_sort(playlist, sort, reset);
pl_library_save();
}
static struct track *pl_library_next(const gchar *name)
{
struct playlist *playlist = __lib_pl_lookup(name);
@ -284,7 +278,6 @@ struct playlist_type pl_library = {
.pl_can_select = pl_library_can_select,
.pl_new = pl_library_new,
.pl_update = pl_library_update,
.pl_sort = pl_library_sort,
.pl_next = pl_library_next,
};

View File

@ -82,6 +82,7 @@ static struct playlist_ops favorites_ops = {
.pl_delete = sys_pl_delete_clear,
.pl_remove = playlist_generic_remove_track,
.pl_set_flag = playlist_generic_set_flag,
.pl_sort = playlist_generic_sort,
};
static struct sys_playlist sys_favorites = {
@ -90,7 +91,6 @@ static struct sys_playlist sys_favorites = {
.spl_save = sys_pl_save_full,
.spl_load = sys_pl_load_full,
.spl_can_select = playlist_generic_can_select,
.spl_sort = playlist_generic_sort,
.spl_next = playlist_generic_next,
};
@ -143,6 +143,7 @@ static struct playlist_ops hidden_ops = {
.pl_delete = sys_pl_hidden_clear,
.pl_remove = sys_pl_hidden_remove,
.pl_set_flag = playlist_generic_set_flag,
.pl_sort = playlist_generic_sort,
};
static struct sys_playlist sys_hidden = {
@ -151,7 +152,6 @@ static struct sys_playlist sys_hidden = {
.spl_save = sys_pl_save_full,
.spl_load = sys_pl_load_full,
.spl_can_select = playlist_generic_can_select,
.spl_sort = playlist_generic_sort,
.spl_next = playlist_generic_next,
};
@ -193,6 +193,7 @@ static struct playlist_ops queued_ops = {
.pl_delete = sys_pl_delete_clear,
.pl_remove = playlist_generic_remove_track,
.pl_set_flag = playlist_generic_set_flag,
.pl_sort = playlist_generic_sort,
};
static struct sys_playlist sys_queued = {
@ -201,7 +202,6 @@ static struct sys_playlist sys_queued = {
.spl_save = sys_pl_save_full,
.spl_load = sys_pl_load_full,
.spl_can_select = playlist_generic_can_select,
.spl_sort = playlist_generic_sort,
.spl_next = playlist_generic_next,
};
@ -236,6 +236,7 @@ static bool sys_pl_collection_update(struct playlist *playlist,
static struct playlist_ops collection_ops = {
.pl_remove = sys_pl_hidden_add,
.pl_set_flag = playlist_generic_set_flag,
.pl_sort = playlist_generic_sort,
};
static struct sys_playlist sys_collection = {
@ -246,7 +247,6 @@ static struct sys_playlist sys_collection = {
.spl_load = sys_pl_load_partial,
.spl_can_select = sys_pl_collection_can_select,
.spl_update = sys_pl_collection_update,
.spl_sort = playlist_generic_sort,
.spl_next = playlist_generic_next,
};
@ -277,7 +277,6 @@ static struct sys_playlist sys_history = {
.spl_save = sys_pl_save_partial,
.spl_load = sys_pl_load_partial,
.spl_can_select = playlist_noop_can_select,
.spl_sort = playlist_noop_sort,
.spl_next = playlist_generic_next,
};
@ -295,6 +294,7 @@ static bool sys_pl_unplayed_update(struct playlist *playlist,
static struct playlist_ops unplayed_ops = {
.pl_set_flag = playlist_generic_set_flag,
.pl_sort = playlist_generic_sort,
};
static struct sys_playlist sys_unplayed = {
@ -304,7 +304,6 @@ static struct sys_playlist sys_unplayed = {
.spl_load = sys_pl_load_partial,
.spl_can_select = playlist_generic_can_select,
.spl_update = sys_pl_unplayed_update,
.spl_sort = playlist_generic_sort,
.spl_next = playlist_generic_next,
};
@ -323,6 +322,7 @@ static bool sys_pl_most_played_update(struct playlist *playlist,
static struct playlist_ops most_played_ops = {
.pl_set_flag = playlist_generic_set_flag,
.pl_sort = playlist_generic_sort,
};
static struct sys_playlist sys_most_played = {
@ -333,7 +333,6 @@ static struct sys_playlist sys_most_played = {
.spl_load = sys_pl_load_partial,
.spl_can_select = playlist_generic_can_select,
.spl_update = sys_pl_most_played_update,
.spl_sort = playlist_generic_sort,
.spl_next = playlist_generic_next,
};
@ -352,6 +351,7 @@ static bool sys_pl_least_played_update(struct playlist *playlist,
static struct playlist_ops least_played_ops = {
.pl_set_flag = playlist_generic_set_flag,
.pl_sort = playlist_generic_sort,
};
static struct sys_playlist sys_least_played = {
@ -362,7 +362,6 @@ static struct sys_playlist sys_least_played = {
.spl_load = sys_pl_load_partial,
.spl_can_select = playlist_generic_can_select,
.spl_update = sys_pl_least_played_update,
.spl_sort = playlist_generic_sort,
.spl_next = playlist_generic_next,
};
@ -516,15 +515,6 @@ static void pl_system_update(const gchar *name)
playlist_generic_update(&sys_pl->spl_playlist, sys_pl->spl_update);
}
static void pl_system_sort(const gchar *name, enum compare_t sort, bool reset)
{
struct sys_playlist *sys_pl = __sys_pl_lookup(name);
if (sys_pl) {
sys_pl->spl_sort(&sys_pl->spl_playlist, sort, reset);
__sys_pl_save();
}
}
static struct track *pl_system_next(const gchar *name)
{
struct sys_playlist *sys_pl = __sys_pl_lookup(name);
@ -544,7 +534,6 @@ struct playlist_type pl_system = {
.pl_get_name = pl_system_get_name,
.pl_can_select = pl_system_can_select,
.pl_update = pl_system_update,
.pl_sort = pl_system_sort,
.pl_next = pl_system_next,
};

View File

@ -86,6 +86,7 @@ static struct playlist_ops user_ops = {
.pl_delete = pl_user_delete,
.pl_remove = playlist_generic_remove_track,
.pl_set_flag = playlist_generic_set_flag,
.pl_sort = playlist_generic_sort,
};
@ -136,13 +137,6 @@ static void pl_user_update(const gchar *name)
{
}
static void pl_user_sort(const gchar *name, enum compare_t sort, bool reset)
{
struct playlist *playlist = __user_pl_lookup(name);
playlist_generic_sort(playlist, sort, reset);
pl_user_save();
}
static struct track *pl_user_next(const gchar *name)
{
struct playlist *playlist = __user_pl_lookup(name);
@ -160,7 +154,6 @@ struct playlist_type pl_user = {
.pl_can_select = pl_user_can_select,
.pl_new = pl_user_new,
.pl_update = pl_user_update,
.pl_sort = pl_user_sort,
.pl_next = pl_user_next,
};

View File

@ -132,8 +132,7 @@ static void __gui_treeview_column_clicked(GtkTreeViewColumn *col,
if (!playlist)
return;
playlist_sort(playlist->pl_type, playlist->pl_name, compare, reset);
if (!playlist->pl_queue.q_sort)
if (!playlist_sort(playlist, compare, reset))
return;
__gui_treeview_set_sort_indicators();

View File

@ -59,7 +59,7 @@ void playlist_set_random(struct playlist *, bool);
bool playlist_get_random(struct playlist *);
/* Called to change the sort order of the playlist. */
void playlist_sort(enum playlist_type_t, const gchar *, enum compare_t, bool);
bool playlist_sort(struct playlist *, enum compare_t, bool);
/* Called to get the next track from the default playlist. */

View File

@ -26,7 +26,6 @@ struct sys_playlist {
void (*spl_load)(struct playlist *, struct file *);
bool (*spl_can_select)(struct playlist *);
bool (*spl_update)(struct playlist *, struct track *);
void (*spl_sort)(struct playlist *, enum compare_t, bool);
struct track *(*spl_next)(struct playlist *);
};

View File

@ -32,6 +32,9 @@ struct playlist_ops {
/* Called to set a playlist flag. */
void (*pl_set_flag)(struct playlist *, enum queue_flags, bool);
/* Called to sort the playlist. */
void (*pl_sort)(struct playlist *, enum compare_t, bool);
};
@ -74,9 +77,6 @@ struct playlist_type {
/* Called to update a playlist. */
void (*pl_update)(const gchar *);
/* Called to sort a playlist. */
void (*pl_sort)(const gchar *, enum compare_t, bool);
/* Called to pick the next track from a playlist. */
struct track *(*pl_next)(const gchar *);
};
@ -85,9 +85,6 @@ struct playlist_type {
/* Noop playlist can-select operation. */
bool playlist_noop_can_select(struct playlist *);
/* Noop playlist sorting operation. */
void playlist_noop_sort(struct playlist *, enum compare_t, bool);
/* Generic playlist init function. */
void playlist_generic_init(struct playlist *, unsigned int, struct queue_ops *);

View File

@ -23,6 +23,9 @@ static void test_null()
g_assert_false(playlist_get_random(NULL));
playlist_set_random(NULL, true);
g_assert_false(playlist_get_random(NULL));
g_assert_false(playlist_sort(NULL, COMPARE_TRACK, true));
g_assert_false(playlist_sort(NULL, COMPARE_TRACK, false));
}
int main(int argc, char **argv)

View File

@ -76,12 +76,14 @@ void test_library()
g_assert_false(playlist_get_random(playlist));
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 3);
playlist_sort(PL_LIBRARY, "tests/Music", COMPARE_ARTIST, true);
g_assert_true(playlist_sort(playlist, COMPARE_ARTIST, true));
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 1);
playlist_sort(PL_LIBRARY, "tests/Music", COMPARE_YEAR, false);
g_assert_true(playlist_sort(playlist, COMPARE_YEAR, false));
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 2);
playlist_sort(PL_LIBRARY, "tests/Music", COMPARE_TRACK, false);
g_assert_true(playlist_sort(playlist, COMPARE_TRACK, false));
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 3);
g_assert_true(playlist_sort(playlist, COMPARE_TITLE, true));
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 1);
g_rename("tests/Music/Hyrule Symphony/", "tests/Hyrule Symphony/");
playlist_update(PL_LIBRARY, "tests/Music");

View File

@ -231,10 +231,6 @@ static void test_history()
__test_playlist_id("History", SYS_PL_HISTORY);
__test_playlist_noselect("History");
g_assert_cmpuint(g_slist_length(queue->q_sort), ==, 0);
playlist_sort(PL_SYSTEM, "History", COMPARE_TRACK, true);
g_assert_cmpuint(g_slist_length(queue->q_sort), ==, 0);
__test_playlist_state("History", 0, false, false);
g_assert_true(playlist_add(__test_pl_history(), track_get(0)));
g_assert_true(playlist_add(__test_pl_history(), track_get(0)));
@ -403,6 +399,45 @@ static void test_random()
g_assert_false(playlist_get_random(__test_pl_least_played()));
}
static void test_sort()
{
g_assert_true( playlist_sort(__test_pl_favorites(), COMPARE_TRACK, true));
g_assert_true( playlist_sort(__test_pl_hidden(), COMPARE_TRACK, true));
g_assert_true( playlist_sort(__test_pl_queued(), COMPARE_TRACK, true));
g_assert_true( playlist_sort(__test_pl_collection(), COMPARE_TRACK, true));
g_assert_false(playlist_sort(__test_pl_history(), COMPARE_TRACK, true));
g_assert_true( playlist_sort(__test_pl_unplayed(), COMPARE_TRACK, true));
g_assert_true( playlist_sort(__test_pl_most_played(), COMPARE_TRACK, true));
g_assert_true( playlist_sort(__test_pl_least_played(), COMPARE_TRACK, true));
g_assert_cmpuint(g_slist_length(__test_pl_favorites()->pl_queue.q_sort), ==, 1);
g_assert_cmpuint(g_slist_length(__test_pl_hidden()->pl_queue.q_sort), ==, 1);
g_assert_cmpuint(g_slist_length(__test_pl_queued()->pl_queue.q_sort), ==, 1);
g_assert_cmpuint(g_slist_length(__test_pl_collection()->pl_queue.q_sort), ==, 1);
g_assert_cmpuint(g_slist_length(__test_pl_history()->pl_queue.q_sort), ==, 0);
g_assert_cmpuint(g_slist_length(__test_pl_unplayed()->pl_queue.q_sort), ==, 1);
g_assert_cmpuint(g_slist_length(__test_pl_most_played()->pl_queue.q_sort), ==, 1);
g_assert_cmpuint(g_slist_length(__test_pl_least_played()->pl_queue.q_sort), ==, 1);
g_assert_true( playlist_sort(__test_pl_favorites(), COMPARE_YEAR, false));
g_assert_true( playlist_sort(__test_pl_hidden(), COMPARE_YEAR, false));
g_assert_true( playlist_sort(__test_pl_queued(), COMPARE_YEAR, false));
g_assert_true( playlist_sort(__test_pl_collection(), COMPARE_YEAR, false));
g_assert_false(playlist_sort(__test_pl_history(), COMPARE_YEAR, false));
g_assert_true( playlist_sort(__test_pl_unplayed(), COMPARE_YEAR, false));
g_assert_true( playlist_sort(__test_pl_most_played(), COMPARE_YEAR, false));
g_assert_true( playlist_sort(__test_pl_least_played(), COMPARE_YEAR, false));
g_assert_cmpuint(g_slist_length(__test_pl_favorites()->pl_queue.q_sort), ==, 2);
g_assert_cmpuint(g_slist_length(__test_pl_hidden()->pl_queue.q_sort), ==, 2);
g_assert_cmpuint(g_slist_length(__test_pl_queued()->pl_queue.q_sort), ==, 2);
g_assert_cmpuint(g_slist_length(__test_pl_collection()->pl_queue.q_sort), ==, 2);
g_assert_cmpuint(g_slist_length(__test_pl_history()->pl_queue.q_sort), ==, 0);
g_assert_cmpuint(g_slist_length(__test_pl_unplayed()->pl_queue.q_sort), ==, 2);
g_assert_cmpuint(g_slist_length(__test_pl_most_played()->pl_queue.q_sort), ==, 2);
g_assert_cmpuint(g_slist_length(__test_pl_least_played()->pl_queue.q_sort), ==, 2);
}
static void test_add()
{
struct library *library = library_find("tests/Music");
@ -567,6 +602,7 @@ int main(int argc, char **argv)
g_test_add_func("/Core/Playlists/System/Most Played Tracks", test_most_played);
g_test_add_func("/Core/Playlists/System/Least Played Tracks", test_least_played);
g_test_add_func("/Core/Playlists/System/Random", test_random);
g_test_add_func("/Core/Playlists/System/Sort", test_sort);
g_test_add_func("/Core/Playlists/System/Add Tracks", test_add);
g_test_add_func("/Core/Playlists/System/Remove Tracks", test_remove);
g_test_add_func("/Core/Playlists/System/Delete", test_delete);