Update year information of license boilerplate
[platform/core/uifw/multi-assistant-service.git] / src / multi_assistant_config.c
1 /*
2  * Copyright 2018-2019 Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 <libxml/parser.h>
18 #include <sys/types.h>
19 #include <dirent.h>
20
21 #include "multi_assistant_config.h"
22 #include "multi_assistant_main.h"
23
24 int mas_config_parse_assistant_info(mas_config_assistant_info_cb callback, const char *path, void* user_data)
25 {
26         xmlDocPtr doc = NULL;
27         xmlNodePtr cur = NULL;
28         xmlChar *key;
29
30         int loop;
31         int retry_count = 0;
32
33         while (NULL == doc) {
34                 doc = xmlParseFile(path);
35                 if (NULL != doc) {
36                         break;
37                 }
38
39                 if (MA_RETRY_COUNT == retry_count++) {
40                         MAS_LOGE("[ERROR] Fail to parse file error : %s", path);
41                         xmlCleanupParser();
42                         return -1;
43                 }
44                 usleep(10000);
45         }
46
47         cur = xmlDocGetRootElement(doc);
48         if (cur == NULL) {
49                 MAS_LOGE("[ERROR] Empty document");
50                 xmlFreeDoc(doc);
51                 return -1;
52         }
53
54         if (xmlStrcmp(cur->name, (const xmlChar *) MA_TAG_ASSISTANT_BASE)) {
55                 MAS_LOGE("[ERROR] The wrong type, root node is NOT %s", MA_TAG_ASSISTANT_BASE);
56                 xmlFreeDoc(doc);
57                 return -1;
58         }
59
60         cur = cur->xmlChildrenNode;
61         if (cur == NULL) {
62                 MAS_LOGE("[ERROR] Empty document");
63                 xmlFreeDoc(doc);
64                 return -1;
65         }
66
67         /* alloc assistant info */
68         ma_assistant_info_s* temp;
69         temp = (ma_assistant_info_s*)calloc(1, sizeof(ma_assistant_info_s));
70         if (NULL == temp) {
71                 MAS_LOGE("[ERROR] Fail to allocate memory");
72                 xmlFreeDoc(doc);
73                 return -1;
74         }
75
76         temp->app_id = NULL;
77         temp->name = NULL;
78         temp->icon_path = NULL;
79         memset(temp->wakeup_list, 0x00, sizeof(temp->wakeup_list));
80         memset(temp->wakeup_language, 0x00, sizeof(temp->wakeup_language));
81         temp->cnt_wakeup = 0;
82         memset(temp->supported_lang, 0x00, sizeof(temp->supported_lang));
83         temp->cnt_lang = 0;
84         temp->wakeup_engine = NULL;
85         temp->custom_ui_option = false;
86
87         while (cur != NULL) {
88                 if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar *)MA_TAG_ASSISTANT_LANGUAGE_SET)) {
89                         xmlNodePtr child_node = cur->xmlChildrenNode;
90                         while (child_node != NULL) {
91                                 if (child_node->name && 0 == xmlStrcmp(child_node->name, (const xmlChar*)MA_TAG_ASSISTANT_LANGUAGE)) {
92                                         key = xmlNodeGetContent(child_node);
93                                         if (key) {
94                                                 temp->supported_lang[temp->cnt_lang++] = strdup((const char*)key);
95                                                 MAS_LOGD("Language : %s", key);
96                                                 xmlFree(key);
97                                         }
98                                 }
99
100                                 child_node = child_node->next;
101                         }
102                 } else if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar *)MA_TAG_ASSISTANT_WAKEUP_WORD_SET)) {
103                         xmlNodePtr child_node = cur->xmlChildrenNode;
104                         while (child_node != NULL) {
105                                 if (child_node->name && 0 == xmlStrcmp(child_node->name, (const xmlChar*)MA_TAG_ASSISTANT_WAKEUP_WORD)) {
106                                         key = xmlNodeGetContent(child_node);
107                                         if (key) {
108                                                 temp->wakeup_list[temp->cnt_wakeup] = strdup((const char*)key);
109                                                 MAS_LOGD("Wakeup Word : %s", key);
110                                                 xmlFree(key);
111                                         }
112                                         xmlChar* prop = xmlNodeGetLang(child_node);
113                                         if (prop) {
114                                                 temp->wakeup_language[temp->cnt_wakeup] = strdup((const char*)prop);
115                                                 MAS_LOGD("Wakeup Language for %s : %s", temp->wakeup_list[temp->cnt_wakeup], prop);
116                                                 xmlFree(prop);
117                                         }
118                                         temp->cnt_wakeup++;
119                                 }
120
121                                 child_node = child_node->next;
122                         }
123                 } else if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar*)MA_TAG_ASSISTANT_NAME)) {
124                         key = xmlNodeGetContent(cur);
125                         if (key) {
126                                 temp->name = strdup((const char*)key);
127                                 MAS_LOGD("Name : %s", key);
128                                 xmlFree(key);
129                         }
130                 } else if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar*)MA_TAG_ASSISTANT_APPID)) {
131                         key = xmlNodeGetContent(cur);
132                         if (key) {
133                                 temp->app_id = strdup((const char*)key);
134                                 MAS_LOGD("ID : %s", key);
135                                 xmlFree(key);
136                         }
137                 } else if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar*)MA_TAG_ASSISTANT_ICON_PATH)) {
138                         key = xmlNodeGetContent(cur);
139                         if (key) {
140                                 temp->icon_path = strdup((const char*)key);
141                                 MAS_LOGD("Icon Path : %s", key);
142                                 xmlFree(key);
143                         }
144                 } else if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar*)MA_TAG_ASSISTANT_WAKEUP_ENGINE_APPID)) {
145                         key = xmlNodeGetContent(cur);
146                         if (key) {
147                                 temp->wakeup_engine = strdup((const char*)key);
148                                 MAS_LOGD("Wakeup Engine : %s", key);
149                                 xmlFree(key);
150                         }
151                 } else if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar*)MA_TAG_ASSISTANT_CUSTOM_UI)) {
152                         key = xmlNodeGetContent(cur);
153                         if (key) {
154                                 if (0 == xmlStrcasecmp(key, "true")) {
155                                         temp->custom_ui_option = true;
156                                 }
157                                 MAS_LOGD("Use custom UI : %d", temp->custom_ui_option);
158                                 xmlFree(key);
159                         }
160                 }
161
162                 cur = cur->next;
163         }
164
165         if (callback) {
166                 callback(temp->app_id, temp->name, temp->icon_path,
167                         temp->wakeup_list, temp->wakeup_language, temp->cnt_wakeup,
168                         temp->supported_lang, temp->cnt_lang,
169                         temp->wakeup_engine, temp->custom_ui_option, user_data);
170         }
171
172         if (temp->app_id) {
173                 free((void*)temp->app_id);
174         }
175         if (temp->name) {
176                 free((void*)temp->name);
177         }
178         if (temp->icon_path) {
179                 free((void*)temp->icon_path);
180         }
181         for (loop = 0; loop < temp->cnt_wakeup; loop++) {
182                 if (temp->wakeup_list[loop]) {
183                         free((void*)(temp->wakeup_list[loop]));
184                 }
185         }
186         for (loop = 0; loop < temp->cnt_lang; loop++) {
187                 if (temp->supported_lang[loop]) {
188                         free((void*)(temp->supported_lang[loop]));
189                 }
190         }
191         if (temp->wakeup_engine) {
192                 free((void*)temp->wakeup_engine);
193         }
194
195         free(temp);
196         xmlFreeDoc(doc);
197
198         return 0;
199 }
200
201 int mas_config_get_assistant_info(mas_config_assistant_info_cb callback, void* user_data)
202 {
203         const char *suffix = ".xml";
204
205         DIR *d;
206         struct dirent *dir;
207         d = opendir(MA_ASSISTANT_INFO);
208
209         if (d) {
210                 while (NULL != (dir = readdir(d))) {
211                         if (suffix && strlen(suffix) <= strlen(dir->d_name)) {
212                                 if (0 == strcmp(dir->d_name + strlen(dir->d_name) - strlen(suffix), suffix)) {
213                                         char fullpath[_POSIX_PATH_MAX];
214                                         snprintf(fullpath, _POSIX_PATH_MAX - 1, "%s/%s", MA_ASSISTANT_INFO, dir->d_name);
215                                         MAS_LOGD("Parsing file : %s\n", fullpath);
216                                         mas_config_parse_assistant_info(callback, fullpath, user_data);
217                                 }
218                         }
219                 }
220                 closedir(d);
221         }
222
223         return 0;
224 }