ocarina/core/playlists/library.c

95 lines
2.0 KiB
C

/*
* Copyright 2016 (c) Anna Schumaker.
*/
#include <core/idle.h>
#include <core/playlists/library.h>
static struct queue_ops *lib_ops = NULL;
static struct playlist *__lib_pl_alloc(const gchar *path)
{
struct playlist *playlist = g_malloc(sizeof(struct playlist));
playlist->pl_name = path;
playlist->pl_type = PL_LIBRARY;
queue_init(&playlist->pl_queue, Q_ENABLED | Q_REPEAT, lib_ops, playlist);
return playlist;
}
static void __lib_pl_free(struct playlist *playlist)
{
if (playlist) {
queue_deinit(&playlist->pl_queue);
g_free(playlist);
}
}
static bool __lib_pl_load(void *data)
{
struct playlist *playlist = (struct playlist *)data;
struct library *library = library_lookup(playlist->pl_name);
struct db_entry *dbe, *next;
queue_set_flag(&playlist->pl_queue, Q_ADD_FRONT);
db_for_each(dbe, next, track_db_get()) {
if (TRACK(dbe)->tr_library == library)
queue_add(&playlist->pl_queue, TRACK(dbe));
}
queue_unset_flag(&playlist->pl_queue, Q_ADD_FRONT);
return true;
}
static struct queue *pl_library_get_queue(const gchar *name)
{
struct library *library = library_lookup(name);
struct playlist *playlist = NULL;
if (library)
playlist = (struct playlist *)library->li_playlist;
return playlist ? &playlist->pl_queue : NULL;
}
struct playlist_type pl_library = {
.pl_get_queue = pl_library_get_queue,
};
static bool __lib_pl_init(void *data)
{
struct db_entry *dbe, *next;
struct playlist *playlist;
db_for_each(dbe, next, library_db_get()) {
playlist = __lib_pl_alloc(LIBRARY(dbe)->li_path);
LIBRARY(dbe)->li_playlist = playlist;
idle_schedule(IDLE_SYNC, __lib_pl_load, playlist);
}
return true;
}
void pl_library_init(struct queue_ops *ops)
{
lib_ops = ops;
idle_schedule(IDLE_SYNC, __lib_pl_init, NULL);
}
void pl_library_deinit()
{
struct db_entry *dbe, *next;
struct playlist *playlist;
db_for_each(dbe, next, library_db_get()) {
playlist = LIBRARY(dbe)->li_playlist;
LIBRARY(dbe)->li_playlist = NULL;
__lib_pl_free(playlist);
}
}