support speech rate
[platform/core/uifw/tts.git] / server / ttsd_config.c
1 /*
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.
12 */
13
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"
20
21 #define CONFIG_DEFAULT                  BASE_DIRECTORY_DEFAULT"/ttsd.conf"
22
23 #define DEFAULT_CONFIG_FILE_NAME        CONFIG_DIRECTORY"/ttsd_default.conf"
24 #define DEFAULT_ERROR_FILE_NAME         CONFIG_DIRECTORY"/ttsd_default.err"
25
26 #define ENGINE_ID       "ENGINE_ID"
27 #define VOICE           "VOICE"
28 #define SPEED           "SPEED"
29
30 static char*    g_engine_id;
31 static char*    g_language;
32 static int      g_vc_type;
33 static int      g_speed;
34
35 static ttsd_config_lang_changed_cb g_lang_cb;
36 static ttsd_config_speed_changed_cb g_speed_cb;
37
38 int __ttsd_config_save()
39 {
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);
43                 } else {
44                         SLOG(LOG_DEBUG, get_tag(), "[Config] Create directory (%s)", CONFIG_DIRECTORY);
45                 }
46         }
47
48         FILE* config_fp;
49         config_fp = fopen(DEFAULT_CONFIG_FILE_NAME, "w+");
50
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);
54                 return -1;
55         }
56
57         SLOG(LOG_DEBUG, get_tag(), "[Config] Rewrite config file");
58
59         /* Write engine id */
60         fprintf(config_fp, "%s %s\n", ENGINE_ID, g_engine_id);
61
62         /* Write voice */
63         fprintf(config_fp, "%s %s %d\n", VOICE, g_language, g_vc_type);
64
65         /* Read speed */
66         fprintf(config_fp, "%s %d\n", SPEED, g_speed);
67
68         fclose(config_fp);
69
70         return 0;
71 }
72
73 int __ttsd_config_load()
74 {
75         FILE* config_fp;
76         char buf_id[256] = {0};
77         char buf_param[256] = {0};
78         int int_param = 0;
79         bool is_default_open = false;
80
81         config_fp = fopen(DEFAULT_CONFIG_FILE_NAME, "r");
82
83         if (NULL == config_fp) {
84                 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Not open file(%s)", DEFAULT_CONFIG_FILE_NAME);
85                 
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);
89                         return -1;
90                 }
91                 is_default_open = true;
92         }
93
94         /* Read engine id */
95         if (EOF == fscanf(config_fp, "%s %s", buf_id, buf_param)) {
96                 fclose(config_fp);
97                 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (engine id)");
98                 __ttsd_config_save();
99                 return -1;
100         } else {
101                 if (0 == strncmp(ENGINE_ID, buf_id, strlen(ENGINE_ID))) {
102                         g_engine_id = strdup(buf_param);
103                 } else {
104                         fclose(config_fp);
105                         SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (engine id)");
106                         __ttsd_config_save();
107                         return -1;
108                 }
109         }
110
111         /* Read voice */
112         if (EOF == fscanf(config_fp, "%s %s %d", buf_id, buf_param, &int_param)) {
113                 fclose(config_fp);
114                 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (voice)");
115                 __ttsd_config_save();
116                 return -1;
117         } else {
118                 if (0 == strncmp(VOICE, buf_id, strlen(VOICE))) {
119                         g_language = strdup(buf_param);
120                         g_vc_type = int_param;
121                 } else {
122                         fclose(config_fp);
123                         SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (voice)");
124                         __ttsd_config_save();
125                         return -1;
126                 }
127         }
128         
129         /* Read speed */
130         if (EOF == fscanf(config_fp, "%s %d", buf_id, &int_param)) {
131                 fclose(config_fp);
132                 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (speed)");
133                 __ttsd_config_save();
134                 return -1;
135         } else {
136                 if (0 == strncmp(SPEED, buf_id, strlen(SPEED))) {
137                         g_speed = int_param;
138                 } else {
139                         fclose(config_fp);
140                         SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (speed)");
141                         __ttsd_config_save();
142                         return -1;
143                 }
144         }
145         
146         fclose(config_fp);
147
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);
150
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);
154                 }
155         }
156
157         return 0;
158 }
159
160 int ttsd_config_initialize(ttsd_config_lang_changed_cb lang_cb, ttsd_config_speed_changed_cb speed_cb)
161 {
162         g_engine_id = NULL;
163         g_language = NULL;
164         g_vc_type = 1;
165         g_speed = 3;
166
167         g_lang_cb = lang_cb;
168         g_speed_cb = speed_cb;
169
170         ecore_file_mkpath(CONFIG_DIRECTORY);
171
172         __ttsd_config_load();
173
174         return 0;
175 }
176
177 int ttsd_config_finalize()
178 {
179         __ttsd_config_save();
180
181         if (NULL != g_language) {
182                 free(g_language);
183         }
184
185         if (NULL != g_engine_id) {
186                 free(g_engine_id);
187         }
188
189         return 0;
190 }
191
192 int ttsd_config_update_language()
193 {
194         /* no work in default mode */
195         return 0;
196 }
197
198 int ttsd_config_get_default_engine(char** engine_id)
199 {
200         if (NULL == engine_id)
201                 return -1;
202
203         if (NULL != g_engine_id) {
204                 *engine_id = strdup(g_engine_id);
205         } else {
206                 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current engine id is NULL");
207                 return -1;
208         }
209
210         return 0;
211 }
212
213 int ttsd_config_set_default_engine(const char* engine_id)
214 {
215         if (NULL == engine_id)
216                 return -1;
217
218         if (NULL != g_engine_id)
219                 free(g_engine_id);
220
221         g_engine_id = strdup(engine_id);
222
223         __ttsd_config_save();
224         return 0;
225 }
226
227 int ttsd_config_get_default_voice(char** language, int* type)
228 {
229         if (NULL == language || NULL == type)
230                 return -1;
231
232         if (NULL != g_language) {
233                 *language = strdup(g_language);
234                 *type = g_vc_type;
235         } else {
236                 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current language is NULL");
237                 return -1;
238         }
239         
240         return 0;
241 }
242
243 int ttsd_config_set_default_voice(const char* language, int type)
244 {
245         if (NULL == language)
246                 return -1;
247
248         if (NULL != g_language)
249                 free(g_language);
250
251         g_language = strdup(language);
252         g_vc_type = type;
253
254         __ttsd_config_save();
255         return 0;
256 }
257
258 int ttsd_config_get_default_speed(int* speed)
259 {
260         if (NULL == speed)
261                 return -1;
262
263         *speed = g_speed;
264
265         return 0;
266 }
267
268 int ttsd_config_set_default_speed(int speed)
269 {
270         g_speed = speed;
271
272         __ttsd_config_save();
273         return 0;
274 }
275
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)
278 {
279         FILE* err_fp;
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);
283                 return -1;
284         }
285         SLOG(LOG_DEBUG, get_tag(), "Save Error File (%s)", DEFAULT_ERROR_FILE_NAME);
286
287         /* func */
288         if (NULL != func) {
289                 fprintf(err_fp, "function - %s\n", func);
290         }
291         
292         /* line */
293         fprintf(err_fp, "line - %d\n", line);
294
295         /* message */
296         if (NULL != message) {
297                 fprintf(err_fp, "message - %s\n", message);
298         }
299
300         int ret;
301         /* uid */
302         fprintf(err_fp, "uid - %d\n", uid);
303         
304         /* uttid */
305         fprintf(err_fp, "uttid - %d\n", uttid);
306
307         /* lang */
308         if (NULL != lang) {
309                 fprintf(err_fp, "language - %s\n", lang);
310         }
311
312         /* vctype */
313         fprintf(err_fp, "vctype - %d\n", vctype);
314
315         /* text */
316         if (NULL != text) {
317                 fprintf(err_fp, "text - %s\n", text);
318         }
319
320         /* get current engine */
321         char *engine_id = NULL;
322
323         ret = ttsd_engine_setting_get_engine(&engine_id);
324         if (0 != ret) {
325                 SLOG(LOG_ERROR, get_tag(), "[ERROR] Fail to get current engine");
326         } else {
327                 fprintf(err_fp, "current engine - %s", engine_id);
328         }
329
330         /* get data */
331         ttsd_data_save_error_log(uid, err_fp);
332
333         fclose(err_fp);
334
335         return 0;
336 }