From fe3d7867d118ec5e43b6871ed71c511ec5aaf6eb Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Thu, 22 Oct 2015 13:19:09 -0400 Subject: [PATCH] core/tags/artist: Directly inherit from DatabaseEntry Signed-off-by: Anna Schumaker --- core/queue.cpp | 2 +- core/tags/artist.cpp | 39 ++++++++++++++++++++++++++++++++++++-- core/tags/track.cpp | 4 ++-- gui/gst.cpp | 2 +- gui/queue/model.cpp | 2 +- include/core/tags/artist.h | 14 ++++++++++++-- tests/core/tags/artist.cpp | 14 +++++++------- 7 files changed, 61 insertions(+), 16 deletions(-) diff --git a/core/queue.cpp b/core/queue.cpp index e05aa4b4..940b0c28 100644 --- a/core/queue.cpp +++ b/core/queue.cpp @@ -80,7 +80,7 @@ static inline int track_compare(Track *lhs, Track *rhs, sort_t field) { switch (field) { case SORT_ARTIST: - return lhs->artist()->compare(rhs->artist()); + return artist_compare(lhs->artist(), rhs->artist()); case SORT_COUNT: return lhs->count() - rhs->count(); case SORT_GENRE: diff --git a/core/tags/artist.cpp b/core/tags/artist.cpp index 487a05ce..5f1c2ae0 100644 --- a/core/tags/artist.cpp +++ b/core/tags/artist.cpp @@ -1,17 +1,47 @@ /** * Copyright 2014 (c) Anna Schumaker. */ +extern "C" { +#include +} #include static database artist_db; -artist :: artist() : GenericTag() {} +artist :: artist() {} artist :: artist(const std::string &name) - : GenericTag(name) { + gchar *lower = string_lowercase(name.c_str()); + + ar_name = name; + ar_lower = lower; + + g_free(lower); +} + +const std::string artist :: primary_key() const +{ + return ar_name; +} + +void artist :: read(file &file) +{ + gchar *name = file_readl(&file); + gchar *g_lc = string_lowercase(name); + + ar_name = name; + ar_lower = g_lc; + + g_free(name); + g_free(g_lc); +} + +void artist :: write(file &file) +{ + file_writef(&file, "%s", ar_name.c_str()); } @@ -35,3 +65,8 @@ struct artist *artist_get(const unsigned int index) { return db_at(&artist_db, index); } + +int artist_compare(struct artist *lhs, struct artist *rhs) +{ + return string_compare(lhs->ar_lower.c_str(), rhs->ar_lower.c_str()); +} diff --git a/core/tags/track.cpp b/core/tags/track.cpp index 8dd10283..6c65f783 100644 --- a/core/tags/track.cpp +++ b/core/tags/track.cpp @@ -39,7 +39,7 @@ Track :: Track(struct album *album, struct artist *artist, Genre *genre, { date_set(&_date, 0, 0, 0); filter :: add(this->name(), index()); - filter :: add(_artist->name(), index()); + filter :: add(_artist->ar_name, index()); filter :: add(_album->al_name, index()); _library->inc_size(); } @@ -126,7 +126,7 @@ void Track :: read(file &file) _genre = tags :: get_genre(genre_id); filter :: add(name(), index()); - filter :: add(_artist->name(), index()); + filter :: add(_artist->ar_name, index()); filter :: add(_album->al_name, index()); _library->inc_size(); } diff --git a/gui/gst.cpp b/gui/gst.cpp index 2d8ec86f..3ab041fa 100644 --- a/gui/gst.cpp +++ b/gui/gst.cpp @@ -67,7 +67,7 @@ public: g_free(uri); set_markup(o_album, "x-large", "From: " + track->album()->al_name); - set_markup(o_artist, "x-large", "By: " + track->artist()->name()); + set_markup(o_artist, "x-large", "By: " + track->artist()->ar_name); set_markup(o_title, "xx-large", track->name()); o_duration->set_text(len); g_free(len); diff --git a/gui/queue/model.cpp b/gui/queue/model.cpp index 5fb2585a..61deb0d1 100644 --- a/gui/queue/model.cpp +++ b/gui/queue/model.cpp @@ -117,7 +117,7 @@ void QueueModel::get_value_vfunc(const Gtk::TreeIter &iter, int column, g_free(str); return; case 3: - return set_val(track->artist()->name(), value); + return set_val(track->artist()->ar_name, value); case 4: return set_val(track->album()->al_name, value); case 5: diff --git a/include/core/tags/artist.h b/include/core/tags/artist.h index d8e41ba3..0ece1247 100644 --- a/include/core/tags/artist.h +++ b/include/core/tags/artist.h @@ -4,13 +4,16 @@ #ifndef OCARINA_CORE_TAGS_ARTIST_H #define OCARINA_CORE_TAGS_ARTIST_H -#include +#include /** * The Artist tag is used to store the name of artists added * to the tag database. */ -struct artist : public GenericTag { +struct artist : public DatabaseEntry { + std::string ar_name; /* This artist's name. */ + std::string ar_lower; /* This artist's name (lowercased). */ + artist(); /**< Artist tag constructor. */ /** @@ -19,6 +22,10 @@ struct artist : public GenericTag { * @param name Artist name. */ artist(const std::string &); + + const std::string primary_key() const; + void read(file &); + void write(file &); }; @@ -34,4 +41,7 @@ struct artist *artist_find(const std::string &); /* Called to get an artist tag with a specific index. */ struct artist *artist_get(const unsigned int); +/* Called to compare two artist tags. */ +int artist_compare(struct artist *, struct artist *); + #endif /* OCARINA_CORE_TAGS_ARTIST_H */ diff --git a/tests/core/tags/artist.cpp b/tests/core/tags/artist.cpp index af7097a3..b2d50f7a 100644 --- a/tests/core/tags/artist.cpp +++ b/tests/core/tags/artist.cpp @@ -6,15 +6,15 @@ static void test_verify_empty(struct artist *artist) { - test_equal(artist->name(), ""); - test_equal(artist->lowercase(), ""); + test_equal(artist->ar_name, ""); + test_equal(artist->ar_lower, ""); test_equal(artist->primary_key(), ""); } static void test_verify_koji(struct artist *artist) { - test_equal(artist->name(), "Koji Kondo"); - test_equal(artist->lowercase(), "koji kondo"); + test_equal(artist->ar_name, "Koji Kondo"); + test_equal(artist->ar_lower, "koji kondo"); test_equal(artist->primary_key(), "Koji Kondo"); } @@ -53,9 +53,9 @@ static void test_artist_compare() struct artist *koji = new struct artist("Koji Kondo"); struct artist *hajime = new struct artist("hajime wakai"); - test_equal(koji->compare(koji), 0); - test_equal(koji->compare(hajime), 1); - test_equal(hajime->compare(koji), -1); + test_equal(artist_compare(koji, koji), 0); + test_equal(artist_compare(koji, hajime), 1); + test_equal(artist_compare(hajime, koji), -1); delete hajime; delete koji;