From a2b3ca129214cf1c09052abc32c1de8b7561ad70 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Thu, 27 Nov 2014 10:37:46 -0500 Subject: [PATCH] Track: Make last played date private This patch creates a new structure to track the last played date, which can be expanded into a class at some future point. Additionally, I use strftime() to calculate the current date based on the user's current locale settings. Signed-off-by: Anna Schumaker --- core/tags.cpp | 26 ++++++-------------------- core/tags/track.cpp | 32 +++++++++++++++++++++++++++++++- include/core/tags/track.h | 30 ++++++++++++++++++++---------- lib/model.cpp | 11 +---------- tests/core/tags/track.cpp | 39 +++++++++++++++++++++++++++++++++------ 5 files changed, 91 insertions(+), 47 deletions(-) diff --git a/core/tags.cpp b/core/tags.cpp index 7b4274ec..0c2c316a 100644 --- a/core/tags.cpp +++ b/core/tags.cpp @@ -22,9 +22,7 @@ Database track_db("track.db", false); Track :: Track(const std::string &f, Library *library) : _album(NULL), _artist(NULL), _genre(NULL), _library(library), - _count(0), _path(f.substr(library->primary_key().size() + 1)), - last_year(0), last_month(0), last_day(0) - + _count(0), _path(f.substr(library->primary_key().size() + 1)) { library->inc_size(); } @@ -34,7 +32,7 @@ void Track :: read(File &f) unsigned int library_id, artist_id, album_id, genre_id; f >> library_id >> artist_id >> album_id >> genre_id; - f >> _track >> last_year >> last_month >> last_day; + f >> _track >> _date.year >> _date.month >> _date.day; f >> _count >> _length; GenericTag :: read(f); @@ -55,7 +53,7 @@ void Track :: write(File &f) { f << _library->index() << " " << _artist->index() << " "; f << _album->index() << " " << _genre->index() << " " << _track << " "; - f << last_year << " " << last_month << " " << last_day << " "; + f << _date.year << " " << _date.month << " " << _date.day << " "; f << _count << " " << _length << " "; GenericTag :: write(f); f << std::endl << _path << std::endl; @@ -105,18 +103,6 @@ bool Track :: tag() return true; } -void Track :: played() -{ - time_t the_time = time(NULL); - struct tm *now = localtime(&the_time); - - _count++; - last_day = now->tm_mday; - last_month = now->tm_mon + 1; - last_year = now->tm_year + 1900; - - tagdb :: commit(); -} /* * Returns: @@ -157,11 +143,11 @@ int Track :: less_than(Track *rhs, sort_t field) case SORT_LENGTH: return compare_uint(_length, rhs->_length); case SORT_PLAYED: - ret = compare_uint(last_year, rhs->last_year); + ret = compare_uint(_date.year, rhs->_date.year); if (ret == 0) { - ret = compare_uint(last_month, rhs->last_month); + ret = compare_uint(_date.month, rhs->_date.month); if (ret == 0) - ret = compare_uint(last_day, rhs->last_day); + ret = compare_uint(_date.day, rhs->_date.day); } return ret; case SORT_TITLE: diff --git a/core/tags/track.cpp b/core/tags/track.cpp index dbb191e3..a471fcae 100644 --- a/core/tags/track.cpp +++ b/core/tags/track.cpp @@ -5,12 +5,13 @@ #include #include +#include Track :: Track() : GenericTag(), _album(NULL), _artist(NULL), _genre(NULL), _library(NULL), - _count(0), _length(0), _track(0), last_year(0), last_month(0), last_day(0) + _count(0), _length(0), _track(0) {} Track :: Track(Album *album, Artist *artist, Genre *genre, Library *library, @@ -39,6 +40,22 @@ unsigned int Track :: count() { return _count; } unsigned int Track :: length() { return _length; } unsigned int Track :: track() { return _track; } +const std::string Track :: date() const +{ + struct tm tm; + char buffer[20]; + std::string res = "Never"; + + if (_count > 0) { + tm.tm_mday = _date.day; + tm.tm_mon = _date.month - 1; + tm.tm_year = _date.year - 1900; + strftime(buffer, 20, "%Ex", &tm); + res = buffer; + } + return res; +} + const std::string Track :: path() const { if (_library) @@ -66,3 +83,16 @@ const std::string Track :: primary_key() const ss << _library->index() << "/" << _path; return ss.str(); } + +void Track :: played() +{ + time_t rawtime = time(NULL); + struct tm *now = localtime(&rawtime); + + _count++; + _date.day = now->tm_mday; + _date.month = now->tm_mon + 1; + _date.year = now->tm_year + 1900; + + //tagdb :: commit(); +} diff --git a/include/core/tags/track.h b/include/core/tags/track.h index ae771fdf..deb43980 100644 --- a/include/core/tags/track.h +++ b/include/core/tags/track.h @@ -37,6 +37,15 @@ enum sort_t { }; +/** Structure used to represent dates. */ +struct date { + unsigned int day; /**< Day of the month (1 - 31). */ + unsigned int month; /**< Month of the year (1 - 12). */ + unsigned int year; /**< Number of years, Commen Era. */ + date() : day(0), month(0), year(0) {}; +}; + + class Track : public GenericTag { private: Album *_album; /**< Pointer to the Album containing this track. */ @@ -48,16 +57,10 @@ private: unsigned int _length; /**< Length of this track (in seconds). */ unsigned int _track; /**< Track number of this track. */ + struct date _date; /**< Date that we last played this track. */ std::string _path; /**< Path of this track, relative to the Library. */ public: - /** The year this track was last played */ - unsigned int last_year; - /** The month this track was last played */ - unsigned int last_month; - /** The day this track was last played */ - unsigned int last_day; - /** Track constructor */ Track(); @@ -97,6 +100,13 @@ public: unsigned int track(); /**< @return Track::_track. */ + /** + * @return A locale-dependent string containing the day this + * track was last played, or "Never" if the track has + * never been played. + */ + const std::string date() const; + /** @return The full path of this track. */ const std::string path() const; @@ -111,6 +121,9 @@ public: */ const std::string primary_key() const; + /** Increment Track::_count and set Track::_date to today's date. */ + void played(); + /** * Read track data from file @@ -131,9 +144,6 @@ public: */ bool tag(); - /** Increments play count and sets date-last-played information. */ - void played(); - /** * Compare two tracks based on a specific field. * @param rhs The other track to compare. diff --git a/lib/model.cpp b/lib/model.cpp index 2c484b59..b1c45d89 100644 --- a/lib/model.cpp +++ b/lib/model.cpp @@ -7,7 +7,6 @@ #include #include #include -#include QueueModel::QueueModel(Queue *q) @@ -134,7 +133,6 @@ void QueueModel::get_value_uint(Track *track, int column, void QueueModel::get_value_str(Track *track, int column, Glib::ValueBase &value) const { - std::stringstream ss; Glib::Value specific; specific.init(Glib::Value::value_type()); @@ -155,14 +153,7 @@ void QueueModel::get_value_str(Track *track, int column, specific.set(track->genre()->name()); break; case 8: - if (track->count() == 0) - specific.set("Never"); - else { - ss << track->last_month << " / "; - ss << track->last_day << " / "; - ss << track->last_year; - specific.set(ss.str()); - } + specific.set(track->date()); break; case 9: specific.set(Glib::Markup::escape_text(track->path())); diff --git a/tests/core/tags/track.cpp b/tests/core/tags/track.cpp index 6645bf31..5fc75d61 100644 --- a/tests/core/tags/track.cpp +++ b/tests/core/tags/track.cpp @@ -4,6 +4,7 @@ */ #include #include +#include static void test_track_tag_default() { @@ -17,15 +18,13 @@ static void test_track_tag_default() test_equal(track.name(), (std::string)""); test_equal(track.lowercase(), (std::string)""); test_equal(track.primary_key(), (std::string)""); + test_equal(track.date(), (std::string)"Never"); test_equal(track.path(), (std::string)""); test_equal(track.length_str(), (std::string)"0:00"); - test_equal(track.track(), (unsigned)0); - test_equal(track.length(), (unsigned)0); - test_equal(track.count(), (unsigned)0); - test_equal(track.last_year, (unsigned)0); - test_equal(track.last_month, (unsigned)0); - test_equal(track.last_day, (unsigned)0); + test_equal(track.track(), (unsigned)0); + test_equal(track.length(), (unsigned)0); + test_equal(track.count(), (unsigned)0); } static void test_track_tag_constructor() @@ -46,6 +45,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.date(), (std::string)"Never"); test_equal(track.path(), (std::string)"/home/Zelda/Music/Hyrule Symphony/13 - Legend of Zelda Medley.mp3"); test_equal(track.length_str(), (std::string)"4:48"); test_equal(track.primary_key(), (std::string)"0/Hyrule Symphony/13 - Legend of Zelda Medley.mp3"); @@ -67,12 +67,39 @@ static void test_track_tag_functional() Artist *artist = tags :: get_artist("Koji Kondo"); Genre *genre = tags :: get_genre("Video Game Music"); Library *library = tags :: get_library("/home/Zelda/Music"); + time_t rawtime = time(NULL); + struct tm *now = localtime(&rawtime); + std::stringstream ss; Track track; track = Track(album, artist, genre, library, "/home/Zelda/Music/Hyrule Symphony/6 - Kakariko Village.mp3", "Kakariko Village", 186, 6); + track.played(); test_equal(track.length_str(), (std::string)"3:06"); + test_equal(track.count(), (unsigned)1); + + + std::setlocale(LC_TIME, "C"); + if ((now->tm_mon + 1) < 10) + ss << "0"; + ss << (now->tm_mon + 1) << "/"; + if (now->tm_mday < 10) + ss << "0"; + ss << now->tm_mday << "/" << (now->tm_year % 100); + test_equal(track.date(), ss.str()); + + + std::setlocale(LC_TIME, "en_US"); + ss.str(""); + if ((now->tm_mon + 1) < 10) + ss << "0"; + ss << (now->tm_mon + 1) << "/"; + if (now->tm_mday < 10) + ss << "0"; + ss << now->tm_mday << "/" << now->tm_year + 1900; + test_equal(track.date(), ss.str()); + /* Not an actual track on the album, I just needed something < 1 min. */ track = Track(album, artist, genre, library,