From a30f5ef794e963fcfd5fd1a5eff0d2c5a708d463 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sat, 29 Nov 2014 10:38:27 -0500 Subject: [PATCH] Track: Create an add_track() function We return a new Track tag or NULL if we have already tagged this track. Signed-off-by: Anna Schumaker --- core/tags/track.cpp | 14 ++++++++++++++ include/core/tags/track.h | 21 +++++++++++++++++++++ tests/core/tags/track.cpp | 17 +++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/core/tags/track.cpp b/core/tags/track.cpp index 2a85516d..743ff625 100644 --- a/core/tags/track.cpp +++ b/core/tags/track.cpp @@ -8,6 +8,9 @@ #include +static Database track_db("track.db", false); + + Track :: Track() : GenericTag(), _album(NULL), _artist(NULL), _genre(NULL), _library(NULL), @@ -150,3 +153,14 @@ void Track :: write(File &f) GenericTag :: write(f); f << std::endl << _path << std::endl; } + + +Track *tags :: add_track(Album *album, Artist *artist, Genre *genre, + Library *library, const std::string &filepath, + const std::string &name, unsigned int length, + unsigned int track) +{ + std::string path = filepath.substr(library->primary_key().size() + 1); + return track_db.insert(Track(album, artist, genre, library, path, + name, length, track)); +} diff --git a/include/core/tags/track.h b/include/core/tags/track.h index 9e15f4e1..a92845b6 100644 --- a/include/core/tags/track.h +++ b/include/core/tags/track.h @@ -123,4 +123,25 @@ public: void write(File &); }; +namespace tags +{ + /** + * Called to create a new Track tag. + * + * @param album The album containing this track. + * @param artist The artist performing this track. + * @param genre The genre describing this track. + * @param library The library containing this track. + * @param filepath The path of this track, relative to the library root. + * @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. + * @return A new Track tag, or NULL if this Track is already in + * the track_db. + */ + Track *add_track(Album *, Artist *, Genre *, Library *, + const std::string &, const std::string &, + unsigned int, unsigned int); +} + #endif /* OCARINA_CORE_TAGS_TRACK_H */ diff --git a/tests/core/tags/track.cpp b/tests/core/tags/track.cpp index c6e7e45f..3813e48d 100644 --- a/tests/core/tags/track.cpp +++ b/tests/core/tags/track.cpp @@ -81,6 +81,22 @@ static void test_track_tag_destructor() test_equal(library->size(), (unsigned)0); } +static void test_track_tag_lookup() +{ + Track *a, *b; + + a = tags :: add_track(album, artist, genre, library, + MUSIC_DIR + "/Hyrule Symphony/13 - Legend of Zelda Medley.mp3", + "Legend of Zelda Medley", 288, 13); + test_not_equal(a, (Track *)NULL); + verify_track_tag(a, 1); + + b = tags :: add_track(album, artist, genre, library, + MUSIC_DIR + "/Hyrule Symphony/13 - Legend of Zelda Medley.mp3", + "Legend of Zelda Medley", 288, 13); + test_equal(b, (Track *)NULL); +} + static void test_track_tag_functional() { time_t rawtime = time(NULL); @@ -139,6 +155,7 @@ 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 Lookup Test", test_track_tag_lookup); run_test("Track Tag Functional Test", test_track_tag_functional); return 0; }