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