diff --git a/config b/config index 9fbe776c..d58d2cb7 100644 --- a/config +++ b/config @@ -40,6 +40,7 @@ class Config: self.GROUP = False self.IDLE = False self.INDEX = False + self.LIBRARY = False self.TEST = TEST self.reconfigure() diff --git a/design/library.txt b/design/library.txt index e6ee7827..c16a7798 100644 --- a/design/library.txt +++ b/design/library.txt @@ -22,6 +22,15 @@ Library: (lib/library.cpp) When a library : Track is created, it should be added to the "Library" group if it is NOT a member of the banned songs group. +- Databases: + enum DB_Type { + DB_ALBUM, + DB_ARTIST, + DB_GENRE, + DB_LIBRARY, + DB_TRACK, + }; + - Album: class library :: Album : public DatabaseEntry { public: @@ -47,8 +56,8 @@ Library: (lib/library.cpp) File << name -- Path: - class library :: Path : public DatabaseEntry { +- Library: + class library :: Library : public DatabaseEntry { public: string root_path; bool enabled; @@ -70,6 +79,7 @@ Library: (lib/library.cpp) short last_day; unsigned int play_count; unsigned int length; + bool banned; string title; string length_str; @@ -119,39 +129,33 @@ Library: (lib/library.cpp) happen while idle. - Testing: - A test library should be created to test adding tags and anything else. - The command `arecord -d 10 &library :: get_albums(); - Return the album database. - - const Database &library :: get_artists(); - Return the artist database. - - const Database &library :: get_genres(); - Return the genre database. - - const Database &library :: get_libraries(); - Return the library database. - - const Database &library :: get_tracks(); - Return the track database. +#ifdef CONFIG_DEBUG + void library :: print_db(DB_Type); + Print the database corresponding to DB_Type +endif /* CONFIG_DEBUG */ diff --git a/include/library.h b/include/library.h new file mode 100644 index 00000000..dea7bf27 --- /dev/null +++ b/include/library.h @@ -0,0 +1,44 @@ +/* + * Copyright 2013 (c) Anna Schumaker. + */ +#ifndef OCARINA_LIBRARY_H +#define OCARINA_LIBRARY_H + +#include +#include + +namespace library +{ + + enum DB_Type { + DB_ALBUM, + DB_ARTIST, + DB_GENRE, + DB_LIBRARY, + DB_TRACK, + }; + + class Library : public DatabaseEntry { + public: + std::string root_path; + bool enabled; + + Library(const std::string &, bool); + void read(File &); + void write(File &); +#ifdef CONFIG_DEBUG + void print(); +#endif /* CONFIG_DEBUG */ + bool operator==(Library &); + }; + + + bool add_path(const std::string &); + void del_path(unsigned int); +#ifdef CONFIG_DEBUG + void print_db(DB_Type); + void reset(); +#endif /* CONFIG_DEBUG */ +}; + +#endif /* OCARINA_LIBRARY_H */ diff --git a/lib/Sconscript b/lib/Sconscript index 83729f9f..3bb38bb7 100644 --- a/lib/Sconscript +++ b/lib/Sconscript @@ -2,7 +2,7 @@ Import("env", "CONFIG") class Module: - def __init__(self, source = None, package = "", depends = ""): + def __init__(self, source = None, package = "", depends = []): self.depends = depends self.package = package self.source = source @@ -15,12 +15,13 @@ modules = { # # ########################### - "DATABASE" : Module("database.cpp", depends = "FILE"), + "DATABASE" : Module("database.cpp", depends = [ "FILE" ]), "FILE" : Module("file.cpp", package = "glib-2.0"), - "FILTER" : Module("filter.cpp", depends = "INDEX"), - "GROUP" : Module("group.cpp", depends = "INDEX"), + "FILTER" : Module("filter.cpp", depends = [ "INDEX" ]), + "GROUP" : Module("group.cpp", depends = [ "INDEX" ]), "IDLE" : Module("idle.cpp"), - "INDEX" : Module("index.cpp", depends = "FILE"), + "INDEX" : Module("index.cpp", depends = [ "FILE" ]), + "LIBRARY" : Module("library.cpp", depends = [ "DATABASE", "IDLE" ]), ########################### ########################### @@ -38,8 +39,10 @@ def resolve(name): CONFIG.package(mod.package) res = [ env.Object(mod.source) ] - if CONFIG.__dict__.get(mod.depends) == False: - res += resolve(mod.depends) + + for dep in mod.depends: + if CONFIG.__dict__.get(dep) == False: + res += resolve(dep) return res diff --git a/lib/library.cpp b/lib/library.cpp new file mode 100644 index 00000000..294b6ea8 --- /dev/null +++ b/lib/library.cpp @@ -0,0 +1,82 @@ +/* + * Copyright 2013 (c) Anna Schumaker. + */ +#include + +#include + + +static Database library_db("library.db", DB_NORMAL); + + + +/* + * library :: Library: Basic information about each directory in the library + */ + +library :: Library :: Library(const std::string &path, bool is_enabled) + : root_path(path), enabled(is_enabled) +{ +} + +void library :: Library :: read(File &f) +{ +} + +void library :: Library :: write(File &f) +{ +} + +#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; +} + + + +/* + * API used by the GUI begins here + */ + +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)); + return true; +} + +void library :: del_path(unsigned int id) +{ + library_db.remove(id); +} + +#ifdef CONFIG_DEBUG +void library :: print_db(DB_Type type) +{ + switch (type) { + case DB_LIBRARY: + library_db.print(); + break; + default: + break; + } +} + +void library :: reset() +{ + library_db.clear(); +} +#endif /* CONFIG_DEBUG */ diff --git a/tests/Sconscript b/tests/Sconscript index 6a234485..8a832a80 100644 --- a/tests/Sconscript +++ b/tests/Sconscript @@ -57,7 +57,7 @@ rm_test_dir(xdg.BaseDirectory.xdg_data_home); # # Read SConscript files # -scripts = [ "database", "file", "filter", "group", "idle", "index", "print" ] +scripts = [ "database", "file", "filter", "group", "idle", "index", "library", "print" ] for s in scripts: CONFIG.reset(TEST = True) SConscript("%s/Sconscript" % s) diff --git a/tests/library/Sconscript b/tests/library/Sconscript new file mode 100644 index 00000000..433b4906 --- /dev/null +++ b/tests/library/Sconscript @@ -0,0 +1,6 @@ +#!/usr/bin/python +Import("Test", "CONFIG") + +CONFIG.LIBRARY = True + +Test("library", "library.cpp") diff --git a/tests/library/gen_library.sh b/tests/library/gen_library.sh index 169e663f..fb9745a4 100755 --- a/tests/library/gen_library.sh +++ b/tests/library/gen_library.sh @@ -29,6 +29,11 @@ function gen_tracks() for i in $(seq 10); do track="Track $i" let remainder=$i%4 + out="/tmp/$library/$artist/$album/$i - $track.ogg" + + if [ -f "$out" ]; then + continue + fi case $remainder in 0) OGG="1.ogg" ;; @@ -74,3 +79,5 @@ for i in $(seq 0 4); do echo "Generating library: $i" gen_artists $i done + +touch /tmp/library/file diff --git a/tests/library/library.cpp b/tests/library/library.cpp new file mode 100644 index 00000000..f7295ed1 --- /dev/null +++ b/tests/library/library.cpp @@ -0,0 +1,70 @@ +/* + * Copyright 2013 (c) Anna Schumaker. + */ +#include +#include + +#include + +void gen_library() +{ + system("tests/library/gen_library.sh"); + print("\n"); +} + +void test_add_dir(const std::string &test, const std::string &dir, bool expected) +{ + print("Test %s: ", test.c_str()); + if (library :: add_path(dir.c_str()) == expected) + print("PASSED\n"); + else + print("FAILED\n"); + library :: print_db(library :: DB_LIBRARY); +} + +void test_del_dir(const std::string &test, const unsigned int path_id) +{ + print("Test %s\n", test.c_str()); + library :: del_path(path_id); + library :: print_db(library :: DB_LIBRARY); +} + +/* Add paths library that SHOULD fail */ +void test_0() +{ + test_add_dir("0a", "/tmp/library/error", false); + test_add_dir("0b", "/tmp/library/file", false); + print("\n"); +} + +/* Simple library path operations */ +void test_1() +{ + test_add_dir("1a", "/tmp/library/0", true); + library :: del_path(0); + print("\n"); +} + +/* Test multiple paths */ +void test_2() +{ + library :: reset(); + test_add_dir("2a", "/tmp/library/0", true); + test_add_dir("2b", "/tmp/library/1", true); + test_add_dir("2c", "/tmp/library/2", true); + + test_del_dir("2d", 1); + test_del_dir("2e", 0); + test_del_dir("2f", 2); + print("\n"); +} + +int main(int argc, char **argv) +{ + gen_library(); + + test_0(); + test_1(); + test_2(); + return 0; +}