playqueue: Write library sort order to disk

Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
This commit is contained in:
Anna Schumaker 2014-01-26 14:44:56 -05:00 committed by Anna Schumaker
parent f83f8d47fa
commit 6a227df41b
3 changed files with 64 additions and 9 deletions

View File

@ -28,15 +28,21 @@ enum sort_t {
SORT_YEAR, SORT_YEAR,
}; };
struct sort_info {
sort_t field;
bool ascending;
};
class Playqueue { class Playqueue {
private: private:
std :: vector <unsigned int> tracks; std :: vector <unsigned int> tracks;
std :: list <sort_t> sort_order; std :: list <sort_info> sort_order;
unsigned int flags; unsigned int flags;
unsigned int cur; unsigned int cur;
unsigned int length; unsigned int length;
unsigned int add_sorted(unsigned int, library :: Song &); unsigned int add_sorted(unsigned int, library :: Song &);
void _add_sort(sort_t);
public: public:
Playqueue(); Playqueue();
@ -59,6 +65,7 @@ public:
void add_sort(sort_t); void add_sort(sort_t);
void reset_sort(sort_t); void reset_sort(sort_t);
std::list <sort_info> &get_sort_order();
unsigned int operator[](unsigned int); unsigned int operator[](unsigned int);
unsigned int next(); unsigned int next();

View File

@ -41,6 +41,8 @@ void deck :: read()
{ {
unsigned int num; unsigned int num;
int random; int random;
unsigned int field;
bool ascending;
std::list<Playqueue>::iterator it; std::list<Playqueue>::iterator it;
if (!deck_file.exists()) if (!deck_file.exists())
@ -48,7 +50,17 @@ void deck :: read()
deck_file.open(OPEN_READ); deck_file.open(OPEN_READ);
deck_file >> random >> num; 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) if (random)
library_playqueue.set_flag(PQ_RANDOM); library_playqueue.set_flag(PQ_RANDOM);
@ -62,9 +74,18 @@ void deck :: read()
void deck :: write() void deck :: write()
{ {
std::list<Playqueue>::iterator it; std::list<Playqueue>::iterator it;
std::list<sort_info>::iterator st;
std::list<sort_info> sort_order;
deck_file.open(OPEN_WRITE); 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 << (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; deck_file << playqueue_deck.size() << std :: endl;
for (it = playqueue_deck.begin(); it != playqueue_deck.end(); it++) { for (it = playqueue_deck.begin(); it != playqueue_deck.end(); it++) {
it->write(deck_file); it->write(deck_file);

View File

@ -144,13 +144,16 @@ static inline int track_compare(library :: Song &lhs, library :: Song &rhs,
} }
static bool track_less_than(library :: Song &lhs, library :: Song &rhs, static bool track_less_than(library :: Song &lhs, library :: Song &rhs,
std::list<sort_t> &order) std::list<sort_info> &order)
{ {
std::list<sort_t>::iterator it; std::list<sort_info>::iterator it;
int res; int res;
for (it = order.begin(); it != order.end(); it++) { for (it = order.begin(); it != order.end(); it++) {
res = track_compare(lhs, rhs, *it); if (it->ascending == true)
res = track_compare(lhs, rhs, it->field);
else
res = track_compare(rhs, lhs, it->field);
if (res != 0) if (res != 0)
return res < 0; return res < 0;
} }
@ -246,9 +249,9 @@ unsigned int Playqueue :: size()
/* Sorting function */ /* Sorting function */
class SortTracks { class SortTracks {
private: private:
std::list<sort_t> fields; std::list<sort_info> fields;
public: public:
SortTracks(std::list<sort_t> f) : fields(f) {} SortTracks(std::list<sort_info> f) : fields(f) {}
bool operator()(unsigned int a, unsigned int b) bool operator()(unsigned int a, unsigned int b)
{ {
library::Song lhs, rhs; library::Song lhs, rhs;
@ -258,15 +261,34 @@ public:
} }
}; };
void Playqueue :: add_sort(sort_t field) void Playqueue :: _add_sort(sort_t field)
{ {
sort_order.remove(field); struct sort_info info;
sort_order.push_back(field); std::list<sort_info>::iterator it;
/* Is field already in the sort_order? */
for (it = sort_order.begin(); it != sort_order.end(); it++) {
if (it->field == field) {
it->ascending = !it->ascending;
return;
}
}
info.field = field;
info.ascending = true;
sort_order.push_back(info);
if (sort_order.size() >= 4) if (sort_order.size() >= 4)
sort_order.erase(sort_order.begin()); sort_order.erase(sort_order.begin());
}
void Playqueue :: add_sort(sort_t field)
{
_add_sort(field);
std::stable_sort(tracks.begin(), tracks.end(), SortTracks(sort_order)); std::stable_sort(tracks.begin(), tracks.end(), SortTracks(sort_order));
for (unsigned int i = 0; i < tracks.size(); i++) for (unsigned int i = 0; i < tracks.size(); i++)
get_callbacks()->on_queue_track_changed(this, i); get_callbacks()->on_queue_track_changed(this, i);
get_callbacks()->on_queue_changed();
} }
void Playqueue :: reset_sort(sort_t field) void Playqueue :: reset_sort(sort_t field)
@ -275,6 +297,11 @@ void Playqueue :: reset_sort(sort_t field)
add_sort(field); add_sort(field);
} }
std::list<sort_info> &Playqueue :: get_sort_order()
{
return sort_order;
}
unsigned int Playqueue :: operator[](unsigned int i) unsigned int Playqueue :: operator[](unsigned int i)
{ {
return tracks[i]; return tracks[i];