Upload initial version
[platform/core/uifw/libscl-ui-nui.git] / scl / magnifier_configure_parser.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 #include <string.h>
18 #include <libxml/parser.h>
19
20 #include "magnifier_configure_parser.h"
21 #include <assert.h>
22 #include "xml_parser_utils.h"
23 #include "simple_debug.h"
24 using namespace std;
25
26 static SCLMagnifierStyle
27 get_content_magnifier_style(const xmlNodePtr cur_node) {
28     assert(cur_node != NULL);
29
30     SCLMagnifierStyle style = MAGNIFIER_STYLE_LABEL_ONLY;
31
32     xmlChar* key = xmlNodeGetContent(cur_node);
33     if (key!= NULL) {
34         if (0 == strcmp("area_capture", (const char*)key)) {
35             style = MAGNIFIER_STYLE_SEL_AREA_CAPTURE;
36         }
37         xmlFree(key);
38     }
39
40     return style;
41 }
42
43 class MagnifierConfigureParserImpl {
44     public:
45     MagnifierConfigureParserImpl() {
46         memset((void*)&m_magnifier_configure, 0x00, sizeof(SclMagnifierWndConfigure));
47     }
48
49     ~MagnifierConfigureParserImpl() {
50         reset();
51     }
52
53     void reset() {
54         if (m_magnifier_configure.bg_image_path) {
55             xmlFree(m_magnifier_configure.bg_image_path);
56             m_magnifier_configure.bg_image_path = NULL;
57         }
58         if (m_magnifier_configure.bg_shift_image_path) {
59             xmlFree(m_magnifier_configure.bg_shift_image_path);
60             m_magnifier_configure.bg_shift_image_path = NULL;
61         }
62         if (m_magnifier_configure.bg_shift_lock_image_path) {
63             xmlFree(m_magnifier_configure.bg_shift_lock_image_path);
64             m_magnifier_configure.bg_shift_lock_image_path = NULL;
65         }
66         if (m_magnifier_configure.bg_long_key_image_path) {
67             xmlFree(m_magnifier_configure.bg_long_key_image_path);
68             m_magnifier_configure.bg_long_key_image_path = NULL;
69         }
70         if (m_magnifier_configure.label_type) {
71             xmlFree(m_magnifier_configure.label_type);
72             m_magnifier_configure.label_type = NULL;
73         }
74         for (int loop = 0; loop < MAX_WND_DECORATOR; loop++) {
75             if (m_magnifier_configure.decoration_image_path[loop]) {
76                 xmlFree(m_magnifier_configure.decoration_image_path[loop]);
77                 m_magnifier_configure.decoration_image_path[loop] = NULL;
78             }
79         }
80
81         memset((void*)&m_magnifier_configure, 0x00, sizeof(SclMagnifierWndConfigure));
82     }
83
84     int reload_magnifier_configure(const char* input_file) {
85         reset();
86         return parsing_magnifier_configure(input_file);
87     }
88
89     int parsing_magnifier_configure(const char* input_file) {
90         xmlDocPtr doc;
91         xmlNodePtr cur_node;
92
93         doc = xmlReadFile(input_file, NULL, 0);
94         if (doc == NULL) {
95             SCLLOG(SclLog::DEBUG, "Could not load file: %s.", input_file);
96             return -1;
97         }
98
99         cur_node = xmlDocGetRootElement(doc);
100         if (cur_node == NULL) {
101             SCLLOG(SclLog::DEBUG, "MagnifierConfigParser: empty document.\n");
102             xmlFreeDoc(doc);
103             return -1;
104         }
105         if (0 != xmlStrcmp(cur_node->name, (const xmlChar*)"magnifier_configure"))
106         {
107             SCLLOG(SclLog::DEBUG, "MagnifierConfigParser: root name error: %s\n!", (char *)cur_node->name);
108             xmlFreeDoc(doc);
109             return -1;
110         }
111
112         cur_node = cur_node->xmlChildrenNode;
113
114         while (cur_node != NULL) {
115             if (0 == xmlStrcmp(cur_node->name, (const xmlChar *)"magnifier_style")) {
116                 m_magnifier_configure.style = get_content_magnifier_style(cur_node);
117             } else if (0 == xmlStrcmp(cur_node->name, (const xmlChar *)"width")) {
118                 m_magnifier_configure.width = get_content_int(cur_node);
119             } else if (0 == xmlStrcmp(cur_node->name, (const xmlChar *)"height")) {
120                 m_magnifier_configure.height = get_content_int(cur_node);
121             } else if (0 == xmlStrcmp(cur_node->name, (const xmlChar *)"label_area")) {
122                 parsing_label_area(cur_node);
123             } else if (0 == xmlStrcmp(cur_node->name, (const xmlChar *)"background_image_path")) {
124                 parsing_background_images(cur_node);
125             } else if (0 == xmlStrcmp(cur_node->name, (const xmlChar *)"use_window")) {
126                 m_magnifier_configure.use_actual_window = get_content_bool(cur_node);
127             } else if (0 == xmlStrcmp(cur_node->name, (const xmlChar *)"label_type")) {
128                 xmlChar* temp = xmlNodeGetContent(cur_node);
129                 m_magnifier_configure.label_type = (sclchar*)temp;
130             } else if (0 == xmlStrcmp(cur_node->name, (const xmlChar *)"window_decorator")) {
131                 get_prop_number(cur_node, "size", &(m_magnifier_configure.decoration_size));
132                 parsing_window_decorator_image_path(cur_node);
133             } else if (0 == xmlStrcmp(cur_node->name, (const xmlChar *)"padding")) {
134                 parsing_padding_values(cur_node);
135             } else if (0 == xmlStrcmp(cur_node->name, (const xmlChar *)"show_shift_label")) {
136                 m_magnifier_configure.show_shift_label = get_content_bool(cur_node);
137             }
138             cur_node = cur_node->next;
139         }
140         xmlFreeDoc(doc);
141
142         return 0;
143     }
144
145     void parsing_label_area(const xmlNodePtr cur_node) {
146         assert(cur_node != NULL);
147
148         xmlNodePtr child_node = cur_node->xmlChildrenNode;
149         while (child_node != NULL) {
150             if (0 == xmlStrcmp(child_node->name, (const xmlChar *)"left")) {
151                 m_magnifier_configure.label_area_rect.left = get_content_int(child_node);
152             } else if (0 == xmlStrcmp(child_node->name, (const xmlChar *)"top")) {
153                 m_magnifier_configure.label_area_rect.top = get_content_int(child_node);
154             } else if (0 == xmlStrcmp(child_node->name, (const xmlChar *)"right")) {
155                 m_magnifier_configure.label_area_rect.right = get_content_int(child_node);
156             } else if (0 == xmlStrcmp(child_node->name, (const xmlChar *)"bottom")) {
157                 m_magnifier_configure.label_area_rect.bottom = get_content_int(child_node);
158             }
159
160             child_node = child_node->next;
161         }
162     }
163     void parsing_background_images(const xmlNodePtr cur_node) {
164         assert(cur_node != NULL);
165
166         xmlNodePtr child_node = cur_node->xmlChildrenNode;
167         while (child_node != NULL) {
168             if (0 == xmlStrcmp(child_node->name, (const xmlChar *)"image")) {
169                 if (equal_prop(child_node, "state", "normal")) {
170                     xmlChar* temp = xmlNodeGetContent(child_node);
171                     m_magnifier_configure.bg_image_path = (sclchar *)temp;
172                 } else if (equal_prop(child_node, "state", "shift")) {
173                     xmlChar* temp = xmlNodeGetContent(child_node);
174                     m_magnifier_configure.bg_shift_image_path = (sclchar *)temp;
175                 } else if (equal_prop(child_node, "state", "lock")) {
176                     xmlChar* temp = xmlNodeGetContent(child_node);
177                     m_magnifier_configure.bg_shift_lock_image_path = (sclchar *)temp;
178                 } else if (equal_prop(child_node, "state", "longkey")) {
179                     xmlChar* temp = xmlNodeGetContent(child_node);
180                     m_magnifier_configure.bg_long_key_image_path = (sclchar *)temp;
181                 }
182             }
183
184             child_node = child_node->next;
185         }
186     }
187     void parsing_window_decorator_image_path(const xmlNodePtr cur_node) {
188         assert(cur_node != NULL);
189         assert(0 == xmlStrcmp(cur_node->name, (const xmlChar*)"window_decorator"));
190         xmlNodePtr child_node = cur_node->xmlChildrenNode;
191
192         while (child_node != NULL) {
193             if (0 == xmlStrcmp(child_node->name, (const xmlChar*)"image")) {
194                 if (equal_prop(child_node, "direction", "top_left")) {
195                     m_magnifier_configure.decoration_image_path[WND_DECORATOR_TOP_LEFT] = (sclchar *)xmlNodeGetContent(child_node);
196                 } else if (equal_prop(child_node, "direction", "top_center")) {
197                     m_magnifier_configure.decoration_image_path[WND_DECORATOR_TOP_CENTER] = (sclchar *)xmlNodeGetContent(child_node);
198                 } else if (equal_prop(child_node, "direction", "top_right")) {
199                     m_magnifier_configure.decoration_image_path[WND_DECORATOR_TOP_RIGHT] = (sclchar *)xmlNodeGetContent(child_node);
200                 } else if (equal_prop(child_node, "direction", "middle_left")) {
201                     m_magnifier_configure.decoration_image_path[WND_DECORATOR_MIDDLE_LEFT] = (sclchar *)xmlNodeGetContent(child_node);
202                 } else if (equal_prop(child_node, "direction", "middle_center")) {
203                     m_magnifier_configure.decoration_image_path[WND_DECORATOR_MIDDLE_CENTER] = (sclchar *)xmlNodeGetContent(child_node);
204                 } else if (equal_prop(child_node, "direction", "middle_right")) {
205                     m_magnifier_configure.decoration_image_path[WND_DECORATOR_MIDDLE_RIGHT] = (sclchar *)xmlNodeGetContent(child_node);
206                 } else if (equal_prop(child_node, "direction", "bottom_left")) {
207                     m_magnifier_configure.decoration_image_path[WND_DECORATOR_BOTTOM_LEFT] = (sclchar *)xmlNodeGetContent(child_node);
208                 } else if (equal_prop(child_node, "direction", "bottom_center")) {
209                     m_magnifier_configure.decoration_image_path[WND_DECORATOR_BOTTOM_CENTER] = (sclchar *)xmlNodeGetContent(child_node);
210                 } else if (equal_prop(child_node, "direction", "bottom_right")) {
211                     m_magnifier_configure.decoration_image_path[WND_DECORATOR_BOTTOM_RIGHT] = (sclchar *)xmlNodeGetContent(child_node);
212                 }
213             }
214             child_node = child_node->next;
215         }
216     }
217     void parsing_padding_values(const xmlNodePtr cur_node) {
218         assert(cur_node != NULL);
219
220         xmlNodePtr child_node = cur_node->xmlChildrenNode;
221         while (child_node != NULL) {
222             if (0 == xmlStrcmp(child_node->name, (const xmlChar *)"x")) {
223                 m_magnifier_configure.padding_x = get_content_int(child_node);
224             } else if (0 == xmlStrcmp(child_node->name, (const xmlChar *)"y")) {
225                 m_magnifier_configure.padding_y = get_content_int(child_node);
226             }
227
228             child_node = child_node->next;
229         }
230     }
231     SclMagnifierWndConfigure m_magnifier_configure;
232 };
233
234 MagnifierConfigParser::MagnifierConfigParser() {
235     m_impl = new MagnifierConfigureParserImpl;
236 }
237
238 MagnifierConfigParser::~MagnifierConfigParser() {
239     if (m_impl) {
240         SCLLOG(SclLog::MESSAGE, "~MagnifierConfigParser() has called");
241         delete m_impl;
242         m_impl = NULL;
243     }
244 }
245
246 MagnifierConfigParser*
247 MagnifierConfigParser::get_instance() {
248     static MagnifierConfigParser instance;
249     return &instance;
250 }
251
252 int
253 MagnifierConfigParser::init(const char* file) {
254     return m_impl->parsing_magnifier_configure(file);
255 }
256
257 int
258 MagnifierConfigParser::reload(const char* file) {
259     return m_impl->reload_magnifier_configure(file);
260 }
261
262 PSclMagnifierWndConfigure
263 MagnifierConfigParser::get_magnifier_configure() {
264     return &m_impl->m_magnifier_configure;
265 }