upload tizen1.0 source
[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
15 #include <vconf.h>
16 #include "ttsd_main.h"
17 #include "ttsd_config.h"
18
19 /*
20 * tts-daemon config
21 */
22
23 int ttsd_config_get_char_type(const char* key, char** value)
24 {
25         if (NULL == key || NULL == value) {
26                 SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] Input parameter is NULL\n");
27                 return TTSD_ERROR_INVALID_PARAMETER;
28         } 
29
30         *value = vconf_get_str(key);
31         if (NULL == *value) {
32                 SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail to get char type from config : key(%s)\n", key);
33                 return -1;
34         }
35
36         return 0;
37 }
38
39 int ttsd_config_set_char_type(const char* key, const char* value)
40 {
41         if (NULL == key || NULL == value) {
42                 SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] Input parameter is NULL\n");
43                 return TTSD_ERROR_INVALID_PARAMETER;
44         } 
45
46         if (0 != vconf_set_str(key, value)) {
47                 SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail to set char type \n"); 
48                 return -1;
49         }
50
51         return 0;
52 }
53
54 int ttsd_config_get_bool_type(const char* key, bool* value)
55 {
56         if (NULL == key || NULL == value) {
57                 SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] Input parameter is NULL\n");
58                 return TTSD_ERROR_INVALID_PARAMETER;
59         } 
60
61         int result ;
62         if (0 != vconf_get_int(key, &result)) {
63                 SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail to get bool type config : key(%s)\n", key);
64                 return -1;
65         }
66
67         *value = (bool) result;
68
69         return 0;
70 }
71
72 int ttsd_config_set_bool_type(const char* key, const bool value)
73 {
74         if (NULL == key) {
75                 SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] Input parameter is NULL\n");
76                 return TTSD_ERROR_INVALID_PARAMETER;
77         } 
78
79         int result = (int)value;
80         if (0 != vconf_set_int(key, result)) {
81                 SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail to set bool type config : key(%s)\n", key);
82                 return -1;
83         }
84
85         return 0;
86 }
87
88 int ttsd_config_get_int_type(const char* key, int* value)
89 {
90         if (NULL == key || NULL == value) {
91                 SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] Input parameter is NULL\n");
92                 return TTSD_ERROR_INVALID_PARAMETER;
93         } 
94
95         if (0 != vconf_get_int(key, value)) {
96                 SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail to get bool type config : key(%s)\n", key);
97                 return -1;
98         }
99
100         return 0;
101 }
102
103 int ttsd_config_set_int_type(const char* key, const int value)
104 {
105         if (NULL == key) {
106                 SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] Input parameter is NULL\n");
107                 return TTSD_ERROR_INVALID_PARAMETER;
108         } 
109
110         if (0 != vconf_set_int(key, value)) {
111                 SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail to set int type config : key(%s)\n", key);
112                 return -1;
113         }
114
115         return 0;
116 }
117
118 /*
119 * interface for engine plug-in
120 */
121
122 int config_make_key_for_engine(const char* engine_id, const char* key, char** out_key)
123 {
124         int key_size = strlen(TTSD_CONFIG_PREFIX) + strlen(engine_id) + strlen(key) + 2; /* 2 is '/' and '\0' */
125
126         *out_key = (char*) g_malloc0( sizeof(char) * key_size);
127
128         if (*out_key == NULL) {
129                 SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] Not enough memory!! \n");
130                 return -1;
131         } else {
132                 snprintf(*out_key, key_size, "%s%s/%s", TTSD_CONFIG_PREFIX, engine_id, key );
133                 SLOG(LOG_DEBUG, TAG_TTSD, "[Config DEBUG] make key (%s) \n", *out_key);
134         }
135
136         return 0;
137 }
138
139 int ttsd_config_set_persistent_data(const char* engine_id, const char* key, const char* value)
140 {
141         char* vconf_key = NULL;
142
143         if (0 != config_make_key_for_engine(engine_id, key, &vconf_key)) {
144                 SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail config_make_key_for_engine()\n"); 
145                 return -1;
146         }
147
148         if (0 != vconf_set_str(vconf_key, value)) {
149                 SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail to set key, value\n");
150                 
151                 if(vconf_key != NULL)   
152                         g_free(vconf_key);
153
154                 return -1;
155         }
156
157         SLOG(LOG_DEBUG, TAG_TTSD, "[Config DEBUG] Set data : key(%s), value(%s) \n", vconf_key, value);
158
159         if (vconf_key != NULL)  
160                 g_free(vconf_key);
161
162         return 0;
163 }
164
165 int ttsd_config_get_persistent_data(const char* engine_id, const char* key, char** value)
166 {
167         char* vconf_key = NULL;
168
169         if (0 != config_make_key_for_engine(engine_id, key, &vconf_key)) {
170                 SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail config_make_key_for_engine()\n");
171                 return -1;
172         }
173
174         char* temp;
175         temp = vconf_get_str(vconf_key);
176         if (temp == NULL) {
177                 SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail to get value\n");
178
179                 if(vconf_key != NULL)   
180                         g_free(vconf_key);
181
182                 return -1;
183         }
184
185         *value = g_strdup(temp);
186
187         SLOG(LOG_DEBUG, TAG_TTSD, "[Config DEBUG] Get data : key(%s), value(%s) \n", vconf_key, *value);
188
189         if (NULL != vconf_key)  
190                 g_free(vconf_key);
191
192         if (NULL != temp)               
193                 g_free(temp);
194
195         return 0;
196 }
197
198 int ttsd_config_remove_persistent_data(const char* engine_id, const char* key)
199 {
200         char* vconf_key = NULL;
201         int result = 0;
202
203         if (0 != config_make_key_for_engine(engine_id, key, &vconf_key)) {
204                 SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail config_make_key_for_engine()\n");
205                 return -1;
206         }
207
208         if( NULL == vconf_key )         
209                 return -1;
210
211         if (0 != vconf_unset(vconf_key)) {
212                 SLOG(LOG_ERROR, TAG_TTSD, "[Config ERROR] fail to remove key\n");
213                 result = -1;
214         } else {
215                 SLOG(LOG_DEBUG, TAG_TTSD, "[Config DEBUG] Remove data : key(%s)", vconf_key);
216         }
217
218         if( vconf_key != NULL ) 
219                 g_free(vconf_key);
220
221         return result;
222 }