From bc3220ae85375eed8ca6625c457552b1d3c22735 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Wed, 22 Jan 2014 21:17:51 -0500 Subject: [PATCH] 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 --- include/playqueue.h | 1 + lib/playqueue.cpp | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/include/playqueue.h b/include/playqueue.h index fd729aed..634b23ce 100644 --- a/include/playqueue.h +++ b/include/playqueue.h @@ -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); diff --git a/lib/playqueue.cpp b/lib/playqueue.cpp index 19d5a9aa..ee7187cb 100644 --- a/lib/playqueue.cpp +++ b/lib/playqueue.cpp @@ -4,7 +4,13 @@ #include #include #include + #include +#include + +#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();