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 "stt-engine-parser"
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"
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")
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"
60 typedef struct metadata {
65 static xmlDocPtr g_doc;
67 static int __create_engine_info_xml(const char *pkgid)
69 LOGD("=== Create engine info doc");
70 g_doc = xmlNewDoc((xmlChar*)"1.0");
72 LOGE("[ERROR] Fail to new doc");
79 static int __save_engine_info_xml(const char *pkgid)
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);
88 LOGD("Success to make directory : %s", STT_CONFIG_BASE);
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);
97 LOGD("Success to make directory : %s", STT_HOME);
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);
106 LOGD("Success to make directory : %s", STT_ENGINE_BASE);
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);
115 LOGD("Success to make directory : %s", STT_ENGINE_INFO);
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);
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", 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);
142 static void __insert_language_from_metadata(xmlNodePtr root, const char *language)
144 LOGD("==== Insert language");
147 char *tmp_lang, *tmp_free;
148 tmp_free = tmp_lang = strdup(language);
149 xmlNodePtr languages_node = NULL;
150 xmlNodePtr lang_node = NULL;
152 languages_node = xmlNewNode(NULL, (const xmlChar*)STT_TAG_ENGINE_LANGUAGE_SET);
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, ",");
162 xmlAddChild(root, languages_node);
168 int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
170 LOGD("METADATA INSTALL");
171 LOGD("pkgid(%s) appid(%s) list(%d)", pkgid, appid, g_list_length(list));
175 ret = pkgmgr_installer_info_get_target_uid(&uid);
177 LOGE("[ERROR] Fail to get target uid");
180 LOGD("uid(%d)", uid);
183 ret = tzplatform_set_user(uid);
185 LOGE("[ERROR] Invalid uid");
188 LOGD("TZ_USER_HOME: %s", tzplatform_mkstr(TZ_USER_HOME, "/"));
191 if (0 >= g_list_length(list)) {
192 LOGE("[ERROR] No Engine Metadata");
199 __create_engine_info_xml(pkgid);
201 xmlNodePtr root = NULL;
202 xmlNodePtr cur = NULL;
204 root = xmlNewNode(NULL, (const xmlChar*)STT_TAG_ENGINE_BASE);
206 LOGE("[ERROR] Fail to get new node");
210 xmlDocSetRootElement(g_doc, root);
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);
236 LOGW("[WARNING] Unknown metadata type");
239 iter = g_list_next(iter);
242 cur = xmlNewNode(NULL, (const xmlChar*)STT_TAG_ENGINE_ID);
243 xmlNodeSetContent(cur, (const xmlChar*)appid);
244 xmlAddChild(root, cur);
248 if (0 != __save_engine_info_xml(pkgid)) {
249 LOGE("[ERROR] Fail to make engine info file");
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 LOGD(" - key(%s) value(%s)", md->key, md->value);
271 iter = g_list_next(iter);
274 __remove_engine_info_xml(pkgid);
281 int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
283 LOGD("METADATA UPGRADE");
284 LOGD("pkgid(%s) appid(%s) list(%d)", pkgid, appid, g_list_length(list));
286 PKGMGR_MDPARSER_PLUGIN_UNINSTALL(pkgid, appid, list);
287 PKGMGR_MDPARSER_PLUGIN_INSTALL(pkgid, appid, list);