ocarina/core/playlists/system.c

589 lines
15 KiB
C

/*
* Copyright 2016 (c) Anna Schumaker.
*/
#include <core/idle.h>
#include <core/playlists/system.h>
#include <core/string.h>
static struct playlist *pl_system_get_playlist(const gchar *);
static void pl_system_update(const gchar *);
static inline struct queue *__sys_pl_queue(enum sys_playlist_t);
static void __sys_pl_save();
static bool __sys_pl_load();
static struct file sys_file = FILE_INIT("playlist.db", 0);
static struct file sys_deck_f = FILE_INIT("deck", 1);
static struct file sys_collection_f = FILE_INIT("library.q", 0);
static struct file sys_pl_file = FILE_INIT("playlist.system", 0);
static struct sys_playlist *sys_playlists[SYS_PL_NUM_PLAYLISTS];
/*
* Generic system playlist operations.
*/
static bool sys_pl_delete_clear(struct playlist *playlist)
{
playlist_generic_clear(playlist);
__sys_pl_save();
return false;
}
static void sys_pl_generic_init(struct playlist *playlist, unsigned int flags,
struct queue_ops *ops)
{
playlist_generic_init(playlist, Q_REPEAT, ops);
}
static void sys_pl_update_init(struct playlist *playlist, unsigned int flags,
struct queue_ops *ops)
{
sys_pl_generic_init(playlist, flags, ops);
pl_system_update(playlist->pl_name);
}
static void sys_pl_save_partial(struct playlist *playlist, struct file *file)
{
file_writef(file, "%s\n", playlist->pl_name);
queue_save_flags(&playlist->pl_queue, file, true);
}
static void sys_pl_save_full(struct playlist *playlist, struct file *file)
{
sys_pl_save_partial(playlist, file);
queue_save_tracks(&playlist->pl_queue, file);
}
static void sys_pl_load_partial(struct playlist *playlist, struct file *file)
{
queue_load_flags(&playlist->pl_queue, file, true);
}
static void sys_pl_load_full(struct playlist *playlist, struct file *file)
{
sys_pl_load_partial(playlist, file);
queue_load_tracks(&playlist->pl_queue, file);
}
static bool sys_pl_generic_add_front(struct playlist *playlist,
struct track *track)
{
if (queue_has(__sys_pl_queue(SYS_PL_HIDDEN), track))
return false;
return playlist_generic_add_track_front(playlist, track);
}
/*
* Favorite tracks playlist operations.
*/
static struct playlist_ops favorites_ops = {
.pl_add = playlist_generic_add_track,
.pl_delete = sys_pl_delete_clear,
.pl_remove = playlist_generic_remove_track,
.pl_set_flag = playlist_generic_set_flag,
};
static struct sys_playlist sys_favorites = {
.spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Favorites", &favorites_ops),
.spl_init = playlist_generic_init,
.spl_save = sys_pl_save_full,
.spl_load = sys_pl_load_full,
.spl_can_select = playlist_generic_can_select,
.spl_sort = playlist_generic_sort,
.spl_next = playlist_generic_next,
};
/*
* Hidden tracks playlist operations.
*/
static bool sys_pl_hidden_add(struct playlist *playlist, struct track *track)
{
bool ret = playlist_generic_add_track(pl_system_get_playlist("Hidden"), track);
playlist_generic_remove_track(pl_system_get_playlist("Collection"), track);
playlist_generic_remove_track(pl_system_get_playlist("Unplayed"), track);
playlist_generic_remove_track(pl_system_get_playlist("Most Played"), track);
playlist_generic_remove_track(pl_system_get_playlist("Least Played"), track);
return ret;
}
static bool sys_pl_hidden_remove(struct playlist *playlist, struct track *track)
{
bool ret = playlist_generic_remove_track(playlist, track);
struct playlist *add;
if (track->tr_count == 0)
add = pl_system_get_playlist("Unplayed");
else if (track->tr_count > track_db_average_plays())
add = pl_system_get_playlist("Most Played");
else
add = pl_system_get_playlist("Least Played");
playlist_generic_add_track(pl_system_get_playlist("Collection"), track);
playlist_generic_add_track(add, track);
return ret;
}
static bool sys_pl_hidden_clear(struct playlist *playlist)
{
struct track *track;
while (queue_size(&playlist->pl_queue) > 0) {
track = queue_at(&playlist->pl_queue, 0);
sys_pl_hidden_remove(playlist, track);
}
__sys_pl_save();
return false;
}
static struct playlist_ops hidden_ops = {
.pl_add = sys_pl_hidden_add,
.pl_delete = sys_pl_hidden_clear,
.pl_remove = sys_pl_hidden_remove,
.pl_set_flag = playlist_generic_set_flag,
};
static struct sys_playlist sys_hidden = {
.spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Hidden", &hidden_ops),
.spl_init = playlist_generic_init,
.spl_save = sys_pl_save_full,
.spl_load = sys_pl_load_full,
.spl_can_select = playlist_generic_can_select,
.spl_sort = playlist_generic_sort,
.spl_next = playlist_generic_next,
};
/*
* Queued tracks playlist operations.
*/
static bool sys_pl_queued_load()
{
struct playlist *playlist = &sys_playlists[SYS_PL_QUEUED]->spl_playlist;
struct queue *queue = &playlist->pl_queue;
unsigned int num, i, flags = 0;
if (!file_open(&sys_deck_f, OPEN_READ))
return true;
file_readf(&sys_deck_f, "%u", &num);
for (i = 0; i < num; i++) {
file_readf(&sys_deck_f, "%u", &flags);
flags &= Q_UNUSED_MASK;
if (i == 0)
queue->q_flags |= flags;
queue_load_tracks(queue, &sys_deck_f);
}
file_close(&sys_deck_f);
file_remove(&sys_deck_f);
return true;
}
static void sys_pl_queued_init(struct playlist *playlist,
unsigned int flags, struct queue_ops *ops)
{
queue_init(&playlist->pl_queue, 0, ops, playlist);
}
static struct playlist_ops queued_ops = {
.pl_add = playlist_generic_add_track,
.pl_delete = sys_pl_delete_clear,
.pl_remove = playlist_generic_remove_track,
.pl_set_flag = playlist_generic_set_flag,
};
static struct sys_playlist sys_queued = {
.spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Queued Tracks", &queued_ops),
.spl_init = sys_pl_queued_init,
.spl_save = sys_pl_save_full,
.spl_load = sys_pl_load_full,
.spl_can_select = playlist_generic_can_select,
.spl_sort = playlist_generic_sort,
.spl_next = playlist_generic_next,
};
/*
* Collection playlist operations.
*/
static bool sys_pl_collection_load()
{
struct playlist *playlist = &sys_playlists[SYS_PL_COLLECTION]->spl_playlist;
if (file_open(&sys_collection_f, OPEN_READ)) {
queue_load_flags(&playlist->pl_queue, &sys_collection_f, false);
file_close(&sys_collection_f);
file_remove(&sys_collection_f);
}
return true;
}
static bool sys_pl_collection_can_select(struct playlist *playlist)
{
return true;
}
static bool sys_pl_collection_update(struct playlist *playlist,
struct track *track)
{
return sys_pl_generic_add_front(playlist, track) || true;
}
static struct playlist_ops collection_ops = {
.pl_remove = sys_pl_hidden_add,
.pl_set_flag = playlist_generic_set_flag,
};
static struct sys_playlist sys_collection = {
.spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Collection",
&collection_ops),
.spl_init = sys_pl_update_init,
.spl_save = sys_pl_save_partial,
.spl_load = sys_pl_load_partial,
.spl_can_select = sys_pl_collection_can_select,
.spl_update = sys_pl_collection_update,
.spl_sort = playlist_generic_sort,
.spl_next = playlist_generic_next,
};
/*
* History playlist operations.
*/
static void sys_pl_history_init(struct playlist *playlist,
unsigned int flags, struct queue_ops *ops)
{
queue_init(&playlist->pl_queue, Q_REPEAT, ops, playlist);
}
static bool sys_pl_history_add(struct playlist *playlist, struct track *track)
{
queue_add_front(&playlist->pl_queue, track);
queue_iter_set(&playlist->pl_queue, &playlist->pl_queue.q_cur, 0);
return true;
}
static struct playlist_ops history_ops = {
.pl_add = sys_pl_history_add,
};
static struct sys_playlist sys_history = {
.spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "History", &history_ops),
.spl_init = sys_pl_history_init,
.spl_save = sys_pl_save_partial,
.spl_load = sys_pl_load_partial,
.spl_can_select = playlist_noop_can_select,
.spl_sort = playlist_noop_sort,
.spl_next = playlist_generic_next,
};
/*
* Unplayed tracks playlist operations.
*/
static bool sys_pl_unplayed_update(struct playlist *playlist,
struct track *track)
{
if (track->tr_count > 0)
return false;
return sys_pl_generic_add_front(playlist, track) || true;
}
static struct playlist_ops unplayed_ops = {
.pl_set_flag = playlist_generic_set_flag,
};
static struct sys_playlist sys_unplayed = {
.spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Unplayed", &unplayed_ops),
.spl_init = sys_pl_update_init,
.spl_save = sys_pl_save_partial,
.spl_load = sys_pl_load_partial,
.spl_can_select = playlist_generic_can_select,
.spl_update = sys_pl_unplayed_update,
.spl_sort = playlist_generic_sort,
.spl_next = playlist_generic_next,
};
/*
* Most played tracks playlist operations.
*/
static bool sys_pl_most_played_update(struct playlist *playlist,
struct track *track)
{
unsigned int average = track_db_average_plays();
if (track->tr_count <= average)
return false;
return sys_pl_generic_add_front(playlist, track) || true;
}
static struct playlist_ops most_played_ops = {
.pl_set_flag = playlist_generic_set_flag,
};
static struct sys_playlist sys_most_played = {
.spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Most Played",
&most_played_ops),
.spl_init = sys_pl_update_init,
.spl_save = sys_pl_save_partial,
.spl_load = sys_pl_load_partial,
.spl_can_select = playlist_generic_can_select,
.spl_update = sys_pl_most_played_update,
.spl_sort = playlist_generic_sort,
.spl_next = playlist_generic_next,
};
/*
* Least played tracks playlist operations.
*/
static bool sys_pl_least_played_update(struct playlist *playlist,
struct track *track)
{
unsigned int average = track_db_average_plays();
if (!track->tr_count || track->tr_count > average)
return false;
return sys_pl_generic_add_front(playlist, track) || true;
}
static struct playlist_ops least_played_ops = {
.pl_set_flag = playlist_generic_set_flag,
};
static struct sys_playlist sys_least_played = {
.spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Least Played",
&least_played_ops),
.spl_init = sys_pl_update_init,
.spl_save = sys_pl_save_partial,
.spl_load = sys_pl_load_partial,
.spl_can_select = playlist_generic_can_select,
.spl_update = sys_pl_least_played_update,
.spl_sort = playlist_generic_sort,
.spl_next = playlist_generic_next,
};
static struct sys_playlist *sys_playlists[SYS_PL_NUM_PLAYLISTS] = {
[SYS_PL_FAVORITES] = &sys_favorites,
[SYS_PL_HIDDEN] = &sys_hidden,
[SYS_PL_QUEUED] = &sys_queued,
[SYS_PL_COLLECTION] = &sys_collection,
[SYS_PL_HISTORY] = &sys_history,
[SYS_PL_UNPLAYED] = &sys_unplayed,
[SYS_PL_MOST_PLAYED] = &sys_most_played,
[SYS_PL_LEAST_PLAYED] = &sys_least_played,
};
static struct sys_playlist * __sys_pl_lookup(const gchar *name)
{
unsigned int i;
for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) {
if (string_match(name, sys_playlists[i]->spl_playlist.pl_name))
return sys_playlists[i];
}
return NULL;
}
static inline struct queue *__sys_pl_queue(enum sys_playlist_t plist)
{
return &sys_playlists[plist]->spl_playlist.pl_queue;
}
static void __sys_pl_save()
{
struct sys_playlist *sys_pl;
unsigned int i;
if (!file_open(&sys_pl_file, OPEN_WRITE))
return;
file_writef(&sys_pl_file, "%u\n", SYS_PL_NUM_PLAYLISTS);
for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) {
sys_pl = sys_playlists[i];
sys_pl->spl_save(&sys_pl->spl_playlist, &sys_pl_file);
}
file_close(&sys_pl_file);
}
static bool __sys_pl_update_save()
{
__sys_pl_save();
return true;
}
static bool __sys_pl_load_new()
{
struct sys_playlist *sys_pl;
unsigned int i, n;
gchar *name;
if (!file_open(&sys_pl_file, OPEN_READ)) {
__sys_pl_load();
sys_pl_collection_load();
sys_pl_queued_load();
__sys_pl_update_save();
return true;
}
file_readf(&sys_pl_file, "%u\n", &n);
for (i = 0; i < n; i++) {
name = file_readl(&sys_pl_file);
sys_pl = __sys_pl_lookup(name);
if (sys_pl)
sys_pl->spl_load(&sys_pl->spl_playlist, &sys_pl_file);
g_free(name);
}
file_close(&sys_pl_file);
return true;
}
static bool __sys_pl_load()
{
struct sys_playlist *plist;
unsigned int i, n;
gchar *name;
if (!file_open(&sys_file, OPEN_READ))
return true;
file_readf(&sys_file, "%u\n", &n);
for (i = 0; i < n; i++) {
file_readf(&sys_file, "%*u %m[^\n]\n", &name);
if (string_match(name, "Banned")) {
g_free(name);
name = g_strdup("Hidden");
}
plist = __sys_pl_lookup(name);
if (plist)
queue_load_tracks(&plist->spl_playlist.pl_queue, &sys_file);
g_free(name);
}
file_close(&sys_file);
file_remove(&sys_file);
return true;
}
static void pl_system_save(void)
{
__sys_pl_save();
}
static struct playlist *pl_system_get_playlist(const gchar *name)
{
struct sys_playlist *sys_pl = __sys_pl_lookup(name);
return sys_pl ? &sys_pl->spl_playlist : NULL;
}
static unsigned int pl_system_get_id(const gchar *name)
{
unsigned int i;
for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) {
if (string_match(name, sys_playlists[i]->spl_playlist.pl_name))
return i;
}
return SYS_PL_NUM_PLAYLISTS;
}
static gchar *pl_system_get_name(unsigned int id)
{
if (id < SYS_PL_NUM_PLAYLISTS)
return g_strdup(sys_playlists[id]->spl_playlist.pl_name);
return NULL;
}
static bool pl_system_can_select(const gchar *name)
{
struct sys_playlist *sys_pl = __sys_pl_lookup(name);
return sys_pl ? sys_pl->spl_can_select(&sys_pl->spl_playlist) : false;
}
static void pl_system_update(const gchar *name)
{
struct sys_playlist *sys_pl = __sys_pl_lookup(name);
if (sys_pl)
playlist_generic_update(&sys_pl->spl_playlist, sys_pl->spl_update);
}
static void pl_system_sort(const gchar *name, enum compare_t sort, bool reset)
{
struct sys_playlist *sys_pl = __sys_pl_lookup(name);
if (sys_pl) {
sys_pl->spl_sort(&sys_pl->spl_playlist, sort, reset);
__sys_pl_save();
}
}
static struct track *pl_system_next(const gchar *name)
{
struct sys_playlist *sys_pl = __sys_pl_lookup(name);
struct track *track = NULL;;
if (sys_pl) {
track = sys_pl->spl_next(&sys_pl->spl_playlist);
__sys_pl_save();
}
return track;
}
struct playlist_type pl_system = {
.pl_save = pl_system_save,
.pl_get_playlist = pl_system_get_playlist,
.pl_get_id = pl_system_get_id,
.pl_get_name = pl_system_get_name,
.pl_can_select = pl_system_can_select,
.pl_update = pl_system_update,
.pl_sort = pl_system_sort,
.pl_next = pl_system_next,
};
void pl_system_init(struct queue_ops *ops)
{
struct sys_playlist *sys_pl;
unsigned int i;
idle_schedule(IDLE_SYNC, __sys_pl_load_new, NULL);
for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) {
sys_pl = sys_playlists[i];
sys_pl->spl_init(&sys_pl->spl_playlist, Q_REPEAT, ops);
}
}
void pl_system_deinit()
{
unsigned int i;
for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++)
queue_deinit(__sys_pl_queue(i));
}
void pl_system_new_track(struct track *track)
{
playlist_generic_add_track(pl_system_get_playlist("Collection"), track);
playlist_generic_add_track(pl_system_get_playlist("Unplayed"), track);
}
void pl_system_delete_track(struct track *track)
{
struct sys_playlist *sys_pl;
unsigned int i;
for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) {
sys_pl = sys_playlists[i];
playlist_generic_remove_track(&sys_pl->spl_playlist, track);
}
}