Add parsing extra voice info xml 33/183333/2
authorsooyeon.kim <sooyeon.kim@samsung.com>
Wed, 4 Jul 2018 12:19:51 +0000 (21:19 +0900)
committersooyeon.kim <sooyeon.kim@samsung.com>
Thu, 5 Jul 2018 09:35:40 +0000 (18:35 +0900)
Change-Id: I132f9e12bb871f136dd271e1e97553531525b956
Signed-off-by: sooyeon.kim <sooyeon.kim@samsung.com>
engine-parser/src/tts-engine-parser.c

index 802f6e5..0f6c34d 100644 (file)
 #define TTS_TAG_ENGINE_CREDENTIAL              "credential"
 #define TTS_TAG_ENGINE_TEXT_SIZE               "text-size"
 
+#define TTS_TAG_VOICE_BASE                     "tts-voice"
+
 #define TTS_CONFIG_BASE                tzplatform_mkpath(tzplatform_getid("TZ_USER_HOME"), "share/.voice")
 #define TTS_HOME               tzplatform_mkpath(tzplatform_getid("TZ_USER_HOME"), "share/.voice/tts")
 #define TTS_ENGINE_BASE                tzplatform_mkpath(tzplatform_getid("TZ_USER_HOME"), "share/.voice/tts/1.0")
 #define TTS_ENGINE_INFO                tzplatform_mkpath(tzplatform_getid("TZ_USER_SHARE"), ".voice/tts/1.0/engine-info")
 
+#define TTS_VOICE_INFO tzplatform_mkpath(tzplatform_getid("TZ_SYS_RO_SHARE"), "voice/tts/1.0/tts-voice.xml")
+
 #define TTS_GLOBAL_CONFIG_BASE         "/etc/skel/share/.voice"
 #define TTS_GLOBAL_HOME                "/etc/skel/share/.voice/tts"
 #define TTS_GLOBAL_ENGINE_BASE         "/etc/skel/share/.voice/tts/1.0"
@@ -82,6 +86,16 @@ typedef struct metadata {
        const char *value;
 } metadata;
 
+typedef struct {
+       char *lang;
+       char *type;
+} voice_info_s;
+
+typedef struct {
+       voice_info_s *voice_info;
+       int size;
+} voice_info_list_s;
+
 static xmlDocPtr g_doc;
 GumUser *g_guser = NULL;
 uid_t g_uid = 301;     // app_fw
@@ -94,6 +108,7 @@ char *g_dir_home = NULL;
 char *g_dir_engine_base = NULL;
 char *g_dir_engine_info = NULL;
 
+GSList *g_voice_info_list = NULL;
 
 static int __create_engine_info_xml(const char *pkgid)
 {
@@ -360,11 +375,156 @@ static void __insert_language_from_metadata(xmlNodePtr root, const char *languag
                xmlAddChild(voices_node, voice_node);
                voice = strsep(&tmp_lang, ",");
        }
+
+       /* add extra voices */
+       LOGD("Add extra voices");
+       if (NULL != g_voice_info_list) {
+               GSList *iter;
+               voice_info_s *extra_voice = NULL;
+               for (iter = g_voice_info_list ; iter != NULL ; iter = g_slist_next(iter)) {
+                       extra_voice = (voice_info_s*)iter->data;
+                       if (NULL != extra_voice) {
+                               lang = strdup(extra_voice->lang);
+                               type = strdup(extra_voice->type);
+                               LOGD("lang(%s), type(%s)", lang, type);
+
+                               if (NULL != lang) {
+                                       voice_node = xmlNewNode(NULL, (const xmlChar*)TTS_TAG_ENGINE_VOICE);
+                                       xmlSetProp(voice_node, (const xmlChar*)TTS_TAG_ENGINE_VOICE_TYPE, (const xmlChar*)type);
+                                       xmlNodeSetContent(voice_node, (const xmlChar*)lang);
+                                       xmlAddChild(voices_node, voice_node);
+                               }
+
+                               LOGD("Finish to add voice node");
+
+                               if (NULL != lang) {
+                                       free(lang);
+                               }
+                               if (NULL != type) {
+                                       free(type);
+                               }
+                               LOGD("Memory release");
+
+                               if (NULL != extra_voice->lang) {
+                                       free(extra_voice->lang);
+                               }
+                               if (NULL != extra_voice->type) {
+                                       free(extra_voice->type);
+                               }
+                               LOGD("Free extra_voice");
+                               free(extra_voice);
+                               extra_voice = NULL;
+                       }
+               }
+
+               LOGD("Free voice info list");
+               g_slist_free(g_voice_info_list);
+               g_voice_info_list = NULL;
+       }
+
        xmlAddChild(root, voices_node);
 
        FREE(tmp_free)
 }
 
