Add engine info parser
[platform/core/uifw/tts.git] / engine-parser / src / tts-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 "tts-engine-parser"
39
40 #define TTS_TAG_ENGINE_BASE                     "tts-engine"
41 #define TTS_TAG_ENGINE_NAME                     "name"
42 #define TTS_TAG_ENGINE_ID                       "id"
43 #define TTS_TAG_ENGINE_SETTING                  "setting"
44 #define TTS_TAG_ENGINE_VOICE_SET                "voices"
45 #define TTS_TAG_ENGINE_VOICE                    "voice"
46 #define TTS_TAG_ENGINE_VOICE_TYPE               "type"
47 #define TTS_TAG_ENGINE_PITCH_SUPPORT            "pitch-support"
48 #define TTS_TAG_ENGINE_CREDENTIAL               "credential"
49
50 #define TTS_CONFIG_BASE         tzplatform_mkpath(TZ_USER_HOME, "share/.voice")
51 #define TTS_HOME                tzplatform_mkpath(TZ_USER_HOME, "share/.voice/tts")
52 #define TTS_ENGINE_BASE         tzplatform_mkpath(TZ_USER_HOME, "share/.voice/tts/1.0")
53 #define TTS_ENGINE_INFO         tzplatform_mkpath(TZ_USER_SHARE, ".voice/tts/1.0/engine-info")
54
55 #define TTS_METADATA_LANGUAGE                   "http://tizen.org/metadata/tts-engine/language"
56 #define TTS_METADATA_CREDENTIAL_REQUIRED        "http://tizen.org/metadata/tts-engine/credential-required"
57
58
59 typedef struct metadata {
60         const char *key;
61         const char *value;
62 } metadata;
63
64 static xmlDocPtr g_doc;
65
66 static int __create_engine_info_xml(const char *pkgid)
67 {
68         LOGD("=== Create engine info doc");
69         g_doc = xmlNewDoc((xmlChar*)"1.0");
70         if (NULL == g_doc) {
71                 LOGE("[ERROR] Fail to new doc");
72                 return -1;
73         }
74         LOGD("===");
75         return 0;
76 }
77
78 static int __save_engine_info_xml(const char *pkgid)
79 {
80         LOGD("=== Save engine info doc");
81
82         /* Make directories */
83         if (0 != access(TTS_CONFIG_BASE, F_OK)) {
84                 if (0 != mkdir(TTS_CONFIG_BASE, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
85                         LOGE("[ERROR] Fail to make directory : %s", TTS_CONFIG_BASE);
86                         return -1;
87                 } else {
88                         LOGD("Success to make directory : %s", TTS_CONFIG_BASE);
89                 }
90         }
91
92         if (0 != access(TTS_HOME, F_OK)) {
93                 if (0 != mkdir(TTS_HOME, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
94                         LOGE("[ERROR] Fail to make directory : %s", TTS_HOME);
95                         return -1;
96                 } else {
97                         LOGD("Success to make directory : %s", TTS_HOME);
98                 }
99         }
100
101         if (0 != access(TTS_ENGINE_BASE, F_OK)) {
102                 if (0 != mkdir(TTS_ENGINE_BASE, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
103                         LOGE("[ERROR] Fail to make directory : %s", TTS_ENGINE_BASE);
104                         return -1;
105                 } else {
106                         LOGD("Success to make directory : %s", TTS_ENGINE_BASE);
107                 }
108         }
109
110         if (0 != access(TTS_ENGINE_INFO, F_OK)) {
111                 if (0 != mkdir(TTS_ENGINE_INFO, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
112                         LOGE("[ERROR] Fail to make directory : %s", TTS_ENGINE_INFO);
113                         return -1;
114                 } else {
115                         LOGD("Success to make directory : %s", TTS_ENGINE_INFO);
116                 }
117         }
118
119         char path[256] = {'\0',};
120         snprintf(path, 256, "%s/%s.xml", TTS_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", TTS_ENGINE_INFO, pkgid);
132         if (0 == access(path, F_OK)) {
133                 LOGD("Remove engine info xml(%s)", path);
134                 remove(path);
135         }
136         LOGD("===");
137         return 0;
138 }
139
140 static void __insert_language_from_metadata(xmlNodePtr root, const char *language)
141 {
142         LOGD("==== Insert language");
143         char* voice = NULL;
144         char* lang = NULL;
145         char* type = NULL;
146
147         char *tmp_lang = NULL;
148         char *tmp_free = NULL;
149         tmp_free = tmp_lang = strdup(language);
150         if (NULL != tmp_lang) {
151                 LOGE("Fail to memory allocation");
152                 return;
153         }
154         xmlNodePtr voices_node = NULL;
155         xmlNodePtr voice_node = NULL;
156
157         voices_node = xmlNewNode(NULL, (const xmlChar*)TTS_TAG_ENGINE_VOICE_SET);
158
159         voice = strsep(&tmp_lang, ",");
160         while(NULL != voice) {
161                 LOGD("voice (%s)", voice);
162                 voice_node = xmlNewNode(NULL, (const xmlChar*)TTS_TAG_ENGINE_VOICE);
163                 lang = strsep(&voice, ":");
164                 LOGD("lang (%s)", lang);
165                 type = strsep(&voice, " ");
166                 LOGD("type (%s)", type);
167                 xmlSetProp(voice_node, (const xmlChar*)TTS_TAG_ENGINE_VOICE_TYPE, (const xmlChar*)type);
168                 xmlNodeSetContent(voice_node, (const xmlChar*)lang);
169                 xmlAddChild(voices_node, voice_node);
170                 voice = strsep(&tmp_lang, ",");
171         }
172         xmlAddChild(root, voices_node);
173
174         free(tmp_free);
175         tmp_free = NULL;
176 }
177
178 EXPORT_API
179 int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
180 {
181         LOGD("METADATA INSTALL");
182         LOGD("pkgid(%s) appid(%s) list(%d)", pkgid, appid, g_list_length(list));
183
184         uid_t uid = 0;
185         int ret = -1;
186         ret = pkgmgr_installer_info_get_target_uid(&uid);
187         if (ret < 0 ) {
188                 LOGE("[ERROR] Fail to get target uid");
189                 return 0;
190         } else {
191                 LOGD("uid(%d)", uid);
192         }
193
194         ret = tzplatform_set_user(uid);
195         if (ret < 0) {
196                 LOGE("[ERROR] Invalid uid");
197                 return 0;
198         } else {
199                 LOGD("TZ_USER_HOME: %s", tzplatform_mkstr(TZ_USER_HOME, "/"));
200         }
201
202         if (0 >= g_list_length(list)) {
203                 LOGE("[ERROR] No Engine Metadata");
204                 return 0;
205         }
206
207         GList *iter = NULL;
208         metadata *md = NULL;
209
210         __create_engine_info_xml(pkgid);
211
212         xmlNodePtr root = NULL;
213         xmlNodePtr cur = NULL;
214
215         root = xmlNewNode(NULL, (const xmlChar*)TTS_TAG_ENGINE_BASE);
216         if (NULL == root) {
217                 LOGE("[ERROR] Fail to get new node");
218                 xmlFreeDoc(g_doc);
219                 return -1;
220         }
221         xmlDocSetRootElement(g_doc, root);
222
223         /* Save name */
224         cur = xmlNewNode(NULL, (const xmlChar*)TTS_TAG_ENGINE_NAME);
225         xmlNodeSetContent(cur, (const xmlChar*)pkgid);
226         xmlAddChild(root, cur);
227
228         iter = g_list_first(list);
229         while (NULL != iter) {
230                 md = (metadata *)iter->data;
231                 if (NULL != md && NULL != md->key) {
232                         LOGD(" - key(%s) value(%s)", md->key, md->value);
233                         if (!strcmp(md->key, TTS_METADATA_LANGUAGE)) {
234                                 __insert_language_from_metadata(root, md->value);
235                         } else if (!strcmp(md->key, TTS_METADATA_CREDENTIAL_REQUIRED)) {
236                                 cur = xmlNewNode(NULL, (const xmlChar*)TTS_TAG_ENGINE_CREDENTIAL);
237                                 xmlNodeSetContent(cur, (const xmlChar*)md->value);
238                                 xmlAddChild(root, cur);
239                         } else {
240                                 LOGW("[WARNING] Unknown metadata type");
241                         }
242                 }
243                 iter = g_list_next(iter);
244         }
245
246         cur = xmlNewNode(NULL, (const xmlChar*)TTS_TAG_ENGINE_ID);
247         xmlNodeSetContent(cur, (const xmlChar*)pkgid);
248         xmlAddChild(root, cur);
249
250         LOGD("");
251
252         __save_engine_info_xml(pkgid);
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                 if (NULL != md && NULL != md->key) {
271                         LOGD(" - key(%s) value(%s)", md->key, md->value);
272                 }
273                 iter = g_list_next(iter);
274         }
275
276         __remove_engine_info_xml(pkgid);
277
278         LOGD("");
279         return 0;
280 }
281
282 EXPORT_API
283 int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
284 {
285         LOGD("METADATA UPGRADE");
286         LOGD("pkgid(%s) appid(%s) list(%d)", pkgid, appid, g_list_length(list));
287
288         PKGMGR_MDPARSER_PLUGIN_UNINSTALL(pkgid, appid, list);
289         PKGMGR_MDPARSER_PLUGIN_INSTALL(pkgid, appid, list);
290
291         LOGD("");
292         return 0;
293 }