tizen beta release
[platform/core/uifw/stt.git] / server / sttd_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
17 #include "sttd_main.h"
18 #include "sttd_config.h"
19
20
21 /*
22 * stt-daemon config
23 */
24
25 int sttd_config_get_char_type(const char* key, char** value)
26 {
27         if (NULL == key || NULL == value) {
28                 SLOG(LOG_ERROR, TAG_STTD, "[STTD Config ERROR] Input parameter is NULL");
29                 return STTD_ERROR_INVALID_PARAMETER;
30         } 
31
32         *value = vconf_get_str(key);
33         if (NULL == *value) {
34                 SLOG(LOG_ERROR, TAG_STTD, "[STTD Config ERROR] Fail to get char type from config : key(%s)", key);
35                 return -1;
36         }
37
38         return 0;
39 }
40
41 int sttd_config_set_char_type(const char* key, const char* value)
42 {
43         if (NULL == key || NULL == value) {
44                 SLOG(LOG_ERROR, TAG_STTD, "[STTD Config ERROR] Input parameter is NULL");
45                 return STTD_ERROR_INVALID_PARAMETER;
46         } 
47
48         if (0 != vconf_set_str(key, value)) {
49                 SLOG(LOG_ERROR, TAG_STTD, "[STTD Config ERROR] Fail to set char type"); 
50                 return -1;
51         }
52
53         return 0;
54 }
55
56 int sttd_config_get_bool_type(const char* key, bool* value)
57 {
58         if (NULL == key || NULL == value) {
59                 SLOG(LOG_ERROR, TAG_STTD, "[STTD Config ERROR] Input parameter is NULL");
60                 return STTD_ERROR_INVALID_PARAMETER;
61         } 
62
63         int result ;
64         if (0 != vconf_get_int(key, &result)) {
65                 SLOG(LOG_ERROR, TAG_STTD, "[STTD Config ERROR] Fail to get bool type config : key(%s)", key);
66                 return -1;
67         }
68
69         *value = (bool) result;
70
71         return 0;
72 }
73
74 int sttd_config_set_bool_type(const char* key, const bool value)
75 {
76         if (NULL == key) {
77                 SLOG(LOG_ERROR, TAG_STTD, "[STTD Config ERROR] Input parameter is NULL");
78                 return STTD_ERROR_INVALID_PARAMETER;
79         } 
80
81         int result = (int)value;
82         if (0 != vconf_set_int(key, result)) {
83                 SLOG(LOG_ERROR, TAG_STTD, "[STTD Config ERROR] Fail to get bool type config : key(%s)", key);
84                 return -1;
85         }
86
87         return 0;
88 }
89
90 /*
91 * plug-in daemon interface
92 */
93
94 int __make_key_for_engine(const char* engine_id, const char* key, char** out_key)
95 {
96         int key_size = strlen(STTD_CONFIG_PREFIX) + strlen(engine_id) + strlen(key) + 2; /* 2 means both '/' and '\0'*/
97
98         *out_key = (char*) malloc( sizeof(char) * key_size);
99
100         snprintf(*out_key, key_size, "%s%s/%s", STTD_CONFIG_PREFIX, engine_id, key );
101
102         return 0;
103 }
104
105 int sttd_config_set_persistent_data(const char* engine_id, const char* key, const char* value)
106 {
107         if (NULL == engine_id || NULL == key || NULL == value) {
108                 SLOG(LOG_ERROR, TAG_STTD, "[STTD Config ERROR] BAD Parameter"); 
109                 return STTD_ERROR_INVALID_PARAMETER;
110         }
111
112         char* vconf_key = NULL;
113         if (0 != __make_key_for_engine(engine_id, key, &vconf_key)) {
114                 SLOG(LOG_ERROR, TAG_STTD, "[STTD Config ERROR] Fail __make_key_for_engine()");
115                 return -1;
116         }
117
118         if (NULL == vconf_key)          
119                 return -1;
120
121         if (0 != vconf_set_str(vconf_key, value)) {
122                 SLOG(LOG_ERROR, TAG_STTD, "[STTD Config ERROR] Fail to set key, value");
123
124                 if (vconf_key != NULL)  
125                         free(vconf_key);
126
127                 return -1;
128         }
129
130         SLOG(LOG_DEBUG, TAG_STTD, "[STTD Config DEBUG] sttd_config_set_persistent_data : key(%s), value(%s)", vconf_key, value);
131
132         if (NULL != vconf_key)  
133                 free(vconf_key);
134
135         return 0;
136 }
137
138 int sttd_config_get_persistent_data(const char* engine_id, const char* key, char** value)
139 {
140         if (NULL == engine_id) {
141                 SLOG(LOG_ERROR, TAG_STTD, "[STTD Config ERROR] BAD Parameter"); 
142                 return STTD_ERROR_INVALID_PARAMETER;
143         }
144
145         char* vconf_key = NULL;
146
147         if (0 != __make_key_for_engine(engine_id, key, &vconf_key)) {
148                 SLOG(LOG_ERROR, TAG_STTD, "[STTD Config ERROR] Fail __make_key_for_engine()");
149                 return -1;
150         }
151
152         if (NULL == vconf_key)
153                 return -1;
154
155         char* temp;
156         temp = vconf_get_str(vconf_key);
157         if (NULL == temp) {
158                 SLOG(LOG_ERROR, TAG_STTD, "[STTD Config ERROR] Fail to get value");
159
160                 if(vconf_key != NULL)   
161                         free(vconf_key);
162
163                 return -1;
164         }
165
166         *value = g_strdup(temp);
167
168         SLOG(LOG_DEBUG, TAG_STTD, "[STTD Config DEBUG] sttd_config_get_persistent_data : key(%s), value(%s)", vconf_key, *value);
169
170         if (vconf_key != NULL)  free(vconf_key);
171         if (temp != NULL)       free(temp);
172
173         return 0;
174 }
175
176 int sttd_config_remove_persistent_data(const char* engine_id, const char* key)
177 {
178         if (NULL == engine_id || NULL == key) {
179                 SLOG(LOG_ERROR, TAG_STTD, "[STTD Config ERROR] BAD Parameter"); 
180                 return STTD_ERROR_INVALID_PARAMETER;
181         }
182
183         char* vconf_key = NULL;
184         if (0 != __make_key_for_engine(engine_id, key, &vconf_key)) {
185                 SLOG(LOG_ERROR, TAG_STTD, "[STTD Config ERROR] Fail __make_key_for_engine()");
186                 return -1;
187         }
188
189         if (NULL == vconf_key)          
190                 return -1;
191
192         if (0 != vconf_unset(vconf_key)) {      
193                 SLOG(LOG_ERROR, TAG_STTD, "[STTD Config ERROR] Fail to remove key");
194
195                 if(vconf_key != NULL)   
196                         free(vconf_key);
197
198                 return -1;
199         }
200
201         SLOG(LOG_DEBUG, TAG_STTD, "[STTD Config DEBUG] sttd_config_remove_persistent_data : key(%s)", vconf_key);
202
203         if( NULL != vconf_key )         
204                 free(vconf_key);
205
206         return 0;
207 }
208
209