Update tts for tizen 2.0 beta
[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 "ttsd_main.h"
15 #include "ttsd_config.h"
16
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         FILE* config_fp;
34         config_fp = fopen(CONFIG_FILE_PATH, "w+");
35
36         if (NULL == config_fp) {
37                 /* make file and file default */
38                 SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] Fail to load config (engine id)");
39                 return -1;
40         }
41
42         /* Write engine id */
43         fprintf(config_fp, "%s %s\n", ENGINE_ID, g_engine_id);
44
45         /* Write voice */
46         fprintf(config_fp, "%s %s %d\n", VOICE, g_language, g_vc_type);
47
48         /* Read speed */
49         fprintf(config_fp, "%s %d\n", SPEED, g_speed);
50
51         fclose(config_fp);
52
53         return 0;
54 }
55
56
57 int __ttsd_config_load()
58 {
59         FILE* config_fp;
60         char buf_id[256] = {0};
61         char buf_param[256] = {0};
62         int int_param = 0;
63
64         config_fp = fopen(CONFIG_FILE_PATH, "r");
65
66         if (NULL == config_fp) {
67                 SLOG(LOG_WARN, TAG_TTSD, "[Config WARNING] Not open file(%s)", CONFIG_FILE_PATH);
68                 
69                 config_fp = fopen(CONFIG_DEFAULT, "r");
70                 if (NULL == config_fp) {
71                         SLOG(LOG_WARN, TAG_TTSD, "[Config WARNING] Not open original config file(%s)", CONFIG_FILE_PATH);
72                         __ttsd_config_save();
73                         return -1;
74                 }
75         }
76
77         /* Read engine id */
78         fscanf(config_fp, "%s %s", buf_id, buf_param);
79         if (0 == strncmp(ENGINE_ID, buf_id, strlen(ENGINE_ID))) {
80                 g_engine_id = strdup(buf_param);
81         } else {
82                 fclose(config_fp);
83                 SLOG(LOG_WARN, TAG_TTSD, "[Config WARNING] Fail to load config (engine id)");
84                 __ttsd_config_save();
85                 return -1;
86         }
87
88         /* Read voice */
89         fscanf(config_fp, "%s %s %d", buf_id, buf_param, &int_param);
90         if (0 == strncmp(VOICE, buf_id, strlen(VOICE))) {
91                 g_language = strdup(buf_param);
92                 g_vc_type = int_param;
93         } else {
94                 fclose(config_fp);
95                 SLOG(LOG_WARN, TAG_TTSD, "[Config WARNING] Fail to load config (voice)");
96                 __ttsd_config_save();
97                 return -1;
98         }
99
100         /* Read speed */
101         fscanf(config_fp, "%s %d", buf_id, &int_param);
102         if (0 == strncmp(SPEED, buf_id, strlen(SPEED))) {
103                 g_speed = int_param;
104         } else {
105                 fclose(config_fp);
106                 SLOG(LOG_WARN, TAG_TTSD, "[Config WARNING] Fail to load config (speed)");
107                 __ttsd_config_save();
108                 return -1;
109         }
110
111         SLOG(LOG_DEBUG, TAG_TTSD, "[Config] Load config : engine(%s), voice(%s,%d), speed(%d)",
112                 g_engine_id, g_language, g_vc_type, g_speed);
113
114         return 0;
115 }
116
117 int ttsd_config_initialize()
118 {
119         g_engine_id = NULL;
120         g_language = NULL;
121         g_vc_type = 1;
122         g_speed = 3;
123
124         __ttsd_config_load();
125
126         return 0;
127 }
128
129 int ttsd_config_finalize()
130 {
131         __ttsd_config_save();
132         return 0;
133 }
134
135 int ttsd_config_get_default_engine(char** engine_id)
136 {
137         if (NULL == engine_id)
138                 return -1;
139
140         *engine_id = strdup(g_engine_id);
141         return 0;
142 }
143
144 int ttsd_config_set_default_engine(const char* engine_id)
145 {
146         if (NULL == engine_id)
147                 return -1;
148
149         if (NULL != g_engine_id)
150                 free(g_engine_id);
151
152         g_engine_id = strdup(engine_id);
153         __ttsd_config_save();
154         return 0;
155 }
156
157 int ttsd_config_get_default_voice(char** language, int* type)
158 {
159         if (NULL == language || NULL == type)
160                 return -1;
161
162         *language = strdup(g_language);
163         *type = g_vc_type;
164
165         return 0;
166 }
167
168 int ttsd_config_set_default_voice(const char* language, int type)
169 {
170         if (NULL == language)
171                 return -1;
172
173         if (NULL != g_language)
174                 free(g_language);
175
176         g_language = strdup(language);
177         g_vc_type = type;
178
179         __ttsd_config_save();
180
181         return 0;
182 }
183
184 int ttsd_config_get_default_speed(int* speed)
185 {
186         if (NULL == speed)
187                 return -1;
188
189         *speed = g_speed;
190
191         return 0;
192 }
193
194 int ttsd_config_set_default_speed(int speed)
195 {
196         g_speed = speed;
197         __ttsd_config_save();
198         return 0;
199 }