+static int __get_voice_inxml()
+{
+       xmlDocPtr doc = NULL;
+       xmlNodePtr cur = NULL;
+
+       LOGD("TTS voice info xml (%s)", TTS_VOICE_INFO);
+
+       if (0 != access(TTS_VOICE_INFO, F_OK)) {
+               LOGD("There is no extra voice info.");
+               return 0;
+       } else {
+               doc = xmlParseFile(TTS_VOICE_INFO);
+               if (NULL == doc) {
+                       LOGE("Fail to parse file");
+                       return -1;
+               }
+
+               cur = xmlDocGetRootElement(doc);
+               if (NULL == cur) {
+                       LOGE("Empty document");
+                       xmlFreeDoc(doc);
+                       return -1;
+               }
+
+               if (xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_VOICE_BASE)) {
+                       LOGE("root node is NOT %s", TTS_TAG_VOICE_BASE);
+                       xmlFreeDoc(doc);
+                       return -1;
+               }
+
+               cur = cur->xmlChildrenNode;
+               if (NULL == cur) {
+                       LOGE("<tts-voice> child NULL");
+                       xmlFreeDoc(doc);
+                       return -1;
+               }
+
+               xmlChar *attr = NULL;
+               xmlChar *key = NULL;
+//             char lang[16];
+//             char type[16];
+
+               while (NULL != cur) {
+                       if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_VOICE_SET)) {
+                               LOGD("<voices>");
+                               xmlNodePtr voice_node = NULL;
+                               voice_node = cur->xmlChildrenNode;
+
+                               while (NULL != voice_node) {
+                                       if (0 == xmlStrcmp(voice_node->name, (const xmlChar *)TTS_TAG_ENGINE_VOICE)) {
+                                               voice_info_s *voice_info = (voice_info_s *)calloc(1, sizeof(voice_info_s));
+                                               if (NULL == voice_info) {
+                                                       LOGE("Fail to allocate memory");
+                                                       xmlFreeDoc(doc);
+                                                       return -1;
+                                               }
+
+                                               LOGD("Get property and keys");
+                                               attr = xmlGetProp(voice_node, (const xmlChar *)TTS_TAG_ENGINE_VOICE_TYPE);
+                                               if (NULL != attr) {
+//                                                     strncpy(type, (char*)attr, strlen((char*)attr));
+                                                       voice_info->type = strdup((char*)attr);
+                                                       LOGD("type(%s)", voice_info->type);
+                                                       xmlFree(attr);
+                                                       attr = NULL;
+                                               } else {
+                                                       LOGD("No voice type");
+                                               }
+
+                                               key = xmlNodeGetContent(voice_node);
+                                               if (NULL != key) {
+//                                                     strncpy(lang, (char*)key, strlen((char*)key));
+                                                       voice_info->lang = strdup((char*)key);
+                                                       LOGD("lang(%s)", voice_info->lang);
+                                                       xmlFree(key);
+                                                       key = NULL;
+                                               } else {
+                                                       LOGD("No voice info");
+                                               }
+
+                                               g_voice_info_list = g_slist_append(g_voice_info_list, voice_info);
+                                       }
+
+                                       voice_node = voice_node->next;
+                               }
+                       } else {
+                               LOGD("Unknown tag (%s)", (const char*)cur->name);
+                       }
+
+                       cur = cur->next;
+               }
+
+               xmlFreeDoc(doc);
+       }
+
+       return 0;
+}
+
 static int __write_metadata_inxml(const char *pkgid, const char *appid, GList *list)
 {
        GList *iter = NULL;
@@ -397,6 +557,7 @@ static int __write_metadata_inxml(const char *pkgid, const char *appid, GList *l
                if (NULL != md && NULL != md->key) {
                        LOGD(" - key(%s) value(%s)", md->key, md->value);
                        if (!strcmp(md->key, TTS_METADATA_LANGUAGE)) {
+                               __get_voice_inxml();
                                __insert_language_from_metadata(root, md->value);
                        } else if (!strcmp(md->key, TTS_METADATA_CREDENTIAL_REQUIRED)) {
                                cur = xmlNewNode(NULL, (const xmlChar*)TTS_TAG_ENGINE_CREDENTIAL);