Added libsystemd dependency.
[platform/core/api/system-settings.git] / src / system_settings_vconf.c
1 /*
2  * Copyright (c) 2011 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
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include <vconf.h>
22 #include <dlog.h>
23
24 #include <system_settings.h>
25 #include <system_settings_private.h>
26
27 #ifdef LOG_TAG
28 #undef LOG_TAG
29 #endif
30
31 #define LOG_TAG "TIZEN_N_SYSTEM_SETTINGS"
32
33
34 int system_setting_vconf_get_value_int(const char *vconf_key, int *value)
35 {
36         return vconf_get_int(vconf_key, value);
37 }
38
39 int system_setting_vconf_get_value_bool(const char *vconf_key, bool *value)
40 {
41         int tempvalue = 0;
42         int ret = vconf_get_bool(vconf_key, &tempvalue);
43
44         if (tempvalue == 1) {
45                 *value = true;
46         } else {
47                 *value = false;
48         }
49         return ret;
50 }
51
52 int system_setting_vconf_get_value_string(const char *vconf_key, char **value)
53 {
54         char *str_value = NULL;
55
56         str_value = vconf_get_str(vconf_key);
57
58         if (str_value != NULL) {
59                 *value = str_value;
60                 return 0;
61         } else {
62                 return -1;
63         }
64 }
65
66 int system_setting_vconf_set_value_int(const char *vconf_key, int value)
67 {
68         return vconf_set_int(vconf_key, value);
69 }
70
71 int system_setting_vconf_set_value_bool(const char *vconf_key, bool value)
72 {
73         return vconf_set_bool(vconf_key, (int)value);
74 }
75
76 int system_setting_vconf_set_value_string(const char *vconf_key, char *value)
77 {
78         return vconf_set_str(vconf_key, value);
79 }
80
81 /*/////////////////////////////////////////////////////////////////////////////////////////// */
82
83 typedef void (*system_setting_vconf_event_cb)(keynode_t *node, void *event_data);
84
85 /*  LCOV_EXCL_START */
86 static void system_setting_vconf_event_cb0(keynode_t *node, void *event_data)
87 {
88         system_settings_key_e pkey = (system_settings_key_e)event_data;
89
90         if (node != NULL) {
91                 system_setting_h system_setting_item;
92
93                 if (system_settings_get_item(pkey, &system_setting_item)) {
94                         LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid key", __FUNCTION__, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
95                 }
96
97                 void *user_data = NULL;
98                 user_data = system_setting_item->user_data;
99                 system_setting_item->changed_cb(pkey, user_data);
100         }
101 }
102 /*  LCOV_EXCL_STOP */
103
104 /*  LCOV_EXCL_START */
105 static void system_setting_vconf_event_cb1(keynode_t *node, void *event_data)
106 {
107         system_settings_key_e pkey = (system_settings_key_e)event_data;
108
109         if (node != NULL) {
110                 system_setting_h system_setting_item;
111
112                 if (system_settings_get_item(pkey, &system_setting_item)) {
113                         LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid key", __FUNCTION__, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
114                 }
115
116                 void *user_data = NULL;
117                 user_data = system_setting_item->user_data;
118                 system_setting_item->changed_cb(pkey, user_data);
119         }
120 }
121 /*  LCOV_EXCL_STOP */
122
123 /*  LCOV_EXCL_START */
124 static void system_setting_vconf_event_cb2(keynode_t *node, void *event_data)
125 {
126         system_settings_key_e pkey = (system_settings_key_e)event_data;
127
128         if (node != NULL) {
129                 system_setting_h system_setting_item;
130
131                 if (system_settings_get_item(pkey, &system_setting_item)) {
132                         LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid key", __FUNCTION__, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
133                 }
134
135                 void *user_data = NULL;
136                 user_data = system_setting_item->user_data;
137                 system_setting_item->changed_cb(pkey, user_data);
138         }
139 }
140 /*  LCOV_EXCL_STOP */
141
142 /*  LCOV_EXCL_START */
143 static void system_setting_vconf_event_cb3(keynode_t *node, void *event_data)
144 {
145         system_settings_key_e pkey = (system_settings_key_e)event_data;
146
147         if (node != NULL) {
148                 system_setting_h system_setting_item;
149
150                 if (system_settings_get_item(pkey, &system_setting_item)) {
151                         LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid key", __FUNCTION__, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
152                 }
153
154                 void *user_data = NULL;
155                 user_data = system_setting_item->user_data;
156                 system_setting_item->changed_cb(pkey, user_data);
157         }
158 }
159 /*  LCOV_EXCL_STOP */
160
161 /*  LCOV_EXCL_START */
162 static void system_setting_vconf_event_cb4(keynode_t *node, void *event_data)
163 {
164         system_settings_key_e pkey = (system_settings_key_e)event_data;
165
166         if (node != NULL) {
167                 system_setting_h system_setting_item;
168
169                 if (system_settings_get_item(pkey, &system_setting_item)) {
170                         LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid key", __FUNCTION__, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
171                 }
172
173                 void *user_data = NULL;
174                 user_data = system_setting_item->user_data;
175                 system_setting_item->changed_cb(pkey, user_data);
176         }
177 }
178 /*  LCOV_EXCL_STOP */
179
180 /*  LCOV_EXCL_START */
181 static void system_setting_vconf_event_cb100(keynode_t *node, void *event_data)
182 {
183         system_settings_key_e pkey = (system_settings_key_e)event_data;
184
185         if (node != NULL) {
186                 system_setting_h system_setting_item;
187
188                 if (system_settings_get_item(pkey, &system_setting_item)) {
189                         LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid key", __FUNCTION__, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
190                 }
191
192                 void *user_data = NULL;
193                 user_data = system_setting_item->user_data;
194                 system_setting_item->changed_cb(pkey, user_data);
195         }
196 }
197 /*  LCOV_EXCL_STOP */
198
199
200 /*  LCOV_EXCL_START */
201 static system_setting_vconf_event_cb system_setting_vconf_get_event_cb_slot(int slot)
202 {
203         switch (slot) {
204         case 0:
205                 return system_setting_vconf_event_cb0;
206
207         case 1:
208                 return system_setting_vconf_event_cb1;
209
210         case 2:
211                 return system_setting_vconf_event_cb2;
212
213         case 3:
214                 return system_setting_vconf_event_cb3;
215
216         case 4:
217                 return system_setting_vconf_event_cb4;
218         case 100:
219                 return system_setting_vconf_event_cb100;
220
221         default:
222                 return NULL;
223         }
224 }
225 /*  LCOV_EXCL_STOP */
226
227 int system_setting_vconf_set_changed_cb(const char *vconf_key, system_settings_key_e key, int slot, void *user_data)
228 {
229         system_setting_vconf_event_cb vconf_event_cb;
230
231         vconf_event_cb = system_setting_vconf_get_event_cb_slot(slot);
232
233         if (vconf_event_cb == NULL) {
234                 LOGE("[%s] INVALID_error , %s", __FUNCTION__, "vconf_event_cb == 0");
235                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
236         }
237
238 #if 1
239         if (vconf_notify_key_changed(vconf_key, vconf_event_cb, (void *)key))
240 #else
241         if (vconf_notify_key_changed(vconf_key, vconf_event_cb, (void *)user_data))
242 #endif
243         {
244                 LOGE("[%s] INVALID_error , %s", __FUNCTION__, "vconf_notify_key_changed error");
245                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
246         }
247
248         return SYSTEM_SETTINGS_ERROR_NONE;
249 }
250
251 int system_setting_vconf_unset_changed_cb(const char *vconf_key, int slot)
252 {
253         system_setting_vconf_event_cb vconf_event_cb;
254
255         vconf_event_cb = system_setting_vconf_get_event_cb_slot(slot);
256
257         if (vconf_event_cb != NULL) {
258                 vconf_ignore_key_changed(vconf_key, vconf_event_cb);
259         }
260
261         return SYSTEM_SETTINGS_ERROR_NONE;
262 }
263
264