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 CONFIG_DIRECTORY"/ttsd_default.conf"
24 #define DEFAULT_ERROR_FILE_NAME CONFIG_DIRECTORY"/ttsd_default.err"
26 #define ENGINE_ID "ENGINE_ID"
30 static char* g_engine_id;
31 static char* g_language;
35 static ttsd_config_lang_changed_cb g_lang_cb;
36 static ttsd_config_speed_changed_cb g_speed_cb;
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();
130 if (EOF == fscanf(config_fp, "%s %d", buf_id, &int_param)) {
132 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (speed)");
133 __ttsd_config_save();
136 if (0 == strncmp(SPEED, buf_id, strlen(SPEED))) {
140 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (speed)");
141 __ttsd_config_save();
148 SLOG(LOG_DEBUG, get_tag(), "[Config] Load config : engine(%s), voice(%s,%d), speed(%d)",
149 g_engine_id, g_language, g_vc_type, g_speed);
151 if (true == is_default_open) {
152 if(0 == __ttsd_config_save()) {
153 SLOG(LOG_DEBUG, get_tag(), "[Config] Create config(%s)", DEFAULT_CONFIG_FILE_NAME);
160 int ttsd_config_initialize(ttsd_config_lang_changed_cb lang_cb, ttsd_config_speed_changed_cb speed_cb)
168 g_speed_cb = speed_cb;
170 ecore_file_mkpath(CONFIG_DIRECTORY);
172 __ttsd_config_load();
177 int ttsd_config_finalize()
179 __ttsd_config_save();
181 if (NULL != g_language) {
185 if (NULL != g_engine_id) {
192 int ttsd_config_update_language()
194 /* no work in default mode */
198 int ttsd_config_get_default_engine(char** engine_id)
200 if (NULL == engine_id)
203 if (NULL != g_engine_id) {
204 *engine_id = strdup(g_engine_id);
206 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current engine id is NULL");
213 int ttsd_config_set_default_engine(const char* engine_id)
215 if (NULL == engine_id)
218 if (NULL != g_engine_id)
221 g_engine_id = strdup(engine_id);
223 __ttsd_config_save();
227 int ttsd_config_get_default_voice(char** language, int* type)
229 if (NULL == language || NULL == type)
232 if (NULL != g_language) {
233 *language = strdup(g_language);
236 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current language is NULL");
243 int ttsd_config_set_default_voice(const char* language, int type)
245 if (NULL == language)
248 if (NULL != g_language)
251 g_language = strdup(language);
254 __ttsd_config_save();
258 int ttsd_config_get_default_speed(int* speed)
268 int ttsd_config_set_default_speed(int speed)
272 __ttsd_config_save();
276 int ttsd_config_save_error(int uid, int uttid, const char* lang, int vctype, const char* text,
277 const char* func, int line, const char* message)
280 err_fp = fopen(DEFAULT_ERROR_FILE_NAME, "a");
281 if (NULL == err_fp) {
282 SLOG(LOG_WARN, get_tag(), "[WARNING] Fail to open error file (%s)", DEFAULT_ERROR_FILE_NAME);
285 SLOG(LOG_DEBUG, get_tag(), "Save Error File (%s)", DEFAULT_ERROR_FILE_NAME);
289 fprintf(err_fp, "function - %s\n", func);
293 fprintf(err_fp, "line - %d\n", line);
296 if (NULL != message) {
297 fprintf(err_fp, "message - %s\n", message);
302 fprintf(err_fp, "uid - %d\n", uid);
305 fprintf(err_fp, "uttid - %d\n", uttid);
309 fprintf(err_fp, "language - %s\n", lang);
313 fprintf(err_fp, "vctype - %d\n", vctype);
317 fprintf(err_fp, "text - %s\n", text);
320 /* get current engine */
321 char *engine_id = NULL;
323 ret = ttsd_engine_setting_get_engine(&engine_id);
325 SLOG(LOG_ERROR, get_tag(), "[ERROR] Fail to get current engine");
327 fprintf(err_fp, "current engine - %s", engine_id);
331 ttsd_data_save_error_log(uid, err_fp);