tizen beta release
[framework/web/wrt-installer.git] / src / configuration_parser / WidgetConfigurationManager.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        WidgetConfigurationManager.cpp
18  * @author      Piotr Fatyga (p.fatyga@samsung.com)
19  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20  * @version     0.1
21  * @brief
22  */
23 #include "WidgetConfigurationManager.h"
24 #include <dirent.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)
35
36 //TODO Rewrite this as steps/tasks
37 namespace // anonymous
38 {
39 const char *const DEFAULT_LANGUAGE = "default";
40 const size_t MAX_WIDGET_PATH_SIZE = 1024;
41
42 //#define WRT_WIDGET_DEFAULT_ICON_WIDTH 80
43 //#define WRT_WIDGET_DEFAULT_ICON_HEIGHT 80
44
45 //#define WRT_WIDGET_CONFIG_BASE_LOCALE "locales"
46 const char *const WRT_WIDGET_CONFIG_FILE_NAME = "config.xml";
47 }
48
49 bool WidgetConfigurationManager::locateAndParseConfigurationFile(
50         const std::string& _currentPath,
51         WrtDB::WidgetRegisterInfo& pWidgetConfigInfo,
52         const std::string& baseFolder,
53         int* pErrCode)
54 {
55     using namespace WrtDB;
56
57     if (!pErrCode) {
58         return false;
59     }
60
61     //TODO: use DPL::String in the caller to this function too.
62     DPL::String currentPath = DPL::FromUTF8String(_currentPath);
63
64     if (currentPath.empty() || baseFolder.empty()) {
65         *pErrCode = WRT_ERR_INVALID_ARG;
66         return false;
67     }
68
69     //TODO: rewrite this madness
70     char cfgAbsPath[MAX_WIDGET_PATH_SIZE + 1] = { 0 };
71     DIR* dir = NULL;
72     struct dirent* ptr = NULL;
73     std::string language = "";
74
75     dir = opendir(_currentPath.c_str());
76     if (dir == NULL) {
77         *pErrCode = WRT_ERR_UNKNOWN;
78         return false;
79     }
80
81     ConfigParserData& configInfo = pWidgetConfigInfo.configInfo;
82
83     //TODO why don't we use fopen here
84     bool has_config_xml = false;
85     errno = 0;
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);
93                 ParserRunner parser;
94
95                 Try
96                 {
97                     parser.Parse(cfgAbsPath, ElementParserPtr(new
98                                                               RootParser<
99                                                                   WidgetParser>(
100                                                                   configInfo,
101                                                                   DPL
102                                                                       ::
103                                                                       FromUTF32String(
104                                                                       L"widget"))));
105                 }
106                 Catch(ElementParser::Exception::Base)
107                 {
108                     LogDebug("Invalid widget configuration file!");
109                     //                    _rethrown_exception.Dump();
110                     *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
111                     closedir(dir);
112                     return false;
113                 }
114
115                 //
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;
120                 //                    closedir(dir);
121                 //                    return false;
122                 //                }
123
124                 has_config_xml = true;
125                 break;
126             }
127         }
128     }
129     closedir(dir);
130
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;
135         return false;
136     }
137
138     char *tmp_language;
139     if (!_WrtUtilStringToLower(baseFolder.c_str(), &tmp_language)) {
140         *pErrCode = WRT_ERR_UNKNOWN;
141         return false;
142     }
143
144     if (!tmp_language) {
145         *pErrCode = WRT_ERR_UNKNOWN;
146         return false;
147     }
148     language = tmp_language;
149     free(tmp_language);
150
151     if (!!configInfo.widget_id) {
152         if (!pWidgetConfigInfo.guid) {
153             pWidgetConfigInfo.guid = configInfo.widget_id;
154         } else {
155             if (pWidgetConfigInfo.guid != configInfo.widget_id) {
156                 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
157                 LogDebug("Invalid archive");
158                 return false;
159             }
160         }
161     }
162
163     if (!!configInfo.pkgname) {
164         if (!pWidgetConfigInfo.pkgname) {
165             pWidgetConfigInfo.pkgname = configInfo.pkgname;
166         } else {
167             if (pWidgetConfigInfo.pkgname != configInfo.pkgname) {
168                 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
169                 LogDebug("Invalid archive - Package Name not same error");
170                 return false;
171             }
172         }
173     }
174
175     if (!!configInfo.version) {
176         if (!pWidgetConfigInfo.version) {
177             pWidgetConfigInfo.version = configInfo.version;
178         } else {
179             if (pWidgetConfigInfo.version != configInfo.version) {
180                 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
181                 LogDebug("Invalid archive");
182                 return false;
183             }
184         }
185     }
186
187     return true;
188 }
189
190 void WidgetConfigurationManager::processFile(const std::string& path,
191         WrtDB::WidgetRegisterInfo &widgetConfiguration)
192 {
193     int pErrCode;
194
195     if (!locateAndParseConfigurationFile(path, widgetConfiguration,
196                                          DEFAULT_LANGUAGE, &pErrCode)) {
197         LogWarning("Widget archive: Failed while parsing config file");
198         ThrowMsg(Exception::ProcessFailed, path);
199     }
200 }