4632c3c607bd6f38927ab7ce5ac830bca07d11b4
[platform/core/uifw/stt.git] / server / sttd_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
16 /* For multi-user support */
17 #include <tzplatform_config.h>
18
19 #include "sttd_main.h"
20 #include "sttd_config.h"
21
22
23 #define CONFIG_FILE_PATH        tzplatform_mkpath(TZ_USER_HOME, ".voice/sttd.conf")
24 #define CONFIG_DEFAULT          BASE_DIRECTORY_DEFAULT"/sttd.conf"
25
26 #define ENGINE_ID       "ENGINE_ID"
27 #define LANGUAGE        "LANGUAGE"
28 #define SILENCE         "SILENCE"
29 #define PROFANITY       "PROFANITY"
30 #define PUNCTUATION     "PUNCTUATION"
31
32
33 static char*    g_engine_id;
34 static char*    g_language;
35 static int      g_silence;
36 static int      g_profanity;
37 static int      g_punctuation;
38
39 int __sttd_config_save()
40 {
41         if (0 != access(CONFIG_FILE_PATH, R_OK|W_OK)) {
42                 if (0 == ecore_file_mkpath(CONFIG_DIRECTORY)) {
43                         SLOG(LOG_ERROR, TAG_STTD, "[Config ERROR ] Fail to create directory (%s)", CONFIG_DIRECTORY);
44                         return -1;
45                 }
46
47                 SLOG(LOG_WARN, TAG_STTD, "[Config] Create directory (%s)", CONFIG_DIRECTORY);
48         }
49
50         FILE* config_fp;
51         config_fp = fopen(CONFIG_FILE_PATH, "w+");
52
53         if (NULL == config_fp) {
54                 /* make file and file default */
55                 SLOG(LOG_ERROR, TAG_STTD, "[Config ERROR] Fail to load config (engine id)");
56                 return -1;
57         }
58
59         SLOG(LOG_DEBUG, TAG_STTD, "[Config] Rewrite config file");
60
61         /* Write engine id */
62         fprintf(config_fp, "%s %s\n", ENGINE_ID, g_engine_id);
63
64         /* Write language */
65         fprintf(config_fp, "%s %s\n", LANGUAGE, g_language);
66
67         /* Write silence detection */
68         fprintf(config_fp, "%s %d\n", SILENCE, g_silence);
69
70         /* Write profanity */
71         fprintf(config_fp, "%s %d\n", PROFANITY, g_profanity);
72
73         /* Write punctuation */
74         fprintf(config_fp, "%s %d\n", PUNCTUATION, g_punctuation);
75
76         fclose(config_fp);
77
78         return 0;
79 }
80
81 int __sttd_config_load()
82 {
83         FILE* config_fp;
84         char buf_id[256] = {0};
85         char buf_param[256] = {0};
86         int int_param = 0;
87         bool is_default_open = false;
88
89         config_fp = fopen(CONFIG_FILE_PATH, "r");
90
91         if (NULL == config_fp) {
92                 SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Not open file(%s)", CONFIG_FILE_PATH);
93
94                 config_fp = fopen(CONFIG_DEFAULT, "r");
95                 if (NULL == config_fp) {
96                         SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Not open original config file(%s)", CONFIG_FILE_PATH);
97                         __sttd_config_save();
98                         return 0;
99                 }
100                 is_default_open = true;
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_WARN, TAG_STTD, "[Config WARNING] Fail to load config (engine id)");
107                 __sttd_config_save();
108                 return 0;
109         } else {
110                 if (0 == strncmp(ENGINE_ID, buf_id, strlen(ENGINE_ID))) {
111                         g_engine_id = strdup(buf_param);
112                 } else {
113                         fclose(config_fp);
114                         SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Fail to load config (engine id)");
115                         __sttd_config_save();
116                         return 0;
117                 }
118         }
119
120         /* Read language */
121         if (EOF == fscanf(config_fp, "%s %s", buf_id, buf_param)) {
122                 fclose(config_fp);
123                 SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Fail to load config (language)");
124                 __sttd_config_save();
125                 return 0;
126         } else {
127                 if (0 == strncmp(LANGUAGE, buf_id, strlen(LANGUAGE))) {
128                         g_language = strdup(buf_param);
129                 } else {
130                         fclose(config_fp);
131                         SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Fail to load config (language)");
132                         __sttd_config_save();
133                         return 0;
134                 }
135         }
136         
137         /* Read silence detection */
138         if (EOF == fscanf(config_fp, "%s %d", buf_id, &int_param)) {
139                 fclose(config_fp);
140                 SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Fail to load config (silence)");
141                 __sttd_config_save();
142                 return 0;
143         } else {
144                 if (0 == strncmp(SILENCE, buf_id, strlen(SILENCE))) {
145                         g_silence = int_param;
146                 } else {
147                         fclose(config_fp);
148                         SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Fail to load config (silence)");
149                         __sttd_config_save();
150                         return 0;
151                 }
152         }
153
154         /* Read profanity filter */
155         if (EOF == fscanf(config_fp, "%s %d", buf_id, &int_param)) {
156                 fclose(config_fp);
157                 SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Fail to load config (profanity filter)");
158                 __sttd_config_save();
159                 return 0;
160         } else {
161                 if (0 == strncmp(PROFANITY, buf_id, strlen(PROFANITY))) {
162                         g_profanity = int_param;
163                 } else {
164                         fclose(config_fp);
165                         SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Fail to load config (profanity filter)");
166                         __sttd_config_save();
167                         return 0;
168                 }
169         }
170         
171
172         /* Read punctuation override */
173         if (EOF == fscanf(config_fp, "%s %d", buf_id, &int_param)) {
174                 fclose(config_fp);
175                 SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Fail to load config (punctuation override)");
176                 __sttd_config_save();
177                 return 0;
178         } else {
179                 if (0 == strncmp(PUNCTUATION, buf_id, strlen(PUNCTUATION))) {
180                         g_punctuation = int_param;
181                 } else {
182                         fclose(config_fp);
183                         SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Fail to load config (punctuation override)");
184                         __sttd_config_save();
185                         return 0;
186                 }
187         }
188         
189         fclose(config_fp);
190
191         SLOG(LOG_DEBUG, TAG_STTD, "[Config] Load config : engine(%s), language(%s), silence(%d), profanity(%d), punctuation(%d)",
192                 g_engine_id, g_language, g_silence, g_profanity, g_punctuation);
193
194         if (true == is_default_open) {
195                 if(0 == __sttd_config_save()) {
196                         SLOG(LOG_DEBUG, TAG_STTD, "[Config] Create config(%s)", CONFIG_FILE_PATH);
197                 }
198         }
199
200         return 0;
201 }
202
203 int sttd_config_initialize()
204 {
205         g_engine_id = NULL;
206         g_language = NULL;
207         g_silence = 1;
208         g_profanity = 1;
209         g_punctuation = 0;
210
211         __sttd_config_load();
212
213         return 0;
214 }
215
216 int sttd_config_finalize()
217 {
218         __sttd_config_save();
219         return 0;
220 }
221
222 int sttd_config_get_default_engine(char** engine_id)
223 {
224         if (NULL == engine_id)
225                 return -1;
226
227         *engine_id = strdup(g_engine_id);
228         return 0;
229 }
230
231 int sttd_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         __sttd_config_save();
241         return 0;
242 }
243
244 int sttd_config_get_default_language(char** language)
245 {
246         if (NULL == language)
247                 return -1;
248
249         *language = strdup(g_language);
250
251         return 0;
252 }
253
254 int sttd_config_set_default_language(const char* language)
255 {
256         if (NULL == language)
257                 return -1;
258
259         if (NULL != g_language)
260                 free(g_language);
261
262         g_language = strdup(language);
263
264         __sttd_config_save();
265
266         return 0;
267 }
268
269 int sttd_config_get_default_silence_detection(int* silence)
270 {
271         if (NULL == silence)
272                 return -1;
273
274         *silence = g_silence;
275
276         return 0;
277 }
278
279 int sttd_config_set_default_silence_detection(int silence)
280 {
281         g_silence = silence;
282         __sttd_config_save();
283         return 0;
284 }
285
286 int sttd_config_get_default_profanity_filter(int* profanity)
287 {
288         if (NULL == profanity)
289                 return -1;
290
291         *profanity = g_profanity;
292
293         return 0;
294 }
295
296 int sttd_config_set_default_profanity_filter(int profanity)
297 {
298         g_profanity = profanity;
299         __sttd_config_save();
300         return 0;
301 }
302
303 int sttd_config_get_default_punctuation_override(int* punctuation)
304 {
305         if (NULL == punctuation)
306                 return -1;
307
308         *punctuation = g_punctuation;
309
310         return 0;
311 }
312
313 int sttd_config_set_default_punctuation_override(int punctuation)
314 {
315         g_punctuation = punctuation;
316         __sttd_config_save();
317         return 0;
318 }