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