ocarina/core/tags/library.c

109 lines
2.0 KiB
C

/*
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/tags/library.h>
static struct database library_db;
static struct library *__library_alloc(gchar *path, bool enabled)
{
struct library *library = g_malloc(sizeof(struct library));
dbe_init(&library->li_dbe, library);
library->li_size = 0;
library->li_enabled = enabled;
library->li_path = path;
library->li_playlist = NULL;
return library;
}
static struct db_entry *library_alloc(const gchar *path)
{
return &__library_alloc(g_strdup(path), true)->li_dbe;
}
static void library_free(struct db_entry *dbe)
{
g_free(LIBRARY(dbe));
}
static gchar *library_key(struct db_entry *dbe)
{
return LIBRARY(dbe)->li_path;
}
static struct db_entry *library_read(struct file *file)
{
int enabled;
gchar *path;
file_readf(file, "%d %m[^\n]", &enabled, &path);
return &__library_alloc(path, enabled)->li_dbe;
}
static void library_write(struct file *file, struct db_entry *dbe)
{
file_writef(file, "%d %s", LIBRARY(dbe)->li_enabled,
LIBRARY(dbe)->li_path);
}
static const struct db_ops library_ops = {
library_alloc,
library_free,
library_key,
library_read,
NULL,
library_write,
};
void library_db_init()
{
db_init(&library_db, "library.db", true, &library_ops);
db_load_idle(&library_db);
}
void library_db_deinit()
{
db_deinit(&library_db);
}
const struct database *library_db_get()
{
return &library_db;
}
struct library *library_find(const gchar *path)
{
return LIBRARY(db_find(&library_db, path));
}
struct library *library_get(const unsigned int index)
{
return LIBRARY(db_at(&library_db, index));
}
void library_remove(struct library *library)
{
if (library)
db_remove(&library_db, &library->li_dbe);
}
void library_set_enabled(struct library *library, bool enabled)
{
library->li_enabled = enabled;
db_save(&library_db);
}
gchar *library_file(struct library *library, const gchar *path)
{
return g_strdup_printf("%s/%s", library->li_path, path);
}
#ifdef CONFIG_TESTING
const struct db_ops *test_library_ops() { return &library_ops; }
#endif /* CONFIG_TESTING */