Fix the prevent issue
[platform/core/uifw/tts.git] / server / ttsd_config.c
1 /*
2 *  Copyright (c) 2011 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 "ttsd_main.h"
16 #include "ttsd_config.h"
17
18 #define CONFIG_FILE_PATH        BASE_DIRECTORY_DOWNLOAD"/ttsd.conf"
19 #define CONFIG_DEFAULT          BASE_DIRECTORY_DEFAULT"/ttsd.conf"
20
21 #define ENGINE_ID       "ENGINE_ID"
22 #define VOICE           "VOICE"
23 #define SPEED           "SPEED"
24
25
26 static char*    g_engine_id;
27 static char*    g_language;
28 static int      g_vc_type;
29 static int      g_speed;
30
31 int __ttsd_config_save()
32 {
33         if (0 != access(CONFIG_FILE_PATH, R_OK|W_OK)) {
34                 if (0 == ecore_file_mkpath(BASE_DIRECTORY_DOWNLOAD)) {
35                         SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR ] Fail to create directory (%s)", BASE_DIRECTORY_DOWNLOAD);
36                         return -1;
37                 }
38
39                 SLOG(LOG_WARN, TAG_TTSD, "[Config] Create directory (%s)", BASE_DIRECTORY_DOWNLOAD);
40         }
41
42         FILE* config_fp;
43         config_fp = fopen(CONFIG_FILE_PATH, "w+");
44
45         if (NULL == config_fp) {
46                 /* make file and file default */
47                 SLOG(LOG_WARN, TAG_TTSD, "[Config WARNING] Fail to open config (%s)", CONFIG_FILE_PATH);
48                 return -1;
49         }
50
51         SLOG(LOG_DEBUG, TAG_TTSD, "[Config] Rewrite config file");
52
53         /* Write engine id */
54         fprintf(config_fp, "%s %s\n", ENGINE_ID, g_engine_id);
55
56         /* Write voice */
57         fprintf(config_fp, "%s %s %d\n", VOICE, g_language, g_vc_type);
58
59         /* Read speed */
60         fprintf(config_fp, "%s %d\n", SPEED, g_speed);
61
62         fclose(config_fp);
63
64         return 0;
65 }
66
67
68 int __ttsd_config_load()
69 {
70         FILE* config_fp;
71         char buf_id[256] = {0};
72         char buf_param[256] = {0};
73         int int_param = 0;
74         bool is_default_open = false;
75
76         config_fp = fopen(CONFIG_FILE_PATH, "r");
77
78         if (NULL == config_fp) {
79                 SLOG(LOG_WARN, TAG_TTSD, "[Config WARNING] Not open file(%s)", CONFIG_FILE_PATH);
80                 
81                 config_fp = fopen(CONFIG_DEFAULT, "r");
82                 if (NULL == config_fp) {
83                         SLOG(LOG_ERROR, TAG_TTSD, "[Config WARNING] Not open original config file(%s)", CONFIG_FILE_PATH);
84                         return -1;
85                 }
86                 is_default_open = true;
87         }
88
89         /* Read engine id */
90         if (EOF == fscanf(config_fp, "%s %s", buf_id, buf_param)) {
91                 fclose(config_fp);
92                 SLOG(LOG_WARN, TAG_TTSD, "[Config WARNING] Fail to read config (engine id)");
93                 __ttsd_config_save();
94                 return -1;
95         } else {
96                 if (0 == strncmp(ENGINE_ID, buf_id, strlen(ENGINE_ID))) {
97                         g_engine_id = strdup(buf_param);
98                 } else {
99                         fclose(config_fp);
100                         SLOG(LOG_WARN, TAG_TTSD, "[Config WARNING] Fail to load config (engine id)");
101                         __ttsd_config_save();
102                         return -1;
103                 }
104         }
105
106         
107
108         /* Read voice */
109         if (EOF == fscanf(config_fp, "%s %s %d", buf_id, buf_param, &int_param)) {
110                 fclose(config_fp);
111                 SLOG(LOG_WARN, TAG_TTSD, "[Config WARNING] Fail to read config (voice)");
112                 __ttsd_config_save();
113                 return -1;
114         } else {
115                 if (0 == strncmp(VOICE, buf_id, strlen(VOICE))) {
116                         g_language = strdup(buf_param);
117                         g_vc_type = int_param;
118                 } else {
119                         fclose(config_fp);
120                         SLOG(LOG_WARN, TAG_TTSD, "[Config WARNING] Fail to load config (voice)");
121                         __ttsd_config_save();
122                         return -1;
123                 }
124         }
125         
126         /* Read speed */
127         if (EOF == fscanf(config_fp, "%s %d", buf_id, &int_param)) {
128                 fclose(config_fp);
129                 SLOG(LOG_WARN, TAG_TTSD, "[Config WARNING] Fail to read config (speed)");
130                 __ttsd_config_save();
131                 return -1;
132         } else {
133                 if (0 == strncmp(SPEED, buf_id, strlen(SPEED))) {
134                         g_speed = int_param;
135                 } else {
136                         fclose(config_fp);
137                         SLOG(LOG_WARN, TAG_TTSD, "[Config WARNING] Fail to load config (speed)");
138                         __ttsd_config_save();
139                         return -1;
140                 }
141         }
142         
143         fclose(config_fp);
144
145         SLOG(LOG_DEBUG, TAG_TTSD, "[Config] Load config : engine(%s), voice(%s,%d), speed(%d)",
146                 g_engine_id, g_language, g_vc_type, g_speed);
147
148         if (true == is_default_open) {
149                 if(0 == __ttsd_config_save()) {
150                         SLOG(LOG_DEBUG, TAG_TTSD, "[Config] Create config(%s)", CONFIG_FILE_PATH);
151                 }
152         }
153
154         return 0;
155 }
156
157 int ttsd_config_initialize()
158 {
159         g_engine_id = NULL;
160         g_language = NULL;
161         g_vc_type = 1;
162         g_speed = 3;
163
164         __ttsd_config_load();
165
166         return 0;
167 }
168
169 int ttsd_config_finalize()
170 {
171         __ttsd_config_save();
172         return 0;
173 }
174
175 int ttsd_config_get_default_engine(char** engine_id)
176 {
177         if (NULL == engine_id)
178                 return -1;
179
180         *engine_id = strdup(g_engine_id);
181         return 0;
182 }
183
184 int ttsd_config_set_default_engine(const char* engine_id)
185 {
186         if (NULL == engine_id)
187                 return -1;
188
189         if (NULL != g_engine_id)
190                 free(g_engine_id);
191
192         g_engine_id = strdup(engine_id);
193         __ttsd_config_save();
194         return 0;
195 }
196
197 int ttsd_config_get_default_voice(char** language, int* type)
198 {
199         if (NULL == language || NULL == type)
200                 return -1;
201
202         *language = strdup(g_language);
203         *type = g_vc_type;
204
205         return 0;
206 }
207
208 int ttsd_config_set_default_voice(const char* language, int type)
209 {
210         if (NULL == language)
211                 return -1;
212
213         if (NULL != g_language)
214                 free(g_language);
215
216         g_language = strdup(language);
217         g_vc_type = type;
218
219         __ttsd_config_save();
220
221         return 0;
222 }
223
224 int ttsd_config_get_default_speed(int* speed)
225 {
226         if (NULL == speed)
227                 return -1;
228
229         *speed = g_speed;
230
231         return 0;
232 }
233
234 int ttsd_config_set_default_speed(int speed)
235 {
236         g_speed = speed;
237         __ttsd_config_save();
238         return 0;
239 }