e1dbdb3ffb
- Correct spelling of "minutes" - Don't pluralize if duration is only 1 Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
188 lines
3.2 KiB
C++
188 lines
3.2 KiB
C++
/*
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
*/
|
|
#include <callback.h>
|
|
#include <library.h>
|
|
#include <playqueue.h>
|
|
|
|
#include <stdlib.h>
|
|
#include <sstream>
|
|
|
|
#define O_MINUTES (60)
|
|
#define O_HOURS (60 * O_MINUTES)
|
|
#define O_DAYS (24 * O_HOURS)
|
|
|
|
Playqueue :: Playqueue()
|
|
: flags(0), cur(-1), length(0)
|
|
{
|
|
}
|
|
|
|
Playqueue :: Playqueue(playqueue_flags f)
|
|
: flags(f), cur(-1), length(0)
|
|
{
|
|
}
|
|
|
|
Playqueue :: ~Playqueue()
|
|
{
|
|
}
|
|
|
|
void Playqueue :: write(File &f)
|
|
{
|
|
f << flags << " " << tracks.size();
|
|
for (unsigned int i = 0; i < tracks.size(); i++)
|
|
f << " " << tracks[i];
|
|
}
|
|
|
|
void Playqueue :: read(File &f)
|
|
{
|
|
unsigned int n;
|
|
f >> flags >> n;
|
|
tracks.resize(n);
|
|
for (unsigned int i = 0; i < n; i++)
|
|
f >> tracks[i];
|
|
}
|
|
|
|
void Playqueue :: set_flag(playqueue_flags f)
|
|
{
|
|
flags |= f;
|
|
}
|
|
|
|
void Playqueue :: unset_flag(playqueue_flags f)
|
|
{
|
|
flags &= ~f;
|
|
}
|
|
|
|
const unsigned int Playqueue :: get_flags()
|
|
{
|
|
return flags;
|
|
}
|
|
|
|
unsigned int Playqueue :: get_length()
|
|
{
|
|
return length;
|
|
}
|
|
|
|
static inline void add_duration(std::stringstream &ss, unsigned int dur,
|
|
unsigned int remaining, const std::string &field)
|
|
{
|
|
if (dur > 0) {
|
|
ss << dur << " " << field;
|
|
if (dur > 1)
|
|
ss << "s";
|
|
if (remaining > 0)
|
|
ss << ", ";
|
|
}
|
|
}
|
|
|
|
std::string Playqueue :: get_length_str()
|
|
{
|
|
std::stringstream ss;
|
|
unsigned int len = length;
|
|
|
|
unsigned int days = len / O_DAYS;
|
|
len -= days * O_DAYS;
|
|
add_duration(ss, days, len, "day");
|
|
|
|
unsigned int hours = len / O_HOURS;
|
|
len -= hours *O_HOURS;
|
|
add_duration(ss, hours, len, "hour");
|
|
unsigned int mins = len / O_MINUTES;
|
|
add_duration(ss, mins, len, "minute");
|
|
unsigned int secs = len - (mins * O_MINUTES);
|
|
add_duration(ss, secs, 0, "second");
|
|
|
|
return ss.str();
|
|
}
|
|
|
|
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);
|
|
length += song.track->length;
|
|
get_callbacks()->on_queue_track_add(this, id);
|
|
return id;
|
|
}
|
|
|
|
unsigned int Playqueue :: add_front(unsigned int track_id)
|
|
{
|
|
library :: Song song;
|
|
tracks.insert(tracks.begin(), track_id);
|
|
|
|
library :: lookup(track_id, &song);
|
|
length += song.track->length;
|
|
get_callbacks()->on_queue_track_add(this, 0);
|
|
return 0;
|
|
}
|
|
|
|
void Playqueue :: del(unsigned int plist_id)
|
|
{
|
|
library :: Song song;
|
|
unsigned int track_id = tracks[plist_id];
|
|
|
|
tracks.erase(tracks.begin() + plist_id);
|
|
library :: lookup(track_id, &song);
|
|
length -= song.track->length;
|
|
get_callbacks()->on_queue_track_del(this, plist_id);
|
|
}
|
|
|
|
void Playqueue :: del_track(unsigned int track_id)
|
|
{
|
|
unsigned int i = 0;
|
|
while (i < tracks.size()) {
|
|
if (tracks[i] == track_id)
|
|
del(i);
|
|
else
|
|
i++;
|
|
}
|
|
}
|
|
|
|
unsigned int Playqueue :: size()
|
|
{
|
|
return tracks.size();
|
|
}
|
|
|
|
unsigned int Playqueue :: operator[](unsigned int i)
|
|
{
|
|
return tracks[i];
|
|
}
|
|
|
|
unsigned int Playqueue :: next()
|
|
{
|
|
unsigned int res;
|
|
|
|
if (tracks.size() == 0)
|
|
throw -E_EXIST;
|
|
else if (tracks.size() == 1)
|
|
cur = 0;
|
|
else if (flags & PQ_RANDOM)
|
|
cur += rand() % (tracks.size() / 2) + 1;
|
|
else
|
|
cur++;
|
|
|
|
if (cur >= tracks.size())
|
|
cur -= tracks.size();
|
|
|
|
res = tracks[cur];
|
|
if (!(flags & PQ_REPEAT)) {
|
|
del(cur);
|
|
cur--;
|
|
}
|
|
return res;
|
|
}
|
|
|
|
void Playqueue :: reset_cur()
|
|
{
|
|
cur = 0;
|
|
}
|
|
|
|
#ifdef CONFIG_TEST
|
|
void Playqueue :: reset()
|
|
{
|
|
tracks.clear();
|
|
reset_cur();
|
|
}
|
|
#endif /* CONFIG_TEST */
|