2016-08-14 10:20:15 -04:00
|
|
|
/*
|
|
|
|
* Copyright 2016 (c) Anna Schumaker.
|
|
|
|
*/
|
|
|
|
#include <core/playlists/user.h>
|
|
|
|
|
|
|
|
static struct queue_ops *user_pl_ops = NULL;
|
|
|
|
static struct database user_db;
|
|
|
|
|
|
|
|
static struct user_playlist *__user_db_alloc(gchar *name)
|
|
|
|
{
|
|
|
|
struct user_playlist *playlist = g_malloc(sizeof(struct user_playlist));
|
|
|
|
|
|
|
|
dbe_init(&playlist->pl_dbe, playlist);
|
|
|
|
playlist->pl_playlist.pl_name = name;
|
|
|
|
playlist->pl_playlist.pl_type = PL_USER;
|
|
|
|
playlist_generic_init(&playlist->pl_playlist, Q_REPEAT, user_pl_ops);
|
|
|
|
|
|
|
|
return playlist;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct db_entry *user_db_alloc(const gchar *name)
|
|
|
|
{
|
|
|
|
return &__user_db_alloc(g_strdup(name))->pl_dbe;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void user_db_free(struct db_entry *dbe)
|
|
|
|
{
|
|
|
|
queue_deinit(&USER_PLAYLIST(dbe)->pl_playlist.pl_queue);
|
|
|
|
g_free(USER_PLAYLIST(dbe));
|
|
|
|
}
|
|
|
|
|
|
|
|
static gchar *user_db_key(struct db_entry *dbe)
|
|
|
|
{
|
|
|
|
return g_strdup(USER_PLAYLIST(dbe)->pl_playlist.pl_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct db_entry *user_db_read(struct file *file)
|
|
|
|
{
|
|
|
|
gchar *name = file_readl(file);
|
|
|
|
struct user_playlist *playlist = __user_db_alloc(name);
|
|
|
|
|
|
|
|
queue_load_flags(&playlist->pl_playlist.pl_queue, file, true);
|
|
|
|
queue_load_tracks(&playlist->pl_playlist.pl_queue, file);
|
|
|
|
queue_iter_set(&playlist->pl_playlist.pl_queue,
|
|
|
|
&playlist->pl_playlist.pl_queue.q_cur,
|
|
|
|
playlist->pl_playlist.pl_queue.q_cur.it_pos);
|
|
|
|
|
|
|
|
return &playlist->pl_dbe;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void user_db_write(struct file *file, struct db_entry *dbe)
|
|
|
|
{
|
|
|
|
struct playlist *playlist = &USER_PLAYLIST(dbe)->pl_playlist;
|
|
|
|
|
|
|
|
file_writef(file, "%s\n", playlist->pl_name);
|
|
|
|
queue_save_flags(&playlist->pl_queue, file, true);
|
|
|
|
queue_save_tracks(&playlist->pl_queue, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const struct db_ops user_db_ops = {
|
|
|
|
.dbe_alloc = user_db_alloc,
|
|
|
|
.dbe_free = user_db_free,
|
|
|
|
.dbe_key = user_db_key,
|
|
|
|
.dbe_read = user_db_read,
|
|
|
|
.dbe_write = user_db_write,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static struct playlist *__user_pl_lookup(const gchar *name)
|
|
|
|
{
|
|
|
|
struct db_entry *dbe = db_get(&user_db, name);
|
|
|
|
return dbe ? &USER_PLAYLIST(dbe)->pl_playlist : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pl_user_save(void)
|
|
|
|
{
|
|
|
|
db_save(&user_db);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct queue *pl_user_get_queue(const gchar *name)
|
|
|
|
{
|
|
|
|
struct playlist *playlist = __user_pl_lookup(name);
|
|
|
|
return playlist ? &playlist->pl_queue : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int pl_user_get_id(const gchar *name)
|
|
|
|
{
|
|
|
|
struct db_entry *dbe = db_get(&user_db, name);
|
|
|
|
return dbe ? dbe->dbe_index : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gchar *pl_user_get_name(unsigned int id)
|
|
|
|
{
|
|
|
|
struct db_entry *dbe = db_at(&user_db, id);
|
|
|
|
return dbe ? g_strdup(USER_PLAYLIST(dbe)->pl_playlist.pl_name) : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool pl_user_can_select(const gchar *name)
|
|
|
|
{
|
|
|
|
return db_get(&user_db, name) != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool pl_user_new(const gchar *name)
|
|
|
|
{
|
|
|
|
if (db_get(&user_db, name))
|
|
|
|
return false;
|
|
|
|
db_insert(&user_db, name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool pl_user_delete(const gchar *name)
|
|
|
|
{
|
|
|
|
struct db_entry *dbe = db_get(&user_db, name);
|
|
|
|
if (dbe) {
|
|
|
|
db_remove(&user_db, dbe);
|
|
|
|
db_defrag(&user_db);
|
|
|
|
}
|
|
|
|
return dbe != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool pl_user_add(const gchar *name, struct track *track)
|
|
|
|
{
|
|
|
|
struct playlist *playlist = __user_pl_lookup(name);
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
if (playlist) {
|
|
|
|
ret = playlist_generic_add_track(playlist, track);
|
|
|
|
if (ret)
|
|
|
|
pl_user_save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool pl_user_remove(const gchar *name, struct track *track)
|
|
|
|
{
|
|
|
|
struct playlist *playlist = __user_pl_lookup(name);
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
if (playlist) {
|
|
|
|
ret = playlist_generic_remove_track(playlist, track);
|
|
|
|
if (ret)
|
|
|
|
pl_user_save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pl_user_update(const gchar *name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pl_user_set_flag(const gchar *name, enum queue_flags flag,
|
|
|
|
bool enabled)
|
|
|
|
{
|
|
|
|
struct playlist *playlist = __user_pl_lookup(name);
|
|
|
|
playlist_generic_set_flag(playlist, flag, enabled);
|
|
|
|
pl_user_save();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pl_user_sort(const gchar *name, enum compare_t sort, bool reset)
|
|
|
|
{
|
|
|
|
struct playlist *playlist = __user_pl_lookup(name);
|
|
|
|
playlist_generic_sort(playlist, sort, reset);
|
|
|
|
pl_user_save();
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct track *pl_user_next(const gchar *name)
|
|
|
|
{
|
|
|
|
struct playlist *playlist = __user_pl_lookup(name);
|
|
|
|
struct track *track = playlist_generic_next(playlist);
|
|
|
|
pl_user_save();
|
|
|
|
return track;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct playlist_type pl_user = {
|
|
|
|
.pl_save = pl_user_save,
|
|
|
|
.pl_get_queue = pl_user_get_queue,
|
|
|
|
.pl_get_id = pl_user_get_id,
|
|
|
|
.pl_get_name = pl_user_get_name,
|
|
|
|
.pl_can_select = pl_user_can_select,
|
|
|
|
.pl_new = pl_user_new,
|
|
|
|
.pl_delete = pl_user_delete,
|
|
|
|
.pl_add_track = pl_user_add,
|
|
|
|
.pl_remove_track = pl_user_remove,
|
|
|
|
.pl_update = pl_user_update,
|
|
|
|
.pl_set_flag = pl_user_set_flag,
|
|
|
|
.pl_sort = pl_user_sort,
|
|
|
|
.pl_next = pl_user_next,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void pl_user_init(struct queue_ops *ops)
|
|
|
|
{
|
|
|
|
user_pl_ops = ops;
|
2016-08-17 14:17:52 -04:00
|
|
|
db_init(&user_db, "playlist.user", true, &user_db_ops, 0);
|
2016-08-22 11:17:28 -04:00
|
|
|
db_load(&user_db);
|
2016-08-14 10:20:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void pl_user_deinit()
|
|
|
|
{
|
|
|
|
db_deinit(&user_db);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct database *pl_user_db_get()
|
|
|
|
{
|
|
|
|
return &user_db;
|
|
|
|
}
|