Album: Add functions for looking up or creating Album tags

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

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2014-11-16 14:47:51 -05:00
parent bac54857fd
commit 7d3e9a3d28
5 changed files with 81 additions and 7 deletions

View File

@ -12,7 +12,6 @@
#include <taglib/tag.h>
#include <taglib/fileref.h>
Database<Album> album_db("album.db", true);
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 = album_db.at(album_id);
album = tags :: get_album(album_id);
genre = genre_db.at(genre_id);
title_lower = filter :: add(title, index());
@ -120,7 +119,7 @@ bool Track :: tag()
audio = ref.audioProperties();
artist = tags :: get_artist(format_tag(tag->artist()));
album = find_or_insert(Album(format_tag(tag->album()), tag->year()), album_db);
album = tags :: get_album(format_tag(tag->album()), tag->year());
genre = find_or_insert(Genre(format_tag(tag->genre())), genre_db);
track = tag->track();
length = audio->length();
@ -220,7 +219,6 @@ int Track :: less_than(Track *rhs, sort_t field)
void tagdb :: init()
{
tags :: init();
album_db.load();
genre_db.load();
library_db.load();
track_db.load();

View File

@ -5,6 +5,17 @@
#include <core/tags/album.h>
#include <sstream>
static Database<Album> album_db("album.db", true);
static const std::string make_key(const std::string &name, unsigned int year)
{
std::stringstream ss;
ss << year << "/" << name;
return ss.str();
}
Album :: Album() : GenericTag(), _year(0) {}
Album :: Album(const std::string &name, unsigned int year)
@ -14,9 +25,7 @@ Album :: Album(const std::string &name, unsigned int year)
const std::string Album :: primary_key() const
{
std::stringstream ss;
ss << _year << "/" << GenericTag :: primary_key();
return ss.str();
return make_key(GenericTag :: primary_key(), _year);
}
void Album :: read(File &file)
@ -35,3 +44,22 @@ unsigned int Album :: year()
{
return _year;
}
void tags :: init_album_db()
{
album_db.load();
}
Album *tags :: get_album(const std::string &name, unsigned int year)
{
Album *ret = album_db.find(make_key(name, year));
if (ret)
return ret;
return album_db.insert(Album(name, year));
}
Album *tags ::get_album(const unsigned int index)
{
return album_db.at(index);
}

View File

@ -2,10 +2,12 @@
* @file
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/tags/album.h>
#include <core/tags/artist.h>
#include <core/tags/tags.h>
void tags :: init()
{
tags :: init_album_db();
tags :: init_artist_db();
}

View File

@ -64,4 +64,31 @@ public:
unsigned int year();
};
namespace tags
{
/** Called to read the album_db from disk. */
void init_album_db();
/**
* Called to look up an Album tag by name and year. If no
* existing tag is found a new one will be created and
* returned to the caller.
*
* @param name The name of the album.
* @param year The year of the album.
* @return A matching Album tag.
*/
Album *get_album(const std::string &, unsigned int);
/**
* Called to look up an Album tag by tag index.
*
* @param index The index of the Album tag.
* @return A matching Album tag or NULL.
*/
Album *get_album(const unsigned int);
} /* Namespace: tags */
#endif /* OCARINA_CORE_TAGS_ALBUM_H */

View File

@ -35,8 +35,27 @@ static void test_album_tag()
test_equal(album.primary_key(), (std::string)"1998/Hyrule Symphony");
}
static void test_album_tag_lookup()
{
Database<Album> album_db("album.db", false);
Album *album = tags :: get_album("Hyrule Symphony", 1998);
test_equal(album->name(), (std::string)"Hyrule Symphony");
test_equal(album->lowercase(), (std::string)"hyrule symphony");
test_equal(album->year(), (unsigned int)1998);
test_equal(album->primary_key(), (std::string)"1998/Hyrule Symphony");
test_equal(tags :: get_album("Hyrule Symphony", 1998), album);
test_equal(tags :: get_album(0), album);
test_equal(tags :: get_album(1), (Album *)NULL);
album_db.load();
test_equal(album_db.size(), (unsigned)1);
}
int main(int argc, char **argv)
{
run_test("Album Tag Test", test_album_tag);
run_test("Album Tag Lookup Test", test_album_tag_lookup);
return 0;
}