[Bug] Fix font reset issue and remove some build warnings .
[apps/core/preloaded/settings.git] / setting-font / src / setting-font-main.c
1 /*
2  * setting
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
5  *
6  * Contact: MyoungJune Park <mj2004.park@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 #include <setting-font-main.h>
22 #include <sysman.h>
23 #include <fontconfig/fontconfig.h>
24 #include <unicode/uloc.h>
25 #include <libxml/xmlmemory.h>
26 #include <libxml/parser.h>
27 #include <Ecore_X.h>
28 #include <system_settings.h>
29
30 #define SETTING_FONT_MAX_LEN 10
31
32 static int setting_font_main_create(void *cb);
33 static int setting_font_main_destroy(void *cb);
34 static int setting_font_main_update(void *cb);
35 static int setting_font_main_cleanup(void *cb);
36
37 static void __setting_font_main_click_softkey_back_cb(void *data, Evas_Object *obj,
38                                         void *event_info);
39
40 setting_view setting_view_font_main = {
41         .create = setting_font_main_create,
42         .destroy = setting_font_main_destroy,
43         .update = setting_font_main_update,
44         .cleanup = setting_font_main_cleanup,
45 };
46
47 static char* __setting_font_main_get_font_size(int value)
48 {
49         const char* font_size_arr[] = {
50                                         _("IDS_ST_BODY_SMALL_M_TEXTSIZE"), // 0
51                                         _("IDS_ST_BODY_NORMAL_T_PROFILE"), // 1
52                                         _("IDS_ST_BODY_TEXTSTYLE_LARGE"),  // 2
53                                         _("IDS_EMAIL_OPT_HUGE_M_TEXTSIZE"),                        // 3
54                                         _("IDS_EMAIL_POP_GIANT_M_TEXTSIZE"),                       // 4
55                                 };
56         switch (value)
57         {
58                 case SYSTEM_SETTINGS_FONT_SIZE_SMALL:
59                         return (char *)font_size_arr[0];
60                 case SYSTEM_SETTINGS_FONT_SIZE_NORMAL:
61                         return (char *)font_size_arr[1];
62                 case SYSTEM_SETTINGS_FONT_SIZE_LARGE:
63                         return (char *)font_size_arr[2];
64                 case SYSTEM_SETTINGS_FONT_SIZE_HUGE:
65                         return (char *)font_size_arr[3];
66                 case SYSTEM_SETTINGS_FONT_SIZE_GIANT:
67                         return (char *)font_size_arr[4];
68                 default:
69                         return (char *)font_size_arr[1];
70         }
71 }
72
73 static Eina_List *__setting_font_main_available_list_get()
74 {
75         SETTING_TRACE_BEGIN
76         FcObjectSet *os = NULL;
77         FcFontSet *fs = NULL;
78         FcPattern *pat = NULL;
79         Eina_List *list = NULL;
80         FcConfig *font_config = NULL;
81
82         font_config = FcInitLoadConfigAndFonts();
83
84         setting_retvm_if(font_config == NULL, NULL, "Failed: FcInitLoadConfigAndFonts");
85
86         pat = FcPatternCreate();
87
88         os = FcObjectSetBuild(FC_FAMILY, FC_FILE, (char *) 0);
89
90         if (os) {
91                 fs = FcFontList(font_config, pat, os);
92                 FcObjectSetDestroy(os);
93                 os = NULL;
94         }
95
96         if (pat) {
97                 FcPatternDestroy(pat);
98                 pat = NULL;
99         }
100
101         if (fs) {
102                 int j;
103                 SETTING_TRACE_DEBUG("fs->nfont = %d", fs->nfont);
104
105                 for (j = 0; j < fs->nfont; j++) {
106                         FcChar8 *family = NULL;
107                         FcChar8 *file = NULL;
108
109                         if (FcPatternGetString(fs->fonts[j], FC_FILE, 0, &file) == FcResultMatch) {
110                                 //SETTING_TRACE_DEBUG("FC_FILE : %s\n", file);
111                                 int preload_path_len = safeStrLen(SETTING_FONT_PRELOAD_FONT_PATH);
112                                 int download_path_len = safeStrLen(SETTING_FONT_DOWNLOADED_FONT_PATH);
113                                 if (!safeStrNCmp((const char*)file, SETTING_FONT_PRELOAD_FONT_PATH, preload_path_len) ||
114                                         !safeStrNCmp((const char*)file, SETTING_FONT_DOWNLOADED_FONT_PATH, download_path_len)) {
115                                         if (FcPatternGetString(fs->fonts[j], FC_FAMILY, 0, &family) != FcResultMatch) {
116                                                 SETTING_TRACE_DEBUG("Family name is invalid");
117                                                 continue;
118                                         }
119
120                                         //SETTING_TRACE_DEBUG("family = %s", (char *)family);
121                                         if (eina_list_data_find(list, family) == NULL) {
122                                                 list = eina_list_append(list, family);
123                                                 /* list = eina_list_append(list, family); */
124                                                 // for TEST because there's 1 font on target.
125                                         }
126                                 }
127                         }
128                 }
129                 FcFontSetDestroy(fs);
130                 fs = NULL;
131         }
132
133         FcConfigDestroy(font_config);
134         font_config = NULL;
135         return list;
136 }
137
138 static void __setting_font_main_genlist_sel_cb(void *data, Evas_Object * obj, void *event_info)
139 {
140         //error check
141         setting_retm_if(data == NULL, "Data parameter is NULL");
142
143         retm_if(event_info == NULL, "Invalid argument: event info is NULL");
144         Elm_Object_Item *item = (Elm_Object_Item *) event_info;
145         elm_genlist_item_selected_set(item, 0);
146         Setting_GenGroupItem_Data *list_item = (Setting_GenGroupItem_Data *)elm_object_item_data_get(item);
147
148         SettingFontUG *ad = (SettingFontUG *)data;
149
150         if (ad->font_size == list_item) {
151                 setting_view_change(&setting_view_font_main, &setting_view_font_font_size, ad);
152         }
153 }
154
155 static void __setting_font_main_genlist_item_create(void *data)
156 {
157         SETTING_TRACE_BEGIN;
158         setting_retm_if(NULL == data, "data is NULL");
159
160         SettingFontUG *ad = (SettingFontUG *)data;
161         Elm_Object_Item *item = NULL;
162
163         if (ad->viewmode == FONT_SIZE_AND_FONT_SEL_UG || ad->viewmode == FONT_SEL_VIEW_APPCONTROL)              // font type only
164         {
165                 // [UI] Font Type
166                 item = elm_genlist_item_append(ad->genlist, &(ad->itc_seperator), NULL, NULL,
167                                         ELM_GENLIST_ITEM_NONE, NULL, NULL);
168                 elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
169                 char *font_type_name = NULL;
170                 font_type_name = cur_font_get();
171
172                 ad->font_type = setting_create_Gendial_exp_parent_field(ad->genlist,
173                                                                 &(ad->itc_2text_3_parent),
174                                                                 NULL, NULL,
175                                                                 SWALLOW_Type_INVALID,
176                                                                 SETTING_FONT_TYPE_STR,
177                                                                 font_type_name);
178                 if (ad->font_type) {
179                         ad->font_type->userdata = ad;
180                 } else {
181                         SETTING_TRACE_ERROR("ad->font_type is NULL");
182                 }
183                 G_FREE(ad->font_name);
184                 ad->font_name = g_strdup(font_type_name);
185                 G_FREE(font_type_name);
186         }
187
188         if (ad->viewmode == FONT_SIZE_AND_FONT_SEL_UG || ad->viewmode == FONT_SIZE_VIEW_APPCONTROL)             // font size only
189         {
190                 int err = -1;
191                 // [UI] Font Size
192                 int ret = setting_get_int_slp_key(INT_SLP_SETTING_ACCESSIBILITY_FONT_SIZE, &ad->ret_font_size, &err);
193                 if (ret != 0) {
194                         SETTING_TRACE_ERROR("get vconf failed");
195                 }
196
197                 ad->font_size = setting_create_Gendial_field_def(ad->genlist, &(ad->itc_2text_2),
198                                         __setting_font_main_genlist_sel_cb,
199                                         ad, SWALLOW_Type_INVALID, NULL,
200                                         NULL, ad->ret_font_size, SETTING_FONT_SIZE_STR,
201                                         __setting_font_main_get_font_size(ad->ret_font_size), NULL);
202                 if (ad->font_size) {
203                         ad->font_size->userdata = ad;
204                 } else {
205                         SETTING_TRACE_ERROR("ad->font_size is NULL");
206                 }
207
208 #if DISABlED_CODE
209         setting_create_Gendial_field_def(ad->genlist, &(ad->itc_bg_1icon), NULL,
210                                          ad, SWALLOW_Type_LAYOUT_SPECIALIZTION_X,
211                                          NULL, NULL, 0, SETTING_FONT_SIZE_DESC, NULL,
212                                          NULL);
213 #endif
214         }
215 }
216
217
218 /**
219  * font change
220  */
221 static void __setting_font_main_font_set(char *font_name)
222 {
223         retm_if(font_name == NULL, "Invalid argument: font_name is NULL");
224         xmlDocPtr doc = (xmlDocPtr)font_conf_doc_parse(SETTING_FONT_CONF_FILE, font_name);
225
226         if(doc != NULL) {
227                 xmlSaveFormatFile(SETTING_FONT_CONF_FILE, doc, 0);
228                 xmlFreeDoc(doc);
229                 doc = NULL;
230         }
231
232         font_config_set(font_name);
233
234         /* notification */
235         Ecore_X_Window ecore_win = ecore_x_window_root_first_get();
236         Ecore_X_Atom atom = ecore_x_atom_get("FONT_TYPE_change");
237         ecore_x_window_prop_string_set(ecore_win, atom, "slp");
238 }
239
240 static void __setting_font_main_popup_cb(void *data, Evas_Object *obj, void *event_info)
241 {
242         SETTING_TRACE_BEGIN;
243         setting_retm_if(data == NULL, "Data parameter is NULL");
244         SettingFontUG *ad = (SettingFontUG *) data;
245
246         switch (btn_type(obj)) {
247         case POPUP_RESPONSE_OK:
248                 {
249                         ad->font_type->sub_desc = (char *)g_strdup(_(ad->subitem->keyStr));
250                         elm_object_item_data_set(ad->font_type->item, ad->font_type);
251                         elm_genlist_item_update(ad->font_type->item);
252
253                         ad->prev_font = ad->subitem->chk_status;
254                         G_FREE(ad->font_name);
255                         ad->font_name = (char *)g_strdup(_(ad->subitem->keyStr));
256                         SETTING_TRACE("ad->subitem->keyStr = %s", ad->subitem->keyStr);
257                         __setting_font_main_font_set(ad->subitem->keyStr);
258                         ug_destroy_me(ad->ug);
259                         break;
260                 }
261         case POPUP_RESPONSE_CANCEL:
262                 {
263                         elm_radio_value_set(ad->subitem->rgd, ad->prev_font);
264
265                         break;
266                 }
267         default:
268                 {
269                         break;
270                 }
271         }
272
273         if (ad->popup) {
274                 evas_object_del(ad->popup);
275                 ad->popup = NULL;
276         }
277 }
278
279 static void __setting_font_main_popup(void *data)
280 {
281         SETTING_TRACE_BEGIN;
282         setting_retm_if(data == NULL, "Data parameter is NULL");
283         SettingFontUG *ad = (SettingFontUG *) data;
284
285         char popup_desc[SETTING_STR_SLP_LEN + 1] = {0,};
286         snprintf(popup_desc, SETTING_STR_SLP_LEN, "%s %s?", _(SETTING_FONT_POPUP_DESC), ad->subitem->keyStr);
287
288         ad->popup = setting_create_popup_with_btn(ad, ad->win_get, NULL,
289                                          popup_desc,
290                                          __setting_font_main_popup_cb, 0,
291                                          2, _("IDS_COM_SK_YES"),_("IDS_COM_SK_NO"));
292 }
293
294
295 /**
296  * [UI][expandable list][event handler:selection][font change]
297  */
298 void __setting_font_main_sub_list_sel_cb(void *data, Evas_Object *obj, void *event_info)
299 {
300         SETTING_TRACE_BEGIN;
301         /* error check */
302         retm_if(event_info == NULL, "Invalid argument: event info is NULL");
303         Elm_Object_Item *subitem = (Elm_Object_Item *) event_info;
304         elm_genlist_item_selected_set(subitem, 0);
305         Setting_GenGroupItem_Data *data_subItem = elm_object_item_data_get(subitem);
306         ret_if(NULL == data_subItem);
307
308         SettingFontUG *ad = (SettingFontUG *) data;
309
310         /*  if not change, return */
311         if (ad->prev_font == data_subItem->chk_status) {
312                 SETTING_TRACE("NOT CHANGED ");
313                 return;
314         }
315
316         ad->subitem = data_subItem;
317         elm_radio_value_set(ad->subitem->rgd, ad->subitem->chk_status);
318
319         __setting_font_main_popup(ad);
320 }
321
322 static void __setting_font_main_exp_cb(void *data, Evas_Object *obj, void *event_info)
323 {
324         ret_if(NULL == data || NULL == event_info);
325         SETTING_TRACE_BEGIN;
326         SettingFontUG *ad = (SettingFontUG *) data;
327         Elm_Object_Item *parentItem = event_info;       /* parent item */
328         Setting_GenGroupItem_Data *data_parentItem = elm_object_item_data_get(parentItem);      /* parent data */
329         Evas_Object *scroller = elm_object_item_widget_get(parentItem);
330
331         Evas_Object *rgd = NULL;
332         if (data_parentItem == ad->font_type) {
333                 rgd = elm_radio_add(scroller);
334                 elm_radio_value_set(rgd, -1);
335
336                 Eina_List *font_list = NULL;
337                 Eina_List *l = NULL;
338                 FcChar8 *font_data = NULL;
339                 font_list = __setting_font_main_available_list_get();
340
341                 int i = 0;
342                 int matched_font = -1;
343                 Setting_GenGroupItem_Data *item = NULL;
344                 EINA_LIST_FOREACH(font_list, l, font_data)
345                 {
346                         item = setting_create_Gendial_exp_sub_field(scroller, &(ad->itc_1icon_1text_sub),
347                                                      __setting_font_main_sub_list_sel_cb, ad,
348                                                      parentItem, SWALLOW_Type_1RADIO, rgd,
349                                                      i, (const char*)font_data,
350                                                      NULL);
351
352                         if (item) {
353                                 item->userdata = ad;
354                                 if (!safeStrCmp(ad->font_name, (const char*)font_data)) {
355                                         matched_font = i;
356                                 }
357                         }
358
359                         i++;
360                 }
361
362                 /* update check status */
363                 if (matched_font != -1) {
364                         elm_radio_value_set(rgd, matched_font);
365                 }
366
367                 ad->prev_font = matched_font;
368         }
369 }
370
371
372 /* ***************************************************
373  *
374  *basic func
375  *
376  ***************************************************/
377 static int setting_font_main_create(void *cb)
378 {
379         SETTING_TRACE_BEGIN;
380         /* error check */
381         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
382
383         SettingFontUG *ad = (SettingFontUG *) cb;
384
385         retvm_if(ad->win_main_layout == NULL, SETTING_DRAW_ERR_FAIL_LOAD_EDJ,
386                  "win_main_layout is NULL");
387
388         ad->ly_main =
389             setting_create_layout_navi_bar_genlist(ad->win_main_layout,
390                                                    ad->win_get,
391                                                    _(KeyStr_Font),
392                                                    _("IDS_COM_BODY_BACK"),
393                                                    NULL,
394                                                    __setting_font_main_click_softkey_back_cb,
395                                                    NULL,
396                                                    ad, &ad->genlist, &ad->navibar);
397
398         setting_enable_expandable_genlist(ad->genlist, ad,
399                                 __setting_font_main_exp_cb, NULL);
400
401         __setting_font_main_genlist_item_create(ad);
402
403         setting_view_font_main.is_create = 1;
404         return SETTING_RETURN_SUCCESS;
405 }
406
407 static int setting_font_main_destroy(void *cb)
408 {
409         /* error check */
410         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
411
412         SettingFontUG *ad = (SettingFontUG *) cb;
413
414         if (ad->popup) {
415                 evas_object_del(ad->popup);
416                 ad->popup = NULL;
417         }
418
419         if (ad->ly_main != NULL) {
420                 evas_object_del(ad->ly_main);
421                 setting_view_font_main.is_create = 0;
422         }
423
424         return SETTING_RETURN_SUCCESS;
425 }
426
427 static int setting_font_main_update(void *cb)
428 {
429         SETTING_TRACE_BEGIN;
430         /* error check */
431         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
432
433         SettingFontUG *ad = (SettingFontUG *) cb;
434
435         if (ad->ly_main != NULL) {
436                 evas_object_show(ad->ly_main);
437
438                 int value = -1;
439                 int err = -1;
440                 int ret = setting_get_int_slp_key(INT_SLP_SETTING_ACCESSIBILITY_FONT_SIZE, &value, &err);
441                 if (ret != 0) {
442                         SETTING_TRACE_ERROR("get vconf failed");
443                 }
444
445                 if (ad->font_size) {
446                         G_FREE(ad->font_size->sub_desc);
447                         ad->font_size->sub_desc = (char *)g_strdup(__setting_font_main_get_font_size(value));
448                         elm_object_item_data_set(ad->font_size->item, ad->font_size);
449                         elm_genlist_item_update(ad->font_size->item);
450                 }
451
452                 setting_font_update_vconf_key(ad, SETTING_VCONF_INT_TYPE,
453                                               INT_SLP_SETTING_LCD_TIMEOUT_NORMAL);
454         }
455
456         return SETTING_RETURN_SUCCESS;
457 }
458
459 static int setting_font_main_cleanup(void *cb)
460 {
461         /* error check */
462         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
463
464         SettingFontUG *ad = (SettingFontUG *) cb;
465
466         if (ad->ly_main != NULL) {
467                 evas_object_hide(ad->ly_main);
468         }
469
470         return SETTING_RETURN_SUCCESS;
471 }
472
473 /* ***************************************************
474  *
475  *call back func
476  *
477  ***************************************************/
478
479 static void __setting_font_main_click_softkey_back_cb(void *data, Evas_Object *obj,
480                                         void *event_info)
481 {
482         SETTING_TRACE_BEGIN;
483         /* error check */
484         ret_if(data == NULL);
485
486         SettingFontUG *ad = (SettingFontUG *) data;
487
488         if (ad->viewmode == FONT_SEL_VIEW_APPCONTROL)
489         {
490                 service_h svc;
491                 if (service_create(&svc))
492                         return;
493
494                 service_add_extra_data(svc, "category", "FontType");
495                 service_add_extra_data(svc, "FontType", ad->font_name);
496
497                 SETTING_TRACE(" SERVICE_ADD_EXTRA : %s %s","category", "FontType");
498                 SETTING_TRACE(" SERVICE_ADD_EXTRA : %s %s","FontType", ad->font_name);
499
500
501                 ug_send_result(ad->ug, svc);
502                 service_destroy(svc);
503         }
504         else if (ad->viewmode == FONT_SIZE_VIEW_APPCONTROL)
505         {
506                 service_h svc;
507                 if (service_create(&svc))
508                         return;
509
510                 int err;
511                 int ret = setting_set_int_slp_key(INT_SLP_SETTING_ACCESSIBILITY_FONT_SIZE, ad->ret_font_size, &err);
512                 setting_retm_if(ret == -1, "Failed to set vconf");
513
514                 char font_size[SETTING_FONT_MAX_LEN] = {0,};
515                 snprintf(font_size, SETTING_FONT_MAX_LEN, "%d", ad->ret_font_size);
516
517                 service_add_extra_data(svc, "category", "FontSize");
518                 service_add_extra_data(svc, "FontSize", font_size);
519
520                 SETTING_TRACE(" SERVICE_ADD_EXTRA : %s %s","category", "FontSize");
521                 SETTING_TRACE(" SERVICE_ADD_EXTRA : %s %s","FontSize", font_size);
522
523
524                 ug_send_result(ad->ug, svc);
525                 service_destroy(svc);
526         }
527         /* Send destroy request */
528         ug_destroy_me(ad->ug);
529 }