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 WrtLogD("Registering content handler %ls %ls",
55 handler.target.c_str(), handler.base_url.c_str());
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 WrtLogE("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 WrtLogD("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 WrtLogD("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 WrtLogD("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 WrtLogD("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 WrtLogD("Registering protocol handler %ls %ls", handler.target.c_str(), handler.base_url.c_str());
137 if (handler.user_decision & Agreed) {
138 //need to disable all previous, agreed entries
139 CUSTOM_HANDLER_DB_SELECT(select, ProtocolHandlers);
140 select->Where(And(Equals<ProtocolHandlers::target>(handler.target),
141 Or(Equals<ProtocolHandlers::user_allowed>(Agreed),
142 Equals<ProtocolHandlers::user_allowed>(
145 ProtocolHandlers::Select::RowList rows = select->GetRowList();
146 if (rows.size() > 1) {
147 //more than one activ protocol handler - not good. Remove all.
148 //this should never happen
149 WrtLogE("Database data incoherent.");
150 CUSTOM_HANDLER_DB_DELETE(deleteProtocol, ProtocolHandlers);
151 deleteProtocol->Where(And(Equals<ProtocolHandlers::target>(
153 Or(Equals<ProtocolHandlers::
154 user_allowed>(Agreed),
155 Equals<ProtocolHandlers::
157 AgreedPermanently))));
158 deleteProtocol->Execute();
159 //all protocol handlers removed. New one can be inserted
160 } else if (!rows.empty()) {
161 //one active handler. Can be updaed
162 CUSTOM_HANDLER_DB_UPDATE(update, ProtocolHandlers);
163 update->Where(And(Equals<ProtocolHandlers::target>(handler.
165 Or(Equals<ProtocolHandlers::user_allowed>(
167 Equals<ProtocolHandlers::user_allowed>(
170 ProtocolHandlers::Row rowToUpdate = rows.front();
172 if (handler.user_decision & DecisionSaved) {
173 //do not ask about previous one
174 rowToUpdate.Set_user_allowed(DeclinedPermanently);
177 rowToUpdate.Set_user_allowed(Declined);
179 update->Values(rowToUpdate);
183 WrtLogD("Inserting new protocol handler");
184 ProtocolHandlers::Row row;
185 fillRow(row, handler, m_pkgName);
186 if (getProtocolHandler(handler.target, handler.url,
189 WrtLogD("Protocol handler exist. Update its state");
190 CUSTOM_HANDLER_DB_UPDATE(updateRow, ProtocolHandlers);
191 updateRow->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
192 And(Equals<ProtocolHandlers::target>(handler.
194 And(Equals<ProtocolHandlers::url>(handler.
196 Equals<ProtocolHandlers::base_url>(
197 handler.base_url)))));
198 updateRow->Values(row);
199 updateRow->Execute();
203 CUSTOM_HANDLER_DB_INSERT(insert, ProtocolHandlers);
206 WrtLogD("insterted");
208 Catch(DPL::DB::SqlConnection::Exception::Base){
209 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
210 "Failed to register custom handler");
214 void CustomHandlerDAO::unregisterContentHandler(const DPL::String& target,
215 const DPL::String& url)
217 WrtLogD("Removing content handler %ls %ls", target.c_str(), url.c_str());
219 CUSTOM_HANDLER_DB_DELETE(deleteFrom, ContentHandlers);
220 deleteFrom->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
221 And(Equals<ContentHandlers::target>(target),
222 Equals<ContentHandlers::url>(url))));
223 deleteFrom->Execute();
225 Catch(DPL::DB::SqlConnection::Exception::Base) {
226 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
227 "Failed to remove content handler");
231 void CustomHandlerDAO::unregisterProtocolHandler(const DPL::String& target,
232 const DPL::String& url)
234 WrtLogD("Removing protocol handler %ls %ls", target.c_str(), url.c_str());
236 CUSTOM_HANDLER_DB_DELETE(deleteFrom, ProtocolHandlers);
237 deleteFrom->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
238 And(Equals<ProtocolHandlers::target>(target),
239 Equals<ProtocolHandlers::url>(url))));
240 deleteFrom->Execute();
242 Catch(DPL::DB::SqlConnection::Exception::Base) {
243 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
244 "Failed to remove content handler");
248 void CustomHandlerDAO::unregisterContentHandler(const DPL::String& target,
249 const DPL::String& url,
250 const DPL::String& baseURL)
252 WrtLogD("Removing content handler %ls %ls", target.c_str(), url.c_str());
254 CUSTOM_HANDLER_DB_DELETE(deleteFrom, ContentHandlers);
255 deleteFrom->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
256 And(Equals<ContentHandlers::target>(target),
257 And(Equals<ContentHandlers::url>(url),
258 Equals<ContentHandlers::base_url>(baseURL)))));
259 deleteFrom->Execute();
261 Catch(DPL::DB::SqlConnection::Exception::Base) {
262 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
263 "Failed to remove content handler");
267 void CustomHandlerDAO::unregisterProtocolHandler(const DPL::String& target,
268 const DPL::String& url,
269 const DPL::String& baseURL)
271 WrtLogD("Removing protocol handler %ls %ls", target.c_str(), url.c_str());
273 CUSTOM_HANDLER_DB_DELETE(deleteFrom, ProtocolHandlers);
274 deleteFrom->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
275 And(Equals<ProtocolHandlers::target>(target),
276 And(Equals<ProtocolHandlers::url>(url),
277 Equals<ProtocolHandlers::base_url>(
279 deleteFrom->Execute();
281 Catch(DPL::DB::SqlConnection::Exception::Base) {
282 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
283 "Failed to remove content handler");
287 void CustomHandlerDAO::removeWidgetProtocolHandlers()
291 CUSTOM_HANDLER_DB_DELETE(deleteProtocol, ProtocolHandlers);
292 deleteProtocol->Where(Equals<ProtocolHandlers::app_id>(m_pkgName));
293 deleteProtocol->Execute();
294 } Catch(DPL::DB::SqlConnection::Exception::Base) {
295 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
296 "Failed to remove widget protoc");
300 void CustomHandlerDAO::removeWidgetContentHandlers()
304 CUSTOM_HANDLER_DB_DELETE(deleteContent, ContentHandlers);
305 deleteContent->Where(Equals<ContentHandlers::app_id>(m_pkgName));
306 deleteContent->Execute();
307 } Catch(DPL::DB::SqlConnection::Exception::Base) {
308 ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
309 "Failed to remove widget entries");
312 } // namespace CustomHandlerDB