core/playlist: Add a function to force save playlists

This is needed to handle track database defragmentation, but could also
be expanded on later to save playlists from a generic place.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-08-12 16:12:17 -04:00
parent 54bb0ffe2b
commit f726b6994c
6 changed files with 64 additions and 43 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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