724e6dab6a4e4f8025e46e5651ae63575a7f2831
[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
29 static int setting_font_main_create(void *cb);
30 static int setting_font_main_destroy(void *cb);
31 static int setting_font_main_update(void *cb);
32 static int setting_font_main_cleanup(void *cb);
33
34 static void __setting_font_main_click_softkey_back_cb(void *data, Evas_Object *obj,
35                                         void *event_info);
36
37 setting_view setting_view_font_main = {
38         .create = setting_font_main_create,
39         .destroy = setting_font_main_destroy,
40         .update = setting_font_main_update,
41         .cleanup = setting_font_main_cleanup,
42 };
43
44 static char* __setting_font_main_get_font_size(int value)
45 {
46         const char* font_size_arr[] = {
47                                         _("IDS_ST_BODY_SMALL_M_TEXTSIZE"), // 0
48                                         _("IDS_ST_BODY_NORMAL_T_PROFILE"), // 1
49                                         _("IDS_ST_BODY_TEXTSTYLE_LARGE"),  // 2
50                                         _("IDS_EMAIL_OPT_HUGE_M_TEXTSIZE"),                        // 3
51                                         _("IDS_EMAIL_POP_GIANT_M_TEXTSIZE"),                       // 4
52                                 };
53         switch (value)
54         {
55                 case SYSTEM_SETTINGS_FONT_SIZE_SMALL:
56                         return (char *)font_size_arr[0];
57                 case SYSTEM_SETTINGS_FONT_SIZE_NORMAL:
58                         return (char *)font_size_arr[1];
59                 case SYSTEM_SETTINGS_FONT_SIZE_LARGE:
60                         return (char *)font_size_arr[2];
61                 case SYSTEM_SETTINGS_FONT_SIZE_HUGE:
62                         return (char *)font_size_arr[3];
63                 case SYSTEM_SETTINGS_FONT_SIZE_GIANT:
64                         return (char *)font_size_arr[4];
65                 default:
66                         return (char *)font_size_arr[1];
67         }
68 }
69
70 static Eina_List *__setting_font_main_available_list_get()
71 {
72         SETTING_TRACE_BEGIN
73         FcObjectSet *os = NULL;
74         FcFontSet *fs = NULL;
75         FcPattern *pat = NULL;
76         Eina_List *list = NULL;
77         FcConfig *font_config = NULL;
78
79         font_config = FcInitLoadConfigAndFonts();
80
81         setting_retvm_if(font_config == NULL, NULL, "Failed: FcInitLoadConfigAndFonts");
82
83         pat = FcPatternCreate();
84
85         os = FcObjectSetBuild(FC_FAMILY, FC_FILE, (char *) 0);
86
87         if (os) {
88                 fs = FcFontList(font_config, pat, os);
89                 FcObjectSetDestroy(os);
90                 os = NULL;
91         }
92
93         if (pat) {
94                 FcPatternDestroy(pat);
95                 pat = NULL;
96         }
97
98         if (fs) {
99                 int j;
100                 SETTING_TRACE_DEBUG("fs->nfont = %d", fs->nfont);
101
102                 for (j = 0; j < fs->nfont; j++) {
103                         FcChar8 *family = NULL;
104                         FcChar8 *file = NULL;
105
106                         if (FcPatternGetString(fs->fonts[j], FC_FILE, 0, &file) == FcResultMatch) {
107                                 //SETTING_TRACE_DEBUG("FC_FILE : %s\n", file);
108                                 int preload_path_len = safeStrLen(SETTING_FONT_PRELOAD_FONT_PATH);
109                                 int download_path_len = safeStrLen(SETTING_FONT_DOWNLOADED_FONT_PATH);
110                                 if (!safeStrNCmp((const char*)file, SETTING_FONT_PRELOAD_FONT_PATH, preload_path_len) ||
111                                         !safeStrNCmp((const char*)file, SETTING_FONT_DOWNLOADED_FONT_PATH, download_path_len)) {
112                                         if (FcPatternGetString(fs->fonts[j], FC_FAMILY, 0, &family) != FcResultMatch) {
113                                                 SETTING_TRACE_DEBUG("Family name is invalid");
114                                                 continue;
115                                         }
116
117                                         //SETTING_TRACE_DEBUG("family = %s", (char *)family);
118                                         if (eina_list_data_find(list, family) == NULL) {
119                                                 list = eina_list_append(list, family);
120                                                 /* list = eina_list_append(list, family); */
121                                                 // for TEST because there's 1 font on target.
122                                         }
123                                 }
124                         }
125                 }
126                 FcFontSetDestroy(fs);
127                 fs = NULL;
128         }
129
130         FcConfigDestroy(font_config);
131         font_config = NULL;
132         return list;
133 }
134
135 void __setting_font_main_launch_ug_destroy_cb(ui_gadget_h ug, void *priv)
136 {
137         SETTING_TRACE_BEGIN;
138         /* if(ug) ug_destroy(ug); */
139         /* restore the '<-' button on the navigate bar */
140         retm_if(priv == NULL, "priv is NULL");
141         SettingFontUG *ad = (SettingFontUG *) priv;
142         if (ug) {
143                 ug_destroy(ug);
144         }
145         Elm_Object_Item *navi_it = elm_naviframe_top_item_get(ad->navibar);
146         retm_if(navi_it == NULL, "navi_it is NULL");
147         Evas_Object *back_btn = elm_object_item_part_content_get(navi_it, "prev_btn");
148
149         if (back_btn != NULL) {
150                 elm_object_style_set(back_btn, NAVI_BACK_BUTTON_STYLE); /* take into effect */
151         }
152
153         // update font size item
154         int value = -1;
155         int err = -1;
156         int ret = setting_get_int_slp_key(INT_SLP_SETTING_ACCESSIBILITY_FONT_SIZE, &value, &err);
157         retm_if(ret != 0, "get vconf failed");
158
159         if (ad->font_size) {
160                 G_FREE(ad->font_size->sub_desc);
161                 ad->font_size->sub_desc = g_strdup(__setting_font_main_get_font_size(value));
162
163                 elm_object_item_data_set(ad->font_size->item, ad->font_size);
164                 elm_genlist_item_update(ad->font_size->item);
165         }
166 }
167
168 void __setting_font_main_launch_ug_layout_cb(ui_gadget_h ug, enum ug_mode mode, void *priv)
169 {
170         SETTING_TRACE_BEGIN;
171         retm_if(priv == NULL, "priv is NULL");
172         SettingFontUG *ad = (SettingFontUG *) priv;
173         Evas_Object *base = NULL;
174         base = (Evas_Object *) ug_get_layout(ug);
175         retm_if(base == NULL, "base is NULL");
176
177         switch (mode) {
178         case UG_MODE_FULLVIEW:
179                 evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
180                 elm_win_resize_object_add(ad->win_get, base);
181                 evas_object_show(base);
182                 break;
183         default:
184                 break;
185         }
186         SETTING_TRACE_END;
187 }
188
189
190 static void __setting_font_main_launch_ug(void *data)
191 {
192         SETTING_TRACE_BEGIN;
193         /* error check */
194         retm_if(data == NULL, "Data parameter is NULL");
195
196         SettingFontUG *ad = (SettingFontUG *) data;
197
198         service_h svc;
199         if (service_create(&svc)) {
200                 return;
201         }
202
203         service_add_extra_data(svc, "caller", "font_size");
204
205         struct ug_cbs *cbs = (struct ug_cbs *)calloc(1, sizeof(struct ug_cbs));
206         if (!cbs) {
207                 SETTING_TRACE_ERROR("calloc failed");
208                 service_destroy(svc);
209                 return;
210         }
211         cbs->layout_cb = __setting_font_main_launch_ug_layout_cb;
212         cbs->result_cb = NULL;
213         cbs->destroy_cb = __setting_font_main_launch_ug_destroy_cb;
214         cbs->priv = (void *)ad;
215
216         ui_gadget_h ug = ug_create(ad->ug, "setting-accessibility-efl", UG_MODE_FULLVIEW, svc, cbs);
217         if (NULL == ug) {       /* error handling */
218                 SETTING_TRACE_ERROR("NULL == ug");
219         }
220
221         //bundle_free(b);
222         service_destroy(svc);
223         FREE(cbs);
224 }
225
226 static void __setting_font_main_genlist_sel_cb(void *data, Evas_Object * obj, void *event_info)
227 {
228         //error check
229         setting_retm_if(data == NULL, "Data parameter is NULL");
230
231         retm_if(event_info == NULL, "Invalid argument: event info is NULL");
232         Elm_Object_Item *item = (Elm_Object_Item *) event_info;
233         elm_genlist_item_selected_set(item, 0);
234         Setting_GenGroupItem_Data *list_item = (Setting_GenGroupItem_Data *)elm_object_item_data_get(item);
235
236         SettingFontUG *ad = (SettingFontUG *)data;
237
238         if (ad->font_size == list_item) {
239                 __setting_font_main_launch_ug(ad);
240         }
241 }
242
243 static void __setting_font_main_genlist_item_create(void *data)
244 {
245         SETTING_TRACE_BEGIN;
246         setting_retm_if(NULL == data, "data is NULL");
247
248         SettingFontUG *ad = (SettingFontUG *)data;
249         Elm_Object_Item *item = NULL;
250         item = elm_genlist_item_append(ad->genlist, &(ad->itc_seperator), NULL, NULL,
251                                 ELM_GENLIST_ITEM_NONE, NULL, NULL);
252         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
253
254         char *font_type_name = NULL;
255         font_type_name = cur_font_get();
256
257         ad->font_type = setting_create_Gendial_exp_parent_field(ad->genlist,
258                                                     &(ad->itc_2text_3_parent),
259                                                     NULL, NULL,
260                                                     SWALLOW_Type_INVALID,
261                                                     SETTING_FONT_TYPE_STR,
262                                                     font_type_name);
263         if (ad->font_type) {
264                 ad->font_type->userdata = ad;
265         } else {
266                 SETTING_TRACE_ERROR("ad->font_type is NULL");
267         }
268         G_FREE(ad->font_name);
269         ad->font_name = g_strdup(font_type_name);
270         G_FREE(font_type_name);
271
272         if (ad->viewmode == FONT_SEL_VIEW_APPCONTROL)
273                 return;
274         //--------------------------------------------------------------------
275
276         // [UI] Font Size
277         int value = -1;
278         int err = -1;
279         int ret = setting_get_int_slp_key(INT_SLP_SETTING_ACCESSIBILITY_FONT_SIZE, &value, &err);
280         if (ret != 0) {
281                 SETTING_TRACE_ERROR("get vconf failed");
282         }
283
284
285
286         ad->font_size = setting_create_Gendial_field_def(ad->genlist, &(ad->itc_2text_2),
287                                 __setting_font_main_genlist_sel_cb,
288                                 ad, SWALLOW_Type_INVALID, NULL,
289                                 NULL, value, SETTING_FONT_SIZE_STR,
290                                 __setting_font_main_get_font_size(value), NULL);
291         if (ad->font_size) {
292                 ad->font_size->userdata = ad;
293         } else {
294                 SETTING_TRACE_ERROR("ad->font_size is NULL");
295         }
296
297         setting_create_Gendial_field_def(ad->genlist, &(ad->itc_bg_1icon), NULL,
298                                          ad, SWALLOW_Type_LAYOUT_SPECIALIZTION_X,
299                                          NULL, NULL, 0, SETTING_FONT_SIZE_DESC, NULL,
300                                          NULL);
301 }
302
303
304 /**
305  * font change
306  */
307 static void __setting_font_main_font_set(char *font_name)
308 {
309         retm_if(font_name == NULL, "Invalid argument: font_name is NULL");
310         xmlDocPtr doc = (xmlDocPtr)font_conf_doc_parse(SETTING_FONT_CONF_FILE, font_name);
311
312         if(doc != NULL) {
313                 xmlSaveFormatFile(SETTING_FONT_CONF_FILE, doc, 0);
314                 xmlFreeDoc(doc);
315                 doc = NULL;
316         }
317
318         font_config_set(font_name);
319
320         /* notification */
321         Ecore_X_Window ecore_win = ecore_x_window_root_first_get();
322         Ecore_X_Atom atom = ecore_x_atom_get("FONT_TYPE_change");
323         ecore_x_window_prop_string_set(ecore_win, atom, "slp");
324 }
325
326 static void __setting_font_main_popup_cb(void *data, Evas_Object *obj, void *event_info)
327 {
328         SETTING_TRACE_BEGIN;
329         setting_retm_if(data == NULL, "Data parameter is NULL");
330         SettingFontUG *ad = (SettingFontUG *) data;
331
332         switch (btn_type(obj)) {
333         case POPUP_RESPONSE_OK:
334                 {
335                         ad->font_type->sub_desc = (char *)g_strdup(_(ad->subitem->keyStr));
336                         elm_object_item_data_set(ad->font_type->item, ad->font_type);
337                         elm_genlist_item_update(ad->font_type->item);
338
339                         ad->prev_font = ad->subitem->chk_status;
340                         G_FREE(ad->font_name);
341                         ad->font_name = (char *)g_strdup(_(ad->subitem->keyStr));
342                         SETTING_TRACE("ad->subitem->keyStr = %s", ad->subitem->keyStr);
343                         __setting_font_main_font_set(ad->subitem->keyStr);
344                         ug_destroy_me(ad->ug);
345                         break;
346                 }
347         case POPUP_RESPONSE_CANCEL:
348                 {
349                         elm_radio_value_set(ad->subitem->rgd, ad->prev_font);
350
351                         break;
352                 }
353         default:
354                 {
355                         break;
356                 }
357         }
358
359         if (ad->popup) {
360                 evas_object_del(ad->popup);
361                 ad->popup = NULL;
362         }
363 }
364
365 static void __setting_font_main_popup(void *data)
366 {
367         SETTING_TRACE_BEGIN;
368         setting_retm_if(data == NULL, "Data parameter is NULL");
369         SettingFontUG *ad = (SettingFontUG *) data;
370
371         char popup_desc[SETTING_STR_SLP_LEN + 1] = {0,};
372         snprintf(popup_desc, SETTING_STR_SLP_LEN, "%s %s?", _(SETTING_FONT_POPUP_DESC), ad->subitem->keyStr);
373
374         ad->popup = setting_create_popup_with_btn(ad, ad->win_get, NULL,
375                                          popup_desc,
376                                          __setting_font_main_popup_cb, 0,
377                                          2, _("IDS_COM_SK_YES"),_("IDS_COM_SK_NO"));
378 }
379
380
381 /**
382  * [UI][expandable list][event handler:selection][font change]
383  */
384 void __setting_font_main_sub_list_sel_cb(void *data, Evas_Object *obj, void *event_info)
385 {
386         SETTING_TRACE_BEGIN;
387         /* error check */
388         retm_if(event_info == NULL, "Invalid argument: event info is NULL");
389         Elm_Object_Item *subitem = (Elm_Object_Item *) event_info;
390         elm_genlist_item_selected_set(subitem, 0);
391         Setting_GenGroupItem_Data *data_subItem = elm_object_item_data_get(subitem);
392         ret_if(NULL == data_subItem);
393
394         SettingFontUG *ad = (SettingFontUG *) data;
395
396         /*  if not change, return */
397         if (ad->prev_font == data_subItem->chk_status) {
398                 SETTING_TRACE("NOT CHANGED ");
399                 return;
400         }
401
402         ad->subitem = data_subItem;
403         elm_radio_value_set(ad->subitem->rgd, ad->subitem->chk_status);
404
405         __setting_font_main_popup(ad);
406 }
407
408 static void __setting_font_main_exp_cb(void *data, Evas_Object *obj, void *event_info)
409 {
410         ret_if(NULL == data || NULL == event_info);
411         SETTING_TRACE_BEGIN;
412         SettingFontUG *ad = (SettingFontUG *) data;
413         Elm_Object_Item *parentItem = event_info;       /* parent item */
414         Setting_GenGroupItem_Data *data_parentItem = elm_object_item_data_get(parentItem);      /* parent data */
415         Evas_Object *scroller = elm_object_item_widget_get(parentItem);
416
417         Evas_Object *rgd = NULL;
418         if (data_parentItem == ad->font_type) {
419                 rgd = elm_radio_add(scroller);
420                 elm_radio_value_set(rgd, -1);
421
422                 Eina_List *font_list = NULL;
423                 Eina_List *l = NULL;
424                 FcChar8 *font_data = NULL;
425                 font_list = __setting_font_main_available_list_get();
426
427                 int i = 0;
428                 int matched_font = -1;
429                 Setting_GenGroupItem_Data *item = NULL;
430                 EINA_LIST_FOREACH(font_list, l, font_data)
431                 {
432                         item = setting_create_Gendial_exp_sub_field(scroller, &(ad->itc_1icon_1text_sub),
433                                                      __setting_font_main_sub_list_sel_cb, ad,
434                                                      parentItem, SWALLOW_Type_1RADIO, rgd,
435                                                      i, (const char*)font_data,
436                                                      NULL);
437
438                         if (item) {
439                                 item->userdata = ad;
440                                 if (!safeStrCmp(ad->font_name, (const char*)font_data)) {
441                                         matched_font = i;
442                                 }
443                         }
444
445                         i++;
446                 }
447
448                 /* update check status */
449                 if (matched_font != -1) {
450                         elm_radio_value_set(rgd, matched_font);
451                 }
452
453                 ad->prev_font = matched_font;
454         }
455 }
456
457
458 /* ***************************************************
459  *
460  *basic func
461  *
462  ***************************************************/
463 static int setting_font_main_create(void *cb)
464 {
465         SETTING_TRACE_BEGIN;
466         /* error check */
467         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
468
469         SettingFontUG *ad = (SettingFontUG *) cb;
470
471         retvm_if(ad->win_main_layout == NULL, SETTING_DRAW_ERR_FAIL_LOAD_EDJ,
472                  "win_main_layout is NULL");
473
474         ad->ly_main =
475             setting_create_layout_navi_bar_genlist(ad->win_main_layout,
476                                                    ad->win_get,
477                                                    _(KeyStr_Font),
478                                                    _("IDS_COM_BODY_BACK"),
479                                                    NULL,
480                                                    __setting_font_main_click_softkey_back_cb,
481                                                    NULL,
482                                                    ad, &ad->genlist, &ad->navibar);
483
484         setting_enable_expandable_genlist(ad->genlist, ad,
485                                 __setting_font_main_exp_cb, NULL);
486
487         __setting_font_main_genlist_item_create(ad);
488
489         setting_view_font_main.is_create = 1;
490         return SETTING_RETURN_SUCCESS;
491 }
492
493 static int setting_font_main_destroy(void *cb)
494 {
495         /* error check */
496         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
497
498         SettingFontUG *ad = (SettingFontUG *) cb;
499
500         if (ad->popup) {
501                 evas_object_del(ad->popup);
502                 ad->popup = NULL;
503         }
504
505         if (ad->ly_main != NULL) {
506                 evas_object_del(ad->ly_main);
507                 setting_view_font_main.is_create = 0;
508         }
509
510         return SETTING_RETURN_SUCCESS;
511 }
512
513 static int setting_font_main_update(void *cb)
514 {
515         SETTING_TRACE_BEGIN;
516         /* error check */
517         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
518
519         SettingFontUG *ad = (SettingFontUG *) cb;
520
521         if (ad->ly_main != NULL) {
522                 evas_object_show(ad->ly_main);
523
524                 setting_font_update_vconf_key(ad, SETTING_VCONF_INT_TYPE,
525                                               INT_SLP_SETTING_LCD_TIMEOUT_NORMAL);
526         }
527
528         return SETTING_RETURN_SUCCESS;
529 }
530
531 static int setting_font_main_cleanup(void *cb)
532 {
533         /* error check */
534         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
535
536         SettingFontUG *ad = (SettingFontUG *) cb;
537
538         if (ad->ly_main != NULL) {
539                 evas_object_hide(ad->ly_main);
540         }
541
542         return SETTING_RETURN_SUCCESS;
543 }
544
545 /* ***************************************************
546  *
547  *call back func
548  *
549  ***************************************************/
550
551 static void __setting_font_main_click_softkey_back_cb(void *data, Evas_Object *obj,
552                                         void *event_info)
553 {
554         SETTING_TRACE_BEGIN;
555         /* error check */
556         ret_if(data == NULL);
557
558         SettingFontUG *ad = (SettingFontUG *) data;
559
560         if (ad->viewmode == FONT_SEL_VIEW_APPCONTROL)
561         {
562                 service_h svc;
563                 if (service_create(&svc))
564                         return;
565
566                 service_add_extra_data(svc, "category", "FontType");
567                 service_add_extra_data(svc, "FontType", ad->font_name);
568
569                 SETTING_TRACE(" SERVICE_ADD_EXTRA : %s %s","category", "FontType");
570                 SETTING_TRACE(" SERVICE_ADD_EXTRA : %s %s","FontType", ad->font_name);
571
572
573                 ug_send_result(ad->ug, svc);
574                 service_destroy(svc);
575         }
576
577         /* Send destroy request */
578         ug_destroy_me(ad->ug);
579 }