playqueue: Sort playqueues if PQ_SORTED is enabled

I don't have user-configurable sorting (yet), but for now I'm sorting by
Artist -> Year -> Track #.

I also fix a bug where the library wasn't lowercasing artist, album,
genere, and track fields when reading from file.

Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
This commit is contained in:
Anna Schumaker 2014-01-25 14:43:51 -05:00 committed by Anna Schumaker
parent cde7e5d96c
commit e256c24b20
4 changed files with 47 additions and 2 deletions

View File

@ -12,6 +12,7 @@ enum playqueue_flags {
PQ_ENABLED = (1 << 0),
PQ_RANDOM = (1 << 1),
PQ_REPEAT = (1 << 2),
PQ_SORTED = (1 << 3),
PQ_DISABLE_CHANGED_SIZE = (1 << 3),
};
@ -22,6 +23,8 @@ private:
unsigned int cur;
unsigned int length;
unsigned int add_sorted(unsigned int, library :: Song &);
public:
Playqueue();
Playqueue(playqueue_flags);

View File

@ -26,6 +26,7 @@ static void del_library_track(unsigned int id)
void deck :: init()
{
library_playqueue.set_flag(PQ_REPEAT);
library_playqueue.set_flag(PQ_SORTED);
library_playqueue.set_flag(PQ_DISABLE_CHANGED_SIZE);
read();

View File

@ -64,6 +64,7 @@ library :: AGInfo :: AGInfo(DB_Type type, const std::string &str)
void library :: AGInfo :: read(File &f)
{
primary_key = f.getline();
key_lower = filter :: to_lowercase(primary_key);
}
void library :: AGInfo :: write(File &f)
@ -115,6 +116,7 @@ void library :: Album :: read(File &f)
{
f >> artist_id >> year;
name = f.getline();
name_lower = filter :: to_lowercase(name);
}
void library :: Album :: write(File &f)
@ -237,6 +239,7 @@ void library :: Track :: read(File &f)
length_str = f.getline();
title = f.getline();
filepath = f.getline();
title_lower = filter :: to_lowercase(title);
library_db[library_id].size++;
}

View File

@ -96,13 +96,51 @@ std::string Playqueue :: get_length_str()
return ss.str();
}
/*
* std::string.compare() returns
* 0: Strings are equal
* < 0: a < b
* > 0: a > b
*/
static bool track_less_than(library :: Song &lhs, library :: Song &rhs)
{
int cmp_res;
cmp_res = lhs.artist->key_lower.compare(rhs.artist->key_lower);
if (cmp_res != 0)
return cmp_res < 0;
if (lhs.album->year != rhs.album->year)
return lhs.album->year < rhs.album->year;
return lhs.track->track < rhs.track->track;
}
unsigned int Playqueue :: add_sorted(unsigned int track_id, library :: Song &rhs)
{
unsigned int id;
library :: Song lhs;
for (id = 0; id != tracks.size(); id++) {
library :: lookup(tracks[id], &lhs);
if (!track_less_than(lhs, rhs))
break;
}
tracks.insert(tracks.begin() + id, track_id);
return id;
}
unsigned int Playqueue :: add(unsigned int track_id)
{
unsigned int id = tracks.size();
library :: Song song;
tracks.push_back(track_id);
library :: lookup(track_id, &song);
if (flags & PQ_SORTED)
add_sorted(track_id, song);
else
tracks.push_back(track_id);
length += song.track->length;
get_callbacks()->on_queue_track_add(this, id);
if (!(flags & PQ_DISABLE_CHANGED_SIZE))