119 lines
2.8 KiB
C
119 lines
2.8 KiB
C
/*
|
|
* Copyright 2016 (c) Anna Schumaker.
|
|
*/
|
|
#include <core/idle.h>
|
|
#include <core/playlists/generic.h>
|
|
|
|
static struct playlist_callbacks *callbacks = NULL;
|
|
|
|
|
|
void playlist_generic_set_callbacks(struct playlist_callbacks *cb)
|
|
{
|
|
callbacks = cb;
|
|
}
|
|
|
|
void playlist_generic_init(struct playlist *playlist, unsigned int flags,
|
|
struct queue_ops *ops)
|
|
{
|
|
queue_init(&playlist->pl_queue, flags, 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);
|
|
playlist->pl_private = NULL;
|
|
}
|
|
|
|
void playlist_generic_load(struct playlist *playlist, struct file *file,
|
|
unsigned int flags)
|
|
{
|
|
unsigned int f, n, i, t, it = 0;
|
|
int field, ascending;
|
|
GSList *sort = NULL;
|
|
gchar *line;
|
|
|
|
if (!playlist)
|
|
return;
|
|
|
|
if (flags & PL_SAVE_ITER)
|
|
file_readf(file, "%u", &it);
|
|
|
|
if (flags & PL_SAVE_FLAGS) {
|
|
file_readf(file, "%u %u", &f, &n);
|
|
for (i = 0; i < n; i++) {
|
|
file_readf(file, "%u %d", &field, &ascending);
|
|
field += 1;
|
|
if (!ascending)
|
|
field = -field;
|
|
sort = g_slist_append(sort, GINT_TO_POINTER(field));
|
|
}
|
|
playlist->pl_queue.q_sort = sort;
|
|
queue_resort(&playlist->pl_queue);
|
|
|
|
if (file_readf(file, "%m\n", &line))
|
|
g_free(line);
|
|
}
|
|
|
|
if (flags & PL_SAVE_TRACKS) {
|
|
file_readf(file, "%u ", &n);
|
|
for (i = 0; i < n; i++) {
|
|
file_readf(file, "%u", &t);
|
|
queue_add(&playlist->pl_queue, track_get(t));
|
|
}
|
|
if (file_readf(file, "%m\n", &line))
|
|
g_free(line);
|
|
}
|
|
|
|
playlist->pl_queue.q_flags |= (f & Q_VALID_FLAGS);
|
|
queue_iter_set(&playlist->pl_queue, &playlist->pl_queue.q_cur, it);
|
|
}
|
|
|
|
bool playlist_generic_can_select(struct playlist *playlist)
|
|
{
|
|
return queue_size(&playlist->pl_queue) > 0;
|
|
}
|
|
|
|
void playlist_generic_clear(struct playlist *playlist)
|
|
{
|
|
queue_clear(&playlist->pl_queue);
|
|
}
|
|
|
|
bool playlist_generic_add_track(struct playlist *playlist, struct track *track)
|
|
{
|
|
if (queue_has(&playlist->pl_queue, track))
|
|
return false;
|
|
queue_add(&playlist->pl_queue, track);
|
|
return true;
|
|
}
|
|
|
|
bool playlist_generic_add_track_front(struct playlist *playlist,
|
|
struct track *track)
|
|
{
|
|
if (queue_has(&playlist->pl_queue, track))
|
|
return false;
|
|
queue_add_front(&playlist->pl_queue, track);
|
|
return true;
|
|
}
|
|
|
|
bool playlist_generic_remove_track(struct playlist *playlist, struct track *track)
|
|
{
|
|
return queue_remove_all(&playlist->pl_queue, track);
|
|
}
|
|
|
|
void playlist_generic_set_flag(struct playlist *playlist,
|
|
enum queue_flags flag, bool enabled)
|
|
{
|
|
if (enabled)
|
|
return queue_set_flag(&playlist->pl_queue, flag);
|
|
return queue_unset_flag(&playlist->pl_queue, flag);
|
|
}
|
|
|
|
void playlist_generic_sort(struct playlist *playlist,
|
|
enum compare_t sort, bool reset)
|
|
{
|
|
queue_sort(&playlist->pl_queue, sort, reset);
|
|
}
|
|
|
|
struct track *playlist_generic_next(struct playlist *playlist)
|
|
{
|
|
return queue_next(&playlist->pl_queue);
|
|
}
|