playqueue: Generate a string representing current runtime.

I almost did this on the gui side, but then I remembered that this isn't
a trivial job.  I chose to stick with my rule: "if something can be done
in the backend then it should be done in the backend"

Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
This commit is contained in:
Anna Schumaker 2014-01-22 21:17:51 -05:00 committed by Anna Schumaker
parent 0f11ba0ee6
commit bc3220ae85
2 changed files with 37 additions and 0 deletions

View File

@ -32,6 +32,7 @@ public:
void unset_flag(playqueue_flags);
const unsigned int get_flags();
unsigned int get_length();
std::string get_length_str();
unsigned int add(unsigned int);
unsigned int add_front(unsigned int);

View File

@ -4,7 +4,13 @@
#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)
@ -56,6 +62,36 @@ 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 (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, "days");
unsigned int hours = len / O_HOURS;
len -= hours *O_HOURS;
add_duration(ss, hours, len, "hours");
unsigned int mins = len / O_MINUTES;
add_duration(ss, mins, len, "miutes");
unsigned int secs = len - (mins * O_MINUTES);
add_duration(ss, secs, 0, "seconds");
return ss.str();
}
unsigned int Playqueue :: add(unsigned int track_id)
{
unsigned int id = tracks.size();