deck: Create TempQueues
TempQueues create a way to trigger deck :: write() whenever a queue changes. This means higher layers don't need to remember to save after changes! Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
36322a6ff8
commit
f8f389c7ed
37
DESIGN
37
DESIGN
|
@ -1176,15 +1176,29 @@ Playlist:
|
||||||
Deck:
|
Deck:
|
||||||
The deck is used to hold temporary queues created by the user. This
|
The deck is used to hold temporary queues created by the user. This
|
||||||
layer is also in charge of maintaining a "recently played" queue of
|
layer is also in charge of maintaining a "recently played" queue of
|
||||||
tracks. The function deck :: write() must be called whenever a queue
|
tracks.
|
||||||
is modified to save the changes to disk.
|
|
||||||
|
|
||||||
The deck will be saved to the file "deck". When upgrading from file
|
The deck will be saved to the file "deck". When upgrading from file
|
||||||
version V0 to V1, use the saved random flag and sort order to set up
|
version V0 to V1, use the saved random flag and sort order to set up
|
||||||
the library_q.
|
the library_q.
|
||||||
|
|
||||||
|
- TempQueue:
|
||||||
|
class TempQueue : public Queue {
|
||||||
|
public:
|
||||||
|
TempQueue(bool);
|
||||||
|
|
||||||
|
void set_flag(queue_flag);
|
||||||
|
void unset_flag(queue_flag);
|
||||||
|
|
||||||
|
unsigned int add(Track *);
|
||||||
|
void del(Track *);
|
||||||
|
void del(unsigned int);
|
||||||
|
|
||||||
|
void sort(sort_t, bool);
|
||||||
|
};
|
||||||
|
|
||||||
- Deck:
|
- Deck:
|
||||||
list<Queue> deck;
|
list<TempQueue> deck;
|
||||||
|
|
||||||
V0:
|
V0:
|
||||||
File << library_q.random << library_q.sort_order().size();
|
File << library_q.random << library_q.sort_order().size();
|
||||||
|
@ -1205,6 +1219,23 @@ Deck:
|
||||||
unsigned int add(Track *);
|
unsigned int add(Track *);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
- TempQueue API:
|
||||||
|
TempQueue :: TempQueue(bool random);
|
||||||
|
Initialize a new TempQueue with the flag Q_ENABLED set.
|
||||||
|
If random is True then also set the Q_RANDOM flag.
|
||||||
|
|
||||||
|
void TempQueue :: set_flag(queue_flag flag);
|
||||||
|
void TempQueue :: unset_flag(queue_flag flag);
|
||||||
|
unsigned int TempQueue :: add(Track *track);
|
||||||
|
void TempQueue :: del(Track *track);
|
||||||
|
void TempQueue :: del(unsigned int index);
|
||||||
|
void TempQueue :: sort(sort_t field, bool ascending);
|
||||||
|
These functions are all wrappers around the basic Queue
|
||||||
|
functions of the same name. First, call the corresponding
|
||||||
|
Queue :: <whatever>() function to make the correct Queue
|
||||||
|
modification. Then, call deck :: write() to save changes to
|
||||||
|
disk.
|
||||||
|
|
||||||
- RecentQueue API:
|
- RecentQueue API:
|
||||||
RecentQueue :: RecentQueue();
|
RecentQueue :: RecentQueue();
|
||||||
Initialize a Queue with the flags Q_ENABLED, Q_REPEAT, and
|
Initialize a Queue with the flags Q_ENABLED, Q_REPEAT, and
|
||||||
|
|
|
@ -7,6 +7,23 @@
|
||||||
#include <queue.h>
|
#include <queue.h>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
|
||||||
|
|
||||||
|
class TempQueue : public Queue
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
TempQueue();
|
||||||
|
TempQueue(bool random);
|
||||||
|
|
||||||
|
void set_flag(queue_flags);
|
||||||
|
void unset_flag(queue_flags);
|
||||||
|
|
||||||
|
unsigned int add(Track *);
|
||||||
|
void del(Track *);
|
||||||
|
void del(unsigned int);
|
||||||
|
|
||||||
|
void sort(sort_t, bool);
|
||||||
|
};
|
||||||
|
|
||||||
namespace deck
|
namespace deck
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -21,7 +38,7 @@ namespace deck
|
||||||
Track *next();
|
Track *next();
|
||||||
Track *prev();
|
Track *prev();
|
||||||
|
|
||||||
std::list<Queue> &get_queues();
|
std::list<TempQueue> &get_queues();
|
||||||
Queue *get_queue();
|
Queue *get_queue();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -49,8 +49,8 @@ public:
|
||||||
bool has_flag(queue_flags);
|
bool has_flag(queue_flags);
|
||||||
|
|
||||||
virtual unsigned int add(Track *);
|
virtual unsigned int add(Track *);
|
||||||
void del(Track *);
|
virtual void del(Track *);
|
||||||
void del(unsigned int);
|
virtual void del(unsigned int);
|
||||||
void updated(Track *);
|
void updated(Track *);
|
||||||
Track *next();
|
Track *next();
|
||||||
|
|
||||||
|
|
68
lib/deck.cpp
68
lib/deck.cpp
|
@ -21,11 +21,53 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static std::list<Queue> queue_deck;
|
static std::list<TempQueue> queue_deck;
|
||||||
static RecentQueue recent_queue;
|
static RecentQueue recent_queue;
|
||||||
static File deck_file("deck", 1);
|
static File deck_file("deck", 1);
|
||||||
|
|
||||||
|
|
||||||
|
TempQueue :: TempQueue() {}
|
||||||
|
TempQueue :: TempQueue(bool random)
|
||||||
|
: Queue(Q_ENABLED | (random ? Q_RANDOM : 0)) {}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static void upgrade_v0()
|
static void upgrade_v0()
|
||||||
{
|
{
|
||||||
int random, ascending;
|
int random, ascending;
|
||||||
|
@ -48,7 +90,7 @@ void deck :: init()
|
||||||
{
|
{
|
||||||
unsigned int num;
|
unsigned int num;
|
||||||
bool upgraded = false;
|
bool upgraded = false;
|
||||||
std::list<Queue>::iterator it;
|
std::list<TempQueue>::iterator it;
|
||||||
|
|
||||||
if (!deck_file.open(OPEN_READ))
|
if (!deck_file.open(OPEN_READ))
|
||||||
return;
|
return;
|
||||||
|
@ -71,7 +113,7 @@ void deck :: init()
|
||||||
|
|
||||||
void deck :: write()
|
void deck :: write()
|
||||||
{
|
{
|
||||||
std::list<Queue>::iterator it;
|
std::list<TempQueue>::iterator it;
|
||||||
|
|
||||||
if (!deck_file.open(OPEN_WRITE))
|
if (!deck_file.open(OPEN_WRITE))
|
||||||
return;
|
return;
|
||||||
|
@ -87,12 +129,11 @@ void deck :: write()
|
||||||
|
|
||||||
Queue *deck :: create(bool random)
|
Queue *deck :: create(bool random)
|
||||||
{
|
{
|
||||||
unsigned int flag = (random ? Q_RANDOM : 0);
|
queue_deck.push_back(TempQueue(random));
|
||||||
queue_deck.push_back(Queue(Q_ENABLED | flag));
|
|
||||||
return &queue_deck.back();
|
return &queue_deck.back();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _destroy(std::list<Queue>::iterator &it)
|
static void _destroy(std::list<TempQueue>::iterator &it)
|
||||||
{
|
{
|
||||||
get_callbacks()->on_pq_removed(&(*it));
|
get_callbacks()->on_pq_removed(&(*it));
|
||||||
queue_deck.erase(it);
|
queue_deck.erase(it);
|
||||||
|
@ -101,7 +142,7 @@ static void _destroy(std::list<Queue>::iterator &it)
|
||||||
|
|
||||||
void deck :: destroy(Queue *queue)
|
void deck :: destroy(Queue *queue)
|
||||||
{
|
{
|
||||||
std::list<Queue>::iterator it;
|
std::list<TempQueue>::iterator it;
|
||||||
|
|
||||||
for (it = queue_deck.begin(); it != queue_deck.end(); it++) {
|
for (it = queue_deck.begin(); it != queue_deck.end(); it++) {
|
||||||
if (&(*it) == queue) {
|
if (&(*it) == queue) {
|
||||||
|
@ -114,8 +155,8 @@ void deck :: destroy(Queue *queue)
|
||||||
void deck :: move(Queue *queue, unsigned int new_pos)
|
void deck :: move(Queue *queue, unsigned int new_pos)
|
||||||
{
|
{
|
||||||
unsigned int old_pos = deck :: index(queue);
|
unsigned int old_pos = deck :: index(queue);
|
||||||
std::list<Queue>::iterator it_old = queue_deck.begin();
|
std::list<TempQueue>::iterator it_old = queue_deck.begin();
|
||||||
std::list<Queue>::iterator it_new = queue_deck.begin();
|
std::list<TempQueue>::iterator it_new = queue_deck.begin();
|
||||||
|
|
||||||
for (unsigned int i = 0; i < queue_deck.size(); i++) {
|
for (unsigned int i = 0; i < queue_deck.size(); i++) {
|
||||||
if (i < old_pos)
|
if (i < old_pos)
|
||||||
|
@ -129,12 +170,13 @@ void deck :: move(Queue *queue, unsigned int new_pos)
|
||||||
it_new++;
|
it_new++;
|
||||||
|
|
||||||
queue_deck.splice(it_new, queue_deck, it_old);
|
queue_deck.splice(it_new, queue_deck, it_old);
|
||||||
|
write();
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int deck :: index(Queue *queue)
|
unsigned int deck :: index(Queue *queue)
|
||||||
{
|
{
|
||||||
unsigned int i = 0;
|
unsigned int i = 0;
|
||||||
std::list<Queue>::iterator it;
|
std::list<TempQueue>::iterator it;
|
||||||
|
|
||||||
for (it = queue_deck.begin(); it != queue_deck.end(); it++) {
|
for (it = queue_deck.begin(); it != queue_deck.end(); it++) {
|
||||||
if (&(*it) == queue)
|
if (&(*it) == queue)
|
||||||
|
@ -148,7 +190,7 @@ unsigned int deck :: index(Queue *queue)
|
||||||
Track *deck :: next()
|
Track *deck :: next()
|
||||||
{
|
{
|
||||||
Track *track = NULL;
|
Track *track = NULL;
|
||||||
std::list<Queue>::iterator it;
|
std::list<TempQueue>::iterator it;
|
||||||
|
|
||||||
for (it = queue_deck.begin(); it != queue_deck.end(); it++) {
|
for (it = queue_deck.begin(); it != queue_deck.end(); it++) {
|
||||||
if (it->has_flag(Q_ENABLED) == false)
|
if (it->has_flag(Q_ENABLED) == false)
|
||||||
|
@ -157,8 +199,6 @@ Track *deck :: next()
|
||||||
track = it->next();
|
track = it->next();
|
||||||
if (it->size() == 0)
|
if (it->size() == 0)
|
||||||
_destroy(it);
|
_destroy(it);
|
||||||
else
|
|
||||||
write();
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,7 +214,7 @@ Track *deck :: prev()
|
||||||
return recent_queue.next();
|
return recent_queue.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::list<Queue> &deck :: get_queues()
|
std::list<TempQueue> &deck :: get_queues()
|
||||||
{
|
{
|
||||||
return queue_deck;
|
return queue_deck;
|
||||||
}
|
}
|
||||||
|
|
|
@ -165,7 +165,7 @@ Track :: Track(const std::string &f, Library *l)
|
||||||
|
|
||||||
Track :: ~Track()
|
Track :: ~Track()
|
||||||
{
|
{
|
||||||
library->count--;
|
//library->count--;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string Track :: primary_key() const
|
const std::string Track :: primary_key() const
|
||||||
|
|
|
@ -13,7 +13,7 @@ static void test_init()
|
||||||
{
|
{
|
||||||
unsigned int val;
|
unsigned int val;
|
||||||
File f("deck", 0);
|
File f("deck", 0);
|
||||||
std::list<Queue>::iterator it;
|
std::list<TempQueue>::iterator it;
|
||||||
|
|
||||||
deck :: init();
|
deck :: init();
|
||||||
test_equal(library :: get_queue()->has_flag(Q_RANDOM), true);
|
test_equal(library :: get_queue()->has_flag(Q_RANDOM), true);
|
||||||
|
@ -96,7 +96,7 @@ static void test_create_mv_destroy()
|
||||||
|
|
||||||
static void test_next_prev()
|
static void test_next_prev()
|
||||||
{
|
{
|
||||||
std::list<Queue>::iterator it = deck :: get_queues().begin();
|
std::list<TempQueue>::iterator it = deck :: get_queues().begin();
|
||||||
Queue *q = deck :: get_queue();
|
Queue *q = deck :: get_queue();
|
||||||
Queue *q0 = &(*it++);
|
Queue *q0 = &(*it++);
|
||||||
Queue *q1 = &(*it++);
|
Queue *q1 = &(*it++);
|
||||||
|
|
Loading…
Reference in New Issue