Genre: Add functions for looking up or creating Genre tags

The genre_db should really be controlled from within the Genre tag,
so this patch adds functions to find or create Genre tags to the tags
namespace.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2014-11-16 15:15:32 -05:00
parent 7d3e9a3d28
commit 8545837672
5 changed files with 71 additions and 4 deletions

View File

@ -12,7 +12,6 @@
#include <taglib/tag.h>
#include <taglib/fileref.h>
Database<Genre> genre_db("genre.db", true);
Database<Library> library_db("library.db", true);
Database<Track> track_db("track.db", false);
@ -58,7 +57,7 @@ void Track :: read(File &f)
library = library_db.at(library_id);
artist = tags :: get_artist(artist_id);
album = tags :: get_album(album_id);
genre = genre_db.at(genre_id);
genre = tags :: get_genre(genre_id);
title_lower = filter :: add(title, index());
filter :: add(artist->name(), index());
@ -120,7 +119,7 @@ bool Track :: tag()
artist = tags :: get_artist(format_tag(tag->artist()));
album = tags :: get_album(format_tag(tag->album()), tag->year());
genre = find_or_insert(Genre(format_tag(tag->genre())), genre_db);
genre = tags :: get_genre(format_tag(tag->genre()));
track = tag->track();
length = audio->length();
title = format_tag(tag->title());
@ -219,7 +218,6 @@ int Track :: less_than(Track *rhs, sort_t field)
void tagdb :: init()
{
tags :: init();
genre_db.load();
library_db.load();
track_db.load();
}

View File

@ -4,9 +4,32 @@
*/
#include <core/tags/genre.h>
static Database<Genre> genre_db("genre.db", true);
Genre :: Genre() : GenericTag() {}
Genre :: Genre(const std::string &name)
: GenericTag(name)
{
}
void tags :: init_genre_db()
{
genre_db.load();
}
Genre *tags :: get_genre(const std::string &name)
{
Genre *ret = genre_db.find(name);
if (ret)
return ret;
return genre_db.insert(Genre(name));
}
Genre *tags :: get_genre(const unsigned int index)
{
return genre_db.at(index);
}

View File

@ -4,10 +4,12 @@
*/
#include <core/tags/album.h>
#include <core/tags/artist.h>
#include <core/tags/genre.h>
#include <core/tags/tags.h>
void tags :: init()
{
tags :: init_album_db();
tags :: init_artist_db();
tags :: init_genre_db();
}

View File

@ -23,4 +23,30 @@ public:
Genre(const std::string &);
};
namespace tags
{
/** Called to read the genre_db from disk. */
void init_genre_db();
/**
* Called to look up a Genre tag by name. If no existing
* tag is found a new one will be created and returned to
* the caller.
*
* @param name The name of the genre.
* @return A matching Genre tag.
*/
Genre *get_genre(const std::string &);
/**
* Called to look up a Genre tag by tag index.
*
* @param index The index of the Genre tag.
* @return A matching Genre tag or NULL.
*/
Genre *get_genre(const unsigned int);
}
#endif /* OCARINA_CORE_TAGS_GENRE_H */

View File

@ -18,8 +18,26 @@ static void test_artist_tag()
test_equal(genre.primary_key(), (std::string)"Video Game Music");
}
static void test_genere_tag_lookup()
{
Database<Genre> genre_db("genre.db", false);
Genre *genre = tags :: get_genre("Video Game Music");
test_equal(genre->name(), (std::string)"Video Game Music");
test_equal(genre->lowercase(), (std::string)"video game music");
test_equal(genre->primary_key(), (std::string)"Video Game Music");
test_equal(tags :: get_genre("Video Game Music"), genre);
test_equal(tags :: get_genre(0), genre);
test_equal(tags :: get_genre(1), (Genre *)NULL);
genre_db.load();
test_equal(genre_db.size(), (unsigned)1);
}
int main(int argc, char **argv)
{
run_test("Genre Tag Test", test_artist_tag);
run_test("Genre Tag Lookup Test", test_genere_tag_lookup);
return 0;
}