library: Scan a directory

I don't build up a complete set of databases yet, but I do create the
Artist name database to use later.

Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
This commit is contained in:
Anna Schumaker 2013-11-10 16:09:31 -05:00 committed by Anna Schumaker
parent 81429b0229
commit 10fd3292fd
4 changed files with 139 additions and 3 deletions

View File

@ -18,6 +18,22 @@ namespace library
DB_TRACK, DB_TRACK,
}; };
class Artist : public DatabaseEntry {
public:
std:: string name;
Artist();
Artist(const std :: string &);
void read(File &);
void write(File &);
#ifdef CONFIG_DEBUG
void print();
#endif /* CONFIG_DEBUG */
bool operator==(const Artist &);
};
class Library : public DatabaseEntry { class Library : public DatabaseEntry {
public: public:
std::string root_path; std::string root_path;
@ -37,6 +53,7 @@ namespace library
void init(); void init();
bool add_path(const std::string &); bool add_path(const std::string &);
void del_path(unsigned int); void del_path(unsigned int);
void update_path(unsigned int);
#ifdef CONFIG_DEBUG #ifdef CONFIG_DEBUG
void print_db(DB_Type); void print_db(DB_Type);
void reset(); void reset();

View File

@ -21,7 +21,7 @@ modules = {
"GROUP" : Module("group.cpp", depends = [ "INDEX" ]), "GROUP" : Module("group.cpp", depends = [ "INDEX" ]),
"IDLE" : Module("idle.cpp"), "IDLE" : Module("idle.cpp"),
"INDEX" : Module("index.cpp", depends = [ "FILE" ]), "INDEX" : Module("index.cpp", depends = [ "FILE" ]),
"LIBRARY" : Module("library.cpp", depends = [ "DATABASE", "IDLE" ]), "LIBRARY" : Module("library.cpp", package = "taglib", depends = [ "DATABASE", "IDLE" ]),
########################### ###########################
########################### ###########################

View File

@ -4,12 +4,51 @@
#include <library.h> #include <library.h>
#include <glib.h> #include <glib.h>
#include <taglib/tag.h>
#include <taglib/fileref.h>
static Database<library :: Artist> artist_db("artist.db", DB_UNIQUE);
static Database<library :: Library> library_db("library.db", DB_NORMAL); static Database<library :: Library> library_db("library.db", DB_NORMAL);
/*
* library :: Artist: Artist tag information
*/
library :: Artist :: Artist()
: name("")
{
}
library :: Artist :: Artist(const std::string &artist_name)
: name(artist_name)
{
}
void library :: Artist :: read(File &f)
{
name = f.getline();
}
void library :: Artist :: write(File &f)
{
f << name;
}
#ifdef CONFIG_DEBUG
void library :: Artist :: print()
{
:: print("Artist: %s", name.c_str());
}
#endif /* CONFIG_DEBUG */
bool library :: Artist :: operator==(const library :: Artist &rhs)
{
return name == rhs.name;
}
/* /*
* library :: Library: Basic information about each directory in the library * library :: Library: Basic information about each directory in the library
*/ */
@ -53,6 +92,54 @@ bool library :: Library :: operator==(library :: Library &rhs)
/*
* Internal library functions
*/
static void do_update(unsigned int, const std :: string &);
static void read_tags(unsigned int lib_id, const std :: string &path)
{
TagLib :: Tag *tag;
TagLib :: FileRef ref(path.c_str());
if (ref.isNull()) {
print("ERROR: Could not read tags for file %s", path.c_str());
return;
}
tag = ref.tag();
artist_db.insert(tag->artist().to8Bit(true));
}
static void process_path(unsigned int lib_id, const std :: string &dir,
const std :: string &name)
{
std :: string path = dir + "/" + name;
if (g_file_test(path.c_str(), G_FILE_TEST_IS_DIR) == true)
do_update(lib_id, path);
else
read_tags(lib_id, path);
}
static void do_update(unsigned int lib_id, const std :: string &path)
{
GDir *dir;
const char *name;
dir = g_dir_open(path.c_str(), 0, NULL);
if (dir == NULL)
return;
name = g_dir_read_name(dir);
while (name != NULL) {
process_path(lib_id, path, name);
name = g_dir_read_name(dir);
}
}
/* /*
* API used by the GUI begins here * API used by the GUI begins here
*/ */
@ -64,11 +151,13 @@ void library :: init()
bool library :: add_path(const std::string &dir) bool library :: add_path(const std::string &dir)
{ {
unsigned int id;
if (g_file_test(dir.c_str(), G_FILE_TEST_IS_DIR) == false) if (g_file_test(dir.c_str(), G_FILE_TEST_IS_DIR) == false)
return false; return false;
library_db.insert(library :: Library(dir, true)); id = library_db.insert(library :: Library(dir, true));
library_db.save(); library_db.save();
update_path(id);
return true; return true;
} }
@ -78,10 +167,22 @@ void library :: del_path(unsigned int id)
library_db.save(); library_db.save();
} }
void library :: update_path(unsigned int id)
{
if (id > library_db.size())
return;
if (library_db[id].valid == false)
return;
do_update(id, library_db[id].root_path);
}
#ifdef CONFIG_DEBUG #ifdef CONFIG_DEBUG
void library :: print_db(DB_Type type) void library :: print_db(DB_Type type)
{ {
switch (type) { switch (type) {
case DB_ARTIST:
artist_db.print();
break;
case DB_LIBRARY: case DB_LIBRARY:
library_db.print(); library_db.print();
break; break;
@ -92,6 +193,7 @@ void library :: print_db(DB_Type type)
void library :: reset() void library :: reset()
{ {
artist_db.clear();
library_db.clear(); library_db.clear();
} }
#endif /* CONFIG_DEBUG */ #endif /* CONFIG_DEBUG */

View File

@ -29,6 +29,13 @@ void test_del_dir(const std::string &test, const unsigned int path_id)
library :: print_db(library :: DB_LIBRARY); library :: print_db(library :: DB_LIBRARY);
} }
void test_print_dbs(const std::string &test)
{
print("Test %s\n", test.c_str());
library :: print_db(library :: DB_GENRE);
}
/* Add paths library that SHOULD fail */ /* Add paths library that SHOULD fail */
void test_0() void test_0()
{ {
@ -77,6 +84,15 @@ void test_3()
print("\n"); print("\n");
} }
/* Test scanning a single path */
void test_4()
{
library :: reset();
test_add_dir("4a", "/tmp/library/0", true);
library :: print_db(library :: DB_ARTIST);
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
gen_library(); gen_library();
@ -85,5 +101,6 @@ int main(int argc, char **argv)
test_1(); test_1();
test_2(); test_2();
test_3(); test_3();
test_4();
return 0; return 0;
} }