Update engine parser
[platform/core/uifw/stt.git] / engine-parser / src / stt-engine-parser.c
1 //
2 // Copyright (c) 2016 Samsung Electronics Co., Ltd.
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 <app_manager.h>
18 #include <dlog.h>
19 #include <errno.h>
20 #include <glib.h>
21 #include <libxml/parser.h>
22 #include <libxml/tree.h>
23 #include <pkgmgr-info.h>
24 #include <pkgmgr_installer_info.h>
25 #include <stdio.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <tzplatform_config.h>
29
30 /* Define EXPORT_API */
31 #ifndef EXPORT_API
32 #define EXPORT_API __attribute__((visibility("default")))
33 #endif
34
35 #ifdef LOG_TAG
36 #undef LOG_TAG
37 #endif
38 #define LOG_TAG "stt-engine-parser"
39
40 #define STT_TAG_ENGINE_BASE                     "stt-engine"
41 #define STT_TAG_ENGINE_NAME                     "name"
42 #define STT_TAG_ENGINE_ID                       "id"
43 #define STT_TAG_ENGINE_SETTING                  "setting"
44 #define STT_TAG_ENGINE_LANGUAGE_SET             "languages"
45 #define STT_TAG_ENGINE_LANGUAGE                 "lang"
46 #define STT_TAG_ENGINE_SILENCE_DETECTION_SUPPORT        "silence-detection-support"
47 #define STT_TAG_ENGINE_CREDENTIAL               "credential"
48
49 #define STT_CONFIG_BASE         tzplatform_mkpath(TZ_USER_HOME, "share/.voice")
50 #define STT_HOME                tzplatform_mkpath(TZ_USER_HOME, "share/.voice/stt")
51 #define STT_ENGINE_BASE         tzplatform_mkpath(TZ_USER_HOME, "share/.voice/stt/1.0")
52 #define STT_ENGINE_INFO         tzplatform_mkpath(TZ_USER_HOME, "/share/.voice/stt/1.0/engine-info")
53
54 #define STT_METADATA_LANGUAGE                   "http://tizen.org/metadata/stt-engine/language"
55 #define STT_METADATA_SILENCE_DETECTION          "http://tizen.org/metadata/stt-engine/silence-detection"
56 #define STT_METADATA_CREDENTIAL_REQUIRED        "http://tizen.org/metadata/stt-engine/credential-required"
57 #define STT_METADATA_ENGINE_SETTING             "http://tizen.org/metadata/stt-engine/setting"
58 #define STT_METADATA_ENGINE_NAME                "http://tizen.org/metadata/stt-engine/name"
59
60 typedef struct metadata {
61         const char *key;
62         const char *value;
63 } metadata;
64
65 static xmlDocPtr g_doc;
66
67 static int __create_engine_info_xml(const char *pkgid)
68 {
69         LOGD("=== Create engine info doc");
70         g_doc = xmlNewDoc((xmlChar*)"1.0");
71         if (NULL == g_doc) {
72                 LOGE("[ERROR] Fail to new doc");
73                 return -1;
74         }
75         LOGD("===");
76         return 0;
77 }
78
79 static int __save_engine_info_xml(const char *pkgid)
80 {
81         LOGD("=== Save engine info doc");
82         /* Make directories */
83         if (0 != access(STT_CONFIG_BASE, F_OK)) {
84                 if (0 != mkdir(STT_CONFIG_BASE, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
85                         LOGE("[ERROR] Fail to make directory : %s", STT_CONFIG_BASE);
86                         return -1;
87                 } else {
88                         LOGD("Success to make directory : %s", STT_CONFIG_BASE);
89                 }
90         }
91
92         if (0 != access(STT_HOME, F_OK)) {
93                 if (0 != mkdir(STT_HOME, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
94                         LOGE("[ERROR] Fail to make directory : %s", STT_HOME);
95                         return -1;
96                 } else {
97                         LOGD("Success to make directory : %s", STT_HOME);
98                 }
99         }
100
101         if (0 != access(STT_ENGINE_BASE, F_OK)) {
102                 if (0 != mkdir(STT_ENGINE_BASE, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
103                         LOGE("[ERROR] Fail to make directory : %s", STT_ENGINE_BASE);
104                         return -1;
105                 } else {
106                         LOGD("Success to make directory : %s", STT_ENGINE_BASE);
107                 }
108         }
109
110         if (0 != access(STT_ENGINE_INFO, F_OK)) {
111                 if (0 != mkdir(STT_ENGINE_INFO, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
112                         LOGE("[ERROR] Fail to make directory : %s", STT_ENGINE_INFO);
113                         return -1;
114                 } else {
115                         LOGD("Success to make directory : %s", STT_ENGINE_INFO);
116                 }
117         }
118
119         char path[256] = {'\0',};
120         snprintf(path, 256, "%s/%s.xml", STT_ENGINE_INFO, pkgid);
121         int ret = xmlSaveFormatFile(path, g_doc, 1);
122         LOGD("xmlSaveFile (%d)", ret);
123         LOGD("===");
124         return 0;
125 }
126
127 static int __remove_engine_info_xml(const char *pkgid)
128 {
129         LOGD("=== Remove engine info doc");
130         char path[256] = {'\0',};
131         snprintf(path, 256, "%s/%s.xml", STT_ENGINE_INFO, pkgid);
132         if (0 == access(path, F_OK)) {
133                 LOGD("Remove engine info xml(%s)", path);
134                 if (0 != remove(path)) {
135                         LOGE("[ERROR] Fail to emove engine info xml(%s)", path);
136                 }
137         }
138         LOGD("===");
139         return 0;
140 }
141
142 static void __insert_language_from_metadata(xmlNodePtr root, const char *language)
143 {
144         LOGD("==== Insert language");
145         char* lang = NULL;
146
147         char *tmp_lang, *tmp_free;
148         tmp_free = tmp_lang = strdup(language);
149         xmlNodePtr languages_node = NULL;
150         xmlNodePtr lang_node = NULL;
151
152         languages_node = xmlNewNode(NULL, (const xmlChar*)STT_TAG_ENGINE_LANGUAGE_SET);
153
154         lang = strsep(&tmp_lang, ",");
155         while (NULL != lang) {
156                 LOGD("lang (%s)", lang);
157                 lang_node = xmlNewNode(NULL, (const xmlChar*)STT_TAG_ENGINE_LANGUAGE);
158                 xmlNodeSetContent(lang_node, (const xmlChar*)lang);
159                 xmlAddChild(languages_node, lang_node);
160                 lang = strsep(&tmp_lang, ",");
161         }
162         xmlAddChild(root, languages_node);
163
164         free(tmp_free);
165 }
166
167 EXPORT_API
168 int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
169 {
170         LOGD("METADATA INSTALL");
171         LOGD("pkgid(%s) appid(%s) list(%d)", pkgid, appid, g_list_length(list));
172
173         uid_t uid = 0;
174         int ret = -1;
175         ret = pkgmgr_installer_info_get_target_uid(&uid);
176         if (ret < 0) {
177                 LOGE("[ERROR] Fail to get target uid");
178                 return 0;
179         } else {
180                 LOGD("uid(%d)", uid);
181         }
182
183         ret = tzplatform_set_user(uid);
184         if (ret < 0) {
185                 LOGE("[ERROR] Invalid uid");
186                 return 0;
187         } else {
188                 LOGD("TZ_USER_HOME: %s", tzplatform_mkstr(TZ_USER_HOME, "/"));
189         }
190
191         if (0 >= g_list_length(list)) {
192                 LOGE("[ERROR] No Engine Metadata");
193                 return 0;
194         }
195
196         GList *iter = NULL;
197         metadata *md = NULL;
198
199         __create_engine_info_xml(pkgid);
200
201         xmlNodePtr root = NULL;
202         xmlNodePtr cur = NULL;
203
204         root = xmlNewNode(NULL, (const xmlChar*)STT_TAG_ENGINE_BASE);
205         if (NULL == root) {
206                 LOGE("[ERROR] Fail to get new node");
207                 xmlFreeDoc(g_doc);
208                 return -1;
209         }
210         xmlDocSetRootElement(g_doc, root);
211
212         iter = g_list_first(list);
213         while (NULL != iter) {
214                 md = (metadata *)iter->data;
215                 if (NULL != md && NULL != md->key && NULL != md->value) {
216                         LOGD(" - key(%s) value(%s)", md->key, md->value);
217                         if (!strcmp(md->key, STT_METADATA_LANGUAGE)) {
218                                 __insert_language_from_metadata(root, md->value);
219                         } else if (!strcmp(md->key, STT_METADATA_SILENCE_DETECTION)) {
220                                 cur = xmlNewNode(NULL, (const xmlChar*)STT_TAG_ENGINE_SILENCE_DETECTION_SUPPORT);
221                                 xmlNodeSetContent(cur, (const xmlChar*)md->value);
222                                 xmlAddChild(root, cur);
223                         } else if (!strcmp(md->key, STT_METADATA_CREDENTIAL_REQUIRED)) {
224                                 cur = xmlNewNode(NULL, (const xmlChar*)STT_TAG_ENGINE_CREDENTIAL);
225                                 xmlNodeSetContent(cur, (const xmlChar*)md->value);
226                                 xmlAddChild(root, cur);
227                         } else if (!strcmp(md->key, STT_METADATA_ENGINE_SETTING)) {
228                                 cur = xmlNewNode(NULL, (const xmlChar*)STT_TAG_ENGINE_SETTING);
229                                 xmlNodeSetContent(cur, (const xmlChar*)md->value);
230                                 xmlAddChild(root, cur);
231                         } else if (!strcmp(md->key, STT_METADATA_ENGINE_NAME)) {
232                                 cur = xmlNewNode(NULL, (const xmlChar*)STT_TAG_ENGINE_NAME);
233                                 xmlNodeSetContent(cur, (const xmlChar*)md->value);
234                                 xmlAddChild(root, cur);
235                         } else {
236                                 LOGW("[WARNING] Unknown metadata type");
237                         }
238                 }
239                 iter = g_list_next(iter);
240         }
241
242         cur = xmlNewNode(NULL, (const xmlChar*)STT_TAG_ENGINE_ID);
243         xmlNodeSetContent(cur, (const xmlChar*)appid);
244         xmlAddChild(root, cur);
245
246         LOGD("");
247
248         if (0 != __save_engine_info_xml(pkgid)) {
249                 LOGE("[ERROR] Fail to make engine info file");
250                 return -1;
251         }
252
253         xmlFreeDoc(g_doc);
254
255         return 0;
256 }
257
258 EXPORT_API
259 int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
260 {
261         LOGD("METADATA UNINSTALL");
262         LOGD("pkgid(%s) appid(%s) list(%d)", pkgid, appid, g_list_length(list));
263
264         GList *iter = NULL;
265         metadata *md = NULL;
266
267         iter = g_list_first(list);
268         while (NULL != iter) {
269                 md = (metadata *)iter->data;
270                 LOGD(" - key(%s) value(%s)", md->key, md->value);
271                 iter = g_list_next(iter);
272         }
273
274         __remove_engine_info_xml(pkgid);
275
276         LOGD("");
277         return 0;
278 }
279
280 EXPORT_API
281 int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
282 {
283         LOGD("METADATA UPGRADE");
284         LOGD("pkgid(%s) appid(%s) list(%d)", pkgid, appid, g_list_length(list));
285
286         PKGMGR_MDPARSER_PLUGIN_UNINSTALL(pkgid, appid, list);
287         PKGMGR_MDPARSER_PLUGIN_INSTALL(pkgid, appid, list);
288
289         LOGD("");
290         return 0;
291 }