Library: Add functions for looking up or creating Library tags

This patch moves the library_db into tags/library.cpp, where it can be
effectively managed by the Library tag.  For this to work, I need to add
some extra functions to the tags namespace to:

	- Create new Library tags,
	- Find tags by index,
	- Remove the Library at a specific index, and
	- Find the actual size of the library_db.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2014-11-17 08:30:54 -05:00
parent 8545837672
commit f433ced9cb
10 changed files with 123 additions and 61 deletions

View File

@ -156,7 +156,7 @@ Library *library :: add(const std::string &dir)
if (g_file_test(dir.c_str(), G_FILE_TEST_IS_DIR) == false)
return library;
library = tagdb :: add_library(dir);
library = tags :: get_library(dir);
if (library)
update(library);
return library;
@ -185,11 +185,13 @@ void library :: update(Library *library)
void library :: update_all()
{
Database<Library>::iterator it;
Database<Library> *db = &tagdb :: get_library_db();
Library *library;
for (it = db->begin(); it != db->end(); it = db->next(it))
update(*it);
for (unsigned int i = 0; i < tags :: library_size(); i++) {
library = tags :: get_library(i);
if (library)
update(library);
}
}
void library :: set_enabled(Library *library, bool enabled)
@ -201,7 +203,6 @@ void library :: set_enabled(Library *library, bool enabled)
return;
library->set_enabled(enabled);
tagdb :: commit_library();
for (it = db->begin(); it != db->end(); it = db->next(it)) {
if ((*it)->library == library) {

View File

@ -12,7 +12,6 @@
#include <taglib/tag.h>
#include <taglib/fileref.h>
Database<Library> library_db("library.db", true);
Database<Track> track_db("track.db", false);
@ -54,7 +53,7 @@ void Track :: read(File &f)
title = f.getline();
filepath = f.getline();
library = library_db.at(library_id);
library = tags :: get_library(library_id);
artist = tags :: get_artist(artist_id);
album = tags :: get_album(album_id);
genre = tags :: get_genre(genre_id);
@ -218,7 +217,6 @@ int Track :: less_than(Track *rhs, sort_t field)
void tagdb :: init()
{
tags :: init();
library_db.load();
track_db.load();
}
@ -227,11 +225,6 @@ void tagdb :: commit()
track_db.save();
}
void tagdb :: commit_library()
{
library_db.save();
}
Track *tagdb :: add_track(const std::string &filepath, Library *library)
{
Track *track = track_db.insert(Track(filepath, library));
@ -242,11 +235,6 @@ Track *tagdb :: add_track(const std::string &filepath, Library *library)
return track;
}
Library *tagdb :: add_library(const std::string &filepath)
{
return library_db.insert(Library(filepath));
}
void tagdb :: remove_track(unsigned int track_id)
{
track_db.remove(track_id);
@ -260,7 +248,7 @@ void tagdb :: remove_library(unsigned int library_id)
track_db.remove((*it)->index());
}
tagdb :: commit();
library_db.remove(library_id);
tags :: remove_library(tags :: get_library(library_id));
}
Track *tagdb :: lookup(unsigned int track_id)
@ -268,17 +256,7 @@ Track *tagdb :: lookup(unsigned int track_id)
return track_db.at(track_id);
}
Library *tagdb :: lookup_library(unsigned int library_id)
{
return library_db.at(library_id);
}
Database<Track> &tagdb :: get_track_db()
{
return track_db;
}
Database<Library> &tagdb :: get_library_db()
{
return library_db;
}

View File

@ -4,6 +4,10 @@
*/
#include <core/tags/library.h>
static Database<Library> library_db("library.db", true);
Library :: Library()
: _size(0), _enabled(false)
{
@ -38,6 +42,7 @@ const bool Library :: enabled()
void Library :: set_enabled(bool enabled)
{
_enabled = enabled;
library_db.save();
}
const unsigned int Library :: size()
@ -54,3 +59,33 @@ void Library :: dec_size()
{
_size--;
}
void tags :: init_library_db()
{
library_db.load();
}
Library *tags :: get_library(const std::string &path)
{
Library *ret = library_db.find(path);
if (ret)
return ret;
return library_db.insert(Library(path));
}
Library *tags :: get_library(const unsigned int index)
{
return library_db.at(index);
}
void tags :: remove_library(Library *library)
{
if (library)
library_db.remove(library->index());
}
unsigned int tags :: library_size()
{
return library_db.actual_size();
}

View File

@ -5,6 +5,7 @@
#include <core/tags/album.h>
#include <core/tags/artist.h>
#include <core/tags/genre.h>
#include <core/tags/library.h>
#include <core/tags/tags.h>
void tags :: init()
@ -12,4 +13,5 @@ void tags :: init()
tags :: init_album_db();
tags :: init_artist_db();
tags :: init_genre_db();
tags :: init_library_db();
}

View File

