core/tags/genre: Directly inherit from DatabaseEntry

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-10-23 09:04:23 -04:00
parent 8aa05ecedc
commit cc3becd566
5 changed files with 59 additions and 14 deletions

View File

@ -84,7 +84,7 @@ static inline int track_compare(Track *lhs, Track *rhs, sort_t field)
case SORT_COUNT: case SORT_COUNT:
return lhs->count() - rhs->count(); return lhs->count() - rhs->count();
case SORT_GENRE: case SORT_GENRE:
return lhs->genre()->compare(rhs->genre()); return genre_compare(lhs->genre(), rhs->genre());
case SORT_LENGTH: case SORT_LENGTH:
return lhs->length() - rhs->length(); return lhs->length() - rhs->length();
case SORT_PLAYED: case SORT_PLAYED:

View File

@ -1,19 +1,49 @@
/** /**
* Copyright 2014 (c) Anna Schumaker. * Copyright 2014 (c) Anna Schumaker.
*/ */
extern "C" {
#include <core/string.h>
}
#include <core/tags/genre.h> #include <core/tags/genre.h>
static database<struct genre> genre_db; static database<struct genre> genre_db;
genre :: genre() {}
genre :: genre() : GenericTag() {}
genre :: genre(const std::string &name) genre :: genre(const std::string &name)
: GenericTag(name)
{ {
gchar *lower = string_lowercase(name.c_str());
ge_name = name;
ge_lower = lower;
g_free(lower);
} }
const std::string genre :: primary_key() const
{
return ge_name;
}
void genre :: read(file &file)
{
gchar *name = file_readl(&file);
gchar *g_lc = string_lowercase(name);
ge_name = name;
ge_lower = g_lc;
g_free(name);
g_free(g_lc);
}
void genre :: write(file &file)
{
file_writef(&file, "%s", ge_name.c_str());
}
void genre_db_init() void genre_db_init()
{ {
db_init(&genre_db, "genre.db", true); db_init(&genre_db, "genre.db", true);
@ -34,3 +64,8 @@ struct genre *genre_get(const unsigned int index)
{ {
return db_at(&genre_db, index); return db_at(&genre_db, index);
} }
int genre_compare(struct genre *lhs, struct genre *rhs)
{
return string_compare(lhs->ge_lower.c_str(), rhs->ge_lower.c_str());
}

View File

@ -123,7 +123,7 @@ void QueueModel::get_value_vfunc(const Gtk::TreeIter &iter, int column,
case 5: case 5:
return set_val(track->album()->al_year, value); return set_val(track->album()->al_year, value);
case 6: case 6:
return set_val(track->genre()->name(), value); return set_val(track->genre()->ge_name, value);
case 7: case 7:
return set_val(track->count(), value); return set_val(track->count(), value);
case 8: case 8:

View File

@ -4,14 +4,17 @@
#ifndef OCARINA_CORE_TAGS_GENRE_H #ifndef OCARINA_CORE_TAGS_GENRE_H
#define OCARINA_CORE_TAGS_GENRE_H #define OCARINA_CORE_TAGS_GENRE_H
#include <core/tags/generic.h> #include <core/database.h>
/** /**
* The Genre tag is used to store the name of genres added * The Genre tag is used to store the name of genres added
* to the tag database. * to the tag database.
*/ */
struct genre : public GenericTag { struct genre : public DatabaseEntry {
public: public:
std::string ge_name; /* This genre's name. */
std::string ge_lower; /* This genre's name (lowercased). */
genre(); /**< Genre tag constructor. */ genre(); /**< Genre tag constructor. */
/** /**
@ -20,6 +23,10 @@ public:
* @param name Genre name. * @param name Genre name.
*/ */
genre(const std::string &); genre(const std::string &);
const std::string primary_key() const;
void read(file &);
void write(file &);
}; };
@ -35,4 +42,7 @@ struct genre *genre_find(const std::string &);
/* Called to get a genre tag with a specific index. */ /* Called to get a genre tag with a specific index. */
struct genre *genre_get(const unsigned int); struct genre *genre_get(const unsigned int);
/* Called to compare two genre tags. */
int genre_compare(struct genre *, struct genre *);
#endif /* OCARINA_CORE_TAGS_GENRE_H */ #endif /* OCARINA_CORE_TAGS_GENRE_H */

View File

@ -6,15 +6,15 @@
static void test_verify_empty(struct genre *genre) static void test_verify_empty(struct genre *genre)
{ {
test_equal(genre->name(), ""); test_equal(genre->ge_name, "");
test_equal(genre->lowercase(), ""); test_equal(genre->ge_lower, "");
test_equal(genre->primary_key(), ""); test_equal(genre->primary_key(), "");
} }
static void test_verify_vg(struct genre *genre) static void test_verify_vg(struct genre *genre)
{ {
test_equal(genre->name(), "Video Game Music"); test_equal(genre->ge_name, "Video Game Music");
test_equal(genre->lowercase(), "video game music"); test_equal(genre->ge_lower, "video game music");
test_equal(genre->primary_key(), "Video Game Music"); test_equal(genre->primary_key(), "Video Game Music");
} }
@ -53,9 +53,9 @@ static void test_genre_compare()
struct genre *video = new struct genre("Video Game Music"); struct genre *video = new struct genre("Video Game Music");
struct genre *game = new struct genre("game music"); struct genre *game = new struct genre("game music");
test_equal(video->compare(video), 0); test_equal(genre_compare(video, video), 0);
test_equal(video->compare(game), 1); test_equal(genre_compare(video, game), 1);
test_equal(game->compare(video), -1); test_equal(genre_compare(game, video), -1);
delete game; delete game;
delete video; delete video;