From 5eecac74c28b80df6b98b687fd5b0f2b17db081b Mon Sep 17 00:00:00 2001 From: Andrzej Surdej Date: Wed, 12 Dec 2012 11:33:55 +0100 Subject: [PATCH] CustomHandlersDB API updated to handle new functionalities. [Issue#] N/A [Problem] Supprot for custom handlers [Cause] N/A [Solution] Exended API provided [Verification] To verify build repository Change-Id: Id0f4b0b643fc311ce1b1f5a56441aaaf4800c58a --- .../dao/custom_handler_dao.cpp | 152 +++++++++++++++++- .../dao/custom_handler_dao_read_only.cpp | 119 +++++++++++++- .../custom-handler-dao-ro/common_dao_types.h | 20 ++- .../custom_handler_dao_read_only.h | 18 +++ .../custom_handler_dao.h | 8 + .../custom_handler_dao/orm/custom_handler_db | 4 +- modules/db/include/dpl/db/orm.h | 8 + 7 files changed, 318 insertions(+), 11 deletions(-) diff --git a/modules/custom_handler_dao/dao/custom_handler_dao.cpp b/modules/custom_handler_dao/dao/custom_handler_dao.cpp index cbab366..89f6144 100644 --- a/modules/custom_handler_dao/dao/custom_handler_dao.cpp +++ b/modules/custom_handler_dao/dao/custom_handler_dao.cpp @@ -23,6 +23,7 @@ #include #include #include +#include using namespace DPL::DB::ORM; using namespace DPL::DB::ORM::custom_handler; @@ -39,7 +40,7 @@ void fillRow(T& row, const CustomHandler& handler, const DPL::String& pkgName) row.Set_base_url(handler.base_url); row.Set_url(handler.url); row.Set_title(handler.title); - row.Set_user_allowed(handler.user_allowed); + row.Set_user_allowed(handler.user_decision); } } // namespace @@ -58,13 +59,64 @@ void CustomHandlerDAO::registerContentHandler(const CustomHandler& handler) LogDebug("Registering content handler " << handler.target << " " << handler.base_url); Try { + if (handler.user_decision & Agreed) { + //need to disable all previous, agreed entries + CUSTOM_HANDLER_DB_SELECT(select, ContentHandlers); + select->Where(And(Equals(handler.target), + Or(Equals(Agreed), + Equals(AgreedPermanently)) + )); + ContentHandlers::Select::RowList rows = select->GetRowList(); + if (rows.size() > 1) { + //more than one activ content handler - not good. Remove all. + //this should never happen + LogError("Database data incoherent."); + CUSTOM_HANDLER_DB_DELETE(deleteContent, ContentHandlers); + deleteContent->Where(And(Equals(handler.target), + Or(Equals(Agreed), + Equals(AgreedPermanently)))); + deleteContent->Execute(); + //all content handlers removed. New one can be inserted + } else if (!rows.empty()) { + //one active handler. Can be updaed + LogDebug("Activ content handler exist. Update"); + CUSTOM_HANDLER_DB_UPDATE(update, ContentHandlers); + update->Where(And(Equals(handler.target), + Or(Equals(Agreed), + Equals(AgreedPermanently)) + )); + ContentHandlers::Row rowToUpdate = rows.front(); + + if (handler.user_decision & DecisionSaved) { + //do not ask about previous one + rowToUpdate.Set_user_allowed(DeclinedPermanently); + } else { + //ask for next time + rowToUpdate.Set_user_allowed(Declined); + } + update->Values(rowToUpdate); + update->Execute(); + } + } + LogDebug("Inserting new content handler"); ContentHandlers::Row row; fillRow(row, handler, m_pkgName); - - // TODO remove previous if necessary + if (getContentHandler(handler.target, handler.url, handler.base_url)) { + LogDebug("Content handler exist. Update its state"); + CUSTOM_HANDLER_DB_UPDATE(updateRow, ContentHandlers); + updateRow->Where(And(Equals(m_pkgName), + And(Equals(handler.target), + And(Equals(handler.url), + Equals(handler.base_url))))); + updateRow->Values(row); + updateRow->Execute(); + LogDebug("updated"); + return; + } CUSTOM_HANDLER_DB_INSERT(insert, ContentHandlers); insert->Values(row); insert->Execute(); + LogDebug("insterted"); } Catch(DPL::DB::SqlConnection::Exception::Base){ ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError, @@ -77,13 +129,64 @@ void CustomHandlerDAO::registerProtocolHandler(const CustomHandler& handler) LogDebug("Registering protocol handler " << handler.target << " " << handler.base_url); Try { + + if (handler.user_decision & Agreed) { + //need to disable all previous, agreed entries + CUSTOM_HANDLER_DB_SELECT(select, ProtocolHandlers); + select->Where(And(Equals(handler.target), + Or(Equals(Agreed), + Equals(AgreedPermanently)) + )); + ProtocolHandlers::Select::RowList rows = select->GetRowList(); + if (rows.size() > 1) { + //more than one activ protocol handler - not good. Remove all. + //this should never happen + LogError("Database data incoherent."); + CUSTOM_HANDLER_DB_DELETE(deleteProtocol, ProtocolHandlers); + deleteProtocol->Where(And(Equals(handler.target), + Or(Equals(Agreed), + Equals(AgreedPermanently)))); + deleteProtocol->Execute(); + //all protocol handlers removed. New one can be inserted + } else if (!rows.empty()) { + //one active handler. Can be updaed + CUSTOM_HANDLER_DB_UPDATE(update, ProtocolHandlers); + update->Where(And(Equals(handler.target), + Or(Equals(Agreed), + Equals(AgreedPermanently)) + )); + ProtocolHandlers::Row rowToUpdate = rows.front(); + + if (handler.user_decision & DecisionSaved) { + //do not ask about previous one + rowToUpdate.Set_user_allowed(DeclinedPermanently); + } else { + //ask for next time + rowToUpdate.Set_user_allowed(Declined); + } + update->Values(rowToUpdate); + update->Execute(); + } + } + LogDebug("Inserting new protocol handler"); ProtocolHandlers::Row row; fillRow(row, handler, m_pkgName); - - // TODO remove previous if necessary + if (getProtocolHandler(handler.target, handler.url, handler.base_url)) { + LogDebug("Protocol handler exist. Update its state"); + CUSTOM_HANDLER_DB_UPDATE(updateRow, ProtocolHandlers); + updateRow->Where(And(Equals(m_pkgName), + And(Equals(handler.target), + And(Equals(handler.url), + Equals(handler.base_url))))); + updateRow->Values(row); + updateRow->Execute(); + LogDebug("updated"); + return; + } CUSTOM_HANDLER_DB_INSERT(insert, ProtocolHandlers); insert->Values(row); insert->Execute(); + LogDebug("insterted"); } Catch(DPL::DB::SqlConnection::Exception::Base){ ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError, @@ -126,4 +229,43 @@ void CustomHandlerDAO::unregisterProtocolHandler(const DPL::String& target, } +void CustomHandlerDAO::unregisterContentHandler(const DPL::String& target, + const DPL::String& url, + const DPL::String& baseURL) +{ + LogDebug("Removing content handler " << target << " " << url); + Try { + CUSTOM_HANDLER_DB_DELETE(deleteFrom, ContentHandlers); + deleteFrom->Where(And(Equals(m_pkgName), + And(Equals(target), + And(Equals(url), + Equals(baseURL))))); + deleteFrom->Execute(); + } + Catch(DPL::DB::SqlConnection::Exception::Base) { + ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError, + "Failed to remove content handler"); + } +} + +void CustomHandlerDAO::unregisterProtocolHandler(const DPL::String& target, + const DPL::String& url, + const DPL::String& baseURL) +{ + LogDebug("Removing protocol handler " << target << " " << url); + Try { + CUSTOM_HANDLER_DB_DELETE(deleteFrom, ProtocolHandlers); + deleteFrom->Where(And(Equals(m_pkgName), + And(Equals(target), + And(Equals(url), + Equals(baseURL))))); + deleteFrom->Execute(); + } + Catch(DPL::DB::SqlConnection::Exception::Base) { + ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError, + "Failed to remove content handler"); + } + +} + } // namespace CustomHandlerDB diff --git a/modules/custom_handler_dao/dao/custom_handler_dao_read_only.cpp b/modules/custom_handler_dao/dao/custom_handler_dao_read_only.cpp index ef8e3ef..a7159fa 100644 --- a/modules/custom_handler_dao/dao/custom_handler_dao_read_only.cpp +++ b/modules/custom_handler_dao/dao/custom_handler_dao_read_only.cpp @@ -50,7 +50,9 @@ CustomHandlerPtr getSingleHandler(std::list row) handler->base_url = row.front().Get_base_url(); handler->url = row.front().Get_url(); handler->title = row.front().Get_title(); - handler->user_allowed = row.front().Get_user_allowed(); + handler->user_allowed = static_cast(row.front().Get_user_allowed()); + handler->user_decision = + static_cast(row.front().Get_user_allowed()); } return handler; } @@ -106,6 +108,121 @@ CustomHandlerPtr CustomHandlerDAOReadOnly::getContentHandler( } } +CustomHandlerPtr CustomHandlerDAOReadOnly::getActivProtocolHandler( + const DPL::String& protocol) +{ + LogDebug("Getting active protocol handler"); + Try{ + CUSTOM_HANDLER_DB_SELECT(select, ProtocolHandlers); + + select->Where(And(Equals(m_pkgName), + Equals(protocol))); + + std::list list = select->GetRowList(); + CustomHandlerPtr handler; + + FOREACH(it, list) { + if (it->Get_user_allowed() & Agreed) { + handler.reset(new CustomHandler()); + handler->base_url = it->Get_base_url(); + handler->target = it->Get_target(); + handler->title = it->Get_title(); + handler->url = it->Get_url(); + handler->user_allowed = + static_cast(it->Get_user_allowed()); + handler->user_decision = + static_cast(it->Get_user_allowed()); + } + } + return handler; + } Catch(DPL::DB::SqlConnection::Exception::Base) { + ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError, + "Failed to get protocol handler"); + } +} + +CustomHandlerPtr CustomHandlerDAOReadOnly::getProtocolHandler( + const DPL::String& protocol, + const DPL::String& url, + const DPL::String& baseURL) +{ + LogDebug("Check if protocol is registered"); + Try{ + CUSTOM_HANDLER_DB_SELECT(select, ProtocolHandlers); + + select->Where(And(Equals(m_pkgName), + And(Equals(protocol), + And(Equals(url), + Equals(baseURL) + ) + ) + ) + ); + + std::list list = select->GetRowList(); + return getSingleHandler(list); + } Catch(DPL::DB::SqlConnection::Exception::Base) { + ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError, + "Failed to get content handler"); + } +} + + +CustomHandlerPtr CustomHandlerDAOReadOnly::getActivContentHandler( + const DPL::String& content) +{ + LogDebug("Getting active content handler"); + Try{ + CUSTOM_HANDLER_DB_SELECT(select, ContentHandlers); + + select->Where(And(Equals(m_pkgName), + Equals(content))); + + std::list list = select->GetRowList(); + CustomHandlerPtr handler; + + FOREACH(it, list) { + if (it->Get_user_allowed() & Agreed) { + handler.reset(new CustomHandler()); + handler->base_url = it->Get_base_url(); + handler->target = it->Get_target(); + handler->title = it->Get_title(); + handler->url = it->Get_url(); + handler->user_allowed = + static_cast(it->Get_user_allowed()); + handler->user_decision = + static_cast(it->Get_user_allowed()); + } + } + return handler; + } Catch(DPL::DB::SqlConnection::Exception::Base) { + ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError, + "Failed to get protocol handler"); + } +} + +CustomHandlerPtr CustomHandlerDAOReadOnly::getContentHandler( + const DPL::String& content, + const DPL::String& url, + const DPL::String& baseURL) +{ + LogDebug("Check if content is registered"); + Try{ + CUSTOM_HANDLER_DB_SELECT(select, ContentHandlers); + + select->Where(And(Equals(m_pkgName), + And(Equals(content), + And(Equals(url), + Equals(baseURL))))); + + std::list list = select->GetRowList(); + return getSingleHandler(list); + } Catch(DPL::DB::SqlConnection::Exception::Base) { + ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError, + "Failed to get content handler"); + } +} + CustomHandlerPtr CustomHandlerDAOReadOnly::getAllowedProtocolHandler( const DPL::String& protocol) { diff --git a/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/common_dao_types.h b/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/common_dao_types.h index 6be001c..b08142a 100644 --- a/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/common_dao_types.h +++ b/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/common_dao_types.h @@ -21,8 +21,8 @@ * @brief This file contains the declaration of * common data types for custom handler database. */ -#ifndef SHARE_COMMON_DAO_TYPES_H_ -#define SHARE_COMMON_DAO_TYPES_H_ +#ifndef SRC_MODULES_CUSTOM_HANDLERS_DAO_COMMON_DAO_TYPES_H_ +#define SRC_MODULES_CUSTOM_HANDLERS_DAO_COMMON_DAO_TYPES_H_ #include #include @@ -35,6 +35,18 @@ namespace CustomHandlerDB { * * Describes custom handler for protocol and content. */ +enum HandlerState { + Agreed = 0x01, //user agreed to use protocol only, + //but want to ask in next occurence + Declined = 0x02, //user declined to use protocol, + //but want to ask in next occurence + //in fact it is used when user wants to cover saved agreed + //decision by agreeing to another one without save. + DecisionSaved = 0x04, //user dont want to ask again + AgreedPermanently = Agreed | DecisionSaved, + DeclinedPermanently = Declined | DecisionSaved +}; + struct CustomHandler { DPL::String target; // protocol/content ("mailto"/"application/x-soup") @@ -42,10 +54,12 @@ struct CustomHandler DPL::String url; // url used for protocol/content handling DPL::String title; // user friendly handler name bool user_allowed; // true if user has allowed the handler + HandlerState user_decision; }; typedef std::shared_ptr CustomHandlerPtr; +typedef std::list CustomHandlersList; } // namespace CustomHandlerDB -#endif /* SHARE_COMMON_DAO_TYPES_H_ */ +#endif /* SRC_MODULES_CUSTOM_HANDLERS_DAO_COMMON_DAO_TYPES_H_ */ diff --git a/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/custom_handler_dao_read_only.h b/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/custom_handler_dao_read_only.h index 021d6a8..88c720d 100644 --- a/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/custom_handler_dao_read_only.h +++ b/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/custom_handler_dao_read_only.h @@ -53,12 +53,30 @@ class CustomHandlerDAOReadOnly */ CustomHandlerPtr getProtocolHandler(const DPL::String& protocol, const DPL::String& url); + CustomHandlerPtr getProtocolHandler(const DPL::String& protocol, + const DPL::String& url, + const DPL::String& baseURL); + + /** + * Returns protocol handler that is agreed or agreed and saved and match tizenID + */ + CustomHandlerPtr getActivProtocolHandler(const DPL::String& protocol); + + /** * Returns content handler */ CustomHandlerPtr getContentHandler(const DPL::String& content, const DPL::String& url); + CustomHandlerPtr getContentHandler(const DPL::String& protocol, + const DPL::String& url, + const DPL::String& baseURL); + + /** + * Returns content handler that is agreed or agreed and saved and match tizenID + */ + CustomHandlerPtr getActivContentHandler(const DPL::String& content); /** * Returns allowed handler for given protocol diff --git a/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-rw/custom_handler_dao.h b/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-rw/custom_handler_dao.h index d1307fe..f773681 100644 --- a/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-rw/custom_handler_dao.h +++ b/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-rw/custom_handler_dao.h @@ -54,6 +54,14 @@ class CustomHandlerDAO : public CustomHandlerDAOReadOnly */ void unregisterProtocolHandler(const DPL::String& target, const DPL::String& url); + + void unregisterContentHandler(const DPL::String& target, + const DPL::String& url, + const DPL::String& baseURL); + + void unregisterProtocolHandler(const DPL::String& target, + const DPL::String& url, + const DPL::String& baseURL); }; } // namespace CustomHandlerDB diff --git a/modules/custom_handler_dao/orm/custom_handler_db b/modules/custom_handler_dao/orm/custom_handler_db index 6b07417..848c84a 100644 --- a/modules/custom_handler_dao/orm/custom_handler_db +++ b/modules/custom_handler_dao/orm/custom_handler_db @@ -11,7 +11,7 @@ CREATE_TABLE(ProtocolHandlers) COLUMN_NOT_NULL(user_allowed, INT,) TABLE_CONSTRAINTS( - PRIMARY KEY (app_id, target, url) + PRIMARY KEY (app_id, target, base_url, url) ) CREATE_TABLE_END() @@ -24,7 +24,7 @@ CREATE_TABLE(ContentHandlers) COLUMN_NOT_NULL(user_allowed, INT,) TABLE_CONSTRAINTS( - PRIMARY KEY (app_id, target, url) + PRIMARY KEY (app_id, target, base_url, url) ) CREATE_TABLE_END() diff --git a/modules/db/include/dpl/db/orm.h b/modules/db/include/dpl/db/orm.h index dcc67a5..c404fba 100644 --- a/modules/db/include/dpl/db/orm.h +++ b/modules/db/include/dpl/db/orm.h @@ -128,6 +128,14 @@ BinaryExpression (leftExpression, rightExpression); } +template +BinaryExpression + Or(const LeftExpression& leftExpression, const RightExpression& rightExpression) +{ + return BinaryExpression + (leftExpression, rightExpression); +} + template class __attribute__ ((visibility("hidden"))) ExpressionWithArgument : public Expression { protected: -- 2.34.1