Add logic to set default language of the engine
[platform/core/uifw/stt.git] / common / stt_config_parser.c
index 8116ba4..6a7925d 100644 (file)
@@ -1,5 +1,5 @@
 /*
-*  Copyright (c) 2011-2014 Samsung Electronics Co., Ltd All Rights Reserved
+*  Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
@@ -23,6 +23,7 @@
 #define STT_TAG_ENGINE_ID              "id"
 #define STT_TAG_ENGINE_SETTING         "setting"
 #define STT_TAG_ENGINE_AGREEMENT       "agreement"
+#define STT_TAG_ENGINE_DEFAULT         "default"
 #define STT_TAG_ENGINE_LANGUAGE_SET    "languages"
 #define STT_TAG_ENGINE_LANGUAGE                "lang"
 #define STT_TAG_ENGINE_SILENCE_SUPPORT "silence-detection-support"
@@ -98,10 +99,13 @@ int stt_parser_get_engine_info(const char* path, stt_engine_info_s** engine_info
        temp->uuid = NULL;
        temp->setting = NULL;
        temp->agreement = NULL;
+       temp->default_lang = NULL;
        temp->languages = NULL;
        temp->support_silence_detection = false;
        temp->need_credential = false;
 
+       bool is_default_lang_set = false;
+
        while (cur != NULL) {
                if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_NAME)) {
                        key = xmlNodeGetContent(cur);
@@ -143,6 +147,18 @@ int stt_parser_get_engine_info(const char* path, stt_engine_info_s** engine_info
                        } else {
                                SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_ENGINE_AGREEMENT);
                        }
+               } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_DEFAULT)) {
+                       key = xmlNodeGetContent(cur);
+                       if (NULL != key) {
+                               SLOG(LOG_DEBUG, stt_tag(), "Engine agreement : %s", (char *)key);
+                               if (NULL != temp->default_lang) free(temp->default_lang);
+                               temp->default_lang = strdup((char*)key);
+
+                               is_default_lang_set = true;
+                               xmlFree(key);
+                       } else {
+                               SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_ENGINE_DEFAULT);
+                       }
                } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_LANGUAGE_SET)) {
                        xmlNodePtr lang_node = NULL;
                        char* temp_lang = NULL;
@@ -156,6 +172,13 @@ int stt_parser_get_engine_info(const char* path, stt_engine_info_s** engine_info
                                                /* SLOG(LOG_DEBUG, stt_tag(), "language : %s", (char *)key); */
                                                temp_lang = strdup((char*)key);
                                                temp->languages = g_slist_append(temp->languages, temp_lang);
+
+                                               if (false == is_default_lang_set) {
+                                                       if (NULL != temp->default_lang) free(temp->default_lang);
+                                                       temp->default_lang = strdup((char*)key);
+
+                                                       is_default_lang_set = true;
+                                               }
                                                xmlFree(key);
                                        } else {
                                                SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_ENGINE_LANGUAGE);
@@ -437,9 +460,18 @@ int stt_parser_unload_config(stt_config_s* config_info)
 {
        if (NULL != g_config_doc)       xmlFreeDoc(g_config_doc);
        if (NULL != config_info) {
-               if (NULL != config_info->engine_id)     free(config_info->engine_id);
-               if (NULL != config_info->setting)       free(config_info->setting);
-               if (NULL != config_info->language)      free(config_info->language);
+               if (NULL != config_info->engine_id) {
+                       free(config_info->engine_id);
+                       config_info->engine_id = NULL;
+               }
+               if (NULL != config_info->setting) {
+                       free(config_info->setting);
+                       config_info->setting = NULL;
+               }
+               if (NULL != config_info->language) {
+                       free(config_info->language);
+                       config_info->language = NULL;
+               }
 
                free(config_info);
        }
@@ -859,7 +891,7 @@ int stt_parser_set_time_info(GSList* time_list)
 
                xmlNodePtr temp_node = NULL;
 
-               SLOG(LOG_DEBUG, stt_tag(), "[%d] i(%d) t(%s) s(%d) e(%d)",
+               SLOG(LOG_DEBUG, stt_tag(), "[%d] i(%d) t(%s) s(%ld) e(%ld)",
                        data->index, data->event, data->text, data->start_time, data->end_time);
 
                temp_node = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_TEXT);
@@ -887,6 +919,20 @@ int stt_parser_set_time_info(GSList* time_list)
        return 0;
 }
 
+void __stt_parser_time_info_free(void* data)
+{
+       stt_result_time_info_s* time_info = (stt_result_time_info_s*)data;
+
+       if (NULL != time_info) {
+               if (NULL != time_info->text) {
+                       free(time_info->text);
+                       time_info->text = NULL;
+               }
+
+               free(time_info);
+       }
+}
+
 int stt_parser_get_time_info(GSList** time_list)
 {
        if (NULL == time_list) {
@@ -933,6 +979,12 @@ int stt_parser_get_time_info(GSList** time_list)
                        key = xmlGetProp(cur, (const xmlChar*)STT_TAG_TIME_COUNT);
                        if (NULL == key) {
                                SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_TIME_COUNT);
+
+                               if (NULL != temp_time_list) {
+                                       g_slist_free_full(temp_time_list, __stt_parser_time_info_free);
+                                       temp_time_list = NULL;
+                               }
+                               xmlFreeDoc(doc);
                                return -1;
                        }
 
@@ -961,6 +1013,12 @@ int stt_parser_get_time_info(GSList** time_list)
 
                                if (NULL == temp_info) {
                                        SLOG(LOG_ERROR, stt_tag(), "[ERROR] Memory alloc error!!");
+
+                                       if (NULL != temp_time_list) {
+                                               g_slist_free_full(temp_time_list, __stt_parser_time_info_free);
+                                               temp_time_list = NULL;
+                                       }
+                                       xmlFreeDoc(doc);
                                        return -1;
                                }