Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules_wearable / 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 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 DPL::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 }
221
222 #define DO_INSERT(row, table)                              \
223     {                                                      \
224         WRT_DB_INSERT(insert, table, &WrtDatabase::interface()) \
225         insert->Values(row);                               \
226         insert->Execute();                                 \
227     }
228
229 void WidgetDAO::registerWidgetExtendedInfo(DbWidgetHandle widgetHandle,
230         time_t installedTime,
231         const DPL::OptionalString & splashImgSrc, const DPL::OptionalString & backgroundPage,
232         const DPL::OptionalString & widgetInstalledPath)
233 {
234     //Try and transaction not needed
235     using namespace DPL::DB::ORM;
236     using namespace DPL::DB::ORM::wrt;
237
238     WidgetExtendedInfo::Row row;
239     row.Set_app_id(widgetHandle);
240     row.Set_install_time(installedTime);
241     row.Set_splash_img_src(splashImgSrc);
242     row.Set_background_page(backgroundPage);
243     row.Set_installed_path(widgetInstalledPath);
244
245     DO_INSERT(row, WidgetExtendedInfo)
246 }
247
248 DbWidgetHandle WidgetDAO::registerWidgetInfo(
249     const TizenAppId & tzAppId,
250     const TizenPkgId & tzPkgId,
251     const std::string & baseFolder,
252     AppType appType,
253     PkgType pkgType,
254     const ConfigParserData &widgetConfigurationInfo,
255     const IWidgetSecurity &widgetSecurity,
256     const DPL::Optional<DbWidgetHandle> handle)
257 {
258     using namespace DPL::DB::ORM;
259     using namespace DPL::DB::ORM::wrt;
260     // TODO in wrt_db all Columns in WidgetInfo have DEFAULT VALUE set.
261     // Because of that, "Optional" is not used there
262
263     WidgetInfo::Row row;
264     if (!!handle) {
265         row.Set_app_id(*handle);
266     }
267
268     row.Set_widget_type(appType);
269     row.Set_widget_id(widgetConfigurationInfo.widget_id);
270     row.Set_defaultlocale(widgetConfigurationInfo.defaultlocale);
271     row.Set_widget_version(widgetConfigurationInfo.version);
272     row.Set_widget_width(widgetConfigurationInfo.width);
273     row.Set_widget_height(widgetConfigurationInfo.height);
274     row.Set_author_name(widgetConfigurationInfo.authorName);
275     row.Set_author_email(widgetConfigurationInfo.authorEmail);
276     row.Set_author_href(widgetConfigurationInfo.authorHref);
277     row.Set_csp_policy(widgetConfigurationInfo.cspPolicy);
278     row.Set_csp_policy_report_only(widgetConfigurationInfo.cspPolicyReportOnly);
279     row.Set_base_folder(DPL::FromUTF8String(baseFolder));
280     row.Set_webkit_plugins_required(widgetConfigurationInfo.flashNeeded);
281     row.Set_tizen_appid(tzAppId);
282     row.Set_tizen_pkgid(tzPkgId);
283     {
284         if (!!widgetConfigurationInfo.minVersionRequired) {
285             row.Set_min_version(widgetConfigurationInfo.minVersionRequired);
286         } else if (!!widgetConfigurationInfo.tizenMinVersionRequired) {
287             row.Set_min_version(widgetConfigurationInfo.tizenMinVersionRequired);
288         }
289     }
290     row.Set_back_supported(widgetConfigurationInfo.backSupported);
291     row.Set_access_network(widgetConfigurationInfo.accessNetwork);
292     row.Set_pkg_type(pkgType);
293     row.Set_security_model_version(
294         static_cast<int>(widgetConfigurationInfo.securityModelVersion));
295
296     Try
297     {
298         DO_INSERT(row, WidgetInfo);
299     }
300     Catch(DPL::DB::SqlConnection::Exception::Base)
301     {
302         ReThrowMsg(WidgetDAO::Exception::DatabaseError,
303                    "Failed to register widget info.");
304     }
305
306     if (!handle) {
307         //get autoincremented value of widgetHandle
308         WRT_DB_SELECT(select, WidgetInfo, &WrtDatabase::interface())
309         select->Where(Equals<WidgetInfo::tizen_appid>(tzAppId));
310         return select->GetSingleValue<WidgetInfo::app_id>();
311     } else {
312         return *handle;
313     }
314 }
315
316 void WidgetDAO::registerWidgetLocalizedInfo(DbWidgetHandle widgetHandle,
317                                            const ConfigParserData::LocalizedDataSet &localizedDataSet)
318 {
319     using namespace DPL::DB::ORM;
320     using namespace DPL::DB::ORM::wrt;
321
322     FOREACH(it, localizedDataSet)
323     {
324         const DPL::String& locale = it->first;
325         const ConfigParserData::LocalizedData& data = it->second;
326
327         LocalizedWidgetInfo::Row row;
328         row.Set_app_id(widgetHandle);
329         row.Set_widget_locale(locale);
330         row.Set_widget_name(data.name);
331         row.Set_widget_shortname(data.shortName);
332         row.Set_widget_description(data.description);
333         row.Set_widget_license(data.license);
334         row.Set_widget_license_file(data.licenseFile);
335         row.Set_widget_license_href(data.licenseHref);
336
337         DO_INSERT(row, LocalizedWidgetInfo)
338     }
339 }
340
341 void WidgetDAO::registerWidgetIcons(DbWidgetHandle widgetHandle,
342                                     const WidgetRegisterInfo::LocalizedIconList &icons)
343 {
344     using namespace DPL::DB::ORM;
345     using namespace DPL::DB::ORM::wrt;
346
347     FOREACH(i, icons)
348     {
349         wrt::WidgetIcon::icon_id::ColumnType icon_id;
350         {
351             wrt::WidgetIcon::Row row;
352             row.Set_app_id(widgetHandle);
353             row.Set_icon_src(i->src);
354             row.Set_icon_width(i->width);
355             row.Set_icon_height(i->height);
356
357             WRT_DB_INSERT(insert, wrt::WidgetIcon, &WrtDatabase::interface())
358             insert->Values(row);
359             icon_id = static_cast<int>(insert->Execute());
360         }
361
362         FOREACH(j, i->availableLocales)
363         {
364             WidgetLocalizedIcon::Row row;
365             row.Set_app_id(widgetHandle);
366             row.Set_icon_id(icon_id);
367             row.Set_widget_locale(*j);
368             DO_INSERT(row, WidgetLocalizedIcon)
369         }
370     }
371 }
372
373 void WidgetDAO::registerWidgetStartFile(DbWidgetHandle widgetHandle,
374                                         const WidgetRegisterInfo::LocalizedStartFileList &startFiles)
375 {
376     using namespace DPL::DB::ORM;
377     using namespace DPL::DB::ORM::wrt;
378
379     FOREACH(i, startFiles)
380     {
381         WidgetStartFile::start_file_id::ColumnType startFileID;
382         {
383             WidgetStartFile::Row row;
384             row.Set_app_id(widgetHandle);
385             row.Set_src(i->path);
386
387             WRT_DB_INSERT(insert, WidgetStartFile, &WrtDatabase::interface())
388             insert->Values(row);
389             startFileID = static_cast<int>(insert->Execute());
390         }
391
392         FOREACH(j, i->propertiesForLocales)
393         {
394             WidgetLocalizedStartFile::Row row;
395             row.Set_app_id(widgetHandle);
396             row.Set_start_file_id(startFileID);
397             row.Set_widget_locale(j->first);
398             row.Set_type(j->second.type);
399             row.Set_encoding(j->second.encoding);
400
401             DO_INSERT(row, WidgetLocalizedStartFile)
402         }
403     }
404 }
405
406 void WidgetDAO::registerWidgetFeatures(DbWidgetHandle widgetHandle,
407                                        const ConfigParserData::FeaturesList &featuresList)
408 {
409     using namespace DPL::DB::ORM;
410     FOREACH(pWidgetFeature, featuresList)
411     {
412         wrt::WidgetFeature::Row widgetFeature;
413         widgetFeature.Set_app_id(widgetHandle);
414         widgetFeature.Set_name(pWidgetFeature->name);
415         widgetFeature.Set_rejected(false);
416
417         DO_INSERT(widgetFeature, wrt::WidgetFeature)
418     }
419 }
420
421 void WidgetDAO::registerWidgetPrivilege(DbWidgetHandle widgetHandle,
422                                        const ConfigParserData::PrivilegeList &privilegeList)
423 {
424     using namespace DPL::DB::ORM;
425     using namespace DPL::DB::ORM::wrt;
426     FOREACH(it, privilegeList)
427     {
428         WidgetPrivilege::Row widgetPrivilege;
429         widgetPrivilege.Set_app_id(widgetHandle);
430         widgetPrivilege.Set_name(it->name);
431
432         DO_INSERT(widgetPrivilege, WidgetPrivilege)
433     }
434 }
435
436 void WidgetDAO::updateFeatureRejectStatus(const DbWidgetFeature &widgetFeature)
437 {
438     // This function could be merged with registerWidgetFeature but it requires
439     // desing change:
440     // 1. Check "ace step" in installer must be done before "update database
441     // step"
442     // And:
443     // ConfigurationParserData shouldn't be called "ParserData" any more.
444     using namespace DPL::DB::ORM;
445
446     wrt::ScopedTransaction transaction(&WrtDatabase::interface());
447     WRT_DB_SELECT(select, wrt::WidgetFeature, &WrtDatabase::interface())
448     select->Where(And(Equals<wrt::WidgetFeature::app_id>(m_widgetHandle),
449                       Equals<wrt::WidgetFeature::name>(widgetFeature.name)));
450
451     auto row = select->GetSingleRow();
452     row.Set_rejected(widgetFeature.rejected);
453
454     WRT_DB_UPDATE(update, wrt::WidgetFeature, &WrtDatabase::interface())
455     update->Where(And(Equals<wrt::WidgetFeature::app_id>(m_widgetHandle),
456                       Equals<wrt::WidgetFeature::name>(widgetFeature.name)));
457     update->Values(row);
458     update->Execute();
459     transaction.Commit();
460 }
461
462 void WidgetDAO::registerWidgetWindowModes(DbWidgetHandle widgetHandle,
463                                           const ConfigParserData::StringsList &windowModes)
464 {
465     using namespace DPL::DB::ORM;
466     using namespace DPL::DB::ORM::wrt;
467     FOREACH(i, windowModes)
468     {
469         wrt::WidgetWindowModes::Row windowMode;
470         windowMode.Set_app_id(widgetHandle);
471         windowMode.Set_window_mode(*i);
472
473         DO_INSERT(windowMode, wrt::WidgetWindowModes)
474     }
475 }
476
477 void WidgetDAO::registerWidgetWarpInfo(DbWidgetHandle widgetHandle,
478                                        const ConfigParserData::AccessInfoSet &accessInfoSet)
479 {
480     using namespace DPL::DB::ORM;
481     using namespace DPL::DB::ORM::wrt;
482     FOREACH(AccIt, accessInfoSet)
483     {
484         WidgetWARPInfo::Row row;
485         row.Set_app_id(widgetHandle);
486         row.Set_iri(AccIt->m_strIRI);
487         row.Set_subdomain_access(static_cast <int>(
488                                      AccIt->m_bSubDomainAccess));
489
490         DO_INSERT(row, WidgetWARPInfo)
491     }
492 }
493
494 void WidgetDAO::registerWidgetAllowNavigationInfo(DbWidgetHandle widgetHandle,
495                                                   const ConfigParserData::AllowNavigationInfoList &allowNavigationInfoList)
496 {
497     using namespace DPL::DB::ORM;
498     using namespace DPL::DB::ORM::wrt;
499     FOREACH(allowNaviIt, allowNavigationInfoList)
500     {
501         WidgetAllowNavigation::Row row;
502         row.Set_app_id(widgetHandle);
503         row.Set_scheme(allowNaviIt->m_scheme);
504         row.Set_host(allowNaviIt->m_host);
505         DO_INSERT(row, WidgetAllowNavigation)
506     }
507 }
508
509 void WidgetDAO::registerWidgetCertificates(DbWidgetHandle widgetHandle,
510                                            const IWidgetSecurity &widgetSecurity)
511 {
512     using namespace DPL::DB::ORM;
513     using namespace DPL::DB::ORM::wrt;
514
515     FOREACH(it, widgetSecurity.getCertificateList())
516     {
517         WidgetCertificateFingerprint::Row row;
518         row.Set_app_id(widgetHandle);
519         row.Set_owner(it->owner);
520         row.Set_chainid(it->chainId);
521         row.Set_type(it->type);
522         row.Set_md5_fingerprint(DPL::FromUTF8String(it->strMD5Fingerprint));
523         row.Set_sha1_fingerprint(DPL::FromUTF8String(it->strSHA1Fingerprint));
524         row.Set_common_name(it->strCommonName);
525
526         DO_INSERT(row, WidgetCertificateFingerprint)
527     }
528 }
529
530 void WidgetDAO::registerCertificatesChains(
531     DbWidgetHandle widgetHandle,
532     CertificateSource certificateSource,
533     const CertificateChainList &
534     certificateChainList)
535 {
536     using namespace DPL::DB::ORM;
537     using namespace DPL::DB::ORM::wrt;
538     FOREACH(certChain, certificateChainList)
539     {
540         WidgetCertificate::Row row;
541         row.Set_app_id(widgetHandle);
542         row.Set_cert_source(certificateSource);
543         row.Set_encoded_chain(DPL::FromASCIIString(*certChain));
544
545         DO_INSERT(row, WidgetCertificate);
546     }
547 }
548
549 void WidgetDAO::registerWidgetSettings(DbWidgetHandle widgetHandle,
550                                        const  ConfigParserData::SettingsList &settingsList)
551 {
552     using namespace DPL::DB::ORM;
553     using namespace DPL::DB::ORM::wrt;
554
555     FOREACH(pWidgetSetting, settingsList)
556     {
557         SettingsList::Row row;
558         row.Set_appId(widgetHandle);
559         row.Set_settingName(pWidgetSetting->m_name);
560         row.Set_settingValue(pWidgetSetting->m_value);
561
562         DO_INSERT(row, SettingsList)
563     }
564 }
565
566 void WidgetDAO::insertAppControlInfo(DbWidgetHandle handle,
567                                              DPL::String src,
568                                              DPL::String operation,
569                                              DPL::String uri,
570                                              DPL::String mime,
571                                              unsigned index,
572                                              unsigned disposition)
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_execute_index(index);
581     row.Set_src(src);
582     row.Set_operation(operation);
583     row.Set_uri(uri);
584     row.Set_mime(mime);
585     row.Set_disposition(disposition);
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         unsigned index = appControl_it->m_index;
602         unsigned disposition =
603             static_cast<unsigned>(appControl_it->m_disposition);
604
605         if (!appControl_it->m_uriList.empty())
606         {
607             FOREACH(uri_it, appControl_it->m_uriList)
608             {
609                 DPL::String uri = *uri_it;
610
611                 if (!appControl_it->m_mimeList.empty())
612                 {
613                     FOREACH(mime_it, appControl_it->m_mimeList)
614                     {
615                         DPL::String mime = *mime_it;
616
617                         insertAppControlInfo(widgetHandle, src, operation, uri, mime, index, disposition);
618                     }
619                 }
620                 else
621                 {
622                     DPL::String mime = L"";
623
624                     insertAppControlInfo(widgetHandle, src, operation, uri, mime, index, disposition);
625                 }
626             }
627         }
628         else
629         {
630             DPL::String uri = L"";
631
632             if (!appControl_it->m_mimeList.empty())
633             {
634                 FOREACH(mime_it, appControl_it->m_mimeList)
635                 {
636                     DPL::String mime = *mime_it;
637
638                     insertAppControlInfo(widgetHandle, src, operation, uri, mime, index, disposition);
639                 }
640             }
641             else
642             {
643                 DPL::String mime = L"";
644
645                 insertAppControlInfo(widgetHandle, src, operation, uri, mime, index, disposition);
646             }
647         }
648     }
649 }
650
651 void WidgetDAO::registerEncryptedResouceInfo(DbWidgetHandle widgetHandle,
652                                              const EncryptedFileList &encryptedFiles)
653 {
654     using namespace DPL::DB::ORM;
655     using namespace DPL::DB::ORM::wrt;
656
657     FOREACH(it, encryptedFiles)
658     {
659         EncryptedResourceList::Row row;
660         row.Set_app_id(widgetHandle);
661         row.Set_resource(it->fileName);
662         row.Set_size(it->fileSize);
663
664         DO_INSERT(row, EncryptedResourceList)
665     }
666 }
667
668 void WidgetDAO::registerServiceInternal(const ConfigParserData::ServiceAppInfo &serviceAppInfo, const WidgetRegisterInfo &widgetRegInfo, const IWidgetSecurity &widgetSecurity)
669 {
670     using namespace DPL::DB::ORM;
671     using namespace DPL::DB::ORM::wrt;
672
673     DPL::String tzAppId = serviceAppInfo.serviceId;
674
675
676     DbWidgetHandle widgetHandle = registerWidgetInfo(tzAppId, widgetRegInfo.tzPkgid, widgetRegInfo.baseFolder,
677                                                  APP_TYPE_TIZENWEBSERVICE, widgetRegInfo.packagingType.pkgType,
678                                                  widgetRegInfo.configInfo, widgetSecurity);
679
680     registerWidgetExtendedInfo(widgetHandle, widgetRegInfo.installedTime,
681                                                         widgetRegInfo.configInfo.splashImgSrc, widgetRegInfo.configInfo.backgroundPage,
682                                                         widgetRegInfo.widgetInstalledPath);
683
684     registerWidgetLocalizedInfo(widgetHandle, serviceAppInfo.m_localizedDataSet);
685
686
687     WidgetRegisterInfo::LocalizedIconList iconList;
688     LocaleSet localeSet;
689     FOREACH(it, serviceAppInfo.m_iconsList) {
690         iconList.push_back(WidgetRegisterInfo::LocalizedIcon(*it, localeSet));
691     }
692     registerWidgetIcons(widgetHandle, iconList);
693
694     WidgetRegisterInfo::LocalizedStartFileList startFileList;
695     WidgetRegisterInfo::LocalizedStartFile startFile;
696     WidgetRegisterInfo::StartFileProperties startFileProperties;
697     startFile.path = serviceAppInfo.serviceContent;
698     startFileProperties.type = L"";
699     startFileProperties.encoding = L"UTF-8";
700     startFile.propertiesForLocales[L""] = startFileProperties;
701     startFileList.push_back(startFile);
702     registerWidgetStartFile(widgetHandle, startFileList);
703
704
705     PropertyDAO::RegisterProperties(widgetHandle, tzAppId, widgetRegInfo);
706
707     registerWidgetFeatures(widgetHandle, widgetRegInfo.configInfo.featuresList);
708
709     registerWidgetPrivilege(widgetHandle, widgetRegInfo.configInfo.privilegeList);
710
711     registerWidgetWindowModes(widgetHandle, widgetRegInfo.configInfo.windowModes);
712
713     registerWidgetWarpInfo(widgetHandle, widgetRegInfo.configInfo.accessInfoSet);
714
715     registerWidgetAllowNavigationInfo(widgetHandle, widgetRegInfo.configInfo.allowNavigationInfoList);
716
717     registerWidgetCertificates(widgetHandle, widgetSecurity);
718
719     CertificateChainList list;
720     widgetSecurity.getCertificateChainList(list, SIGNATURE_DISTRIBUTOR);
721     registerCertificatesChains(widgetHandle, SIGNATURE_DISTRIBUTOR, list);
722
723     list.clear();
724     widgetSecurity.getCertificateChainList(list, SIGNATURE_AUTHOR);
725     registerCertificatesChains(widgetHandle, SIGNATURE_AUTHOR, list);
726
727     registerWidgetSettings(widgetHandle, widgetRegInfo.configInfo.settingsList);
728
729     registerEncryptedResouceInfo(widgetHandle, widgetRegInfo.encryptedFiles);
730
731     registerExternalLocations(widgetHandle, widgetRegInfo.externalLocations);
732 }
733
734 void WidgetDAO::registerExternalLocations(
735     DbWidgetHandle widgetHandle,
736     const ExternalLocationList &
737     externals)
738 {
739     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
740     {
741         using namespace DPL::DB::ORM;
742         using namespace DPL::DB::ORM::wrt;
743         DPL::DB::ORM::wrt::ScopedTransaction transaction(
744             &WrtDatabase::interface());
745         LogDebug("Inserting external files for widgetHandle: " << widgetHandle);
746         FOREACH(it, externals)
747         {
748             WidgetExternalLocations::Row row;
749             row.Set_app_id(widgetHandle);
750             row.Set_path(DPL::FromUTF8String(*it));
751
752             DO_INSERT(row, WidgetExternalLocations)
753         }
754         transaction.Commit();
755     }
756     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register external files");
757 }
758
759 void WidgetDAO::unregisterAllExternalLocations()
760 {
761     using namespace DPL::DB::ORM;
762     using namespace DPL::DB::ORM::wrt;
763     LogDebug("Deleting external files for widgetHandle: " << m_widgetHandle);
764     WRT_DB_DELETE(del, WidgetExternalLocations, &WrtDatabase::interface());
765     del->Where(Equals<WidgetExternalLocations::app_id>(m_widgetHandle));
766     del->Execute();
767 }
768
769 void WidgetDAO::unregisterWidget(const TizenAppId & tzAppId)
770 {
771     LogDebug("Unregistering widget from DB. tzAppId: " << tzAppId);
772     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
773     {
774         DPL::DB::ORM::wrt::ScopedTransaction transaction(
775             &WrtDatabase::interface());
776         unregisterWidgetInternal(tzAppId);
777         transaction.Commit();
778     }
779     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to unregister widget")
780 }
781
782 void WidgetDAO::unregisterWidgetInternal(
783     const TizenAppId & tzAppId)
784 {
785     using namespace DPL::DB::ORM;
786     using namespace DPL::DB::ORM::wrt;
787
788     DbWidgetHandle handle = getHandle(tzAppId);
789
790     // Delete from table Widget Info
791     WRT_DB_DELETE(del, WidgetInfo, &WrtDatabase::interface())
792     del->Where(Equals<WidgetInfo::app_id>(handle));
793     del->Execute();
794
795     // Deleting in other tables is done via "delete cascade" in SQL
796 }
797
798 #undef DO_INSERT
799
800 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
801 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
802 } // namespace WrtDB