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"
58 typedef struct metadata {
63 static xmlDocPtr g_doc;
65 static int __create_engine_info_xml(const char *pkgid)
67 LOGD("=== Create engine info doc");
68 g_doc = xmlNewDoc((xmlChar*)"1.0");
70 LOGE("[ERROR] Fail to new doc");
77 static int __save_engine_info_xml(const char *pkgid)
79 LOGD("=== Save engine info doc");
80 /* Make directories */
81 if (0 != access(STT_CONFIG_BASE, F_OK)) {
82 if (0 != mkdir(STT_CONFIG_BASE, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
83 LOGE("[ERROR] Fail to make directory : %s", STT_CONFIG_BASE);
86 LOGD("Success to make directory : %s", STT_CONFIG_BASE);
90 if (0 != access(STT_HOME, F_OK)) {
91 if (0 != mkdir(STT_HOME, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
92 LOGE("[ERROR] Fail to make directory : %s", STT_HOME);
95 LOGD("Success to make directory : %s", STT_HOME);
99 if (0 != access(STT_ENGINE_BASE, F_OK)) {
100 if (0 != mkdir(STT_ENGINE_BASE, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
101 LOGE("[ERROR] Fail to make directory : %s", STT_ENGINE_BASE);
104 LOGD("Success to make directory : %s", STT_ENGINE_BASE);
108 if (0 != access(STT_ENGINE_INFO, F_OK)) {
109 if (0 != mkdir(STT_ENGINE_INFO, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
110 LOGE("[ERROR] Fail to make directory : %s", STT_ENGINE_INFO);
113 LOGD("Success to make directory : %s", STT_ENGINE_INFO);
117 char path[256] = {'\0',};
118 snprintf(path, 256, "%s/%s.xml", STT_ENGINE_INFO, pkgid);
119 int ret = xmlSaveFormatFile(path, g_doc, 1);
120 LOGD("xmlSaveFile (%d)", ret);
125 static int __remove_engine_info_xml(const char *pkgid)
127 LOGD("=== Remove engine info doc");
128 char path[256] = {'\0',};
129 snprintf(path, 256, "%s/%s.xml", STT_ENGINE_INFO, pkgid);
130 if (0 == access(path, F_OK)) {
131 LOGD("Remove engine info xml(%s)", path);
138 static void __insert_language_from_metadata(xmlNodePtr root, const char *language)
140 LOGD("==== Insert language");
143 char *tmp_lang, *tmp_free;
144 tmp_free = tmp_lang = strdup(language);
145 xmlNodePtr languages_node = NULL;
146 xmlNodePtr lang_node = NULL;
148 languages_node = xmlNewNode(NULL, (const xmlChar*)STT_TAG_ENGINE_LANGUAGE_SET);
150 lang = strsep(&tmp_lang, ",");
151 while(NULL != lang) {
152 LOGD("lang (%s)", lang);
153 lang_node = xmlNewNode(NULL, (const xmlChar*)STT_TAG_ENGINE_LANGUAGE);
154 xmlNodeSetContent(lang_node, (const xmlChar*)lang);
155 xmlAddChild(languages_node, lang_node);
156 lang = strsep(&tmp_lang, ",");
158 xmlAddChild(root, languages_node);
164 int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
166 LOGD("METADATA INSTALL");
167 LOGD("pkgid(%s) appid(%s) list(%d)", pkgid, appid, g_list_length(list));
171 ret = pkgmgr_installer_info_get_target_uid(&uid);
173 LOGE("[ERROR] Fail to get target uid");
176 LOGD("uid(%d)", uid);
179 ret = tzplatform_set_user(uid);
181 LOGE("[ERROR] Invalid uid");
184 LOGD("TZ_USER_HOME: %s", tzplatform_mkstr(TZ_USER_HOME, "/"));
187 if (0 >= g_list_length(list)) {
188 LOGE("[ERROR] No Engine Metadata");
195 __create_engine_info_xml(pkgid);
197 xmlNodePtr root = NULL;
198 xmlNodePtr cur = NULL;
200 root = xmlNewNode(NULL, (const xmlChar*)STT_TAG_ENGINE_BASE);
202 LOGE("[ERROR] Fail to get new node");
206 xmlDocSetRootElement(g_doc, root);
209 cur = xmlNewNode(NULL, (const xmlChar*)STT_TAG_ENGINE_NAME);
210 xmlNodeSetContent(cur, (const xmlChar*)pkgid);
211 xmlAddChild(root, cur);
213 iter = g_list_first(list);
214 while (NULL != iter) {
215 md = (metadata *)iter->data;
216 if (NULL != md && NULL != md->key && NULL != md->value) {
217 LOGD(" - key(%s) value(%s)", md->key, md->value);
218 if (!strcmp(md->key, STT_METADATA_LANGUAGE)) {
219 __insert_language_from_metadata(root, md->value);
220 } else if (!strcmp(md->key, STT_METADATA_SILENCE_DETECTION)) {
221 cur = xmlNewNode(NULL, (const xmlChar*)STT_TAG_ENGINE_SILENCE_DETECTION_SUPPORT);
222 xmlNodeSetContent(cur, (const xmlChar*)md->value);
223 xmlAddChild(root, cur);
224 } else if (!strcmp(md->key, STT_METADATA_CREDENTIAL_REQUIRED)) {
225 cur = xmlNewNode(NULL, (const xmlChar*)STT_TAG_ENGINE_CREDENTIAL);
226 xmlNodeSetContent(cur, (const xmlChar*)md->value);
227 xmlAddChild(root, cur);
229 LOGW("[WARNING] Unknown metadata type");
232 iter = g_list_next(iter);
235 cur = xmlNewNode(NULL, (const xmlChar*)STT_TAG_ENGINE_ID);
236 xmlNodeSetContent(cur, (const xmlChar*)appid);
237 xmlAddChild(root, cur);
241 if (0 != __save_engine_info_xml(pkgid)) {
242 LOGE("[ERROR] Fail to make engine info file");
252 int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
254 LOGD("METADATA UNINSTALL");
255 LOGD("pkgid(%s) appid(%s) list(%d)", pkgid, appid, g_list_length(list));
260 iter = g_list_first(list);
261 while (NULL != iter) {
262 md = (metadata *)iter->data;
263 LOGD(" - key(%s) value(%s)", md->key, md->value);
264 iter = g_list_next(iter);
267 __remove_engine_info_xml(pkgid);
274 int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
276 LOGD("METADATA UPGRADE");
277 LOGD("pkgid(%s) appid(%s) list(%d)", pkgid, appid, g_list_length(list));
279 PKGMGR_MDPARSER_PLUGIN_UNINSTALL(pkgid, appid, list);
280 PKGMGR_MDPARSER_PLUGIN_INSTALL(pkgid, appid, list);