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