6044a5f37998906bdb1683c72f106e449880af83
[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
96                                                // on its name
97             if (ptr->d_type == DT_REG) {
98                 if (!strcmp(ptr->d_name, WRT_WIDGET_CONFIG_FILE_NAME)) {
99                     _WrtUtilSetAbsolutePath(cfgAbsPath,
100                                             _currentPath.c_str(), ptr->d_name);
101                     //Parse widget configuration file
102                     LogDebug("Found config: " << cfgAbsPath);
103
104                     Try
105                     {
106                         parser.Parse(cfgAbsPath, ElementParserPtr(new
107                                                                   RootParser<
108                                                                       WidgetParser>(
109                                                                       configInfo,
110                                                                       DPL
111                                                                           ::
112                                                                           FromUTF32String(
113                                                                           L"widget"))));
114                     }
115                     Catch(ElementParser::Exception::Base)
116                     {
117                         LogDebug("Invalid widget configuration file!");
118                         //                    _rethrown_exception.Dump();
119                         *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
120                         closedir(dir);
121                         return false;
122                     }
123
124                     //
125                     //                WidgetConfigurationParser & parser =
126                     // WidgetConfigurationParserSingleton::Instance();
127                     //                if
128                     // (!parser.parseConfigurationFile(cfgAbsPath, configInfo,
129                     // baseFolder.c_str(), pWidgetConfigInfo.signature_type)) {
130                     //                    LogDebug("Invalid widget configuration
131                     // file!");
132                     //                    *pErrCode =
133                     // WRT_WM_ERR_INVALID_ARCHIVE;
134                     //                    closedir(dir);
135                     //                    return false;
136                     //                }
137
138                     has_config_xml = true;
139                     break;
140                 }
141             }
142         }
143         closedir(dir);
144
145         //We must have config.xml so leaveing if we doesn't
146         if (!has_config_xml) {
147             LogDebug("Invalid archive");
148             *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
149             return false;
150         }
151     } else {
152         // in case of browser installation
153         Try
154         {
155             parser.Parse(_currentPath, ElementParserPtr(new
156                                                         RootParser<
157                                                             WidgetParser>(
158                                                             configInfo,
159                                                             DPL::
160                                                                 FromUTF32String(
161                                                                 L"widget"))));
162         }
163         Catch(ElementParser::Exception::Base)
164         {
165             LogDebug("Invalid widget configuration file!");
166             //                    _rethrown_exception.Dump();
167             *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
168             return false;
169         }
170     }
171
172     char *tmp_language;
173     if (!_WrtUtilStringToLower(baseFolder.c_str(), &tmp_language)) {
174         *pErrCode = WRT_ERR_UNKNOWN;
175         return false;
176     }
177
178     if (!tmp_language) {
179         *pErrCode = WRT_ERR_UNKNOWN;
180         return false;
181     }
182     language = tmp_language;
183     free(tmp_language);
184
185     if (!!configInfo.widget_id) {
186         if (!pWidgetConfigInfo.guid) {
187             pWidgetConfigInfo.guid = configInfo.widget_id;
188         } else {
189             if (pWidgetConfigInfo.guid != configInfo.widget_id) {
190                 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
191                 LogDebug("Invalid archive");
192                 return false;
193             }
194         }
195     }
196
197     if (!!configInfo.tizenId) {
198         if (pWidgetConfigInfo.pkgName != *configInfo.tizenId) {
199             *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
200             LogDebug("Invalid archive - Tizen ID not same error");
201             return false;
202         }
203     }
204
205     if (!!configInfo.version) {
206         if (!pWidgetConfigInfo.version) {
207             pWidgetConfigInfo.version = configInfo.version;
208         } else {
209             if (pWidgetConfigInfo.version != configInfo.version) {
210                 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
211                 LogDebug("Invalid archive");
212                 return false;
213             }
214         }
215     }
216
217     if (!!configInfo.minVersionRequired) {
218         pWidgetConfigInfo.minVersion = configInfo.minVersionRequired;
219     } else if (!!configInfo.tizenMinVersionRequired) {
220         pWidgetConfigInfo.minVersion = configInfo.tizenMinVersionRequired;
221     }
222
223     return true;
224 }
225
226 void WidgetConfigurationManager::processFile(
227     const std::string& path,
228     WrtDB::WidgetRegisterInfo &
229     widgetConfiguration)
230 {
231     int pErrCode;
232
233     if (!locateAndParseConfigurationFile(path, widgetConfiguration,
234                                          DEFAULT_LANGUAGE, &pErrCode))
235     {
236         LogWarning("Widget archive: Failed while parsing config file");
237         ThrowMsg(Exception::ProcessFailed, path);
238     }
239 }