DPL::DB::SqlConnection::Flag::UseLucene;
}
-DPL::Mutex g_CustomHandlerDbQueriesMutex;
-DPL::DB::ThreadDatabaseSupport m_customHandlerdbInterface(
- CustomHandler_DB_DATABASE, CustomHandler_DB_FLAGS);
-
+DPL::Mutex g_dbQueriesMutex;
+DPL::DB::ThreadDatabaseSupport g_dbInterface(CustomHandler_DB_DATABASE,
+ CustomHandler_DB_FLAGS);
void attachDatabaseRO()
{
- m_customHandlerdbInterface.AttachToThread(DPL::DB::SqlConnection::Flag::RO);
+ g_dbInterface.AttachToThread(DPL::DB::SqlConnection::Flag::RO);
}
void attachDatabaseRW()
{
- m_customHandlerdbInterface.AttachToThread(DPL::DB::SqlConnection::Flag::RW);
+ g_dbInterface.AttachToThread(DPL::DB::SqlConnection::Flag::RW);
}
void detachDatabase()
{
- m_customHandlerdbInterface.DetachFromThread();
+ g_dbInterface.DetachFromThread();
}
} //namespace Interface
#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>
+
+using namespace DPL::DB::ORM;
+using namespace DPL::DB::ORM::custom_handler;
namespace CustomHandlerDB {
+namespace {
+
+template <typename T>
+void fillRow(T& row, const CustomHandler& handler, const DPL::String& pkgName)
+{
+ row.Set_app_id(pkgName);
+ row.Set_target(handler.target);
+ row.Set_base_url(handler.base_url);
+ row.Set_url(handler.url);
+ row.Set_title(handler.title);
+ row.Set_user_allowed(handler.user_allowed);
+}
+
+} // namespace
+
CustomHandlerDAO::CustomHandlerDAO(const DPL::String& pkgName) :
CustomHandlerDAOReadOnly(pkgName)
{
{
}
+void CustomHandlerDAO::registerContentHandler(const CustomHandler& handler)
+{
+ LogDebug("Registering content handler " << handler.target << " " <<
+ handler.base_url);
+ Try {
+ ContentHandlers::Row row;
+ fillRow(row, handler, m_pkgName);
+
+ // TODO remove previous if necessary
+ CUSTOM_HANDLER_DB_INSERT(insert, ContentHandlers);
+ insert->Values(row);
+ insert->Execute();
+ }
+ Catch(DPL::DB::SqlConnection::Exception::Base){
+ ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
+ "Failed to register custom handler");
+ }
+}
+
+void CustomHandlerDAO::registerProtocolHandler(const CustomHandler& handler)
+{
+ LogDebug("Registering protocol handler " << handler.target << " " <<
+ handler.base_url);
+ Try {
+ ProtocolHandlers::Row row;
+ fillRow(row, handler, m_pkgName);
+
+ // TODO remove previous if necessary
+ CUSTOM_HANDLER_DB_INSERT(insert, ProtocolHandlers);
+ insert->Values(row);
+ insert->Execute();
+ }
+ Catch(DPL::DB::SqlConnection::Exception::Base){
+ ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
+ "Failed to register custom handler");
+ }
+}
+
+void CustomHandlerDAO::unregisterContentHandler(const DPL::String& target,
+ const DPL::String& url)
+{
+ 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),
+ Equals<ContentHandlers::url>(url))));
+ 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)
+{
+ 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),
+ Equals<ProtocolHandlers::url>(url))));
+ deleteFrom->Execute();
+ }
+ Catch(DPL::DB::SqlConnection::Exception::Base) {
+ ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
+ "Failed to remove content handler");
+ }
+
+}
+
} // namespace CustomHandlerDB
#include <wrt-commons/custom-handler-dao-ro/custom_handler_dao_read_only.h>
#include <wrt-commons/custom-handler-dao-ro/CustomHandlerDatabase.h>
+#include <orm_generator_custom_handler.h>
+
+using namespace DPL::DB::ORM;
+using namespace DPL::DB::ORM::custom_handler;
+
namespace CustomHandlerDB {
-CustomHandlerDAOReadOnly::CustomHandlerDAOReadOnly(const DPL::String& pkgName)
+namespace {
+
+template <typename T>
+CustomHandlerPtr getSingleHandler(std::list<T> row)
+{
+ CustomHandlerPtr handler;
+ if (!row.empty()) {
+ // There should be only one
+ if (row.size() > 1) {
+ ThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError,
+ "More than one handler registered");
+ }
+
+ handler.reset(new CustomHandler());
+ handler->target = row.front().Get_target();
+ 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();
+ }
+ return handler;
+}
+
+} // namespace
+
+CustomHandlerDAOReadOnly::CustomHandlerDAOReadOnly(const DPL::String& pkgName) :
+ m_pkgName(pkgName)
{
}
{
}
+CustomHandlerPtr CustomHandlerDAOReadOnly::getProtocolHandler(
+ const DPL::String& protocol,
+ const DPL::String& url)
+{
+ LogDebug("Getting protocol handler");
+ Try{
+ CUSTOM_HANDLER_DB_SELECT(select, ProtocolHandlers);
+
+ select->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
+ And(Equals<ProtocolHandlers::target>(protocol),
+ Equals<ProtocolHandlers::url>(url))));
+
+ std::list<ProtocolHandlers::Row> list = select->GetRowList();
+ return getSingleHandler(list);
+ } 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)
+{
+ LogDebug("Getting content handler");
+ Try{
+ CUSTOM_HANDLER_DB_SELECT(select, ContentHandlers);
+
+ select->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
+ And(Equals<ContentHandlers::target>(content),
+ Equals<ContentHandlers::url>(url))));
+
+ 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)
+{
+ LogDebug("Getting allowed protocol handler");
+ Try{
+ CUSTOM_HANDLER_DB_SELECT(select, ProtocolHandlers);
+
+ select->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
+ And(Equals<ProtocolHandlers::target>(protocol),
+ Equals<ProtocolHandlers::user_allowed>(true))));
+
+ 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::getAllowedContentHandler(
+ const DPL::String& protocol)
+{
+ LogDebug("Getting allowed content handler");
+ Try{
+ CUSTOM_HANDLER_DB_SELECT(select, ContentHandlers)
+
+ select->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
+ And(Equals<ContentHandlers::target>(protocol),
+ Equals<ContentHandlers::user_allowed>(true))));
+
+ 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");
+ }
+}
+
} // namespace CustomHandlerDB
#include <dpl/mutex.h>
#include <dpl/db/thread_database_support.h>
-extern DPL::Mutex g_CustomHandlerDbQueriesMutex;
-
-#define CUSTOM_HANDLER_DB_INTERNAL(tlsCommand, InternalType, interface) \
- static DPL::ThreadLocalVariable<InternalType> *tlsCommand ## Ptr = NULL; \
- { \
- DPL::Mutex::ScopedLock lock(&customHandlerDbQueriesMutex); \
- if (!tlsCommand ## Ptr) { \
- static DPL::ThreadLocalVariable<InternalType> tmp; \
- tlsCommand ## Ptr = &tmp; \
- } \
- } \
- DPL::ThreadLocalVariable<InternalType> &tlsCommand = *tlsCommand ## Ptr; \
- if (tlsCommand.IsNull()) { tlsCommand = InternalType(interface); }
-
-#define CUSTOM_HANDLER_DB_SELECT(name, type, interface) \
- CUSTOM_HANDLER_DB_INTERNAL(name, type::Select, interface)
-
-#define CUSTOM_HANDLER_DB_INSERT(name, type, interface) \
- CUSTOM_HANDLER_DB_INTERNAL(name, type::Insert, interface)
-
-#define CUSTOM_HANDLER_DB_UPDATE(name, type, interface) \
- CUSTOM_HANDLER_DB_INTERNAL(name, type::Update, interface)
-
-#define CUSTOM_HANDLER_DB_DELETE(name, type, interface) \
- CUSTOM_HANDLER_DB_INTERNAL(name, type::Delete, interface)
-
namespace CustomHandlerDB {
namespace Interface {
void attachDatabaseRW();
void detachDatabase();
-extern DPL::DB::ThreadDatabaseSupport m_customHandlerdbInterface;
+extern DPL::Mutex g_dbQueriesMutex;
+extern DPL::DB::ThreadDatabaseSupport g_dbInterface;
} // namespace Interface
} // namespace CustomHandlerDB
+#define CUSTOM_HANDLER_DB_INTERNAL(tlsCommand, InternalType) \
+ static DPL::ThreadLocalVariable<InternalType> *tlsCommand ## Ptr = NULL; \
+ { \
+ DPL::Mutex::ScopedLock lock( \
+ &CustomHandlerDB::Interface::g_dbQueriesMutex); \
+ if (!tlsCommand ## Ptr) { \
+ static DPL::ThreadLocalVariable<InternalType> tmp; \
+ tlsCommand ## Ptr = &tmp; \
+ } \
+ } \
+ DPL::ThreadLocalVariable<InternalType> &tlsCommand = *tlsCommand ## Ptr; \
+ if (tlsCommand.IsNull()) \
+ { \
+ tlsCommand = InternalType(&CustomHandlerDB::Interface::g_dbInterface); \
+ }
+
+#define CUSTOM_HANDLER_DB_SELECT(name, type) \
+ CUSTOM_HANDLER_DB_INTERNAL(name, type::Select)
+
+#define CUSTOM_HANDLER_DB_INSERT(name, type) \
+ CUSTOM_HANDLER_DB_INTERNAL(name, type::Insert)
+
+#define CUSTOM_HANDLER_DB_UPDATE(name, type) \
+ CUSTOM_HANDLER_DB_INTERNAL(name, type::Update)
+
+#define CUSTOM_HANDLER_DB_DELETE(name, type) \
+ CUSTOM_HANDLER_DB_INTERNAL(name, type::Delete)
+
#endif /* _CUSTOM_HANDLER_DATABASE_H_ */
#ifndef SHARE_COMMON_DAO_TYPES_H_
#define SHARE_COMMON_DAO_TYPES_H_
+#include <list>
+#include <memory>
+#include <dpl/string.h>
+
namespace CustomHandlerDB {
+/**
+ * @brief Custom Handler struct
+ *
+ * Describes custom handler for protocol and content.
+ */
+struct CustomHandler
+{
+ DPL::String target; // protocol/content ("mailto"/"application/x-soup")
+ DPL::String base_url; // base url of registered page
+ 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
+};
+
+typedef std::shared_ptr<CustomHandler> CustomHandlerPtr;
+
} // namespace CustomHandlerDB
#endif /* SHARE_COMMON_DAO_TYPES_H_ */
#include <dpl/string.h>
#include <dpl/exception.h>
+#include "common_dao_types.h"
namespace CustomHandlerDB {
explicit CustomHandlerDAOReadOnly(const DPL::String& pkgName);
virtual ~CustomHandlerDAOReadOnly();
- // TODO
+ /**
+ * Returns protocol handler
+ */
+ CustomHandlerPtr getProtocolHandler(const DPL::String& protocol,
+ const DPL::String& url);
+
+ /**
+ * Returns content handler
+ */
+ CustomHandlerPtr getContentHandler(const DPL::String& content,
+ const DPL::String& url);
+
+ /**
+ * Returns allowed handler for given protocol
+ */
+ CustomHandlerPtr getAllowedProtocolHandler(const DPL::String& protocol);
+
+ /**
+ * Returns allowed handler for given content
+ */
+ CustomHandlerPtr getAllowedContentHandler(const DPL::String& protocol);
+
+ protected:
+ DPL::String m_pkgName;
};
} // namespace CustomHandlerDB
explicit CustomHandlerDAO(const DPL::String& pkgName);
virtual ~CustomHandlerDAO();
- // TODO
+ /**
+ * Registers custom content handler
+ */
+ void registerContentHandler(const CustomHandler& handler);
+
+ /**
+ * Registers custom protocol handler
+ */
+ void registerProtocolHandler(const CustomHandler& handler);
+
+ /**
+ * Unregisters custom content handler
+ */
+ void unregisterContentHandler(const DPL::String& target,
+ const DPL::String& burl);
+ /**
+ * Unregisters custom protocol handler
+ */
+ void unregisterProtocolHandler(const DPL::String& target,
+ const DPL::String& url);
};
} // namespace CustomHandlerDB