diff --git a/include/library.h b/include/library.h index dea7bf27..d24c736a 100644 --- a/include/library.h +++ b/include/library.h @@ -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 diff --git a/lib/library.cpp b/lib/library.cpp index 294b6ea8..376be636 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -14,6 +14,11 @@ static Database 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 diff --git a/tests/library/library.cpp b/tests/library/library.cpp index f7295ed1..d6c7d0fd 100644 --- a/tests/library/library.cpp +++ b/tests/library/library.cpp @@ -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; }