new release for RSA
[platform/core/api/system-settings.git] / src / system_setting_platform.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 <unistd.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24
25 #include <dlog.h>
26 #include <vconf.h>
27
28 #include <glib.h>
29 #include <libxml/xmlmemory.h>
30 #include <libxml/parser.h>
31
32 #include <Ecore_X.h>
33 #include <Elementary.h>
34
35 #include <system_settings.h>
36 #include <system_settings_private.h>
37
38
39 #ifdef LOG_TAG
40 #undef LOG_TAG
41 #endif
42
43 #define SMALL_FONT_DPI                      (-80)
44 #define MIDDLE_FONT_DPI                     (-100)
45 #define LARGE_FONT_DPI                      (-150)
46 #define HUGE_FONT_DPI                       (-190)
47 #define GIANT_FONT_DPI                      (-250)
48
49 #define SETTING_FONT_CONF_FILE "/opt/etc/fonts/conf.avail/99-slp.conf"
50 #define SETTING_STR_SLP_LEN  256
51
52 static char* _get_cur_font();
53 static void font_size_set();
54 static int __font_size_get();
55
56 static void font_config_set(char *font_name);
57 static void font_config_set_notification();
58
59 int system_setting_get_incoming_call_ringtone(system_settings_key_e key, system_setting_data_type_e data_type, void** value)
60 {
61         char* vconf_value;
62         if (system_setting_vconf_get_value_string(VCONFKEY_SETAPPL_CALL_RINGTONE_PATH_STR, &vconf_value)) {
63                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
64         }
65         *value = vconf_value;
66         return SYSTEM_SETTINGS_ERROR_NONE;
67 }
68
69
70 int system_setting_get_wallpaper_home_screen(system_settings_key_e key, system_setting_data_type_e data_type, void** value)
71 {
72         char* vconf_value;
73         if (system_setting_vconf_get_value_string(VCONFKEY_BGSET, &vconf_value)) {
74                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
75         }
76         *value = vconf_value;
77         return SYSTEM_SETTINGS_ERROR_NONE;
78 }
79
80
81 int system_setting_get_wallpaper_lock_screen(system_settings_key_e key, system_setting_data_type_e data_type, void** value)
82 {
83         char* vconf_value;
84
85         if (system_setting_vconf_get_value_string(VCONFKEY_IDLE_LOCK_BGSET, &vconf_value)) {
86                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
87         }
88         *value = vconf_value;
89
90         return SYSTEM_SETTINGS_ERROR_NONE;
91 }
92
93
94 // [int] vconf GET
95 int system_setting_get_font_size(system_settings_key_e key, system_setting_data_type_e data_type, void** value)
96 {
97         printf("system_setting_get_font_size \n");
98         int vconf_value;
99
100         if (system_setting_vconf_get_value_int(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, &vconf_value)) {
101                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
102         }
103         *value = (void*)vconf_value;
104
105         return SYSTEM_SETTINGS_ERROR_NONE;
106 }
107
108
109 // [int] vconf GET
110 int system_setting_get_font_type(system_settings_key_e key, system_setting_data_type_e data_type, void** value)
111 {
112         printf("system_setting_get_font_type\n");
113         //int vconf_value;
114
115         char* font_name = _get_cur_font();
116         #if 0
117         if (system_setting_vconf_get_value_int(VCONFKEY_SETAPPL_FONT_TYPE_INT, &vconf_value)) {
118                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
119         }
120         //*value = (void*)vconf_value;
121         #endif
122         *value = (void*)font_name;
123
124         return SYSTEM_SETTINGS_ERROR_NONE;
125 }
126
127
128 int system_setting_get_motion_activation(system_settings_key_e key, system_setting_data_type_e data_type, void** value)
129 {
130         bool vconf_value;
131
132         if (system_setting_vconf_get_value_bool(VCONFKEY_SETAPPL_MOTION_ACTIVATION, &vconf_value)) {
133                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
134         }
135         *value = (void*)vconf_value;
136
137         return SYSTEM_SETTINGS_ERROR_NONE;
138 }
139
140 ////////////////////////////////////////////////////////////////////////////////////////////////////
141
142 int system_setting_set_incoming_call_ringtone(system_settings_key_e key, system_setting_data_type_e data_type, void* value)
143 {
144         printf(" mock --> real system_setting_set_incoming_call_ringtone \n");
145         char* vconf_value;
146         vconf_value = (char*)value;
147         if (system_setting_vconf_set_value_string(VCONFKEY_SETAPPL_CALL_RINGTONE_PATH_STR, vconf_value)) {
148                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
149         }
150
151         return SYSTEM_SETTINGS_ERROR_NONE;
152 }
153
154 int system_setting_set_wallpaper_home_screen(system_settings_key_e key, system_setting_data_type_e data_type, void* value)
155 {
156         printf(" mock --> real system_setting_set_wallpaper_home_screen \n");
157
158         char* vconf_value;
159         vconf_value = (char*)value;
160         if (system_setting_vconf_set_value_string(VCONFKEY_BGSET, vconf_value)) {
161                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
162         }
163
164         return SYSTEM_SETTINGS_ERROR_NONE;
165 }
166
167 int system_setting_set_wallpaper_lock_screen(system_settings_key_e key, system_setting_data_type_e data_type, void* value)
168 {
169         printf(" mock --> real system_setting_set_wallpaper_lock_screen \n");
170
171         char* vconf_value;
172         vconf_value = (char*)value;
173         if (system_setting_vconf_set_value_string(VCONFKEY_IDLE_LOCK_BGSET, vconf_value)) {
174                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
175         }
176
177         return SYSTEM_SETTINGS_ERROR_NONE;
178 }
179
180 int system_setting_set_font_size(system_settings_key_e key, system_setting_data_type_e data_type, void* value)
181 {
182         printf(" real system_setting_set_font_size \n");
183         int* vconf_value;
184         vconf_value = (int*)value;
185
186         if (*vconf_value < 0 || *vconf_value > SYSTEM_SETTINGS_FONT_SIZE_GIANT) {
187                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
188         }
189
190         if (system_setting_vconf_set_value_int(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, *vconf_value)) {
191                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
192         }
193         font_size_set();
194
195         printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>. system_setting_set_font_size called \n");
196
197         return SYSTEM_SETTINGS_ERROR_NONE;
198 }
199
200 int system_setting_set_font_type(system_settings_key_e key, system_setting_data_type_e data_type, void* value)
201 {
202         printf(" real system_setting_set_font_type \n");
203         char* font_name = NULL;
204         font_name = (char*)value;
205
206         printf(">>>>>>>>>>>>> font name = %s \n", font_name);
207         font_config_set(font_name);
208         font_config_set_notification();
209
210         char* vconf_value;
211         vconf_value = (char*)value;
212         if (system_setting_vconf_set_value_string(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_NAME, vconf_value)) {
213                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
214         }
215         return SYSTEM_SETTINGS_ERROR_NONE;
216 }
217
218 int system_setting_set_motion_activation(system_settings_key_e key, system_setting_data_type_e data_type, void* value)
219 {
220         printf(" mock --> real system_setting_set_motion_activation \n");
221
222         bool* vconf_value;
223         vconf_value = (bool*)value;
224         if (system_setting_vconf_set_value_bool(VCONFKEY_SETAPPL_MOTION_ACTIVATION, *vconf_value)) {
225                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
226         }
227         return SYSTEM_SETTINGS_ERROR_NONE;
228 }
229
230 /////////////////////////////////////////////////////////////////////////////////////////////////
231 //
232
233 int system_setting_set_changed_callback_incoming_call_ringtone(system_settings_key_e key, system_settings_changed_cb callback, void *user_data)
234 {
235         return system_setting_vconf_set_changed_cb(VCONFKEY_SETAPPL_CALL_RINGTONE_PATH_STR, SYSTEM_SETTINGS_KEY_INCOMING_CALL_RINGTONE, 0);
236 }
237
238 int system_setting_unset_changed_callback_incoming_call_ringtone(system_settings_key_e key)
239 {
240         return system_setting_vconf_unset_changed_cb(VCONFKEY_SETAPPL_CALL_RINGTONE_PATH_STR, 0);
241 }
242
243 int system_setting_set_changed_callback_wallpaper_home_screen(system_settings_key_e key, system_settings_changed_cb callback, void *user_data)
244 {
245         return system_setting_vconf_set_changed_cb(VCONFKEY_BGSET, SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN, 0);
246 }
247
248 int system_setting_unset_changed_callback_wallpaper_home_screen(system_settings_key_e key)
249 {
250         return system_setting_vconf_unset_changed_cb(VCONFKEY_BGSET, 0);
251 }
252
253 int system_setting_set_changed_callback_wallpaper_lock_screen(system_settings_key_e key, system_settings_changed_cb callback, void *user_data)
254 {
255         return system_setting_vconf_set_changed_cb(VCONFKEY_IDLE_LOCK_BGSET,SYSTEM_SETTINGS_KEY_WALLPAPER_LOCK_SCREEN, 0);
256 }
257
258 int system_setting_unset_changed_callback_wallpaper_lock_screen(system_settings_key_e key)
259 {
260         return system_setting_vconf_unset_changed_cb(VCONFKEY_IDLE_LOCK_BGSET, 0);
261 }
262
263 int system_setting_set_changed_callback_font_size(system_settings_key_e key, system_settings_changed_cb callback, void *user_data)
264 {
265         return system_setting_vconf_set_changed_cb(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE,SYSTEM_SETTINGS_KEY_FONT_SIZE, 1);
266 }
267
268 int system_setting_unset_changed_callback_font_size(system_settings_key_e key)
269 {
270         return system_setting_vconf_unset_changed_cb(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, 1);
271 }
272
273 /**
274  * @todo need to add custom event notification method
275  */
276 int system_setting_set_changed_callback_font_type(system_settings_key_e key, system_settings_changed_cb callback, void *user_data)
277 {
278         return system_setting_vconf_set_changed_cb(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_NAME,SYSTEM_SETTINGS_KEY_FONT_TYPE, 2);
279 }
280
281 int system_setting_unset_changed_callback_font_type(system_settings_key_e key)
282 {
283         return system_setting_vconf_unset_changed_cb(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_NAME,2);
284 }
285
286 // TODO : 2th argument, callback, is not in use.
287 int system_setting_set_changed_callback_motion_activation(system_settings_key_e key, system_settings_changed_cb callback, void *user_data)
288 {
289         printf("system_setting_set_changed_callback_motion_activation \n");
290         return system_setting_vconf_set_changed_cb(VCONFKEY_SETAPPL_MOTION_ACTIVATION, SYSTEM_SETTINGS_KEY_MOTION_ACTIVATION, 3);
291 }
292
293 int system_setting_unset_changed_callback_motion_activation(system_settings_key_e key)
294 {
295         printf("system_setting_unset_changed_callback_motion_activation \n");
296         return system_setting_vconf_unset_changed_cb(VCONFKEY_SETAPPL_MOTION_ACTIVATION, 3);
297 }
298
299 static char* _get_cur_font()
300 {
301     printf("get current font \n");
302
303     xmlDocPtr doc = NULL;
304     xmlNodePtr cur = NULL;
305     xmlNodePtr cur2 = NULL;
306     xmlNodePtr cur3 = NULL;
307     xmlChar *key = NULL;
308
309     char *font_name = NULL;
310
311     doc = xmlParseFile(SETTING_FONT_CONF_FILE);
312
313     cur = xmlDocGetRootElement(doc);
314
315     if(cur == NULL) {
316         printf("empty document");
317         xmlFreeDoc(doc);
318         doc = NULL;
319         return NULL;
320     }
321
322     if(xmlStrcmp(cur->name, (const xmlChar *)"fontconfig")) {
323         printf("document of the wrong type, root node != fontconfig");
324         xmlFreeDoc(doc);
325         doc = NULL;
326         return NULL;
327     }
328
329     cur = cur->xmlChildrenNode;
330
331     while(cur != NULL)
332     {
333         if((!xmlStrcmp(cur->name, (const xmlChar *)"match")))
334         {
335             cur2 = cur->xmlChildrenNode;
336             while(cur2 != NULL)
337             {
338                 if((!xmlStrcmp(cur2->name, (const xmlChar *)"edit")))
339                 {
340                     cur3 = cur2->xmlChildrenNode;
341                     while(cur3 != NULL)
342                     {
343                         if((!xmlStrcmp(cur3->name, (const xmlChar *)"string")))
344                         {
345                             key = xmlNodeListGetString(doc, cur3->xmlChildrenNode, 1);
346                             //printf("string is: %s", key);
347
348                             font_name = g_strdup((char *)key);
349                             xmlFree(key);
350                             key = NULL;
351                             xmlFreeDoc(doc);
352                             doc = NULL;
353                             return font_name;
354                         }
355                        cur3 = cur3->next;
356                     }
357                 }
358                 cur2 = cur2->next;
359             }
360         }
361         cur = cur->next;
362     }
363
364     xmlFreeDoc(doc);
365     doc = NULL;
366     return NULL;
367 }
368
369 static void font_config_set_notification()
370 {
371     /* notification */
372         Ecore_X_Window ecore_win = ecore_x_window_root_first_get();
373         printf("FONT CHANGE NOTIFICATION >>>>>>>>>> : %d  \n", (unsigned int)ecore_win);
374         Ecore_X_Atom atom = ecore_x_atom_get("FONT_TYPE_change");
375         ecore_x_window_prop_string_set(ecore_win, atom, "slp");
376 }
377
378 static void font_config_set(char *font_name)
379 {
380     Eina_List *text_classes = NULL;
381     Elm_Text_Class *etc = NULL;
382     const Eina_List *l = NULL;
383     Eina_List *fo_list = NULL;
384     Elm_Font_Overlay *efo = NULL;
385     int font_size = __font_size_get();
386     int size = 0;
387
388     text_classes = elm_config_text_classes_list_get();
389
390     fo_list = elm_config_font_overlay_list_get();
391
392     Eina_List *ll = NULL;
393     Eina_List *l_next = NULL;
394
395     Eina_Bool slp_medium_exist = EINA_FALSE;
396     Eina_Bool slp_roman_exist = EINA_FALSE;
397     Eina_Bool slp_bold_exist = EINA_FALSE;
398     Eina_Bool slp_regular_exist = EINA_FALSE;
399
400     EINA_LIST_FOREACH_SAFE(fo_list, ll, l_next, efo)
401     {
402         if (!strcmp(efo->text_class, "slp_medium")) {
403             elm_config_font_overlay_set(efo->text_class, (const char*)font_name, efo->size);
404             slp_medium_exist = EINA_TRUE;
405         } else if (!strcmp(efo->text_class, "slp_roman")) {
406             elm_config_font_overlay_set(efo->text_class, (const char*)font_name, efo->size);
407             slp_roman_exist = EINA_TRUE;
408         } else if (!strcmp(efo->text_class, "slp_bold")) {
409             elm_config_font_overlay_set(efo->text_class, (const char*)font_name, efo->size);
410             slp_bold_exist = EINA_TRUE;
411         } else if (!strcmp(efo->text_class, "slp_regular")) {
412             elm_config_font_overlay_set(efo->text_class, (const char*)font_name, efo->size);
413             slp_regular_exist = EINA_TRUE;
414         }
415     }
416
417     /* if slp_XX do not exist, need to set them, font size is -100(100%) */
418     if (slp_medium_exist == EINA_FALSE) {
419         elm_config_font_overlay_set("slp_medium", (const char*)font_name,  MIDDLE_FONT_DPI);
420     }
421     if (slp_roman_exist == EINA_FALSE) {
422         elm_config_font_overlay_set("slp_roman", (const char*)font_name,  MIDDLE_FONT_DPI);
423     }
424     if (slp_bold_exist == EINA_FALSE) {
425         elm_config_font_overlay_set("slp_bold", (const char*)font_name,  MIDDLE_FONT_DPI);
426     }
427     if (slp_regular_exist == EINA_FALSE) {
428         elm_config_font_overlay_set("slp_regular", (const char*)font_name,  MIDDLE_FONT_DPI);
429     }
430
431     EINA_LIST_FOREACH(text_classes, l, etc)
432     {
433         ll = NULL;
434
435         size = font_size;
436         EINA_LIST_FOREACH(fo_list, ll, efo)
437         {
438             if (!strcmp(etc->name, efo->text_class)) {
439                 size = efo->size;
440             }
441         }
442         elm_config_font_overlay_set(etc->name, (const char*)font_name, size);
443     }
444
445     elm_config_font_overlay_apply();
446     elm_config_all_flush();
447     elm_config_save();
448     elm_config_text_classes_list_free(text_classes);
449     text_classes = NULL;
450 }
451
452 static void font_size_set()
453 {
454     Eina_List *text_classes = NULL;
455     Elm_Text_Class *etc = NULL;
456     const Eina_List *l = NULL;
457     int font_size = __font_size_get();
458     char *font_name = _get_cur_font();
459
460     if (font_size == -1) {
461         //SETTING_TRACE_DEBUG("failed to call font_size_get");
462         return;
463     } else {
464                 printf(">> font name = %s, font size = %d \n", font_name, font_size);
465         }
466
467     text_classes = elm_config_text_classes_list_get();
468
469     EINA_LIST_FOREACH(text_classes, l, etc)
470     {
471         elm_config_font_overlay_set(etc->name, font_name, font_size);
472     }
473
474         elm_config_font_overlay_apply();
475     elm_config_all_flush();
476     elm_config_save();
477     elm_config_text_classes_list_free(text_classes);
478     text_classes = NULL;
479     //G_FREE(font_name);
480     g_free(font_name);
481         printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>. font_size_set called \n");
482 }
483
484 static int __font_size_get()
485 {
486     int font_size = -1;
487     int err = -1;
488
489         int vconf_value = -1;
490         if (system_setting_vconf_get_value_int(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, &vconf_value)) {
491                 return -1;
492         }
493
494     switch(vconf_value) {
495     case SYSTEM_SETTINGS_FONT_SIZE_SMALL:
496         font_size = SMALL_FONT_DPI;
497         break;
498     case SYSTEM_SETTINGS_FONT_SIZE_NORMAL:
499         font_size = MIDDLE_FONT_DPI;
500         break;
501     case SYSTEM_SETTINGS_FONT_SIZE_LARGE:
502         font_size = LARGE_FONT_DPI;
503         break;
504     case SYSTEM_SETTINGS_FONT_SIZE_HUGE:
505         font_size = HUGE_FONT_DPI;
506         break;
507     case SYSTEM_SETTINGS_FONT_SIZE_GIANT:
508         font_size = GIANT_FONT_DPI;
509         break;
510     default:
511         font_size = MIDDLE_FONT_DPI;
512         break;
513     }
514     return font_size;
515 }