core/tags/album: Directly inherit from DatabaseEntry

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-10-22 09:58:12 -04:00
parent a7746cf901
commit e5b0047812
7 changed files with 48 additions and 28 deletions

View File

@ -97,7 +97,7 @@ static inline int track_compare(Track *lhs, Track *rhs, sort_t field)
if (lhs->album()->al_year - rhs->album()->al_year != 0)
return lhs->album()->al_year - rhs->album()->al_year;
case SORT_ALBUM:
return lhs->album()->compare(rhs->album());
return album_compare(lhs->album(), rhs->album());
}
return 0;
}

View File

@ -5,7 +5,6 @@ extern "C" {
#include <core/string.h>
}
#include <core/tags/album.h>
#include <glib.h>
static database<album> album_db;
@ -20,21 +19,24 @@ static const std::string make_key(const std::string &name, unsigned int year)
}
album :: album() : GenericTag(), al_year(0) {}
album :: album() : al_year(0), al_name("") {}
album :: album(const std::string &name, unsigned int year)
: GenericTag(name), al_year(year)
: al_year(year), al_name(name)
{
gchar *lower = string_lowercase(al_name.c_str());
al_lower = lower;
g_free(lower);
}
album :: album(const std::string &key)
{
gchar *name, *lower;
sscanf(key.c_str(), "%u/%m[^\n]", &al_year, &name);
sscanf(key.c_str(), "%u/%m[^\n]\n", &al_year, &name);
lower = string_lowercase(name);
_name = name;
_lower = lower;
lower = string_lowercase(name);
al_name = name;
al_lower = lower;
g_free(name);
g_free(lower);
@ -42,19 +44,27 @@ album :: album(const std::string &key)
const std::string album :: primary_key() const
{
return make_key(GenericTag :: primary_key(), al_year);
return make_key(al_name, al_year);
}
void album :: read(file &file)
{
gchar *name, *lower;
file_readf(&file, "%u", &al_year);
GenericTag :: read(file);
name = file_readl(&file);
lower = string_lowercase(name);
al_name = name;
al_lower = lower;
g_free(name);
g_free(lower);
}
void album :: write(file &file)
{
file_writef(&file, "%u ", al_year);
GenericTag :: write(file);
file_writef(&file, "%u %s", al_year, al_name.c_str());
}
@ -78,3 +88,8 @@ struct album *album_get(const unsigned int index)
{
return db_at(&album_db, index);
}
int album_compare(struct album *lhs, struct album *rhs)
{
return string_compare(lhs->al_lower.c_str(), rhs->al_lower.c_str());
}

View File

@ -40,7 +40,7 @@ Track :: Track(struct album *album, Artist *artist, Genre *genre, Library *libra
date_set(&_date, 0, 0, 0);
filter :: add(this->name(), index());
filter :: add(_artist->name(), index());
filter :: add(_album->name(), index());
filter :: add(_album->al_name, index());
_library->inc_size();
}
@ -127,7 +127,7 @@ void Track :: read(file &file)
filter :: add(name(), index());
filter :: add(_artist->name(), index());
filter :: add(_album->name(), index());
filter :: add(_album->al_name, index());
_library->inc_size();
}

View File

@ -66,7 +66,7 @@ public:
g_object_set(G_OBJECT(gst_player), "uri", uri, NULL);
g_free(uri);
set_markup(o_album, "x-large", "From: " + track->album()->name());
set_markup(o_album, "x-large", "From: " + track->album()->al_name);
set_markup(o_artist, "x-large", "By: " + track->artist()->name());
set_markup(o_title, "xx-large", track->name());
o_duration->set_text(len);

View File

@ -119,7 +119,7 @@ void QueueModel::get_value_vfunc(const Gtk::TreeIter &iter, int column,
case 3:
return set_val(track->artist()->name(), value);
case 4:
return set_val(track->album()->name(), value);
return set_val(track->album()->al_name, value);
case 5:
return set_val(track->album()->al_year, value);
case 6:

View File

@ -4,7 +4,7 @@
#ifndef OCARINA_CORE_TAGS_ALBUM_H
#define OCARINA_CORE_TAGS_ALBUM_H
#include <core/tags/generic.h>
#include <core/database.h>
/**
* The Album tag is used to store the name and year of albums
@ -17,8 +17,10 @@
* ... << year2 << GenericTag::write()
* ... << year3 << GenericTag::write()
*/
struct album : public GenericTag {
unsigned int al_year; /* This album's year. */
struct album : public DatabaseEntry {
unsigned int al_year; /* This album's year. */
std::string al_name; /* This album's name. */
std::string al_lower; /* This album's name (lowercased). */
album(); /**< Album tag constructor */
@ -68,4 +70,7 @@ struct album *album_find(const std::string &, unsigned int);
/* Called to get an album tag with a specific index. */
struct album *album_get(const unsigned int);
/* Called to compare two album tags. */
int album_compare(struct album *, struct album *);
#endif /* OCARINA_CORE_TAGS_ALBUM_H */

View File

@ -6,17 +6,17 @@
static void test_verify_empty(struct album *album)
{
test_equal(album->name(), "");
test_equal(album->lowercase(), "");
test_equal(album->al_year, 0);
test_equal(album->al_name, "");
test_equal(album->al_lower, "");
test_equal(album->al_year, 0);
test_equal(album->primary_key(), "0/");
}
static void test_verify_hyrule(struct album *album)
{
test_equal(album->name(), "Hyrule Symphony");
test_equal(album->lowercase(), "hyrule symphony");
test_equal(album->al_year, 1998);
test_equal(album->al_name, "Hyrule Symphony");
test_equal(album->al_lower, "hyrule symphony");
test_equal(album->al_year, 1998);
test_equal(album->primary_key(), "1998/Hyrule Symphony");
}
@ -52,9 +52,9 @@ static void test_album_compare()
struct album *twilight = new struct album("Twilight Princess", 2006);
struct album *skyward = new struct album("skyward sword", 2011);
test_equal(twilight->compare(twilight), 0);
test_equal(twilight->compare(skyward), 1);
test_equal(skyward->compare(twilight), -1);
test_equal(album_compare(twilight, twilight), 0);
test_equal(album_compare(twilight, skyward), 1);
test_equal(album_compare(skyward, twilight), -1);
delete skyward;
delete twilight;