tizen 2.3 release
[framework/web/wearable/wrt-commons.git] / modules / widget_dao / dao / widget_dao.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * This file contains the definition of widget dao class.
18  *
19  * @file    widget_dao.cpp
20  * @author  Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
21  * @author  Bartosz Janiak (b.janiak@samsung.com)
22  * @author  Yang Jie (jie2.yang@samsung.com)
23  * @author  Koeun Choi(koeun.choi@samsung.com)
24  * @author  Pawel Sikorski(p.sikorski@samsung.com)
25  * @version 1.0
26  * @brief   This file contains the definition of Configuration.
27  */
28 #include <stddef.h>
29 #include <dpl/wrt-dao-rw/widget_dao.h>
30
31 #include <sstream>
32 #include <dpl/log/log.h>
33 #include <dpl/foreach.h>
34 #include <dpl/wrt-dao-ro/webruntime_database.h>
35 #include <dpl/wrt-dao-rw/property_dao.h>
36 #include <orm_generator_wrt.h>
37 #include <dpl/wrt-dao-ro/WrtDatabase.h>
38
39 namespace WrtDB {
40 //TODO in current solution in each getter there exists a check
41 //"IsWidgetInstalled". Maybe it should be verified, if it could be done
42 //differently  (check in WidgetDAO constructor)
43
44 #define SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN          Try
45
46 #define SQL_CONNECTION_EXCEPTION_HANDLER_END(message)   \
47     Catch(DPL::DB::SqlConnection::Exception::Base) {       \
48         LogError(message);                              \
49         ReThrowMsg(WidgetDAO::Exception::DatabaseError, \
50                    message);                            \
51     }
52
53 WidgetDAO::WidgetDAO(DPL::OptionalString widgetGUID) :
54     WidgetDAOReadOnly(WidgetDAOReadOnly::getHandle(widgetGUID))
55 {}
56
57 WidgetDAO::WidgetDAO(DPL::String tzAppId) :
58     WidgetDAOReadOnly(WidgetDAOReadOnly::getHandle(tzAppId))
59 {}
60
61 WidgetDAO::~WidgetDAO()
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     LogDebug("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     LogDebug("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
124 TizenAppId WidgetDAO::registerWidgetGeneratePkgId(
125     const WidgetRegisterInfo &pWidgetRegisterInfo,
126     const IWidgetSecurity &widgetSecurity)
127 {
128     TizenAppId tzAppId = generatePkgId();
129     registerWidget(tzAppId, pWidgetRegisterInfo, widgetSecurity);
130     return tzAppId;
131 }
132
133 void WidgetDAO::updateTizenAppId(
134     const TizenAppId & fromAppId,
135     const TizenAppId & toAppId)
136 {
137     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
138     {
139         using namespace DPL::DB::ORM;
140         using namespace DPL::DB::ORM::wrt;
141
142         ScopedTransaction transaction(&WrtDatabase::interface());
143         if (!isWidgetInstalled(fromAppId)) {
144             ThrowMsg(WidgetDAOReadOnly::Exception::WidgetNotExist,
145                      "Cannot find widget. tzAppId: " << fromAppId);
146         }
147
148         WRT_DB_SELECT(select, WidgetInfo, &WrtDatabase::interface())
149         select->Where(Equals<WidgetInfo::tizen_appid>(fromAppId));
150
151         WidgetInfo::Row row = select->GetSingleRow();
152
153         //WidgetInfo::Row row;
154         row.Set_tizen_appid(toAppId);
155
156         WRT_DB_UPDATE(update, WidgetInfo, &WrtDatabase::interface())
157         update->Where(Equals<WidgetInfo::tizen_appid>(fromAppId));
158         update->Values(row);
159         update->Execute();
160
161         transaction.Commit();
162     }
163     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to update appid")
164 }
165
166 void WidgetDAO::registerWidgetInternal(
167     const TizenAppId & tzAppId,
168     const WidgetRegisterInfo &widgetRegInfo,
169     const IWidgetSecurity &widgetSecurity,
170     const DPL::Optional<DbWidgetHandle> handle)
171 {
172     //Register into WidgetInfo has to be first
173     //as all other tables depend upon that
174     DbWidgetHandle widgetHandle = registerWidgetInfo(tzAppId, widgetRegInfo.tzPkgid, widgetRegInfo.baseFolder,
175                                                      widgetRegInfo.webAppType.appType, widgetRegInfo.packagingType.pkgType,
176                                                      widgetRegInfo.configInfo, widgetSecurity,handle);
177
178     registerWidgetExtendedInfo(widgetHandle, widgetRegInfo.installedTime,
179                                                     widgetRegInfo.configInfo.splashImgSrc, widgetRegInfo.configInfo.backgroundPage,
180                                                     widgetRegInfo.widgetInstalledPath);
181
182     registerWidgetLocalizedInfo(widgetHandle, widgetRegInfo.configInfo.localizedDataSet);
183
184     registerWidgetIcons(widgetHandle, widgetRegInfo.localizationData.icons);
185
186     registerWidgetStartFile(widgetHandle, widgetRegInfo.localizationData.startFiles);
187
188     PropertyDAO::RegisterProperties(widgetHandle, tzAppId, widgetRegInfo);
189
190     registerWidgetFeatures(widgetHandle, widgetRegInfo.configInfo.featuresList);
191
192     registerWidgetPrivilege(widgetHandle, widgetRegInfo.configInfo.privilegeList);
193
194     registerWidgetWindowModes(widgetHandle, widgetRegInfo.configInfo.windowModes);
195
196     registerWidgetWarpInfo(widgetHandle, widgetRegInfo.configInfo.accessInfoSet);
197
198     registerWidgetAllowNavigationInfo(widgetHandle, widgetRegInfo.configInfo.allowNavigationInfoList);
199
200     registerWidgetCertificates(widgetHandle, widgetSecurity);
201
202     CertificateChainList list;
203     widgetSecurity.getCertificateChainList(list, SIGNATURE_DISTRIBUTOR);
204     registerCertificatesChains(widgetHandle, SIGNATURE_DISTRIBUTOR, list);
205
206     list.clear();
207     widgetSecurity.getCertificateChainList(list, SIGNATURE_DISTRIBUTOR2);
208     registerCertificatesChains(widgetHandle, SIGNATURE_DISTRIBUTOR2, list);
209
210     list.clear();
211     widgetSecurity.getCertificateChainList(list, SIGNATURE_AUTHOR);
212     registerCertificatesChains(widgetHandle, SIGNATURE_AUTHOR, list);
213
214     registerWidgetSettings(widgetHandle, widgetRegInfo.configInfo.settingsList);
215
216     registerAppControl(widgetHandle, widgetRegInfo.configInfo.appControlList);
217
218     registerEncryptedResouceInfo(widgetHandle, widgetRegInfo.encryptedFiles);
219
220     registerExternalLocations(widgetHandle, widgetRegInfo.externalLocations);
221 }
222
223 #define DO_INSERT(row, table)                              \
224     {                                                      \
225         WRT_DB_INSERT(insert, table, &WrtDatabase::interface()) \
226         insert->Values(row);                               \
227         insert->Execute();                                 \
228     }
229
230 void WidgetDAO::registerWidgetExtendedInfo(DbWidgetHandle widgetHandle,
231         time_t installedTime,
232         const DPL::OptionalString & splashImgSrc, const DPL::OptionalString & backgroundPage,
233         const DPL::OptionalString & widgetInstalledPath)
234 {
235     //Try and transaction not needed
236     using namespace DPL::DB::ORM;
237     using namespace DPL::DB::ORM::wrt;
238
239     WidgetExtendedInfo::Row row;
240     row.Set_app_id(widgetHandle);
241     row.Set_install_time(installedTime);
242     row.Set_splash_img_src(splashImgSrc);
243     row.Set_background_page(backgroundPage);
244     row.Set_installed_path(widgetInstalledPath);
245
246     DO_INSERT(row, WidgetExtendedInfo)
247 }
248
249 DbWidgetHandle WidgetDAO::registerWidgetInfo(
250     const TizenAppId & tzAppId,
251     const TizenPkgId & tzPkgId,
252     const std::string & baseFolder,
253     AppType appType,
254     PkgType pkgType,
255     const ConfigParserData &widgetConfigurationInfo,
256     const IWidgetSecurity &widgetSecurity,
257     const DPL::Optional<DbWidgetHandle> handle)
258 {
259     using namespace DPL::DB::ORM;
260     using namespace DPL::DB::ORM::wrt;
261     // TODO in wrt_db all Columns in WidgetInfo have DEFAULT VALUE set.
262     // Because of that, "Optional" is not used there
263
264     WidgetInfo::Row row;
265     if (!!handle) {
266         row.Set_app_id(*handle);
267     }
268
269     row.Set_widget_type(appType);
270     row.Set_widget_id(widgetConfigurationInfo.widget_id);
271     row.Set_defaultlocale(widgetConfigurationInfo.defaultlocale);
272     row.Set_widget_version(widgetConfigurationInfo.version);
273     row.Set_widget_width(widgetConfigurationInfo.width);
274     row.Set_widget_height(widgetConfigurationInfo.height);
275     row.Set_author_name(widgetConfigurationInfo.authorName);
276     row.Set_author_email(widgetConfigurationInfo.authorEmail);
277     row.Set_author_href(widgetConfigurationInfo.authorHref);
278     row.Set_csp_policy(widgetConfigurationInfo.cspPolicy);
279     row.Set_csp_policy_report_only(widgetConfigurationInfo.cspPolicyReportOnly);
280     row.Set_base_folder(DPL::FromUTF8String(baseFolder));
281     row.Set_webkit_plugins_required(widgetConfigurationInfo.flashNeeded);
282     row.Set_tizen_appid(tzAppId);
283     row.Set_tizen_pkgid(tzPkgId);
284     {
285         if (!!widgetConfigurationInfo.minVersionRequired) {
286             row.Set_min_version(widgetConfigurationInfo.minVersionRequired);
287         } else if (!!widgetConfigurationInfo.tizenMinVersionRequired) {
288             row.Set_min_version(widgetConfigurationInfo.tizenMinVersionRequired);
289         }
290     }
291     row.Set_back_supported(widgetConfigurationInfo.backSupported);
292     row.Set_access_network(widgetConfigurationInfo.accessNetwork);
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                                              unsigned index,
573                                              unsigned disposition)
574 {
575     using namespace DPL::DB::ORM;
576     using namespace DPL::DB::ORM::wrt;
577
578     AppControlInfo::Row row;
579
580     row.Set_app_id(handle);
581     row.Set_execute_index(index);
582     row.Set_src(src);
583     row.Set_operation(operation);
584     row.Set_uri(uri);
585     row.Set_mime(mime);
586     row.Set_disposition(disposition);
587
588     DO_INSERT(row, AppControlInfo);
589 }
590
591 void WidgetDAO::registerAppControl(DbWidgetHandle widgetHandle,
592                                    const ConfigParserData::AppControlInfoList &appControlList)
593 {
594     using namespace DPL::DB::ORM;
595     using namespace DPL::DB::ORM::wrt;
596
597     // appControlList
598     FOREACH(appControl_it, appControlList)
599     {
600         DPL::String src = appControl_it->m_src;
601         DPL::String operation = appControl_it->m_operation;
602         unsigned index = appControl_it->m_index;
603         unsigned disposition =
604             static_cast<unsigned>(appControl_it->m_disposition);
605
606         if (!appControl_it->m_uriList.empty())
607         {
608             FOREACH(uri_it, appControl_it->m_uriList)
609             {
610                 DPL::String uri = *uri_it;
611
612                 if (!appControl_it->m_mimeList.empty())
613                 {
614                     FOREACH(mime_it, appControl_it->m_mimeList)
615                     {
616                         DPL::String mime = *mime_it;
617
618                         insertAppControlInfo(widgetHandle, src, operation, uri, mime, index, disposition);
619                     }
620                 }
621                 else
622                 {
623                     DPL::String mime = L"";
624
625                     insertAppControlInfo(widgetHandle, src, operation, uri, mime, index, disposition);
626                 }
627             }
628         }
629         else
630         {
631             DPL::String uri = L"";
632
633             if (!appControl_it->m_mimeList.empty())
634             {
635                 FOREACH(mime_it, appControl_it->m_mimeList)
636                 {
637                     DPL::String mime = *mime_it;
638
639                     insertAppControlInfo(widgetHandle, src, operation, uri, mime, index, disposition);
640                 }
641             }
642             else
643             {
644                 DPL::String mime = L"";
645
646                 insertAppControlInfo(widgetHandle, src, operation, uri, mime, index, disposition);
647             }
648         }
649     }
650 }
651
652 void WidgetDAO::registerEncryptedResouceInfo(DbWidgetHandle widgetHandle,
653                                              const EncryptedFileList &encryptedFiles)
654 {
655     using namespace DPL::DB::ORM;
656     using namespace DPL::DB::ORM::wrt;
657
658     FOREACH(it, encryptedFiles)
659     {
660         EncryptedResourceList::Row row;
661         row.Set_app_id(widgetHandle);
662         row.Set_resource(it->fileName);
663         row.Set_size(it->fileSize);
664
665         DO_INSERT(row, EncryptedResourceList)
666     }
667 }
668
669 void WidgetDAO::registerServiceInternal(const ConfigParserData::ServiceAppInfo &serviceAppInfo, const WidgetRegisterInfo &widgetRegInfo, const IWidgetSecurity &widgetSecurity)
670 {
671     using namespace DPL::DB::ORM;
672     using namespace DPL::DB::ORM::wrt;
673
674     DPL::String tzAppId = serviceAppInfo.serviceId;
675
676
677     DbWidgetHandle widgetHandle = registerWidgetInfo(tzAppId, widgetRegInfo.tzPkgid, widgetRegInfo.baseFolder,
678                                                  APP_TYPE_TIZENWEBSERVICE, widgetRegInfo.packagingType.pkgType,
679                                                  widgetRegInfo.configInfo, widgetSecurity);
680
681     registerWidgetExtendedInfo(widgetHandle, widgetRegInfo.installedTime,
682                                                         widgetRegInfo.configInfo.splashImgSrc, widgetRegInfo.configInfo.backgroundPage,
683                                                         widgetRegInfo.widgetInstalledPath);
684
685     registerWidgetLocalizedInfo(widgetHandle, serviceAppInfo.m_localizedDataSet);
686
687
688     WidgetRegisterInfo::LocalizedIconList iconList;
689     LocaleSet localeSet;
690     FOREACH(it, serviceAppInfo.m_iconsList) {
691         iconList.push_back(WidgetRegisterInfo::LocalizedIcon(*it, localeSet));
692     }
693     registerWidgetIcons(widgetHandle, iconList);
694
695     WidgetRegisterInfo::LocalizedStartFileList startFileList;
696     WidgetRegisterInfo::LocalizedStartFile startFile;
697     WidgetRegisterInfo::StartFileProperties startFileProperties;
698     startFile.path = serviceAppInfo.serviceContent;
699     startFileProperties.type = L"";
700     startFileProperties.encoding = L"UTF-8";
701     startFile.propertiesForLocales[L""] = startFileProperties;
702     startFileList.push_back(startFile);
703     registerWidgetStartFile(widgetHandle, startFileList);
704
705
706     PropertyDAO::RegisterProperties(widgetHandle, tzAppId, widgetRegInfo);
707
708     registerWidgetFeatures(widgetHandle, widgetRegInfo.configInfo.featuresList);
709
710     registerWidgetPrivilege(widgetHandle, widgetRegInfo.configInfo.privilegeList);
711
712     registerWidgetWindowModes(widgetHandle, widgetRegInfo.configInfo.windowModes);
713
714     registerWidgetWarpInfo(widgetHandle, widgetRegInfo.configInfo.accessInfoSet);
715
716     registerWidgetAllowNavigationInfo(widgetHandle, widgetRegInfo.configInfo.allowNavigationInfoList);
717
718     registerWidgetCertificates(widgetHandle, widgetSecurity);
719
720     CertificateChainList list;
721     widgetSecurity.getCertificateChainList(list, SIGNATURE_DISTRIBUTOR);
722     registerCertificatesChains(widgetHandle, SIGNATURE_DISTRIBUTOR, list);
723
724     list.clear();
725     widgetSecurity.getCertificateChainList(list, SIGNATURE_AUTHOR);
726     registerCertificatesChains(widgetHandle, SIGNATURE_AUTHOR, list);
727
728     registerWidgetSettings(widgetHandle, widgetRegInfo.configInfo.settingsList);
729
730     registerEncryptedResouceInfo(widgetHandle, widgetRegInfo.encryptedFiles);
731
732     registerExternalLocations(widgetHandle, widgetRegInfo.externalLocations);
733 }
734
735 void WidgetDAO::registerExternalLocations(
736     DbWidgetHandle widgetHandle,
737     const ExternalLocationList &
738     externals)
739 {
740     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
741     {
742         using namespace DPL::DB::ORM;
743         using namespace DPL::DB::ORM::wrt;
744         DPL::DB::ORM::wrt::ScopedTransaction transaction(
745             &WrtDatabase::interface());
746         LogDebug("Inserting external files for widgetHandle: " << widgetHandle);
747         FOREACH(it, externals)
748         {
749             WidgetExternalLocations::Row row;
750             row.Set_app_id(widgetHandle);
751             row.Set_path(DPL::FromUTF8String(*it));
752
753             DO_INSERT(row, WidgetExternalLocations)
754         }
755         transaction.Commit();
756     }
757     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register external files");
758 }
759
760 void WidgetDAO::unregisterAllExternalLocations()
761 {
762     using namespace DPL::DB::ORM;
763     using namespace DPL::DB::ORM::wrt;
764     LogDebug("Deleting external files for widgetHandle: " << m_widgetHandle);
765     WRT_DB_DELETE(del, WidgetExternalLocations, &WrtDatabase::interface());
766     del->Where(Equals<WidgetExternalLocations::app_id>(m_widgetHandle));
767     del->Execute();
768 }
769
770 void WidgetDAO::unregisterWidget(const TizenAppId & tzAppId)
771 {
772     LogDebug("Unregistering widget from DB. tzAppId: " << tzAppId);
773     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
774     {
775         DPL::DB::ORM::wrt::ScopedTransaction transaction(
776             &WrtDatabase::interface());
777         unregisterWidgetInternal(tzAppId);
778         transaction.Commit();
779     }
780     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to unregister widget")
781 }
782
783 void WidgetDAO::unregisterWidgetInternal(
784     const TizenAppId & tzAppId)
785 {
786     using namespace DPL::DB::ORM;
787     using namespace DPL::DB::ORM::wrt;
788
789     DbWidgetHandle handle = getHandle(tzAppId);
790
791     // Delete from table Widget Info
792     WRT_DB_DELETE(del, WidgetInfo, &WrtDatabase::interface())
793     del->Where(Equals<WidgetInfo::app_id>(handle));
794     del->Execute();
795
796     // Deleting in other tables is done via "delete cascade" in SQL
797 }
798
799 #undef DO_INSERT
800
801 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
802 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
803 } // namespace WrtDB