Fix to check SYSTEM_SETTINGS_KEY_AUTOMATIC_TIME_UPDATE feature
[platform/core/api/system-settings.git] / src / sst_api.c
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "system_settings.h"
17
18 #include "sst.h"
19 #include "sst_vconf.h"
20 #include "sst_sound.h"
21 #include "sst_interface.h"
22
23 API int system_settings_set_value_int(system_settings_key_e key, int value)
24 {
25         int ret;
26         sst_interface *iface = NULL;
27         ret = sst_get_interface(key, &iface);
28         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
29                 ERR("sst_get_interface(%d) Fail(%d)", key, ret);
30                 return ret;
31         }
32
33         if (SYSTEM_SETTING_DATA_TYPE_INT != iface->data_type) {
34                 ERR("Invalid type for key(%d) : %d", key, iface->data_type);
35                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
36         }
37
38         if (NULL == iface->setter.i) {
39                 ERR("No setter for key(%d)", key);
40                 return SYSTEM_SETTINGS_ERROR_NOT_SUPPORTED;
41         }
42
43         ret = iface->setter.i(iface, value);
44         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
45                 ERR("setter(%d) Fail(%d)", key, ret);
46                 return ret;
47         }
48
49         return SYSTEM_SETTINGS_ERROR_NONE;
50 }
51
52 API int system_settings_get_value_int(system_settings_key_e key, int *value)
53 {
54         int ret;
55         sst_interface *iface = NULL;
56
57         if (NULL == value) {
58                 ERR("Invalid input parameter value == NULL");
59                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
60         }
61
62         ret = sst_get_interface(key, &iface);
63         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
64                 ERR("sst_get_interface(%d) Fail(%d)", key, ret);
65                 return ret;
66         }
67
68         if (SYSTEM_SETTING_DATA_TYPE_INT != iface->data_type) {
69                 ERR("Invalid type for key(%d) : %d", key, iface->data_type);
70                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
71         }
72
73         if (NULL == iface->getter.i) {
74                 ERR("No getter for key(%d)", key);
75                 return SYSTEM_SETTINGS_ERROR_NOT_SUPPORTED;
76         }
77
78         int result = 0;
79         ret = iface->getter.i(iface, &result);
80         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
81                 ERR("getter(%d) Fail(%d)", key, ret);
82                 return ret;
83         }
84
85         *value = result;
86         return SYSTEM_SETTINGS_ERROR_NONE;
87 }
88
89 API int system_settings_set_value_bool(system_settings_key_e key, bool value)
90 {
91         int ret;
92         sst_interface *iface = NULL;
93         ret = sst_get_interface(key, &iface);
94         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
95                 ERR("sst_get_interface(%d) Fail(%d)", key, ret);
96                 return ret;
97         }
98
99         if (SYSTEM_SETTING_DATA_TYPE_BOOL != iface->data_type) {
100                 ERR("Invalid type for key(%d) : %d", key, iface->data_type);
101                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
102         }
103
104         if (NULL == iface->setter.b) {
105                 ERR("No setter for key(%d)", key);
106                 return SYSTEM_SETTINGS_ERROR_NOT_SUPPORTED;
107         }
108
109         ret = iface->setter.b(iface, value);
110         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
111                 ERR("setter(%d) Fail(%d)", key, ret);
112                 return ret;
113         }
114
115         return SYSTEM_SETTINGS_ERROR_NONE;
116 }
117
118 API int system_settings_get_value_bool(system_settings_key_e key, bool *value)
119 {
120         int ret;
121         sst_interface *iface = NULL;
122
123         if (NULL == value) {
124                 ERR("Invalid input parameter value == NULL");
125                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
126         }
127
128         ret = sst_get_interface(key, &iface);
129         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
130                 ERR("sst_get_interface(%d) Fail(%d)", key, ret);
131                 return ret;
132         }
133
134         if (SYSTEM_SETTING_DATA_TYPE_BOOL != iface->data_type) {
135                 ERR("Invalid type for key(%d) : %d", key, iface->data_type);
136                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
137         }
138
139         if (NULL == iface->getter.b) {
140                 ERR("No getter for key(%d)", key);
141                 return SYSTEM_SETTINGS_ERROR_NOT_SUPPORTED;
142         }
143
144         bool result = 0;
145         ret = iface->getter.b(iface, &result);
146         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
147                 ERR("getter(%d) Fail(%d)", key, ret);
148                 return ret;
149         }
150
151         *value = result;
152         return SYSTEM_SETTINGS_ERROR_NONE;
153 }
154
155 API int system_settings_set_value_string(system_settings_key_e key, const char *value)
156 {
157         int ret;
158         sst_interface *iface = NULL;
159         ret = sst_get_interface(key, &iface);
160         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
161                 ERR("sst_get_interface(%d) Fail(%d)", key, ret);
162                 return ret;
163         }
164
165         if (SYSTEM_SETTING_DATA_TYPE_STRING != iface->data_type) {
166                 ERR("Invalid type for key(%d) : %d", key, iface->data_type);
167                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
168         }
169
170         if (NULL == iface->setter.s) {
171                 ERR("No setter for key(%d)", key);
172                 return SYSTEM_SETTINGS_ERROR_NOT_SUPPORTED;
173         }
174
175         ret = iface->setter.s(iface, value);
176         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
177                 ERR("setter(%d) Fail(%d)", key, ret);
178                 return ret;
179         }
180
181         return SYSTEM_SETTINGS_ERROR_NONE;
182 }
183
184 API int system_settings_get_value_string(system_settings_key_e key, char **value)
185 {
186         int ret;
187         sst_interface *iface = NULL;
188         if (NULL == value) {
189                 ERR("Invalid input parameter value == NULL");
190                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
191         }
192
193         ret = sst_get_interface(key, &iface);
194         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
195                 ERR("sst_get_interface(%d) Fail(%d)", key, ret);
196                 return ret;
197         }
198
199         if (SYSTEM_SETTING_DATA_TYPE_STRING != iface->data_type) {
200                 ERR("Invalid type for key(%d) : %d", key, iface->data_type);
201                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
202         }
203
204         if (NULL == iface->getter.s) {
205                 ERR("No getter for key(%d)", key);
206                 return SYSTEM_SETTINGS_ERROR_NOT_SUPPORTED;
207         }
208
209         char *result = NULL;
210         ret = iface->getter.s(iface, &result);
211         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
212                 ERR("getter(%d) Fail(%d)", key, ret);
213                 return ret;
214         }
215
216         *value = result;
217         return SYSTEM_SETTINGS_ERROR_NONE;
218 }
219
220 //todo: need to deprecate
221 API int system_settings_set_changed_cb(system_settings_key_e key, system_settings_changed_cb callback, void *user_data)
222 {
223         int ret;
224
225         RETV_IF(NULL == callback, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
226
227         sst_interface *iface = NULL;
228         ret = sst_get_interface(key, &iface);
229         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
230                 ERR("sst_get_interface(%d) Fail(%d)", key, ret);
231                 return ret;
232         }
233
234         if (NULL == iface->vconf_key) {
235                 ERR("NULL set_changed_cb of key(%d)", key);
236                 return SYSTEM_SETTINGS_ERROR_NOT_SUPPORTED;
237         }
238
239         return sst_vconf_set_cb(iface, callback, user_data);
240 }
241
242 //todo: need to deprecate
243 API int system_settings_unset_changed_cb(system_settings_key_e key)
244 {
245         sst_interface *iface = NULL;
246         int ret = sst_get_interface(key, &iface);
247         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
248                 ERR("sst_get_interface(%d) Fail(%d)", key, ret);
249                 return ret;
250         }
251
252         if (NULL == iface->vconf_key) {
253                 ERR("NULL set_changed_cb of key(%d)", key);
254                 return SYSTEM_SETTINGS_ERROR_NOT_SUPPORTED;
255         }
256
257         return sst_vconf_unset_cb(iface);
258 }
259
260 API int system_settings_foreach_value_string(system_settings_key_e key, system_settings_iter_cb callback, void *value)
261 {
262         DBG("Enter");
263         RETVM_IF(SYSTEM_SETTINGS_KEY_INCOMING_CALL_RINGTONE != key,
264                 SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER,
265                 "Invalid Key(%d)", key);
266
267         return sst_sound_get_call_ringtone_list(key, callback, value);
268 }
269
270 API int system_settings_add_value_string(system_settings_key_e key, const char* value)
271 {
272         DBG("Enter");
273         RETVM_IF(SYSTEM_SETTINGS_KEY_INCOMING_CALL_RINGTONE != key,
274                 SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER,
275                 "Invalid Key(%d)", key);
276
277         return sst_sound_add_call_ringtone(key, value);
278 }
279
280 API int system_settings_delete_value_string(system_settings_key_e key, const char* value)
281 {
282         DBG("Enter");
283         RETVM_IF(SYSTEM_SETTINGS_KEY_INCOMING_CALL_RINGTONE != key,
284                 SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER,
285                 "Invalid Key(%d)", key);
286
287         return sst_sound_del_call_ringtone(key, value);
288 }
289
290 API int system_settings_add_changed_cb(system_settings_key_e key, system_settings_changed_cb callback, void *user_data)
291 {
292         int ret;
293
294         RETV_IF(NULL == callback, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
295
296         sst_interface *iface = NULL;
297         ret = sst_get_interface(key, &iface);
298         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
299                 ERR("sst_get_interface(%d) Fail(%d)", key, ret);
300                 return ret;
301         }
302
303         if (NULL == iface->vconf_key) {
304                 ERR("NULL vconf_key of key(%d)", key);
305                 return SYSTEM_SETTINGS_ERROR_NOT_SUPPORTED;
306         }
307
308         INFO("key = %d, %s", key, iface->vconf_key);
309
310         ret = sst_vconf_add_multi_cb(iface, callback, user_data);
311         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
312                 ERR("sst_vconf_add_multi_cb() Fail(%d)", ret);
313                 return ret;
314         }
315
316         return SYSTEM_SETTINGS_ERROR_NONE;
317 }
318
319 API int system_settings_remove_changed_cb(system_settings_key_e key, system_settings_changed_cb callback)
320 {
321         int ret;
322
323         RETV_IF(NULL == callback, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
324
325         sst_interface *iface = NULL;
326         ret = sst_get_interface(key, &iface);
327         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
328                 ERR("sst_get_interface(%d) Fail(%d)", key, ret);
329                 return ret;
330         }
331
332         if (NULL == iface->vconf_key) {
333                 ERR("NULL vconf_key of key(%d)", key);
334                 return SYSTEM_SETTINGS_ERROR_NOT_SUPPORTED;
335         }
336
337         ret = sst_vconf_del_multi_cb(iface, callback);
338         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
339                 ERR("sst_vconf_del_multi_cb() Fail(%d)", ret);
340                 return ret;
341         }
342
343         return SYSTEM_SETTINGS_ERROR_NONE;
344 }
345
346 int system_settings_subscribe_changes(system_settings_key_e key, system_settings_changed_cb callback, void *user_data, system_settings_cb_id *id)
347 {
348         int ret;
349
350         RETV_IF(NULL == id, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
351         RETV_IF(NULL == callback, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
352
353         sst_interface *iface = NULL;
354         ret = sst_get_interface(key, &iface);
355         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
356                 ERR("sst_get_interface(%d) Fail(%d)", key, ret);
357                 return ret;
358         }
359
360         if (NULL == iface->vconf_key) {
361                 ERR("NULL vconf_key of key(%d)", key);
362                 return SYSTEM_SETTINGS_ERROR_NOT_SUPPORTED;
363         }
364
365         ret = sst_vconf_subscribe(iface, callback, user_data, id);
366         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
367                 ERR("sst_vconf_subscribe() Fail(%d)", ret);
368                 return ret;
369         }
370         return SYSTEM_SETTINGS_ERROR_NONE;
371 }
372
373 int system_settings_unsubscribe_changes(system_settings_key_e key, system_settings_cb_id id)
374 {
375         int ret;
376
377         RETV_IF(NULL == id, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
378
379         sst_interface *iface = NULL;
380         ret = sst_get_interface(key, &iface);
381         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
382                 ERR("sst_get_interface(%d) Fail(%d)", key, ret);
383                 return ret;
384         }
385
386         if (NULL == iface->vconf_key) {
387                 ERR("NULL vconf_key of key(%d)", key);
388                 return SYSTEM_SETTINGS_ERROR_NOT_SUPPORTED;
389         }
390
391         ret = sst_vconf_unsubscribe(iface, id);
392         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
393                 ERR("sst_vconf_unsubscribe() Fail(%d)", ret);
394                 return ret;
395         }
396         return SYSTEM_SETTINGS_ERROR_NONE;
397
398
399 }