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