cdb99dae82d725ffd8191cfee157febfe4dcdaef
[framework/web/wrt-commons.git] / modules / widget_dao / dao / widget_dao.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * This file contains the definition of widget dao class.
18  *
19  * @file    widget_dao.cpp
20  * @author  Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
21  * @author  Bartosz Janiak (b.janiak@samsung.com)
22  * @author  Yang Jie (jie2.yang@samsung.com)
23  * @author  Koeun Choi(koeun.choi@samsung.com)
24  * @author  Pawel Sikorski(p.sikorski@samsung.com)
25  * @version 1.0
26  * @brief   This file contains the definition of Configuration.
27  */
28 #include <stddef.h>
29 #include <dpl/wrt-dao-rw/widget_dao.h>
30
31 #include <sstream>
32 #include <dpl/log/log.h>
33 #include <dpl/foreach.h>
34 #include <dpl/wrt-dao-ro/webruntime_database.h>
35 #include <dpl/wrt-dao-rw/property_dao.h>
36 #include <orm_generator_wrt.h>
37 #include <dpl/wrt-dao-ro/WrtDatabase.h>
38
39 namespace WrtDB {
40
41 //TODO in current solution in each getter there exists a check
42 //"IsWidgetInstalled". Maybe it should be verified, if it could be done
43 //differently  (check in WidgetDAO constructor)
44
45 #define SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN          Try
46
47 #define SQL_CONNECTION_EXCEPTION_HANDLER_END(message)   \
48     Catch(DPL::DB::SqlConnection::Exception::Base) {       \
49         LogError(message);                              \
50         ReThrowMsg(WidgetDAO::Exception::DatabaseError, \
51                    message);                            \
52     }
53
54 WidgetDAO::WidgetDAO(DPL::OptionalString widgetGUID) :
55     WidgetDAOReadOnly(WidgetDAOReadOnly::getHandle(widgetGUID))
56 {
57 }
58
59 WidgetDAO::WidgetDAO(DPL::String pkgName) :
60     WidgetDAOReadOnly(WidgetDAOReadOnly::getHandle(pkgName))
61 {
62 }
63
64 WidgetDAO::WidgetDAO(DbWidgetHandle handle) :
65     WidgetDAOReadOnly(handle)
66 {
67 }
68
69 WidgetDAO::~WidgetDAO()
70 {
71 }
72
73 void WidgetDAO::removeProperty(
74         const PropertyDAOReadOnly::WidgetPropertyKey &key)
75 {
76     Try {
77         PropertyDAO::RemoveProperty(m_widgetHandle, key);
78     }
79     Catch(PropertyDAOReadOnly::Exception::ReadOnlyProperty){
80         ReThrowMsg(WidgetDAO::Exception::DatabaseError,
81                    "Failure during removing property");
82     }
83 }
84
85 void WidgetDAO::setProperty(
86         const PropertyDAOReadOnly::WidgetPropertyKey &key,
87         const PropertyDAOReadOnly::WidgetPropertyValue &value,
88         bool readOnly)
89 {
90     Try {
91         PropertyDAO::SetProperty(m_widgetHandle, key, value, readOnly);
92     }
93     Catch(PropertyDAOReadOnly::Exception::ReadOnlyProperty){
94         ReThrowMsg(WidgetDAO::Exception::DatabaseError,
95                    "Failure during setting/updating property");
96     }
97 }
98
99 void WidgetDAO::setPkgName(const DPL::OptionalString& pkgName)
100 {
101     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
102     {
103         using namespace DPL::DB::ORM;
104         wrt::ScopedTransaction transaction(&WrtDatabase::interface());
105
106         isWidgetInstalled(getHandle());
107
108         wrt::WidgetInfo::Row row;
109         row.Set_pkgname(pkgName);
110
111         WRT_DB_UPDATE(update, wrt::WidgetInfo, &WrtDatabase::interface())
112         update->Where(
113             Equals<wrt::WidgetInfo::app_id>(getHandle()));
114
115         update->Values(row);
116         update->Execute();
117         transaction.Commit();
118     }
119     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget")
120 }
121
122 void WidgetDAO::registerWidget(
123         const WidgetPkgName & widgetPkgname,
124         const WidgetRegisterInfo &widgetRegInfo,
125         const IWacSecurity &wacSecurity)
126 {
127     LogDebug("Registering widget");
128     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
129     {
130         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
131         DbWidgetHandle widgetHandle = registerWidgetInternal(widgetPkgname, widgetRegInfo, wacSecurity);
132         registerExternalLocations(widgetHandle, widgetRegInfo.externalLocations);
133         //TODO: remove construction from handle and move that call to registerWidgetInternal
134         // in new version external fiels should be register together with other informations
135         transaction.Commit();
136     }
137     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget")
138 }
139
140 void WidgetDAO::registerWidget(
141             WrtDB::DbWidgetHandle handle,
142             const WidgetRegisterInfo & widgetRegInfo,
143             const IWacSecurity &wacSecurity)
144 {
145     LogDebug("Registering widget");
146     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
147     {
148         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
149         registerWidgetInternal(L"", widgetRegInfo, wacSecurity, handle);
150         transaction.Commit();
151     }
152     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget")
153 }
154
155 DbWidgetHandle WidgetDAO::registerWidget(
156             const WidgetRegisterInfo &pWidgetRegisterInfo,
157             const IWacSecurity &wacSecurity)
158 {
159     //make it more precise due to very fast tests
160     struct timeval tv;
161     gettimeofday(&tv, NULL);
162     srand(time(NULL) + tv.tv_usec);
163     DbWidgetHandle widgetHandle;
164     do {
165         widgetHandle = rand();
166     } while (isWidgetInstalled(widgetHandle));
167
168     registerWidget(widgetHandle, pWidgetRegisterInfo, wacSecurity);
169     return widgetHandle;
170 }
171
172 DbWidgetHandle WidgetDAO::registerWidgetInternal(
173         const WidgetPkgName & widgetName,
174         const WidgetRegisterInfo &widgetRegInfo,
175         const IWacSecurity &wacSecurity,
176         const DPL::Optional<DbWidgetHandle> handle)
177 {
178     //Register into WidgetInfo has to be first
179     //as all other tables depend upon that
180     DbWidgetHandle widgetHandle = registerWidgetInfo(widgetName, widgetRegInfo, wacSecurity, handle);
181
182     registerWidgetExtendedInfo(widgetHandle, widgetRegInfo);
183
184     registerWidgetLocalizedInfo(widgetHandle, widgetRegInfo);
185
186     registerWidgetIcons(widgetHandle, widgetRegInfo);
187
188     registerWidgetStartFile(widgetHandle, widgetRegInfo);
189
190     PropertyDAO::RegisterProperties(widgetHandle, widgetRegInfo);
191
192     registerWidgetFeatures(widgetHandle, widgetRegInfo);
193
194     registerWidgetWindowModes(widgetHandle, widgetRegInfo);
195
196     registerWidgetWarpInfo(widgetHandle, widgetRegInfo);
197
198     registerWidgetCertificates(widgetHandle, wacSecurity);
199
200     CertificateChainList list;
201     wacSecurity.getCertificateChainList(list, SIGNATURE_DISTRIBUTOR);
202     registerCertificatesChains(widgetHandle, SIGNATURE_DISTRIBUTOR, list);
203
204     list.clear();
205     wacSecurity.getCertificateChainList(list, SIGNATURE_AUTHOR);
206     registerCertificatesChains(widgetHandle, SIGNATURE_AUTHOR, list);
207
208     registerWidgetSettings(widgetHandle, widgetRegInfo);
209
210     registerAppService(widgetHandle, widgetRegInfo);
211
212     registerEncryptedResouceInfo(widgetHandle, widgetRegInfo);
213
214     return widgetHandle;
215 }
216
217 void WidgetDAO::registerOrUpdateWidget(
218         const WidgetPkgName & widgetName,
219         const WidgetRegisterInfo &widgetRegInfo,
220         const IWacSecurity &wacSecurity)
221 {
222     LogDebug("Reregistering widget");
223     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
224     {
225         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
226
227         unregisterWidgetInternal(widgetName);
228         DbWidgetHandle widgetHandle = registerWidgetInternal(widgetName, widgetRegInfo, wacSecurity);
229         registerExternalLocations(widgetHandle, widgetRegInfo.externalLocations);
230         //TODO: remove construction from handle and move that call to registerWidgetInternal
231         // in new version external fiels should be register together with other informations
232         transaction.Commit();
233     }
234     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to reregister widget")
235 }
236
237 #define DO_INSERT(row, table)                              \
238     {                                                      \
239         WRT_DB_INSERT(insert, table, &WrtDatabase::interface()) \
240         insert->Values(row);                               \
241         insert->Execute();                                 \
242     }
243
244 void WidgetDAO::registerWidgetExtendedInfo(DbWidgetHandle widgetHandle,
245         const WidgetRegisterInfo &regInfo)
246 {
247     //Try and transaction not needed
248     using namespace DPL::DB::ORM;
249     using namespace DPL::DB::ORM::wrt;
250
251
252     WidgetExtendedInfo::Row row;
253     row.Set_app_id(widgetHandle);
254     //    row.Set_share_href    (DPL::FromUTF8String(regInfo.shareHref));
255     row.Set_signature_type(regInfo.signatureType);
256     row.Set_test_widget(regInfo.isTestWidget);
257     row.Set_install_time(regInfo.installedTime);
258     row.Set_splash_img_src(regInfo.configInfo.splashImgSrc);
259     row.Set_background_page(regInfo.configInfo.backgroundPage);
260
261
262     DO_INSERT(row, WidgetExtendedInfo)
263 }
264
265 DbWidgetHandle WidgetDAO::registerWidgetInfo(
266         const WidgetPkgName & widgetName,
267         const WidgetRegisterInfo &regInfo,
268         const IWacSecurity &wacSecurity,
269         const DPL::Optional<DbWidgetHandle> handle)
270 {
271     using namespace DPL::DB::ORM;
272     using namespace DPL::DB::ORM::wrt;
273     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
274
275     // TODO in wrt_db all Columns in WidgetInfo have DEFAULT VALUE set.
276     // Because of that, "Optional" is not used there
277
278     WidgetInfo::Row row;
279     if(!!handle)
280     {
281         row.Set_app_id(*handle);
282     }
283
284     if (regInfo.webAppType == APP_TYPE_UNKNOWN && regInfo.type !=
285             APP_TYPE_UNKNOWN) {
286         // TODO : regInfo.type is temporary code for security.
287         //        This code will be removed.
288         row.Set_widget_type(regInfo.type.appType);
289     } else {
290         row.Set_widget_type(regInfo.webAppType.appType);
291     }
292     row.Set_widget_id(widgetConfigurationInfo.widget_id);
293     row.Set_defaultlocale(widgetConfigurationInfo.defaultlocale);
294     row.Set_widget_version(widgetConfigurationInfo.version);
295     row.Set_widget_width(widgetConfigurationInfo.width);
296     row.Set_widget_height(widgetConfigurationInfo.height);
297     row.Set_author_name(widgetConfigurationInfo.authorName);
298     row.Set_author_email(widgetConfigurationInfo.authorEmail);
299     row.Set_author_href(widgetConfigurationInfo.authorHref);
300     row.Set_base_folder(DPL::FromUTF8String(regInfo.baseFolder));
301     row.Set_webkit_plugins_required(widgetConfigurationInfo.flashNeeded);
302     row.Set_recognized(wacSecurity.isRecognized());
303     row.Set_wac_signed(wacSecurity.isWacSigned());
304     row.Set_distributor_signed(wacSecurity.isDistributorSigned());
305     row.Set_pkgname(widgetName);
306     {
307         std::stringstream tmp;
308         tmp << regInfo.minVersion;
309         row.Set_min_version(DPL::FromUTF8String(tmp.str()));
310     }
311     row.Set_back_supported(widgetConfigurationInfo.backSupported);
312     row.Set_access_network(widgetConfigurationInfo.accessNetwork);
313     row.Set_pkgname(regInfo.pkgname);
314     row.Set_pkg_type(regInfo.packagingType.pkgType);
315
316     Try
317     {
318         DO_INSERT(row, WidgetInfo);
319     }
320     Catch(DPL::DB::SqlConnection::Exception::Base)
321     {
322         ReThrowMsg(WidgetDAO::Exception::DatabaseError,
323                    "Failed to register widget info.");
324     }
325
326     if(!handle)
327     {
328         //get autoincremented value of widgetHandle
329         WRT_DB_SELECT(select, WidgetInfo, &WrtDatabase::interface())
330         select->Where(Equals<WidgetInfo::pkgname>(widgetName));
331         return select->GetSingleValue<WidgetInfo::app_id>();
332     }
333     else
334     {
335         return *handle;
336     }
337 }
338
339 void WidgetDAO::registerWidgetLocalizedInfo(DbWidgetHandle widgetHandle,
340         const WidgetRegisterInfo &regInfo)
341 {
342     using namespace DPL::DB::ORM;
343     using namespace DPL::DB::ORM::wrt;
344
345     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
346
347     FOREACH(it, widgetConfigurationInfo.localizedDataSet)
348     {
349         const DPL::String& locale = it->first;
350         const ConfigParserData::LocalizedData& data = it->second;
351
352         LocalizedWidgetInfo::Row row;
353         row.Set_app_id(widgetHandle);
354         row.Set_widget_locale(locale);
355         row.Set_widget_name(data.name);
356         row.Set_widget_shortname(data.shortName);
357         row.Set_widget_description(data.description);
358         row.Set_widget_license(data.license);
359         row.Set_widget_license_file(data.licenseFile);
360         row.Set_widget_license_href(data.licenseHref);
361
362         DO_INSERT(row, LocalizedWidgetInfo)
363     }
364 }
365
366 void WidgetDAO::registerWidgetIcons(DbWidgetHandle widgetHandle,
367         const WidgetRegisterInfo &regInfo)
368 {
369     using namespace DPL::DB::ORM;
370     using namespace DPL::DB::ORM::wrt;
371
372
373     FOREACH(i, regInfo.localizationData.icons)
374     {
375         wrt::WidgetIcon::icon_id::ColumnType icon_id;
376         {
377             wrt::WidgetIcon::Row row;
378             row.Set_app_id(widgetHandle);
379             row.Set_icon_src(i->src);
380             row.Set_icon_width(i->width);
381             row.Set_icon_height(i->height);
382
383             WRT_DB_INSERT(insert, wrt::WidgetIcon, &WrtDatabase::interface())
384             insert->Values(row);
385             icon_id = insert->Execute();
386         }
387
388         FOREACH(j, i->availableLocales)
389         {
390             WidgetLocalizedIcon::Row row;
391             row.Set_app_id(widgetHandle);
392             row.Set_icon_id(icon_id);
393             row.Set_widget_locale(*j);
394             WRT_DB_SELECT(select, WidgetLocalizedIcon, &WrtDatabase::interface())
395             select->Where(And(Equals<WidgetLocalizedIcon::app_id>(widgetHandle),
396                               Equals<WidgetLocalizedIcon::widget_locale>(*j)));
397             WidgetLocalizedIcon::Select::RowList rows = select->GetRowList();
398
399             bool flag = !rows.empty();
400
401             if(flag == true)
402             {
403                 // already default icon value of same locale exists
404                 WRT_DB_UPDATE(update, WidgetLocalizedIcon, &WrtDatabase::interface())
405                 update->Where(And(Equals<WidgetLocalizedIcon::app_id>(widgetHandle),
406                                   Equals<WidgetLocalizedIcon::widget_locale>(*j)));
407                 update->Values(row);
408                 update->Execute();
409             }else{
410                 // any icon value of same locale doesn't exist
411                 DO_INSERT(row, WidgetLocalizedIcon)
412             }
413         }
414     }
415 }
416
417 void WidgetDAO::registerWidgetStartFile(DbWidgetHandle widgetHandle,
418         const WidgetRegisterInfo &regInfo)
419 {
420     using namespace DPL::DB::ORM;
421     using namespace DPL::DB::ORM::wrt;
422
423
424     FOREACH(i, regInfo.localizationData.startFiles)
425     {
426         WidgetStartFile::start_file_id::ColumnType startFileID;
427         {
428             WidgetStartFile::Row row;
429             row.Set_app_id(widgetHandle);
430             row.Set_src(i->path);
431
432             WRT_DB_INSERT(insert, WidgetStartFile, &WrtDatabase::interface())
433             insert->Values(row);
434             startFileID = insert->Execute();
435         }
436
437         FOREACH(j, i->propertiesForLocales)
438         {
439             WidgetLocalizedStartFile::Row row;
440             row.Set_app_id(widgetHandle);
441             row.Set_start_file_id(startFileID);
442             row.Set_widget_locale(j->first);
443             row.Set_type(j->second.type);
444             row.Set_encoding(j->second.encoding);
445
446             DO_INSERT(row, WidgetLocalizedStartFile)
447         }
448     }
449 }
450
451 void WidgetDAO::registerWidgetFeatures(DbWidgetHandle widgetHandle,
452         const WidgetRegisterInfo &regInfo)
453 {
454     using namespace DPL::DB::ORM;
455     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
456
457
458     FOREACH(pWidgetFeature, widgetConfigurationInfo.featuresList)
459     {
460         wrt::WidgetFeature::Row widgetFeature;
461         widgetFeature.Set_app_id(widgetHandle);
462         widgetFeature.Set_name(pWidgetFeature->name);
463         widgetFeature.Set_required(pWidgetFeature->required);
464         widgetFeature.Set_rejected(false);
465
466         wrt::WidgetFeature::widget_feature_id::ColumnType widgetFeatureID;
467         {
468             WRT_DB_INSERT(insert, wrt::WidgetFeature, &WrtDatabase::interface())
469             insert->Values(widgetFeature);
470             widgetFeatureID = insert->Execute();
471         }
472
473         // Insert into table FeatureParam
474         wrt::FeatureParam::Row featureParam;
475         featureParam.Set_widget_feature_id(widgetFeatureID);
476
477         FOREACH(iter, pWidgetFeature->paramsList)
478         {
479             featureParam.Set_name(iter->name);
480             featureParam.Set_value(iter->value);
481
482             DO_INSERT(featureParam, wrt::FeatureParam)
483         }
484     }
485 }
486
487 void WidgetDAO::updateFeatureRejectStatus(const DbWidgetFeature &widgetFeature){
488     // This function could be merged with registerWidgetFeature but it requires desing change:
489     // 1. Check "ace step" in installer must be done before "update database step"
490     // And:
491     // ConfigurationParserData shouldn't be called "ParserData" any more.
492     using namespace DPL::DB::ORM;
493
494     wrt::ScopedTransaction transaction(&WrtDatabase::interface());
495     WRT_DB_SELECT(select, wrt::WidgetFeature, &WrtDatabase::interface())
496     select->Where(And(Equals<wrt::WidgetFeature::app_id>(m_widgetHandle),
497                       Equals<wrt::WidgetFeature::name>(widgetFeature.name)));
498
499     auto row = select->GetSingleRow();
500     row.Set_rejected(widgetFeature.rejected);
501
502     WRT_DB_UPDATE(update, wrt::WidgetFeature, &WrtDatabase::interface())
503     update->Where(And(Equals<wrt::WidgetFeature::app_id>(m_widgetHandle),
504                       Equals<wrt::WidgetFeature::name>(widgetFeature.name)));
505     update->Values(row);
506     update->Execute();
507     transaction.Commit();
508 }
509
510 void WidgetDAO::registerWidgetWindowModes(DbWidgetHandle widgetHandle,
511         const WidgetRegisterInfo &regInfo)
512 {
513     using namespace DPL::DB::ORM;
514     using namespace DPL::DB::ORM::wrt;
515     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
516
517
518     FOREACH(i, widgetConfigurationInfo.windowModes)
519     {
520         wrt::WidgetWindowModes::Row windowMode;
521         windowMode.Set_app_id(widgetHandle);
522         windowMode.Set_window_mode(*i);
523
524         DO_INSERT(windowMode, wrt::WidgetWindowModes)
525     }
526 }
527
528 void WidgetDAO::registerWidgetWarpInfo(DbWidgetHandle widgetHandle,
529         const WidgetRegisterInfo &regInfo)
530 {
531     using namespace DPL::DB::ORM;
532     using namespace DPL::DB::ORM::wrt;
533     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
534
535
536     FOREACH(AccIt, widgetConfigurationInfo.accessInfoSet)
537     {
538         WidgetWARPInfo::Row row;
539         row.Set_app_id(widgetHandle);
540         row.Set_iri(AccIt->m_strIRI);
541         row.Set_subdomain_access(static_cast <int>(
542                                      AccIt->m_bSubDomainAccess));
543
544         DO_INSERT(row, WidgetWARPInfo)
545     }
546 }
547
548 void WidgetDAO::registerWidgetCertificates(DbWidgetHandle widgetHandle,
549         const IWacSecurity &wacSecurity)
550 {
551     using namespace DPL::DB::ORM;
552     using namespace DPL::DB::ORM::wrt;
553
554
555     FOREACH(it, wacSecurity.getCertificateList())
556     {
557         WidgetCertificateFingerprint::Row row;
558         row.Set_app_id(widgetHandle);
559         row.Set_owner(it->owner);
560         row.Set_chainid(it->chainId);
561         row.Set_type(it->type);
562         row.Set_md5_fingerprint(DPL::FromUTF8String(it->strMD5Fingerprint));
563         row.Set_sha1_fingerprint(DPL::FromUTF8String(it->strSHA1Fingerprint));
564         row.Set_common_name(it->strCommonName);
565
566         DO_INSERT(row, WidgetCertificateFingerprint)
567     }
568 }
569
570 void WidgetDAO::registerCertificatesChains(DbWidgetHandle widgetHandle,
571         CertificateSource certificateSource,
572         const CertificateChainList &certificateChainList)
573 {
574     using namespace DPL::DB::ORM;
575     using namespace DPL::DB::ORM::wrt;
576     FOREACH(certChain, certificateChainList)
577     {
578         WidgetCertificate::Row row;
579         row.Set_app_id(widgetHandle);
580         row.Set_cert_source(certificateSource);
581         row.Set_encoded_chain(DPL::FromASCIIString(*certChain));
582
583         DO_INSERT(row, WidgetCertificate);
584     }
585 }
586
587 void WidgetDAO::registerWidgetSettings(DbWidgetHandle widgetHandle,
588         const WidgetRegisterInfo &regInfo)
589 {
590     using namespace DPL::DB::ORM;
591     using namespace DPL::DB::ORM::wrt;
592
593     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
594
595     FOREACH(pWidgetSetting, widgetConfigurationInfo.settingsList)
596     {
597         SettingsList::Row row;
598         row.Set_appId(widgetHandle);
599         row.Set_settingName(pWidgetSetting->m_name);
600         row.Set_settingValue(pWidgetSetting->m_value);
601
602         DO_INSERT(row, SettingsList)
603     }
604 }
605
606 void WidgetDAO::registerAppService(DbWidgetHandle widgetHandle,
607                                    const WidgetRegisterInfo &regInfo)
608 {
609     using namespace DPL::DB::ORM;
610     using namespace DPL::DB::ORM::wrt;
611     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
612
613     FOREACH(ASIt, widgetConfigurationInfo.appServiceList)
614     {
615         ApplicationServiceInfo::Row row;
616         row.Set_app_id(widgetHandle);
617         row.Set_src(ASIt->m_src);
618         row.Set_operation(ASIt->m_operation);
619         row.Set_scheme(ASIt->m_scheme);
620         row.Set_mime(ASIt->m_mime);
621
622         DO_INSERT(row, ApplicationServiceInfo)
623     }
624 }
625
626 void WidgetDAO::registerEncryptedResouceInfo(DbWidgetHandle widgetHandle,
627         const WidgetRegisterInfo &regInfo)
628 {
629     using namespace DPL::DB::ORM;
630     using namespace DPL::DB::ORM::wrt;
631
632     FOREACH(it, regInfo.encryptedFiles)
633     {
634         EncryptedResourceList::Row row;
635         row.Set_app_id(widgetHandle);
636         row.Set_resource(it->fileName);
637         row.Set_size(it->fileSize);
638
639         DO_INSERT(row, EncryptedResourceList)
640     }
641 }
642
643 void WidgetDAO::registerExternalLocations(const ExternalLocationList & externals)
644 {
645     registerExternalLocations(m_widgetHandle, externals);
646 }
647
648 void WidgetDAO::registerExternalLocations(DbWidgetHandle widgetHandle,
649                                           const ExternalLocationList & externals)
650 {
651     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
652     {
653         using namespace DPL::DB::ORM;
654         using namespace DPL::DB::ORM::wrt;
655         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
656         LogDebug("Inserting external files for widgetHandle: " << widgetHandle);
657         FOREACH(it, externals)
658         {
659             WidgetExternalLocations::Row row;
660             row.Set_app_id(widgetHandle);
661             row.Set_path(DPL::FromUTF8String(*it));
662
663             DO_INSERT(row, WidgetExternalLocations)
664         }
665         transaction.Commit();
666     }
667     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register external files");
668 }
669
670 void WidgetDAO::unregisterAllExternalLocations()
671 {
672     using namespace DPL::DB::ORM;
673     using namespace DPL::DB::ORM::wrt;
674     LogDebug("Deleting external files for widgetHandle: " << m_widgetHandle);
675     WRT_DB_DELETE(del, WidgetExternalLocations, &WrtDatabase::interface());
676     del->Where(Equals<WidgetExternalLocations::app_id>(m_widgetHandle));
677     del->Execute();
678 }
679
680 void WidgetDAO::unregisterWidget(const WidgetPkgName & pkgName)
681 {
682     LogDebug("Unregistering widget from DB. PkgName: " << pkgName);
683     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
684     {
685         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
686         unregisterWidgetInternal(pkgName);
687         transaction.Commit();
688     }
689     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to unregister widget")
690 }
691
692 void WidgetDAO::unregisterWidget(WrtDB::DbWidgetHandle handle)
693 {
694     LogDebug("Unregistering widget from DB. Handle: " << handle);
695     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
696     {
697         using namespace DPL::DB::ORM;
698         using namespace DPL::DB::ORM::wrt;
699         ScopedTransaction transaction(&WrtDatabase::interface());
700
701         // Delete from table Widget Info
702         WRT_DB_DELETE(del, WidgetInfo, &WrtDatabase::interface())
703         del->Where(Equals<WidgetInfo::app_id>(handle));
704         del->Execute();
705
706         transaction.Commit();
707     }
708     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to unregister widget")
709 }
710
711 void WidgetDAO::unregisterWidgetInternal(
712         const WidgetPkgName & pkgName)
713 {
714     using namespace DPL::DB::ORM;
715     using namespace DPL::DB::ORM::wrt;
716
717     DbWidgetHandle handle = getHandle(pkgName);
718
719     // Delete from table Widget Info
720     WRT_DB_DELETE(del, WidgetInfo, &WrtDatabase::interface())
721     del->Where(Equals<WidgetInfo::app_id>(handle));
722     del->Execute();
723
724     // Deleting in other tables is done via "delete cascade" in SQL
725 }
726
727 #undef DO_INSERT
728
729 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
730 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
731
732 } // namespace WrtDB