Fix error handling
[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 <vconf.h>
17
18 #include "ttsd_main.h"
19 #include "ttsd_config.h"
20 #include "ttsd_engine_agent.h"
21 #include "ttsd_data.h"
22
23 #define CONFIG_DEFAULT                  BASE_DIRECTORY_DEFAULT"/ttsd.conf"
24
25 #define DEFAULT_ERROR_FILE_NAME         CONFIG_DIRECTORY"/ttsd_default.err"
26
27 #define ENGINE_ID       "ENGINE_ID"
28 #define VOICE           "VOICE"
29 #define SPEED           "SPEED"
30
31 static char*    g_engine_id;
32 static char*    g_language;
33 static int      g_vc_type;
34 static int      g_speed;
35
36 static ttsd_config_changed_cb g_callback;
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         if (true == is_default_open) {
130                 /* Change default language to display language */
131                 char* value;
132                 value = vconf_get_str(VCONFKEY_LANGSET);
133
134                 if (NULL != value) {
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);
138
139                         free(value);
140                 } else {
141                         SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Fail to get system language");
142                 }
143         }
144
145         /* Read speed */
146         if (EOF == fscanf(config_fp, "%s %d", buf_id, &int_param)) {
147                 fclose(config_fp);
148                 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (speed)");
149                 __ttsd_config_save();
150                 return -1;
151         } else {
152                 if (0 == strncmp(SPEED, buf_id, strlen(SPEED))) {
153                         g_speed = int_param;
154                 } else {
155                         fclose(config_fp);
156                         SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (speed)");
157                         __ttsd_config_save();
158                         return -1;
159                 }
160         }
161         
162         fclose(config_fp);
163
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);
166
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);
170                 }
171         }
172
173         return 0;
174 }
175
176 int ttsd_config_initialize(ttsd_config_changed_cb callback)
177 {
178         g_engine_id = NULL;
179         g_language = NULL;
180         g_vc_type = 1;
181         g_speed = 3;
182
183         g_callback = callback;
184         
185         ecore_file_mkpath(CONFIG_DIRECTORY);
186
187         __ttsd_config_load();
188
189         return 0;
190 }
191
192 int ttsd_config_finalize()
193 {
194         __ttsd_config_save();
195
196         if (NULL != g_language) {
197                 free(g_language);
198         }
199
200         if (NULL != g_engine_id) {
201                 free(g_engine_id);
202         }
203
204         return 0;
205 }
206
207 int ttsd_config_update_language()
208 {
209         /* no work in default mode */
210         return 0;
211 }
212
213 int ttsd_config_get_default_engine(char** engine_id)
214 {
215         if (NULL == engine_id)
216                 return -1;
217
218         if (NULL != g_engine_id) {
219                 *engine_id = strdup(g_engine_id);
220         } else {
221                 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current engine id is NULL");
222                 return -1;
223         }
224
225         return 0;
226 }
227
228 int ttsd_config_set_default_engine(const char* engine_id)
229 {
230         if (NULL == engine_id)
231                 return -1;
232
233         if (NULL != g_engine_id)
234                 free(g_engine_id);
235
236         g_engine_id = strdup(engine_id);
237
238         __ttsd_config_save();
239         return 0;
240 }
241
242 int ttsd_config_get_default_voice(char** language, int* type)
243 {
244         if (NULL == language || NULL == type)
245                 return -1;
246
247         if (NULL != g_language) {
248                 *language = strdup(g_language);
249                 *type = g_vc_type;
250         } else {
251                 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current language is NULL");
252                 return -1;
253         }
254         
255         return 0;
256 }
257
258 int ttsd_config_set_default_voice(const char* language, int type)
259 {
260         if (NULL == language)
261                 return -1;
262
263         if (NULL != g_language)
264                 free(g_language);
265
266         g_language = strdup(language);
267         g_vc_type = type;
268
269         __ttsd_config_save();
270         return 0;
271 }
272
273 int ttsd_config_get_default_speed(int* speed)
274 {
275         if (NULL == speed)
276                 return -1;
277
278         *speed = g_speed;
279
280         return 0;
281 }
282
283 int ttsd_config_set_default_speed(int speed)
284 {
285         g_speed = speed;
286
287         __ttsd_config_save();
288         return 0;
289 }
290
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)
293 {
294         SLOG(LOG_ERROR, get_tag(), "=============== TTS ERROR LOG ====================");
295
296         SLOG(LOG_ERROR, get_tag(), "uid(%d) uttid(%d)", uid, uttid);
297         
298         if (NULL != func)       SLOG(LOG_ERROR, get_tag(), "Function(%s) Line(%d)", func, line);
299         if (NULL != message)    SLOG(LOG_ERROR, get_tag(), "Message(%s)", message);
300         if (NULL != lang)       SLOG(LOG_ERROR, get_tag(), "Lang(%s), type(%d)", lang, vctype);
301         if (NULL != text)       SLOG(LOG_ERROR, get_tag(), "Text(%s)", text);
302
303         SLOG(LOG_ERROR, get_tag(), "==================================================");
304
305         return 0;
306 }