diff --git a/core/playlist.c b/core/playlist.c index 9ad49499..cb16c9f5 100644 --- a/core/playlist.c +++ b/core/playlist.c @@ -32,6 +32,13 @@ void playlist_deinit() pl_library_deinit(); } +void playlist_save() +{ + unsigned int i; + for (i = 0; i < PL_MAX_TYPE; i++) + playlist_types[i]->pl_save(); +} + bool playlist_select(enum playlist_type_t type, const gchar *name) { if (!playlist_types[type]->pl_can_select(name)) diff --git a/core/playlists/artist.c b/core/playlists/artist.c index 05b15012..1642c9b0 100644 --- a/core/playlists/artist.c +++ b/core/playlists/artist.c @@ -50,24 +50,6 @@ static struct playlist *__artist_pl_lookup(const gchar *name) return artist ? artist->ar_playlist : NULL; } -static void __artist_pl_save(void) -{ - struct db_entry *dbe, *next; - struct playlist *playlist; - - if (!file_open(&artist_file, OPEN_WRITE)) - return; - - file_writef(&artist_file, "%u\n", artist_db_get()->db_size); - 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); - } - - file_close(&artist_file); -} - static bool __artist_pl_load(void *data) { struct playlist *playlist; @@ -94,6 +76,24 @@ static bool __artist_pl_load(void *data) } +static void pl_artist_save(void) +{ + struct db_entry *dbe, *next; + struct playlist *playlist; + + if (!file_open(&artist_file, OPEN_WRITE)) + return; + + file_writef(&artist_file, "%u\n", artist_db_get()->db_size); + 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); + } + + file_close(&artist_file); +} + static struct queue *pl_artist_get_queue(const gchar *name) { struct playlist *playlist = __artist_pl_lookup(name); @@ -137,26 +137,27 @@ static void pl_artist_set_flag(const gchar *name, enum queue_flags flag, { struct playlist *playlist = __artist_pl_lookup(name); playlist_generic_set_flag(playlist, flag, enabled); - __artist_pl_save(); + pl_artist_save(); } static void pl_artist_sort(const gchar *name, enum compare_t sort, bool reset) { struct playlist *playlist = __artist_pl_lookup(name); playlist_generic_sort(playlist, sort, reset); - __artist_pl_save(); + pl_artist_save(); } static struct track *pl_artist_next(const gchar *name) { struct playlist *playlist = __artist_pl_lookup(name); struct track *track = playlist_generic_next(playlist);; - __artist_pl_save(); + pl_artist_save(); return track; } struct playlist_type pl_artist = { + .pl_save = pl_artist_save, .pl_get_queue = pl_artist_get_queue, .pl_get_id = pl_artist_get_id, .pl_get_name = pl_artist_get_name, diff --git a/core/playlists/library.c b/core/playlists/library.c index b7b30433..3a3bc4f0 100644 --- a/core/playlists/library.c +++ b/core/playlists/library.c @@ -58,24 +58,6 @@ static struct playlist *__lib_pl_lookup(const gchar *name) return library ? library->li_playlist : NULL; } -static void __lib_pl_save(void) -{ - struct db_entry *dbe, *next; - struct playlist *playlist; - - if (!file_open(&lib_file, OPEN_WRITE)) - return; - - file_writef(&lib_file, "%u\n", library_db_get()->db_size); - 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); - } - - file_close(&lib_file); -} - static bool __lib_pl_load(void *data) { struct playlist *playlist; @@ -183,6 +165,24 @@ static bool __lib_pl_update(void *data) } +static void pl_library_save(void) +{ + struct db_entry *dbe, *next; + struct playlist *playlist; + + if (!file_open(&lib_file, OPEN_WRITE)) + return; + + file_writef(&lib_file, "%u\n", library_db_get()->db_size); + 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); + } + + file_close(&lib_file); +} + static struct queue *pl_library_get_queue(const gchar *name) { struct playlist *playlist = __lib_pl_lookup(name); @@ -238,7 +238,7 @@ static bool pl_library_delete(const gchar *name) track_remove_all(library); library_remove(library); - __lib_pl_save(); + pl_library_save(); return true; } @@ -259,26 +259,27 @@ static void pl_library_set_flag(const gchar *name, enum queue_flags flag, { struct playlist *playlist = __lib_pl_lookup(name); playlist_generic_set_flag(playlist, flag, enabled); - __lib_pl_save(); + pl_library_save(); } static void pl_library_sort(const gchar *name, enum compare_t sort, bool reset) { struct playlist *playlist = __lib_pl_lookup(name); playlist_generic_sort(playlist, sort, reset); - __lib_pl_save(); + pl_library_save(); } static struct track *pl_library_next(const gchar *name) { struct playlist *playlist = __lib_pl_lookup(name); struct track *track = playlist_generic_next(playlist); - __lib_pl_save(); + pl_library_save(); return track; } struct playlist_type pl_library = { + .pl_save = pl_library_save, .pl_get_queue = pl_library_get_queue, .pl_get_id = pl_library_get_id, .pl_get_name = pl_library_get_name, diff --git a/core/playlists/system.c b/core/playlists/system.c index 5289864a..90cbea8e 100644 --- a/core/playlists/system.c +++ b/core/playlists/system.c @@ -479,6 +479,11 @@ static bool __sys_pl_load() } +static void pl_system_save(void) +{ + __sys_pl_save(); +} + static struct queue *pl_system_get_queue(const gchar *name) { struct sys_playlist *sys_pl = __sys_pl_lookup(name); @@ -589,6 +594,7 @@ static struct track *pl_system_next(const gchar *name) struct playlist_type pl_system = { + .pl_save = pl_system_save, .pl_get_queue = pl_system_get_queue, .pl_get_id = pl_system_get_id, .pl_get_name = pl_system_get_name, diff --git a/include/core/playlist.h b/include/core/playlist.h index 97a48532..bf82b64f 100644 --- a/include/core/playlist.h +++ b/include/core/playlist.h @@ -20,6 +20,9 @@ void playlist_init(struct queue_ops *); /* Called to deinitialize the playlist manager. */ void playlist_deinit(); +/* Called to force-save all playlists. */ +void playlist_save(); + /* Called to select the current playlist. */ bool playlist_select(enum playlist_type_t, const gchar *); diff --git a/include/core/playlists/type.h b/include/core/playlists/type.h index 537290df..ff361e68 100644 --- a/include/core/playlists/type.h +++ b/include/core/playlists/type.h @@ -28,6 +28,9 @@ struct playlist { struct playlist_type { + /* Called to save all playlists of the given type. */ + void (*pl_save)(void); + /* Called to get the queue for the playlist. */ struct queue *(*pl_get_queue)(const gchar *);