library: Add support for saving and restoring the library

Eventually these functions may have to become idle tasks, but for now I
write to disk whenever the library_db is changed and then read from disk
by calling the init() function.

Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
This commit is contained in:
Anna Schumaker 2013-11-03 16:03:01 -05:00 committed by Anna Schumaker
parent 9ddbaf0b97
commit 81429b0229
3 changed files with 36 additions and 0 deletions

View File

@ -23,6 +23,7 @@ namespace library
std::string root_path;
bool enabled;
Library();
Library(const std::string &, bool);
void read(File &);
void write(File &);
@ -33,6 +34,7 @@ namespace library
};
void init();
bool add_path(const std::string &);
void del_path(unsigned int);
#ifdef CONFIG_DEBUG

View File

@ -14,6 +14,11 @@ static Database<library :: Library> library_db("library.db", DB_NORMAL);
* library :: Library: Basic information about each directory in the library
*/
library :: Library :: Library()
: root_path(""), enabled(false)
{
}
library :: Library :: Library(const std::string &path, bool is_enabled)
: root_path(path), enabled(is_enabled)
{
@ -21,10 +26,13 @@ library :: Library :: Library(const std::string &path, bool is_enabled)
void library :: Library :: read(File &f)
{
f >> enabled;
root_path = f.getline();
}
void library :: Library :: write(File &f)
{
f << enabled << " " << root_path;
}
#ifdef CONFIG_DEBUG
@ -49,18 +57,25 @@ bool library :: Library :: operator==(library :: Library &rhs)
* API used by the GUI begins here
*/
void library :: init()
{
library_db.load();
}
bool library :: add_path(const std::string &dir)
{
if (g_file_test(dir.c_str(), G_FILE_TEST_IS_DIR) == false)
return false;
library_db.insert(library :: Library(dir, true));
library_db.save();
return true;
}
void library :: del_path(unsigned int id)
{
library_db.remove(id);
library_db.save();
}
#ifdef CONFIG_DEBUG

View File

@ -59,6 +59,24 @@ void test_2()
print("\n");
}
/* Test load and save of library db */
void test_3()
{
library :: reset();
test_add_dir("3a", "/tmp/library/0", true);
test_add_dir("3b", "/tmp/library/1", true);
test_add_dir("3c", "/tmp/library/2", true);
print("Test 3d\n");
library :: reset();
library :: print_db(library :: DB_LIBRARY);
print("Test 3e\n");
library :: init();
library :: print_db(library :: DB_LIBRARY);
print("\n");
}
int main(int argc, char **argv)
{
gen_library();
@ -66,5 +84,6 @@ int main(int argc, char **argv)
test_0();
test_1();
test_2();
test_3();
return 0;
}