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>
16 #include "ttsd_main.h"
17 #include "ttsd_config.h"
18 #include "ttsd_engine_agent.h"
19 #include "ttsd_data.h"
21 #define CONFIG_DEFAULT BASE_DIRECTORY_DEFAULT"/ttsd.conf"
23 #define DEFAULT_CONFIG_FILE_NAME "/ttsd_default.conf"
24 #define NOTI_CONFIG_FILE_NAME "/ttsd_noti.conf"
25 #define SR_CONFIG_FILE_NAME "/ttsd_sr.conf"
27 #define DEFAULT_ERROR_FILE_NAME "/ttsd_default.err"
28 #define NOTI_ERROR_FILE_NAME "/ttsd_noti.err"
29 #define SR_ERROR_FILE_NAME "/ttsd_sr.err"
31 #define ENGINE_ID "ENGINE_ID"
35 static char* g_engine_id;
36 static char* g_language;
40 static char* g_config_file;
42 static ttsd_config_lang_changed_cb g_callback;
44 void __system_language_changed_cb(runtime_info_key_e key, void *user_data)
46 if (RUNTIME_INFO_KEY_LANGUAGE == key) {
47 if (TTSD_MODE_NOTIFICATION == ttsd_get_mode() || TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
51 ret = runtime_info_get_value_string(RUNTIME_INFO_KEY_LANGUAGE, &value);
53 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Fail to get system language : %d", ret);
56 SLOG(LOG_DEBUG, get_tag(), "[Config] System language changed : %s", value);
57 if (NULL != g_callback)
58 g_callback(value, g_vc_type);
69 int __ttsd_config_save()
71 if (0 != access(g_config_file, R_OK|W_OK)) {
72 if (0 == ecore_file_mkpath(CONFIG_DIRECTORY)) {
73 SLOG(LOG_ERROR, get_tag(), "[Config ERROR ] Fail to create directory (%s)", CONFIG_DIRECTORY);
77 SLOG(LOG_WARN, get_tag(), "[Config] Create directory (%s)", CONFIG_DIRECTORY);
81 config_fp = fopen(g_config_file, "w+");
83 if (NULL == config_fp) {
84 /* make file and file default */
85 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to open config (%s)", g_config_file);
89 SLOG(LOG_DEBUG, get_tag(), "[Config] Rewrite config file");
92 fprintf(config_fp, "%s %s\n", ENGINE_ID, g_engine_id);
95 fprintf(config_fp, "%s %s %d\n", VOICE, g_language, g_vc_type);
98 fprintf(config_fp, "%s %d\n", SPEED, g_speed);
105 int __ttsd_config_load()
108 char buf_id[256] = {0};
109 char buf_param[256] = {0};
111 bool is_default_open = false;
113 config_fp = fopen(g_config_file, "r");
115 if (NULL == config_fp) {
116 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Not open file(%s)", g_config_file);
118 config_fp = fopen(CONFIG_DEFAULT, "r");
119 if (NULL == config_fp) {
120 SLOG(LOG_ERROR, get_tag(), "[Config WARNING] Not open original config file(%s)", CONFIG_DEFAULT);
123 is_default_open = true;
127 if (EOF == fscanf(config_fp, "%s %s", buf_id, buf_param)) {
129 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (engine id)");
130 __ttsd_config_save();
133 if (0 == strncmp(ENGINE_ID, buf_id, strlen(ENGINE_ID))) {
134 g_engine_id = strdup(buf_param);
137 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (engine id)");
138 __ttsd_config_save();
144 if (EOF == fscanf(config_fp, "%s %s %d", buf_id, buf_param, &int_param)) {
146 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (voice)");
147 __ttsd_config_save();
150 if (0 == strncmp(VOICE, buf_id, strlen(VOICE))) {
151 g_language = strdup(buf_param);
152 g_vc_type = int_param;
155 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (voice)");
156 __ttsd_config_save();
162 if (EOF == fscanf(config_fp, "%s %d", buf_id, &int_param)) {
164 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (speed)");
165 __ttsd_config_save();
168 if (0 == strncmp(SPEED, buf_id, strlen(SPEED))) {
172 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (speed)");
173 __ttsd_config_save();
180 SLOG(LOG_DEBUG, get_tag(), "[Config] Load config : engine(%s), voice(%s,%d), speed(%d)",
181 g_engine_id, g_language, g_vc_type, g_speed);
183 if (true == is_default_open) {
184 if(0 == __ttsd_config_save()) {
185 SLOG(LOG_DEBUG, get_tag(), "[Config] Create config(%s)", g_config_file);
192 int ttsd_config_initialize(ttsd_config_lang_changed_cb callback)
199 g_callback = callback;
201 if (TTSD_MODE_NOTIFICATION == ttsd_get_mode()) {
202 g_config_file = (char*)malloc(strlen(CONFIG_DIRECTORY) + strlen(NOTI_CONFIG_FILE_NAME) + 1);
203 strcpy(g_config_file, CONFIG_DIRECTORY);
204 strcat(g_config_file, NOTI_CONFIG_FILE_NAME);
205 } else if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
206 g_config_file = (char*)malloc(strlen(CONFIG_DIRECTORY) + strlen(SR_CONFIG_FILE_NAME) + 1);
207 strcpy(g_config_file, CONFIG_DIRECTORY);
208 strcat(g_config_file, SR_CONFIG_FILE_NAME);
210 g_config_file = (char*)malloc(strlen(CONFIG_DIRECTORY) + strlen(DEFAULT_CONFIG_FILE_NAME) + 1);
211 strcpy(g_config_file, CONFIG_DIRECTORY);
212 strcat(g_config_file, DEFAULT_CONFIG_FILE_NAME);
215 __ttsd_config_load();
217 /* Register system language changed callback */
218 int ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_LANGUAGE, __system_language_changed_cb, NULL);
\r
220 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Fail to register callback : %d", ret);
221 return TTSD_ERROR_OPERATION_FAILED;
227 int ttsd_config_finalize()
229 __ttsd_config_save();
231 if (NULL != g_config_file) {
238 int ttsd_config_update_language()
240 if (TTSD_MODE_NOTIFICATION == ttsd_get_mode() || TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
244 ret = runtime_info_get_value_string(RUNTIME_INFO_KEY_LANGUAGE, &value);
246 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Fail to get system language : %d", ret);
249 SLOG(LOG_DEBUG, get_tag(), "[Config] System language : %s", value);
251 if (0 != strcmp(value, g_language)) {
252 if (NULL != g_callback)
253 g_callback(value, g_vc_type);
264 int ttsd_config_get_default_engine(char** engine_id)
266 if (NULL == engine_id)
269 *engine_id = strdup(g_engine_id);
273 int ttsd_config_set_default_engine(const char* engine_id)
275 if (NULL == engine_id)
278 if (NULL != g_engine_id)
281 g_engine_id = strdup(engine_id);
282 __ttsd_config_save();
286 int ttsd_config_get_default_voice(char** language, int* type)
288 if (NULL == language || NULL == type)
291 *language = strdup(g_language);
297 int ttsd_config_set_default_voice(const char* language, int type)
299 if (NULL == language)
302 if (NULL != g_language)
305 g_language = strdup(language);
308 __ttsd_config_save();
313 int ttsd_config_get_default_speed(int* speed)
323 int ttsd_config_set_default_speed(int speed)
326 __ttsd_config_save();
330 int ttsd_config_save_error(int uid, int uttid, const char* lang, int vctype, const char* text,
331 const char* func, int line, const char* message)
335 if (TTSD_MODE_NOTIFICATION == ttsd_get_mode()) {
336 err_file = (char*)malloc(strlen(CONFIG_DIRECTORY) + strlen(NOTI_ERROR_FILE_NAME) + 1);
337 strcpy(err_file, CONFIG_DIRECTORY);
338 strcat(err_file, NOTI_ERROR_FILE_NAME);
339 } else if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
340 err_file = (char*)malloc(strlen(CONFIG_DIRECTORY) + strlen(SR_ERROR_FILE_NAME) + 1);
341 strcpy(err_file, CONFIG_DIRECTORY);
342 strcat(err_file, SR_ERROR_FILE_NAME);
344 err_file = (char*)malloc(strlen(CONFIG_DIRECTORY) + strlen(DEFAULT_ERROR_FILE_NAME) + 1);
345 strcpy(err_file, CONFIG_DIRECTORY);
346 strcat(err_file, DEFAULT_ERROR_FILE_NAME);
350 err_fp = fopen(err_file, "w+");
351 if (NULL == err_fp) {
352 SLOG(LOG_WARN, get_tag(), "[WARNING] Fail to open error file (%s)", err_file);
355 SLOG(LOG_DEBUG, get_tag(), "Save Error File (%s)", err_file);
359 fprintf(err_fp, "function - %s\n", func);
363 fprintf(err_fp, "line - %d\n", line);
366 if (NULL != message) {
367 fprintf(err_fp, "message - %s\n", message);
372 fprintf(err_fp, "uid - %d\n", uid);
375 fprintf(err_fp, "uttid - %d\n", uttid);
379 fprintf(err_fp, "language - %s\n", lang);
383 fprintf(err_fp, "vctype - %d\n", vctype);
387 fprintf(err_fp, "text - %s\n", text);
390 /* get current engine */
391 char *engine_id = NULL;
393 ret = ttsd_engine_setting_get_engine(&engine_id);
395 SLOG(LOG_ERROR, get_tag(), "[ERROR] Fail to get current engine");
397 fprintf(err_fp, "current engine - %s", engine_id);
401 ttsd_data_save_error_log(uid, err_fp);