From 33894d7068a5a4dfe7bbf75951800654ce8fd69b Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Fri, 5 Aug 2016 10:27:36 -0400 Subject: [PATCH] core/playlists/system: Add a new function for loading playlists This function loads playlist information from a single file, falling back to multi-file loading if playlist.system doesn't exist yet. Signed-off-by: Anna Schumaker --- core/playlists/system.c | 52 ++++++++++++++++++++++++++++++--- include/core/playlists/system.h | 1 + 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/core/playlists/system.c b/core/playlists/system.c index e176e8e1..8a3a54eb 100644 --- a/core/playlists/system.c +++ b/core/playlists/system.c @@ -10,6 +10,7 @@ static bool pl_system_remove_track(const gchar *, struct track *); static void pl_system_update(const gchar *); static inline struct queue *__sys_pl_queue(enum sys_playlist_t); static void __sys_pl_save(); +static bool __sys_pl_load(); static void __sys_pl_save_new(); static struct file sys_file = FILE_INIT("playlist.db", 0, 0); @@ -47,6 +48,17 @@ static void sys_pl_save_full(struct playlist *playlist, struct file *file) queue_save_tracks(&playlist->pl_queue, file); } +static void sys_pl_load_partial(struct playlist *playlist, struct file *file) +{ + queue_load_flags(&playlist->pl_queue, file, true); +} + +static void sys_pl_load_full(struct playlist *playlist, struct file *file) +{ + sys_pl_load_partial(playlist, file); + queue_load_tracks(&playlist->pl_queue, file); +} + static bool sys_pl_generic_add(struct playlist *playlist, struct track *track) { if (queue_has(__sys_pl_queue(SYS_PL_HIDDEN), track)) @@ -86,6 +98,7 @@ static struct sys_playlist sys_favorites = { .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Favorites"), .spl_init = playlist_generic_init, .spl_save = sys_pl_save_full, + .spl_load = sys_pl_load_full, .spl_can_select = playlist_generic_can_select, .spl_add = sys_pl_favorites_add, .spl_remove = sys_pl_favorites_remove, @@ -146,6 +159,7 @@ static struct sys_playlist sys_hidden = { .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Hidden"), .spl_init = playlist_generic_init, .spl_save = sys_pl_save_full, + .spl_load = sys_pl_load_full, .spl_can_select = playlist_generic_can_select, .spl_add = sys_pl_hidden_add, .spl_remove = sys_pl_hidden_remove, @@ -246,6 +260,7 @@ static struct sys_playlist sys_queued = { .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Queued Tracks"), .spl_init = sys_pl_queued_init, .spl_save = sys_pl_save_full, + .spl_load = sys_pl_load_full, .spl_can_select = playlist_noop_can_select, .spl_add = sys_pl_queued_add, .spl_remove = sys_pl_queued_remove, @@ -321,6 +336,7 @@ static struct sys_playlist sys_collection = { .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Collection"), .spl_init = sys_pl_update_init, .spl_save = sys_pl_save_partial, + .spl_load = sys_pl_load_partial, .spl_can_select = sys_pl_collection_can_select, .spl_add = sys_pl_collection_add, .spl_remove = playlist_generic_remove_track, @@ -354,6 +370,7 @@ static struct sys_playlist sys_history = { .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "History"), .spl_init = sys_pl_history_init, .spl_save = sys_pl_save_partial, + .spl_load = sys_pl_load_partial, .spl_can_select = playlist_noop_can_select, .spl_add = sys_pl_history_add, .spl_remove = playlist_generic_remove_track, @@ -386,6 +403,7 @@ static struct sys_playlist sys_unplayed = { .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Unplayed"), .spl_init = sys_pl_update_init, .spl_save = sys_pl_save_partial, + .spl_load = sys_pl_load_partial, .spl_can_select = playlist_generic_can_select, .spl_add = sys_pl_unplayed_add, .spl_remove = playlist_generic_remove_track, @@ -421,6 +439,7 @@ static struct sys_playlist sys_most_played = { .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Most Played"), .spl_init = sys_pl_update_init, .spl_save = sys_pl_save_partial, + .spl_load = sys_pl_load_partial, .spl_can_select = playlist_generic_can_select, .spl_add = sys_pl_most_played_add, .spl_remove = playlist_generic_remove_track, @@ -456,6 +475,7 @@ static struct sys_playlist sys_least_played = { .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Least Played"), .spl_init = sys_pl_update_init, .spl_save = sys_pl_save_partial, + .spl_load = sys_pl_load_partial, .spl_can_select = playlist_generic_can_select, .spl_clear = playlist_noop_clear, .spl_add = sys_pl_least_played_add, @@ -521,6 +541,33 @@ static bool __sys_pl_update_save() return true; } +static bool __sys_pl_load_new() +{ + struct sys_playlist *sys_pl; + unsigned int i, n; + gchar *name; + + if (!file_open(&sys_pl_file, OPEN_READ)) { + idle_schedule(IDLE_SYNC, __sys_pl_load, NULL); + idle_schedule(IDLE_SYNC, sys_pl_collection_load, NULL); + idle_schedule(IDLE_SYNC, sys_pl_queued_load, NULL); + idle_schedule(IDLE_SYNC, __sys_pl_update_save, NULL); + return true; + } + + file_readf(&sys_pl_file, "%u\n", &n); + for (i = 0; i < n; i++) { + name = file_readl(&sys_pl_file); + sys_pl = __sys_pl_lookup(name); + if (sys_pl) + sys_pl->spl_load(&sys_pl->spl_playlist, &sys_pl_file); + g_free(name); + } + + file_close(&sys_pl_file); + return true; +} + static void __sys_pl_save() { if (!file_open(&sys_file, OPEN_WRITE)) @@ -672,10 +719,7 @@ void pl_system_init(struct queue_ops *ops) struct sys_playlist *sys_pl; unsigned int i; - idle_schedule(IDLE_SYNC, __sys_pl_load, NULL); - idle_schedule(IDLE_SYNC, sys_pl_collection_load, NULL); - idle_schedule(IDLE_SYNC, sys_pl_queued_load, NULL); - idle_schedule(IDLE_SYNC, __sys_pl_update_save, NULL); + idle_schedule(IDLE_SYNC, __sys_pl_load_new, NULL); for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) { sys_pl = sys_playlists[i]; diff --git a/include/core/playlists/system.h b/include/core/playlists/system.h index b7d404ca..d26a46e7 100644 --- a/include/core/playlists/system.h +++ b/include/core/playlists/system.h @@ -23,6 +23,7 @@ struct sys_playlist { void (*spl_init)(struct playlist *, unsigned int, struct queue_ops *); void (*spl_save)(struct playlist *, struct file *); + void (*spl_load)(struct playlist *, struct file *); bool (*spl_can_select)(struct playlist *); bool (*spl_add)(struct playlist *, struct track *); bool (*spl_remove)(struct playlist *, struct track *);