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 //TODO: use DPL::String in the caller to this function too.
62 DPL::String currentPath = DPL::FromUTF8String(_currentPath);
64 if (currentPath.empty() || baseFolder.empty()) {
65 *pErrCode = WRT_ERR_INVALID_ARG;
69 //TODO: rewrite this madness
70 char cfgAbsPath[MAX_WIDGET_PATH_SIZE + 1] = { 0 };
72 struct dirent* ptr = NULL;
73 std::string language = "";
75 dir = opendir(_currentPath.c_str());
77 *pErrCode = WRT_ERR_UNKNOWN;
81 ConfigParserData& configInfo = pWidgetConfigInfo.configInfo;
83 //TODO why don't we use fopen here
84 bool has_config_xml = false;
86 while ((ptr = readdir(dir)) != NULL) { //Find configuration file, based on its name
87 if (ptr->d_type == DT_REG) {
88 if (!strcmp(ptr->d_name, WRT_WIDGET_CONFIG_FILE_NAME)) {
89 _WrtUtilSetAbsolutePath(cfgAbsPath,
90 _currentPath.c_str(), ptr->d_name);
91 //Parse widget configuration file
92 LogDebug("Found config: " << cfgAbsPath);
97 parser.Parse(cfgAbsPath, ElementParserPtr(new
106 Catch(ElementParser::Exception::Base)
108 LogDebug("Invalid widget configuration file!");
109 // _rethrown_exception.Dump();
110 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
116 // WidgetConfigurationParser & parser = WidgetConfigurationParserSingleton::Instance();
117 // if (!parser.parseConfigurationFile(cfgAbsPath, configInfo, baseFolder.c_str(), pWidgetConfigInfo.signature_type)) {
118 // LogDebug("Invalid widget configuration file!");
119 // *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
124 has_config_xml = true;
131 //We must have config.xml so leaveing if we doesn't
132 if (!has_config_xml) {
133 LogDebug("Invalid archive");
134 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
139 if (!_WrtUtilStringToLower(baseFolder.c_str(), &tmp_language)) {
140 *pErrCode = WRT_ERR_UNKNOWN;
145 *pErrCode = WRT_ERR_UNKNOWN;
148 language = tmp_language;
151 if (!!configInfo.widget_id) {
152 if (!pWidgetConfigInfo.guid) {
153 pWidgetConfigInfo.guid = configInfo.widget_id;
155 if (pWidgetConfigInfo.guid != configInfo.widget_id) {
156 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
157 LogDebug("Invalid archive");
163 if (!!configInfo.pkgname) {
164 if (!pWidgetConfigInfo.pkgname) {
165 pWidgetConfigInfo.pkgname = configInfo.pkgname;
167 if (pWidgetConfigInfo.pkgname != configInfo.pkgname) {
168 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
169 LogDebug("Invalid archive - Package Name not same error");
175 if (!!configInfo.version) {
176 if (!pWidgetConfigInfo.version) {
177 pWidgetConfigInfo.version = configInfo.version;
179 if (pWidgetConfigInfo.version != configInfo.version) {
180 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
181 LogDebug("Invalid archive");
190 void WidgetConfigurationManager::processFile(const std::string& path,
191 WrtDB::WidgetRegisterInfo &widgetConfiguration)
195 if (!locateAndParseConfigurationFile(path, widgetConfiguration,
196 DEFAULT_LANGUAGE, &pErrCode)) {
197 LogWarning("Widget archive: Failed while parsing config file");
198 ThrowMsg(Exception::ProcessFailed, path);