2 // Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <app_manager.h>
21 #include <libxml/parser.h>
22 #include <libxml/tree.h>
23 #include <pkgmgr-info.h>
24 #include <pkgmgr_installer_info.h>
27 #include <sys/types.h>
28 #include <tzplatform_config.h>
30 /* Define EXPORT_API */
32 #define EXPORT_API __attribute__((visibility("default")))
38 #define LOG_TAG "tts-engine-parser"
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"
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")
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"
59 typedef struct metadata {
64 static xmlDocPtr g_doc;
66 static int __create_engine_info_xml(const char *pkgid)
68 LOGD("=== Create engine info doc");
69 g_doc = xmlNewDoc((xmlChar*)"1.0");
71 LOGE("[ERROR] Fail to new doc");
78 static int __save_engine_info_xml(const char *pkgid)
80 LOGD("=== Save engine info doc");
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);
88 LOGD("Success to make directory : %s", TTS_CONFIG_BASE);
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);
97 LOGD("Success to make directory : %s", TTS_HOME);
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);
106 LOGD("Success to make directory : %s", TTS_ENGINE_BASE);
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);
115 LOGD("Success to make directory : %s", TTS_ENGINE_INFO);
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);
127 static int __remove_engine_info_xml(const char *pkgid)
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);
140 static void __insert_language_from_metadata(xmlNodePtr root, const char *language)
142 LOGD("==== Insert language");
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");
154 xmlNodePtr voices_node = NULL;
155 xmlNodePtr voice_node = NULL;
157 voices_node = xmlNewNode(NULL, (const xmlChar*)TTS_TAG_ENGINE_VOICE_SET);
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, ",");
172 xmlAddChild(root, voices_node);
179 int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
181 LOGD("METADATA INSTALL");
182 LOGD("pkgid(%s) appid(%s) list(%d)", pkgid, appid, g_list_length(list));
186 ret = pkgmgr_installer_info_get_target_uid(&uid);
188 LOGE("[ERROR] Fail to get target uid");
191 LOGD("uid(%d)", uid);
194 ret = tzplatform_set_user(uid);
196 LOGE("[ERROR] Invalid uid");
199 LOGD("TZ_USER_HOME: %s", tzplatform_mkstr(TZ_USER_HOME, "/"));
202 if (0 >= g_list_length(list)) {
203 LOGE("[ERROR] No Engine Metadata");
210 __create_engine_info_xml(pkgid);
212 xmlNodePtr root = NULL;
213 xmlNodePtr cur = NULL;
215 root = xmlNewNode(NULL, (const xmlChar*)TTS_TAG_ENGINE_BASE);
217 LOGE("[ERROR] Fail to get new node");
221 xmlDocSetRootElement(g_doc, root);
224 cur = xmlNewNode(NULL, (const xmlChar*)TTS_TAG_ENGINE_NAME);
225 xmlNodeSetContent(cur, (const xmlChar*)pkgid);
226 xmlAddChild(root, cur);
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);
240 LOGW("[WARNING] Unknown metadata type");
243 iter = g_list_next(iter);
246 cur = xmlNewNode(NULL, (const xmlChar*)TTS_TAG_ENGINE_ID);
247 xmlNodeSetContent(cur, (const xmlChar*)pkgid);
248 xmlAddChild(root, cur);
252 __save_engine_info_xml(pkgid);
259 int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
261 LOGD("METADATA UNINSTALL");
262 LOGD("pkgid(%s) appid(%s) list(%d)", pkgid, appid, g_list_length(list));
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);
273 iter = g_list_next(iter);
276 __remove_engine_info_xml(pkgid);
283 int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
285 LOGD("METADATA UPGRADE");
286 LOGD("pkgid(%s) appid(%s) list(%d)", pkgid, appid, g_list_length(list));
288 PKGMGR_MDPARSER_PLUGIN_UNINSTALL(pkgid, appid, list);
289 PKGMGR_MDPARSER_PLUGIN_INSTALL(pkgid, appid, list);