Refactor logic for automatically setting voice 95/289395/2
authorSuyeon Hwang <stom.hwang@samsung.com>
Thu, 23 Feb 2023 06:47:49 +0000 (15:47 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Tue, 7 Mar 2023 06:18:14 +0000 (15:18 +0900)
- Issue:
The logic is too complicated to enhance and expand. And also, the logic
requires unnecessary duplicated code.

- Solution:
This patch refactors the logic for automatically setting voice. Through
this patch, the logic will become easier to read and enhance. And also,
this patch removes unnecessary duplicated code.

Change-Id: I214d1a4d0a31ed34d794573eec9cec405769cedf
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
common/tts_config_mgr.c

index bbd1570..fd65e85 100644 (file)
@@ -162,18 +162,21 @@ static bool __is_client_type(int type_flag)
        return false;
 }
 
-static void __set_language_into_config(tts_config_s* config_info, const char* language)
+static void set_voice_into_config(tts_config_s* config_info, const char* language, int voice_type)
 {
        if (NULL == config_info) {
                return;
        }
 
-       memset(g_language, '\0', sizeof(g_language));
-       config_info->language = g_language;
-
        if (NULL != language) {
+               memset(g_language, '\0', sizeof(g_language));
+               config_info->language = g_language;
                strncpy(config_info->language, language, sizeof(g_language) - 1);
        }
+
+       if (TTS_CONFIG_VOICE_TYPE_NONE != voice_type) {
+               config_info->type = voice_type;
+       }
 }
 
 int __tts_config_mgr_check_engine_is_valid(const char* engine_id)
@@ -236,36 +239,20 @@ int __tts_config_mgr_check_engine_is_valid(const char* engine_id)
        SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Default engine is changed : %s", config_info.engine_id);
 
        /* Change is default voice */
-       bool is_valid_voice = false;
        tts_config_voice_s* voice = __get_voice_info(engine_info, config_info.language, config_info.type);
