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