tizen beta release
[framework/web/wrt-commons.git] / modules / widget_dao / dao / global_dao.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file   global_dao.cpp
18  * @author  Pawel Sikorski (p.sikorski@samsung.com)
19  * @version 1.0
20  * @brief   This file contains the definition of global dao class.
21  */
22
23 #include <dpl/wrt-dao-rw/global_dao.h>
24 #include <dpl/log/log.h>
25 #include <dpl/string.h>
26 #include <dpl/db/orm.h>
27 #include <orm_generator_wrt.h>
28 #include <dpl/wrt-dao-ro/webruntime_database.h>
29 #include <dpl/wrt-dao-ro/WrtDatabase.h>
30
31 namespace WrtDB {
32
33 void GlobalDAO::AddAdultBlackListElement(const DPL::String &url)
34 {
35     Try {
36         using namespace DPL::DB::ORM;
37         using namespace DPL::DB::ORM::wrt;
38
39         ScopedTransaction transaction(&WrtDatabase::interface());
40         if (IsElementOnAdultBlackList(url)) {
41             transaction.Commit();
42             return;
43         }
44
45         WRT_DB_INSERT(insert, ChildProtectionBlacklist, &WrtDatabase::interface())
46         ChildProtectionBlacklist::Row row;
47         row.Set_url(url);
48
49         insert->Values(row);
50         insert->Execute();
51         transaction.Commit();
52     } Catch(DPL::DB::SqlConnection::Exception::Base) {
53         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
54                    "Failed to add element to AdultBlackList");
55     }
56 }
57
58 void GlobalDAO::RemoveAdultBlackListElement(const DPL::String &url)
59 {
60     Try {
61         using namespace DPL::DB::ORM;
62         using namespace DPL::DB::ORM::wrt;
63         WRT_DB_DELETE(deleteFrom, ChildProtectionBlacklist, &WrtDatabase::interface())
64
65         deleteFrom->Where(Equals<ChildProtectionBlacklist::url>(url));
66         deleteFrom->Execute();
67     } Catch(DPL::DB::SqlConnection::Exception::Base) {
68         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
69                    "Failed to removed element from AdultBlackList");
70     }
71 }
72
73 void GlobalDAO::UpdateAdultBlackList(const DPL::String &oldUrl,
74                                      const DPL::String &newUrl)
75 {
76     Try {
77         using namespace DPL::DB::ORM;
78         using namespace DPL::DB::ORM::wrt;
79
80         ScopedTransaction transaction(&WrtDatabase::interface());
81         if (IsElementOnAdultBlackList(newUrl)) {
82             transaction.Commit();
83             return;
84         }
85         WRT_DB_UPDATE(update, ChildProtectionBlacklist, &WrtDatabase::interface())
86         ChildProtectionBlacklist::Row row;
87         row.Set_url(newUrl);
88
89         update->Where(Equals<ChildProtectionBlacklist::url>(oldUrl));
90         update->Values(row);
91         update->Execute();
92         transaction.Commit();
93     } Catch(DPL::DB::SqlConnection::Exception::Base) {
94         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
95                    "Failed to update an element in AdultBlackList");
96     }
97 }
98
99 void GlobalDAO::SetDeveloperMode(bool mode)
100 {
101     LogDebug("updating Developer mode to:" << mode);
102     Try {
103         using namespace DPL::DB::ORM;
104         using namespace DPL::DB::ORM::wrt;
105         GlobalProperties::Row row;
106         row.Set_developer_mode(mode);
107
108         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
109         update->Values(row);
110         update->Execute();
111     }
112     Catch(DPL::DB::SqlConnection::Exception::Base){
113         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
114                    "Failed to update Developer Mode");
115     }
116 }
117
118 void GlobalDAO::AddDefferedWidgetPackageInstallation(const DPL::String &path)
119 {
120     LogDebug("Adding widget package as defered. Path: " << path);
121     Try {
122         using namespace DPL::DB::ORM;
123         using namespace DPL::DB::ORM::wrt;
124         DefferedWidgetPackageInstallation::Row row;
125         row.Set_path(path);
126
127         WRT_DB_INSERT(insert, DefferedWidgetPackageInstallation, &WrtDatabase::interface())
128         insert->Values(row);
129         insert->Execute();
130     }
131     Catch(DPL::DB::SqlConnection::Exception::Base){
132         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
133                    "Failed to add defered widget package");
134     }
135 }
136
137 void GlobalDAO::RemoveDefferedWidgetPackageInstallation(const DPL::String &path)
138 {
139     LogDebug("Remove widget package from differed list. Path: " << path);
140     Try {
141         using namespace DPL::DB::ORM;
142         using namespace DPL::DB::ORM::wrt;
143         WRT_DB_DELETE(del, DefferedWidgetPackageInstallation, &WrtDatabase::interface())
144         del->Where(Equals<DefferedWidgetPackageInstallation::path>(path));
145         del->Execute();
146     }
147     Catch(DPL::DB::SqlConnection::Exception::Base){
148         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
149                    "Failed to remove defered widget package");
150     }
151 }
152
153 void GlobalDAO::SetParentalMode(bool parental_status)
154 {
155     Try {
156         using namespace DPL::DB::ORM;
157         using namespace DPL::DB::ORM::wrt;
158         GlobalProperties::Row row;
159         row.Set_parental_mode(parental_status);
160         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
161
162         update->Values(row);
163         update->Execute();
164     } Catch(DPL::DB::SqlConnection::Exception::Base) {
165         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
166                    "Failed to update Parental Mode");
167     }
168 }
169
170 void GlobalDAO::SetParentalAllowedAge(const DPL::OptionalInt& age)
171 {
172     using namespace DPL::DB::ORM;
173     using namespace DPL::DB::ORM::wrt;
174     Try {
175         typedef wrt::GlobalProperties::Row ParentalAllowedAgeRow;
176
177         ParentalAllowedAgeRow row;
178         row.Set_parental_allowed_age(age);
179         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
180         update->Values(row);
181         update->Execute();
182     } Catch(DPL::DB::SqlConnection::Exception::Base) {
183         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
184                    "Failed to update Parental Mode");
185     }
186 }
187
188 void GlobalDAO::SetSecureByDefault(bool secure)
189 {
190     //If secure == true -> widget does not need to be signed
191     Try {
192         using namespace DPL::DB::ORM;
193         using namespace DPL::DB::ORM::wrt;
194         GlobalProperties::Row row;
195         row.Set_secure_by_default(secure);
196         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
197         update->Values(row);
198         update->Execute();
199     }
200     Catch(DPL::DB::SqlConnection::Exception::Base) {
201         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
202                    "Failed to update secureByDefault");
203     }
204 }
205
206 void GlobalDAO::setComplianceMode(bool mode)
207 {
208     LogDebug("Updating compliance mode to:" << mode);
209     Try{
210         using namespace DPL::DB::ORM; using namespace DPL::DB::ORM::wrt;
211         GlobalProperties::Row row;
212         row.Set_compliance_mode(mode);
213
214         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
215         update->Values(row);
216         update->Execute();
217     }
218     Catch (DPL::DB::SqlConnection::Exception::Base){
219         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
220                    "Failed to update compliance mode");
221     }
222 }
223
224 void GlobalDAO::setComplianceFakeImei(const std::string &imei)
225 {
226     LogDebug("Setting compliance fake IMEI: " << imei);
227     Try{
228         using namespace DPL::DB::ORM; using namespace DPL::DB::ORM::wrt;
229         GlobalProperties::Row row;
230         row.Set_compliance_fake_imei(DPL::FromASCIIString(imei));
231
232         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
233         update->Values(row);
234         update->Execute();
235     }
236     Catch (DPL::DB::SqlConnection::Exception::Base){
237         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
238                    "Failed to update compliance fake IMEI");
239     }
240 }
241
242 void GlobalDAO::setComplianceFakeMeid(const std::string &meid)
243 {
244     LogDebug("Setting compliance fake MEID: " << meid);
245     Try{
246         using namespace DPL::DB::ORM; using namespace DPL::DB::ORM::wrt;
247         GlobalProperties::Row row;
248         row.Set_compliance_fake_meid(DPL::FromASCIIString(meid));
249
250         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
251         update->Values(row);
252         update->Execute();
253     }
254     Catch (DPL::DB::SqlConnection::Exception::Base){
255         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
256                    "Failed to update compliance fake MEID");
257     }
258 }
259
260 void GlobalDAO::AddCategoryRule(const ChildProtection::PowderRules::
261                                 CategoryRule& powder)
262 {
263     Try {
264         using namespace DPL::DB::ORM;
265         using namespace DPL::DB::ORM::wrt;
266         PowderRules::Row row;
267         row.Set_category(powder.category);
268         row.Set_level(powder.level);
269         row.Set_context(powder.context);
270
271         wrt::ScopedTransaction transaction(&WrtDatabase::interface());
272         if (IsPowderRulePresent(powder)) {
273             transaction.Commit();
274             return;
275         }
276         WRT_DB_INSERT(insert, PowderRules, &WrtDatabase::interface())
277
278         insert->Values(row);
279         insert->Execute();
280         transaction.Commit();
281     } Catch(DPL::DB::SqlConnection::Exception::Base) {
282         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
283                    "Failed to add POWDER rules");
284     }
285 }
286
287 void GlobalDAO::RemoveCategoryRule(const ChildProtection::PowderRules::
288                                    CategoryRule& powder)
289 {
290     Try {
291         using namespace DPL::DB::ORM;
292         using namespace DPL::DB::ORM::wrt;
293
294         if (!powder.context.IsNull()) {
295             WRT_DB_DELETE(delWithContext, PowderRules, &WrtDatabase::interface())
296
297             delWithContext->Where(
298                 And(Equals<PowderRules::category>(powder.category),
299                     And(Equals<PowderRules::level>(powder.level),
300                         Equals<PowderRules::context>(powder.context))));
301             delWithContext->Execute();
302         } else {
303             WRT_DB_DELETE(delWithoutContext, PowderRules, &WrtDatabase::interface())
304             delWithoutContext->Where(
305                 And(Equals<PowderRules::category>(powder.category),
306                     And(Equals<PowderRules::level>(powder.level),
307                         Is<PowderRules::context>(powder.context))));
308             delWithoutContext->Execute();
309         }
310     } Catch(DPL::DB::SqlConnection::Exception::Base) {
311         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
312                    "Failed to remove POWDER rules");
313     }
314 }
315
316 void GlobalDAO::UpdateCategoryRule(
317         const ChildProtection::PowderRules::CategoryRule& oldRule,
318         const ChildProtection::PowderRules::CategoryRule& newRule)
319 {
320     Try {
321         using namespace DPL::DB::ORM;
322         using namespace DPL::DB::ORM::wrt;
323         PowderRules::Row row;
324         row.Set_category(newRule.category);
325         row.Set_level(newRule.level);
326         row.Set_context(newRule.context);
327
328         wrt::ScopedTransaction transaction(&WrtDatabase::interface());
329
330         if (IsPowderRulePresent(newRule)) {
331             transaction.Commit();
332             return;
333         }
334
335         if (!oldRule.context.IsNull()) {
336             WRT_DB_UPDATE(updateWithContext, PowderRules, &WrtDatabase::interface())
337             updateWithContext->Where(
338                 And(Equals<PowderRules::category>(oldRule.category),
339                     And(Equals<PowderRules::level>(oldRule.level),
340                         Equals<PowderRules::context>(oldRule.context))));
341             updateWithContext->Values(row);
342             updateWithContext->Execute();
343         } else {
344             WRT_DB_UPDATE(updateWithoutContext, PowderRules, &WrtDatabase::interface())
345             updateWithoutContext->Where(
346                 And(Equals<PowderRules::category>(oldRule.category),
347                     And(Equals<PowderRules::level>(oldRule.level),
348                         Is<PowderRules::context>(oldRule.context))));
349             updateWithoutContext->Values(row);
350             updateWithoutContext->Execute();
351         }
352         transaction.Commit();
353     } Catch(DPL::DB::SqlConnection::Exception::Base) {
354         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
355                    "Failed to update POWDER rules");
356     }
357 }
358
359 void GlobalDAO::SetHomeNetworkDataUsage(GlobalDAO::NetworkAccessMode newMode)
360 {
361     LogDebug("updating home network data usage to:" << newMode);
362     Try {
363         using namespace DPL::DB::ORM;
364         using namespace DPL::DB::ORM::wrt;
365         GlobalProperties::Row row;
366         row.Set_home_network_data_usage(static_cast<int>(newMode));
367
368         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
369         update->Values(row);
370         update->Execute();
371     }
372     Catch(DPL::DB::SqlConnection::Exception::Base){
373         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
374                    "Failed to update home network data usage");
375     }
376 }
377
378 void GlobalDAO::SetRoamingDataUsage(GlobalDAO::NetworkAccessMode newMode)
379 {
380     LogDebug("updating roaming network data usage to:" << newMode);
381     Try {
382         using namespace DPL::DB::ORM;
383         using namespace DPL::DB::ORM::wrt;
384         GlobalProperties::Row row;
385         row.Set_roaming_data_usage(static_cast<int>(newMode));
386
387         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
388         update->Values(row);
389         update->Execute();
390     }
391     Catch(DPL::DB::SqlConnection::Exception::Base){
392         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
393                    "Failed to update roaming network data usage");
394     }
395 }
396
397 void GlobalDAO::SetAutoSaveIdPasswd(const DPL::String &url,
398                                     const AutoSaveData &saveData)
399 {
400     Try {
401         using namespace DPL::DB::ORM;
402         using namespace DPL::DB::ORM::wrt;
403         ScopedTransaction transaction(&WrtDatabase::interface());
404         AutoSaveIdPasswd::Row row;
405
406         row.Set_address(url);
407         row.Set_userId(saveData.userId);
408         row.Set_passwd(saveData.passwd);
409
410         DPL::Optional<GlobalDAO::AutoSaveData> savedData =
411             GetAutoSaveIdPasswd(url);
412
413         if (!savedData.IsNull()) {
414             WRT_DB_UPDATE(update, AutoSaveIdPasswd, &WrtDatabase::interface())
415
416             update->Where(Equals<AutoSaveIdPasswd::address>(url));
417             update->Values(row);
418             update->Execute();
419         } else {
420             WRT_DB_INSERT(insert, AutoSaveIdPasswd, &WrtDatabase::interface());
421             insert->Values(row);
422             insert->Execute();
423         }
424
425         transaction.Commit();
426     } Catch(DPL::DB::SqlConnection::Exception::Base) {
427         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
428                    "Fail to register id, passwd for autosave");
429     }
430 }
431
432 void GlobalDAO::AddWhiteURI(const std::string &uri, bool subDomain)
433 {
434     LogDebug("Add White URI : " << uri);
435     Try {
436         using namespace DPL::DB::ORM;
437         using namespace DPL::DB::ORM::wrt;
438         WidgetWhiteURIList::Row row;
439         row.Set_uri(DPL::FromASCIIString(uri));
440         row.Set_subdomain_access(static_cast<int>(subDomain));
441         wrt::ScopedTransaction transaction(&WrtDatabase::interface());
442
443         WRT_DB_INSERT(insert, WidgetWhiteURIList, &WrtDatabase::interface())
444
445         insert->Values(row);
446         insert->Execute();
447         transaction.Commit();
448     }
449     Catch(DPL::DB::SqlConnection::Exception::Base){
450         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
451                    "Failed to add white URI");
452     }
453 }
454
455 void GlobalDAO::RemoveWhiteURI(const std::string &uri)
456 {
457     LogDebug("Remove White URI : " << uri);
458     Try {
459         using namespace DPL::DB::ORM;
460         using namespace DPL::DB::ORM::wrt;
461
462         WRT_DB_DELETE(deleteFrom, WidgetWhiteURIList, &WrtDatabase::interface())
463         deleteFrom->Where(Equals<WidgetWhiteURIList::uri>(DPL::FromASCIIString(uri)));
464         deleteFrom->Execute();
465     } Catch(DPL::DB::SqlConnection::Exception::Base) {
466         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
467                    "Failed to removed white URI from AdultBlackList");
468     }
469 }
470
471 } // namespace WrtDB