2014-09-13 10:26:17 -04:00
|
|
|
/**
|
2013-12-22 22:21:19 -05:00
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
|
|
*/
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/deck.h>
|
|
|
|
#include <core/file.h>
|
|
|
|
#include <core/library.h>
|
2013-12-22 22:21:19 -05:00
|
|
|
|
|
|
|
|
2014-05-26 22:20:07 -04:00
|
|
|
class RecentQueue : public Queue
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RecentQueue() : Queue(Q_ENABLED | Q_REPEAT | Q_NO_SORT) {}
|
|
|
|
|
|
|
|
unsigned int add(Track *track)
|
|
|
|
{
|
|
|
|
del(track);
|
|
|
|
_cur = 0;
|
|
|
|
return _add_at(track, 0);
|
|
|
|
}
|
|
|
|
};
|
2014-01-16 22:25:04 -05:00
|
|
|
|
2014-05-26 22:20:07 -04:00
|
|
|
|
2014-05-31 09:09:44 -04:00
|
|
|
static std::list<TempQueue> queue_deck;
|
2014-05-26 22:20:07 -04:00
|
|
|
static RecentQueue recent_queue;
|
2015-09-10 08:00:42 -04:00
|
|
|
static file deck_file("deck", 1);
|
2014-05-26 22:20:07 -04:00
|
|
|
|
|
|
|
|
2014-05-31 09:09:44 -04:00
|
|
|
TempQueue :: TempQueue() {}
|
|
|
|
TempQueue :: TempQueue(bool random)
|
2015-04-09 09:19:32 -04:00
|
|
|
: Queue(Q_ENABLED | (random ? Q_RANDOM : 0)) {}
|
2014-05-31 09:09:44 -04:00
|
|
|
|
|
|
|
void TempQueue :: set_flag(queue_flags flag)
|
|
|
|
{
|
|
|
|
Queue :: set_flag(flag);
|
|
|
|
deck :: write();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TempQueue :: unset_flag(queue_flags flag)
|
|
|
|
{
|
|
|
|
Queue :: unset_flag(flag);
|
|
|
|
deck :: write();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int TempQueue :: add(Track *track)
|
|
|
|
{
|
|
|
|
unsigned int res = Queue :: add(track);
|
|
|
|
deck :: write();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TempQueue :: del(Track *track)
|
|
|
|
{
|
|
|
|
Queue :: del(track);
|
|
|
|
deck :: write();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TempQueue :: del(unsigned int id)
|
|
|
|
{
|
|
|
|
Queue :: del(id);
|
|
|
|
deck :: write();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TempQueue :: sort(sort_t field, bool ascending)
|
|
|
|
{
|
|
|
|
Queue :: sort(field, ascending);
|
|
|
|
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;
|
|
|
|
Queue *library = library :: get_queue();
|
|
|
|
|
|
|
|
deck_file >> random >> num;
|
|
|
|
if (random)
|
|
|
|
library->set_flag(Q_RANDOM);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < num; i++) {
|
|
|
|
deck_file >> field >> ascending;
|
|
|
|
library->sort((sort_t)field, (i == 0) ? true : false);
|
|
|
|
if (!ascending)
|
|
|
|
library->sort((sort_t)field, false);
|
|
|
|
}
|
2014-01-16 22:25:04 -05:00
|
|
|
}
|
2013-12-22 22:21:19 -05:00
|
|
|
|
2014-05-26 22:20:07 -04:00
|
|
|
void deck :: init()
|
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
|
|
|
|
2014-05-26 22:20:07 -04:00
|
|
|
if (!deck_file.open(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
|
|
|
|
2014-01-26 14:44:56 -05:00
|
|
|
deck_file >> num;
|
2014-05-26 22:20:07 -04:00
|
|
|
queue_deck.resize(num);
|
2013-12-23 10:37:22 -05:00
|
|
|
|
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->read(deck_file);
|
|
|
|
deck_file.close();
|
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
|
|
|
|
2014-05-26 22:20:07 -04:00
|
|
|
if (!deck_file.open(OPEN_WRITE))
|
|
|
|
return;
|
2014-01-26 14:44:56 -05:00
|
|
|
|
2014-05-26 22:20:07 -04:00
|
|
|
deck_file << queue_deck.size() << std :: endl;
|
|
|
|
for (it = queue_deck.begin(); it != queue_deck.end(); it++) {
|
2014-01-24 21:43:18 -05:00
|
|
|
it->write(deck_file);
|
|
|
|
deck_file << std::endl;
|
2013-12-23 10:37:22 -05:00
|
|
|
}
|
2014-05-26 22:20:07 -04:00
|
|
|
|
2014-01-24 21:43:18 -05:00
|
|
|
deck_file.close();
|
2013-12-23 10:37:22 -05:00
|
|
|
}
|
|
|
|
|
2014-04-01 20:28:19 -04:00
|
|
|
Queue *deck :: create(bool random)
|
2013-12-22 22:21:19 -05:00
|
|
|
{
|
2014-05-31 09:09:44 -04:00
|
|
|
queue_deck.push_back(TempQueue(random));
|
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
|
|
|
}
|
|
|
|
|
2014-05-26 22:20:07 -04: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
|
|
|
}
|
|
|
|
|
2014-12-12 08:25:31 -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
|
|
|
}
|
|
|
|
|
2014-05-26 22:20:07 -04: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
|
|
|
}
|
|
|
|
|
2014-06-05 11:04:55 -04:00
|
|
|
Queue *deck :: get(unsigned int index)
|
|
|
|
{
|
|
|
|
std::list<TempQueue>::iterator it;
|
|
|
|
|
|
|
|
for (it = queue_deck.begin(); it != queue_deck.end(); it++) {
|
|
|
|
if (index == 0)
|
|
|
|
return &(*it);
|
|
|
|
index--;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-04-13 12:26:32 -04:00
|
|
|
Track *deck :: next()
|
2013-12-23 10:22:37 -05:00
|
|
|
{
|
2014-05-26 22:20:07 -04:00
|
|
|
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++) {
|
|
|
|
if (it->has_flag(Q_ENABLED) == false)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
track = it->next();
|
|
|
|
if (it->size() == 0)
|
|
|
|
_destroy(it);
|
|
|
|
break;
|
2013-12-23 10:22:37 -05:00
|
|
|
}
|
|
|
|
|
2014-05-26 22:20:07 -04:00
|
|
|
if (!track)
|
|
|
|
track = library :: get_queue()->next();
|
2014-06-01 09:38:12 -04:00
|
|
|
if (track)
|
|
|
|
recent_queue.add(track);
|
2014-05-26 22:20:07 -04:00
|
|
|
return track;
|
|
|
|
}
|
2014-01-29 21:06:24 -05:00
|
|
|
|
2014-05-26 22:20:07 -04:00
|
|
|
Track *deck :: prev()
|
2013-12-23 10:37:22 -05:00
|
|
|
{
|
2014-05-26 22:20:07 -04:00
|
|
|
return recent_queue.next();
|
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
|
|
|
|
2014-05-26 22:20:07 -04:00
|
|
|
Queue *deck :: get_queue()
|
|
|
|
{
|
|
|
|
return &recent_queue;
|
2013-12-22 22:21:19 -05:00
|
|
|
}
|