c5bbc5bd79041fc81f44cb87afbb3c9eb5ed5838
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_widget_config.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  * @file    task_widget_config.cpp
18  * @author  Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for installer task widget config
21  */
22
23 #include <map>
24 #include <sstream>
25 #include <string>
26 #include <sys/stat.h>
27 #include <dirent.h>
28
29 #include <dpl/errno_string.h>
30 #include <dpl/foreach.h>
31 #include <dpl/localization/w3c_file_localization.h>
32 #include <dpl/singleton_impl.h>
33 #include <dpl/utils/mime_type_utils.h>
34 #include <dpl/utils/wrt_global_settings.h>
35 #include <dpl/utils/wrt_utility.h>
36 #include <dpl/wrt-dao-ro/global_config.h>
37 #include <dpl/wrt-dao-rw/feature_dao.h>
38
39 #include <libiriwrapper.h>
40 #include <parser_runner.h>
41 #include <root_parser.h>
42
43 #include <widget_install/job_widget_install.h>
44 #include <widget_install/task_widget_config.h>
45 #include <widget_install/widget_install_context.h>
46 #include <widget_install/widget_install_errors.h>
47 #include <widget_parser.h>
48
49 namespace { // anonymous
50 const DPL::String BR = DPL::FromUTF8String("<br>");
51 const std::string WIDGET_NOT_COMPATIBLE = "This widget is "
52                                           "not compatible with WRT.<br><br>";
53 const std::string QUESTION = "Do you want to install it anyway?";
54
55 const char *const DEFAULT_LANGUAGE = "default";
56
57 const char *const WRT_WIDGET_CONFIG_FILE_NAME = "config.xml";
58
59 const std::string WINDGET_INSTALL_NETWORK_ACCESS = "network access";
60 }
61
62 namespace Jobs {
63 namespace WidgetInstall {
64 void InstallerTaskWidgetPopupData::PopupData::addWidgetInfo(
65     const DPL::String &info)
66 {
67     widgetInfo = info;
68 }
69
70 TaskWidgetConfig::TaskWidgetConfig(InstallerContext& installContext) :
71     DPL::TaskDecl<TaskWidgetConfig>(this),
72     m_installContext(installContext)
73 {
74     AddStep(&TaskWidgetConfig::StepProcessConfigurationFile);
75     AddStep(&TaskWidgetConfig::ReadLocaleFolders);
76     AddStep(&TaskWidgetConfig::ProcessLocalizedStartFiles);
77     AddStep(&TaskWidgetConfig::ProcessBackgroundPageFile);
78     AddStep(&TaskWidgetConfig::ProcessLocalizedIcons);
79     AddStep(&TaskWidgetConfig::ProcessWidgetInstalledPath);
80     AddStep(&TaskWidgetConfig::ProcessAppControlInfo);
81     AddStep(&TaskWidgetConfig::ProcessSecurityModel);
82     AddStep(&TaskWidgetConfig::StepVerifyFeatures);
83     AddStep(&TaskWidgetConfig::StepCheckMinVersionInfo);
84 }
85
86 void TaskWidgetConfig::StepProcessConfigurationFile()
87 {
88     Try
89     {
90         std::string path = m_installContext.locations->getConfigurationDir();
91         LogInfo("path: " << path);
92
93         processFile(path, m_installContext.widgetConfig);
94     }
95     Catch(Exception::ConfigParseFailed)
96     {
97         LogError("Parsing failed.");
98         ReThrow(Exceptions::WidgetConfigFileInvalid);
99     }
100
101     m_installContext.job->UpdateProgress(
102         InstallerContext::INSTALL_WIDGET_CONFIG1,
103         "Parse elements of configuration file and save them");
104 }
105
106 void TaskWidgetConfig::ReadLocaleFolders()
107 {
108     LogDebug("Reading locale");
109     //Adding default locale
110     m_localeFolders.insert(L"");
111
112     std::string localePath =
113         m_installContext.locations->getConfigurationDir() + "/locales";
114     DIR* localeDir = opendir(localePath.c_str());
115     if (!localeDir) {
116         LogDebug("No /locales directory in the widget package.");
117         return;
118     }
119
120
121
122     struct stat statStruct;
123     struct dirent dirent;
124     struct dirent *result;
125     int return_code;
126     errno = 0;
127     for (return_code = readdir_r(localeDir, &dirent, &result);
128             result != NULL && return_code == 0;
129             return_code = readdir_r(localeDir, &dirent, &result))
130     {
131         DPL::String dirName = DPL::FromUTF8String(dirent.d_name);
132         std::string absoluteDirName = localePath + "/";
133         absoluteDirName += dirent.d_name;
134
135         if (stat(absoluteDirName.c_str(), &statStruct) != 0) {
136             LogError("stat() failed with " << DPL::GetErrnoString());
137             continue;
138         }
139
140         if (S_ISDIR(statStruct.st_mode)) {
141             //Yes, we ignore current, parent & hidden directories
142             if (dirName[0] != L'.') {
143                 LogDebug("Adding locale directory \"" << dirName << "\"");
144                 m_localeFolders.insert(dirName);
145             }
146         }
147     }
148
149     if (return_code != 0 || errno != 0) {
150         LogError("readdir_r() failed with " << DPL::GetErrnoString());
151     }
152
153     if (-1 == TEMP_FAILURE_RETRY(closedir(localeDir))) {
154         LogError("Failed to close dir: " << localePath << " with error: "
155                                          << DPL::GetErrnoString());
156     }
157 }
158
159 void TaskWidgetConfig::ProcessLocalizedStartFiles()
160 {
161     typedef DPL::String S;
162     ProcessStartFile(
163         m_installContext.widgetConfig.configInfo.startFile,
164         m_installContext.widgetConfig.configInfo.
165             startFileContentType,
166         m_installContext.widgetConfig.configInfo.startFileEncoding,
167         true);
168     ProcessStartFile(S(L"index.htm"), S(L"text/html"));
169     ProcessStartFile(S(L"index.html"), S(L"text/html"));
170     ProcessStartFile(S(L"index.svg"), S(L"image/svg+xml"));
171     ProcessStartFile(S(L"index.xhtml"), S(L"application/xhtml+xml"));
172     ProcessStartFile(S(L"index.xht"), S(L"application/xhtml+xml"));
173     // TODO: we need better check if in current locales widget is valid
174     FOREACH(it, m_installContext.widgetConfig.localizationData.startFiles) {
175         if (it->propertiesForLocales.size() > 0) {
176             return;
177         }
178     }
179     ThrowMsg(Exceptions::WidgetConfigFileInvalid,
180              L"The Widget has no valid start file");
181 }
182
183 void TaskWidgetConfig::ProcessStartFile(const DPL::OptionalString& path,
184                                         const DPL::OptionalString& type,
185                                         const DPL::OptionalString& encoding,
186                                         bool typeForcedInConfig)
187 {
188     using namespace WrtDB;
189
190     if (!!path) {
191         WidgetRegisterInfo::LocalizedStartFile startFileData;
192         startFileData.path = *path;
193
194         FOREACH(i, m_localeFolders) {
195             DPL::String pathPrefix = *i;
196             if (!pathPrefix.empty()) {
197                 pathPrefix = L"locales/" + pathPrefix + L"/";
198             }
199
200             DPL::String relativePath = pathPrefix + *path;
201             DPL::String absolutePath = DPL::FromUTF8String(
202                     m_installContext.locations->getConfigurationDir()) + L"/" +
203                 relativePath;
204
205             // get property data from packaged app
206             if (WrtUtilFileExists(DPL::ToUTF8String(absolutePath))) {
207                 WidgetRegisterInfo::StartFileProperties startFileProperties;
208                 if (!!type) {
209                     startFileProperties.type = *type;
210                 } else {
211                     startFileProperties.type =
212                         MimeTypeUtils::identifyFileMimeType(absolutePath);
213                 }
214
215                 //proceed only if MIME type is supported
216                 if (MimeTypeUtils::isMimeTypeSupportedForStartFile(
217                         startFileProperties.type))
218                 {
219                     if (!!encoding) {
220                         startFileProperties.encoding = *encoding;
221                     } else {
222                         MimeTypeUtils::MimeAttributes attributes =
223                             MimeTypeUtils::getMimeAttributes(
224                                 startFileProperties.type);
225                         if (attributes.count(L"charset") > 0) {
226                             startFileProperties.encoding =
227                                 attributes[L"charset"];
228                         } else {
229                             startFileProperties.encoding = L"UTF-8";
230                         }
231                     }
232
233                     startFileData.propertiesForLocales[*i] =
234                         startFileProperties;
235                 } else {
236                     //9.1.16.5.content.8
237                     //(there seems to be no similar requirement in .6,
238                     //so let's throw only when mime type is
239                     // provided explcitly in config.xml)
240                     if (typeForcedInConfig) {
241                         ThrowMsg(Exceptions::WidgetConfigFileInvalid,
242                                  "Unsupported MIME type for start file.");
243                     }
244                 }
245             } else {
246                 // set property data for hosted start url
247                 // Hosted start url only support TIZEN WebApp
248                 if (m_installContext.widgetConfig.webAppType ==
249                     APP_TYPE_TIZENWEBAPP)
250                 {
251                     std::string startPath = DPL::ToUTF8String(
252                             startFileData.path);
253
254                     if (strstr(startPath.c_str(),
255                                "http") == startPath.c_str())
256                     {
257                         WidgetRegisterInfo::StartFileProperties
258                             startFileProperties;
259                         if (!!type) {
260                             startFileProperties.type = *type;
261                         }
262                         if (!!encoding) {
263                             startFileProperties.encoding = *encoding;
264                         }
265                         startFileData.propertiesForLocales[*i] =
266                             startFileProperties;
267                     }
268                 }
269             }
270         }
271
272         m_installContext.widgetConfig.localizationData.startFiles.push_back(
273             startFileData);
274     }
275 }
276
277 void TaskWidgetConfig::ProcessBackgroundPageFile()
278 {
279     if (!!m_installContext.widgetConfig.configInfo.backgroundPage) {
280         // check whether file exists
281         DPL::String backgroundPagePath = DPL::FromUTF8String(
282                 m_installContext.locations->getConfigurationDir()) + L"/" +
283             *m_installContext.widgetConfig.configInfo.backgroundPage;
284         //if no then cancel installation
285         if (!WrtUtilFileExists(DPL::ToUTF8String(backgroundPagePath))) {
286             ThrowMsg(Exceptions::WidgetConfigFileInvalid,
287                      L"Given background page file not found in archive");
288         }
289     }
290 }
291
292 void TaskWidgetConfig::ProcessLocalizedIcons()
293 {
294     using namespace WrtDB;
295     ProcessIcon(ConfigParserData::Icon(L"icon.svg"));
296     ProcessIcon(ConfigParserData::Icon(L"icon.ico"));
297     ProcessIcon(ConfigParserData::Icon(L"icon.png"));
298     ProcessIcon(ConfigParserData::Icon(L"icon.gif"));
299     ProcessIcon(ConfigParserData::Icon(L"icon.jpg"));
300
301     FOREACH(i, m_installContext.widgetConfig.configInfo.iconsList)
302     {
303         ProcessIcon(*i);
304     }
305 }
306
307 void TaskWidgetConfig::ProcessIcon(const WrtDB::ConfigParserData::Icon& icon)
308 {
309     LogInfo("enter");
310     bool isAnyIconValid = false;
311     //In case a default filename is passed as custom filename in config.xml, we
312     //need to keep a set of already processed filenames to avoid icon
313     // duplication
314     //in database.
315
316     using namespace WrtDB;
317
318     if (m_processedIconSet.count(icon.src) > 0) {
319         return;
320     }
321     m_processedIconSet.insert(icon.src);
322
323     LocaleSet localesAvailableForIcon;
324
325     FOREACH(i, m_localeFolders)
326     {
327         DPL::String pathPrefix = *i;
328         if (!pathPrefix.empty()) {
329             pathPrefix = L"locales/" + pathPrefix + L"/";
330         }
331
332         DPL::String relativePath = pathPrefix + icon.src;
333         DPL::String absolutePath = DPL::FromUTF8String(
334                 m_installContext.locations->getConfigurationDir()) + L"/" +
335             relativePath;
336
337         if (WrtUtilFileExists(DPL::ToUTF8String(absolutePath))) {
338             DPL::String type = MimeTypeUtils::identifyFileMimeType(absolutePath);
339
340             if (MimeTypeUtils::isMimeTypeSupportedForIcon(type)) {
341                 isAnyIconValid = true;
342                 localesAvailableForIcon.insert(*i);
343                 LogInfo("Icon absolutePath :" << absolutePath <<
344                         ", assigned locale :" << *i << ", type: " << type);
345             }
346         }
347     }
348
349     if (isAnyIconValid) {
350         WidgetRegisterInfo::LocalizedIcon localizedIcon(icon,
351                                                         localesAvailableForIcon);
352         m_installContext.widgetConfig.localizationData.icons.push_back(
353             localizedIcon);
354     }
355 }
356
357 void TaskWidgetConfig::ProcessWidgetInstalledPath()
358 {
359     LogDebug("ProcessWidgetInstalledPath");
360     m_installContext.widgetConfig.widgetInstalledPath =
361         DPL::FromUTF8String(
362             m_installContext.locations->getPackageInstallationDir());
363 }
364
365 void TaskWidgetConfig::ProcessAppControlInfo()
366 {
367     LogDebug("ProcessAppControlInfo");
368     using namespace WrtDB;
369
370     WrtDB::ConfigParserData::AppControlInfo::Disposition disposition =
371         WrtDB::ConfigParserData::AppControlInfo::Disposition::WINDOW;
372     FOREACH(it, m_installContext.widgetConfig.configInfo.settingsList) {
373         if (!strcmp(DPL::ToUTF8String(it->m_name).c_str(), "nodisplay") &&
374             !strcmp(DPL::ToUTF8String(it->m_value).c_str(), "true"))
375         {
376             disposition =
377                 WrtDB::ConfigParserData::AppControlInfo::Disposition::INLINE;
378         }
379     }
380
381     std::map<std::string, int> srcMap;
382     int index = 0;
383     // index = 0 is reserved for start file
384     FOREACH(startFileIt, m_installContext.widgetConfig.localizationData.startFiles)
385     {
386         if (!startFileIt->propertiesForLocales.empty()) {
387             std::string src = DPL::ToUTF8String(startFileIt->path);
388              if (srcMap.find(src) == srcMap.end()) {
389                 LogDebug("Insert [" << src << "," << index << "]");
390                 srcMap.insert(std::pair<std::string, int>(src, index++));
391             }
392         }
393     }
394
395     FOREACH(appControlIt, m_installContext.widgetConfig.configInfo.appControlList)
396     {
397         appControlIt->m_disposition = disposition;
398         std::string src = DPL::ToUTF8String(appControlIt->m_src);
399         LogDebug("src = [" << src << "]");
400         std::map<std::string, int>::iterator findIt = srcMap.find(src);
401         if (findIt == srcMap.end()) {
402             LogDebug("Insert [" << src << "," << index << "]");
403             srcMap.insert(std::pair<std::string, int>(src, index));
404             appControlIt->m_index = index++;
405         } else {
406             LogDebug("Exist  [" << src << "," << findIt->second << "]");
407             appControlIt->m_index = findIt->second;
408         }
409     }
410 }
411
412 void TaskWidgetConfig::ProcessSecurityModel()
413 {
414     bool isSecurityModelV1 = false;
415     bool isSecurityModelV2 = false;
416     WrtDB::ConfigParserData &data = m_installContext.widgetConfig.configInfo;
417
418     // WARP is V1
419     if (!data.accessInfoSet.empty()) {
420         isSecurityModelV1 = true;
421     }
422
423     // CSP is V2
424     if (!data.cspPolicy.IsNull() || !data.cspPolicyReportOnly.IsNull()) {
425         isSecurityModelV2 = true;
426     }
427
428     // allow-navigation is V2
429     if (!data.allowNavigationInfoList.empty()) {
430         isSecurityModelV2 = true;
431     }
432
433     if (isSecurityModelV1 && isSecurityModelV2) {
434         LogError("Security model is conflict");
435         ThrowMsg(Exceptions::NotAllowed, "Security model is conflict");
436     } else if (isSecurityModelV1) {
437         data.securityModelVersion =
438             WrtDB::ConfigParserData::SecurityModelVersion::SECURITY_MODEL_V1;
439     } else if (isSecurityModelV2) {
440         data.securityModelVersion =
441             WrtDB::ConfigParserData::SecurityModelVersion::SECURITY_MODEL_V2;
442     } else {
443         data.securityModelVersion =
444             WrtDB::ConfigParserData::SecurityModelVersion::SECURITY_MODEL_V1;
445     }
446
447     m_installContext.job->UpdateProgress(
448         InstallerContext::INSTALL_WIDGET_CONFIG2,
449         "Finished process security model");
450 }
451
452 void TaskWidgetConfig::StepCheckMinVersionInfo()
453 {
454     if (!isMinVersionCompatible(
455             m_installContext.widgetConfig.webAppType.appType,
456             m_installContext.widgetConfig.minVersion))
457     {
458         LogError(
459             "Platform version lower than required -> cancelling installation");
460         ThrowMsg(Exceptions::NotAllowed,
461                  "Platform version does not meet requirements");
462     }
463
464     m_installContext.job->UpdateProgress(
465         InstallerContext::INSTALL_WIDGET_CONFIG2,
466         "Check MinVersion Finished");
467 }
468
469 void TaskWidgetConfig::StepVerifyFeatures()
470 {
471     using namespace WrtDB;
472     ConfigParserData &data = m_installContext.widgetConfig.configInfo;
473     ConfigParserData::FeaturesList list = data.featuresList;
474     ConfigParserData::FeaturesList newList;
475
476     //in case of tests, this variable is unused
477     std::string featureInfo;
478     FOREACH(it, list)
479     {
480         // check feature vender for permission
481         // WAC, TIZEN WebApp cannot use other feature
482
483         if (!isFeatureAllowed(m_installContext.widgetConfig.webAppType.appType,
484                               it->name))
485         {
486             LogInfo("This application type not allowed to use this feature");
487             ThrowMsg(
488                 Exceptions::WidgetConfigFileInvalid,
489                 "This app type [" <<
490                 m_installContext.widgetConfig.webAppType.getApptypeToString()
491                                   <<
492                 "] cannot be allowed to use [" <<
493                 DPL::ToUTF8String(it->name) + "] feature");
494         } else {
495             newList.insert(*it);
496             featureInfo += DPL::ToUTF8String(it->name);
497             featureInfo += DPL::ToUTF8String(BR);
498         }
499     }
500     if (!data.accessInfoSet.empty()) {
501         featureInfo += WINDGET_INSTALL_NETWORK_ACCESS;
502         featureInfo += DPL::ToUTF8String(BR);
503     }
504     data.featuresList = newList;
505     if (!featureInfo.empty()) {
506         m_popupData.addWidgetInfo(DPL::FromUTF8String(featureInfo));
507     }
508
509     m_installContext.job->UpdateProgress(
510         InstallerContext::INSTALL_WIDGET_CONFIG2,
511         "Widget Config step2 Finished");
512 }
513
514 bool TaskWidgetConfig::isFeatureAllowed(WrtDB::AppType appType,
515                                         DPL::String featureName)
516 {
517     using namespace WrtDB;
518     LogInfo("AppType = [" <<
519             WidgetType(appType).getApptypeToString() << "]");
520     LogInfo("FetureName = [" << featureName << "]");
521
522     AppType featureType = APP_TYPE_UNKNOWN;
523     std::string featureStr = DPL::ToUTF8String(featureName);
524     const char* feature = featureStr.c_str();
525
526     // check prefix of  feature name
527     if (strstr(feature, PluginsPrefix::TIZENPluginsPrefix) == feature) {
528         // Tizen WebApp feature
529         featureType = APP_TYPE_TIZENWEBAPP;
530     } else if (strstr(feature, PluginsPrefix::WACPluginsPrefix) == feature) {
531         // WAC 2.0 feature
532         featureType = APP_TYPE_WAC20;
533     } else if (strstr(feature, PluginsPrefix::W3CPluginsPrefix) == feature) {
534         // W3C standard feature
535         // Both WAC and TIZEN WebApp are possible to use W3C plugins
536         return true;
537     } else {
538         // unknown feature
539         // unknown feature will be checked next step
540         return true;
541     }
542
543     if (appType == featureType) {
544         return true;
545     }
546     return false;
547 }
548
549 bool TaskWidgetConfig::parseVersionString(const std::string &version,
550                                           long &majorVersion,
551                                           long &minorVersion,
552                                           long &microVersion) const
553 {
554     std::istringstream inputString(version);
555     inputString >> majorVersion;
556     if (inputString.bad() || inputString.fail()) {
557         LogWarning("Invalid minVersion format.");
558         return false;
559     }
560     inputString.get(); // skip period
561     inputString >> minorVersion;
562     if (inputString.bad() || inputString.fail()) {
563         LogWarning("Invalid minVersion format");
564         return false;
565     } else {
566         inputString.get(); // skip period
567         if (inputString.bad() || inputString.fail()) {
568             inputString >> microVersion;
569         }
570     }
571     return true;
572 }
573
574 bool TaskWidgetConfig::isMinVersionCompatible(
575     WrtDB::AppType appType,
576     const DPL::OptionalString &
577     widgetVersion) const
578 {
579     if (widgetVersion.IsNull() || (*widgetVersion).empty()) {
580         if (appType == WrtDB::AppType::APP_TYPE_TIZENWEBAPP) {
581             return false;
582         } else {
583             LogWarning("minVersion attribute is empty. WRT assumes platform "
584                     "supports this widget.");
585             return true;
586         }
587     }
588
589     //Parse widget version
590     long majorWidget = 0, minorWidget = 0, microWidget = 0;
591     if (!parseVersionString(DPL::ToUTF8String(*widgetVersion), majorWidget,
592                             minorWidget, microWidget))
593     {
594         LogWarning("Invalid format of widget version string.");
595         return false;
596     }
597
598     //Parse supported version
599     long majorSupported = 0, minorSupported = 0, microSupported = 0;
600     std::string version;
601     if (appType == WrtDB::AppType::APP_TYPE_TIZENWEBAPP) {
602         version = WrtDB::GlobalConfig::GetTizenVersion();
603     } else if (appType == WrtDB::AppType::APP_TYPE_WAC20) {
604         version = WrtDB::GlobalConfig::GetWACVersion();
605     } else {
606         LogWarning("Invaild AppType");
607         return false;
608     }
609
610     if (!parseVersionString(version,
611                             majorSupported, minorSupported, microSupported))
612     {
613         LogWarning("Invalid format of platform version string.");
614         return true;
615     }
616
617     if (majorWidget > majorSupported ||
618         (majorWidget == majorSupported && minorWidget > minorSupported) ||
619         (majorWidget == majorSupported && minorWidget == minorSupported
620          && microWidget > microSupported))
621     {
622         LogInfo("Platform doesn't support this widget.");
623         return false;
624     }
625     return true;
626 }
627
628 bool TaskWidgetConfig::isTizenWebApp() const
629 {
630     bool ret = FALSE;
631     if (m_installContext.widgetConfig.webAppType.appType
632         == WrtDB::AppType::APP_TYPE_TIZENWEBAPP)
633     {
634         ret = TRUE;
635     }
636
637     return ret;
638 }
639
640 bool TaskWidgetConfig::parseConfigurationFileBrowser(
641     WrtDB::ConfigParserData& configInfo,
642     const std::string& _currentPath)
643 {
644     ParserRunner parser;
645     Try
646     {
647         parser.Parse(_currentPath, ElementParserPtr(new
648                                                     RootParser<
649                                                         WidgetParser>(
650                                                         configInfo,
651                                                         DPL::FromUTF32String(
652                                                             L"widget"))));
653     }
654     Catch(ElementParser::Exception::Base)
655     {
656         LogError("Invalid widget configuration file!");
657         return false;
658     }
659     return true;
660 }
661
662 bool TaskWidgetConfig::parseConfigurationFileWidget(
663     WrtDB::ConfigParserData& configInfo,
664     const std::string& _currentPath)
665 {
666     std::string configFilePath;
667     WrtUtilJoinPaths(configFilePath, _currentPath, WRT_WIDGET_CONFIG_FILE_NAME);
668     if (!WrtUtilFileExists(configFilePath))
669     {
670         LogError("Archive does not contain configuration file");
671         return false;
672     }
673
674     LogDebug("Configuration file: " << configFilePath);
675
676     Try
677     {
678         ParserRunner parser;
679         parser.Parse(configFilePath,
680                      ElementParserPtr(new RootParser<WidgetParser>(
681                                           configInfo,
682                                           DPL::FromUTF32String(L"widget"))));
683         return true;
684     }
685     Catch (ElementParser::Exception::Base)
686     {
687         LogError("Invalid configuration file!");
688         return false;
689     }
690 }
691
692 bool TaskWidgetConfig::locateAndParseConfigurationFile(
693     const std::string& _currentPath,
694     WrtDB::WidgetRegisterInfo& pWidgetConfigInfo,
695     const std::string& baseFolder)
696 {
697     using namespace WrtDB;
698
699     ConfigParserData& configInfo = pWidgetConfigInfo.configInfo;
700
701     // check if this installation from browser, or not.
702     size_t pos = _currentPath.rfind("/");
703     std::ostringstream infoPath;
704     infoPath << _currentPath.substr(pos + 1);
705
706     if (infoPath.str() != WRT_WIDGET_CONFIG_FILE_NAME) {
707         if (_currentPath.empty() || baseFolder.empty()) {
708             return false;
709         }
710         // in case of general installation using wgt archive
711         if (!parseConfigurationFileWidget(configInfo, _currentPath))
712         {
713             return false;
714         }
715     } else {
716         // in case of browser installation
717         if (!parseConfigurationFileBrowser(configInfo, _currentPath))
718         {
719             return false;
720         }
721     }
722
723     if (!fillWidgetConfig(pWidgetConfigInfo, configInfo)) {
724         return false;
725     }
726     return true;
727 }
728
729 bool TaskWidgetConfig::fillWidgetConfig(
730     WrtDB::WidgetRegisterInfo& pWidgetConfigInfo,
731     WrtDB::ConfigParserData& configInfo)
732 {
733     if (!!configInfo.widget_id) {
734         if (!pWidgetConfigInfo.guid) {
735             pWidgetConfigInfo.guid = configInfo.widget_id;
736         } else {
737             if (pWidgetConfigInfo.guid != configInfo.widget_id) {
738                 LogError("Invalid archive");
739                 return false;
740             }
741         }
742     }
743     if (!!configInfo.tizenAppId) {
744         if (DPL::ToUTF8String(pWidgetConfigInfo.tzAppid).compare(
745                 DPL::ToUTF8String(*configInfo.tizenAppId)) < 0)
746         {
747             LogError("Invalid archive - Tizen App ID not same error");
748             return false;
749         }
750     }
751     if (!!configInfo.tizenPkgId) {
752         if (pWidgetConfigInfo.tzPkgid != *configInfo.tizenPkgId) {
753             LogError("Invalid archive - Tizen Pkg ID not same error");
754             return false;
755         }
756     }
757     if (!!configInfo.version) {
758         if (!pWidgetConfigInfo.version) {
759             pWidgetConfigInfo.version = configInfo.version;
760         } else {
761             if (pWidgetConfigInfo.version != configInfo.version) {
762                 LogError("Invalid archive");
763                 return false;
764             }
765         }
766     }
767     if (!!configInfo.minVersionRequired) {
768         pWidgetConfigInfo.minVersion = configInfo.minVersionRequired;
769     } else if (!!configInfo.tizenMinVersionRequired) {
770         pWidgetConfigInfo.minVersion = configInfo.tizenMinVersionRequired;
771     }
772     return true;
773 }
774
775 void TaskWidgetConfig::processFile(
776     const std::string& path,
777     WrtDB::WidgetRegisterInfo &
778     widgetConfiguration)
779 {
780     if (!locateAndParseConfigurationFile(path, widgetConfiguration,
781                                          DEFAULT_LANGUAGE))
782     {
783         LogWarning("Widget archive: Failed while parsing config file");
784         ThrowMsg(Exception::ConfigParseFailed, path);
785     }
786 }
787 } //namespace WidgetInstall
788 } //namespace Jobs