ocarina/lib/deck.cpp

157 lines
3.2 KiB
C++
Raw Normal View History

/*
* 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;
std::list<Playqueue>::iterator it;
if (!deck_file.exists())
return;
deck_file.open(OPEN_READ);
deck_file >> random >> 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;
deck_file.open(OPEN_WRITE);
deck_file << (library_playqueue.get_flags() & PQ_RANDOM) << " ";
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 */