2016-05-24 07:33:40 -04:00
|
|
|
/*
|
|
|
|
* Copyright 2016 (c) Anna Schumaker.
|
|
|
|
*/
|
2016-06-10 11:01:03 -04:00
|
|
|
#include <core/idle.h>
|
2016-09-16 08:00:48 -04:00
|
|
|
#include <core/playlists/generic.h>
|
2016-09-16 10:04:51 -04:00
|
|
|
#include <stdlib.h>
|
2016-05-24 07:33:40 -04:00
|
|
|
|
2016-09-17 09:30:11 -04:00
|
|
|
static struct playlist_callbacks *callbacks = NULL;
|
|
|
|
|
|
|
|
|
2016-09-16 16:43:13 -04:00
|
|
|
static int __playlist_generic_find_sort(gconstpointer a, gconstpointer b)
|
|
|
|
{
|
|
|
|
return abs(GPOINTER_TO_INT(a)) - abs(GPOINTER_TO_INT(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-17 09:30:11 -04:00
|
|
|
void playlist_generic_set_callbacks(struct playlist_callbacks *cb)
|
|
|
|
{
|
|
|
|
callbacks = cb;
|
|
|
|
}
|
2016-05-24 07:33:40 -04:00
|
|
|
|
2016-05-24 11:55:22 -04:00
|
|
|
void playlist_generic_init(struct playlist *playlist, unsigned int flags,
|
|
|
|
struct queue_ops *ops)
|
|
|
|
{
|
2016-09-07 09:34:01 -04:00
|
|
|
queue_init(&playlist->pl_queue, flags, ops, playlist);
|
2016-09-16 16:43:13 -04:00
|
|
|
playlist_generic_sort(playlist, COMPARE_ARTIST);
|
|
|
|
playlist_generic_sort(playlist, COMPARE_YEAR);
|
|
|
|
playlist_generic_sort(playlist, COMPARE_TRACK);
|
2016-08-22 09:15:22 -04:00
|
|
|
playlist->pl_private = NULL;
|
2016-05-24 11:55:22 -04:00
|
|
|
}
|
|
|
|
|
2016-09-16 10:04:51 -04:00
|
|
|
void playlist_generic_save(struct playlist *playlist, struct file *file,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
struct queue_iter it;
|
|
|
|
GSList *sort;
|
|
|
|
int field;
|
|
|
|
|
|
|
|
if (!playlist)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (flags & PL_SAVE_ITER)
|
|
|
|
file_writef(file, "%u ", playlist->pl_queue.q_cur.it_pos);
|
|
|
|
|
|
|
|
if (flags & PL_SAVE_FLAGS) {
|
|
|
|
sort = playlist->pl_queue.q_sort;
|
|
|
|
file_writef(file, "%u ", playlist->pl_queue.q_flags);
|
|
|
|
file_writef(file, "%u", g_slist_length(sort));
|
|
|
|
while (sort) {
|
|
|
|
field = GPOINTER_TO_INT(sort->data);
|
|
|
|
file_writef(file, " %u %d", abs(field) - 1, field > 0);
|
|
|
|
sort = g_slist_next(sort);
|
|
|
|
}
|
|
|
|
file_writef(file, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & PL_SAVE_TRACKS) {
|
|
|
|
file_writef(file, "%u", queue_size(&playlist->pl_queue));
|
|
|
|
queue_for_each(&playlist->pl_queue, &it)
|
|
|
|
file_writef(file, " %u", track_index(queue_iter_val(&it)));
|
|
|
|
file_writef(file, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-16 11:12:41 -04:00
|
|
|
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));
|
|
|
|
}
|
2016-09-16 15:48:42 -04:00
|
|
|
playlist_clear_sort(playlist);
|
2016-09-16 11:12:41 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-08-01 09:41:10 -04:00
|
|
|
bool playlist_generic_can_select(struct playlist *playlist)
|
|
|
|
{
|
|
|
|
return queue_size(&playlist->pl_queue) > 0;
|
|
|
|
}
|
|
|
|
|
2016-07-28 11:56:50 -04:00
|
|
|
void playlist_generic_clear(struct playlist *playlist)
|
|
|
|
{
|
|
|
|
queue_clear(&playlist->pl_queue);
|
|
|
|
}
|
|
|
|
|
2016-05-24 11:02:47 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-09-08 09:08:18 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-24 11:13:44 -04:00
|
|
|
bool playlist_generic_remove_track(struct playlist *playlist, struct track *track)
|
|
|
|
{
|
|
|
|
return queue_remove_all(&playlist->pl_queue, track);
|
|
|
|
}
|
|
|
|
|
2016-05-24 07:48:11 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-09-16 15:48:42 -04:00
|
|
|
void playlist_generic_sort(struct playlist *playlist, enum compare_t sort)
|
2016-05-24 07:33:40 -04:00
|
|
|
{
|
2016-09-16 16:43:13 -04:00
|
|
|
GSList *found = g_slist_find_custom(playlist->pl_queue.q_sort,
|
|
|
|
GINT_TO_POINTER(sort),
|
|
|
|
__playlist_generic_find_sort);
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
found->data = GINT_TO_POINTER(-GPOINTER_TO_INT(found->data));
|
|
|
|
else
|
|
|
|
playlist->pl_queue.q_sort = g_slist_append(playlist->pl_queue.q_sort,
|
|
|
|
GINT_TO_POINTER(sort));
|
|
|
|
|
|
|
|
queue_resort(&playlist->pl_queue);
|
2016-05-24 07:33:40 -04:00
|
|
|
}
|
2016-08-05 08:00:13 -04:00
|
|
|
|
|
|
|
struct track *playlist_generic_next(struct playlist *playlist)
|
|
|
|
{
|
|
|
|
return queue_next(&playlist->pl_queue);
|
|
|
|
}
|