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, int* pErrCode)
54 using namespace WrtDB;
60 ConfigParserData& configInfo = pWidgetConfigInfo.configInfo;
62 // check if this installation from browser, or not.
63 size_t pos = _currentPath.rfind("/");
64 std::ostringstream infoPath;
65 infoPath << _currentPath.substr(pos + 1);
68 std::string language = "";
70 if (infoPath.str() != WRT_WIDGET_CONFIG_FILE_NAME) {
71 // in case of general installation using wgt archive
72 //TODO: use DPL::String in the caller to this function too.
73 DPL::String currentPath = DPL::FromUTF8String(_currentPath);
75 if (currentPath.empty() || baseFolder.empty()) {
76 *pErrCode = WRT_ERR_INVALID_ARG;
80 //TODO: rewrite this madness
81 char cfgAbsPath[MAX_WIDGET_PATH_SIZE + 1] = { 0 };
83 struct dirent* ptr = NULL;
85 dir = opendir(_currentPath.c_str());
87 *pErrCode = WRT_ERR_UNKNOWN;
91 //TODO why don't we use fopen here
92 bool has_config_xml = false;
94 while ((ptr = readdir(dir)) != NULL) { //Find configuration file, based on its name
95 if (ptr->d_type == DT_REG) {
96 if (!strcmp(ptr->d_name, WRT_WIDGET_CONFIG_FILE_NAME)) {
97 _WrtUtilSetAbsolutePath(cfgAbsPath, _currentPath.c_str(),
99 //Parse widget configuration file
100 LogDebug("Found config: " << cfgAbsPath);
104 parser.Parse(cfgAbsPath,
106 new RootParser<WidgetParser>(configInfo,
107 DPL::FromUTF32String(
110 Catch(ElementParser::Exception::Base){
111 LogDebug("Invalid widget configuration file!");
112 // _rethrown_exception.Dump();
113 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
119 // WidgetConfigurationParser & parser = WidgetConfigurationParserSingleton::Instance();
120 // if (!parser.parseConfigurationFile(cfgAbsPath, configInfo, baseFolder.c_str(), pWidgetConfigInfo.signature_type)) {
121 // LogDebug("Invalid widget configuration file!");
122 // *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
127 has_config_xml = true;
134 //We must have config.xml so leaveing if we doesn't
135 if (!has_config_xml) {
136 LogDebug("Invalid archive");
137 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
141 // in case of browser installation
144 parser.Parse(_currentPath,
146 new RootParser<WidgetParser>(configInfo,
147 DPL::FromUTF32String(L"widget"))));
149 Catch(ElementParser::Exception::Base){
150 LogDebug("Invalid widget configuration file!");
151 // _rethrown_exception.Dump();
152 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
158 if (!_WrtUtilStringToLower(baseFolder.c_str(), &tmp_language)) {
159 *pErrCode = WRT_ERR_UNKNOWN;
164 *pErrCode = WRT_ERR_UNKNOWN;
167 language = tmp_language;
170 if (!!configInfo.widget_id) {
171 if (!pWidgetConfigInfo.guid) {
172 pWidgetConfigInfo.guid = configInfo.widget_id;
174 if (pWidgetConfigInfo.guid != configInfo.widget_id) {
175 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
176 LogDebug("Invalid archive");
182 if (!!configInfo.tizenId) {
183 if (pWidgetConfigInfo.pkgname_NOTNULL != *configInfo.tizenId) {
184 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
185 LogDebug("Invalid archive - Tizen ID not same error");
190 if (!!configInfo.version) {
191 if (!pWidgetConfigInfo.version) {
192 pWidgetConfigInfo.version = configInfo.version;
194 if (pWidgetConfigInfo.version != configInfo.version) {
195 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
196 LogDebug("Invalid archive");
202 if (!!configInfo.minVersionRequired) {
203 pWidgetConfigInfo.minVersion = configInfo.minVersionRequired;
204 } else if (!!configInfo.tizenMinVersionRequired) {
205 pWidgetConfigInfo.minVersion = configInfo.tizenMinVersionRequired;
211 void WidgetConfigurationManager::processFile(const std::string& path,
212 WrtDB::WidgetRegisterInfo &widgetConfiguration)
216 if (!locateAndParseConfigurationFile(path, widgetConfiguration,
217 DEFAULT_LANGUAGE, &pErrCode)) {
218 LogWarning("Widget archive: Failed while parsing config file");
219 ThrowMsg(Exception::ProcessFailed, path);