From d2335f5c6e17b5c1a61923aa0cabf4646628de90 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Fri, 16 Sep 2016 10:04:51 -0400 Subject: [PATCH] core/playlist: Add a playlist_generic_save() function This function uses the playlist save flags enum to determine what exactly to save, including support for backwards compatibility with 6.4.x playlists. Signed-off-by: Anna Schumaker --- core/playlists/artist.c | 2 +- core/playlists/generic.c | 34 ++++++++++++++++++++++++++++++++ core/playlists/library.c | 2 +- core/playlists/system.c | 13 ++++++------ core/playlists/user.c | 3 +-- core/queue.c | 27 ------------------------- include/core/playlists/generic.h | 3 +++ include/core/queue.h | 7 ------- tests/core/playlist.c | 6 +++--- tests/core/queue.c | 20 ------------------- 10 files changed, 50 insertions(+), 67 deletions(-) diff --git a/core/playlists/artist.c b/core/playlists/artist.c index 88b690bc..e1721b50 100644 --- a/core/playlists/artist.c +++ b/core/playlists/artist.c @@ -94,7 +94,7 @@ static void pl_artist_save(void) db_for_each(dbe, next, artist_db_get()) { playlist = ARTIST(dbe)->ar_playlist; file_writef(&artist_file, "%s\n", playlist->pl_name); - queue_save_flags(&playlist->pl_queue, &artist_file, true); + playlist_generic_save(playlist, &artist_file, PL_SAVE_METADATA); } file_close(&artist_file); diff --git a/core/playlists/generic.c b/core/playlists/generic.c index 1151c8b0..f325e1a8 100644 --- a/core/playlists/generic.c +++ b/core/playlists/generic.c @@ -3,6 +3,7 @@ */ #include #include +#include static struct playlist_callbacks *callbacks = NULL; @@ -22,6 +23,39 @@ void playlist_generic_init(struct playlist *playlist, unsigned int flags, playlist->pl_private = NULL; } +void playlist_generic_save(struct playlist *playlist, struct file *file, + unsigned int flags) +{ + struct queue_iter it; + GSList *sort; + int field; + + if (!playlist) + return; + + if (flags & PL_SAVE_ITER) + file_writef(file, "%u ", playlist->pl_queue.q_cur.it_pos); + + if (flags & PL_SAVE_FLAGS) { + sort = playlist->pl_queue.q_sort; + file_writef(file, "%u ", playlist->pl_queue.q_flags); + file_writef(file, "%u", g_slist_length(sort)); + while (sort) { + field = GPOINTER_TO_INT(sort->data); + file_writef(file, " %u %d", abs(field) - 1, field > 0); + sort = g_slist_next(sort); + } + file_writef(file, "\n"); + } + + if (flags & PL_SAVE_TRACKS) { + file_writef(file, "%u", queue_size(&playlist->pl_queue)); + queue_for_each(&playlist->pl_queue, &it) + file_writef(file, " %u", track_index(queue_iter_val(&it))); + file_writef(file, "\n"); + } +} + void playlist_generic_load(struct playlist *playlist, struct file *file, unsigned int flags) { diff --git a/core/playlists/library.c b/core/playlists/library.c index bbdf76e9..1892df51 100644 --- a/core/playlists/library.c +++ b/core/playlists/library.c @@ -211,7 +211,7 @@ static void pl_library_save(void) db_for_each(dbe, next, library_db_get()) { playlist = LIBRARY(dbe)->li_playlist; file_writef(&lib_file, "%s\n", playlist->pl_name); - queue_save_flags(&playlist->pl_queue, &lib_file, true); + playlist_generic_save(playlist, &lib_file, PL_SAVE_METADATA); } file_close(&lib_file); diff --git a/core/playlists/system.c b/core/playlists/system.c index adf99cc4..6a861b45 100644 --- a/core/playlists/system.c +++ b/core/playlists/system.c @@ -329,25 +329,26 @@ static bool __sys_pl_load_new() static void pl_system_save(void) { struct playlist *playlist; - unsigned int i; + unsigned int i, save; if (!file_open(&sys_pl_file, OPEN_WRITE)) return; file_writef(&sys_pl_file, "%u\n", SYS_PL_NUM_PLAYLISTS); for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) { + save = PL_SAVE_METADATA; playlist = pl_system_get(i); - file_writef(&sys_pl_file, "%s\n", playlist->pl_name); - queue_save_flags(&playlist->pl_queue, &sys_pl_file, true); switch (i) { case SYS_PL_FAVORITES: case SYS_PL_HIDDEN: case SYS_PL_QUEUED: - queue_save_tracks(&playlist->pl_queue, &sys_pl_file); - default: - break; + save = PL_SAVE_ALL; } + + file_writef(&sys_pl_file, "%s\n", playlist->pl_name); + playlist_generic_save(playlist, &sys_pl_file, save); + } file_close(&sys_pl_file); diff --git a/core/playlists/user.c b/core/playlists/user.c index fe1f4c39..aff8e62d 100644 --- a/core/playlists/user.c +++ b/core/playlists/user.c @@ -52,8 +52,7 @@ static void user_db_write(struct file *file, struct db_entry *dbe) struct playlist *playlist = &USER_PLAYLIST(dbe)->pl_playlist; file_writef(file, "%s\n", playlist->pl_name); - queue_save_flags(&playlist->pl_queue, file, true); - queue_save_tracks(&playlist->pl_queue, file); + playlist_generic_save(playlist, file, PL_SAVE_ALL); } diff --git a/core/queue.c b/core/queue.c index f49660ea..3f7e23f2 100644 --- a/core/queue.c +++ b/core/queue.c @@ -136,33 +136,6 @@ void queue_deinit(struct queue *queue) queue->q_sort = NULL; } -void queue_save_flags(struct queue *queue, struct file *file, bool iter) -{ - GSList *cur = queue->q_sort; - int field; - - if (iter) - file_writef(file, "%u ", queue->q_cur.it_pos); - - file_writef(file, "%u %u", queue->q_flags, g_slist_length(cur)); - while (cur) { - field = GPOINTER_TO_INT(cur->data); - file_writef(file, " %u %d", abs(field) - 1, field > 0); - cur = g_slist_next(cur); - } - file_writef(file, "\n"); -} - -void queue_save_tracks(struct queue *queue, struct file *file) -{ - struct queue_iter it; - - file_writef(file, "%u", queue_size(queue)); - queue_for_each(queue, &it) - file_writef(file, " %u", track_index(queue_iter_val(&it))); - file_writef(file, "\n"); -} - void queue_set_flag(struct queue *queue, enum queue_flags flag) { queue->q_flags |= flag; diff --git a/include/core/playlists/generic.h b/include/core/playlists/generic.h index 1c8a2549..bfb50fe9 100644 --- a/include/core/playlists/generic.h +++ b/include/core/playlists/generic.h @@ -23,6 +23,9 @@ void playlist_generic_set_callbacks(struct playlist_callbacks *); /* Generic playlist init function. */ void playlist_generic_init(struct playlist *, unsigned int, struct queue_ops *); +/* Generic playlist save function. */ +void playlist_generic_save(struct playlist *, struct file *, unsigned int); + /* Generic playlist load function. */ void playlist_generic_load(struct playlist *, struct file *, unsigned int); diff --git a/include/core/queue.h b/include/core/queue.h index d176c8cf..e9885763 100644 --- a/include/core/queue.h +++ b/include/core/queue.h @@ -114,13 +114,6 @@ void queue_init(struct queue *, unsigned int, const struct queue_ops *, void *); void queue_deinit(struct queue *); -/* Called to save queue flags, sort order, and (optionally) iterator pos. */ -void queue_save_flags(struct queue *, struct file *, bool); - -/* Called to save the list of queued tracks. */ -void queue_save_tracks(struct queue *, struct file *); - - /* Called to set a queue flag. */ void queue_set_flag(struct queue *, enum queue_flags); diff --git a/tests/core/playlist.c b/tests/core/playlist.c index a41d9790..17d53794 100644 --- a/tests/core/playlist.c +++ b/tests/core/playlist.c @@ -35,6 +35,7 @@ static void test_null() g_assert_false(playlist_sort(NULL, COMPARE_TRACK, true)); g_assert_false(playlist_sort(NULL, COMPARE_TRACK, false)); + playlist_generic_save(NULL, NULL, PL_SAVE_ALL); playlist_generic_load(NULL, NULL, PL_SAVE_ALL); } @@ -58,9 +59,8 @@ static void test_save_load() g_assert_false(file_exists(&f)); g_assert_true( file_open(&f, OPEN_WRITE)); - queue_save_flags(&p.pl_queue, &f, true); - queue_save_flags(&q.pl_queue, &f, true); - queue_save_tracks(&q.pl_queue, &f); + playlist_generic_save(&p, &f, PL_SAVE_METADATA); + playlist_generic_save(&q, &f, PL_SAVE_ALL); file_close(&f); g_assert_true(file_open(&f, OPEN_READ)); diff --git a/tests/core/queue.c b/tests/core/queue.c index c3075221..445bba43 100644 --- a/tests/core/queue.c +++ b/tests/core/queue.c @@ -323,25 +323,6 @@ static void test_sorting() g_assert_null(q.q_sort); } -static void test_save_load() -{ - struct file f = FILE_INIT("queue.q", 0); - struct queue q, r; - unsigned int i; - - queue_init(&q, 0, &test_ops, NULL); - queue_init(&r, 0, &test_ops, NULL); - - for (i = 0; i < 13; i++) - queue_add(&q, track_get(i)); - - g_assert_false(file_exists(&f)); - g_assert_true(file_open(&f, OPEN_WRITE)); - queue_save_tracks(&q, &f); - file_close(&f); - g_assert_true(file_exists(&f)); -} - int main(int argc, char **argv) { struct library *library; @@ -375,7 +356,6 @@ int main(int argc, char **argv) g_test_add_data_func("/Core/Queue/n = 100,009)", GUINT_TO_POINTER(100009), test_queue); g_test_add_func("/Core/Queue/Random Next and Selection", test_rand_select); g_test_add_func("/Core/Queue/Sorting", test_sorting); - g_test_add_func("/Core/Queue/Save and Load", test_save_load); ret = g_test_run(); tags_deinit();