Fix warnings shown by cppcheck for wrt-commons
[framework/web/wrt-commons.git] / modules / widget_dao / dao / widget_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  * This file contains the definition of widget dao class.
18  *
19  * @file    widget_dao.cpp
20  * @author  Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
21  * @author  Bartosz Janiak (b.janiak@samsung.com)
22  * @author  Yang Jie (jie2.yang@samsung.com)
23  * @author  Koeun Choi(koeun.choi@samsung.com)
24  * @author  Pawel Sikorski(p.sikorski@samsung.com)
25  * @version 1.0
26  * @brief   This file contains the definition of Configuration.
27  */
28 #include <stddef.h>
29 #include <dpl/wrt-dao-rw/widget_dao.h>
30
31 #include <sstream>
32 #include <dpl/log/log.h>
33 #include <dpl/foreach.h>
34 #include <dpl/wrt-dao-ro/webruntime_database.h>
35 #include <dpl/wrt-dao-rw/property_dao.h>
36 #include <orm_generator_wrt.h>
37 #include <dpl/wrt-dao-ro/WrtDatabase.h>
38
39 namespace {
40     unsigned int seed = time(NULL);
41 }
42
43 namespace WrtDB {
44 //TODO in current solution in each getter there exists a check
45 //"IsWidgetInstalled". Maybe it should be verified, if it could be done
46 //differently  (check in WidgetDAO constructor)
47
48 #define SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN          Try
49
50 #define SQL_CONNECTION_EXCEPTION_HANDLER_END(message)   \
51     Catch(DPL::DB::SqlConnection::Exception::Base) {       \
52         LogError(message);                              \
53         ReThrowMsg(WidgetDAO::Exception::DatabaseError, \
54                    message);                            \
55     }
56
57 WidgetDAO::WidgetDAO(DPL::OptionalString widgetGUID) :
58     WidgetDAOReadOnly(WidgetDAOReadOnly::getHandle(widgetGUID))
59 {}
60
61 WidgetDAO::WidgetDAO(DPL::String tzAppId) :
62     WidgetDAOReadOnly(WidgetDAOReadOnly::getHandle(tzAppId))
63 {}
64
65 WidgetDAO::WidgetDAO(DbWidgetHandle handle) :
66     WidgetDAOReadOnly(handle)
67 {}
68
69 WidgetDAO::~WidgetDAO()
70 {}
71
72 void WidgetDAO::setTizenAppId(const DPL::OptionalString& tzAppId)
73 {
74     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
75     {
76         using namespace DPL::DB::ORM;
77         wrt::ScopedTransaction transaction(&WrtDatabase::interface());
78
79         if (!isWidgetInstalled(getHandle())) {
80             ThrowMsg(WidgetDAOReadOnly::Exception::WidgetNotExist,
81                      "Cannot find widget. Handle: " << getHandle());
82         }
83
84         wrt::WidgetInfo::Row row;
85         row.Set_tizen_appid(*tzAppId);
86
87         WRT_DB_UPDATE(update, wrt::WidgetInfo, &WrtDatabase::interface())
88         update->Where(
89             Equals<wrt::WidgetInfo::app_id>(getHandle()));
90
91         update->Values(row);
92         update->Execute();
93         transaction.Commit();
94     }
95     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget")
96 }
97
98 void WidgetDAO::setSecurityPopupUsage(const SettingsType value)
99 {
100     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
101     {
102         using namespace DPL::DB::ORM;
103         using namespace DPL::DB::ORM::wrt;
104
105         ScopedTransaction transaction(&WrtDatabase::interface());
106         if (!isWidgetInstalled(getHandle())) {
107             ThrowMsg(WidgetDAOReadOnly::Exception::WidgetNotExist,
108                      "Cannot find widget. Handle: " << getHandle());
109         }
110
111         WidgetSecuritySettings::Row row;
112         row.Set_security_popup_usage(value);
113
114         WRT_DB_UPDATE(update, WidgetSecuritySettings, &WrtDatabase::interface())
115         update->Where(Equals<WidgetSecuritySettings::app_id>(getHandle()));
116         update->Values(row);
117         update->Execute();
118
119         transaction.Commit();
120     }
121     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to set security popup usage")
122 }
123
124 void WidgetDAO::setGeolocationUsage(const SettingsType value)
125 {
126     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
127     {
128         using namespace DPL::DB::ORM;
129         using namespace DPL::DB::ORM::wrt;
130
131         ScopedTransaction transaction(&WrtDatabase::interface());
132         if (!isWidgetInstalled(getHandle())) {
133             ThrowMsg(WidgetDAOReadOnly::Exception::WidgetNotExist,
134                      "Cannot find widget. Handle: " << getHandle());
135         }
136
137         WidgetSecuritySettings::Row row;
138         row.Set_geolocation_usage(value);
139
140         WRT_DB_UPDATE(update, WidgetSecuritySettings, &WrtDatabase::interface())
141         update->Where(Equals<WidgetSecuritySettings::app_id>(getHandle()));
142         update->Values(row);
143         update->Execute();
144
145         transaction.Commit();
146     }
147     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to set geolocation usage")
148 }
149
150 void WidgetDAO::setWebNotificationUsage(const SettingsType value)
151 {
152     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
153     {
154         using namespace DPL::DB::ORM;
155         using namespace DPL::DB::ORM::wrt;
156
157         ScopedTransaction transaction(&WrtDatabase::interface());
158         if (!isWidgetInstalled(getHandle())) {
159             ThrowMsg(WidgetDAOReadOnly::Exception::WidgetNotExist,
160                      "Cannot find widget. Handle: " << getHandle());
161         }
162
163         WidgetSecuritySettings::Row row;
164         row.Set_web_notification_usage(value);
165
166         WRT_DB_UPDATE(update, WidgetSecuritySettings, &WrtDatabase::interface())
167         update->Where(Equals<WidgetSecuritySettings::app_id>(getHandle()));
168         update->Values(row);
169         update->Execute();
170
171         transaction.Commit();
172     }
173     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to set web notification usage")
174 }
175
176 void WidgetDAO::setWebDatabaseUsage(const SettingsType value)
177 {
178     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
179     {
180         using namespace DPL::DB::ORM;
181         using namespace DPL::DB::ORM::wrt;
182
183         ScopedTransaction transaction(&WrtDatabase::interface());
184         if (!isWidgetInstalled(getHandle())) {
185             ThrowMsg(WidgetDAOReadOnly::Exception::WidgetNotExist,
186                      "Cannot find widget. Handle: " << getHandle());
187         }
188
189         WidgetSecuritySettings::Row row;
190         row.Set_web_database_usage(value);
191
192         WRT_DB_UPDATE(update, WidgetSecuritySettings, &WrtDatabase::interface())
193         update->Where(Equals<WidgetSecuritySettings::app_id>(getHandle()));
194         update->Values(row);
195         update->Execute();
196
197         transaction.Commit();
198     }
199     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to set web database usage")
200 }
201
202 void WidgetDAO::setFileSystemUsage(const SettingsType value)
203 {
204     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
205     {
206         using namespace DPL::DB::ORM;
207         using namespace DPL::DB::ORM::wrt;
208
209         ScopedTransaction transaction(&WrtDatabase::interface());
210         if (!isWidgetInstalled(getHandle())) {
211             ThrowMsg(WidgetDAOReadOnly::Exception::WidgetNotExist,
212                      "Cannot find widget. Handle: " << getHandle());
213         }
214
215         WidgetSecuritySettings::Row row;
216         row.Set_file_system_usage(value);
217
218         WRT_DB_UPDATE(update, WidgetSecuritySettings, &WrtDatabase::interface())
219         update->Where(Equals<WidgetSecuritySettings::app_id>(getHandle()));
220         update->Values(row);
221         update->Execute();
222
223         transaction.Commit();
224     }
225     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to set filesystem usage")
226 }
227
228 void WidgetDAO::registerWidget(
229     const TizenAppId & tzAppId,
230     const WidgetRegisterInfo &widgetRegInfo,
231     const IWacSecurity &wacSecurity)
232 {
233     LogDebug("Registering widget");
234     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
235     {
236         DPL::DB::ORM::wrt::ScopedTransaction transaction(
237             &WrtDatabase::interface());
238         registerWidgetInternal(tzAppId, widgetRegInfo, wacSecurity);
239         transaction.Commit();
240     }
241     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget")
242 }
243
244 DbWidgetHandle WidgetDAO::registerWidget(
245     const WidgetRegisterInfo &pWidgetRegisterInfo,
246     const IWacSecurity &wacSecurity)
247 {
248     //make it more precise due to very fast tests
249     struct timeval tv;
250     gettimeofday(&tv, NULL);
251     DbWidgetHandle widgetHandle;
252     do {
253         widgetHandle = rand_r(&seed);
254     } while (isWidgetInstalled(widgetHandle));
255
256     registerWidget(*pWidgetRegisterInfo.configInfo.tizenAppId,
257                    pWidgetRegisterInfo,
258                    wacSecurity);
259     return widgetHandle;
260 }
261
262 TizenAppId WidgetDAO::registerWidgetGeneratePkgId(
263     const WidgetRegisterInfo &pWidgetRegisterInfo,
264     const IWacSecurity &wacSecurity)
265 {
266     TizenAppId tzAppId = generatePkgId();
267     registerWidget(tzAppId, pWidgetRegisterInfo, wacSecurity);
268     return tzAppId;
269 }
270
271 void WidgetDAO::registerWidgetInternal(
272     const TizenAppId & tzAppId,
273     const WidgetRegisterInfo &widgetRegInfo,
274     const IWacSecurity &wacSecurity,
275     const DPL::Optional<DbWidgetHandle> handle)
276 {
277     //Register into WidgetInfo has to be first
278     //as all other tables depend upon that
279     DbWidgetHandle widgetHandle = registerWidgetInfo(tzAppId,
280                                                      widgetRegInfo,
281                                                      wacSecurity,
282                                                      handle);
283
284     registerWidgetExtendedInfo(widgetHandle, widgetRegInfo);
285
286     registerWidgetLocalizedInfo(widgetHandle, widgetRegInfo);
287
288     registerWidgetIcons(widgetHandle, widgetRegInfo);
289
290     registerWidgetStartFile(widgetHandle, widgetRegInfo);
291
292     PropertyDAO::RegisterProperties(tzAppId, widgetRegInfo);
293
294     registerWidgetFeatures(widgetHandle, widgetRegInfo);
295
296     registerWidgetPrivilege(widgetHandle, widgetRegInfo);
297
298     registerWidgetWindowModes(widgetHandle, widgetRegInfo);
299
300     registerWidgetWarpInfo(widgetHandle, widgetRegInfo);
301
302     registerWidgetCertificates(widgetHandle, wacSecurity);
303
304     CertificateChainList list;
305     wacSecurity.getCertificateChainList(list, SIGNATURE_DISTRIBUTOR);
306     registerCertificatesChains(widgetHandle, SIGNATURE_DISTRIBUTOR, list);
307
308     list.clear();
309     wacSecurity.getCertificateChainList(list, SIGNATURE_AUTHOR);
310     registerCertificatesChains(widgetHandle, SIGNATURE_AUTHOR, list);
311
312     registerWidgetSettings(widgetHandle, widgetRegInfo);
313
314     registerAppService(widgetHandle, widgetRegInfo);
315
316     registerEncryptedResouceInfo(widgetHandle, widgetRegInfo);
317
318     registerExternalLocations(widgetHandle, widgetRegInfo.externalLocations);
319
320     registerWidgetSecuritySettings(widgetHandle);
321 }
322
323 void WidgetDAO::registerOrUpdateWidget(
324     const TizenAppId & widgetName,
325     const WidgetRegisterInfo &widgetRegInfo,
326     const IWacSecurity &wacSecurity)
327 {
328     LogDebug("Reregistering widget");
329     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
330     {
331         DPL::DB::ORM::wrt::ScopedTransaction transaction(
332             &WrtDatabase::interface());
333
334         unregisterWidgetInternal(widgetName);
335         registerWidgetInternal(widgetName, widgetRegInfo, wacSecurity);
336         transaction.Commit();
337     }
338     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to reregister widget")
339 }
340
341 #define DO_INSERT(row, table)                              \
342     {                                                      \
343         WRT_DB_INSERT(insert, table, &WrtDatabase::interface()) \
344         insert->Values(row);                               \
345         insert->Execute();                                 \
346     }
347
348 void WidgetDAO::registerWidgetExtendedInfo(DbWidgetHandle widgetHandle,
349                                            const WidgetRegisterInfo &regInfo)
350 {
351     //Try and transaction not needed
352     using namespace DPL::DB::ORM;
353     using namespace DPL::DB::ORM::wrt;
354
355     WidgetExtendedInfo::Row row;
356     row.Set_app_id(widgetHandle);
357     //    row.Set_share_href    (DPL::FromUTF8String(regInfo.shareHref));
358     row.Set_signature_type(regInfo.signatureType);
359     row.Set_test_widget(regInfo.isTestWidget);
360     row.Set_install_time(regInfo.installedTime);
361     row.Set_splash_img_src(regInfo.configInfo.splashImgSrc);
362     row.Set_background_page(regInfo.configInfo.backgroundPage);
363     row.Set_installed_path(regInfo.widgetInstalledPath);
364
365     DO_INSERT(row, WidgetExtendedInfo)
366 }
367
368 DbWidgetHandle WidgetDAO::registerWidgetInfo(
369     const TizenAppId & tzAppId,
370     const WidgetRegisterInfo &regInfo,
371     const IWacSecurity &wacSecurity,
372     const DPL::Optional<DbWidgetHandle> handle)
373 {
374     using namespace DPL::DB::ORM;
375     using namespace DPL::DB::ORM::wrt;
376     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
377
378     // TODO in wrt_db all Columns in WidgetInfo have DEFAULT VALUE set.
379     // Because of that, "Optional" is not used there
380
381     WidgetInfo::Row row;
382     if (!!handle) {
383         row.Set_app_id(*handle);
384     }
385
386     row.Set_widget_type(regInfo.webAppType.appType);
387
388     row.Set_widget_id(widgetConfigurationInfo.widget_id);
389     row.Set_defaultlocale(widgetConfigurationInfo.defaultlocale);
390     row.Set_widget_version(widgetConfigurationInfo.version);
391     row.Set_widget_width(widgetConfigurationInfo.width);
392     row.Set_widget_height(widgetConfigurationInfo.height);
393     row.Set_author_name(widgetConfigurationInfo.authorName);
394     row.Set_author_email(widgetConfigurationInfo.authorEmail);
395     row.Set_author_href(widgetConfigurationInfo.authorHref);
396     row.Set_csp_policy(widgetConfigurationInfo.cspPolicy);
397     row.Set_base_folder(DPL::FromUTF8String(regInfo.baseFolder));
398     row.Set_webkit_plugins_required(widgetConfigurationInfo.flashNeeded);
399     row.Set_recognized(wacSecurity.isRecognized());
400     row.Set_wac_signed(wacSecurity.isWacSigned());
401     row.Set_distributor_signed(wacSecurity.isDistributorSigned());
402     row.Set_tizen_appid(tzAppId);
403     row.Set_tizen_pkgid(regInfo.tzPkgid);
404     {
405         std::stringstream tmp;
406         tmp << regInfo.minVersion;
407         row.Set_min_version(DPL::FromUTF8String(tmp.str()));
408     }
409     row.Set_back_supported(widgetConfigurationInfo.backSupported);
410     row.Set_access_network(widgetConfigurationInfo.accessNetwork);
411     row.Set_pkg_type(regInfo.packagingType.pkgType);
412
413     Try
414     {
415         DO_INSERT(row, WidgetInfo);
416     }
417     Catch(DPL::DB::SqlConnection::Exception::Base)
418     {
419         ReThrowMsg(WidgetDAO::Exception::DatabaseError,
420                    "Failed to register widget info.");
421     }
422
423     if (!handle) {
424         //get autoincremented value of widgetHandle
425         WRT_DB_SELECT(select, WidgetInfo, &WrtDatabase::interface())
426         select->Where(Equals<WidgetInfo::tizen_appid>(tzAppId));
427         return select->GetSingleValue<WidgetInfo::app_id>();
428     } else {
429         return *handle;
430     }
431 }
432
433 void WidgetDAO::registerWidgetLocalizedInfo(DbWidgetHandle widgetHandle,
434                                             const WidgetRegisterInfo &regInfo)
435 {
436     using namespace DPL::DB::ORM;
437     using namespace DPL::DB::ORM::wrt;
438
439     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
440
441     FOREACH(it, widgetConfigurationInfo.localizedDataSet)
442     {
443         const DPL::String& locale = it->first;
444         const ConfigParserData::LocalizedData& data = it->second;
445
446         LocalizedWidgetInfo::Row row;
447         row.Set_app_id(widgetHandle);
448         row.Set_widget_locale(locale);
449         row.Set_widget_name(data.name);
450         row.Set_widget_shortname(data.shortName);
451         row.Set_widget_description(data.description);
452         row.Set_widget_license(data.license);
453         row.Set_widget_license_file(data.licenseFile);
454         row.Set_widget_license_href(data.licenseHref);
455
456         DO_INSERT(row, LocalizedWidgetInfo)
457     }
458 }
459
460 void WidgetDAO::registerWidgetIcons(DbWidgetHandle widgetHandle,
461                                     const WidgetRegisterInfo &regInfo)
462 {
463     using namespace DPL::DB::ORM;
464     using namespace DPL::DB::ORM::wrt;
465
466     FOREACH(i, regInfo.localizationData.icons)
467     {
468         wrt::WidgetIcon::icon_id::ColumnType icon_id;
469         {
470             wrt::WidgetIcon::Row row;
471             row.Set_app_id(widgetHandle);
472             row.Set_icon_src(i->src);
473             row.Set_icon_width(i->width);
474             row.Set_icon_height(i->height);
475
476             WRT_DB_INSERT(insert, wrt::WidgetIcon, &WrtDatabase::interface())
477             insert->Values(row);
478             icon_id = static_cast<int>(insert->Execute());
479         }
480
481         FOREACH(j, i->availableLocales)
482         {
483             WidgetLocalizedIcon::Row row;
484             row.Set_app_id(widgetHandle);
485             row.Set_icon_id(icon_id);
486             row.Set_widget_locale(*j);
487             DO_INSERT(row, WidgetLocalizedIcon)
488         }
489     }
490 }
491
492 void WidgetDAO::registerWidgetStartFile(DbWidgetHandle widgetHandle,
493                                         const WidgetRegisterInfo &regInfo)
494 {
495     using namespace DPL::DB::ORM;
496     using namespace DPL::DB::ORM::wrt;
497
498     FOREACH(i, regInfo.localizationData.startFiles)
499     {
500         WidgetStartFile::start_file_id::ColumnType startFileID;
501         {
502             WidgetStartFile::Row row;
503             row.Set_app_id(widgetHandle);
504             row.Set_src(i->path);
505
506             WRT_DB_INSERT(insert, WidgetStartFile, &WrtDatabase::interface())
507             insert->Values(row);
508             startFileID = static_cast<int>(insert->Execute());
509         }
510
511         FOREACH(j, i->propertiesForLocales)
512         {
513             WidgetLocalizedStartFile::Row row;
514             row.Set_app_id(widgetHandle);
515             row.Set_start_file_id(startFileID);
516             row.Set_widget_locale(j->first);
517             row.Set_type(j->second.type);
518             row.Set_encoding(j->second.encoding);
519
520             DO_INSERT(row, WidgetLocalizedStartFile)
521         }
522     }
523 }
524
525 void WidgetDAO::registerWidgetFeatures(DbWidgetHandle widgetHandle,
526                                        const WidgetRegisterInfo &regInfo)
527 {
528     using namespace DPL::DB::ORM;
529     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
530
531     FOREACH(pWidgetFeature, widgetConfigurationInfo.featuresList)
532     {
533         wrt::WidgetFeature::Row widgetFeature;
534         widgetFeature.Set_app_id(widgetHandle);
535         widgetFeature.Set_name(pWidgetFeature->name);
536         widgetFeature.Set_required(pWidgetFeature->required);
537         widgetFeature.Set_rejected(false);
538
539         wrt::WidgetFeature::widget_feature_id::ColumnType widgetFeatureID;
540         {
541             WRT_DB_INSERT(insert, wrt::WidgetFeature, &WrtDatabase::interface())
542             insert->Values(widgetFeature);
543             widgetFeatureID = static_cast<int>(insert->Execute());
544         }
545
546         // Insert into table FeatureParam
547         wrt::FeatureParam::Row featureParam;
548         featureParam.Set_widget_feature_id(widgetFeatureID);
549
550         FOREACH(iter, pWidgetFeature->paramsList)
551         {
552             featureParam.Set_name(iter->name);
553             featureParam.Set_value(iter->value);
554
555             DO_INSERT(featureParam, wrt::FeatureParam)
556         }
557     }
558 }
559
560 void WidgetDAO::registerWidgetPrivilege(DbWidgetHandle widgetHandle,
561                                         const WidgetRegisterInfo &regInfo)
562 {
563     using namespace DPL::DB::ORM;
564     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
565
566     FOREACH(it, widgetConfigurationInfo.privilegeList)
567     {
568         wrt::WidgetPrivilege::Row widgetPrivilege;
569         widgetPrivilege.Set_app_id(widgetHandle);
570         widgetPrivilege.Set_name(it->name);
571
572         DO_INSERT(widgetPrivilege, wrt::WidgetPrivilege)
573     }
574 }
575
576 void WidgetDAO::updateFeatureRejectStatus(const DbWidgetFeature &widgetFeature)
577 {
578     // This function could be merged with registerWidgetFeature but it requires
579     // desing change:
580     // 1. Check "ace step" in installer must be done before "update database
581     // step"
582     // And:
583     // ConfigurationParserData shouldn't be called "ParserData" any more.
584     using namespace DPL::DB::ORM;
585
586     wrt::ScopedTransaction transaction(&WrtDatabase::interface());
587     WRT_DB_SELECT(select, wrt::WidgetFeature, &WrtDatabase::interface())
588     select->Where(And(Equals<wrt::WidgetFeature::app_id>(m_widgetHandle),
589                       Equals<wrt::WidgetFeature::name>(widgetFeature.name)));
590
591     auto row = select->GetSingleRow();
592     row.Set_rejected(widgetFeature.rejected);
593
594     WRT_DB_UPDATE(update, wrt::WidgetFeature, &WrtDatabase::interface())
595     update->Where(And(Equals<wrt::WidgetFeature::app_id>(m_widgetHandle),
596                       Equals<wrt::WidgetFeature::name>(widgetFeature.name)));
597     update->Values(row);
598     update->Execute();
599     transaction.Commit();
600 }
601
602 void WidgetDAO::registerWidgetWindowModes(DbWidgetHandle widgetHandle,
603                                           const WidgetRegisterInfo &regInfo)
604 {
605     using namespace DPL::DB::ORM;
606     using namespace DPL::DB::ORM::wrt;
607     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
608
609     FOREACH(i, widgetConfigurationInfo.windowModes)
610     {
611         wrt::WidgetWindowModes::Row windowMode;
612         windowMode.Set_app_id(widgetHandle);
613         windowMode.Set_window_mode(*i);
614
615         DO_INSERT(windowMode, wrt::WidgetWindowModes)
616     }
617 }
618
619 void WidgetDAO::registerWidgetWarpInfo(DbWidgetHandle widgetHandle,
620                                        const WidgetRegisterInfo &regInfo)
621 {
622     using namespace DPL::DB::ORM;
623     using namespace DPL::DB::ORM::wrt;
624     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
625
626     FOREACH(AccIt, widgetConfigurationInfo.accessInfoSet)
627     {
628         WidgetWARPInfo::Row row;
629         row.Set_app_id(widgetHandle);
630         row.Set_iri(AccIt->m_strIRI);
631         row.Set_subdomain_access(static_cast <int>(
632                                      AccIt->m_bSubDomainAccess));
633
634         DO_INSERT(row, WidgetWARPInfo)
635     }
636 }
637
638 void WidgetDAO::registerWidgetCertificates(DbWidgetHandle widgetHandle,
639                                            const IWacSecurity &wacSecurity)
640 {
641     using namespace DPL::DB::ORM;
642     using namespace DPL::DB::ORM::wrt;
643
644     FOREACH(it, wacSecurity.getCertificateList())
645     {
646         WidgetCertificateFingerprint::Row row;
647         row.Set_app_id(widgetHandle);
648         row.Set_owner(it->owner);
649         row.Set_chainid(it->chainId);
650         row.Set_type(it->type);
651         row.Set_md5_fingerprint(DPL::FromUTF8String(it->strMD5Fingerprint));
652         row.Set_sha1_fingerprint(DPL::FromUTF8String(it->strSHA1Fingerprint));
653         row.Set_common_name(it->strCommonName);
654
655         DO_INSERT(row, WidgetCertificateFingerprint)
656     }
657 }
658
659 void WidgetDAO::registerCertificatesChains(
660     DbWidgetHandle widgetHandle,
661     CertificateSource certificateSource,
662     const CertificateChainList &
663     certificateChainList)
664 {
665     using namespace DPL::DB::ORM;
666     using namespace DPL::DB::ORM::wrt;
667     FOREACH(certChain, certificateChainList)
668     {
669         WidgetCertificate::Row row;
670         row.Set_app_id(widgetHandle);
671         row.Set_cert_source(certificateSource);
672         row.Set_encoded_chain(DPL::FromASCIIString(*certChain));
673
674         DO_INSERT(row, WidgetCertificate);
675     }
676 }
677
678 void WidgetDAO::registerWidgetSettings(DbWidgetHandle widgetHandle,
679                                        const WidgetRegisterInfo &regInfo)
680 {
681     using namespace DPL::DB::ORM;
682     using namespace DPL::DB::ORM::wrt;
683
684     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
685
686     FOREACH(pWidgetSetting, widgetConfigurationInfo.settingsList)
687     {
688         SettingsList::Row row;
689         row.Set_appId(widgetHandle);
690         row.Set_settingName(pWidgetSetting->m_name);
691         row.Set_settingValue(pWidgetSetting->m_value);
692
693         DO_INSERT(row, SettingsList)
694     }
695 }
696
697 void WidgetDAO::registerAppService(DbWidgetHandle widgetHandle,
698                                    const WidgetRegisterInfo &regInfo)
699 {
700     using namespace DPL::DB::ORM;
701     using namespace DPL::DB::ORM::wrt;
702     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
703
704     FOREACH(ASIt, widgetConfigurationInfo.appServiceList)
705     {
706         ApplicationServiceInfo::Row row;
707         row.Set_app_id(widgetHandle);
708         row.Set_src(ASIt->m_src);
709         row.Set_operation(ASIt->m_operation);
710         row.Set_scheme(ASIt->m_scheme);
711         row.Set_mime(ASIt->m_mime);
712
713         DO_INSERT(row, ApplicationServiceInfo)
714     }
715 }
716
717 void WidgetDAO::registerEncryptedResouceInfo(DbWidgetHandle widgetHandle,
718                                              const WidgetRegisterInfo &regInfo)
719 {
720     using namespace DPL::DB::ORM;
721     using namespace DPL::DB::ORM::wrt;
722
723     FOREACH(it, regInfo.encryptedFiles)
724     {
725         EncryptedResourceList::Row row;
726         row.Set_app_id(widgetHandle);
727         row.Set_resource(it->fileName);
728         row.Set_size(it->fileSize);
729
730         DO_INSERT(row, EncryptedResourceList)
731     }
732 }
733
734 void WidgetDAO::registerExternalLocations(
735     DbWidgetHandle widgetHandle,
736     const ExternalLocationList &
737     externals)
738 {
739     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
740     {
741         using namespace DPL::DB::ORM;
742         using namespace DPL::DB::ORM::wrt;
743         DPL::DB::ORM::wrt::ScopedTransaction transaction(
744             &WrtDatabase::interface());
745         LogDebug("Inserting external files for widgetHandle: " << widgetHandle);
746         FOREACH(it, externals)
747         {
748             WidgetExternalLocations::Row row;
749             row.Set_app_id(widgetHandle);
750             row.Set_path(DPL::FromUTF8String(*it));
751
752             DO_INSERT(row, WidgetExternalLocations)
753         }
754         transaction.Commit();
755     }
756     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register external files");
757 }
758
759 void WidgetDAO::registerWidgetSecuritySettings(DbWidgetHandle widgetHandle)
760 {
761     using namespace DPL::DB::ORM;
762     using namespace DPL::DB::ORM::wrt;
763     WidgetSecuritySettings::Row row;
764     row.Set_app_id(widgetHandle);
765     row.Set_security_popup_usage(SETTINGS_TYPE_ON);
766     row.Set_geolocation_usage(SETTINGS_TYPE_ON);
767     row.Set_web_notification_usage(SETTINGS_TYPE_ON);
768     row.Set_web_database_usage(SETTINGS_TYPE_ON);
769     row.Set_file_system_usage(SETTINGS_TYPE_ON);
770
771     DO_INSERT(row, WidgetSecuritySettings)
772 }
773
774 void WidgetDAO::unregisterAllExternalLocations()
775 {
776     using namespace DPL::DB::ORM;
777     using namespace DPL::DB::ORM::wrt;
778     LogDebug("Deleting external files for widgetHandle: " << m_widgetHandle);
779     WRT_DB_DELETE(del, WidgetExternalLocations, &WrtDatabase::interface());
780     del->Where(Equals<WidgetExternalLocations::app_id>(m_widgetHandle));
781     del->Execute();
782 }
783
784 void WidgetDAO::unregisterWidget(const TizenAppId & tzAppId)
785 {
786     LogDebug("Unregistering widget from DB. tzAppId: " << tzAppId);
787     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
788     {
789         DPL::DB::ORM::wrt::ScopedTransaction transaction(
790             &WrtDatabase::interface());
791         unregisterWidgetInternal(tzAppId);
792         transaction.Commit();
793     }
794     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to unregister widget")
795 }
796
797 void WidgetDAO::unregisterWidget(WrtDB::DbWidgetHandle handle)
798 {
799     LogDebug("Unregistering widget from DB. Handle: " << handle);
800     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
801     {
802         using namespace DPL::DB::ORM;
803         using namespace DPL::DB::ORM::wrt;
804         ScopedTransaction transaction(&WrtDatabase::interface());
805
806         // Delete from table Widget Info
807         WRT_DB_DELETE(del, WidgetInfo, &WrtDatabase::interface())
808         del->Where(Equals<WidgetInfo::app_id>(handle));
809         del->Execute();
810
811         transaction.Commit();
812     }
813     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to unregister widget")
814 }
815
816 void WidgetDAO::unregisterWidgetInternal(
817     const TizenAppId & tzAppId)
818 {
819     using namespace DPL::DB::ORM;
820     using namespace DPL::DB::ORM::wrt;
821
822     DbWidgetHandle handle = getHandle(tzAppId);
823
824     // Delete from table Widget Info
825     WRT_DB_DELETE(del, WidgetInfo, &WrtDatabase::interface())
826     del->Where(Equals<WidgetInfo::app_id>(handle));
827     del->Execute();
828
829     // Deleting in other tables is done via "delete cascade" in SQL
830 }
831
832 #undef DO_INSERT
833
834 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
835 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
836 } // namespace WrtDB