2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file task_widget_config.cpp
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief Implementation file for installer task widget config
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>
38 #include <libiriwrapper.h>
39 #include <parser_runner.h>
40 #include <root_parser.h>
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>
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?";
54 const char *const DEFAULT_LANGUAGE = "default";
56 const char *const WRT_WIDGET_CONFIG_FILE_NAME = "config.xml";
58 const std::string WINDGET_INSTALL_NETWORK_ACCESS = "network access";
62 namespace WidgetInstall {
63 void InstallerTaskWidgetPopupData::PopupData::addWidgetInfo(
64 const DPL::String &info)
69 TaskWidgetConfig::TaskWidgetConfig(InstallerContext& installContext) :
70 DPL::TaskDecl<TaskWidgetConfig>(this),
71 m_installContext(installContext)
73 AddStep(&TaskWidgetConfig::StepProcessConfigurationFile);
74 AddStep(&TaskWidgetConfig::ReadLocaleFolders);
75 AddStep(&TaskWidgetConfig::ProcessLocalizedStartFiles);
76 AddStep(&TaskWidgetConfig::ProcessBackgroundPageFile);
77 AddStep(&TaskWidgetConfig::ProcessLocalizedIcons);
78 AddStep(&TaskWidgetConfig::ProcessWidgetInstalledPath);
79 AddStep(&TaskWidgetConfig::StepVerifyFeatures);
80 AddStep(&TaskWidgetConfig::StepCheckMinVersionInfo);
83 void TaskWidgetConfig::StepProcessConfigurationFile()
87 std::string path = m_installContext.locations->getConfigurationDir();
88 LogInfo("path: " << path);
90 processFile(path, m_installContext.widgetConfig);
92 Catch(Exception::ConfigParseFailed)
94 LogError("Parsing failed.");
95 ReThrow(Exceptions::WidgetConfigFileInvalid);
98 m_installContext.job->UpdateProgress(
99 InstallerContext::INSTALL_WIDGET_CONFIG1,
100 "Parse elements of configuration file and save them");
103 void TaskWidgetConfig::ReadLocaleFolders()
105 LogDebug("Reading locale");
106 //Adding default locale
107 m_localeFolders.insert(L"");
109 std::string localePath =
110 m_installContext.locations->getConfigurationDir() + "/locales";
111 DIR* localeDir = opendir(localePath.c_str());
113 LogDebug("No /locales directory in the widget package.");
119 struct stat statStruct;
120 struct dirent dirent;
121 struct dirent *result;
124 for (return_code = readdir_r(localeDir, &dirent, &result);
125 result != NULL && return_code == 0;
126 return_code = readdir_r(localeDir, &dirent, &result))
128 DPL::String dirName = DPL::FromUTF8String(dirent.d_name);
129 std::string absoluteDirName = localePath + "/";
130 absoluteDirName += dirent.d_name;
132 if (stat(absoluteDirName.c_str(), &statStruct) != 0) {
133 LogError("stat() failed with " << DPL::GetErrnoString());
137 if (S_ISDIR(statStruct.st_mode)) {
138 //Yes, we ignore current, parent & hidden directories
139 if (dirName[0] != L'.') {
140 LogDebug("Adding locale directory \"" << dirName << "\"");
141 m_localeFolders.insert(dirName);
146 if (return_code != 0 || errno != 0) {
147 LogError("readdir_r() failed with " << DPL::GetErrnoString());
150 if (-1 == TEMP_FAILURE_RETRY(closedir(localeDir))) {
151 LogError("Failed to close dir: " << localePath << " with error: "
152 << DPL::GetErrnoString());
156 void TaskWidgetConfig::ProcessLocalizedStartFiles()
158 typedef DPL::String S;
160 m_installContext.widgetConfig.configInfo.startFile,
161 m_installContext.widgetConfig.configInfo.
162 startFileContentType,
163 m_installContext.widgetConfig.configInfo.startFileEncoding,
165 ProcessStartFile(S(L"index.htm"), S(L"text/html"));
166 ProcessStartFile(S(L"index.html"), S(L"text/html"));
167 ProcessStartFile(S(L"index.svg"), S(L"image/svg+xml"));
168 ProcessStartFile(S(L"index.xhtml"), S(L"application/xhtml+xml"));
169 ProcessStartFile(S(L"index.xht"), S(L"application/xhtml+xml"));
170 // TODO: (l.wrzosek) we need better check if in current locales widget is
172 FOREACH(it, m_installContext.widgetConfig.localizationData.startFiles) {
173 if (it->propertiesForLocales.size() > 0) {
177 ThrowMsg(Exceptions::WidgetConfigFileInvalid,
178 L"The Widget has no valid start file");
181 void TaskWidgetConfig::ProcessStartFile(const DPL::OptionalString& path,
182 const DPL::OptionalString& type,
183 const DPL::OptionalString& encoding,
184 bool typeForcedInConfig)
186 using namespace WrtDB;
189 WidgetRegisterInfo::LocalizedStartFile startFileData;
190 startFileData.path = *path;
192 FOREACH(i, m_localeFolders) {
193 DPL::String pathPrefix = *i;
194 if (!pathPrefix.empty()) {
195 pathPrefix = L"locales/" + pathPrefix + L"/";
198 DPL::String relativePath = pathPrefix + *path;
199 DPL::String absolutePath = DPL::FromUTF8String(
200 m_installContext.locations->getConfigurationDir()) + L"/" +
203 // get property data from packaged app
204 if (WrtUtilFileExists(DPL::ToUTF8String(absolutePath))) {
205 WidgetRegisterInfo::StartFileProperties startFileProperties;
207 startFileProperties.type = *type;
209 startFileProperties.type =
210 MimeTypeUtils::identifyFileMimeType(absolutePath);
213 //proceed only if MIME type is supported
214 if (MimeTypeUtils::isMimeTypeSupportedForStartFile(
215 startFileProperties.type))
218 startFileProperties.encoding = *encoding;
220 MimeTypeUtils::MimeAttributes attributes =
221 MimeTypeUtils::getMimeAttributes(
222 startFileProperties.type);
223 if (attributes.count(L"charset") > 0) {
224 startFileProperties.encoding =
225 attributes[L"charset"];
227 startFileProperties.encoding = L"UTF-8";
231 startFileData.propertiesForLocales[*i] =
235 //(there seems to be no similar requirement in .6,
236 //so let's throw only when mime type is
237 // provided explcitly in config.xml)
238 if (typeForcedInConfig) {
239 ThrowMsg(Exceptions::WidgetConfigFileInvalid,
240 "Unsupported MIME type for start file.");
244 // set property data for hosted start url
245 // Hosted start url only support TIZEN WebApp
246 if (m_installContext.widgetConfig.webAppType ==
247 APP_TYPE_TIZENWEBAPP)
249 std::string startPath = DPL::ToUTF8String(
252 if (strstr(startPath.c_str(),
253 "http") == startPath.c_str())
255 WidgetRegisterInfo::StartFileProperties
258 startFileProperties.type = *type;
261 startFileProperties.encoding = *encoding;
263 startFileData.propertiesForLocales[*i] =
270 m_installContext.widgetConfig.localizationData.startFiles.push_back(
275 void TaskWidgetConfig::ProcessBackgroundPageFile()
277 if (!!m_installContext.widgetConfig.configInfo.backgroundPage) {
278 // check whether file exists
279 DPL::String backgroundPagePath = DPL::FromUTF8String(
280 m_installContext.locations->getConfigurationDir()) + L"/" +
281 *m_installContext.widgetConfig.configInfo.backgroundPage;
282 //if no then cancel installation
283 if (!WrtUtilFileExists(DPL::ToUTF8String(backgroundPagePath))) {
284 ThrowMsg(Exceptions::WidgetConfigFileInvalid,
285 L"Given background page file not found in archive");
290 void TaskWidgetConfig::ProcessLocalizedIcons()
292 using namespace WrtDB;
293 ProcessIcon(ConfigParserData::Icon(L"icon.svg"));
294 ProcessIcon(ConfigParserData::Icon(L"icon.ico"));
295 ProcessIcon(ConfigParserData::Icon(L"icon.png"));
296 ProcessIcon(ConfigParserData::Icon(L"icon.gif"));
297 ProcessIcon(ConfigParserData::Icon(L"icon.jpg"));
299 FOREACH(i, m_installContext.widgetConfig.configInfo.iconsList)
305 void TaskWidgetConfig::ProcessIcon(const WrtDB::ConfigParserData::Icon& icon)
308 bool isAnyIconValid = false;
309 //In case a default filename is passed as custom filename in config.xml, we
310 //need to keep a set of already processed filenames to avoid icon
314 using namespace WrtDB;
316 if (m_processedIconSet.count(icon.src) > 0) {
319 m_processedIconSet.insert(icon.src);
321 LocaleSet localesAvailableForIcon;
323 FOREACH(i, m_localeFolders)
325 DPL::String pathPrefix = *i;
326 if (!pathPrefix.empty()) {
327 pathPrefix = L"locales/" + pathPrefix + L"/";
330 DPL::String relativePath = pathPrefix + icon.src;
331 DPL::String absolutePath = DPL::FromUTF8String(
332 m_installContext.locations->getConfigurationDir()) + L"/" +
335 if (WrtUtilFileExists(DPL::ToUTF8String(absolutePath))) {
336 DPL::String type = MimeTypeUtils::identifyFileMimeType(absolutePath);
338 if (MimeTypeUtils::isMimeTypeSupportedForIcon(type)) {
339 isAnyIconValid = true;
340 localesAvailableForIcon.insert(*i);
341 LogInfo("Icon absolutePath :" << absolutePath <<
342 ", assigned locale :" << *i << ", type: " << type);
347 if (isAnyIconValid) {
348 WidgetRegisterInfo::LocalizedIcon localizedIcon(icon,
349 localesAvailableForIcon);
350 m_installContext.widgetConfig.localizationData.icons.push_back(
355 void TaskWidgetConfig::ProcessWidgetInstalledPath()
357 LogDebug("ProcessWidgetInstalledPath");
358 m_installContext.widgetConfig.widgetInstalledPath =
360 m_installContext.locations->getPackageInstallationDir());
363 void TaskWidgetConfig::StepCheckMinVersionInfo()
365 if (!isMinVersionCompatible(
366 m_installContext.widgetConfig.webAppType.appType,
367 m_installContext.widgetConfig.minVersion))
370 "Platform version lower than required -> cancelling installation");
371 ThrowMsg(Exceptions::NotAllowed,
372 "Platform version does not meet requirements");
375 m_installContext.job->UpdateProgress(
376 InstallerContext::INSTALL_WIDGET_CONFIG2,
377 "Check MinVersion Finished");
380 void TaskWidgetConfig::StepVerifyFeatures()
382 using namespace WrtDB;
383 ConfigParserData &data = m_installContext.widgetConfig.configInfo;
384 ConfigParserData::FeaturesList list = data.featuresList;
385 ConfigParserData::FeaturesList newList;
387 //in case of tests, this variable is unused
388 std::string featureInfo;
391 // check feature vender for permission
392 // WAC, TIZEN WebApp cannot use other feature
394 if (!isFeatureAllowed(m_installContext.widgetConfig.webAppType.appType,
397 LogInfo("This application type not allowed to use this feature");
399 Exceptions::WidgetConfigFileInvalid,
401 m_installContext.widgetConfig.webAppType.getApptypeToString()
403 "] cannot be allowed to use [" <<
404 DPL::ToUTF8String(it->name) + "] feature");
407 featureInfo += DPL::ToUTF8String(it->name);
408 featureInfo += DPL::ToUTF8String(BR);
411 if (!data.accessInfoSet.empty()) {
412 featureInfo += WINDGET_INSTALL_NETWORK_ACCESS;
413 featureInfo += DPL::ToUTF8String(BR);
415 data.featuresList = newList;
416 if (!featureInfo.empty()) {
417 m_popupData.addWidgetInfo(DPL::FromUTF8String(featureInfo));
420 m_installContext.job->UpdateProgress(
421 InstallerContext::INSTALL_WIDGET_CONFIG2,
422 "Widget Config step2 Finished");
425 bool TaskWidgetConfig::isFeatureAllowed(WrtDB::AppType appType,
426 DPL::String featureName)
428 using namespace WrtDB;
429 LogInfo("AppType = [" <<
430 WidgetType(appType).getApptypeToString() << "]");
431 LogInfo("FetureName = [" << featureName << "]");
433 AppType featureType = APP_TYPE_UNKNOWN;
434 std::string featureStr = DPL::ToUTF8String(featureName);
435 const char* feature = featureStr.c_str();
437 // check prefix of feature name
438 if (strstr(feature, PluginsPrefix::TIZENPluginsPrefix) == feature) {
439 // Tizen WebApp feature
440 featureType = APP_TYPE_TIZENWEBAPP;
441 } else if (strstr(feature, PluginsPrefix::WACPluginsPrefix) == feature) {
443 featureType = APP_TYPE_WAC20;
444 } else if (strstr(feature, PluginsPrefix::W3CPluginsPrefix) == feature) {
445 // W3C standard feature
446 // Both WAC and TIZEN WebApp are possible to use W3C plugins
450 // unknown feature will be checked next step
454 if (appType == featureType) {
460 bool TaskWidgetConfig::parseVersionString(const std::string &version,
463 long µVersion) const
465 std::istringstream inputString(version);
466 inputString >> majorVersion;
467 if (inputString.bad() || inputString.fail()) {
468 LogWarning("Invalid minVersion format.");
471 inputString.get(); // skip period
472 inputString >> minorVersion;
473 if (inputString.bad() || inputString.fail()) {
474 LogWarning("Invalid minVersion format");
477 inputString.get(); // skip period
478 if (inputString.bad() || inputString.fail()) {
479 inputString >> microVersion;
485 bool TaskWidgetConfig::isMinVersionCompatible(
486 WrtDB::AppType appType,
487 const DPL::OptionalString &
490 if (widgetVersion.IsNull() || (*widgetVersion).empty()) {
491 if (appType == WrtDB::AppType::APP_TYPE_TIZENWEBAPP) {
494 LogWarning("minVersion attribute is empty. WRT assumes platform "
495 "supports this widget.");
500 //Parse widget version
501 long majorWidget = 0, minorWidget = 0, microWidget = 0;
502 if (!parseVersionString(DPL::ToUTF8String(*widgetVersion), majorWidget,
503 minorWidget, microWidget))
505 LogWarning("Invalid format of widget version string.");
509 //Parse supported version
510 long majorSupported = 0, minorSupported = 0, microSupported = 0;
512 if (appType == WrtDB::AppType::APP_TYPE_TIZENWEBAPP) {
513 version = WrtDB::GlobalConfig::GetTizenVersion();
514 } else if (appType == WrtDB::AppType::APP_TYPE_WAC20) {
515 version = WrtDB::GlobalConfig::GetWACVersion();
517 LogWarning("Invaild AppType");
521 if (!parseVersionString(version,
522 majorSupported, minorSupported, microSupported))
524 LogWarning("Invalid format of platform version string.");
528 if (majorWidget > majorSupported ||
529 (majorWidget == majorSupported && minorWidget > minorSupported) ||
530 (majorWidget == majorSupported && minorWidget == minorSupported
531 && microWidget > microSupported))
533 LogInfo("Platform doesn't support this widget.");
539 bool TaskWidgetConfig::isTizenWebApp() const
542 if (m_installContext.widgetConfig.webAppType.appType
543 == WrtDB::AppType::APP_TYPE_TIZENWEBAPP)
551 bool TaskWidgetConfig::parseConfigurationFileBrowser(
552 WrtDB::ConfigParserData& configInfo,
553 const std::string& _currentPath)
558 parser.Parse(_currentPath, ElementParserPtr(new
562 DPL::FromUTF32String(
565 Catch(ElementParser::Exception::Base)
567 LogError("Invalid widget configuration file!");
573 bool TaskWidgetConfig::parseConfigurationFileWidget(
574 WrtDB::ConfigParserData& configInfo,
575 const std::string& _currentPath)
577 std::string configFilePath;
578 WrtUtilJoinPaths(configFilePath, _currentPath, WRT_WIDGET_CONFIG_FILE_NAME);
579 if (!WrtUtilFileExists(configFilePath))
581 LogError("Archive does not contain configuration file");
585 LogDebug("Configuration file: " << configFilePath);
590 parser.Parse(configFilePath,
591 ElementParserPtr(new RootParser<WidgetParser>(
593 DPL::FromUTF32String(L"widget"))));
596 Catch (ElementParser::Exception::Base)
598 LogError("Invalid configuration file!");
603 bool TaskWidgetConfig::locateAndParseConfigurationFile(
604 const std::string& _currentPath,
605 WrtDB::WidgetRegisterInfo& pWidgetConfigInfo,
606 const std::string& baseFolder)
608 using namespace WrtDB;
610 ConfigParserData& configInfo = pWidgetConfigInfo.configInfo;
612 // check if this installation from browser, or not.
613 size_t pos = _currentPath.rfind("/");
614 std::ostringstream infoPath;
615 infoPath << _currentPath.substr(pos + 1);
617 if (infoPath.str() != WRT_WIDGET_CONFIG_FILE_NAME) {
618 if (_currentPath.empty() || baseFolder.empty()) {
621 // in case of general installation using wgt archive
622 if (!parseConfigurationFileWidget(configInfo, _currentPath))
627 // in case of browser installation
628 if (!parseConfigurationFileBrowser(configInfo, _currentPath))
634 if (!fillWidgetConfig(pWidgetConfigInfo, configInfo)) {
640 bool TaskWidgetConfig::fillWidgetConfig(
641 WrtDB::WidgetRegisterInfo& pWidgetConfigInfo,
642 WrtDB::ConfigParserData& configInfo)
644 if (!!configInfo.widget_id) {
645 if (!pWidgetConfigInfo.guid) {
646 pWidgetConfigInfo.guid = configInfo.widget_id;
648 if (pWidgetConfigInfo.guid != configInfo.widget_id) {
649 LogError("Invalid archive");
654 if (!!configInfo.tizenAppId) {
655 if (DPL::ToUTF8String(pWidgetConfigInfo.tzAppid).compare(
656 DPL::ToUTF8String(*configInfo.tizenAppId)) < 0)
658 LogError("Invalid archive - Tizen App ID not same error");
662 if (!!configInfo.tizenPkgId) {
663 if (pWidgetConfigInfo.tzPkgid != *configInfo.tizenPkgId) {
664 LogError("Invalid archive - Tizen Pkg ID not same error");
668 if (!!configInfo.version) {
669 if (!pWidgetConfigInfo.version) {
670 pWidgetConfigInfo.version = configInfo.version;
672 if (pWidgetConfigInfo.version != configInfo.version) {
673 LogError("Invalid archive");
678 if (!!configInfo.minVersionRequired) {
679 pWidgetConfigInfo.minVersion = configInfo.minVersionRequired;
680 } else if (!!configInfo.tizenMinVersionRequired) {
681 pWidgetConfigInfo.minVersion = configInfo.tizenMinVersionRequired;
686 void TaskWidgetConfig::processFile(
687 const std::string& path,
688 WrtDB::WidgetRegisterInfo &
691 if (!locateAndParseConfigurationFile(path, widgetConfiguration,
694 LogWarning("Widget archive: Failed while parsing config file");
695 ThrowMsg(Exception::ConfigParseFailed, path);
698 } //namespace WidgetInstall