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