Add a new TTS Interrupt mode
[platform/core/uifw/tts.git] / server / ttsd_config.c
index ee2a08b..58dc2fa 100644 (file)
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2011 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
 */
 
 
-#include <vconf.h>
-#include "ttsd_main.h"
+/* For multi-user support */
+#include <tzplatform_config.h>
+
+#include "tts_config_mgr.h"
 #include "ttsd_config.h"
+#include "ttsd_main.h"
 
-/*
-* tts-daemon config
-*/
 
-int ttsd_config_get_char_type(const char* key, char** value)
+static ttsd_config_changed_cb g_callback;
+
+static ttsd_config_screen_reader_changed_cb g_sr_callback;
+
+void __ttsd_config_engine_changed_cb(const char* engine_id, const char* setting, const char* language, int voice_type, bool auto_voice, bool need_credential, void* user_data)
 {
-       if (NULL == key || NULL == value) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] Input parameter is NULL\n");
-               return TTSD_ERROR_INVALID_PARAMETER;
-       } 
-
-       *value = vconf_get_str(key);
-       if (NULL == *value) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail to get char type from config : key(%s)\n", key);
-               return -1;
+       /* Need to check engine is valid */
+       if (false == tts_config_check_default_engine_is_valid(engine_id)) {
+               SLOG(LOG_ERROR, tts_tag(), "Engine id is NOT valid : %s", engine_id);
+               return;
        }
 
-       return 0;
+       if (NULL != g_callback) {
+               SECURE_SLOG(LOG_DEBUG, tts_tag(), "Call the engine reload callback : engine id(%s)", engine_id);
+               g_callback(TTS_CONFIG_TYPE_ENGINE, engine_id, 0);
+               SECURE_SLOG(LOG_DEBUG, tts_tag(), "Call the voice changed callback : lang(%s), type(%d)", language, voice_type);
+               g_callback(TTS_CONFIG_TYPE_VOICE, language, voice_type);
+       } else {
+               SLOG(LOG_ERROR, tts_tag(), "Config changed callback is NULL");
+       }
 }
 
-int ttsd_config_set_char_type(const char* key, const char* value)
+void __ttsd_config_voice_changed_cb(const char* before_language, int before_type, const char* language, int type, bool auto_voice, void* user_data)
 {
-       if (NULL == key || NULL == value) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] Input parameter is NULL\n");
-               return TTSD_ERROR_INVALID_PARAMETER;
-       } 
-
-       if (0 != vconf_set_str(key, value)) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail to set char type \n"); 
-               return -1;
+       /* Need to check voice is valid */
+       if (false == tts_config_check_default_voice_is_valid(language, type)) {
+               SLOG(LOG_ERROR, tts_tag(), "Lang(%s) type(%d) is NOT valid", language, type);
+               return;
        }
 
-       return 0;
+       if (NULL != g_callback) {
+               g_callback(TTS_CONFIG_TYPE_VOICE, language, type);
+       } else {
+               SLOG(LOG_ERROR, tts_tag(), "Config changed callback is NULL");
+       }
 }
 
-int ttsd_config_get_bool_type(const char* key, bool* value)
+void __ttsd_config_speech_rate_changed_cb(int value, void* user_data)
 {
-       if (NULL == key || NULL == value) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] Input parameter is NULL\n");
-               return TTSD_ERROR_INVALID_PARAMETER;
-       } 
-
-       int result ;
-       if (0 != vconf_get_int(key, &result)) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail to get bool type config : key(%s)\n", key);
-               return -1;
+       if (NULL != g_callback) {
+               g_callback(TTS_CONFIG_TYPE_SPEED, NULL, value);
+       } else {
+               SLOG(LOG_ERROR, tts_tag(), "Config changed callback is NULL");
        }
+}
 
-       *value = (bool) result;
+void __ttsd_config_pitch_changed_cb(int value, void* user_data)
+{
+       if (NULL != g_callback) {
+               g_callback(TTS_CONFIG_TYPE_PITCH, NULL, value);
+       } else {
+               SLOG(LOG_ERROR, tts_tag(), "Config changed callback is NULL");
+       }
+}
 
-       return 0;
+void __ttsd_config_screen_reader_changed_cb(bool value)
+{
+       if (NULL != g_sr_callback) {
+               g_sr_callback(value);
+       }
 }
 
