CustomHandlersDB API updated to handle new functionalities.
authorAndrzej Surdej <a.surdej@samsung.com>
Wed, 12 Dec 2012 10:33:55 +0000 (11:33 +0100)
committerGerrit Code Review <gerrit2@kim11>
Mon, 17 Dec 2012 06:34:36 +0000 (15:34 +0900)
[Issue#] N/A
[Problem] Supprot for custom handlers
[Cause] N/A
[Solution] Exended API provided
[Verification] To verify build repository

Change-Id: Id0f4b0b643fc311ce1b1f5a56441aaaf4800c58a

modules/custom_handler_dao/dao/custom_handler_dao.cpp
modules/custom_handler_dao/dao/custom_handler_dao_read_only.cpp
modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/common_dao_types.h
modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/custom_handler_dao_read_only.h
modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-rw/custom_handler_dao.h
modules/custom_handler_dao/orm/custom_handler_db
modules/db/include/dpl/db/orm.h

index cbab366..89f6144 100644 (file)
@@ -23,6 +23,7 @@
 #include <wrt-commons/custom-handler-dao-rw/custom_handler_dao.h>
 #include <wrt-commons/custom-handler-dao-ro/CustomHandlerDatabase.h>
 #include <orm_generator_custom_handler.h>
+#include <wrt-commons/custom-handler-dao-ro/custom_handler_dao_read_only.h>
 
 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<ContentHandlers::target>(handler.target),
+                          Or(Equals<ContentHandlers::user_allowed>(Agreed),
+                             Equals<ContentHandlers::user_allowed>(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<ContentHandlers::target>(handler.target),
+                         Or(Equals<ContentHandlers::user_allowed>(Agreed),
+                            Equals<ContentHandlers::user_allowed>(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<ContentHandlers::target>(handler.target),
+                              Or(Equals<ContentHandlers::user_allowed>(Agreed),
+                                 Equals<ContentHandlers::user_allowed>(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<ContentHandlers::app_id>(m_pkgName),
+                             And(Equals<ContentHandlers::target>(handler.target),
+                             And(Equals<ContentHandlers::url>(handler.url),
+                                 Equals<ContentHandlers::base_url>(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<ProtocolHandlers::target>(handler.target),
+                          Or(Equals<ProtocolHandlers::user_allowed>(Agreed),
+                             Equals<ProtocolHandlers::user_allowed>(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<ProtocolHandlers::target>(handler.target),
+                         Or(Equals<ProtocolHandlers::user_allowed>(Agreed),
+                            Equals<ProtocolHandlers::user_allowed>(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<ProtocolHandlers::target>(handler.target),
+                              Or(Equals<ProtocolHandlers::user_allowed>(Agreed),
+                                 Equals<ProtocolHandlers::user_allowed>(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<ProtocolHandlers::app_id>(m_pkgName),
+                             And(Equals<ProtocolHandlers::target>(handler.target),
+                             And(Equals<ProtocolHandlers::url>(handler.url),
+                                 Equals<ProtocolHandlers::base_url>(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<ContentHandlers::app_id>(m_pkgName),
+                         And(Equals<ContentHandlers::target>(target),
+                         And(Equals<ContentHandlers::url>(url),
+                             Equals<ContentHandlers::base_url>(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<ProtocolHandlers::app_id>(m_pkgName),
+                         And(Equals<ProtocolHandlers::target>(target),
+                         And(Equals<ProtocolHandlers::url>(url),
+                             Equals<ProtocolHandlers::base_url>(baseURL)))));
+       deleteFrom->Execute();
+    }
+    Catch(DPL::DB::SqlConnection::Exception::Base) {
+       ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
+                  "Failed to remove content handler");
+    }
+
+}
+
 } // namespace CustomHandlerDB
index ef8e3ef..a7159fa 100644 (file)
@@ -50,7 +50,9 @@ CustomHandlerPtr getSingleHandler(std::list<T> 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<bool>(row.front().Get_user_allowed());
+        handler->user_decision =
+            static_cast<HandlerState>(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<ProtocolHandlers::app_id>(m_pkgName),
+                          Equals<ProtocolHandlers::target>(protocol)));
+
+        std::list<ProtocolHandlers::Row> 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<bool>(it->Get_user_allowed());
+                handler->user_decision =
+                    static_cast<HandlerState>(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<ProtocolHandlers::app_id>(m_pkgName),
+                          And(Equals<ProtocolHandlers::target>(protocol),
+                              And(Equals<ProtocolHandlers::url>(url),
+                                  Equals<ProtocolHandlers::base_url>(baseURL)
+                              )
+                           )
+                       )
+                 );
+
+        std::list<ProtocolHandlers::Row> 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<ContentHandlers::app_id>(m_pkgName),
+                          Equals<ContentHandlers::target>(content)));
+
+        std::list<ContentHandlers::Row> 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<bool>(it->Get_user_allowed());
+                handler->user_decision =
+                    static_cast<HandlerState>(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<ContentHandlers::app_id>(m_pkgName),
+                      And(Equals<ContentHandlers::target>(content),
+                      And(Equals<ContentHandlers::url>(url),
+                          Equals<ContentHandlers::base_url>(baseURL)))));
+
+        std::list<ContentHandlers::Row> 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)
 {
index 6be001c..b08142a 100644 (file)
@@ -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 <list>
 #include <memory>
@@ -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<CustomHandler> CustomHandlerPtr;
+typedef std::list <CustomHandlerPtr> CustomHandlersList;
 
 } // namespace CustomHandlerDB
 
-#endif /* SHARE_COMMON_DAO_TYPES_H_ */
+#endif /* SRC_MODULES_CUSTOM_HANDLERS_DAO_COMMON_DAO_TYPES_H_ */
index 021d6a8..88c720d 100644 (file)
@@ -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
index d1307fe..f773681 100644 (file)
@@ -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
index 6b07417..848c84a 100644 (file)
@@ -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()
 
index dcc67a5..c404fba 100644 (file)
@@ -128,6 +128,14 @@ BinaryExpression<RelationTypes::And, LeftExpression, RightExpression>
             (leftExpression, rightExpression);
 }
 
+template<typename LeftExpression, typename RightExpression>
+BinaryExpression<RelationTypes::Or, LeftExpression, RightExpression>
+    Or(const LeftExpression& leftExpression, const RightExpression& rightExpression)
+{
+    return BinaryExpression<RelationTypes::Or, LeftExpression, RightExpression>
+            (leftExpression, rightExpression);
+}
+
 template<typename ArgumentType>
 class __attribute__ ((visibility("hidden"))) ExpressionWithArgument : public Expression {
 protected: