Upload initial version
[platform/core/uifw/libscl-ui-nui.git] / scl / xmlresource.cpp
1 /*
2  * Copyright (c) 2012 - 2014 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
18 #include <libxml/parser.h>
19 #include <assert.h>
20 #include <limits.h>
21 #include <string.h>
22 #include <string>
23 #include <vector>
24 #include <algorithm>
25 #include "xmlresource.h"
26 #include "simple_debug.h"
27 using namespace std;
28 using namespace xmlresource;
29
30 string g_entry_filepath;
31
32 XMLResource::XMLResource() {
33     m_main_entry_parser = NULL;
34     m_input_mode_configure_parser = NULL;
35     m_layout_parser = NULL;
36     m_modifier_decoration_parser = NULL;
37     m_label_properties_parser = NULL;
38     m_default_configure_parser = NULL;
39     m_autopopup_configure_parser = NULL;
40     m_magnifier_configure_parser = NULL;
41     m_nine_patch_file_list_parser = NULL;
42 }
43
44 XMLResource::~XMLResource() {
45     m_main_entry_parser = NULL;
46     m_input_mode_configure_parser = NULL;
47     m_layout_parser = NULL;
48     m_modifier_decoration_parser = NULL;
49     m_label_properties_parser = NULL;
50     m_default_configure_parser = NULL;
51     m_autopopup_configure_parser = NULL;
52     m_magnifier_configure_parser = NULL;
53     m_nine_patch_file_list_parser = NULL;
54 }
55
56 XMLResource*
57 XMLResource::get_instance() {
58     static XMLResource instance;
59     return &instance;
60 }
61
62 static void
63 get_layout_files(PSclInputModeConfigure input_mode_table,
64     size_t input_mode_size, vector<string> &vec_file) {
65     vec_file.clear();
66     for (unsigned int mode = 0; mode < input_mode_size; mode++) {
67         SclInputModeConfigure &input_mode = input_mode_table[mode];
68         for (int direct = 0; direct < DISPLAYMODE_MAX; direct++) {
69             char * layout_file_path = input_mode.layouts[direct];
70             if (layout_file_path
71                     && 0 != strcmp(layout_file_path, "")) {
72                 vec_file.push_back(layout_file_path);
73             }
74         }
75     }
76
77     // quick sort
78     std::sort(vec_file.begin(), vec_file.end());
79     // use std::unique() to puts duplicates to the [last, end)
80     vector<string>::iterator last = std::unique(vec_file.begin(), vec_file.end());
81     // remove the duplicated items, [last, end)
82     vec_file.erase(last, vec_file.end());
83 }
84
85 void
86 XMLResource::init(const char *entry_filepath) {
87     g_entry_filepath = entry_filepath;
88     if (m_main_entry_parser == NULL) {
89         m_main_entry_parser = MainEntryParser::get_instance();
90         char input_file[_POSIX_PATH_MAX] = {0};
91         snprintf(input_file, _POSIX_PATH_MAX, "%s/%s", get_resource_directory(), entry_filepath);
92         if (0 != m_main_entry_parser->init(input_file)) {
93             /* main entry is necessary */
94             SCLLOG(SclLog::ERROR, "main entry init");
95         }
96     }
97
98     /* get each type of xml file name */
99     XMLFiles& xml_files = m_main_entry_parser->get_xml_files();
100
101     SCLLOG(SclLog::MESSAGE, "init inputmode configure\n\n");
102     if (m_input_mode_configure_parser == NULL) {
103         m_input_mode_configure_parser = InputModeConfigParser::get_instance();
104         char input_file[_POSIX_PATH_MAX] = {0};
105         snprintf(input_file, _POSIX_PATH_MAX, "%s/%s", get_resource_directory(), xml_files.input_mode_configure);
106         if (0 != m_input_mode_configure_parser->init(input_file)) {
107             /* input mode configure is necessary */
108             SCLLOG(SclLog::ERROR, "input mode configure init");
109         }
110     }
111
112     SCLLOG(SclLog::MESSAGE, "init default_configure\n\n");
113     if (m_default_configure_parser == NULL) {
114         m_default_configure_parser = DefaultConfigParser::get_instance();
115         char input_file[_POSIX_PATH_MAX] = {0};
116         snprintf(input_file, _POSIX_PATH_MAX, "%s/%s", get_resource_directory(), xml_files.default_configure);
117
118         if (0 != m_default_configure_parser->init(input_file)) {
119             /* default configure is not necessary */
120             SCLLOG(SclLog::WARNING, "default configure init");
121         }
122     }
123
124     SCLLOG(SclLog::MESSAGE, "init modifier_decoration\n\n");
125     if (m_modifier_decoration_parser == NULL) {
126         m_modifier_decoration_parser = ModifierDecorationParser::get_instance();
127         char input_file[_POSIX_PATH_MAX] = {0};
128         snprintf(input_file, _POSIX_PATH_MAX, "%s/%s", get_resource_directory(), xml_files.modifier_decoration);
129
130         if (0 != m_modifier_decoration_parser->init(input_file)) {
131             /* modifier decoration is not necessary */
132             SCLLOG(SclLog::WARNING, "modifier decoration init");
133         }
134     }
135     SCLLOG(SclLog::MESSAGE, "init label_properties\n\n");
136     if (m_label_properties_parser == NULL) {
137         m_label_properties_parser = LabelPropertyParser::get_instance();
138         char input_file[_POSIX_PATH_MAX] = {0};
139         snprintf(input_file, _POSIX_PATH_MAX, "%s/%s", get_resource_directory(), xml_files.key_label_property);
140
141         if (0 != m_label_properties_parser->init(input_file)) {
142             /* label properties is not necessary */
143             SCLLOG(SclLog::WARNING, "label properties init");
144         }
145     }
146
147     SCLLOG(SclLog::MESSAGE, "init autopopup_configure\n\n");
148     if (m_autopopup_configure_parser == NULL) {
149         m_autopopup_configure_parser = AutoPopupConfigParser::get_instance();
150         char input_file[_POSIX_PATH_MAX] = {0};
151         snprintf(input_file, _POSIX_PATH_MAX, "%s/%s", get_resource_directory(), xml_files.autopopup_configure);
152         if (0 != m_autopopup_configure_parser->init(input_file)) {
153             /* autopopup configure is not necessary */
154             SCLLOG(SclLog::WARNING, "autopopup configure init");
155         }
156     }
157     SCLLOG(SclLog::MESSAGE, "init magnifier_configure\n\n");
158     if (m_magnifier_configure_parser == NULL) {
159         m_magnifier_configure_parser = MagnifierConfigParser::get_instance();
160         char input_file[_POSIX_PATH_MAX] = {0};
161         snprintf(input_file, _POSIX_PATH_MAX, "%s/%s", get_resource_directory(), xml_files.magnifier_configure);
162
163         if (0 != m_magnifier_configure_parser->init(input_file)) {
164             /* magnifier configure is not necessary */
165             SCLLOG(SclLog::WARNING, "magnifier configure init");
166         }
167     }
168
169     SCLLOG(SclLog::MESSAGE, "init nine_patch_file_list\n\n");
170     if (m_nine_patch_file_list_parser == NULL) {
171         m_nine_patch_file_list_parser = NinePatchFileParser::get_instance();
172         char input_file[_POSIX_PATH_MAX] = {0};
173         snprintf(input_file, _POSIX_PATH_MAX, "%s/%s", get_resource_directory(), xml_files.nine_patch_file_list);
174
175         if (0 != m_nine_patch_file_list_parser->init(input_file)) {
176             /* nine patch file list is not necessary */
177             SCLLOG(SclLog::WARNING, "nine patch file list init");
178         }
179     }
180
181     SCLLOG(SclLog::MESSAGE, "init layout\n\n");
182     if (m_layout_parser == NULL) {
183         m_layout_parser = LayoutParser::get_instance();
184
185         vector<string> vec_layout_file_name;
186         get_layout_files(
187             m_input_mode_configure_parser->get_input_mode_configure_table(),
188             m_input_mode_configure_parser->get_inputmode_size(),
189             vec_layout_file_name);
190         if ( 0 != m_layout_parser->init(get_resource_directory(),
191             vec_layout_file_name)) {
192             /* layout is necessary */
193             SCLLOG(SclLog::ERROR, "layout init");
194         }
195     }
196
197     SCLLOG(SclLog::MESSAGE, "init Text XML resources OK.\n\n");
198 }
199
200
201 void
202 XMLResource::load(int layout_id) {
203     m_layout_parser->load(layout_id);
204 }
205
206 void
207 XMLResource::unload() {
208     m_layout_parser->unload();
209 }
210
211 bool
212 XMLResource::loaded(int layout_id) {
213     return m_layout_parser->loaded(layout_id);
214 }
215
216 void
217 XMLResource::reload() {
218     if (m_main_entry_parser) {
219         char input_file[_POSIX_PATH_MAX] = { 0 };
220         snprintf(input_file, _POSIX_PATH_MAX, "%s/%s", get_resource_directory(), g_entry_filepath.c_str());
221         if (0 != m_main_entry_parser->reload(input_file)) {
222             /* main entry is necessary */
223             SCLLOG(SclLog::ERROR, "main entry reload");
224         }
225
226         /* get each type of xml file name */
227         XMLFiles& xml_files = m_main_entry_parser->get_xml_files();
228
229         if (m_input_mode_configure_parser) {
230             char input_file[_POSIX_PATH_MAX] = { 0 };
231             snprintf(input_file, _POSIX_PATH_MAX, "%s/%s", get_resource_directory(), xml_files.input_mode_configure);
232             if (0 != m_input_mode_configure_parser->reload(input_file)) {
233                 /* input mode configure is necessary */
234                 SCLLOG(SclLog::ERROR, "input mode configure reload");
235             }
236         }
237
238         SCLLOG(SclLog::MESSAGE, "reload default_configure\n\n");
239         if (m_default_configure_parser) {
240             char input_file[_POSIX_PATH_MAX] = { 0 };
241             snprintf(input_file, _POSIX_PATH_MAX, "%s/%s", get_resource_directory(), xml_files.default_configure);
242
243             if (0 != m_default_configure_parser->reload(input_file)) {
244                 /* default configure is not necessary */
245                 SCLLOG(SclLog::WARNING, "default configure reload");
246             }
247         }
248
249         SCLLOG(SclLog::MESSAGE, "reload modifier_decoration\n\n");
250         if (m_modifier_decoration_parser) {
251             char input_file[_POSIX_PATH_MAX] = { 0 };
252             snprintf(input_file, _POSIX_PATH_MAX, "%s/%s", get_resource_directory(), xml_files.modifier_decoration);
253
254             if (0 != m_modifier_decoration_parser->reload(input_file)) {
255                 /* modifier decoration is not necessary */
256                 SCLLOG(SclLog::WARNING, "modifier decoration reload");
257             }
258         }
259         SCLLOG(SclLog::MESSAGE, "reload label_properties\n\n");
260         if (m_label_properties_parser) {
261             char input_file[_POSIX_PATH_MAX] = { 0 };
262             snprintf(input_file, _POSIX_PATH_MAX, "%s/%s", get_resource_directory(), xml_files.key_label_property);
263
264             if (0 != m_label_properties_parser->reload(input_file)) {
265                 /* label properties is not necessary */
266                 SCLLOG(SclLog::WARNING, "label properties reload");
267             }
268         }
269
270         SCLLOG(SclLog::MESSAGE, "reload autopopup_configure\n\n");
271         if (m_autopopup_configure_parser) {
272             char input_file[_POSIX_PATH_MAX] = { 0 };
273             snprintf(input_file, _POSIX_PATH_MAX, "%s/%s", get_resource_directory(), xml_files.autopopup_configure);
274             if (0 != m_autopopup_configure_parser->reload(input_file)) {
275                 /* autopopup configure is not necessary */
276                 SCLLOG(SclLog::WARNING, "autopopup configure reload");
277             }
278         }
279         SCLLOG(SclLog::MESSAGE, "reload magnifier_configure\n\n");
280         if (m_magnifier_configure_parser) {
281             char input_file[_POSIX_PATH_MAX] = { 0 };
282             snprintf(input_file, _POSIX_PATH_MAX, "%s/%s", get_resource_directory(), xml_files.magnifier_configure);
283
284             if (0 != m_magnifier_configure_parser->reload(input_file)) {
285                 /* magnifier configure is not necessary */
286                 SCLLOG(SclLog::WARNING, "magnifier configure reload");
287             }
288         }
289
290         SCLLOG(SclLog::MESSAGE, "reload nine_patch_file_list\n\n");
291         if (m_nine_patch_file_list_parser) {
292             char input_file[_POSIX_PATH_MAX] = { 0 };
293             snprintf(input_file, _POSIX_PATH_MAX, "%s/%s", get_resource_directory(), xml_files.nine_patch_file_list);
294
295             if (0 != m_nine_patch_file_list_parser->reload(input_file)) {
296                 /* nine patch file list is not necessary */
297                 SCLLOG(SclLog::WARNING, "nine patch file list reload");
298             }
299         }
300
301         SCLLOG(SclLog::MESSAGE, "reload layout\n\n");
302         if (m_layout_parser && m_input_mode_configure_parser) {
303             vector<string> vec_layout_file_name;
304             get_layout_files(
305                 m_input_mode_configure_parser->get_input_mode_configure_table(),
306                 m_input_mode_configure_parser->get_inputmode_size(),
307                 vec_layout_file_name);
308             if (0 != m_layout_parser->reload(get_resource_directory(),
309                 vec_layout_file_name)) {
310                 /* layout is necessary */
311                 SCLLOG(SclLog::ERROR, "layout init");
312             }
313         }
314     }
315 }
316
317 SclNinePatchInfo*
318 XMLResource::get_nine_patch_list() {
319     return m_nine_patch_file_list_parser->get_nine_patch_list();
320 }
321
322 int
323 XMLResource::get_labelproperty_size() {
324     return m_label_properties_parser->get_size();
325 }
326
327 bool
328 XMLResource::get_nine_patch_info(const char *filename, SclNinePatchInfo *info) {
329     return m_nine_patch_file_list_parser->get_nine_patch_info(filename, info);
330 }
331
332 int
333 XMLResource::get_inputmode_id(const char *name) {
334     return m_input_mode_configure_parser->get_inputmode_id(name);
335 }
336
337 const char*
338 XMLResource::get_inputmode_name(int id) {
339     return m_input_mode_configure_parser->get_inputmode_name(id);
340 }
341
342 int
343 XMLResource::get_inputmode_size() {
344     return m_input_mode_configure_parser->get_inputmode_size();
345 }
346
347 PSclInputModeConfigure
348 XMLResource::get_input_mode_configure_table() {
349     return m_input_mode_configure_parser->get_input_mode_configure_table();
350 }
351
352 int
353 XMLResource::get_layout_id(const char *name) {
354     return m_layout_parser->get_layout_index(name);
355 }
356
357 int
358 XMLResource::get_layout_size() {
359     return m_layout_parser->get_layout_size();
360 }
361
362 int
363 XMLResource::get_modifier_decoration_id(const char *name) {
364     return m_modifier_decoration_parser->get_modifier_decoration_id(name);
365 }
366
367 PSclLayout
368 XMLResource::get_layout_table() {
369     return m_layout_parser->get_layout_table();
370 }
371
372 PSclLayoutKeyCoordinatePointerTable
373 XMLResource::get_key_coordinate_pointer_frame() {
374     return m_layout_parser->get_key_coordinate_pointer_frame();
375 }
376
377 PSclModifierDecoration
378 XMLResource::get_modifier_decoration_table() {
379     return m_modifier_decoration_parser->get_modifier_decoration_table();
380 }
381
382 PSclLabelPropertiesTable
383 XMLResource::get_label_properties_frame() {
384     return m_label_properties_parser->get_label_properties_frame();
385 }
386
387 PSclDefaultConfigure
388 XMLResource::get_default_configure() {
389     return m_default_configure_parser->get_default_configure();
390 }
391
392 PSclAutoPopupConfigure
393 XMLResource::get_autopopup_configure() {
394     return m_autopopup_configure_parser->get_autopopup_configure();
395 }
396
397 PSclMagnifierWndConfigure
398 XMLResource::get_magnifier_configure() {
399     return m_magnifier_configure_parser->get_magnifier_configure();
400 }