2014-09-13 10:26:17 -04:00
|
|
|
/**
|
2013-12-22 22:21:19 -05:00
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
|
|
*/
|
2015-12-07 09:35:58 -05:00
|
|
|
extern "C" {
|
|
|
|
#include <core/collection.h>
|
|
|
|
}
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/deck.h>
|
|
|
|
#include <core/file.h>
|
2013-12-22 22:21:19 -05:00
|
|
|
|
2014-05-31 09:09:44 -04:00
|
|
|
static std::list<TempQueue> queue_deck;
|
2015-11-25 09:04:10 -05:00
|
|
|
static struct queue recent_queue;
|
2015-09-10 10:33:24 -04:00
|
|
|
static struct file deck_file;
|
2014-05-26 22:20:07 -04:00
|
|
|
|
|
|
|
|
2015-11-23 08:36:29 -05:00
|
|
|
TempQueue :: TempQueue()
|
|
|
|
{
|
|
|
|
queue_init(this, 0, NULL);
|
|
|
|
}
|
|
|
|
|
2015-11-24 09:08:20 -05:00
|
|
|
TempQueue :: TempQueue(bool random, struct queue_ops *ops)
|
2015-11-23 08:36:29 -05:00
|
|
|
{
|
2015-11-24 09:08:20 -05:00
|
|
|
unsigned int flags = Q_ENABLED | Q_SAVE_FLAGS | Q_SAVE_SORT;
|
|
|
|
queue_init(this, flags | (random ? Q_RANDOM : 0), ops);
|
2015-11-23 08:36:29 -05:00
|
|
|
}
|
|
|
|
|
2014-05-31 09:09:44 -04:00
|
|
|
|
2015-11-29 17:34:21 -05:00
|
|
|
void TempQueue :: write(file &file)
|
|
|
|
{
|
2015-12-01 11:17:25 -05:00
|
|
|
file_writef(&file, "%u %zu", q_flags, queue_size(this));
|
|
|
|
for (unsigned int i = 0; i < queue_size(this); i++)
|
|
|
|
file_writef(&file, " %u", queue_at(this, i)->tr_dbe.dbe_index);
|
2015-11-29 17:34:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void TempQueue :: read(file &file)
|
|
|
|
{
|
|
|
|
unsigned int n, id;
|
|
|
|
file_readf(&file, "%u %u", &q_flags, &n);
|
|
|
|
for (unsigned int i = 0; i < n; i++) {
|
|
|
|
file_readf(&file, "%u", &id);
|
2015-12-01 11:17:25 -05:00
|
|
|
queue_add(this, track_get(id));
|
2015-11-29 17:34:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
unsigned int TempQueue :: add(struct track *track)
|
2014-05-31 09:09:44 -04:00
|
|
|
{
|
2015-11-25 08:24:04 -05:00
|
|
|
unsigned int res = queue_add(this, track);
|
2014-05-31 09:09:44 -04:00
|
|
|
deck :: write();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
void TempQueue :: del(struct track *track)
|
2014-05-31 09:09:44 -04:00
|
|
|
{
|
2015-11-29 16:34:51 -05:00
|
|
|
queue_remove_all(this, track);
|
2014-05-31 09:09:44 -04:00
|
|
|
deck :: write();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TempQueue :: del(unsigned int id)
|
|
|
|
{
|
2015-11-28 15:24:03 -05:00
|
|
|
queue_remove(this, id);
|
2014-05-31 09:09:44 -04:00
|
|
|
deck :: write();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-26 22:20:07 -04:00
|
|
|
static void upgrade_v0()
|
2014-01-16 22:25:04 -05:00
|
|
|
{
|
2014-05-26 22:20:07 -04:00
|
|
|
int random, ascending;
|
|
|
|
unsigned int num, field;
|
2015-12-05 12:12:01 -05:00
|
|
|
queue *library = collection_get_queue();
|
2014-05-26 22:20:07 -04:00
|
|
|
|
2015-10-08 11:16:38 -04:00
|
|
|
file_readf(&deck_file, "%d %u", &random, &num);
|
2014-05-26 22:20:07 -04:00
|
|
|
if (random)
|
2015-12-03 13:41:39 -05:00
|
|
|
queue_set_flag(library, Q_RANDOM);
|
2014-05-26 22:20:07 -04:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < num; i++) {
|
2015-10-08 11:16:38 -04:00
|
|
|
file_readf(&deck_file, "%u %d", &field, &ascending);
|
2015-12-03 13:43:15 -05:00
|
|
|
queue_sort(library, (compare_t)field, (i == 0) ? true : false);
|
2014-05-26 22:20:07 -04:00
|
|
|
if (!ascending)
|
2015-12-03 13:43:15 -05:00
|
|
|
queue_sort(library, (compare_t)field, false);
|
2014-05-26 22:20:07 -04:00
|
|
|
}
|
2014-01-16 22:25:04 -05:00
|
|
|
}
|
2013-12-22 22:21:19 -05:00
|
|
|
|
2015-11-29 19:45:39 -05:00
|
|
|
void deck :: init(struct queue_ops *history_ops, struct queue_ops *temp_ops)
|
2013-12-23 10:37:22 -05:00
|
|
|
{
|
|
|
|
unsigned int num;
|
2014-05-26 22:20:07 -04:00
|
|
|
bool upgraded = false;
|
2014-05-31 09:09:44 -04:00
|
|
|
std::list<TempQueue>::iterator it;
|
2013-12-23 10:37:22 -05:00
|
|
|
|
2015-11-29 19:45:39 -05:00
|
|
|
queue_init(&recent_queue, Q_ENABLED | Q_REPEAT | Q_NO_SORT | Q_ADD_FRONT, history_ops);
|
2015-09-10 10:33:24 -04:00
|
|
|
file_init(&deck_file, "deck", 1);
|
2015-11-23 08:36:29 -05:00
|
|
|
|
2015-09-10 09:46:33 -04:00
|
|
|
if (!file_open(&deck_file, OPEN_READ))
|
2014-01-24 21:43:18 -05:00
|
|
|
return;
|
|
|
|
|
2015-09-10 08:10:38 -04:00
|
|
|
if (file_version(&deck_file) == 0) {
|
2014-05-26 22:20:07 -04:00
|
|
|
upgrade_v0();
|
|
|
|
upgraded = true;
|
2014-01-26 14:44:56 -05:00
|
|
|
}
|
2014-01-16 22:25:04 -05:00
|
|
|
|
2015-10-08 11:16:38 -04:00
|
|
|
file_readf(&deck_file, "%u", &num);
|
2014-05-26 22:20:07 -04:00
|
|
|
queue_deck.resize(num);
|
2013-12-23 10:37:22 -05:00
|
|
|
|
2015-11-24 09:08:20 -05:00
|
|
|
for (it = queue_deck.begin(); it != queue_deck.end(); it++) {
|
2014-01-24 21:43:18 -05:00
|
|
|
it->read(deck_file);
|
2015-11-29 19:45:39 -05:00
|
|
|
it->q_ops = temp_ops;
|
2015-11-24 09:08:20 -05:00
|
|
|
}
|
2015-09-10 09:12:48 -04:00
|
|
|
file_close(&deck_file);
|
2014-05-26 22:20:07 -04:00
|
|
|
|
|
|
|
if (upgraded)
|
|
|
|
deck :: write();
|
2013-12-23 10:37:22 -05:00
|
|
|
}
|
|
|
|
|
2014-01-24 21:43:18 -05:00
|
|
|
void deck :: write()
|
2013-12-23 10:37:22 -05:00
|
|
|
{
|
2014-05-31 09:09:44 -04:00
|
|
|
std::list<TempQueue>::iterator it;
|
2013-12-23 10:37:22 -05:00
|
|
|
|
2015-09-10 09:46:33 -04:00
|
|
|
if (!file_open(&deck_file, OPEN_WRITE))
|
2014-05-26 22:20:07 -04:00
|
|
|
return;
|
2014-01-26 14:44:56 -05:00
|
|
|
|
2015-10-08 09:41:51 -04:00
|
|
|
file_writef(&deck_file, "%zu\n", queue_deck.size());
|
2014-05-26 22:20:07 -04:00
|
|
|
for (it = queue_deck.begin(); it != queue_deck.end(); it++) {
|
2014-01-24 21:43:18 -05:00
|
|
|
it->write(deck_file);
|
2015-10-08 09:41:51 -04:00
|
|
|
file_writef(&deck_file, "\n");
|
2013-12-23 10:37:22 -05:00
|
|
|
}
|
2014-05-26 22:20:07 -04:00
|
|
|
|
2015-09-10 09:12:48 -04:00
|
|
|
file_close(&deck_file);
|
2013-12-23 10:37:22 -05:00
|
|
|
}
|
|
|
|
|
2015-11-24 09:08:20 -05:00
|
|
|
void deck :: save(struct queue *queue, enum queue_flags flag)
|
|
|
|
{
|
|
|
|
deck :: write();
|
|
|
|
}
|
|
|
|
|
|
|
|
queue *deck :: create(bool random, struct queue_ops *ops)
|
2013-12-22 22:21:19 -05:00
|
|
|
{
|
2015-11-24 09:08:20 -05:00
|
|
|
queue_deck.push_back(TempQueue(random, ops));
|
2014-05-26 22:20:07 -04:00
|
|
|
return &queue_deck.back();
|
2013-12-22 22:21:19 -05:00
|
|
|
}
|
|
|
|
|
2014-05-31 09:09:44 -04:00
|
|
|
static void _destroy(std::list<TempQueue>::iterator &it)
|
2013-12-22 22:21:19 -05:00
|
|
|
{
|
2014-05-26 22:20:07 -04:00
|
|
|
queue_deck.erase(it);
|
|
|
|
deck :: write();
|
2013-12-22 22:21:19 -05:00
|
|
|
}
|
|
|
|
|
2015-11-16 09:12:15 -05:00
|
|
|
void deck :: destroy(queue *queue)
|
2013-12-22 22:21:19 -05:00
|
|
|
{
|
2014-05-31 09:09:44 -04:00
|
|
|
std::list<TempQueue>::iterator it;
|
2013-12-22 22:21:19 -05:00
|
|
|
|
2014-05-26 22:20:07 -04:00
|
|
|
for (it = queue_deck.begin(); it != queue_deck.end(); it++) {
|
|
|
|
if (&(*it) == queue) {
|
|
|
|
_destroy(it);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-01-31 22:45:26 -05:00
|
|
|
}
|
|
|
|
|
2015-11-16 09:12:15 -05:00
|
|
|
void deck :: move(queue *queue, unsigned int index)
|
2013-12-22 22:21:19 -05:00
|
|
|
{
|
2014-05-26 22:20:07 -04:00
|
|
|
unsigned int old_pos = deck :: index(queue);
|
2014-05-31 09:09:44 -04:00
|
|
|
std::list<TempQueue>::iterator it_old = queue_deck.begin();
|
|
|
|
std::list<TempQueue>::iterator it_new = queue_deck.begin();
|
2013-12-22 22:21:19 -05:00
|
|
|
|
2014-05-26 22:20:07 -04:00
|
|
|
for (unsigned int i = 0; i < queue_deck.size(); i++) {
|
2013-12-22 22:21:19 -05:00
|
|
|
if (i < old_pos)
|
|
|
|
it_old++;
|
2014-05-26 22:20:07 -04:00
|
|
|
|
2014-12-12 08:25:31 -05:00
|
|
|
if (i < index)
|
2013-12-22 22:21:19 -05:00
|
|
|
it_new++;
|
2014-01-31 22:45:26 -05:00
|
|
|
}
|
|
|
|
|
2014-12-12 08:25:31 -05:00
|
|
|
if (index > old_pos)
|
2014-01-31 22:45:26 -05:00
|
|
|
it_new++;
|
|
|
|
|
2014-05-26 22:20:07 -04:00
|
|
|
queue_deck.splice(it_new, queue_deck, it_old);
|
2014-05-31 09:09:44 -04:00
|
|
|
write();
|
2014-01-31 22:45:26 -05:00
|
|
|
}
|
|
|
|
|
2015-11-16 09:12:15 -05:00
|
|
|
unsigned int deck :: index(queue *queue)
|
2014-01-31 22:45:26 -05:00
|
|
|
{
|
2014-05-26 22:20:07 -04:00
|
|
|
unsigned int i = 0;
|
2014-05-31 09:09:44 -04:00
|
|
|
std::list<TempQueue>::iterator it;
|
2014-01-31 22:45:26 -05:00
|
|
|
|
2014-05-26 22:20:07 -04:00
|
|
|
for (it = queue_deck.begin(); it != queue_deck.end(); it++) {
|
|
|
|
if (&(*it) == queue)
|
|
|
|
return i;
|
|
|
|
i++;
|
2013-12-22 22:21:19 -05:00
|
|
|
}
|
|
|
|
|
2014-05-26 22:20:07 -04:00
|
|
|
return queue_deck.size();
|
2013-12-22 22:21:19 -05:00
|
|
|
}
|
|
|
|
|
2015-11-16 09:12:15 -05:00
|
|
|
queue *deck :: get(unsigned int index)
|
2014-06-05 11:04:55 -04:00
|
|
|
{
|
|
|
|
std::list<TempQueue>::iterator it;
|
|
|
|
|
|
|
|
for (it = queue_deck.begin(); it != queue_deck.end(); it++) {
|
|
|
|
if (index == 0)
|
|
|
|
return &(*it);
|
|
|
|
index--;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
struct track *deck :: next()
|
2013-12-23 10:22:37 -05:00
|
|
|
{
|
2015-10-24 19:34:45 -04:00
|
|
|
struct track *track = NULL;
|
2014-05-31 09:09:44 -04:00
|
|
|
std::list<TempQueue>::iterator it;
|
2013-12-23 10:22:37 -05:00
|
|
|
|
2014-05-26 22:20:07 -04:00
|
|
|
for (it = queue_deck.begin(); it != queue_deck.end(); it++) {
|
2015-11-20 08:18:54 -05:00
|
|
|
if (queue_has_flag(&(*it), Q_ENABLED) == false)
|
2014-05-26 22:20:07 -04:00
|
|
|
continue;
|
|
|
|
|
2015-11-19 08:25:53 -05:00
|
|
|
track = queue_next(&(*it));
|
2015-11-19 13:48:59 -05:00
|
|
|
if (queue_size(&(*it)) == 0)
|
2014-05-26 22:20:07 -04:00
|
|
|
_destroy(it);
|
|
|
|
break;
|
2013-12-23 10:22:37 -05:00
|
|
|
}
|
|
|
|
|
2014-05-26 22:20:07 -04:00
|
|
|
if (!track)
|
2015-12-05 12:12:01 -05:00
|
|
|
track = queue_next(collection_get_queue());
|
2015-11-25 09:04:10 -05:00
|
|
|
if (track) {
|
2015-11-29 16:34:51 -05:00
|
|
|
queue_remove_all(&recent_queue, track);
|
2015-11-25 09:04:10 -05:00
|
|
|
queue_add(&recent_queue, track);
|
2015-12-01 15:44:18 -05:00
|
|
|
_q_iter_set(&recent_queue.q_tracks, &recent_queue.q_cur, 0);
|
2015-11-25 09:04:10 -05:00
|
|
|
}
|
2014-05-26 22:20:07 -04:00
|
|
|
return track;
|
|
|
|
}
|
2014-01-29 21:06:24 -05:00
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
struct track *deck :: prev()
|
2013-12-23 10:37:22 -05:00
|
|
|
{
|
2015-11-19 08:25:53 -05:00
|
|
|
return queue_next(&recent_queue);
|
2013-12-23 10:37:22 -05:00
|
|
|
}
|
|
|
|
|
2014-05-31 09:09:44 -04:00
|
|
|
std::list<TempQueue> &deck :: get_queues()
|
2013-12-22 22:21:19 -05:00
|
|
|
{
|
2014-05-26 22:20:07 -04:00
|
|
|
return queue_deck;
|
|
|
|
}
|
2013-12-22 22:21:19 -05:00
|
|
|
|
2015-11-16 09:12:15 -05:00
|
|
|
queue *deck :: get_queue()
|
2014-05-26 22:20:07 -04:00
|
|
|
{
|
|
|
|
return &recent_queue;
|
2013-12-22 22:21:19 -05:00
|
|
|
}
|