Artist: Add functions for looking up or creating Artist tags

The artist_db should really be controlled from within the Artist tag, so
this patch creates a new tags namespace containing functions that will
find or create tags as they are requested.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2014-11-16 10:08:12 -05:00
parent d88f008728
commit bac54857fd
8 changed files with 104 additions and 6 deletions

View File

@ -4,6 +4,7 @@
*/
#include <core/tags.h>
#include <core/tags/tags.h>
#include <core/filter.h>
#include <core/print.h>
@ -11,7 +12,6 @@
#include <taglib/tag.h>
#include <taglib/fileref.h>
Database<Artist> artist_db("artist.db", true);
Database<Album> album_db("album.db", true);
Database<Genre> genre_db("genre.db", true);
Database<Library> library_db("library.db", true);
@ -57,7 +57,7 @@ void Track :: read(File &f)
filepath = f.getline();
library = library_db.at(library_id);
artist = artist_db.at(artist_id);
artist = tags :: get_artist(artist_id);
album = album_db.at(album_id);
genre = genre_db.at(genre_id);
@ -119,7 +119,7 @@ bool Track :: tag()
tag = ref.tag();
audio = ref.audioProperties();
artist = find_or_insert(Artist(format_tag(tag->artist())), artist_db);
artist = tags :: get_artist(format_tag(tag->artist()));
album = find_or_insert(Album(format_tag(tag->album()), tag->year()), album_db);
genre = find_or_insert(Genre(format_tag(tag->genre())), genre_db);
track = tag->track();
@ -219,7 +219,7 @@ int Track :: less_than(Track *rhs, sort_t field)
void tagdb :: init()
{
artist_db.load();
tags :: init();
album_db.load();
genre_db.load();
library_db.load();

View File

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

11
core/tags/tags.cpp Normal file
View File

@ -0,0 +1,11 @@
/**
* @file
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/tags/artist.h>
#include <core/tags/tags.h>
void tags :: init()
{
tags :: init_artist_db();
}

View File

@ -23,4 +23,30 @@ public:
Artist(const std::string &);
};
namespace tags
{
/** Called to read the artist_db from disk. */
void init_artist_db();
/**
* Called to look up an Artist 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 artist.
* @return A matching Artist tag.
*/
Artist *get_artist(const std::string &);
/**
* Called to look up an Artist tag by tag index.
*
* @param index The index of the Artist tag.
* @return A matching Artist tag or NULL.
*/
Artist *get_artist(const unsigned int);
} /* Namespace: tags */
#endif /* OCARINA_CORE_TAGS_ARTIST_H */

View File

@ -23,7 +23,7 @@
class GenericTag : public DatabaseEntry {
private:
std::string _name; /**< The name associated with this tag. */
std::string _lower; /**< The lowercase form of ::_name. */
std::string _lower; /**< The lowercase form of GenericTag::_name. */
public:
GenericTag(); /**< GenericTag constructor. */
@ -64,7 +64,7 @@ public:
const std::string &name() const;
/**
* Called to access the lowercase form of ::_name.
* Called to access the lowercase form of GenericTag::_name.
*
* @return GenericTag::_lower.
*/

19
include/core/tags/tags.h Normal file
View File

@ -0,0 +1,19 @@
/**
* @file
* Copyright 2014 (c) Anna Schumaker.
*/
#ifndef OCARINA_CORE_TAGS_TAGS_H
#define OCARINA_CORE_TAGS_TAGS_H
/**
* Namespace for creating and accessing tags.
*/
namespace tags
{
/** Called to read all databases from disk. */
void init();
}
#endif /* OCARINA_CORE_TAGS_TAGS_H */

View File

@ -22,6 +22,7 @@ test( "tags/library" )
test_env.UsePackage("taglib")
objs += [ get_test_obj("tags", "core") ]
objs += [ get_test_obj("tags/tags", "core") ]
#test( "tags" )
test( "random" )

View File

@ -18,8 +18,26 @@ static void test_artist_tag()
test_equal(artist.primary_key(), (std::string)"Koji Kondo");
}
static void test_artist_tag_lookup()
{
Database<Artist> artist_db("artist.db", false);
Artist *artist = tags :: get_artist("Koji Kondo");
test_equal(artist->name(), (std::string)"Koji Kondo");
test_equal(artist->lowercase(), (std::string)"koji kondo");
test_equal(artist->primary_key(), (std::string)"Koji Kondo");
test_equal(tags :: get_artist("Koji Kondo"), artist);
test_equal(tags :: get_artist(0), artist);
test_equal(tags :: get_artist(1), (Artist *)NULL);
artist_db.load();
test_equal(artist_db.size(), (unsigned)1);
}
int main(int argc, char **argv)
{
run_test("Artist Tag Test", test_artist_tag);
run_test("Artist Tag Lookup Test", test_artist_tag_lookup);
return 0;
}