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 WidgetConfigurationManager.cpp
18 * @author Piotr Fatyga (p.fatyga@samsung.com)
19 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
23 #include "WidgetConfigurationManager.h"
25 #include <dpl/wrt-dao-ro/global_config.h>
26 #include "root_parser.h"
27 #include "parser_runner.h"
28 #include "widget_parser.h"
29 #include <wrt_error.h>
30 #include <dpl/utils/mime_type_utils.h>
31 #include <dpl/localization/w3c_file_localization.h>
32 #include <dpl/utils/wrt_utility.h>
33 #include <dpl/singleton_impl.h>
34 IMPLEMENT_SINGLETON(WidgetConfigurationManager)
36 //TODO Rewrite this as steps/tasks
37 namespace // anonymous
39 const char *const DEFAULT_LANGUAGE = "default";
40 const size_t MAX_WIDGET_PATH_SIZE = 1024;
42 //#define WRT_WIDGET_DEFAULT_ICON_WIDTH 80
43 //#define WRT_WIDGET_DEFAULT_ICON_HEIGHT 80
45 //#define WRT_WIDGET_CONFIG_BASE_LOCALE "locales"
46 const char *const WRT_WIDGET_CONFIG_FILE_NAME = "config.xml";
49 bool WidgetConfigurationManager::locateAndParseConfigurationFile(
50 const std::string& _currentPath,
51 WrtDB::WidgetRegisterInfo& pWidgetConfigInfo,
52 const std::string& baseFolder,
55 using namespace WrtDB;
61 ConfigParserData& configInfo = pWidgetConfigInfo.configInfo;
63 // check if this installation from browser, or not.
64 size_t pos = _currentPath.rfind("/");
65 std::ostringstream infoPath;
66 infoPath << _currentPath.substr(pos+1);
69 std::string language = "";
71 if (infoPath.str() != WRT_WIDGET_CONFIG_FILE_NAME) {
72 // in case of general installation using wgt archive
73 //TODO: use DPL::String in the caller to this function too.
74 DPL::String currentPath = DPL::FromUTF8String(_currentPath);
76 if (currentPath.empty() || baseFolder.empty()) {
77 *pErrCode = WRT_ERR_INVALID_ARG;
81 //TODO: rewrite this madness
82 char cfgAbsPath[MAX_WIDGET_PATH_SIZE + 1] = { 0 };
84 struct dirent* ptr = NULL;
86 dir = opendir(_currentPath.c_str());
88 *pErrCode = WRT_ERR_UNKNOWN;
92 //TODO why don't we use fopen here
93 bool has_config_xml = false;
95 while ((ptr = readdir(dir)) != NULL) { //Find configuration file, based on its name
96 if (ptr->d_type == DT_REG) {
97 if (!strcmp(ptr->d_name, WRT_WIDGET_CONFIG_FILE_NAME)) {
98 _WrtUtilSetAbsolutePath(cfgAbsPath,
99 _currentPath.c_str(), ptr->d_name);
100 //Parse widget configuration file
101 LogDebug("Found config: " << cfgAbsPath);
105 parser.Parse(cfgAbsPath, ElementParserPtr(new
114 Catch(ElementParser::Exception::Base)
116 LogDebug("Invalid widget configuration file!");
117 // _rethrown_exception.Dump();
118 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
124 // WidgetConfigurationParser & parser = WidgetConfigurationParserSingleton::Instance();
125 // if (!parser.parseConfigurationFile(cfgAbsPath, configInfo, baseFolder.c_str(), pWidgetConfigInfo.signature_type)) {
126 // LogDebug("Invalid widget configuration file!");
127 // *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
132 has_config_xml = true;
139 //We must have config.xml so leaveing if we doesn't
140 if (!has_config_xml) {
141 LogDebug("Invalid archive");
142 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
146 // in case of browser installation
149 parser.Parse(_currentPath, ElementParserPtr(new
153 DPL::FromUTF32String(
156 Catch(ElementParser::Exception::Base)
158 LogDebug("Invalid widget configuration file!");
159 // _rethrown_exception.Dump();
160 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
167 if (!_WrtUtilStringToLower(baseFolder.c_str(), &tmp_language)) {
168 *pErrCode = WRT_ERR_UNKNOWN;
173 *pErrCode = WRT_ERR_UNKNOWN;
176 language = tmp_language;
179 if (!!configInfo.widget_id) {
180 if (!pWidgetConfigInfo.guid) {
181 pWidgetConfigInfo.guid = configInfo.widget_id;
183 if (pWidgetConfigInfo.guid != configInfo.widget_id) {
184 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
185 LogDebug("Invalid archive");
191 if (!!configInfo.tizenId) {
192 if (pWidgetConfigInfo.pkgName != *configInfo.tizenId) {
193 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
194 LogDebug("Invalid archive - Tizen ID not same error");
199 if (!!configInfo.version) {
200 if (!pWidgetConfigInfo.version) {
201 pWidgetConfigInfo.version = configInfo.version;
203 if (pWidgetConfigInfo.version != configInfo.version) {
204 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
205 LogDebug("Invalid archive");
211 if (!!configInfo.minVersionRequired) {
212 pWidgetConfigInfo.minVersion = configInfo.minVersionRequired;
213 } else if (!!configInfo.tizenMinVersionRequired) {
214 pWidgetConfigInfo.minVersion = configInfo.tizenMinVersionRequired;
220 void WidgetConfigurationManager::processFile(const std::string& path,
221 WrtDB::WidgetRegisterInfo &widgetConfiguration)
225 if (!locateAndParseConfigurationFile(path, widgetConfiguration,
226 DEFAULT_LANGUAGE, &pErrCode)) {
227 LogWarning("Widget archive: Failed while parsing config file");
228 ThrowMsg(Exception::ProcessFailed, path);