core/playlist: Add a playlist_clear_sort() function
This replaces the "reset" field that had been passed to sort. I think this makes things a little more straightforward, and gives us a function we can call when freeing playlists. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
d2335f5c6e
commit
793a8a5817
|
@ -209,12 +209,12 @@ bool playlist_get_random(struct playlist *playlist)
|
||||||
return playlist ? queue_has_flag(&playlist->pl_queue, Q_RANDOM) : false;
|
return playlist ? queue_has_flag(&playlist->pl_queue, Q_RANDOM) : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool playlist_sort(struct playlist *playlist, enum compare_t sort, bool reset)
|
bool playlist_sort(struct playlist *playlist, enum compare_t sort)
|
||||||
{
|
{
|
||||||
if (!playlist || !playlist->pl_ops->pl_sort)
|
if (!playlist || !playlist->pl_ops->pl_sort)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
playlist->pl_ops->pl_sort(playlist, sort, reset);
|
playlist->pl_ops->pl_sort(playlist, sort);
|
||||||
playlist_types[playlist->pl_type]->pl_save();
|
playlist_types[playlist->pl_type]->pl_save();
|
||||||
return g_slist_length(playlist->pl_queue.q_sort) > 0;
|
return g_slist_length(playlist->pl_queue.q_sort) > 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,9 +17,9 @@ void playlist_generic_init(struct playlist *playlist, unsigned int flags,
|
||||||
struct queue_ops *ops)
|
struct queue_ops *ops)
|
||||||
{
|
{
|
||||||
queue_init(&playlist->pl_queue, flags, ops, playlist);
|
queue_init(&playlist->pl_queue, flags, ops, playlist);
|
||||||
queue_sort(&playlist->pl_queue, COMPARE_ARTIST, true);
|
queue_sort(&playlist->pl_queue, COMPARE_ARTIST);
|
||||||
queue_sort(&playlist->pl_queue, COMPARE_YEAR, false);
|
queue_sort(&playlist->pl_queue, COMPARE_YEAR);
|
||||||
queue_sort(&playlist->pl_queue, COMPARE_TRACK, false);
|
queue_sort(&playlist->pl_queue, COMPARE_TRACK);
|
||||||
playlist->pl_private = NULL;
|
playlist->pl_private = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,6 +79,7 @@ void playlist_generic_load(struct playlist *playlist, struct file *file,
|
||||||
field = -field;
|
field = -field;
|
||||||
sort = g_slist_append(sort, GINT_TO_POINTER(field));
|
sort = g_slist_append(sort, GINT_TO_POINTER(field));
|
||||||
}
|
}
|
||||||
|
playlist_clear_sort(playlist);
|
||||||
playlist->pl_queue.q_sort = sort;
|
playlist->pl_queue.q_sort = sort;
|
||||||
queue_resort(&playlist->pl_queue);
|
queue_resort(&playlist->pl_queue);
|
||||||
|
|
||||||
|
@ -140,10 +141,9 @@ void playlist_generic_set_flag(struct playlist *playlist,
|
||||||
return queue_unset_flag(&playlist->pl_queue, flag);
|
return queue_unset_flag(&playlist->pl_queue, flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
void playlist_generic_sort(struct playlist *playlist,
|
void playlist_generic_sort(struct playlist *playlist, enum compare_t sort)
|
||||||
enum compare_t sort, bool reset)
|
|
||||||
{
|
{
|
||||||
queue_sort(&playlist->pl_queue, sort, reset);
|
queue_sort(&playlist->pl_queue, sort);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct track *playlist_generic_next(struct playlist *playlist)
|
struct track *playlist_generic_next(struct playlist *playlist)
|
||||||
|
|
|
@ -256,16 +256,11 @@ void queue_resort(struct queue *queue)
|
||||||
__queue_updated(queue, i);
|
__queue_updated(queue, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
void queue_sort(struct queue *queue, enum compare_t sort, bool reset)
|
void queue_sort(struct queue *queue, enum compare_t sort)
|
||||||
{
|
{
|
||||||
GSList *cur = NULL;
|
GSList *cur = NULL;
|
||||||
int field;
|
int field;
|
||||||
|
|
||||||
if (reset) {
|
|
||||||
g_slist_free(queue->q_sort);
|
|
||||||
queue->q_sort = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
cur = queue->q_sort;
|
cur = queue->q_sort;
|
||||||
while (cur) {
|
while (cur) {
|
||||||
field = GPOINTER_TO_INT(cur->data);
|
field = GPOINTER_TO_INT(cur->data);
|
||||||
|
|
|
@ -126,13 +126,14 @@ static void __gui_treeview_column_clicked(GtkTreeViewColumn *col,
|
||||||
{
|
{
|
||||||
struct playlist *playlist = gui_model_get_playlist();
|
struct playlist *playlist = gui_model_get_playlist();
|
||||||
enum compare_t compare = GPOINTER_TO_UINT(data);
|
enum compare_t compare = GPOINTER_TO_UINT(data);
|
||||||
bool reset = (sort_count == 0);
|
|
||||||
gchar *text;
|
gchar *text;
|
||||||
|
|
||||||
if (!playlist)
|
if (!playlist)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!playlist_sort(playlist, compare, reset))
|
if (sort_count == 0)
|
||||||
|
playlist_clear_sort(playlist);
|
||||||
|
if (!playlist_sort(playlist, compare))
|
||||||
return;
|
return;
|
||||||
__gui_treeview_set_sort_indicators();
|
__gui_treeview_set_sort_indicators();
|
||||||
|
|
||||||
|
|
|
@ -71,6 +71,6 @@ void playlist_set_random(struct playlist *, bool);
|
||||||
bool playlist_get_random(struct playlist *);
|
bool playlist_get_random(struct playlist *);
|
||||||
|
|
||||||
/* Called to change the sort order of the playlist. */
|
/* Called to change the sort order of the playlist. */
|
||||||
bool playlist_sort(struct playlist *, enum compare_t, bool);
|
bool playlist_sort(struct playlist *, enum compare_t);
|
||||||
|
|
||||||
#endif /* OCARINA_CORE_PLAYLIST_H */
|
#endif /* OCARINA_CORE_PLAYLIST_H */
|
||||||
|
|
|
@ -46,7 +46,7 @@ bool playlist_generic_remove_track(struct playlist *, struct track *);
|
||||||
void playlist_generic_set_flag(struct playlist *, enum queue_flags, bool);
|
void playlist_generic_set_flag(struct playlist *, enum queue_flags, bool);
|
||||||
|
|
||||||
/* Generic playlist sorting operation. */
|
/* Generic playlist sorting operation. */
|
||||||
void playlist_generic_sort(struct playlist *, enum compare_t, bool);
|
void playlist_generic_sort(struct playlist *, enum compare_t);
|
||||||
|
|
||||||
/* Generic playlist next track operation. */
|
/* Generic playlist next track operation. */
|
||||||
struct track *playlist_generic_next(struct playlist *);
|
struct track *playlist_generic_next(struct playlist *);
|
||||||
|
|
|
@ -38,7 +38,7 @@ struct playlist_ops {
|
||||||
void (*pl_set_flag)(struct playlist *, enum queue_flags, bool);
|
void (*pl_set_flag)(struct playlist *, enum queue_flags, bool);
|
||||||
|
|
||||||
/* Called to sort the playlist. */
|
/* Called to sort the playlist. */
|
||||||
void (*pl_sort)(struct playlist *, enum compare_t, bool);
|
void (*pl_sort)(struct playlist *, enum compare_t);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -77,4 +77,14 @@ struct playlist_type {
|
||||||
void (*pl_played)(struct track *);
|
void (*pl_played)(struct track *);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/* Called to clear the sort order of the playlist. */
|
||||||
|
static inline void playlist_clear_sort(struct playlist *playlist)
|
||||||
|
{
|
||||||
|
if (playlist) {
|
||||||
|
g_slist_free(playlist->pl_queue.q_sort);
|
||||||
|
playlist->pl_queue.q_sort = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* OCARINA_CORE_PLAYLISTS_PLAYLIST_H */
|
#endif /* OCARINA_CORE_PLAYLISTS_PLAYLIST_H */
|
||||||
|
|
|
@ -171,6 +171,6 @@ struct track *queue_next(struct queue *);
|
||||||
void queue_resort(struct queue *);
|
void queue_resort(struct queue *);
|
||||||
|
|
||||||
/* Called to change the sort order and resort the queue. */
|
/* Called to change the sort order and resort the queue. */
|
||||||
void queue_sort(struct queue *, enum compare_t, bool);
|
void queue_sort(struct queue *, enum compare_t);
|
||||||
|
|
||||||
#endif /* OCARINA_CORE_QUEUE_H */
|
#endif /* OCARINA_CORE_QUEUE_H */
|
||||||
|
|
|
@ -32,13 +32,24 @@ static void test_null()
|
||||||
playlist_set_random(NULL, true);
|
playlist_set_random(NULL, true);
|
||||||
g_assert_false(playlist_get_random(NULL));
|
g_assert_false(playlist_get_random(NULL));
|
||||||
|
|
||||||
g_assert_false(playlist_sort(NULL, COMPARE_TRACK, true));
|
g_assert_false(playlist_sort(NULL, COMPARE_TRACK));
|
||||||
g_assert_false(playlist_sort(NULL, COMPARE_TRACK, false));
|
g_assert_false(playlist_sort(NULL, COMPARE_TRACK));
|
||||||
|
playlist_clear_sort(NULL);
|
||||||
|
|
||||||
playlist_generic_save(NULL, NULL, PL_SAVE_ALL);
|
playlist_generic_save(NULL, NULL, PL_SAVE_ALL);
|
||||||
playlist_generic_load(NULL, NULL, PL_SAVE_ALL);
|
playlist_generic_load(NULL, NULL, PL_SAVE_ALL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_sorting()
|
||||||
|
{
|
||||||
|
struct playlist p = DEFINE_PLAYLIST(PL_MAX_TYPE, "Test", 0, NULL);
|
||||||
|
|
||||||
|
playlist_generic_init(&p, 0, NULL);
|
||||||
|
g_assert_cmpuint(g_slist_length(p.pl_queue.q_sort), ==, 3);
|
||||||
|
playlist_clear_sort(&p);
|
||||||
|
g_assert_cmpuint(g_slist_length(p.pl_queue.q_sort), ==, 0);
|
||||||
|
}
|
||||||
|
|
||||||
static void test_save_load()
|
static void test_save_load()
|
||||||
{
|
{
|
||||||
struct playlist p = DEFINE_PLAYLIST(PL_MAX_TYPE, "Test", 0, NULL);
|
struct playlist p = DEFINE_PLAYLIST(PL_MAX_TYPE, "Test", 0, NULL);
|
||||||
|
@ -53,7 +64,8 @@ static void test_save_load()
|
||||||
queue_add(&q.pl_queue, track_get(i));
|
queue_add(&q.pl_queue, track_get(i));
|
||||||
}
|
}
|
||||||
queue_set_flag(&p.pl_queue, Q_RANDOM);
|
queue_set_flag(&p.pl_queue, Q_RANDOM);
|
||||||
queue_sort(&p.pl_queue, COMPARE_TRACK, true);
|
playlist_clear_sort(&p);
|
||||||
|
queue_sort(&p.pl_queue, COMPARE_TRACK);
|
||||||
p.pl_queue.q_cur.it_pos = 3;
|
p.pl_queue.q_cur.it_pos = 3;
|
||||||
q.pl_queue.q_cur.it_pos = 4;
|
q.pl_queue.q_cur.it_pos = 4;
|
||||||
|
|
||||||
|
@ -111,6 +123,7 @@ int main(int argc, char **argv)
|
||||||
|
|
||||||
g_test_init(&argc, &argv, NULL);
|
g_test_init(&argc, &argv, NULL);
|
||||||
g_test_add_func("/Core/Playlist/NULL", test_null);
|
g_test_add_func("/Core/Playlist/NULL", test_null);
|
||||||
|
g_test_add_func("/Core/Playlists/Sorting", test_sorting);
|
||||||
g_test_add_func("/Core/Playlist/Save and Load", test_save_load);
|
g_test_add_func("/Core/Playlist/Save and Load", test_save_load);
|
||||||
ret = g_test_run();
|
ret = g_test_run();
|
||||||
|
|
||||||
|
|
|
@ -74,14 +74,14 @@ void test_library()
|
||||||
g_assert_false(playlist_get_random(playlist));
|
g_assert_false(playlist_get_random(playlist));
|
||||||
|
|
||||||
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 3);
|
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 3);
|
||||||
g_assert_true(playlist_sort(playlist, COMPARE_ARTIST, true));
|
playlist_clear_sort(playlist);
|
||||||
|
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 0);
|
||||||
|
g_assert_true(playlist_sort(playlist, COMPARE_ARTIST));
|
||||||
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 1);
|
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 1);
|
||||||
g_assert_true(playlist_sort(playlist, COMPARE_YEAR, false));
|
g_assert_true(playlist_sort(playlist, COMPARE_YEAR));
|
||||||
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 2);
|
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 2);
|
||||||
g_assert_true(playlist_sort(playlist, COMPARE_TRACK, false));
|
g_assert_true(playlist_sort(playlist, COMPARE_TRACK));
|
||||||
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 3);
|
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/");
|
g_rename("tests/Music/Hyrule Symphony/", "tests/Hyrule Symphony/");
|
||||||
pl_library_update(playlist);
|
pl_library_update(playlist);
|
||||||
|
|
|
@ -95,16 +95,17 @@ static void test_sort()
|
||||||
|
|
||||||
for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) {
|
for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) {
|
||||||
playlist = playlist_get(PL_SYSTEM, i);
|
playlist = playlist_get(PL_SYSTEM, i);
|
||||||
|
playlist_clear_sort(playlist);
|
||||||
|
|
||||||
if (i == SYS_PL_HISTORY) {
|
if (i == SYS_PL_HISTORY) {
|
||||||
g_assert_false(playlist_sort(playlist, COMPARE_TRACK, true));
|
g_assert_false(playlist_sort(playlist, COMPARE_TRACK));
|
||||||
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 0);
|
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 0);
|
||||||
g_assert_false(playlist_sort(playlist, COMPARE_YEAR, false));
|
g_assert_false(playlist_sort(playlist, COMPARE_YEAR));
|
||||||
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 0);
|
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 0);
|
||||||
} else {
|
} else {
|
||||||
g_assert_true(playlist_sort(playlist, COMPARE_TRACK, true));
|
g_assert_true(playlist_sort(playlist, COMPARE_TRACK));
|
||||||
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 1);
|
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 1);
|
||||||
g_assert_true(playlist_sort(playlist, COMPARE_YEAR, false));
|
g_assert_true(playlist_sort(playlist, COMPARE_YEAR));
|
||||||
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 2);
|
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -224,7 +224,9 @@ static void test_rand_select()
|
||||||
|
|
||||||
for (i = 0; i < 13; i++)
|
for (i = 0; i < 13; i++)
|
||||||
queue_add(&q, track_get(i));
|
queue_add(&q, track_get(i));
|
||||||
queue_sort(&q, COMPARE_TRACK, true);
|
g_slist_free(q.q_sort);
|
||||||
|
q.q_sort = NULL;
|
||||||
|
queue_sort(&q, COMPARE_TRACK);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The comments below use the following notation:
|
* The comments below use the following notation:
|
||||||
|
@ -277,40 +279,50 @@ static void test_sorting()
|
||||||
g_assert_cmpuint(track->tr_dbe.dbe_index, ==, 12 - i);
|
g_assert_cmpuint(track->tr_dbe.dbe_index, ==, 12 - i);
|
||||||
}
|
}
|
||||||
|
|
||||||
queue_sort(&q, COMPARE_TRACK, true);
|
g_slist_free(q.q_sort);
|
||||||
|
q.q_sort = NULL;
|
||||||
|
queue_sort(&q, COMPARE_TRACK);
|
||||||
for (i = 0; i < 13; i++) {
|
for (i = 0; i < 13; i++) {
|
||||||
track = queue_at(&q, i);
|
track = queue_at(&q, i);
|
||||||
g_assert_nonnull(track);
|
g_assert_nonnull(track);
|
||||||
g_assert_cmpuint(track->tr_track, ==, i + 1);
|
g_assert_cmpuint(track->tr_track, ==, i + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
queue_sort(&q, COMPARE_COUNT, true);
|
g_slist_free(q.q_sort);
|
||||||
|
q.q_sort = NULL;
|
||||||
|
queue_sort(&q, COMPARE_COUNT);
|
||||||
for (i = 0; i < 13; i++) {
|
for (i = 0; i < 13; i++) {
|
||||||
track = queue_at(&q, i);
|
track = queue_at(&q, i);
|
||||||
g_assert_nonnull(track);
|
g_assert_nonnull(track);
|
||||||
g_assert_cmpuint(track->tr_track, ==, ex_count[i]);
|
g_assert_cmpuint(track->tr_track, ==, ex_count[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
queue_sort(&q, COMPARE_TITLE, true);
|
g_slist_free(q.q_sort);
|
||||||
|
q.q_sort = NULL;
|
||||||
|
queue_sort(&q, COMPARE_TITLE);
|
||||||
for (i = 0; i < 13; i++) {
|
for (i = 0; i < 13; i++) {
|
||||||
track = queue_at(&q, i);
|
track = queue_at(&q, i);
|
||||||
g_assert_nonnull(track);
|
g_assert_nonnull(track);
|
||||||
g_assert_cmpuint(track->tr_track, ==, ex_title[i]);
|
g_assert_cmpuint(track->tr_track, ==, ex_title[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
queue_sort(&q, COMPARE_COUNT, true);
|
g_slist_free(q.q_sort);
|
||||||
queue_sort(&q, COMPARE_TITLE, false);
|
q.q_sort = NULL;
|
||||||
queue_sort(&q, COMPARE_COUNT, false);
|
queue_sort(&q, COMPARE_COUNT);
|
||||||
|
queue_sort(&q, COMPARE_TITLE);
|
||||||
|
queue_sort(&q, COMPARE_COUNT);
|
||||||
for (i = 0; i < 13; i++) {
|
for (i = 0; i < 13; i++) {
|
||||||
track = queue_at(&q, i);
|
track = queue_at(&q, i);
|
||||||
g_assert_nonnull(track);
|
g_assert_nonnull(track);
|
||||||
g_assert_cmpuint(track->tr_track, ==, ex_co_ti[i]);
|
g_assert_cmpuint(track->tr_track, ==, ex_co_ti[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
queue_sort(&q, COMPARE_ARTIST, true);
|
g_slist_free(q.q_sort);
|
||||||
queue_sort(&q, COMPARE_ALBUM, false);
|
q.q_sort = NULL;
|
||||||
queue_sort(&q, COMPARE_TRACK, false);
|
queue_sort(&q, COMPARE_ARTIST);
|
||||||
queue_sort(&q, COMPARE_TRACK, false);
|
queue_sort(&q, COMPARE_ALBUM);
|
||||||
|
queue_sort(&q, COMPARE_TRACK);
|
||||||
|
queue_sort(&q, COMPARE_TRACK);
|
||||||
for (i = 0; i < 13; i++) {
|
for (i = 0; i < 13; i++) {
|
||||||
track = queue_at(&q, i);
|
track = queue_at(&q, i);
|
||||||
g_assert_nonnull(track);
|
g_assert_nonnull(track);
|
||||||
|
|
Loading…
Reference in New Issue