2015-11-12 08:13:53 -05:00
|
|
|
/*
|
2014-11-10 08:27:31 -05:00
|
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
|
|
*/
|
2017-04-11 14:18:25 -04:00
|
|
|
#include <core/string.h>
|
2014-11-10 08:27:31 -05:00
|
|
|
#include <core/tags/library.h>
|
|
|
|
|
2016-09-11 09:42:00 -04:00
|
|
|
#define LIBRARY_DB_MIN 0 /* Ocarina 6.0 */
|
2015-11-04 12:23:00 -05:00
|
|
|
static struct database library_db;
|
2014-11-17 08:30:54 -05:00
|
|
|
|
2016-09-11 09:42:00 -04:00
|
|
|
static struct library *__library_alloc(gchar *path)
|
2014-11-10 08:27:31 -05:00
|
|
|
{
|
2015-11-12 08:13:53 -05:00
|
|
|
struct library *library = g_malloc(sizeof(struct library));
|
2015-11-11 16:53:56 -05:00
|
|
|
|
|
|
|
dbe_init(&library->li_dbe, library);
|
2016-05-06 10:18:28 -04:00
|
|
|
library->li_path = path;
|
|
|
|
library->li_playlist = NULL;
|
2015-11-11 17:09:17 -05:00
|
|
|
|
|
|
|
return library;
|
|
|
|
}
|
|
|
|
|
2016-09-12 15:27:02 -04:00
|
|
|
static struct db_entry *library_alloc(const gchar *path, unsigned int index)
|
2015-11-11 17:09:17 -05:00
|
|
|
{
|
2016-09-11 09:42:00 -04:00
|
|
|
return &__library_alloc(g_strdup(path))->li_dbe;
|
2014-11-10 08:27:31 -05:00
|
|
|
}
|
|
|
|
|
2015-09-30 09:15:15 -04:00
|
|
|
static void library_free(struct db_entry *dbe)
|
|
|
|
{
|
2015-11-12 08:13:53 -05:00
|
|
|
g_free(LIBRARY(dbe));
|
2015-09-30 09:15:15 -04:00
|
|
|
}
|
|
|
|
|
2015-09-23 09:33:55 -04:00
|
|
|
static gchar *library_key(struct db_entry *dbe)
|
2014-11-10 08:27:31 -05:00
|
|
|
{
|
2015-11-11 17:09:17 -05:00
|
|
|
return LIBRARY(dbe)->li_path;
|
2014-11-10 08:27:31 -05:00
|
|
|
}
|
|
|
|
|
2018-02-16 13:30:23 -05:00
|
|
|
static struct db_entry *library_read(struct file *file, unsigned int index)
|
2014-11-10 08:27:31 -05:00
|
|
|
{
|
2015-11-11 17:09:17 -05:00
|
|
|
gchar *path;
|
2015-10-08 11:16:38 -04:00
|
|
|
|
2018-02-21 16:01:15 -05:00
|
|
|
/* Old "enabled" flag */
|
2018-02-20 15:43:56 -05:00
|
|
|
if (file_version(file) == 0)
|
2018-02-21 16:01:15 -05:00
|
|
|
file_readd(file);
|
2016-09-11 09:42:00 -04:00
|
|
|
|
2018-02-21 16:01:15 -05:00
|
|
|
path = file_readl(file);
|
2016-09-11 09:42:00 -04:00
|
|
|
return &__library_alloc(path)->li_dbe;
|
2014-11-10 08:27:31 -05:00
|
|
|
}
|
|
|
|
|
2018-02-16 13:30:23 -05:00
|
|
|
static void library_write(struct file *file, struct db_entry *dbe)
|
2014-11-10 08:27:31 -05:00
|
|
|
{
|
2018-02-21 08:19:19 -05:00
|
|
|
file_writef(file, "%s", LIBRARY(dbe)->li_path);
|
2014-11-13 08:14:29 -05:00
|
|
|
}
|
|
|
|
|
2014-11-17 08:30:54 -05:00
|
|
|
|
2015-11-03 10:39:06 -05:00
|
|
|
static const struct db_ops library_ops = {
|
2016-09-11 10:07:57 -04:00
|
|
|
.dbe_alloc = library_alloc,
|
|
|
|
.dbe_free = library_free,
|
|
|
|
.dbe_key = library_key,
|
|
|
|
.dbe_read = library_read,
|
|
|
|
.dbe_write = library_write,
|
2015-11-03 10:39:06 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-09-28 09:45:50 -04:00
|
|
|
void library_db_init()
|
2014-11-17 08:30:54 -05:00
|
|
|
{
|
2016-08-17 14:17:52 -04:00
|
|
|
db_init(&library_db, "library.db", true, &library_ops, LIBRARY_DB_MIN);
|
2016-08-22 11:17:28 -04:00
|
|
|
db_load(&library_db);
|
2014-11-17 08:30:54 -05:00
|
|
|
}
|
|
|
|
|
2015-09-28 09:45:50 -04:00
|
|
|
void library_db_deinit()
|
|
|
|
{
|
|
|
|
db_deinit(&library_db);
|
|
|
|
}
|
|
|
|
|
2016-08-12 14:00:11 -04:00
|
|
|
bool library_db_defrag()
|
|
|
|
{
|
|
|
|
return db_defrag(&library_db);
|
|
|
|
}
|
|
|
|
|
2015-11-04 12:23:00 -05:00
|
|
|
const struct database *library_db_get()
|
2015-10-23 13:32:20 -04:00
|
|
|
{
|
|
|
|
return &library_db;
|
|
|
|
}
|
|
|
|
|
2015-11-11 17:09:17 -05:00
|
|
|
struct library *library_find(const gchar *path)
|
2014-11-17 08:30:54 -05:00
|
|
|
{
|
2017-04-11 14:18:25 -04:00
|
|
|
struct library *library = library_lookup(path);
|
|
|
|
if (library)
|
|
|
|
return library;
|
|
|
|
return LIBRARY(db_insert(&library_db, path));
|
2014-11-17 08:30:54 -05:00
|
|
|
}
|
|
|
|
|
2016-05-09 10:09:59 -04:00
|
|
|
struct library *library_lookup(const gchar *path)
|
|
|
|
{
|
2017-04-11 14:18:25 -04:00
|
|
|
struct db_entry *dbe, *next;
|
|
|
|
|
|
|
|
db_for_each(dbe, next, &library_db) {
|
|
|
|
if (string_is_subdir(path, LIBRARY(dbe)->li_path))
|
|
|
|
return LIBRARY(dbe);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2016-05-09 10:09:59 -04:00
|
|
|
}
|
|
|
|
|
2015-09-28 10:08:11 -04:00
|
|
|
struct library *library_get(const unsigned int index)
|
2014-11-17 08:30:54 -05:00
|
|
|
{
|
2015-11-04 12:23:00 -05:00
|
|
|
return LIBRARY(db_at(&library_db, index));
|
2014-11-17 08:30:54 -05:00
|
|
|
}
|
|
|
|
|
2015-09-28 09:55:29 -04:00
|
|
|
void library_remove(struct library *library)
|
2014-11-17 08:30:54 -05:00
|
|
|
{
|
|
|
|
if (library)
|
2015-11-11 16:53:56 -05:00
|
|
|
db_remove(&library_db, &library->li_dbe);
|
2014-11-17 08:30:54 -05:00
|
|
|
}
|
2015-10-23 14:19:42 -04:00
|
|
|
|
2015-11-11 17:09:17 -05:00
|
|
|
gchar *library_file(struct library *library, const gchar *path)
|
2015-10-29 02:34:12 -04:00
|
|
|
{
|
2015-11-11 17:09:17 -05:00
|
|
|
return g_strdup_printf("%s/%s", library->li_path, path);
|
2015-10-29 02:34:12 -04:00
|
|
|
}
|
2015-11-03 10:39:06 -05:00
|
|
|
|
|
|
|
#ifdef CONFIG_TESTING
|
|
|
|
const struct db_ops *test_library_ops() { return &library_ops; }
|
|
|
|
#endif /* CONFIG_TESTING */
|