19014c07c50ad7db19208cb412c369f312c27b31
[platform/core/uifw/libscl-ui-nui.git] / scl / input_mode_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
18 #include <string.h>
19 #include <libxml/parser.h>
20
21 #include "input_mode_configure_parser.h"
22 #include "xml_parser_utils.h"
23 #include "simple_debug.h"
24
25 /** Example of input mode XML file :
26  * <?xml version="1.0"?>
27  * <input_mode_table>
28  *   <mode name="ENGLISH_QTY">
29  *       <keyset>PORTRAIT_QTY_DEFAULT</keyset>
30  *       <keyset>LANDSCAPE_QTY_DEFAULT</keyset>
31  *   </mode>
32  *   <mode name="PUNCTUATION_POPUP" dim_window="true">
33  *       <keyset>PORTRAIT_PUNCTUATION_POPUP</keyset>
34  *       <keyset>LANDSCAPE_PUNCTUATION_POPUP</keyset>
35  *   </mode>
36  * </input_mode_table>
37  */
38
39 #define INPUT_MODE_CONFIGURE_TABLE_TAG "input_mode_table"
40
41 #define INPUT_MODE_CONFIGURE_MODE_TAG "mode"
42 #define INPUT_MODE_CONFIGURE_MODE_NAME_ATTRIBUTE "name"
43 #define INPUT_MODE_CONFIGURE_MODE_DIM_WINDOW_ATTRIBUTE "dim_window"
44 #define INPUT_MODE_CONFIGURE_MODE_VIRTUAL_WINDOW_ATTRIBUTE "virtual_window"
45
46 #define INPUT_MODE_CONFIGURE_LAYOUT_TAG "layouts"
47 #define INPUT_MODE_CONFIGURE_LAYOUT_PORTRAIT_TAG "portrait"
48 #define INPUT_MODE_CONFIGURE_LAYOUT_LANDSCAPE_TAG "landscape"
49
50 class InputModeConfigureParserImpl {
51     public:
52         InputModeConfigureParserImpl() {
53             m_inputmode_size = 0;
54             memset(m_input_mode_configure_table, 0x00, sizeof(SclInputModeConfigure) * MAX_SCL_INPUT_MODE);
55         }
56
57         ~InputModeConfigureParserImpl() {
58             reset();
59         }
60
61         void reset() {
62             for (int input_mode = 0; input_mode < MAX_SCL_INPUT_MODE; ++input_mode) {
63                 SclInputModeConfigure& cur_rec = m_input_mode_configure_table[input_mode];
64                 if (cur_rec.name) {
65                     xmlFree(cur_rec.name);
66                     cur_rec.name = NULL;
67                 }
68                 if (cur_rec.layouts[DISPLAYMODE_PORTRAIT]) {
69                     xmlFree(cur_rec.layouts[DISPLAYMODE_PORTRAIT]);
70                     cur_rec.layouts[DISPLAYMODE_PORTRAIT] = NULL;
71                 }
72                 if (cur_rec.layouts[DISPLAYMODE_LANDSCAPE]) {
73                     xmlFree(cur_rec.layouts[DISPLAYMODE_LANDSCAPE]);
74                     cur_rec.layouts[DISPLAYMODE_LANDSCAPE] = NULL;
75                 }
76             }
77             m_inputmode_size = 0;
78             memset(m_input_mode_configure_table, 0x00, sizeof(SclInputModeConfigure) * MAX_SCL_INPUT_MODE);
79         }
80
81         int reload_input_mode_configure_table(const char* input_file) {
82             reset();
83             return parsing_input_mode_configure_table(input_file);
84         }
85         int parsing_input_mode_configure_table(const char* input_file) {
86             xmlDocPtr doc;
87             xmlNodePtr cur_node;
88
89
90             doc = xmlReadFile(input_file, NULL, 0);
91             if (doc == NULL) {
92                 SCLLOG(SclLog::DEBUG, "Could not load file: %s.", input_file);
93                 return -1;
94             }
95
96             cur_node = xmlDocGetRootElement(doc);
97             if (cur_node == NULL) {
98                 SCLLOG(SclLog::DEBUG, "InputModeConfigParser: empty document.\n");
99                 xmlFreeDoc(doc);
100                 return -1;
101             }
102             if (0 != xmlStrcmp(cur_node->name, (const xmlChar*)INPUT_MODE_CONFIGURE_TABLE_TAG))
103             {
104                 SCLLOG(SclLog::DEBUG, "InputModeConfigParser: root name error: %s\n!", (char *)cur_node->name);
105                 xmlFreeDoc(doc);
106                 return -1;
107             }
108
109             m_inputmode_size = 0;
110             cur_node = cur_node->xmlChildrenNode;
111
112             SclInputModeConfigure* cur_rec = m_input_mode_configure_table;
113             while (cur_node != NULL) {
114                 if (0 == xmlStrcmp(cur_node->name, (const xmlChar *)INPUT_MODE_CONFIGURE_MODE_TAG)) {
115                     parsing_mode_node(cur_node, cur_rec);
116                     m_inputmode_size++;
117                     cur_rec++;
118                     if (m_inputmode_size >= MAX_SCL_INPUT_MODE) {
119                         SCLLOG(SclLog::ERROR, "No Space for input mode record.");
120                         break;
121                     }
122                 }
123                 cur_node = cur_node->next;
124             }
125             xmlFreeDoc(doc);
126
127             return 0;
128         }
129
130         void set_input_mode_configure_default_record(const PSclInputModeConfigure cur_rec) {
131             cur_rec->name = NULL;
132             cur_rec->layouts[DISPLAYMODE_PORTRAIT] = NULL;
133             cur_rec->layouts[DISPLAYMODE_LANDSCAPE] = NULL;
134             cur_rec->use_virtual_window = FALSE;
135             cur_rec->use_dim_window = FALSE;
136         }
137
138         void parsing_mode_node(const xmlNodePtr cur_node, const PSclInputModeConfigure cur_rec) {
139             assert(cur_node != NULL);
140             assert(cur_rec != NULL);
141             set_input_mode_configure_default_record(cur_rec);
142
143             get_prop_bool(cur_node, INPUT_MODE_CONFIGURE_MODE_VIRTUAL_WINDOW_ATTRIBUTE, &(cur_rec->use_virtual_window));
144             get_prop_bool(cur_node, INPUT_MODE_CONFIGURE_MODE_DIM_WINDOW_ATTRIBUTE, &(cur_rec->use_dim_window));
145
146             xmlChar* temp = xmlGetProp(cur_node, (const xmlChar*)INPUT_MODE_CONFIGURE_MODE_NAME_ATTRIBUTE);
147             cur_rec->name = (sclchar *)temp;
148
149             xmlNodePtr child_node = cur_node->xmlChildrenNode;
150             while (child_node != NULL) {
151                 if (0 == xmlStrcmp(child_node->name, (const xmlChar *)"text")) {
152                     child_node = child_node->next;
153                     continue;
154                 }
155                 if (0 == xmlStrcmp(child_node->name, (const xmlChar *)INPUT_MODE_CONFIGURE_LAYOUT_TAG)) {
156                     parsing_layouts(child_node, cur_rec);
157                 } else {
158                     SCLLOG(SclLog::WARNING, "input_mode_configure has no such node name: %s\n", (char *)child_node->name);
159                 }
160
161                 child_node = child_node->next;
162             }
163         }
164
165         void parsing_layouts(const xmlNodePtr cur_node, const PSclInputModeConfigure cur_rec) {
166             assert(cur_node != NULL);
167             assert(cur_rec != NULL);
168             assert(0 == xmlStrcmp(cur_node->name, (const xmlChar*)INPUT_MODE_CONFIGURE_LAYOUT_TAG) );
169
170             xmlNodePtr child_node = cur_node->xmlChildrenNode;
171             while (child_node != NULL) {
172                 if ( 0 == xmlStrcmp(child_node->name, (const xmlChar *)INPUT_MODE_CONFIGURE_LAYOUT_PORTRAIT_TAG) ) {
173                     xmlChar* temp = xmlNodeGetContent(child_node);
174                     cur_rec->layouts[DISPLAYMODE_PORTRAIT] = (sclchar*)temp;
175                 } else if ( 0 == xmlStrcmp(child_node->name, (const xmlChar *)INPUT_MODE_CONFIGURE_LAYOUT_LANDSCAPE_TAG) ) {
176                     xmlChar* temp = xmlNodeGetContent(child_node);
177                     cur_rec->layouts[DISPLAYMODE_LANDSCAPE] = (sclchar*)temp;
178                 }
179                 child_node = child_node->next;
180             }
181         }
182
183         int m_inputmode_size;
184         SclInputModeConfigure m_input_mode_configure_table[MAX_SCL_INPUT_MODE];
185 };
186
187 InputModeConfigParser::InputModeConfigParser() {
188     m_impl = new InputModeConfigureParserImpl;
189 }
190
191 InputModeConfigParser::~InputModeConfigParser() {
192     if (m_impl) {
193         SCLLOG(SclLog::MESSAGE, "~InputModeConfigParser() has called");
194         delete m_impl;
195         m_impl = NULL;
196     }
197 }
198
199 InputModeConfigParser*
200 InputModeConfigParser::get_instance() {
201     static InputModeConfigParser instance;
202     return &instance;
203 }
204
205 int
206 InputModeConfigParser::init(const char* file) {
207     return m_impl->parsing_input_mode_configure_table(file);
208 }
209
210 int
211 InputModeConfigParser::reload(const char* file) {
212     return m_impl->reload_input_mode_configure_table(file);
213 }
214
215 int
216 InputModeConfigParser::get_inputmode_id(const char *name) {
217     if (name == NULL) {
218         SCLLOG(SclLog::DEBUG, "get_inputmode_id failed");
219         return -1;
220     }
221
222     PSclInputModeConfigure config_table = get_input_mode_configure_table();
223
224     if (config_table == NULL) {
225         SCLLOG(SclLog::DEBUG, "get_inputmode_id failed");
226         return -1;
227     }
228
229     for (int i = 0; i < get_inputmode_size(); ++i) {
230         if (config_table[i].name) {
231             if ( 0 == strcmp(config_table[i].name, name) ) {
232                 return i;
233             }
234         }
235     }
236
237     SCLLOG(SclLog::DEBUG, "get_inputmode_id failed");
238     return -1;
239 }
240
241 const char*
242 InputModeConfigParser::get_inputmode_name(int id) {
243     if (id >= 0 && id < MAX_SCL_INPUT_MODE) {
244         PSclInputModeConfigure config_table = get_input_mode_configure_table();
245         if (config_table) {
246             return config_table[id].name;
247         }
248     }
249
250     return NULL;
251 }
252
253 int
254 InputModeConfigParser::get_inputmode_size() {
255     return m_impl->m_inputmode_size;
256 }
257
258 PSclInputModeConfigure
259 InputModeConfigParser::get_input_mode_configure_table() {
260     return m_impl->m_input_mode_configure_table;
261 }
262