Track: Convert length_str into a function

I don't think this value needs to be stored anywhere in the Track class
since it's fairly easy to calculate.  Let's convert it into a function
for now and reevaluate later!

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2014-11-26 08:55:06 -05:00
parent 2540a6aa7a
commit cdd20da5c0
6 changed files with 40 additions and 23 deletions

View File

@ -8,7 +8,6 @@
#include <core/filter.h>
#include <core/print.h>
#include <sstream>
#include <taglib/tag.h>
#include <taglib/fileref.h>
@ -54,7 +53,6 @@ void Track :: read(File &f)
filter :: add(_artist->name(), index());
filter :: add(_album->name(), index());
_library->inc_size();
set_length_str();
}
void Track :: write(File &f)
@ -67,19 +65,6 @@ void Track :: write(File &f)
f << std::endl << filepath << std::endl;
}
void Track :: set_length_str()
{
std::stringstream ss;
unsigned int minutes = _length / 60;
unsigned int seconds = _length % 60;
ss << minutes << ":";
if (seconds < 10)
ss << "0";
ss << seconds;
length_str = ss.str();
}
static inline const std::string format_tag(const TagLib::String &str)
{
return str.stripWhiteSpace().to8Bit(true);
@ -117,7 +102,6 @@ bool Track :: tag()
//_title = format_tag(tag->title());
//title_lower = filter :: add(title, index());
set_length_str();
filter :: add(_artist->name(), index());
filter :: add(_album->name(), index());

View File

@ -4,6 +4,8 @@
*/
#include <core/tags/track.h>
#include <sstream>
Track :: Track()
: GenericTag(),
@ -34,3 +36,16 @@ Library *Track :: library() { return _library; }
unsigned int Track :: count() { return _count; }
unsigned int Track :: length() { return _length; }
unsigned int Track :: track() { return _track; }
const std::string Track :: length_str() const
{
std::stringstream ss;
unsigned int minutes = _length / 60;
unsigned int seconds = _length % 60;
ss << minutes << ":";
if (seconds < 10)
ss << 0;
ss << seconds;
return ss.str();
}

View File

@ -49,7 +49,7 @@ static void on_track_loaded(Track *track)
set_label_text(title, "xx-large", track->name());
set_label_text(artist, "x-large", "By: " + track->artist()->name());
set_label_text(album, "x-large", "From: " + track->album()->name());
duration->set_text(track->length_str);
duration->set_text(track->length_str());
bool banned = playlist :: has(track, "Banned");
bool favorite = playlist :: has(track, "Favorites");

View File

@ -48,8 +48,6 @@ private:
unsigned int _length; /**< Length of this track (in seconds). */
unsigned int _track; /**< Track number of this track. */
void set_length_str();
public:
/** The year this track was last played */
unsigned int last_year;
@ -60,8 +58,6 @@ public:
/** The filepath of this track, relative to the library root */
std :: string filepath;
/** The length of this track in a human readable string */
std :: string length_str;
/** Track constructor */
Track();
@ -100,6 +96,10 @@ public:
unsigned int length(); /**< @return Track::_length. */
unsigned int track(); /**< @return Track::_track. */
/** @return Track::_length in mm:ss format. */
const std::string length_str() const;
/**
* Called to access a track's primary key
* @return The full path of the track

View File

@ -143,7 +143,7 @@ void QueueModel::get_value_str(Track *track, int column,
specific.set(track->name());
break;
case 2:
specific.set(track->length_str);
specific.set(track->length_str());
break;
case 3:
specific.set(track->artist()->name());

View File

@ -18,7 +18,7 @@ static void test_track_tag_default()
test_equal(track.lowercase(), (std::string)"");
test_equal(track.primary_key(), (std::string)"");
test_equal(track.filepath, (std::string)"");
test_equal(track.length_str, (std::string)"");
test_equal(track.length_str(), (std::string)"0:00");
test_equal(track.track(), (unsigned)0);
test_equal(track.length(), (unsigned)0);
@ -44,6 +44,7 @@ static void test_track_tag_constructor()
test_equal(track.name(), (std::string)"Legend of Zelda Medley");
test_equal(track.lowercase(), (std::string)"legend of zelda medley");
test_equal(track.length_str(), (std::string)"4:48");
test_equal(track.track(), (unsigned)13);
test_equal(track.length(), (unsigned)288);
@ -56,10 +57,27 @@ static void test_track_tag_destructor()
test_equal(library->size(), (unsigned)0);
}
static void test_track_tag_functional()
{
Album *album = tags :: get_album("Hyrule Symphony", 1998);
Artist *artist = tags :: get_artist("Koji Kondo");
Genre *genre = tags :: get_genre("Video Game Music");
Library *library = tags :: get_library("/home/Zelda/Music");
Track track;
track = Track(album, artist, genre, library, "Kakariko Village", 186, 6);
test_equal(track.length_str(), (std::string)"3:06");
/* Not an actual track on the album, I just needed something < 1 min. */
track = Track(album, artist, genre, library, "Intro", 56, 0);
test_equal(track.length_str(), (std::string)"0:56");
}
int main(int argc, char **argv)
{
run_test("Track Tag Default Constructor Test", test_track_tag_default);
run_test("Track Tag Constructor Test", test_track_tag_constructor);
run_test("Track Tag Destructor Test", test_track_tag_destructor);
run_test("Track Tag Functional Test", test_track_tag_functional);
return 0;
}