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 #include "ttsd_main.h"
19 #include "ttsd_config.h"
20 #include "ttsd_engine_agent.h"
21 #include "ttsd_data.h"
23 #define CONFIG_DEFAULT BASE_DIRECTORY_DEFAULT"/ttsd.conf"
25 #define DEFAULT_ERROR_FILE_NAME CONFIG_DIRECTORY"/ttsd_default.err"
27 #define ENGINE_ID "ENGINE_ID"
31 static char* g_engine_id;
32 static char* g_language;
36 static ttsd_config_changed_cb g_callback;
38 int __ttsd_config_save()
40 if (0 != access(DEFAULT_CONFIG_FILE_NAME, R_OK|W_OK)) {
41 if (0 == ecore_file_mkpath(CONFIG_DIRECTORY)) {
42 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to create directory (%s)", CONFIG_DIRECTORY);
44 SLOG(LOG_DEBUG, get_tag(), "[Config] Create directory (%s)", CONFIG_DIRECTORY);
49 config_fp = fopen(DEFAULT_CONFIG_FILE_NAME, "w+");
51 if (NULL == config_fp) {
52 /* make file and file default */
53 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to open config (%s)", DEFAULT_CONFIG_FILE_NAME);
57 SLOG(LOG_DEBUG, get_tag(), "[Config] Rewrite config file");
60 fprintf(config_fp, "%s %s\n", ENGINE_ID, g_engine_id);
63 fprintf(config_fp, "%s %s %d\n", VOICE, g_language, g_vc_type);
66 fprintf(config_fp, "%s %d\n", SPEED, g_speed);
73 int __ttsd_config_load()
76 char buf_id[256] = {0};
77 char buf_param[256] = {0};
79 bool is_default_open = false;
81 config_fp = fopen(DEFAULT_CONFIG_FILE_NAME, "r");
83 if (NULL == config_fp) {
84 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Not open file(%s)", DEFAULT_CONFIG_FILE_NAME);
86 config_fp = fopen(CONFIG_DEFAULT, "r");
87 if (NULL == config_fp) {
88 SLOG(LOG_ERROR, get_tag(), "[Config WARNING] Not open original config file(%s)", CONFIG_DEFAULT);
91 is_default_open = true;
95 if (EOF == fscanf(config_fp, "%s %s", buf_id, buf_param)) {
97 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (engine id)");
101 if (0 == strncmp(ENGINE_ID, buf_id, strlen(ENGINE_ID))) {
102 g_engine_id = strdup(buf_param);
105 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (engine id)");
106 __ttsd_config_save();
112 if (EOF == fscanf(config_fp, "%s %s %d", buf_id, buf_param, &int_param)) {
114 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (voice)");
115 __ttsd_config_save();
118 if (0 == strncmp(VOICE, buf_id, strlen(VOICE))) {
119 g_language = strdup(buf_param);
120 g_vc_type = int_param;
123 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (voice)");
124 __ttsd_config_save();
129 if (true == is_default_open) {
130 /* Change default language to display language */
132 value = vconf_get_str(VCONFKEY_LANGSET);
135 SLOG(LOG_DEBUG, get_tag(), "[Config] System language : %s", value);
136 strncpy(g_language, value, strlen(g_language));
137 SLOG(LOG_DEBUG, get_tag(), "[Config] Default language : %s", g_language);
141 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Fail to get system language");
146 if (EOF == fscanf(config_fp, "%s %d", buf_id, &int_param)) {
148 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (speed)");
149 __ttsd_config_save();
152 if (0 == strncmp(SPEED, buf_id, strlen(SPEED))) {
156 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (speed)");
157 __ttsd_config_save();
164 SLOG(LOG_DEBUG, get_tag(), "[Config] Load config : engine(%s), voice(%s,%d), speed(%d)",
165 g_engine_id, g_language, g_vc_type, g_speed);
167 if (true == is_default_open) {
168 if(0 == __ttsd_config_save()) {
169 SLOG(LOG_DEBUG, get_tag(), "[Config] Create config(%s)", DEFAULT_CONFIG_FILE_NAME);
176 int ttsd_config_initialize(ttsd_config_changed_cb callback)
183 g_callback = callback;
185 ecore_file_mkpath(CONFIG_DIRECTORY);
187 __ttsd_config_load();
192 int ttsd_config_finalize()
194 __ttsd_config_save();
196 if (NULL != g_language) {
200 if (NULL != g_engine_id) {
207 int ttsd_config_update_language()
209 /* no work in default mode */
213 int ttsd_config_get_default_engine(char** engine_id)
215 if (NULL == engine_id)
218 if (NULL != g_engine_id) {
219 *engine_id = strdup(g_engine_id);
221 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current engine id is NULL");
228 int ttsd_config_set_default_engine(const char* engine_id)
230 if (NULL == engine_id)
233 if (NULL != g_engine_id)
236 g_engine_id = strdup(engine_id);
238 __ttsd_config_save();
242 int ttsd_config_get_default_voice(char** language, int* type)
244 if (NULL == language || NULL == type)
247 if (NULL != g_language) {
248 *language = strdup(g_language);
251 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current language is NULL");
258 int ttsd_config_set_default_voice(const char* language, int type)
260 if (NULL == language)
263 if (NULL != g_language)
266 g_language = strdup(language);
269 __ttsd_config_save();
273 int ttsd_config_get_default_speed(int* speed)
283 int ttsd_config_set_default_speed(int speed)
287 __ttsd_config_save();
291 int ttsd_config_save_error(int uid, int uttid, const char* lang, int vctype, const char* text,
292 const char* func, int line, const char* message)
295 err_fp = fopen(DEFAULT_ERROR_FILE_NAME, "a");
296 if (NULL == err_fp) {
297 SLOG(LOG_WARN, get_tag(), "[WARNING] Fail to open error file (%s)", DEFAULT_ERROR_FILE_NAME);
300 SLOG(LOG_DEBUG, get_tag(), "Save Error File (%s)", DEFAULT_ERROR_FILE_NAME);
304 fprintf(err_fp, "function - %s\n", func);
308 fprintf(err_fp, "line - %d\n", line);
311 if (NULL != message) {
312 fprintf(err_fp, "message - %s\n", message);
317 fprintf(err_fp, "uid - %d\n", uid);
320 fprintf(err_fp, "uttid - %d\n", uttid);
324 fprintf(err_fp, "language - %s\n", lang);
328 fprintf(err_fp, "vctype - %d\n", vctype);
332 fprintf(err_fp, "text - %s\n", text);
335 /* get current engine */
336 char *engine_id = NULL;
338 ret = ttsd_engine_setting_get_engine(&engine_id);
340 SLOG(LOG_ERROR, get_tag(), "[ERROR] Fail to get current engine");
342 fprintf(err_fp, "current engine - %s", engine_id);
346 ttsd_data_save_error_log(uid, err_fp);