@ -137,8 +137,6 @@ namespace tagdb
void init();
/** Write track database to disk */
void commit();
/** Write library database to disk */
void commit_library();
/**
@ -149,13 +147,6 @@ namespace tagdb
*/
Track *add_track(const std::string &, Library *);
/**
* Add a new library to the database
* @param filepath Path of the directory to be added
* @return A pointer to the newly created library
*/
Library *add_library(const std::string &);
/**
* Remove a track from the database
* @param track_id The index of the track in the database.
@ -175,25 +166,12 @@ namespace tagdb
*/
Track *lookup(unsigned int);
/**
* Find a library based on library_id
* @param library_id The library id to look up
* @return A pointer to the found library, or NULL
*/
Library *lookup_library(unsigned int);
/**
* Call to access the track database
* @return The track database
*/
Database<Track> &get_track_db();
/**
* Call to access the library database
* @return The library database
*/
Database<Library> &get_library_db();
}
#endif /* OCARINA_CORE_TAGS_H */

View File

@ -6,6 +6,7 @@
#define OCARINA_CORE_TAGS_LIBRARY_H
#include <core/database.h>
#include <core/tags/generic.h>
/**
* The Library tag is used to store a single directory added
@ -88,4 +89,44 @@ public:
void dec_size();
};
namespace tags
{
/** Called to read the library_db from disk. */
void init_library_db();
/**
* Called to look up a Library tag by library path. If no
* existing tag is found a new one will be created and
* returned to the caller.
*
* @param path The path to the library directory on disk.
* @return A matching Library tag.
*/
Library *get_library(const std::string &);
/**
* Called to look up a Library tag by tag index.
*
* @param index The index of the Library tag.
* @return A matching Library tag.
*/
Library *get_library(const unsigned int);
/**
* Called to remove a specific Library tag.
*
* @param library The Library tag to remove.
*/
void remove_library(Library *);
/**
* Called to find the number of rows in the library_db,
* including NULL rows.
*
* @return The Database::actual_size() of the library_db.
*/
unsigned int library_size();
}
#endif /* OCARINA_CORE_TAGS_LIBRARY_H */

View File

@ -32,7 +32,7 @@ static Glib::RefPtr<Gtk::ListStore> c_list;
static Library *find_library(const Gtk::TreeModel::Row &row)
{
return tagdb :: lookup_library(row[c_cols.c_id]);
return tags :: get_library(row[c_cols.c_id]);
}
static void list_path(Library *lib)
@ -63,14 +63,16 @@ static void remove_banned_tracks()
void colmgr :: init()
{
Database<Library>::iterator it;
Database<Library> *db = &tagdb :: get_library_db();
Library *library;
c_list = lib :: get_object<Gtk::ListStore>("colmgr_list");
c_list->set_sort_column(c_cols.c_path, Gtk::SORT_ASCENDING);
for (it = db->begin(); it != db->end(); it != db->next(it))
list_path(*it);
for (unsigned int i = 0; i < tags :: library_size(); i++) {
library = tags :: get_library(i);
if (library)
list_path(library);
}
}
void colmgr :: add_path(const std::string &path)

View File

@ -25,7 +25,7 @@ static void test_init()
static void test_enable()
{
Queue *q = library :: get_queue();
Library *library = tagdb :: lookup_library(0);
Library *library = tags :: get_library(0);
library :: set_enabled(LIB_NULL, true);
test_equal(q->size(), (unsigned)24);
@ -49,7 +49,7 @@ static void test_enable()
static void test_remove()
{
Queue *q = library :: get_queue();
Library *library = tagdb :: lookup_library(0);
Library *library = tags :: get_library(0);
library :: remove(LIB_NULL);
test_equal(q->size(), (unsigned)24);

View File

@ -42,8 +42,33 @@ static void test_library_tag()
test_equal(library.enabled(), true);
}
static void test_library_tag_lookup()
{
Database<Library> library_db("library.db", false);
Library *library;
test_equal(tags :: library_size(), (unsigned)0);
library = tags :: get_library("/home/Zelda/Music");
test_equal(library->primary_key(), (std::string)"/home/Zelda/Music");
test_equal(library->size(), (unsigned)0);
test_equal(library->enabled(), true);
test_equal(tags :: get_library("/home/Zelda/Music"), library);
test_equal(tags :: get_library(0), library);
test_equal(tags :: get_library(1), (Library *)NULL);
library_db.load();
test_equal(library_db.actual_size(), (unsigned)tags :: library_size());
tags :: remove_library(library);
test_equal(tags :: get_library(0), (Library *)NULL);
test_equal(tags :: library_size(), (unsigned)1);
}
int main(int argc, char **argv)
{
run_test("Library Tag Test", test_library_tag);
run_test("Library Tag Lookup Test", test_library_tag_lookup);
return 0;
}

View File

@ -43,7 +43,7 @@ void test_colmgr()
path = Gtk::TreePath(row);
test_equal(colmgr :: get_path(path), Glib::ustring("/tmp/ocarina/"));
library = tagdb :: lookup_library(1);
library = tags :: get_library(1);
test_equal(library->enabled(), true);
colmgr :: toggle_path_enabled(path);
test_equal(library->enabled(), false);
@ -60,7 +60,7 @@ void test_colmgr()
*/
row = *list->children().begin();
path = Gtk::TreePath(row);
library = tagdb :: lookup_library(0);
library = tags :: get_library(0);
test_equal(library->enabled(), true);
test_equal(library :: get_queue()->size(), (unsigned)20);