diff --git a/core/tags.cpp b/core/tags.cpp index 2e25cf4e..7b4274ec 100644 --- a/core/tags.cpp +++ b/core/tags.cpp @@ -22,17 +22,13 @@ Database track_db("track.db", false); Track :: Track(const std::string &f, Library *library) : _album(NULL), _artist(NULL), _genre(NULL), _library(library), - _count(0), last_year(0), last_month(0), last_day(0), - filepath(f.substr(library->primary_key().size() + 1)) + _count(0), _path(f.substr(library->primary_key().size() + 1)), + last_year(0), last_month(0), last_day(0) + { library->inc_size(); } -const std::string Track :: primary_key() const -{ - return path(); -} - void Track :: read(File &f) { unsigned int library_id, artist_id, album_id, genre_id; @@ -42,7 +38,7 @@ void Track :: read(File &f) f >> _count >> _length; GenericTag :: read(f); - filepath = f.getline(); + _path = f.getline(); _library = tags :: get_library(library_id); _artist = tags :: get_artist(artist_id); @@ -62,7 +58,7 @@ void Track :: write(File &f) f << last_year << " " << last_month << " " << last_day << " "; f << _count << " " << _length << " "; GenericTag :: write(f); - f << std::endl << filepath << std::endl; + f << std::endl << _path << std::endl; } static inline const std::string format_tag(const TagLib::String &str) @@ -109,13 +105,6 @@ bool Track :: tag() return true; } -const std::string Track :: path() const -{ - if (!_library) - return ""; - return _library->primary_key() + "/" + filepath; -} - void Track :: played() { time_t the_time = time(NULL); diff --git a/core/tags/track.cpp b/core/tags/track.cpp index 08ad3d0f..dbb191e3 100644 --- a/core/tags/track.cpp +++ b/core/tags/track.cpp @@ -14,10 +14,12 @@ Track :: Track() {} Track :: Track(Album *album, Artist *artist, Genre *genre, Library *library, - const std::string &name, unsigned int length, unsigned int track) + const std::string &filepath, const std::string &name, + unsigned int length, unsigned int track) : GenericTag(name), _album(album), _artist(artist), _genre(genre), _library(library), - _count(0), _length(length), _track(track) + _count(0), _length(length), _track(track), + _path(filepath.substr(library->primary_key().size() + 1)) { _library->inc_size(); } @@ -37,6 +39,13 @@ unsigned int Track :: count() { return _count; } unsigned int Track :: length() { return _length; } unsigned int Track :: track() { return _track; } +const std::string Track :: path() const +{ + if (_library) + return _library->primary_key() + "/" + _path; + return ""; +} + const std::string Track :: length_str() const { std::stringstream ss; @@ -49,3 +58,11 @@ const std::string Track :: length_str() const ss << seconds; return ss.str(); } + +const std::string Track :: primary_key() const +{ + std::stringstream ss; + if (_library) + ss << _library->index() << "/" << _path; + return ss.str(); +} diff --git a/include/core/tags/track.h b/include/core/tags/track.h index e95221a6..ae771fdf 100644 --- a/include/core/tags/track.h +++ b/include/core/tags/track.h @@ -48,6 +48,8 @@ private: unsigned int _length; /**< Length of this track (in seconds). */ unsigned int _track; /**< Track number of 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; @@ -56,9 +58,6 @@ public: /** The day this track was last played */ unsigned int last_day; - /** The filepath of this track, relative to the library root */ - std :: string filepath; - /** Track constructor */ Track(); @@ -69,12 +68,13 @@ public: * @param artist The artist performing this track. * @param genre The genre describing this track. * @param library The library containing this track. + * @param filepath The full filepath of this track. * @param name The name (title) of this track. * @param length The length of this track (in seconds). * @param track The track number of this track. */ Track(Album *, Artist *, Genre *, Library *, const std::string &, - unsigned int, unsigned int); + const std::string &, unsigned int, unsigned int); /** * Track constructor @@ -96,16 +96,22 @@ public: unsigned int length(); /**< @return Track::_length. */ unsigned int track(); /**< @return Track::_track. */ + + /** @return The full path of this track. */ + const std::string path() const; + /** @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 + * A track's primary key is the concatenation of the library index + * and the relative path. + * + * @return "Track::_library::index()"/Track::_path */ const std::string primary_key() const; + /** * Read track data from file * @param file The file to read from @@ -125,9 +131,6 @@ public: */ bool tag(); - /** @return The full path of this track */ - const std::string path() const; - /** Increments play count and sets date-last-played information. */ void played(); diff --git a/tests/core/tags/track.cpp b/tests/core/tags/track.cpp index b787f043..6645bf31 100644 --- a/tests/core/tags/track.cpp +++ b/tests/core/tags/track.cpp @@ -17,7 +17,7 @@ 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.filepath, (std::string)""); + test_equal(track.path(), (std::string)""); test_equal(track.length_str(), (std::string)"0:00"); test_equal(track.track(), (unsigned)0); @@ -34,7 +34,9 @@ static void test_track_tag_constructor() 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(album, artist, genre, library, "Legend of Zelda Medley", 288, 13); + Track track(album, artist, genre, library, + "/home/Zelda/Music/Hyrule Symphony/13 - Legend of Zelda Medley.mp3", + "Legend of Zelda Medley", 288, 13); test_equal(track.album(), album); test_equal(track.artist(), artist); @@ -44,7 +46,9 @@ 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.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"); test_equal(track.track(), (unsigned)13); test_equal(track.length(), (unsigned)288); @@ -65,16 +69,21 @@ static void test_track_tag_functional() Library *library = tags :: get_library("/home/Zelda/Music"); Track track; - track = Track(album, artist, genre, library, "Kakariko Village", 186, 6); + track = Track(album, artist, genre, library, + "/home/Zelda/Music/Hyrule Symphony/6 - Kakariko Village.mp3", + "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); + track = Track(album, artist, genre, library, + "/home/Zelda/Music/Hyrule Symphony/0 - intro.mp3", + "Intro", 56, 0); test_equal(track.length_str(), (std::string)"0:56"); } int main(int argc, char **argv) { + test :: reset_data_dir(); 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);