Release version 0.1.60
[platform/core/uifw/tts.git] / server / ttsd_config_sr.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 <vconf-internal-keys.h>
16 #include <vconf.h>
17 #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"
22
23 #define SR_ERROR_FILE_NAME              CONFIG_DIRECTORY"/ttsd_sr.err"
24
25 #define ENGINE_ID       "ENGINE_ID"
26
27 static char*    g_engine_id;
28 static char*    g_language;
29 static int      g_vc_type;
30 static int      g_speed;
31
32 static ttsd_config_changed_cb g_callback;
33
34 void __sr_system_language_changed_cb(runtime_info_key_e key, void *user_data)
35 {
36         if (RUNTIME_INFO_KEY_LANGUAGE == key) {
37                 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
38                         int ret = -1;
39                         char *value;
40
41                         ret = runtime_info_get_value_string(RUNTIME_INFO_KEY_LANGUAGE, &value);
42                         if (0 != ret) {
43                                 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Fail to get system language : %d", ret);
44                                 return;
45                         } else {
46                                 SLOG(LOG_DEBUG, get_tag(), "[Config] System language changed : %s", value);
47                                 if (NULL != g_callback)
48                                         g_callback(TTS_CONFIG_TYPE_VOICE, value, g_vc_type);
49                 
50                                 if (NULL != value)
51                                         free(value);
52                         }
53                 }
54         }
55
56         return;
57 }
58
59 void __sr_speech_rate_changed_cb(keynode_t *key, void *data)
60 {
61         char buf[255];
62         sprintf(buf, "%s", vconf_keynode_get_name(key));
63
64         if (!strcmp(buf, VCONFKEY_SETAPPL_ACCESSIBILITY_SPEECH_RATE)) {
65                 int temp_speech_rate = vconf_keynode_get_int(key);
66
67                 temp_speech_rate ++;
68                 if (g_speed != temp_speech_rate) {
69                         SLOG(LOG_DEBUG, get_tag(), "[Config] Speech rate changed : current(%d), previous(%d)", 
70                                 temp_speech_rate, g_speed);
71                         g_speed = temp_speech_rate;
72
73                         if (NULL != g_callback)
74                                 g_callback(TTS_CONFIG_TYPE_SPEED, NULL, g_speed);
75                 }
76         }
77 }
78
79 int ttsd_config_initialize(ttsd_config_changed_cb callback)
80 {
81         int ret = -1;
82         char* value = NULL;
83         FILE* config_fp;
84         char buf_id[256] = {0};
85         char buf_param[256] = {0};
86
87         g_engine_id = NULL;
88         g_language = NULL;
89         g_vc_type = 2;
90         g_speed = 3;
91
92         g_callback = callback;
93
94         ecore_file_mkpath(CONFIG_DIRECTORY);
95
96         /* Get default engine id */
97         config_fp = fopen(CONFIG_DEFAULT, "r");
98         if (NULL == config_fp) {
99                 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Not open original config file(%s)", CONFIG_DEFAULT);
100                 return -1;
101         }
102
103         /* Read engine id */
104         if (EOF == fscanf(config_fp, "%s %s", buf_id, buf_param)) {
105                 fclose(config_fp);
106                 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Fail to read config (engine id)");
107                 return -1;
108         }
109
110         if (0 == strncmp(ENGINE_ID, buf_id, strlen(ENGINE_ID))) {
111                 g_engine_id = strdup(buf_param);
112                 SLOG(LOG_DEBUG, get_tag(), "[Config] Current engine id : %s", g_engine_id);
113
114                 fclose(config_fp);
115         } else {
116                 fclose(config_fp);
117                 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (engine id)");
118                 return -1;
119         }
120
121         /* Get display language */
122         ret = runtime_info_get_value_string(RUNTIME_INFO_KEY_LANGUAGE, &value);
123         if (0 != ret || NULL == value) {
124                 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Fail to get system language : %d", ret);
125                 return -1;
126         } else {
127                 SLOG(LOG_DEBUG, get_tag(), "[Config] Current system language : %s", value);
128
129                 g_language = strdup(value);
130
131                 if (NULL != value)
132                         free(value);
133         }
134
135         /* Register system language changed callback */
136         ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_LANGUAGE, __sr_system_language_changed_cb, NULL);
137         if (0 != ret) {
138                 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Fail to register callback : %d", ret);
139                 return TTSD_ERROR_OPERATION_FAILED;
140         }
141
142         /* Get speech rate */
143         ret = vconf_get_int(VCONFKEY_SETAPPL_ACCESSIBILITY_SPEECH_RATE, &g_speed);
144         
145         /**
146         * vconf key value scope
147         * 0 : Very slow 
148         * 1 : Slow 
149         * 2 : Normal 
150         * 3 : Fast 
151         * 4 : Very fast 
152         */
153         /* vconf key value is between 0 ~ 4 but tts speech rate value is between 1 ~ 5 */
154         g_speed ++;
155         SLOG(LOG_DEBUG, get_tag(), "[Config] Current speech rate : %d", g_speed);
156
157         /* register callback function */
158         vconf_notify_key_changed (VCONFKEY_SETAPPL_ACCESSIBILITY_SPEECH_RATE, __sr_speech_rate_changed_cb, NULL);
159
160         return 0;
161 }
162
163 int ttsd_config_finalize()
164 {
165         vconf_ignore_key_changed (VCONFKEY_SETAPPL_ACCESSIBILITY_SPEECH_RATE, __sr_speech_rate_changed_cb);
166
167         if (NULL != g_language) {
168                 free(g_language);
169         }
170
171         if (NULL != g_engine_id) {
172                 free(g_engine_id);
173         }
174         
175         return 0;
176 }
177
178 int ttsd_config_update_language()
179 {
180         if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
181                 int ret = -1;
182                 char *value;
183
184                 ret = runtime_info_get_value_string(RUNTIME_INFO_KEY_LANGUAGE, &value);
185                 if (0 != ret) {
186                         SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Fail to get system language : %d", ret);
187                         return -1;
188                 } else {
189                         if (NULL == value) {
190                                 SLOG(LOG_ERROR, get_tag(), "[Config] Fail to get system language");
191                                 return -1;
192                         } else {
193                                 SLOG(LOG_DEBUG, get_tag(), "[Config] System language : %s", value);
194
195                                 if (0 != strcmp(value, g_language)) {
196                                         if (NULL != g_callback)
197                                                 g_callback(TTS_CONFIG_TYPE_VOICE, value, g_vc_type);
198                                 }
199
200                                 free(value);
201                         }
202                 }
203         }
204
205         return 0;
206 }
207
208 int ttsd_config_get_default_engine(char** engine_id)
209 {
210         if (NULL == engine_id)
211                 return -1;
212
213         if (NULL != g_engine_id) {
214                 *engine_id = strdup(g_engine_id);
215         } else {
216                 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current engine id is NULL");
217                 return -1;
218         }
219         return 0;
220 }
221
222 int ttsd_config_set_default_engine(const char* engine_id)
223 {
224         /* Not available in screen mode */
225         return 0;
226 }
227
228 int ttsd_config_get_default_voice(char** language, int* type)
229 {
230         if (NULL == language || NULL == type)
231                 return -1;
232
233         if (NULL != g_language) {
234                 *language = strdup(g_language);
235                 *type = g_vc_type;
236         } else {
237                 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current language is NULL");
238                 return -1;
239         }
240         return 0;
241 }
242
243 int ttsd_config_set_default_voice(const char* language, int type)
244 {
245         /* Not available in screen mode */
246         return 0;
247 }
248
249 int ttsd_config_get_default_speed(int* speed)
250 {
251         if (NULL == speed)
252                 return -1;
253
254         *speed = g_speed;
255
256         return 0;
257 }
258
259 int ttsd_config_set_default_speed(int speed)
260 {
261         /* Not available in screen mode */
262         return 0;
263 }
264
265 int ttsd_config_save_error(int uid, int uttid, const char* lang, int vctype, const char* text, 
266                            const char* func, int line, const char* message)
267 {
268         FILE* err_fp;
269         err_fp = fopen(SR_ERROR_FILE_NAME, "a");
270         if (NULL == err_fp) {
271                 SLOG(LOG_WARN, get_tag(), "[WARNING] Fail to open error file (%s)", SR_ERROR_FILE_NAME);
272                 return -1;
273         }
274         SLOG(LOG_DEBUG, get_tag(), "Save Error File (%s)", SR_ERROR_FILE_NAME);
275
276         /* func */
277         if (NULL != func) {
278                 fprintf(err_fp, "function - %s\n", func);
279         }
280         
281         /* line */
282         fprintf(err_fp, "line - %d\n", line);
283
284         /* message */
285         if (NULL != message) {
286                 fprintf(err_fp, "message - %s\n", message);
287         }
288
289         int ret;
290         /* uid */
291         fprintf(err_fp, "uid - %d\n", uid);
292         
293         /* uttid */
294         fprintf(err_fp, "uttid - %d\n", uttid);
295
296         /* lang */
297         if (NULL != lang) {
298                 fprintf(err_fp, "language - %s\n", lang);
299         }
300
301         /* vctype */
302         fprintf(err_fp, "vctype - %d\n", vctype);
303
304         /* text */
305         if (NULL != text) {
306                 fprintf(err_fp, "text - %s\n", text);
307         }
308
309         /* get current engine */
310         char *engine_id = NULL;
311
312         ret = ttsd_engine_setting_get_engine(&engine_id);
313         if (0 != ret) {
314                 SLOG(LOG_ERROR, get_tag(), "[ERROR] Fail to get current engine");
315         } else {
316                 fprintf(err_fp, "current engine - %s", engine_id);
317         }
318
319         /* get data */
320         ttsd_data_save_error_log(uid, err_fp);
321
322         fclose(err_fp);
323
324         return 0;
325 }