2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file custom_handler_dao.cpp
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
20 * @brief This file contains the definition of custom handler dao class.
23 #include <wrt-commons/custom-handler-dao-rw/custom_handler_dao.h>
24 #include <wrt-commons/custom-handler-dao-ro/CustomHandlerDatabase.h>
25 #include <orm_generator_custom_handler.h>
26 #include <wrt-commons/custom-handler-dao-ro/custom_handler_dao_read_only.h>
28 using namespace DPL::DB::ORM;
29 using namespace DPL::DB::ORM::custom_handler;
31 namespace CustomHandlerDB {
36 void fillRow(T& row, const CustomHandler& handler, const DPL::String& pkgName)
38 row.Set_app_id(pkgName);
39 row.Set_target(handler.target);
40 row.Set_base_url(handler.base_url);
41 row.Set_url(handler.url);
42 row.Set_title(handler.title);
43 row.Set_user_allowed(handler.user_decision);
48 CustomHandlerDAO::CustomHandlerDAO(const DPL::String& pkgName) :
49 CustomHandlerDAOReadOnly(pkgName)
53 CustomHandlerDAO::~CustomHandlerDAO()
57 void CustomHandlerDAO::registerContentHandler(const CustomHandler& handler)
59 LogDebug("Registering content handler " << handler.target << " " <<
62 if (handler.user_decision & Agreed) {
63 //need to disable all previous, agreed entries
64 CUSTOM_HANDLER_DB_SELECT(select, ContentHandlers);
65 select->Where(And(Equals<ContentHandlers::target>(handler.target),
66 Or(Equals<ContentHandlers::user_allowed>(Agreed),
67 Equals<ContentHandlers::user_allowed>(AgreedPermanently))
69 ContentHandlers::Select::RowList rows = select->GetRowList();
70 if (rows.size() > 1) {
71 //more than one activ content handler - not good. Remove all.
72 //this should never happen
73 LogError("Database data incoherent.");
74 CUSTOM_HANDLER_DB_DELETE(deleteContent, ContentHandlers);
75 deleteContent->Where(And(Equals<ContentHandlers::target>(handler.target),
76 Or(Equals<ContentHandlers::user_allowed>(Agreed),
77 Equals<ContentHandlers::user_allowed>(AgreedPermanently))));
78 deleteContent->Execute();
79 //all content handlers removed. New one can be inserted
80 } else if (!rows.empty()) {
81 //one active handler. Can be updaed
82 LogDebug("Activ content handler exist. Update");
83 CUSTOM_HANDLER_DB_UPDATE(update, ContentHandlers);
84 update->Where(And(Equals<ContentHandlers::target>(handler.target),
85 Or(Equals<ContentHandlers::user_allowed>(Agreed),
86 Equals<ContentHandlers::user_allowed>(AgreedPermanently))
88 ContentHandlers::Row rowToUpdate = rows.front();
90 if (handler.user_decision & DecisionSaved) {
91 //do not ask about previous one
92 rowToUpdate.Set_user_allowed(DeclinedPermanently);
95 rowToUpdate.Set_user_allowed(Declined);
97 update->Values(rowToUpdate);
101 LogDebug("Inserting new content handler");
102 ContentHandlers::Row row;
103 fillRow(row, handler, m_pkgName);
104 if (getContentHandler(handler.target, handler.url, handler.base_url)) {
105 LogDebug("Content handler exist. Update its state");
106 CUSTOM_HANDLER_DB_UPDATE(updateRow, ContentHandlers);
107 updateRow->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
108 And(Equals<ContentHandlers::target>(handler.target),
109 And(Equals<ContentHandlers::url>(handler.url),
110 Equals<ContentHandlers::base_url>(handler.base_url)))));
111 updateRow->Values(row);
112 updateRow->Execute();
116 CUSTOM_HANDLER_DB_INSERT(insert, ContentHandlers);
119 LogDebug("insterted");
121 Catch(DPL::DB::SqlConnection::Exception::Base){
122 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
123 "Failed to register custom handler");
127 void CustomHandlerDAO::registerProtocolHandler(const CustomHandler& handler)
129 LogDebug("Registering protocol handler " << handler.target << " " <<
133 if (handler.user_decision & Agreed) {
134 //need to disable all previous, agreed entries
135 CUSTOM_HANDLER_DB_SELECT(select, ProtocolHandlers);
136 select->Where(And(Equals<ProtocolHandlers::target>(handler.target),
137 Or(Equals<ProtocolHandlers::user_allowed>(Agreed),
138 Equals<ProtocolHandlers::user_allowed>(AgreedPermanently))
140 ProtocolHandlers::Select::RowList rows = select->GetRowList();
141 if (rows.size() > 1) {
142 //more than one activ protocol handler - not good. Remove all.
143 //this should never happen
144 LogError("Database data incoherent.");
145 CUSTOM_HANDLER_DB_DELETE(deleteProtocol, ProtocolHandlers);
146 deleteProtocol->Where(And(Equals<ProtocolHandlers::target>(handler.target),
147 Or(Equals<ProtocolHandlers::user_allowed>(Agreed),
148 Equals<ProtocolHandlers::user_allowed>(AgreedPermanently))));
149 deleteProtocol->Execute();
150 //all protocol handlers removed. New one can be inserted
151 } else if (!rows.empty()) {
152 //one active handler. Can be updaed
153 CUSTOM_HANDLER_DB_UPDATE(update, ProtocolHandlers);
154 update->Where(And(Equals<ProtocolHandlers::target>(handler.target),
155 Or(Equals<ProtocolHandlers::user_allowed>(Agreed),
156 Equals<ProtocolHandlers::user_allowed>(AgreedPermanently))
158 ProtocolHandlers::Row rowToUpdate = rows.front();
160 if (handler.user_decision & DecisionSaved) {
161 //do not ask about previous one
162 rowToUpdate.Set_user_allowed(DeclinedPermanently);
165 rowToUpdate.Set_user_allowed(Declined);
167 update->Values(rowToUpdate);
171 LogDebug("Inserting new protocol handler");
172 ProtocolHandlers::Row row;
173 fillRow(row, handler, m_pkgName);
174 if (getProtocolHandler(handler.target, handler.url, handler.base_url)) {
175 LogDebug("Protocol handler exist. Update its state");
176 CUSTOM_HANDLER_DB_UPDATE(updateRow, ProtocolHandlers);
177 updateRow->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
178 And(Equals<ProtocolHandlers::target>(handler.target),
179 And(Equals<ProtocolHandlers::url>(handler.url),
180 Equals<ProtocolHandlers::base_url>(handler.base_url)))));
181 updateRow->Values(row);
182 updateRow->Execute();
186 CUSTOM_HANDLER_DB_INSERT(insert, ProtocolHandlers);
189 LogDebug("insterted");
191 Catch(DPL::DB::SqlConnection::Exception::Base){
192 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
193 "Failed to register custom handler");
197 void CustomHandlerDAO::unregisterContentHandler(const DPL::String& target,
198 const DPL::String& url)
200 LogDebug("Removing content handler " << target << " " << url);
202 CUSTOM_HANDLER_DB_DELETE(deleteFrom, ContentHandlers);
203 deleteFrom->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
204 And(Equals<ContentHandlers::target>(target),
205 Equals<ContentHandlers::url>(url))));
206 deleteFrom->Execute();
208 Catch(DPL::DB::SqlConnection::Exception::Base) {
209 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
210 "Failed to remove content handler");
214 void CustomHandlerDAO::unregisterProtocolHandler(const DPL::String& target,
215 const DPL::String& url)
217 LogDebug("Removing protocol handler " << target << " " << url);
219 CUSTOM_HANDLER_DB_DELETE(deleteFrom, ProtocolHandlers);
220 deleteFrom->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
221 And(Equals<ProtocolHandlers::target>(target),
222 Equals<ProtocolHandlers::url>(url))));
223 deleteFrom->Execute();
225 Catch(DPL::DB::SqlConnection::Exception::Base) {
226 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
227 "Failed to remove content handler");
232 void CustomHandlerDAO::unregisterContentHandler(const DPL::String& target,
233 const DPL::String& url,
234 const DPL::String& baseURL)
236 LogDebug("Removing content handler " << target << " " << url);
238 CUSTOM_HANDLER_DB_DELETE(deleteFrom, ContentHandlers);
239 deleteFrom->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
240 And(Equals<ContentHandlers::target>(target),
241 And(Equals<ContentHandlers::url>(url),
242 Equals<ContentHandlers::base_url>(baseURL)))));
243 deleteFrom->Execute();
245 Catch(DPL::DB::SqlConnection::Exception::Base) {
246 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
247 "Failed to remove content handler");
251 void CustomHandlerDAO::unregisterProtocolHandler(const DPL::String& target,
252 const DPL::String& url,
253 const DPL::String& baseURL)
255 LogDebug("Removing protocol handler " << target << " " << url);
257 CUSTOM_HANDLER_DB_DELETE(deleteFrom, ProtocolHandlers);
258 deleteFrom->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
259 And(Equals<ProtocolHandlers::target>(target),
260 And(Equals<ProtocolHandlers::url>(url),
261 Equals<ProtocolHandlers::base_url>(baseURL)))));
262 deleteFrom->Execute();
264 Catch(DPL::DB::SqlConnection::Exception::Base) {
265 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
266 "Failed to remove content handler");
271 void CustomHandlerDAO::removeWidgetProtocolHandlers()
275 CUSTOM_HANDLER_DB_DELETE(deleteProtocol, ProtocolHandlers);
276 deleteProtocol->Where(Equals<ProtocolHandlers::app_id>(m_pkgName));
277 deleteProtocol->Execute();
279 } Catch(DPL::DB::SqlConnection::Exception::Base) {
280 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
281 "Failed to remove widget protoc");
285 void CustomHandlerDAO::removeWidgetContentHandlers()
289 CUSTOM_HANDLER_DB_DELETE(deleteContent, ContentHandlers);
290 deleteContent->Where(Equals<ContentHandlers::app_id>(m_pkgName));
291 deleteContent->Execute();
293 } Catch(DPL::DB::SqlConnection::Exception::Base) {
294 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
295 "Failed to remove widget entries");
300 } // namespace CustomHandlerDB