Pkgname (tizen id) - not null
[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, int* pErrCode)
53 {
54     using namespace WrtDB;
55
56     if (!pErrCode) {
57         return false;
58     }
59
60     ConfigParserData& configInfo = pWidgetConfigInfo.configInfo;
61
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);
66
67     ParserRunner parser;
68     std::string language = "";
69
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);
74
75         if (currentPath.empty() || baseFolder.empty()) {
76             *pErrCode = WRT_ERR_INVALID_ARG;
77             return false;
78         }
79
80         //TODO: rewrite this madness
81         char cfgAbsPath[MAX_WIDGET_PATH_SIZE + 1] = { 0 };
82         DIR* dir = NULL;
83         struct dirent* ptr = NULL;
84
85         dir = opendir(_currentPath.c_str());
86         if (dir == NULL) {
87             *pErrCode = WRT_ERR_UNKNOWN;
88             return false;
89         }
90
91         //TODO why don't we use fopen here
92         bool has_config_xml = false;
93         errno = 0;
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(),
98                             ptr->d_name);
99                     //Parse widget configuration file
100                     LogDebug("Found config: " << cfgAbsPath);
101
102                     Try
103                     {
104                         parser.Parse(cfgAbsPath,
105                                 ElementParserPtr(
106                                         new RootParser<WidgetParser>(configInfo,
107                                                 DPL::FromUTF32String(
108                                                         L"widget"))));
109                     }
110                     Catch(ElementParser::Exception::Base){
111                     LogDebug("Invalid widget configuration file!");
112                     //                    _rethrown_exception.Dump();
113                     *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
114                     closedir(dir);
115                     return false;
116                 }
117
118                 //
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;
123                 //                    closedir(dir);
124                 //                    return false;
125                 //                }
126
127                     has_config_xml = true;
128                     break;
129                 }
130             }
131         }
132         closedir(dir);
133
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;
138             return false;
139         }
140     } else {
141         // in case of browser installation
142         Try
143         {
144             parser.Parse(_currentPath,
145                     ElementParserPtr(
146                             new RootParser<WidgetParser>(configInfo,
147                                     DPL::FromUTF32String(L"widget"))));
148         }
149         Catch(ElementParser::Exception::Base){
150         LogDebug("Invalid widget configuration file!");
151         //                    _rethrown_exception.Dump();
152         *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
153         return false;
154     }
155 }
156
157     char *tmp_language;
158     if (!_WrtUtilStringToLower(baseFolder.c_str(), &tmp_language)) {
159         *pErrCode = WRT_ERR_UNKNOWN;
160         return false;
161     }
162
163     if (!tmp_language) {
164         *pErrCode = WRT_ERR_UNKNOWN;
165         return false;
166     }
167     language = tmp_language;
168     free(tmp_language);
169
170     if (!!configInfo.widget_id) {
171         if (!pWidgetConfigInfo.guid) {
172             pWidgetConfigInfo.guid = configInfo.widget_id;
173         } else {
174             if (pWidgetConfigInfo.guid != configInfo.widget_id) {
175                 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
176                 LogDebug("Invalid archive");
177                 return false;
178             }
179         }
180     }
181
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");
186             return false;
187         }
188     }
189
190     if (!!configInfo.version) {
191         if (!pWidgetConfigInfo.version) {
192             pWidgetConfigInfo.version = configInfo.version;
193         } else {
194             if (pWidgetConfigInfo.version != configInfo.version) {
195                 *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
196                 LogDebug("Invalid archive");
197                 return false;
198             }
199         }
200     }
201
202     if (!!configInfo.minVersionRequired) {
203         pWidgetConfigInfo.minVersion = configInfo.minVersionRequired;
204     } else if (!!configInfo.tizenMinVersionRequired) {
205         pWidgetConfigInfo.minVersion = configInfo.tizenMinVersionRequired;
206     }
207
208     return true;
209 }
210
211 void WidgetConfigurationManager::processFile(const std::string& path,
212         WrtDB::WidgetRegisterInfo &widgetConfiguration)
213 {
214     int pErrCode;
215
216     if (!locateAndParseConfigurationFile(path, widgetConfiguration,
217             DEFAULT_LANGUAGE, &pErrCode)) {
218         LogWarning("Widget archive: Failed while parsing config file");
219         ThrowMsg(Exception::ProcessFailed, path);
220     }
221 }