2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd All Rights Reserved
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
14 #include <Ecore_File.h>
15 #include <runtime_info.h>
18 /* For multi-user support */
19 #include <tzplatform_config.h>
21 #include "ttsd_main.h"
22 #include "ttsd_config.h"
23 #include "ttsd_engine_agent.h"
24 #include "ttsd_data.h"
26 #define CONFIG_DEFAULT BASE_DIRECTORY_DEFAULT"/ttsd.conf"
28 #define DEFAULT_ERROR_FILE_NAME tzplatform_mkpath(TZ_USER_HOME, ".voice/ttsd_default.err")
30 #define ENGINE_ID "ENGINE_ID"
34 static char* g_engine_id;
35 static char* g_language;
39 static ttsd_config_changed_cb g_callback;
41 int __ttsd_config_save()
43 if (0 != access(DEFAULT_CONFIG_FILE_NAME, R_OK|W_OK)) {
44 if (0 == ecore_file_mkpath(CONFIG_DIRECTORY)) {
45 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to create directory (%s)", CONFIG_DIRECTORY);
47 SLOG(LOG_DEBUG, get_tag(), "[Config] Create directory (%s)", CONFIG_DIRECTORY);
52 config_fp = fopen(DEFAULT_CONFIG_FILE_NAME, "w+");
54 if (NULL == config_fp) {
55 /* make file and file default */
56 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to open config (%s)", DEFAULT_CONFIG_FILE_NAME);
60 SLOG(LOG_DEBUG, get_tag(), "[Config] Rewrite config file");
63 fprintf(config_fp, "%s %s\n", ENGINE_ID, g_engine_id);
66 fprintf(config_fp, "%s %s %d\n", VOICE, g_language, g_vc_type);
69 fprintf(config_fp, "%s %d\n", SPEED, g_speed);
76 int __ttsd_config_load()
79 char buf_id[256] = {0};
80 char buf_param[256] = {0};
82 bool is_default_open = false;
84 config_fp = fopen(DEFAULT_CONFIG_FILE_NAME, "r");
86 if (NULL == config_fp) {
87 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Not open file(%s)", DEFAULT_CONFIG_FILE_NAME);
89 config_fp = fopen(CONFIG_DEFAULT, "r");
90 if (NULL == config_fp) {
91 SLOG(LOG_ERROR, get_tag(), "[Config WARNING] Not open original config file(%s)", CONFIG_DEFAULT);
94 is_default_open = true;
98 if (EOF == fscanf(config_fp, "%s %s", buf_id, buf_param)) {
100 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (engine id)");
101 __ttsd_config_save();
104 if (0 == strncmp(ENGINE_ID, buf_id, strlen(ENGINE_ID))) {
105 g_engine_id = strdup(buf_param);
108 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (engine id)");
109 __ttsd_config_save();
115 if (EOF == fscanf(config_fp, "%s %s %d", buf_id, buf_param, &int_param)) {
117 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (voice)");
118 __ttsd_config_save();
121 if (0 == strncmp(VOICE, buf_id, strlen(VOICE))) {
122 g_language = strdup(buf_param);
123 g_vc_type = int_param;
126 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (voice)");
127 __ttsd_config_save();
132 if (true == is_default_open) {
133 /* Change default language to display language */
135 value = vconf_get_str(VCONFKEY_LANGSET);
138 SLOG(LOG_DEBUG, get_tag(), "[Config] System language : %s", value);
139 strncpy(g_language, value, strlen(g_language));
140 SLOG(LOG_DEBUG, get_tag(), "[Config] Default language : %s", g_language);
144 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Fail to get system language");
149 if (EOF == fscanf(config_fp, "%s %d", buf_id, &int_param)) {
151 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (speed)");
152 __ttsd_config_save();
155 if (0 == strncmp(SPEED, buf_id, strlen(SPEED))) {
159 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (speed)");
160 __ttsd_config_save();
167 SLOG(LOG_DEBUG, get_tag(), "[Config] Load config : engine(%s), voice(%s,%d), speed(%d)",
168 g_engine_id, g_language, g_vc_type, g_speed);
170 if (true == is_default_open) {
171 if(0 == __ttsd_config_save()) {
172 SLOG(LOG_DEBUG, get_tag(), "[Config] Create config(%s)", DEFAULT_CONFIG_FILE_NAME);
179 int ttsd_config_initialize(ttsd_config_changed_cb callback)
186 g_callback = callback;
188 ecore_file_mkpath(CONFIG_DIRECTORY);
190 __ttsd_config_load();
195 int ttsd_config_finalize()
197 __ttsd_config_save();
199 if (NULL != g_language) {
203 if (NULL != g_engine_id) {
210 int ttsd_config_update_language()
212 /* no work in default mode */
216 int ttsd_config_get_default_engine(char** engine_id)
218 if (NULL == engine_id)
221 if (NULL != g_engine_id) {
222 *engine_id = strdup(g_engine_id);
224 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current engine id is NULL");
231 int ttsd_config_set_default_engine(const char* engine_id)
233 if (NULL == engine_id)
236 if (NULL != g_engine_id)
239 g_engine_id = strdup(engine_id);
241 __ttsd_config_save();
245 int ttsd_config_get_default_voice(char** language, int* type)
247 if (NULL == language || NULL == type)
250 if (NULL != g_language) {
251 *language = strdup(g_language);
254 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current language is NULL");
261 int ttsd_config_set_default_voice(const char* language, int type)
263 if (NULL == language)
266 if (NULL != g_language)
269 g_language = strdup(language);
272 __ttsd_config_save();
276 int ttsd_config_get_default_speed(int* speed)
286 int ttsd_config_set_default_speed(int speed)
290 __ttsd_config_save();
294 int ttsd_config_save_error(int uid, int uttid, const char* lang, int vctype, const char* text,
295 const char* func, int line, const char* message)
297 SLOG(LOG_ERROR, get_tag(), "=============== TTS ERROR LOG ====================");
299 SLOG(LOG_ERROR, get_tag(), "uid(%d) uttid(%d)", uid, uttid);
301 if (NULL != func) SLOG(LOG_ERROR, get_tag(), "Function(%s) Line(%d)", func, line);
302 if (NULL != message) SLOG(LOG_ERROR, get_tag(), "Message(%s)", message);
303 if (NULL != lang) SLOG(LOG_ERROR, get_tag(), "Lang(%s), type(%d)", lang, vctype);
304 if (NULL != text) SLOG(LOG_ERROR, get_tag(), "Text(%s)", text);
306 SLOG(LOG_ERROR, get_tag(), "==================================================");