merge from tizen24 to tizen30
[platform/core/api/system-settings.git] / src / system_settings.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 #include <glib.h>
28
29
30 #ifdef LOG_TAG
31 #undef LOG_TAG
32 #endif
33
34 #define LOG_TAG "TIZEN_N_SYSTEM_SETTINGS"
35
36 #define SYSTEM_SETTINGS_MAX -1
37
38
39
40 system_setting_s system_setting_table[] = {
41
42         {
43                 SYSTEM_SETTINGS_KEY_INCOMING_CALL_RINGTONE,
44                 SYSTEM_SETTING_DATA_TYPE_STRING,
45                 system_setting_get_incoming_call_ringtone,
46                 system_setting_set_incoming_call_ringtone,
47                 system_setting_set_changed_callback_incoming_call_ringtone,
48                 system_setting_unset_changed_callback_incoming_call_ringtone,
49                 NULL,
50                 NULL            /* user data */
51         },
52
53         {
54                 SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN,
55                 SYSTEM_SETTING_DATA_TYPE_STRING,
56                 system_setting_get_wallpaper_home_screen,
57                 system_setting_set_wallpaper_home_screen,
58                 system_setting_set_changed_callback_wallpaper_home_screen,
59                 system_setting_unset_changed_callback_wallpaper_home_screen,
60                 NULL,
61                 NULL            /* user data */
62         },
63
64         {
65                 SYSTEM_SETTINGS_KEY_WALLPAPER_LOCK_SCREEN,
66                 SYSTEM_SETTING_DATA_TYPE_STRING,
67                 system_setting_get_wallpaper_lock_screen,
68                 system_setting_set_wallpaper_lock_screen,
69                 system_setting_set_changed_callback_wallpaper_lock_screen,
70                 system_setting_unset_changed_callback_wallpaper_lock_screen,
71                 NULL,
72                 NULL            /* user data */
73         },
74
75         {
76                 SYSTEM_SETTINGS_KEY_FONT_SIZE,
77                 SYSTEM_SETTING_DATA_TYPE_INT,
78                 system_setting_get_font_size,
79                 system_setting_set_font_size,
80                 system_setting_set_changed_callback_font_size,
81                 system_setting_unset_changed_callback_font_size,
82                 NULL,
83                 NULL            /* user data */
84         },
85
86         {
87                 SYSTEM_SETTINGS_KEY_FONT_TYPE,
88                 SYSTEM_SETTING_DATA_TYPE_STRING,
89                 system_setting_get_font_type,
90                 system_setting_set_font_type,
91                 system_setting_set_changed_callback_font_type,
92                 system_setting_unset_changed_callback_font_type,
93                 NULL,
94                 NULL            /* user data */
95         },
96
97         {
98                 SYSTEM_SETTINGS_KEY_MOTION_ACTIVATION,
99                 SYSTEM_SETTING_DATA_TYPE_BOOL,
100                 system_setting_get_motion_activation,
101                 system_setting_set_motion_activation,
102                 system_setting_set_changed_callback_motion_activation,
103                 system_setting_unset_changed_callback_motion_activation,
104                 NULL,
105                 NULL            /* user data */
106         },
107
108         {
109                 SYSTEM_SETTINGS_KEY_EMAIL_ALERT_RINGTONE,
110                 SYSTEM_SETTING_DATA_TYPE_STRING,
111                 system_setting_get_email_alert_ringtone,
112                 system_setting_set_email_alert_ringtone,
113                 system_setting_set_changed_callback_email_alert_ringtone,
114                 system_setting_unset_changed_callback_email_alert_ringtone,
115                 NULL,
116                 NULL            /* user data */
117         },
118         {
119                 SYSTEM_SETTINGS_KEY_USB_DEBUGGING_ENABLED,
120                 SYSTEM_SETTING_DATA_TYPE_BOOL,
121                 system_setting_get_usb_debugging_option,
122                 system_setting_set_usb_debugging_option,
123                 system_setting_set_changed_callback_usb_debugging_option,
124                 system_setting_unset_changed_callback_usb_debugging_option,
125                 NULL,
126                 NULL            /* user data */
127         },
128         {
129                 SYSTEM_SETTINGS_KEY_3G_DATA_NETWORK_ENABLED,
130                 SYSTEM_SETTING_DATA_TYPE_BOOL,
131                 system_setting_get_3g_data_network,
132                 system_setting_set_3g_data_network,
133                 system_setting_set_changed_callback_3g_data_network,
134                 system_setting_unset_changed_callback_3g_data_network,
135                 NULL,
136                 NULL            /* user data */
137         },
138         {
139                 SYSTEM_SETTINGS_MAX, -1, NULL, NULL, NULL, NULL, NULL, NULL
140         }
141 };
142
143 int system_settings_get_item(system_settings_key_e key, system_setting_h *item)
144 {
145     int index = 0;
146
147     while (system_setting_table[index].key != SYSTEM_SETTINGS_MAX)
148     {
149         if (system_setting_table[index].key == key)
150         {
151             *item = &system_setting_table[index];
152             return 0;
153         }
154
155         index++;
156     }
157
158     return -1;
159 }
160
161 int system_settings_get_value(system_settings_key_e key, system_setting_data_type_e data_type, void** value)
162 {
163     system_setting_h system_setting_item;
164         system_setting_get_value_cb     system_setting_getter;
165
166     if (system_settings_get_item(key, &system_setting_item))
167     {
168         LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid key", __FUNCTION__, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
169         return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
170     }
171
172     if (system_setting_item->data_type != data_type)
173     {
174         LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid data type", __FUNCTION__, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
175         return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
176     }
177
178     system_setting_getter = system_setting_item->get_value_cb;
179
180     if (system_setting_getter == NULL)
181     {
182         LOGE("[%s] IO_ERROR(0x%08x) : failed to call getter for the system settings", __FUNCTION__, SYSTEM_SETTINGS_ERROR_IO_ERROR);
183         return SYSTEM_SETTINGS_ERROR_IO_ERROR;
184     }
185
186     return system_setting_getter(key, system_setting_item->data_type, value);
187 }
188
189 int system_settings_set_value(system_settings_key_e key, system_setting_data_type_e data_type, void* value)
190 {
191         system_setting_h system_setting_item;
192         system_setting_set_value_cb     system_setting_setter;
193
194     if (system_settings_get_item(key, &system_setting_item))
195     {
196         LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid key", __FUNCTION__, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
197         return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
198     }
199
200     system_setting_setter = system_setting_item->set_value_cb;
201
202     if (system_setting_setter == NULL)
203     {
204         LOGE("[%s] IO_ERROR(0x%08x) : failed to call getter for the system settings", __FUNCTION__, SYSTEM_SETTINGS_ERROR_IO_ERROR);
205         return SYSTEM_SETTINGS_ERROR_IO_ERROR;
206     }
207
208     return system_setting_setter(key, system_setting_item->data_type, value);
209 }
210
211 // typedef int (*system_setting_set_value_cb) (system_settings_key_e key, system_setting_data_type_e data_type, void* value);
212 int system_settings_set_value_int(system_settings_key_e key, int value)
213 {
214         // TODO: make sure the value is inside of enum.
215         int* ptr = &value;
216         return system_settings_set_value(key, SYSTEM_SETTING_DATA_TYPE_INT,(void*)ptr);
217 }
218
219 int system_settings_get_value_int(system_settings_key_e key, int *value)
220 {
221         return system_settings_get_value(key, SYSTEM_SETTING_DATA_TYPE_INT, (void**)value);
222 }
223
224 int system_settings_set_value_bool(system_settings_key_e key, bool value)
225 {
226         bool* ptr = &value;
227         return system_settings_set_value(key, SYSTEM_SETTING_DATA_TYPE_BOOL,(void*)ptr);
228 }
229
230 int system_settings_get_value_bool(system_settings_key_e key, bool *value)
231 {
232         return system_settings_get_value(key, SYSTEM_SETTING_DATA_TYPE_BOOL, (void**)value);
233 }
234
235 #if 0
236 int system_settings_set_value_double(system_settings_key_e key, double value)
237 {
238         double* ptr = &value;
239         return system_settings_set_value(key, SYSTEM_SETTING_DATA_TYPE_DOUBLE,(void*)ptr);
240 }
241
242 int system_settings_get_value_double(system_settings_key_e key, double *value)
243 {
244         return system_settings_get_value(key, SYSTEM_SETTING_DATA_TYPE_DOUBLE, (void**)value);
245 }
246 #endif
247
248 int system_settings_set_value_string(system_settings_key_e key, const char *value)
249 {
250         return system_settings_set_value(key, SYSTEM_SETTING_DATA_TYPE_STRING,(void*)value);
251 }
252
253 int system_settings_get_value_string(system_settings_key_e key, char **value)
254 {
255         return system_settings_get_value(key, SYSTEM_SETTING_DATA_TYPE_STRING, (void**)value);
256 }
257
258
259 /*
260         - START
261                 - system_settings_set_changed_cb
262                         -> int (*system_setting_set_changed_callback_cb)(key, callback, user_data)
263 */
264
265 /*PUBLIC*/
266 int system_settings_set_changed_cb(system_settings_key_e key, system_settings_changed_cb callback, void *user_data)
267 {
268     system_setting_h system_setting_item;
269         system_setting_set_changed_callback_cb system_setting_set_changed_cb;
270
271     if (system_settings_get_item(key, &system_setting_item))
272     {
273         LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid key", __FUNCTION__, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
274         return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
275     }
276
277         system_setting_set_changed_cb = system_setting_item->set_changed_cb;
278
279
280         // Store the callback function from application side
281         if (callback)
282                 system_setting_item->changed_cb = callback;
283
284         if (user_data)
285                 system_setting_item->user_data = user_data;
286
287     if (system_setting_set_changed_cb == NULL)
288     {
289         LOGE("[%s] IO_ERROR(0x%08x) : failed to call getter for the system settings", __FUNCTION__, SYSTEM_SETTINGS_ERROR_IO_ERROR);
290         return SYSTEM_SETTINGS_ERROR_IO_ERROR;
291     }
292
293         return system_setting_set_changed_cb(key, callback, user_data);
294 }
295
296
297 int system_settings_unset_changed_cb(system_settings_key_e key)
298 {
299     system_setting_h system_setting_item;
300         system_setting_unset_changed_callback_cb system_setting_unset_changed_cb;
301
302     if (system_settings_get_item(key, &system_setting_item))
303     {
304         LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid key", __FUNCTION__, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
305         return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
306     }
307
308         system_setting_unset_changed_cb = system_setting_item->unset_changed_cb;
309
310         // free the callback function from application side
311         if (system_setting_item->changed_cb)
312                 system_setting_item->changed_cb = NULL;
313         //-----------------------------------------------------
314
315     if (system_setting_unset_changed_cb == NULL)
316     {
317         LOGE("[%s] IO_ERROR(0x%08x) : failed to call getter for the system settings", __FUNCTION__, SYSTEM_SETTINGS_ERROR_IO_ERROR);
318         return SYSTEM_SETTINGS_ERROR_IO_ERROR;
319     }
320
321         return system_setting_unset_changed_cb(key);
322 }
323