Add multi-user support
[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 /* For multi-user support */
19 #include <tzplatform_config.h>
20
21 #include "ttsd_main.h"
22 #include "ttsd_config.h"
23 #include "ttsd_engine_agent.h"
24 #include "ttsd_data.h"
25
26 #define CONFIG_DEFAULT                  BASE_DIRECTORY_DEFAULT"/ttsd.conf"
27
28 #define DEFAULT_ERROR_FILE_NAME         tzplatform_mkpath(TZ_USER_HOME, ".voice/ttsd_default.err")
29
30 #define ENGINE_ID       "ENGINE_ID"
31 #define VOICE           "VOICE"
32 #define SPEED           "SPEED"
33
34 static char*    g_engine_id;
35 static char*    g_language;
36 static int      g_vc_type;
37 static int      g_speed;
38
39 static ttsd_config_changed_cb g_callback;
40
41 int __ttsd_config_save()
42 {
43         if (0 != access(DEFAULT_CONFIG_FILE_NAME, R_OK|W_OK)) {
44                 if (0 == ecore_file_mkpath(CONFIG_DIRECTORY)) {
45                         SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to create directory (%s)", CONFIG_DIRECTORY);
46                 } else {
47                         SLOG(LOG_DEBUG, get_tag(), "[Config] Create directory (%s)", CONFIG_DIRECTORY);
48                 }
49         }
50
51         FILE* config_fp;
52         config_fp = fopen(DEFAULT_CONFIG_FILE_NAME, "w+");
53
54         if (NULL == config_fp) {
55                 /* make file and file default */
56                 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to open config (%s)", DEFAULT_CONFIG_FILE_NAME);
57                 return -1;
58         }
59
60         SLOG(LOG_DEBUG, get_tag(), "[Config] Rewrite config file");
61
62         /* Write engine id */
63         fprintf(config_fp, "%s %s\n", ENGINE_ID, g_engine_id);
64
65         /* Write voice */
66         fprintf(config_fp, "%s %s %d\n", VOICE, g_language, g_vc_type);
67
68         /* Read speed */
69         fprintf(config_fp, "%s %d\n", SPEED, g_speed);
70
71         fclose(config_fp);
72
73         return 0;
74 }
75
76 int __ttsd_config_load()
77 {
78         FILE* config_fp;
79         char buf_id[256] = {0};
80         char buf_param[256] = {0};
81         int int_param = 0;
82         bool is_default_open = false;
83
84         config_fp = fopen(DEFAULT_CONFIG_FILE_NAME, "r");
85
86         if (NULL == config_fp) {
87                 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Not open file(%s)", DEFAULT_CONFIG_FILE_NAME);
88                 
89                 config_fp = fopen(CONFIG_DEFAULT, "r");
90                 if (NULL == config_fp) {
91                         SLOG(LOG_ERROR, get_tag(), "[Config WARNING] Not open original config file(%s)", CONFIG_DEFAULT);
92                         return -1;
93                 }
94                 is_default_open = true;
95         }
96
97         /* Read engine id */
98         if (EOF == fscanf(config_fp, "%s %s", buf_id, buf_param)) {
99                 fclose(config_fp);
100                 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (engine id)");
101                 __ttsd_config_save();
102                 return -1;
103         } else {
104                 if (0 == strncmp(ENGINE_ID, buf_id, strlen(ENGINE_ID))) {
105                         g_engine_id = strdup(buf_param);
106                 } else {
107                         fclose(config_fp);
108                         SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (engine id)");
109                         __ttsd_config_save();
110                         return -1;
111                 }
112         }
113
114         /* Read voice */
115         if (EOF == fscanf(config_fp, "%s %s %d", buf_id, buf_param, &int_param)) {
116                 fclose(config_fp);
117                 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (voice)");
118                 __ttsd_config_save();
119                 return -1;
120         } else {
121                 if (0 == strncmp(VOICE, buf_id, strlen(VOICE))) {
122                         g_language = strdup(buf_param);
123                         g_vc_type = int_param;
124                 } else {
125                         fclose(config_fp);
126                         SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (voice)");
127                         __ttsd_config_save();
128                         return -1;
129                 }
130         }
131         
132         if (true == is_default_open) {
133                 /* Change default language to display language */
134                 char* value;
135                 value = vconf_get_str(VCONFKEY_LANGSET);
136
137                 if (NULL != value) {
138                         SLOG(LOG_DEBUG, get_tag(), "[Config] System language : %s", value);
139                         strncpy(g_language, value, strlen(g_language));
140                         SLOG(LOG_DEBUG, get_tag(), "[Config] Default language : %s", g_language);
141
142                         free(value);
143                 } else {
144                         SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Fail to get system language");
145                 }
146         }
147
148         /* Read speed */
149         if (EOF == fscanf(config_fp, "%s %d", buf_id, &int_param)) {
150                 fclose(config_fp);
151                 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (speed)");
152                 __ttsd_config_save();
153                 return -1;
154         } else {
155                 if (0 == strncmp(SPEED, buf_id, strlen(SPEED))) {
156                         g_speed = int_param;
157                 } else {
158                         fclose(config_fp);
159                         SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (speed)");
160                         __ttsd_config_save();
161                         return -1;
162                 }
163         }
164         
165         fclose(config_fp);
166
167         SLOG(LOG_DEBUG, get_tag(), "[Config] Load config : engine(%s), voice(%s,%d), speed(%d)",
168                 g_engine_id, g_language, g_vc_type, g_speed);
169
170         if (true == is_default_open) {
171                 if(0 == __ttsd_config_save()) {
172                         SLOG(LOG_DEBUG, get_tag(), "[Config] Create config(%s)", DEFAULT_CONFIG_FILE_NAME);
173                 }
174         }
175
176         return 0;
177 }
178
179 int ttsd_config_initialize(ttsd_config_changed_cb callback)
180 {
181         g_engine_id = NULL;
182         g_language = NULL;
183         g_vc_type = 1;
184         g_speed = 3;
185
186         g_callback = callback;
187         
188         ecore_file_mkpath(CONFIG_DIRECTORY);
189
190         __ttsd_config_load();
191
192         return 0;
193 }
194
195 int ttsd_config_finalize()
196 {
197         __ttsd_config_save();
198
199         if (NULL != g_language) {
200                 free(g_language);
201         }
202
203         if (NULL != g_engine_id) {
204                 free(g_engine_id);
205         }
206
207         return 0;
208 }
209
210 int ttsd_config_update_language()
211 {
212         /* no work in default mode */
213         return 0;
214 }
215
216 int ttsd_config_get_default_engine(char** engine_id)
217 {
218         if (NULL == engine_id)
219                 return -1;
220
221         if (NULL != g_engine_id) {
222                 *engine_id = strdup(g_engine_id);
223         } else {
224                 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current engine id is NULL");
225                 return -1;
226         }
227
228         return 0;
229 }
230
231 int ttsd_config_set_default_engine(const char* engine_id)
232 {
233         if (NULL == engine_id)
234                 return -1;
235
236         if (NULL != g_engine_id)
237                 free(g_engine_id);
238
239         g_engine_id = strdup(engine_id);
240
241         __ttsd_config_save();
242         return 0;
243 }
244
245 int ttsd_config_get_default_voice(char** language, int* type)
246 {
247         if (NULL == language || NULL == type)
248                 return -1;
249
250         if (NULL != g_language) {
251                 *language = strdup(g_language);
252                 *type = g_vc_type;
253         } else {
254                 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current language is NULL");
255                 return -1;
256         }
257         
258         return 0;
259 }
260
261 int ttsd_config_set_default_voice(const char* language, int type)
262 {
263         if (NULL == language)
264                 return -1;
265
266         if (NULL != g_language)
267                 free(g_language);
268
269         g_language = strdup(language);
270         g_vc_type = type;
271
272         __ttsd_config_save();
273         return 0;
274 }
275
276 int ttsd_config_get_default_speed(int* speed)
277 {
278         if (NULL == speed)
279                 return -1;
280
281         *speed = g_speed;
282
283         return 0;
284 }
285
286 int ttsd_config_set_default_speed(int speed)
287 {
288         g_speed = speed;
289
290         __ttsd_config_save();
291         return 0;
292 }
293
294 int ttsd_config_save_error(int uid, int uttid, const char* lang, int vctype, const char* text, 
295                            const char* func, int line, const char* message)
296 {
297         SLOG(LOG_ERROR, get_tag(), "=============== TTS ERROR LOG ====================");
298
299         SLOG(LOG_ERROR, get_tag(), "uid(%d) uttid(%d)", uid, uttid);
300         
301         if (NULL != func)       SLOG(LOG_ERROR, get_tag(), "Function(%s) Line(%d)", func, line);
302         if (NULL != message)    SLOG(LOG_ERROR, get_tag(), "Message(%s)", message);
303         if (NULL != lang)       SLOG(LOG_ERROR, get_tag(), "Lang(%s), type(%d)", lang, vctype);
304         if (NULL != text)       SLOG(LOG_ERROR, get_tag(), "Text(%s)", text);
305
306         SLOG(LOG_ERROR, get_tag(), "==================================================");
307
308         return 0;
309 }