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 <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-09-16 10:04:51 -04:00
parent 37d95656e9
commit d2335f5c6e
10 changed files with 50 additions and 67 deletions

View File

@ -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);

View File

@ -3,6 +3,7 @@
*/
#include <core/idle.h>
#include <core/playlists/generic.h>
#include <stdlib.h>
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)
{

View File

@ -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);

View File

@ -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);

View File

@ -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);
}

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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));

View File

@ -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();