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