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