diff --git a/core/playlists/system.c b/core/playlists/system.c index 959e648b..e176e8e1 100644 --- a/core/playlists/system.c +++ b/core/playlists/system.c @@ -10,10 +10,12 @@ 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 void __sys_pl_save_new(); static struct file sys_file = FILE_INIT("playlist.db", 0, 0); static struct file sys_deck_f = FILE_INIT("deck", 1, 1); static struct file sys_collection_f = FILE_INIT("library.q", 0, 0); +static struct file sys_pl_file = FILE_INIT("playlist.system", 0, 0); static struct sys_playlist *sys_playlists[SYS_PL_NUM_PLAYLISTS]; @@ -33,6 +35,18 @@ static void sys_pl_update_init(struct playlist *playlist, unsigned int flags, pl_system_update(playlist->pl_name); } +static void sys_pl_save_partial(struct playlist *playlist, struct file *file) +{ + file_writef(file, "%s\n", playlist->pl_name); + queue_save_flags(&playlist->pl_queue, file, true); +} + +static void sys_pl_save_full(struct playlist *playlist, struct file *file) +{ + sys_pl_save_partial(playlist, file); + queue_save_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)) @@ -49,6 +63,7 @@ static bool sys_pl_favorites_add(struct playlist *playlist, struct track *track) { bool ret = playlist_generic_add_track(playlist, track); __sys_pl_save(); + __sys_pl_save_new(); return ret; } @@ -56,6 +71,7 @@ static bool sys_pl_favorites_remove(struct playlist *playlist, struct track *tra { bool ret = playlist_generic_remove_track(playlist, track); __sys_pl_save(); + __sys_pl_save_new(); return ret; } @@ -63,11 +79,13 @@ static void sys_pl_favorites_clear(struct playlist *playlist) { playlist_generic_clear(playlist); __sys_pl_save(); + __sys_pl_save_new(); } 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_can_select = playlist_generic_can_select, .spl_add = sys_pl_favorites_add, .spl_remove = sys_pl_favorites_remove, @@ -89,6 +107,7 @@ static bool sys_pl_hidden_add(struct playlist *playlist, struct track *track) pl_system_remove_track("Most Played", track); pl_system_remove_track("Least Played", track); __sys_pl_save(); + __sys_pl_save_new(); return ret; } @@ -107,6 +126,7 @@ static bool sys_pl_hidden_remove(struct playlist *playlist, struct track *track) { bool ret = sys_pl_hidden_on_remove(playlist, track); __sys_pl_save(); + __sys_pl_save_new(); return ret; } @@ -119,11 +139,13 @@ static void sys_pl_hidden_clear(struct playlist *playlist) sys_pl_hidden_on_remove(playlist, track); } __sys_pl_save(); + __sys_pl_save_new(); } 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_can_select = playlist_generic_can_select, .spl_add = sys_pl_hidden_add, .spl_remove = sys_pl_hidden_remove, @@ -147,7 +169,6 @@ static void sys_pl_queued_save(struct playlist *playlist) else { file_writef(&sys_deck_f, "%zu\n%u ", 1, queue->q_flags); queue_save_tracks(queue, &sys_deck_f); - file_writef(&sys_deck_f, "\n"); } file_close(&sys_deck_f); @@ -186,6 +207,7 @@ static bool sys_pl_queued_add(struct playlist *playlist, struct track *track) { bool ret = playlist_generic_add_track(playlist, track); sys_pl_queued_save(playlist); + __sys_pl_save_new(); return ret; } @@ -193,6 +215,7 @@ static bool sys_pl_queued_remove(struct playlist *playlist, struct track *track) { bool ret = playlist_generic_remove_track(playlist, track); sys_pl_queued_save(playlist); + __sys_pl_save_new(); return ret; } @@ -200,6 +223,7 @@ static void sys_pl_queued_clear(struct playlist *playlist) { playlist_generic_clear(playlist); sys_pl_queued_save(playlist); + __sys_pl_save_new(); } static void sys_pl_queued_set_flag(struct playlist *playlist, @@ -207,18 +231,21 @@ static void sys_pl_queued_set_flag(struct playlist *playlist, { playlist_generic_set_flag(playlist, flag, enabled); sys_pl_queued_save(playlist); + __sys_pl_save_new(); } static struct track *sys_pl_queued_next(struct playlist *playlist) { struct track *track = playlist_generic_next(playlist); sys_pl_queued_save(playlist); + __sys_pl_save_new(); return track; } 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_can_select = playlist_noop_can_select, .spl_add = sys_pl_queued_add, .spl_remove = sys_pl_queued_remove, @@ -279,6 +306,7 @@ static void sys_pl_collection_set_flag(struct playlist *playlist, { playlist_generic_set_flag(playlist, flag, enabled); sys_pl_collection_save(playlist); + __sys_pl_save_new(); } static void sys_pl_collection_sort(struct playlist *playlist, @@ -286,11 +314,13 @@ static void sys_pl_collection_sort(struct playlist *playlist, { playlist_generic_sort(playlist, sort, reset); sys_pl_collection_save(playlist); + __sys_pl_save_new(); } 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_can_select = sys_pl_collection_can_select, .spl_add = sys_pl_collection_add, .spl_remove = playlist_generic_remove_track, @@ -323,6 +353,7 @@ static bool sys_pl_history_add(struct playlist *playlist, struct track *track) 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_can_select = playlist_noop_can_select, .spl_add = sys_pl_history_add, .spl_remove = playlist_generic_remove_track, @@ -354,6 +385,7 @@ static bool sys_pl_unplayed_update(struct playlist *playlist, 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_can_select = playlist_generic_can_select, .spl_add = sys_pl_unplayed_add, .spl_remove = playlist_generic_remove_track, @@ -388,6 +420,7 @@ static bool sys_pl_most_played_update(struct playlist *playlist, 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_can_select = playlist_generic_can_select, .spl_add = sys_pl_most_played_add, .spl_remove = playlist_generic_remove_track, @@ -422,6 +455,7 @@ static bool sys_pl_least_played_update(struct playlist *playlist, 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_can_select = playlist_generic_can_select, .spl_clear = playlist_noop_clear, .spl_add = sys_pl_least_played_add, @@ -464,6 +498,29 @@ static inline struct queue *__sys_pl_queue(enum sys_playlist_t plist) return &sys_playlists[plist]->spl_playlist.pl_queue; } +static void __sys_pl_save_new() +{ + struct sys_playlist *sys_pl; + unsigned int i; + + 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++) { + sys_pl = sys_playlists[i]; + sys_pl->spl_save(&sys_pl->spl_playlist, &sys_pl_file); + } + + file_close(&sys_pl_file); +} + +static bool __sys_pl_update_save() +{ + __sys_pl_save_new(); + return true; +} + static void __sys_pl_save() { if (!file_open(&sys_file, OPEN_WRITE)) @@ -471,9 +528,8 @@ static void __sys_pl_save() file_writef(&sys_file, "%u\n 1 %s\n", 2, "Favorites"); queue_save_tracks(__sys_pl_queue(SYS_PL_FAVORITES), &sys_file); - file_writef(&sys_file, "\n1 %s\n", "Banned"); + file_writef(&sys_file, "1 %s\n", "Banned"); queue_save_tracks(__sys_pl_queue(SYS_PL_HIDDEN), &sys_file); - file_writef(&sys_file, "\n"); file_close(&sys_file); } @@ -568,23 +624,30 @@ static void pl_system_set_flag(const gchar *name, enum queue_flags flag, bool enabled) { struct sys_playlist *sys_pl = __sys_pl_lookup(name); - if (sys_pl) + if (sys_pl) { sys_pl->spl_set_flag(&sys_pl->spl_playlist, flag, enabled); + __sys_pl_save_new(); + } } 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) + if (sys_pl) { sys_pl->spl_sort(&sys_pl->spl_playlist, sort, reset); + __sys_pl_save_new(); + } } static struct track *pl_system_next(const gchar *name) { struct sys_playlist *sys_pl = __sys_pl_lookup(name); - if (sys_pl) - return sys_pl->spl_next(&sys_pl->spl_playlist); - return NULL; + struct track *track = NULL;; + if (sys_pl) { + track = sys_pl->spl_next(&sys_pl->spl_playlist); + __sys_pl_save_new(); + } + return track; } @@ -612,6 +675,7 @@ void pl_system_init(struct queue_ops *ops) 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); 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 50281a74..b7d404ca 100644 --- a/include/core/playlists/system.h +++ b/include/core/playlists/system.h @@ -22,6 +22,7 @@ struct sys_playlist { struct playlist spl_playlist; void (*spl_init)(struct playlist *, unsigned int, struct queue_ops *); + void (*spl_save)(struct playlist *, struct file *); bool (*spl_can_select)(struct playlist *); bool (*spl_add)(struct playlist *, struct track *); bool (*spl_remove)(struct playlist *, struct track *);