-int ttsd_config_set_bool_type(const char* key, const bool value)
+int ttsd_config_initialize(ttsd_config_changed_cb config_cb)
 {
-       if (NULL == key) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] Input parameter is NULL\n");
-               return TTSD_ERROR_INVALID_PARAMETER;
-       } 
-
-       int result = (int)value;
-       if (0 != vconf_set_int(key, result)) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail to set bool type config : key(%s)\n", key);
+       if (NULL == config_cb) {
+               SLOG(LOG_ERROR, tts_tag(), "[Config] Invalid parameter");
                return -1;
        }
 
-       return 0;
-}
+       g_callback = config_cb;
+       g_sr_callback = NULL;
 
-int ttsd_config_get_int_type(const char* key, int* value)
-{
-       if (NULL == key || NULL == value) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] Input parameter is NULL\n");
-               return TTSD_ERROR_INVALID_PARAMETER;
-       } 
+       int ret = -1;
+       ret = tts_config_mgr_initialize(getpid());
+       if (0 != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Config] Fail to initialize config manager");
+               return -1;
+       }
 
-       if (0 != vconf_get_int(key, value)) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail to get bool type config : key(%s)\n", key);
+       ret = tts_config_mgr_set_callback(getpid(), __ttsd_config_engine_changed_cb, __ttsd_config_voice_changed_cb, 
+               __ttsd_config_speech_rate_changed_cb, __ttsd_config_pitch_changed_cb, NULL);
+       if (0 != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set config changed : %d", ret);
                return -1;
        }
 
        return 0;
 }
 
-int ttsd_config_set_int_type(const char* key, const int value)
+int ttsd_config_finalize()
 {
-       if (NULL == key) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] Input parameter is NULL\n");
-               return TTSD_ERROR_INVALID_PARAMETER;
-       } 
+       tts_config_unset_screen_reader_callback(getpid());
 
-       if (0 != vconf_set_int(key, value)) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail to set int type config : key(%s)\n", key);
-               return -1;
-       }
+       tts_config_mgr_finalize(getpid());
 
        return 0;
 }
 
-/*
-* interface for engine plug-in
-*/
-
-int config_make_key_for_engine(const char* engine_id, const char* key, char** out_key)
+int ttsd_config_set_screen_reader_callback(ttsd_config_screen_reader_changed_cb sr_cb)
 {
-       int key_size = strlen(TTSD_CONFIG_PREFIX) + strlen(engine_id) + strlen(key) + 2; /* 2 is '/' and '\0' */
+       if (NULL == sr_cb) {
+               SLOG(LOG_ERROR, tts_tag(), "[Config] Invalid parameter");
+               return -1;
+       }
 
-       *out_key = (char*) g_malloc0( sizeof(char) * key_size);
+       g_sr_callback = sr_cb;
 
-       if (*out_key == NULL) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] Not enough memory!! \n");
+       int ret = tts_config_set_screen_reader_callback(getpid(), __ttsd_config_screen_reader_changed_cb) ;
+       if (0 != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Config] Fail to set screen reader callback");
                return -1;
-       } else {
-               snprintf(*out_key, key_size, "%s%s/%s", TTSD_CONFIG_PREFIX, engine_id, key );
-               SLOG(LOG_DEBUG, TAG_TTSD, "[Config DEBUG] make key (%s) \n", *out_key);
        }
-
        return 0;
 }
 
-int ttsd_config_set_persistent_data(const char* engine_id, const char* key, const char* value)
+int ttsd_config_get_default_engine(char** engine_id)
 {
-       char* vconf_key = NULL;
-
-       if (0 != config_make_key_for_engine(engine_id, key, &vconf_key)) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail config_make_key_for_engine()\n"); 
+       if (NULL == engine_id)
                return -1;
+
+       if (0 != tts_config_mgr_get_engine(engine_id)) {
+               SLOG(LOG_ERROR, tts_tag(), "[Config ERROR] Fail to get engine id");
+               return TTSD_ERROR_OPERATION_FAILED;
        }
 
-       if (0 != vconf_set_str(vconf_key, value)) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail to set key, value\n");
-               
-               if(vconf_key != NULL)   
-                       g_free(vconf_key);
+       return 0;
+}
 
