From 2d9d87afc98880a2f7d208feb2f23886c7f6e0e7 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sat, 8 Nov 2014 20:53:08 -0500 Subject: [PATCH] Album: Move Album tag into a new file Also make it inherit from the GenericTag base class. Also also, add a unit test specific to Album tags. Finally, I remove the corresponding section of the DESIGN file since it is no longer needed. Signed-off-by: Anna Schumaker --- DESIGN | 40 ---------------------- core/tags.cpp | 42 +++-------------------- core/tags/album.cpp | 37 ++++++++++++++++++++ core/tags/generic.cpp | 2 +- gui/gui.cpp | 2 +- include/core/tags.h | 43 +----------------------- include/core/tags/album.h | 67 +++++++++++++++++++++++++++++++++++++ include/core/tags/generic.h | 2 +- lib/model.cpp | 4 +-- tests/core/Sconscript | 1 + tests/core/tags/.gitignore | 1 + tests/core/tags/album.cpp | 42 +++++++++++++++++++++++ tests/lib/.gitignore | 1 + 13 files changed, 159 insertions(+), 125 deletions(-) create mode 100644 core/tags/album.cpp create mode 100644 include/core/tags/album.h create mode 100644 tests/core/tags/album.cpp diff --git a/DESIGN b/DESIGN index 3a4ee32a..66715845 100644 --- a/DESIGN +++ b/DESIGN @@ -120,46 +120,6 @@ Tag Database: -Album Tag: - The album tag is used to collect information about each artist's albums. - -- Album: - class Album : public DatabaseEntry { - public: - std::string name; - std::string lower; - unsigned int year; - - Album(); - Album(const std::string &, unsigned int); - const std::string primary_key() const; - void read(File &); - void write(File &); - }; - -- File Format: - File << year << name; - -- API: - Album(); - Initialize an invalid Album instance. - - Album(const std::string &album_name, unsigned int album_year); - Set name and year from album name and album_year. Find the - lowercase form of the album name. - - const std::string Album :: primary_key() const; - Return the string: "$year.$name" - - void Album :: read(File &f); - Read year, and name from file. Then set the lowercase form - of the album name. - - void Artist :: write(File &f); - Write album information to file. - - - Genre Tag: The genre tag is used to collect basic information about the various genres of songs in the library. diff --git a/core/tags.cpp b/core/tags.cpp index 663e561c..a18eccd9 100644 --- a/core/tags.cpp +++ b/core/tags.cpp @@ -18,40 +18,6 @@ Database library_db("library.db", true); Database track_db("track.db", false); -/** - * - * Album tag - * - */ - -Album :: Album() {} - -Album :: Album(const std::string &s, unsigned int y) - : name(s), lower(filter :: lowercase(name)), year(y) -{ -} - -const std::string Album :: primary_key() const -{ - std::stringstream ss; - ss << year << "." << name; - return ss.str(); -} - -void Album :: read(File &f) -{ - f >> year; - name = f.getline(); - lower = filter :: lowercase(name); -} - -void Album :: write(File &f) -{ - f << year << " " << name; -} - - - /** * * Genre tag @@ -162,7 +128,7 @@ void Track :: read(File &f) title_lower = filter :: add(title, index()); filter :: add(artist->name(), index()); - filter :: add(album->name, index()); + filter :: add(album->name(), index()); library->count++; set_length_str(); } @@ -229,7 +195,7 @@ bool Track :: tag() set_length_str(); filter :: add(artist->name(), index()); - filter :: add(album->name, index()); + filter :: add(album->name(), index()); return true; } @@ -283,7 +249,7 @@ int Track :: less_than(Track *rhs, sort_t field) case SORT_ARTIST: return compare_string(artist->lowercase(), rhs->artist->lowercase()); case SORT_ALBUM: - return compare_string(album->lower, rhs->album->lower); + return compare_string(album->lowercase(), rhs->album->lowercase()); case SORT_COUNT: return compare_uint(play_count, rhs->play_count); case SORT_GENRE: @@ -303,7 +269,7 @@ int Track :: less_than(Track *rhs, sort_t field) case SORT_TRACK: return compare_uint(track, rhs->track); case SORT_YEAR: - return compare_uint(album->year, rhs->album->year); + return compare_uint(album->year(), rhs->album->year()); } return 0; } diff --git a/core/tags/album.cpp b/core/tags/album.cpp new file mode 100644 index 00000000..da2b6812 --- /dev/null +++ b/core/tags/album.cpp @@ -0,0 +1,37 @@ +/** + * @file + * Copyright 2014 (c) Anna Schumaker. + */ +#include +#include + +Album :: Album() : GenericTag(), _year(0) {} + +Album :: Album(const std::string &name, unsigned int year) + : GenericTag(name), _year(year) +{ +} + +const std::string Album :: primary_key() const +{ + std::stringstream ss; + ss << _year << "/" << GenericTag :: primary_key(); + return ss.str(); +} + +void Album :: read(File &file) +{ + file >> _year; + GenericTag :: read(file); +} + +void Album :: write(File &file) +{ + file << _year << " "; + GenericTag :: write(file); +} + +unsigned int Album :: year() +{ + return _year; +} diff --git a/core/tags/generic.cpp b/core/tags/generic.cpp index d800d9ef..30cbfe08 100644 --- a/core/tags/generic.cpp +++ b/core/tags/generic.cpp @@ -28,7 +28,7 @@ void GenericTag :: write(File &file) file << _name; } -const std::string &GenericTag :: name() +const std::string &GenericTag :: name() const { return _name; } diff --git a/gui/gui.cpp b/gui/gui.cpp index 19c413ce..f9dc746d 100644 --- a/gui/gui.cpp +++ b/gui/gui.cpp @@ -48,7 +48,7 @@ static void on_track_loaded(Track *track) set_label_text(title, "xx-large", track->title); set_label_text(artist, "x-large", "By: " + track->artist->name()); - set_label_text(album, "x-large", "From: " + track->album->name); + set_label_text(album, "x-large", "From: " + track->album->name()); duration->set_text(track->length_str); bool banned = playlist :: has(track, "Banned"); diff --git a/include/core/tags.h b/include/core/tags.h index 722434b5..a0cba4cc 100644 --- a/include/core/tags.h +++ b/include/core/tags.h @@ -6,6 +6,7 @@ #define OCARINA_CORE_TAGS_H #include +#include #include @@ -34,48 +35,6 @@ enum sort_t { }; -/** - * Album tag - */ -class Album : public DatabaseEntry { -public: - /** Album name */ - std::string name; - /** Album name (lowercase) */ - std::string lower; - /** Album year */ - unsigned int year; - - /** Album tag constructor */ - Album(); - - /** - * Album tag constructor - * @param name Album name - * @param year Album year - */ - Album(const std::string &, unsigned int); - - /** - * Called to access the artist tag's primary key - * @return "Album::year"."Album::name" (Example: 1968.White Album) - */ - const std::string primary_key() const; - - /** - * Read album information from file. - * @param file The file to read from. - */ - void read(File &); - - /** - * Write album information to file. - * @param file The file to write to. - */ - void write(File &); -}; - - /** * Genre tag */ diff --git a/include/core/tags/album.h b/include/core/tags/album.h new file mode 100644 index 00000000..ffae9485 --- /dev/null +++ b/include/core/tags/album.h @@ -0,0 +1,67 @@ +/** + * @file + * Copyright 2014 (c) Anna Schumaker. + */ +#ifndef OCARINA_CORE_TAGS_ALBUM_H +#define OCARINA_CORE_TAGS_ALBUM_H + +#include + +/** + * The Album tag is used to store the name and year of albums + * added to the tag database. + * + * When writing an Album tag to disk, write out the _year field and + * then call GenericTag to write anything else. + * + * ... << year1 << GenericTag::write() + * ... << year2 << GenericTag::write() + * ... << year3 << GenericTag::write() + */ +class Album : public GenericTag { +private: + unsigned int _year; /**< The year associated with this Album. */ + +public: + Album(); /**< Album tag constructor */ + + /** + * Album tag constructor + * + * @param name Album name + * @param year Album year + */ + Album(const std::string &, unsigned int); + + /** + * The album's primary key is the concatenation of year + * and name, allowing for multiple albums with the same + * name but released at different times. + * + * @return Album::_year / GenericTag::primary_key() (Example: "1998/Hyrule Symphony") + */ + const std::string primary_key() const; + + /** + * Read album information from file. + * + * @param file The file to read from. + */ + void read(File &); + + /** + * Write album information to file. + * + * @param file The file to write to. + */ + void write(File &); + + /** + * Called to access the year associated with this album. + * + * @return Album::_year. + */ + unsigned int year(); +}; + +#endif /* OCARINA_CORE_TAGS_ALBUM_H */ diff --git a/include/core/tags/generic.h b/include/core/tags/generic.h index 2c5f4f27..a55461cf 100644 --- a/include/core/tags/generic.h +++ b/include/core/tags/generic.h @@ -61,7 +61,7 @@ public: * * @return GenericTag::_name. */ - const std::string &name(); + const std::string &name() const; /** * Called to access the lowercase form of ::_name. diff --git a/lib/model.cpp b/lib/model.cpp index 764c7c95..1515a16c 100644 --- a/lib/model.cpp +++ b/lib/model.cpp @@ -121,7 +121,7 @@ void QueueModel::get_value_uint(Track *track, int column, specific.set(track->track); break; case 5: - specific.set(track->album->year); + specific.set(track->album->year()); break; case 7: specific.set(track->play_count); @@ -149,7 +149,7 @@ void QueueModel::get_value_str(Track *track, int column, specific.set(track->artist->name()); break; case 4: - specific.set(track->album->name); + specific.set(track->album->name()); break; case 6: specific.set(track->genre->name); diff --git a/tests/core/Sconscript b/tests/core/Sconscript index 191309e0..b00bd8d3 100644 --- a/tests/core/Sconscript +++ b/tests/core/Sconscript @@ -16,6 +16,7 @@ test( "idle" ) test( "tags/generic" ) test( "tags/artist" ) +test( "tags/album" ) test_env.UsePackage("taglib") objs += [ get_test_obj("tags", "core") ] diff --git a/tests/core/tags/.gitignore b/tests/core/tags/.gitignore index cd17a1bc..183d6287 100644 --- a/tests/core/tags/.gitignore +++ b/tests/core/tags/.gitignore @@ -1,2 +1,3 @@ +album artist generic diff --git a/tests/core/tags/album.cpp b/tests/core/tags/album.cpp new file mode 100644 index 00000000..8bdfedd7 --- /dev/null +++ b/tests/core/tags/album.cpp @@ -0,0 +1,42 @@ +/** + * @file + * Copyright 2014 (c) Anna Schumaker. + */ +#include +#include + +static void test_album_tag() +{ + Album album("Hyrule Symphony", 1998); + File f("album_tag", 0); + + test_equal(album.name(), (std::string)"Hyrule Symphony"); + test_equal(album.lowercase(), (std::string)"hyrule symphony"); + test_equal(album.year(), (unsigned int)1998); + test_equal(album.primary_key(), (std::string)"1998/Hyrule Symphony"); + + f.open(OPEN_WRITE); + album.write(f); + f.close(); + + album = Album(); + test_equal(album.name(), (std::string)""); + test_equal(album.lowercase(), (std::string)""); + test_equal(album.year(), (unsigned int)0); + test_equal(album.primary_key(), (std::string)"0/"); + + f.open(OPEN_READ); + album.read(f); + f.close(); + + test_equal(album.name(), (std::string)"Hyrule Symphony"); + test_equal(album.lowercase(), (std::string)"hyrule symphony"); + test_equal(album.year(), (unsigned int)1998); + test_equal(album.primary_key(), (std::string)"1998/Hyrule Symphony"); +} + +int main(int argc, char **argv) +{ + run_test("Album Tag Test", test_album_tag); + return 0; +} diff --git a/tests/lib/.gitignore b/tests/lib/.gitignore index 7eb63247..bd99e9a8 100644 --- a/tests/lib/.gitignore +++ b/tests/lib/.gitignore @@ -1,3 +1,4 @@ lib colmgr model +tags/