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 {
34 void fillRow(T& row, const CustomHandler& handler, const DPL::String& pkgName)
36 row.Set_app_id(pkgName);
37 row.Set_target(handler.target);
38 row.Set_base_url(handler.base_url);
39 row.Set_url(handler.url);
40 row.Set_title(handler.title);
41 row.Set_user_allowed(handler.user_decision);
45 CustomHandlerDAO::CustomHandlerDAO(const DPL::String& pkgName) :
46 CustomHandlerDAOReadOnly(pkgName)
49 CustomHandlerDAO::~CustomHandlerDAO()
52 void CustomHandlerDAO::registerContentHandler(const CustomHandler& handler)
54 LogDebug("Registering content handler " << handler.target << " " <<
57 if (handler.user_decision & Agreed) {
58 //need to disable all previous, agreed entries
59 CUSTOM_HANDLER_DB_SELECT(select, ContentHandlers);
60 select->Where(And(Equals<ContentHandlers::target>(handler.target),
61 Or(Equals<ContentHandlers::user_allowed>(Agreed),
62 Equals<ContentHandlers::user_allowed>(
65 ContentHandlers::Select::RowList rows = select->GetRowList();
66 if (rows.size() > 1) {
67 //more than one activ content handler - not good. Remove all.
68 //this should never happen
69 LogError("Database data incoherent.");
70 CUSTOM_HANDLER_DB_DELETE(deleteContent, ContentHandlers);
71 deleteContent->Where(And(Equals<ContentHandlers::target>(
73 Or(Equals<ContentHandlers::
74 user_allowed>(Agreed),
75 Equals<ContentHandlers::
77 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.
86 Or(Equals<ContentHandlers::user_allowed>(
88 Equals<ContentHandlers::user_allowed>(
91 ContentHandlers::Row rowToUpdate = rows.front();
93 if (handler.user_decision & DecisionSaved) {
94 //do not ask about previous one
95 rowToUpdate.Set_user_allowed(DeclinedPermanently);
98 rowToUpdate.Set_user_allowed(Declined);
100 update->Values(rowToUpdate);
104 LogDebug("Inserting new content handler");
105 ContentHandlers::Row row;
106 fillRow(row, handler, m_pkgName);
107 if (getContentHandler(handler.target, handler.url, handler.base_url)) {
108 LogDebug("Content handler exist. Update its state");
109 CUSTOM_HANDLER_DB_UPDATE(updateRow, ContentHandlers);
110 updateRow->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
111 And(Equals<ContentHandlers::target>(handler.
113 And(Equals<ContentHandlers::url>(handler.
115 Equals<ContentHandlers::base_url>(
116 handler.base_url)))));
117 updateRow->Values(row);
118 updateRow->Execute();
122 CUSTOM_HANDLER_DB_INSERT(insert, ContentHandlers);
125 LogDebug("insterted");
127 Catch(DPL::DB::SqlConnection::Exception::Base){
128 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
129 "Failed to register custom handler");
133 void CustomHandlerDAO::registerProtocolHandler(const CustomHandler& handler)
135 LogDebug("Registering protocol handler " << handler.target << " " <<
138 if (handler.user_decision & Agreed) {
139 //need to disable all previous, agreed entries
140 CUSTOM_HANDLER_DB_SELECT(select, ProtocolHandlers);
141 select->Where(And(Equals<ProtocolHandlers::target>(handler.target),
142 Or(Equals<ProtocolHandlers::user_allowed>(Agreed),
143 Equals<ProtocolHandlers::user_allowed>(
146 ProtocolHandlers::Select::RowList rows = select->GetRowList();
147 if (rows.size() > 1) {
148 //more than one activ protocol handler - not good. Remove all.
149 //this should never happen
150 LogError("Database data incoherent.");
151 CUSTOM_HANDLER_DB_DELETE(deleteProtocol, ProtocolHandlers);
152 deleteProtocol->Where(And(Equals<ProtocolHandlers::target>(
154 Or(Equals<ProtocolHandlers::
155 user_allowed>(Agreed),
156 Equals<ProtocolHandlers::
158 AgreedPermanently))));
159 deleteProtocol->Execute();
160 //all protocol handlers removed. New one can be inserted
161 } else if (!rows.empty()) {
162 //one active handler. Can be updaed
163 CUSTOM_HANDLER_DB_UPDATE(update, ProtocolHandlers);
164 update->Where(And(Equals<ProtocolHandlers::target>(handler.
166 Or(Equals<ProtocolHandlers::user_allowed>(
168 Equals<ProtocolHandlers::user_allowed>(
171 ProtocolHandlers::Row rowToUpdate = rows.front();
173 if (handler.user_decision & DecisionSaved) {
174 //do not ask about previous one
175 rowToUpdate.Set_user_allowed(DeclinedPermanently);
178 rowToUpdate.Set_user_allowed(Declined);
180 update->Values(rowToUpdate);
184 LogDebug("Inserting new protocol handler");
185 ProtocolHandlers::Row row;
186 fillRow(row, handler, m_pkgName);
187 if (getProtocolHandler(handler.target, handler.url,
190 LogDebug("Protocol handler exist. Update its state");
191 CUSTOM_HANDLER_DB_UPDATE(updateRow, ProtocolHandlers);
192 updateRow->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
193 And(Equals<ProtocolHandlers::target>(handler.
195 And(Equals<ProtocolHandlers::url>(handler.
197 Equals<ProtocolHandlers::base_url>(
198 handler.base_url)))));
199 updateRow->Values(row);
200 updateRow->Execute();
204 CUSTOM_HANDLER_DB_INSERT(insert, ProtocolHandlers);
207 LogDebug("insterted");
209 Catch(DPL::DB::SqlConnection::Exception::Base){
210 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
211 "Failed to register custom handler");
215 void CustomHandlerDAO::unregisterContentHandler(const DPL::String& target,
216 const DPL::String& url)
218 LogDebug("Removing content handler " << target << " " << url);
220 CUSTOM_HANDLER_DB_DELETE(deleteFrom, ContentHandlers);
221 deleteFrom->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
222 And(Equals<ContentHandlers::target>(target),
223 Equals<ContentHandlers::url>(url))));
224 deleteFrom->Execute();
226 Catch(DPL::DB::SqlConnection::Exception::Base) {
227 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
228 "Failed to remove content handler");
232 void CustomHandlerDAO::unregisterProtocolHandler(const DPL::String& target,
233 const DPL::String& url)
235 LogDebug("Removing protocol handler " << target << " " << url);
237 CUSTOM_HANDLER_DB_DELETE(deleteFrom, ProtocolHandlers);
238 deleteFrom->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
239 And(Equals<ProtocolHandlers::target>(target),
240 Equals<ProtocolHandlers::url>(url))));
241 deleteFrom->Execute();
243 Catch(DPL::DB::SqlConnection::Exception::Base) {
244 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
245 "Failed to remove content handler");
249 void CustomHandlerDAO::unregisterContentHandler(const DPL::String& target,
250 const DPL::String& url,
251 const DPL::String& baseURL)
253 LogDebug("Removing content handler " << target << " " << url);
255 CUSTOM_HANDLER_DB_DELETE(deleteFrom, ContentHandlers);
256 deleteFrom->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
257 And(Equals<ContentHandlers::target>(target),
258 And(Equals<ContentHandlers::url>(url),
259 Equals<ContentHandlers::base_url>(baseURL)))));
260 deleteFrom->Execute();
262 Catch(DPL::DB::SqlConnection::Exception::Base) {
263 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
264 "Failed to remove content handler");
268 void CustomHandlerDAO::unregisterProtocolHandler(const DPL::String& target,
269 const DPL::String& url,
270 const DPL::String& baseURL)
272 LogDebug("Removing protocol handler " << target << " " << url);
274 CUSTOM_HANDLER_DB_DELETE(deleteFrom, ProtocolHandlers);
275 deleteFrom->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
276 And(Equals<ProtocolHandlers::target>(target),
277 And(Equals<ProtocolHandlers::url>(url),
278 Equals<ProtocolHandlers::base_url>(
280 deleteFrom->Execute();
282 Catch(DPL::DB::SqlConnection::Exception::Base) {
283 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
284 "Failed to remove content handler");
288 void CustomHandlerDAO::removeWidgetProtocolHandlers()
292 CUSTOM_HANDLER_DB_DELETE(deleteProtocol, ProtocolHandlers);
293 deleteProtocol->Where(Equals<ProtocolHandlers::app_id>(m_pkgName));
294 deleteProtocol->Execute();
295 } Catch(DPL::DB::SqlConnection::Exception::Base) {
296 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
297 "Failed to remove widget protoc");
301 void CustomHandlerDAO::removeWidgetContentHandlers()
305 CUSTOM_HANDLER_DB_DELETE(deleteContent, ContentHandlers);
306 deleteContent->Where(Equals<ContentHandlers::app_id>(m_pkgName));
307 deleteContent->Execute();
308 } Catch(DPL::DB::SqlConnection::Exception::Base) {
309 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
310 "Failed to remove widget entries");
313 } // namespace CustomHandlerDB