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_ERROR_FILE_NAME CONFIG_DIRECTORY"/ttsd_default.err"
25 #define ENGINE_ID "ENGINE_ID"
29 static char* g_engine_id;
30 static char* g_language;
34 static ttsd_config_changed_cb g_callback;
36 int __ttsd_config_save()
38 if (0 != access(DEFAULT_CONFIG_FILE_NAME, R_OK|W_OK)) {
39 if (0 == ecore_file_mkpath(CONFIG_DIRECTORY)) {
40 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to create directory (%s)", CONFIG_DIRECTORY);
42 SLOG(LOG_DEBUG, get_tag(), "[Config] Create directory (%s)", CONFIG_DIRECTORY);
47 config_fp = fopen(DEFAULT_CONFIG_FILE_NAME, "w+");
49 if (NULL == config_fp) {
50 /* make file and file default */
51 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to open config (%s)", DEFAULT_CONFIG_FILE_NAME);
55 SLOG(LOG_DEBUG, get_tag(), "[Config] Rewrite config file");
58 fprintf(config_fp, "%s %s\n", ENGINE_ID, g_engine_id);
61 fprintf(config_fp, "%s %s %d\n", VOICE, g_language, g_vc_type);
64 fprintf(config_fp, "%s %d\n", SPEED, g_speed);
71 int __ttsd_config_load()
74 char buf_id[256] = {0};
75 char buf_param[256] = {0};
77 bool is_default_open = false;
79 config_fp = fopen(DEFAULT_CONFIG_FILE_NAME, "r");
81 if (NULL == config_fp) {
82 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Not open file(%s)", DEFAULT_CONFIG_FILE_NAME);
84 config_fp = fopen(CONFIG_DEFAULT, "r");
85 if (NULL == config_fp) {
86 SLOG(LOG_ERROR, get_tag(), "[Config WARNING] Not open original config file(%s)", CONFIG_DEFAULT);
89 is_default_open = true;
93 if (EOF == fscanf(config_fp, "%s %s", buf_id, buf_param)) {
95 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (engine id)");
99 if (0 == strncmp(ENGINE_ID, buf_id, strlen(ENGINE_ID))) {
100 g_engine_id = strdup(buf_param);
103 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (engine id)");
104 __ttsd_config_save();
110 if (EOF == fscanf(config_fp, "%s %s %d", buf_id, buf_param, &int_param)) {
112 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (voice)");
113 __ttsd_config_save();
116 if (0 == strncmp(VOICE, buf_id, strlen(VOICE))) {
117 g_language = strdup(buf_param);
118 g_vc_type = int_param;
121 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (voice)");
122 __ttsd_config_save();
128 if (EOF == fscanf(config_fp, "%s %d", buf_id, &int_param)) {
130 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (speed)");
131 __ttsd_config_save();
134 if (0 == strncmp(SPEED, buf_id, strlen(SPEED))) {
138 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (speed)");
139 __ttsd_config_save();
146 SLOG(LOG_DEBUG, get_tag(), "[Config] Load config : engine(%s), voice(%s,%d), speed(%d)",
147 g_engine_id, g_language, g_vc_type, g_speed);
149 if (true == is_default_open) {
150 if(0 == __ttsd_config_save()) {
151 SLOG(LOG_DEBUG, get_tag(), "[Config] Create config(%s)", DEFAULT_CONFIG_FILE_NAME);
158 int ttsd_config_initialize(ttsd_config_changed_cb callback)
165 g_callback = callback;
167 ecore_file_mkpath(CONFIG_DIRECTORY);
169 __ttsd_config_load();
174 int ttsd_config_finalize()
176 __ttsd_config_save();
178 if (NULL != g_language) {
182 if (NULL != g_engine_id) {
189 int ttsd_config_update_language()
191 /* no work in default mode */
195 int ttsd_config_get_default_engine(char** engine_id)
197 if (NULL == engine_id)
200 if (NULL != g_engine_id) {
201 *engine_id = strdup(g_engine_id);
203 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current engine id is NULL");
210 int ttsd_config_set_default_engine(const char* engine_id)
212 if (NULL == engine_id)
215 if (NULL != g_engine_id)
218 g_engine_id = strdup(engine_id);
220 __ttsd_config_save();
224 int ttsd_config_get_default_voice(char** language, int* type)
226 if (NULL == language || NULL == type)
229 if (NULL != g_language) {
230 *language = strdup(g_language);
233 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current language is NULL");
240 int ttsd_config_set_default_voice(const char* language, int type)
242 if (NULL == language)
245 if (NULL != g_language)
248 g_language = strdup(language);
251 __ttsd_config_save();
255 int ttsd_config_get_default_speed(int* speed)
265 int ttsd_config_set_default_speed(int speed)
269 __ttsd_config_save();
273 int ttsd_config_save_error(int uid, int uttid, const char* lang, int vctype, const char* text,
274 const char* func, int line, const char* message)
277 err_fp = fopen(DEFAULT_ERROR_FILE_NAME, "a");
278 if (NULL == err_fp) {
279 SLOG(LOG_WARN, get_tag(), "[WARNING] Fail to open error file (%s)", DEFAULT_ERROR_FILE_NAME);
282 SLOG(LOG_DEBUG, get_tag(), "Save Error File (%s)", DEFAULT_ERROR_FILE_NAME);
286 fprintf(err_fp, "function - %s\n", func);
290 fprintf(err_fp, "line - %d\n", line);
293 if (NULL != message) {
294 fprintf(err_fp, "message - %s\n", message);
299 fprintf(err_fp, "uid - %d\n", uid);
302 fprintf(err_fp, "uttid - %d\n", uttid);
306 fprintf(err_fp, "language - %s\n", lang);
310 fprintf(err_fp, "vctype - %d\n", vctype);
314 fprintf(err_fp, "text - %s\n", text);
317 /* get current engine */
318 char *engine_id = NULL;
320 ret = ttsd_engine_setting_get_engine(&engine_id);
322 SLOG(LOG_ERROR, get_tag(), "[ERROR] Fail to get current engine");
324 fprintf(err_fp, "current engine - %s", engine_id);
328 ttsd_data_save_error_log(uid, err_fp);