Update wrt-installer_0.0.52
[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     ConfigParserData& configInfo = pWidgetConfigInfo.configInfo;
62
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);
67
68     ParserRunner parser;
69     std::string language = "";
70
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);
75
76         if (currentPath.empty() || baseFolder.empty()) {
77             *pErrCode = WRT_ERR_INVALID_ARG;
78             return false;
79         }
80
81         //TODO: rewrite this madness
82         char cfgAbsPath[MAX_WIDGET_PATH_SIZE + 1] = { 0 };
83         DIR* dir = NULL;
84         struct dirent* ptr = NULL;
85
86         dir = opendir(_currentPath.c_str());
87         if (dir == NULL) {
88             *pErrCode = WRT_ERR_UNKNOWN;
89             return false;
90         }
91
92         //TODO why don't we use fopen here
93         bool has_config_xml = false;
94         errno = 0;
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);
102
103                     Try
104                     {
105                         parser.Parse(cfgAbsPath, ElementParserPtr(new
106                                                                   RootParser<
107                                                                       WidgetParser>(
108                                                                       configInfo,
109                                                                       DPL
110                                                                           ::
111                                                                           FromUTF32String(
112                                                                           L"widget"))));
113                     }
114                     Catch(ElementParser::Exception::Base)
115                     {
116                         LogDebug("Invalid widget configuration file!");
117                         //                    _rethrown_exception.Dump();
118                         *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
119                         closedir(dir);
120                         return false;
121                     }
122
123                     //
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;
128                     //                    closedir(dir);
129                     //                    return false;
130                     //                }
131
132                     has_config_xml = true;
133                     break;
134                 }
135             }
136         }
137         closedir(dir);
138
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;
143             return false;
144         }
145     } else {
146         // in case of browser installation
147         Try
148         {
149             parser.Parse(_currentPath, ElementParserPtr(new
150                                                       RootParser<
151                                                           WidgetParser>(
152                                                           configInfo,
153                                                           DPL::FromUTF32String(
154                                                               L"widget"))));
155         }
156         Catch(ElementParser::Exception::Base)
157         {
158             LogDebug("Invalid widget configuration file!");
159             //                    _rethrown_exception.Dump();
160             *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
161             return false;
162         }
163     }
164
165
166     char *tmp_language;
167     if (!_WrtUtilStringToLower(baseFolder.c_str(), &tmp_language)) {
168         *pErrCode = WRT_ERR_UNKNOWN;
169         return false;
170     }
171
172     if (!tmp_language) {
173         *pErrCode = WRT_ERR_UNKNOWN;
174         return false;
175     }
176     language = tmp_language;
177     free(tmp_language);
178
179     if (!!configInfo.widget_id) {
180         if (!pWidgetConfigInfo.guid) {
181             pWidgetConfigInfo.guid = configInfo.widget_id;
182         } else {
183             if (pWidgetConfigInfo.guid != configInfo.widget_id) {
184                 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
185                 LogDebug("Invalid archive");
186                 return false;
187             }
188         }
189     }
190
191     if (!!configInfo.tizenId) {
192         if (!pWidgetConfigInfo.pkgname) {
193             pWidgetConfigInfo.pkgname = configInfo.tizenId;
194         } else {
195             if (pWidgetConfigInfo.pkgname != configInfo.tizenId) {
196                 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
197                 LogDebug("Invalid archive - Tizen ID not same error");
198                 return false;
199             }
200         }
201     }
202
203     if (!!configInfo.version) {
204         if (!pWidgetConfigInfo.version) {
205             pWidgetConfigInfo.version = configInfo.version;
206         } else {
207             if (pWidgetConfigInfo.version != configInfo.version) {
208                 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
209                 LogDebug("Invalid archive");
210                 return false;
211             }
212         }
213     }
214
215     if (!!configInfo.minVersionRequired) {
216         pWidgetConfigInfo.minVersion = configInfo.minVersionRequired;
217     } else if (!!configInfo.tizenMinVersionRequired) {
218         pWidgetConfigInfo.minVersion = configInfo.tizenMinVersionRequired;
219     }
220
221     return true;
222 }
223
224 void WidgetConfigurationManager::processFile(const std::string& path,
225         WrtDB::WidgetRegisterInfo &widgetConfiguration)
226 {
227     int pErrCode;
228
229     if (!locateAndParseConfigurationFile(path, widgetConfiguration,
230                                          DEFAULT_LANGUAGE, &pErrCode)) {
231         LogWarning("Widget archive: Failed while parsing config file");
232         ThrowMsg(Exception::ProcessFailed, path);
233     }
234 }