2013-10-27 17:25:26 -04:00
|
|
|
/*
|
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
|
|
*/
|
|
|
|
#include <library.h>
|
|
|
|
|
|
|
|
#include <glib.h>
|
2013-11-10 16:09:31 -05:00
|
|
|
#include <taglib/fileref.h>
|
2013-10-27 17:25:26 -04:00
|
|
|
|
2013-11-10 16:09:31 -05:00
|
|
|
static Database<library :: Artist> artist_db("artist.db", DB_UNIQUE);
|
2013-11-10 16:45:52 -05:00
|
|
|
static Database<library :: Album> album_db("album.db", DB_UNIQUE);
|
|
|
|
static Database<library :: Genre> genre_db("genre.db", DB_UNIQUE);
|
2013-10-27 17:25:26 -04:00
|
|
|
static Database<library :: Library> library_db("library.db", DB_NORMAL);
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-11-10 16:09:31 -05:00
|
|
|
/*
|
|
|
|
* library :: Artist: Artist tag information
|
|
|
|
*/
|
2013-11-10 16:45:52 -05:00
|
|
|
|
2013-11-10 16:09:31 -05:00
|
|
|
library :: Artist :: Artist()
|
|
|
|
: name("")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-11-10 16:57:28 -05:00
|
|
|
library :: Artist :: Artist(TagLib :: Tag *tag)
|
|
|
|
: name(tag->artist().to8Bit(true))
|
2013-11-10 16:09:31 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-10 16:45:52 -05:00
|
|
|
/*
|
|
|
|
* library :: Album: Album tag information
|
|
|
|
*/
|
|
|
|
|
|
|
|
library :: Album :: Album()
|
|
|
|
: name(""), year(0), artist_id(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-11-10 16:57:28 -05:00
|
|
|
library :: Album :: Album(TagLib :: Tag *tag, unsigned int artist)
|
|
|
|
: name(tag->album().to8Bit(true)), year(tag->year()), artist_id(artist)
|
2013-11-10 16:45:52 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void library :: Album :: read(File &f)
|
|
|
|
{
|
|
|
|
f >> artist_id >> year;
|
|
|
|
name = f.getline();
|
|
|
|
}
|
|
|
|
|
|
|
|
void library :: Album :: write(File &f)
|
|
|
|
{
|
|
|
|
f << artist_id << " " << year << " " << name;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
void library :: Album :: print()
|
|
|
|
{
|
|
|
|
:: print("Album: %s (%u) by %s", name.c_str(), year, artist_db[artist_id].name.c_str());
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_DEBUG */
|
|
|
|
|
|
|
|
bool library :: Album :: operator==(const library :: Album &rhs)
|
|
|
|
{
|
|
|
|
if (artist_id == rhs.artist_id) {
|
|
|
|
if (name == rhs.name)
|
|
|
|
return year == rhs.year;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* library :: Genre: Genre tag information
|
|
|
|
*/
|
|
|
|
|
|
|
|
library :: Genre :: Genre()
|
|
|
|
: name("")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-11-10 16:57:28 -05:00
|
|
|
library :: Genre :: Genre(TagLib :: Tag *tag)
|
|
|
|
: name(tag->genre().to8Bit(true))
|
2013-11-10 16:45:52 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void library :: Genre :: read(File &f)
|
|
|
|
{
|
|
|
|
name = f.getline();
|
|
|
|
}
|
|
|
|
|
|
|
|
void library :: Genre :: write(File &f)
|
|
|
|
{
|
|
|
|
f << name;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
void library :: Genre :: print()
|
|
|
|
{
|
|
|
|
:: print("Genre: %s", name.c_str());
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_DEBUG */
|
|
|
|
|
|
|
|
bool library :: Genre :: operator==(const library :: Genre &rhs)
|
|
|
|
{
|
|
|
|
return name == rhs.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-10 16:09:31 -05:00
|
|
|
|
2013-10-27 17:25:26 -04:00
|
|
|
/*
|
|
|
|
* library :: Library: Basic information about each directory in the library
|
|
|
|
*/
|
|
|
|
|
2013-11-03 16:03:01 -05:00
|
|
|
library :: Library :: Library()
|
|
|
|
: root_path(""), enabled(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-10-27 17:25:26 -04:00
|
|
|
library :: Library :: Library(const std::string &path, bool is_enabled)
|
|
|
|
: root_path(path), enabled(is_enabled)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void library :: Library :: read(File &f)
|
|
|
|
{
|
2013-11-03 16:03:01 -05:00
|
|
|
f >> enabled;
|
|
|
|
root_path = f.getline();
|
2013-10-27 17:25:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void library :: Library :: write(File &f)
|
|
|
|
{
|
2013-11-03 16:03:01 -05:00
|
|
|
f << enabled << " " << root_path;
|
2013-10-27 17:25:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
void library :: Library :: print()
|
|
|
|
{
|
|
|
|
:: print("%s", root_path.c_str());
|
|
|
|
if (enabled == true)
|
|
|
|
:: print(" (enabled)");
|
|
|
|
else
|
|
|
|
:: print(" (disabled)");
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_DEBUG */
|
|
|
|
|
|
|
|
bool library :: Library :: operator==(library :: Library &rhs)
|
|
|
|
{
|
|
|
|
return root_path == rhs.root_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-11-10 16:09:31 -05:00
|
|
|
/*
|
|
|
|
* 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());
|
2013-11-10 16:45:52 -05:00
|
|
|
unsigned int artist_id;
|
2013-11-10 16:09:31 -05:00
|
|
|
|
|
|
|
if (ref.isNull()) {
|
|
|
|
print("ERROR: Could not read tags for file %s", path.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tag = ref.tag();
|
2013-11-10 16:57:28 -05:00
|
|
|
artist_id = artist_db.insert(tag);
|
|
|
|
album_db.insert(library :: Album(tag, artist_id));
|
|
|
|
genre_db.insert(library :: Genre(tag));
|
2013-11-10 16:09:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-27 17:25:26 -04:00
|
|
|
/*
|
|
|
|
* API used by the GUI begins here
|
|
|
|
*/
|
|
|
|
|
2013-11-03 16:03:01 -05:00
|
|
|
void library :: init()
|
|
|
|
{
|
|
|
|
library_db.load();
|
|
|
|
}
|
|
|
|
|
2013-10-27 17:25:26 -04:00
|
|
|
bool library :: add_path(const std::string &dir)
|
|
|
|
{
|
2013-11-10 16:09:31 -05:00
|
|
|
unsigned int id;
|
2013-10-27 17:25:26 -04:00
|
|
|
if (g_file_test(dir.c_str(), G_FILE_TEST_IS_DIR) == false)
|
|
|
|
return false;
|
|
|
|
|
2013-11-10 16:09:31 -05:00
|
|
|
id = library_db.insert(library :: Library(dir, true));
|
2013-11-03 16:03:01 -05:00
|
|
|
library_db.save();
|
2013-11-10 16:09:31 -05:00
|
|
|
update_path(id);
|
2013-10-27 17:25:26 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void library :: del_path(unsigned int id)
|
|
|
|
{
|
|
|
|
library_db.remove(id);
|
2013-11-03 16:03:01 -05:00
|
|
|
library_db.save();
|
2013-10-27 17:25:26 -04:00
|
|
|
}
|
|
|
|
|
2013-11-10 16:09:31 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-10-27 17:25:26 -04:00
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
void library :: print_db(DB_Type type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
2013-11-10 16:45:52 -05:00
|
|
|
case DB_ALBUM:
|
|
|
|
album_db.print();
|
|
|
|
break;
|
2013-11-10 16:09:31 -05:00
|
|
|
case DB_ARTIST:
|
|
|
|
artist_db.print();
|
|
|
|
break;
|
2013-11-10 16:45:52 -05:00
|
|
|
case DB_GENRE:
|
|
|
|
genre_db.print();
|
|
|
|
break;
|
2013-10-27 17:25:26 -04:00
|
|
|
case DB_LIBRARY:
|
|
|
|
library_db.print();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void library :: reset()
|
|
|
|
{
|
2013-11-10 16:45:52 -05:00
|
|
|
album_db.clear();
|
2013-11-10 16:09:31 -05:00
|
|
|
artist_db.clear();
|
2013-11-10 16:45:52 -05:00
|
|
|
genre_db.clear();
|
2013-10-27 17:25:26 -04:00
|
|
|
library_db.clear();
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_DEBUG */
|