From: Somin Kim Date: Tue, 2 Feb 2016 04:47:51 +0000 (+0900) Subject: Custom item recovery logic added when initialized X-Git-Tag: submit/tizen/20160314.020719~1^2~8 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9ce08713c4b8c36ee1a600bab22ae81431d9854c;p=platform%2Fcore%2Fcontext%2Fcontext-provider.git Custom item recovery logic added when initialized (Uninstalled provider's item handling is still needed) Change-Id: I7e020b28174bbf2f868cd9c255fbe65dbcd1fbc2 Signed-off-by: Somin Kim --- diff --git a/include/custom_context_provider.h b/include/custom_context_provider.h index 584cf3b..10e4275 100644 --- a/include/custom_context_provider.h +++ b/include/custom_context_provider.h @@ -21,7 +21,7 @@ namespace ctx { bool init_custom_context_provider(); namespace custom_context_provider { - int add_item(std::string subject, std::string name, ctx::json tmpl, const char* owner); + int add_item(std::string subject, std::string name, ctx::json tmpl, const char* owner, bool is_init = false); int remove_item(std::string subject); int publish_data(std::string subject, ctx::json fact); diff --git a/src/custom/custom_context_provider.cpp b/src/custom/custom_context_provider.cpp index 923fa1a..dd79fd5 100644 --- a/src/custom/custom_context_provider.cpp +++ b/src/custom/custom_context_provider.cpp @@ -15,9 +15,11 @@ */ #include +#include #include #include #include +#include #include #include "custom_base.h" @@ -59,10 +61,44 @@ EXTAPI void ctx::custom_context_provider::destroy(void *data) EXTAPI bool ctx::init_custom_context_provider() { + // Create custom template db + std::string q = std::string("CREATE TABLE IF NOT EXISTS context_trigger_custom_template ") + + "(subject TEXT DEFAULT '' NOT NULL PRIMARY KEY, name TEXT DEFAULT '' NOT NULL, operation INTEGER DEFAULT 3 NOT NULL, " + + "attributes TEXT DEFAULT '' NOT NULL, owner TEXT DEFAULT '' NOT NULL)"; + + std::vector record; + bool ret = db_manager::execute_sync(q.c_str(), &record); + IF_FAIL_RETURN_TAG(ret, false, _E, "Create template table failed"); + + // Register custom items + std::string q_select = "SELECT * FROM context_trigger_custom_template"; + ret = db_manager::execute_sync(q_select.c_str(), &record); + IF_FAIL_RETURN_TAG(ret, false, _E, "Failed to query custom templates"); + IF_FAIL_RETURN(record.size() > 0, true); + + int error; + std::vector::iterator vec_end = record.end(); + for (std::vector::iterator vec_pos = record.begin(); vec_pos != vec_end; ++vec_pos) { + ctx::json elem = *vec_pos; + std::string subject; + std::string name; + std::string attributes; + std::string owner; + elem.get(NULL, "subject", &subject); + elem.get(NULL, "name", &name); + elem.get(NULL, "attributes", &attributes); + elem.get(NULL, "owner", &owner); + + error = ctx::custom_context_provider::add_item(subject, name, ctx::json(attributes), owner.c_str(), true); + if (error != ERR_NONE) { + _E("Failed to add custom item(%s): %#x", subject.c_str(), error); + } + } + return true; } -EXTAPI int ctx::custom_context_provider::add_item(std::string subject, std::string name, ctx::json tmpl, const char* owner) +EXTAPI int ctx::custom_context_provider::add_item(std::string subject, std::string name, ctx::json tmpl, const char* owner, bool is_init) { std::map::iterator it; it = custom_map.find(subject); @@ -82,6 +118,15 @@ EXTAPI int ctx::custom_context_provider::add_item(std::string subject, std::stri register_provider(custom->get_subject(), NULL); + // Add item to custom template db + if (!is_init) { + std::string q = "INSERT OR IGNORE INTO context_trigger_custom_template (subject, name, attributes, owner) VALUES ('" + + subject + "', '" + name + "', '" + tmpl.str() + "', '" + owner + "'); "; + std::vector record; + bool ret = db_manager::execute_sync(q.c_str(), &record); + IF_FAIL_RETURN_TAG(ret, false, _E, "Failed to query custom templates"); + } + return ERR_NONE; } @@ -93,6 +138,12 @@ EXTAPI int ctx::custom_context_provider::remove_item(std::string subject) unregister_provider(subject.c_str()); + // Remove item from custom template db + std::string q = "DELETE FROM context_trigger_custom_template WHERE subject = '" + subject + "'"; + std::vector record; + bool ret = db_manager::execute_sync(q.c_str(), &record); + IF_FAIL_RETURN_TAG(ret, false, _E, "Failed to query custom templates"); + return ERR_NONE; }