126 lines
2.9 KiB
C
126 lines
2.9 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);
|
|
queue_sort(&playlist->pl_queue, COMPARE_ARTIST, true);
|
|
queue_sort(&playlist->pl_queue, COMPARE_YEAR, false);
|
|
queue_sort(&playlist->pl_queue, COMPARE_TRACK, false);
|
|
|
|
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 playlist *__lib_pl_lookup(const gchar *name)
|
|
{
|
|
struct library *library = library_lookup(name);
|
|
return library ? library->li_playlist : NULL;
|
|
}
|
|
|
|
|
|
static struct queue *pl_library_get_queue(const gchar *name)
|
|
{
|
|
struct playlist *playlist = __lib_pl_lookup(name);
|
|
return playlist ? &playlist->pl_queue : NULL;
|
|
}
|
|
|
|
static bool pl_library_add_rm(const gchar *name, struct track *track)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
static void pl_library_set_flag(const gchar *name, enum queue_flags flag,
|
|
bool enabled)
|
|
{
|
|
struct playlist *playlist = __lib_pl_lookup(name);
|
|
|
|
if (enabled)
|
|
queue_set_flag(&playlist->pl_queue, flag);
|
|
else
|
|
queue_unset_flag(&playlist->pl_queue, flag);
|
|
}
|
|
|
|
static void pl_library_sort(const gchar *name, enum compare_t sort, bool reset)
|
|
{
|
|
struct playlist *playlist = __lib_pl_lookup(name);
|
|
queue_sort(&playlist->pl_queue, sort, reset);
|
|
}
|
|
|
|
|
|
struct playlist_type pl_library = {
|
|
.pl_get_queue = pl_library_get_queue,
|
|
.pl_add_track = pl_library_add_rm,
|
|
.pl_remove_track = pl_library_add_rm,
|
|
.pl_set_flag = pl_library_set_flag,
|
|
.pl_sort = pl_library_sort,
|
|
};
|
|
|
|
|
|
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);
|
|
}
|
|
}
|