+int ttsd_config_set_default_engine(const char* engine_id)
+{
+       if (NULL == engine_id)
                return -1;
-       }
-
-       SLOG(LOG_DEBUG, TAG_TTSD, "[Config DEBUG] Set data : key(%s), value(%s) \n", vconf_key, value);
 
-       if (vconf_key != NULL)  
-               g_free(vconf_key);
+       if (true == tts_config_check_default_engine_is_valid(engine_id)) {
+               if (0 != tts_config_mgr_set_engine(engine_id)) {
+                       SLOG(LOG_ERROR, tts_tag(), "[Config ERROR] Fail to set engine id");
+               }
+       }
 
        return 0;
 }
 
-int ttsd_config_get_persistent_data(const char* engine_id, const char* key, char** value)
+int ttsd_config_get_default_voice(char** language, int* type)
 {
-       char* vconf_key = NULL;
-
-       if (0 != config_make_key_for_engine(engine_id, key, &vconf_key)) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail config_make_key_for_engine()\n");
+       if (NULL == language || NULL == type)
                return -1;
-       }
 
-       char* temp;
-       temp = vconf_get_str(vconf_key);
-       if (temp == NULL) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail to get value\n");
+       if (0 != tts_config_mgr_get_voice(language, type)) {
+               SLOG(LOG_ERROR, tts_tag(), "[Config ERROR] Fail to get default voice");
+               return TTSD_ERROR_OPERATION_FAILED;
+       }
 
-               if(vconf_key != NULL)   
-                       g_free(vconf_key);
+       return 0;
+}
 
+int ttsd_config_get_default_speed(int* speed)
+{
+       if (NULL == speed)
                return -1;
-       }
 
-       *value = g_strdup(temp);
+       if (0 != tts_config_mgr_get_speech_rate(speed)) {
+               SLOG(LOG_ERROR, tts_tag(), "[Config ERROR] Fail to get default speech rate");
+               return TTSD_ERROR_OPERATION_FAILED;
+       }
 
-       SLOG(LOG_DEBUG, TAG_TTSD, "[Config DEBUG] Get data : key(%s), value(%s) \n", vconf_key, *value);
+       return 0;
+}
 
-       if (NULL != vconf_key)  
-               g_free(vconf_key);
+int ttsd_config_get_default_pitch(int* pitch)
+{
+       if (NULL == pitch)
+               return -1;
 
-       if (NULL != temp)               
-               g_free(temp);
+       if (0 != tts_config_mgr_get_pitch(pitch)) {
+               SLOG(LOG_ERROR, tts_tag(), "[Config ERROR] Fail to get default pitch");
+               return TTSD_ERROR_OPERATION_FAILED;
+       }
 
        return 0;
 }
 
-int ttsd_config_remove_persistent_data(const char* engine_id, const char* key)
+int ttsd_config_save_error(int uid, int uttid, const char* lang, int vctype, const char* text, 
+                          const char* func, int line, const char* message)
 {
-       char* vconf_key = NULL;
-       int result = 0;
+       SLOG(LOG_DEBUG, tts_tag(), "@@@@@ TTS ERROR LOG @@@@@");
 
-       if (0 != config_make_key_for_engine(engine_id, key, &vconf_key)) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail config_make_key_for_engine()\n");
-               return -1;
-       }
+       SLOG(LOG_DEBUG, tts_tag(), "uid(%d) uttid(%d)", uid, uttid);
 
-       if( NULL == vconf_key )         
-               return -1;
 
-       if (0 != vconf_unset(vconf_key)) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail to remove key\n");
-               result = -1;
-       } else {
-               SLOG(LOG_DEBUG, TAG_TTSD, "[Config DEBUG] Remove data : key(%s)", vconf_key);
-       }
+       SLOG(LOG_DEBUG, tts_tag(), "Function(%s) Line(%d)", (NULL == func) ? "NULL" : func, line);
+       SLOG(LOG_DEBUG, tts_tag(), "Message(%s)", (NULL == message) ? "NULL" : message);
+       SLOG(LOG_DEBUG, tts_tag(), "Lang(%s), type(%d)", (NULL == lang) ? "NULL" : lang, vctype);
+       SLOG(LOG_DEBUG, tts_tag(), "Text(%s)", (NULL == text) ? "NULL" : text);
 
-       if( vconf_key != NULL ) 
-               g_free(vconf_key);
+       SLOG(LOG_DEBUG, tts_tag(), "@@@@@");
 
-       return result;
+       return 0;
 }