6a227df41b
Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
178 lines
3.8 KiB
C++
178 lines
3.8 KiB
C++
/*
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
*/
|
|
#include <callback.h>
|
|
#include <deck.h>
|
|
#include <error.h>
|
|
#include <file.h>
|
|
#include <print.h>
|
|
|
|
#include <list>
|
|
|
|
static std::list<Playqueue> playqueue_deck;
|
|
static Playqueue library_playqueue(PQ_ENABLED);
|
|
static File deck_file("deck", FILE_TYPE_DATA);
|
|
|
|
static void add_library_track(unsigned int id)
|
|
{
|
|
library_playqueue.add(id);
|
|
}
|
|
|
|
static void del_library_track(unsigned int id)
|
|
{
|
|
library_playqueue.del_track(id);
|
|
}
|
|
|
|
void deck :: init()
|
|
{
|
|
library_playqueue.set_flag(PQ_REPEAT);
|
|
library_playqueue.set_flag(PQ_DISABLE_CHANGED_SIZE);
|
|
library_playqueue.add_sort(SORT_ARTIST);
|
|
library_playqueue.add_sort(SORT_YEAR);
|
|
library_playqueue.add_sort(SORT_TRACK);
|
|
read();
|
|
|
|
get_callbacks()->on_library_track_add = add_library_track;
|
|
get_callbacks()->on_library_track_del = del_library_track;
|
|
get_callbacks()->on_queue_changed = write;
|
|
}
|
|
|
|
void deck :: read()
|
|
{
|
|
unsigned int num;
|
|
int random;
|
|
unsigned int field;
|
|
bool ascending;
|
|
std::list<Playqueue>::iterator it;
|
|
|
|
if (!deck_file.exists())
|
|
return;
|
|
|
|
deck_file.open(OPEN_READ);
|
|
deck_file >> random >> num;
|
|
for (unsigned int i = 0; i < num; i++) {
|
|
deck_file >> field >> ascending;
|
|
if (i == 0)
|
|
library_playqueue.reset_sort((sort_t)field);
|
|
else
|
|
library_playqueue.add_sort((sort_t)field);
|
|
if (ascending == false)
|
|
library_playqueue.add_sort((sort_t)field);
|
|
}
|
|
|
|
deck_file >> num;
|
|
if (random)
|
|
library_playqueue.set_flag(PQ_RANDOM);
|
|
|
|
playqueue_deck.resize(num);
|
|
|
|
for (it = playqueue_deck.begin(); it != playqueue_deck.end(); it++)
|
|
it->read(deck_file);
|
|
deck_file.close();
|
|
}
|
|
|
|
void deck :: write()
|
|
{
|
|
std::list<Playqueue>::iterator it;
|
|
std::list<sort_info>::iterator st;
|
|
std::list<sort_info> sort_order;
|
|
|
|
deck_file.open(OPEN_WRITE);
|
|
|
|
/* Save library playqueue */
|
|
sort_order = library_playqueue.get_sort_order();
|
|
deck_file << (library_playqueue.get_flags() & PQ_RANDOM) << " ";
|
|
deck_file << sort_order.size() << " ";
|
|
for (st = sort_order.begin(); st != sort_order.end(); st++)
|
|
deck_file << st->field << " " << st->ascending << " ";
|
|
|
|
deck_file << playqueue_deck.size() << std :: endl;
|
|
for (it = playqueue_deck.begin(); it != playqueue_deck.end(); it++) {
|
|
it->write(deck_file);
|
|
deck_file << std::endl;
|
|
}
|
|
deck_file.close();
|
|
}
|
|
|
|
Playqueue *deck :: create()
|
|
{
|
|
playqueue_deck.push_back(Playqueue(PQ_ENABLED));
|
|
return &playqueue_deck.back();
|
|
}
|
|
|
|
void deck :: remove(unsigned int id)
|
|
{
|
|
std::list<Playqueue>::iterator it = playqueue_deck.begin();
|
|
for (unsigned int i = 0; i < id; i++)
|
|
it++;
|
|
playqueue_deck.erase(it);
|
|
}
|
|
|
|
Playqueue *deck :: get(unsigned int id)
|
|
{
|
|
std::list<Playqueue>::iterator it = playqueue_deck.begin();
|
|
for (unsigned int i = 0; i < id; i++)
|
|
it++;
|
|
return &(*it);
|
|
}
|
|
|
|
void deck :: move(unsigned int old_pos, unsigned int new_pos)
|
|
{
|
|
std::list<Playqueue>::iterator it_old = playqueue_deck.begin();
|
|
std::list<Playqueue>::iterator it_new = playqueue_deck.begin();
|
|
|
|
for (unsigned int i = 0; i < playqueue_deck.size(); i++) {
|
|
if (i < old_pos)
|
|
it_old++;
|
|
if (i < new_pos)
|
|
it_new++;
|
|
}
|
|
|
|
if (new_pos > old_pos)
|
|
it_new++;
|
|
|
|
playqueue_deck.splice(it_new, playqueue_deck, it_old);
|
|
}
|
|
|
|
unsigned int deck :: next()
|
|
{
|
|
unsigned int id = 0;
|
|
std::list<Playqueue>::iterator it;
|
|
|
|
for (it = playqueue_deck.begin(); it != playqueue_deck.end(); it++) {
|
|
if (it->get_flags() & PQ_ENABLED) {
|
|
id = it->next();
|
|
if (it->size() == 0)
|
|
playqueue_deck.erase(it);
|
|
return id;
|
|
}
|
|
}
|
|
|
|
return library_playqueue.next();
|
|
}
|
|
|
|
Playqueue *deck :: get_library_pq()
|
|
{
|
|
return &library_playqueue;
|
|
}
|
|
|
|
#ifdef CONFIG_TEST
|
|
void deck :: reset()
|
|
{
|
|
playqueue_deck.clear();
|
|
library_playqueue.reset();
|
|
}
|
|
|
|
void deck :: print_info()
|
|
{
|
|
unsigned int i = 0;
|
|
std::list<Playqueue>::iterator it;
|
|
|
|
for (it = playqueue_deck.begin(); it != playqueue_deck.end(); it++) {
|
|
print("deck[%u] = Playqueue { size = %u, flags = %u }\n",
|
|
i, it->size(), it->get_flags());
|
|
i++;
|
|
}
|
|
}
|
|
#endif /* CONFIG_TEST */
|