-       if (NULL != voice) {
-               is_valid_voice = true;
-
-               __set_language_into_config(&config_info, voice->language);
-               config_info.type = voice->type;
-
-               SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Default voice is changed : lang(%s) type(%d)", voice->language, voice->type);
-       }
-
-       if (false == is_valid_voice) {
-               GSList *iter_voice = g_slist_nth(engine_info->voices, 0);
-               if (NULL == iter_voice) {
-                       SLOG(LOG_ERROR, TAG_TTSCONFIG, "Fail to get voice list");
-                       return TTS_CONFIG_ERROR_OPERATION_FAILED;
-               }
-               voice = iter_voice->data;
-
-               if (NULL == voice || NULL == voice->language) {
-                       SLOG(LOG_ERROR, TAG_TTSCONFIG, "Fail to get voice info from list");
-                       return TTS_CONFIG_ERROR_OPERATION_FAILED;
-               }
+       if (NULL == voice) {
                /* Select first voice as default */
-               __set_language_into_config(&config_info, voice->language);
+               voice = (tts_config_voice_s *)g_slist_nth_data(engine_info->voices, 0);
+       }
 
-               config_info.type = voice->type;
-               SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Default voice is changed : lang(%s) type(%d)", voice->language, voice->type);
+       if (NULL == voice || NULL == voice->language) {
+               SLOG(LOG_ERROR, TAG_TTSCONFIG, "Fail to get voice info from list");
+               return TTS_CONFIG_ERROR_OPERATION_FAILED;
        }
 
+       set_voice_into_config(&config_info, voice->language, voice->type);
+       SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Default voice is changed : lang(%s) type(%d)", voice->language, voice->type);
+
        if (__is_client_type(TTS_CONFIG_CLIENT_TYPE_SETTING | TTS_CONFIG_CLIENT_TYPE_SERVER | TTS_CONFIG_CLIENT_TYPE_INTERRUPT)) {
                if (0 != tts_parser_set_engine(config_info.engine_id, config_info.setting, config_info.language, config_info.type)) {
                        SLOG(LOG_ERROR, TAG_TTSCONFIG, " Fail to save config");
@@ -413,7 +400,7 @@ Eina_Bool tts_config_mgr_inotify_event_cb(void* data, Ecore_Fd_Handler *fd_handl
                char* setting = NULL;
                char* lang = NULL;
                bool auto_voice = config_info.auto_voice;
-               int voice_type = -1;
+               int voice_type = TTS_CONFIG_VOICE_TYPE_NONE;
                int speech_rate = -1;
                int pitch = -1;
                double bg_volume_ratio = -1;
@@ -449,31 +436,18 @@ Eina_Bool tts_config_mgr_inotify_event_cb(void* data, Ecore_Fd_Handler *fd_handl
                        config_info.auto_voice = auto_voice;
                }
 
-               if (NULL != lang || -1 != voice_type) {
-                       char* before_lang = NULL;
-                       int before_type;
-
-                       before_lang = strdup(config_info.language);
-                       before_type = config_info.type;
-
-                       if (NULL != lang) {
-                               __set_language_into_config(&config_info, lang);
-                       }
-                       if (-1 != voice_type) {
-                               config_info.type = voice_type;
-                       }
+               if (NULL != lang || TTS_CONFIG_VOICE_TYPE_NONE != voice_type) {
+                       char* before_lang = strdup(config_info.language);
+                       int before_type = config_info.type;
 
+                       set_voice_into_config(&config_info, lang, voice_type);
                        SECURE_SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Voice change(%s, %d)", config_info.language, config_info.type);
 
                        /* Call all callbacks of client*/
                        voice_changed_cb_parameter_s params = {before_lang, before_type, config_info.language,
                                        config_info.type, config_info.auto_voice};
                        g_slist_foreach(g_config_client_list, __invoke_voice_changed_cb, &params);
-
-                       if (NULL != before_lang) {
-                               free(before_lang);
-                               before_lang = NULL;
-                       }
+                       free(before_lang);
                }
 
                if (-1 != speech_rate) {
@@ -619,57 +593,30 @@ int __tts_config_mgr_unregister_config_event()
        return TTS_CONFIG_ERROR_NONE;
 }
 
-int __tts_config_set_auto_language()
+static int set_voice_by_automation(int allowed_type_flag)
 {
-       char* value = NULL;
-       value = vconf_get_str(TTS_LANGSET_KEY);
-       if (NULL == value) {
-               SLOG(LOG_ERROR, TAG_TTSCONFIG, "[Config ERROR] Fail to get display language");
-               return TTS_CONFIG_ERROR_OPERATION_FAILED;
-       }
-
-       char temp_lang[6] = {'\0', };
-       strncpy(temp_lang, value, 5);
-       free(value);
-       value = NULL;
-
        tts_config_s config_info;
        if (0 != tts_parser_get_config_info(&config_info)){
                SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to get configure information");
                return TTS_CONFIG_ERROR_OPERATION_FAILED;
        }
 
-       if (true == __tts_config_mgr_check_lang_is_valid(config_info.engine_id, temp_lang, config_info.type)) {
-               /* tts default voice change */
-               if (NULL == config_info.language) {
-                       SLOG(LOG_ERROR, TAG_TTSCONFIG, "Current config language is NULL");
-                       return TTS_CONFIG_ERROR_OPERATION_FAILED;
-               }
-
-               char* before_lang = NULL;
-               int before_type;
-
-               before_lang = strdup(config_info.language);
-               before_type = config_info.type;
-
-               __set_language_into_config(&config_info, temp_lang);
-
-               SECURE_SLOG(LOG_DEBUG, TAG_TTSCONFIG, "[Config] Default voice : lang(%s) type(%d)",
-                       config_info.language, config_info.type);
+       char* display_language = vconf_get_str(TTS_LANGSET_KEY);
+       if (NULL == display_language) {
+               SLOG(LOG_ERROR, TAG_TTSCONFIG, "[Config ERROR] Fail to get display language");
+               return TTS_CONFIG_ERROR_OPERATION_FAILED;
+       }
 
-               /* Call all callbacks of client*/
-               voice_changed_cb_parameter_s params = {before_lang, before_type, config_info.language,
-                               config_info.type, config_info.auto_voice};
-               g_slist_foreach(g_config_client_list, __invoke_voice_changed_cb, &params);
+       char selected_language[6] = {'\0', };
+       strncpy(selected_language, display_language, sizeof(selected_language) - 1);
+       free(display_language);
+       display_language = NULL;
+       int selected_type = config_info.type;
 
-               if (NULL != before_lang) {
-                       free(before_lang);
-                       before_lang = NULL;
-               }
-       } else {
+       if (false == __tts_config_mgr_check_lang_is_valid(config_info.engine_id, selected_language, selected_type)) {
                /* Display language is not valid */
                char* tmp_language = NULL;
-               int tmp_type = -1;
+               int tmp_type = TTS_CONFIG_VOICE_TYPE_NONE;
                if (0 != __tts_config_mgr_select_lang(config_info.engine_id, &tmp_language, &tmp_type)) {
                        SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to select language");
                        return TTS_CONFIG_ERROR_OPERATION_FAILED;
@@ -680,25 +627,39 @@ int __tts_config_set_auto_language()
                        return TTS_CONFIG_ERROR_OPERATION_FAILED;
                }
 
-               SECURE_SLOG(LOG_DEBUG, TAG_TTSCONFIG, "[Config] Default voice : lang(%s) type(%d)", tmp_language, tmp_type);
-
-               /* Call all callbacks of client*/
-               voice_changed_cb_parameter_s params = {config_info.language, config_info.type, tmp_language, tmp_type,
-                               config_info.auto_voice};
-               g_slist_foreach(g_config_client_list, __invoke_voice_changed_cb, &params);
-
-               __set_language_into_config(&config_info, tmp_language);
-               config_info.type = tmp_type;
-
+               selected_type = tmp_type;
+               strncpy(selected_language, tmp_language, sizeof(selected_language) - 1);
                free(tmp_language);
                tmp_language = NULL;
        }
 
+       SECURE_SLOG(LOG_DEBUG, TAG_TTSCONFIG, "[Config] Select default voice : lang(%s) type(%d)", selected_language, selected_type);
+       if (__is_client_type(allowed_type_flag)) {
+               if (0 != tts_parser_set_voice(selected_language, selected_type)) {
+                       SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to save config");
+                       return TTS_CONFIG_ERROR_OPERATION_FAILED;
+               }
+       } else {
+               SLOG(LOG_INFO, TAG_TTSCONFIG, "Client is not allowed to save configuration. Skip saving configuration file.");
+       }
+
+       char *before_lang = (NULL != config_info.language ? strdup(config_info.language) : NULL);
+       int before_type = config_info.type;
+
+       set_voice_into_config(&config_info, selected_language, selected_type);
+
        if (0 != tts_parser_set_config_info(&config_info)){
                SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to set configure information");
+               free(before_lang);
                return TTS_CONFIG_ERROR_OPERATION_FAILED;
        }
 
+       /* Call all callbacks of client*/
+       voice_changed_cb_parameter_s params = {before_lang, before_type, config_info.language,
+                       config_info.type, config_info.auto_voice};
+       g_slist_foreach(g_config_client_list, __invoke_voice_changed_cb, &params);
+       free(before_lang);
+
        return TTS_CONFIG_ERROR_NONE;
 }
 
@@ -710,24 +671,12 @@ void __tts_config_display_language_changed_cb(keynode_t *key, void *data)
                return;
        }
 
-       if (false == config_info.auto_voice) {
-               SLOG(LOG_DEBUG, TAG_TTSCONFIG, "[DEBUG] Auto voice selection is disabled. Skip updating language.");
-               return;
+       if (config_info.auto_voice) {
+               SLOG(LOG_DEBUG, TAG_TTSCONFIG, "[DEBUG] Auto voice selection is enabled. Update language.");
+               set_voice_by_automation(TTS_CONFIG_CLIENT_TYPE_SERVER);
        }
 
-       __tts_config_set_auto_language();
-
-       if (__is_client_type(TTS_CONFIG_CLIENT_TYPE_SERVER)) {
-               if (0 != tts_parser_get_config_info(&config_info)){
-                       SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to get configure information");
-                       return;
-               }
-
-               if (0 != tts_parser_set_voice(config_info.language, config_info.type)) {
-                       SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to save config");
-                       return;
-               }
-       }
+       SLOG(LOG_DEBUG, TAG_TTSCONFIG, "[DEBUG] Auto voice selection is disabled. Skip updating language.");
 }
 
 void __tts_config_screen_reader_changed_cb(keynode_t *key, void *data)
@@ -1199,17 +1148,7 @@ int tts_config_mgr_initialize(unsigned int uid, tts_config_client_type_e client_
 
        if (true == config_info.auto_voice) {
                /* Check language with display language */
-               __tts_config_set_auto_language();
-
-               if (__is_client_type(TTS_CONFIG_CLIENT_TYPE_SETTING | TTS_CONFIG_CLIENT_TYPE_SERVER | TTS_CONFIG_CLIENT_TYPE_INTERRUPT)) {
-                       if (0 != tts_parser_get_config_info(&config_info)){
-                               SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to get configure information");
-                       }
-
-                       if (0 != tts_parser_set_voice(config_info.language, config_info.type)) {
-                               SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to save config");
-                       }
-               }
+               set_voice_by_automation(TTS_CONFIG_CLIENT_TYPE_SETTING | TTS_CONFIG_CLIENT_TYPE_SERVER | TTS_CONFIG_CLIENT_TYPE_INTERRUPT);
        } else {
                if (false == __tts_config_mgr_check_lang_is_valid(config_info.engine_id, config_info.language, config_info.type)) {
                        /* Default language is not valid */
@@ -1224,8 +1163,7 @@ int tts_config_mgr_initialize(unsigned int uid, tts_config_client_type_e client_
                        }
 
                        if (NULL != tmp_language) {
-                               __set_language_into_config(&config_info, tmp_language);
-                               config_info.type = tmp_type;
+                               set_voice_into_config(&config_info, tmp_language, tmp_type);
 
                                free(tmp_language);
                                tmp_language = NULL;
@@ -1576,8 +1514,7 @@ int tts_config_mgr_set_engine(const char* engine)
        if (NULL == voice) {
                tts_config_voice_s *new_voice = (tts_config_voice_s *)g_slist_nth_data(engine_info->voices, 0);
                if (NULL != new_voice) {
-                       __set_language_into_config(&config_info, new_voice->language);
-                       config_info.type = new_voice->type;
+                       set_voice_into_config(&config_info, new_voice->language, new_voice->type);
                }
        }
 
@@ -1719,8 +1656,7 @@ int tts_config_mgr_set_voice(const char* language, int type)
                SLOG(LOG_INFO, TAG_TTSCONFIG, "Client type is default. Skip saving configuration file.");
        }
 
-       __set_language_into_config(&config_info, language);
-       config_info.type = type;
+       set_voice_into_config(&config_info, language, type);
 
        if (0 != tts_parser_set_config_info(&config_info)){
                SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to set configure information");
@@ -1785,17 +1721,7 @@ int tts_config_mgr_set_auto_voice(bool value)
        }
 
        if (true == config_info.auto_voice) {
-               __tts_config_set_auto_language();
-
-               if (__is_client_type(TTS_CONFIG_CLIENT_TYPE_SETTING | TTS_CONFIG_CLIENT_TYPE_SERVER | TTS_CONFIG_CLIENT_TYPE_INTERRUPT)) {
-                       if (0 != tts_parser_get_config_info(&config_info)){
-                               SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to get configure information");
-                       }
-
-                       if (0 != tts_parser_set_voice(config_info.language, config_info.type)) {
-                               SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to save config");
-                       }
-               }
+               set_voice_by_automation(TTS_CONFIG_CLIENT_TYPE_SETTING | TTS_CONFIG_CLIENT_TYPE_SERVER | TTS_CONFIG_CLIENT_TYPE_INTERRUPT);
        }
 
        return TTS_CONFIG_ERROR_NONE;