Remove dependency libsystem-settings-util.so
[platform/core/api/system-settings.git] / system-settings-util / src / system_settings_util.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include <time.h>
6
7 #include <regex.h>
8
9 #include <dlog.h>
10 #include <vconf.h>
11
12 #include <glib.h>
13 #include <libxml/xmlmemory.h>
14 #include <libxml/parser.h>
15
16 #include <fontconfig/fontconfig.h>
17
18 #include <Elementary.h>
19 #include <Evas.h>
20 #include <Ecore_Evas.h>
21
22 #include <pkgmgr-info.h>
23
24 #include <system_settings.h>
25 #include <system_settings_private.h>
26
27 #include <tzplatform_config.h>
28 #include <system_settings_util.h>
29 #include <system_settings_font.h>
30
31 #ifdef TIZEN_WEARABLE
32 #define SMALL_FONT_DPI                                          (-90)
33 #endif
34 #ifdef TIZEN_MOBILE
35 #define SMALL_FONT_DPI                                          (-80)
36 #endif
37 #define MIDDLE_FONT_DPI                                         (-100)
38 #ifdef TIZEN_WEARABLE
39 #define LARGE_FONT_DPI                                          (-110)
40 #endif
41 #ifdef TIZEN_MOBILE
42 #define LARGE_FONT_DPI                                          (-150)
43 #endif
44 #define HUGE_FONT_DPI                                           (-190)
45 #define GIANT_FONT_DPI                                          (-250)
46
47 #define SETTING_FONT_PRELOAD_FONT_PATH _TZ_SYS_RO_SHARE"/fonts"
48 #define SETTING_FONT_DOWNLOADED_FONT_PATH _TZ_SYS_SHARE"/fonts"
49
50 #define SETTING_FONT_CONF_FILE _TZ_SYS_ETC"/fonts/conf.avail/99-tizen.conf"
51
52 #define SETTING_FONT_TIZEN_FONT_ALIAS "Tizen"
53 #define SETTING_FONT_TIZEN_DEFAULT_FONT_ALIAS "TizenDefaultFont"
54
55 #define SETTING_FONT_CONFIG_SO_PATH "/usr/lib/libfontconfig.so.1"
56 #define SETTING_ECORE_EVAS_SO_PATH "/usr/lib/libecore_evas.so.1"
57 #define SETTING_EFL_EXTENSION_SO_PATH "/usr/lib/libefl-extension.so.0"
58 #define SETTING_EVAS_SO_PATH "/usr/lib/libevas.so.1"
59
60 static int __font_size_get();
61
62 void *d_font_handle = NULL;
63 void *d_ecore_evas_handle = NULL;
64 void *d_evas_handle = NULL;
65 void *d_efl_eext_handle = NULL;
66
67
68
69 #define DYM_FUNC_LOADING(error, handle, pfunc, func_name) do { \
70                 pfunc = (void*)dlsym(handle, func_name); \
71                 if ((error = dlerror()) != NULL) { \
72                         SETTING_TRACE("ERROR!! canNOT find %s function at %s", func_name, #handle); \
73                         if (handle) \
74                                 dlclose(handle); \
75                         return false; \
76                 } \
77         } while (0)
78
79 #define DYM_CLOSE_HANDLE(handle) do { \
80                 if (handle) { \
81                         dlclose(handle); \
82                         handle = NULL; \
83                 } \
84         } while (0)
85
86
87
88
89 int loading_dym_font()
90 {
91         char *error = NULL;
92
93         if (!d_font_handle) {
94         d_font_handle = dlopen(SETTING_FONT_CONFIG_SO_PATH,  RTLD_LAZY);
95                 if (!d_font_handle) {
96                         SETTING_TRACE("ERROR!! canNOT find "SETTING_FONT_CONFIG_SO_PATH);
97                         return false;
98                 }
99         }
100
101         DYM_FUNC_LOADING(error, d_font_handle, d_FcInitLoadConfigAndFonts, "FcInitLoadConfigAndFonts");
102         DYM_FUNC_LOADING(error, d_font_handle, d_FcPatternBuild, "FcPatternBuild");
103         DYM_FUNC_LOADING(error, d_font_handle, d_FcConfigDestroy, "FcConfigDestroy");
104         DYM_FUNC_LOADING(error, d_font_handle, d_FcConfigSubstitute, "FcConfigSubstitute");
105         DYM_FUNC_LOADING(error, d_font_handle, d_FcDefaultSubstitute, "FcDefaultSubstitute");
106         DYM_FUNC_LOADING(error, d_font_handle, d_FcFontSort, "FcFontSort");
107         DYM_FUNC_LOADING(error, d_font_handle, d_FcPatternGetString, "FcPatternGetString");
108         DYM_FUNC_LOADING(error, d_font_handle, d_FcFontSetDestroy, "FcFontSetDestroy");
109         DYM_FUNC_LOADING(error, d_font_handle, d_FcPatternDestroy, "FcPatternDestroy");
110         DYM_FUNC_LOADING(error, d_font_handle, d_FcPatternCreate, "FcPatternCreate");
111         DYM_FUNC_LOADING(error, d_font_handle, d_FcObjectSetBuild, "FcObjectSetBuild");
112         DYM_FUNC_LOADING(error, d_font_handle, d_FcFontList, "FcFontList");
113         DYM_FUNC_LOADING(error, d_font_handle, d_FcObjectSetDestroy, "FcObjectSetDestroy");
114
115         return true;
116
117 }
118
119 int loading_dym_efl()
120 {
121         char *error = NULL;
122
123         if (!d_ecore_evas_handle) {
124         d_ecore_evas_handle = dlopen(SETTING_ECORE_EVAS_SO_PATH,  RTLD_LAZY);
125                 if (!d_ecore_evas_handle) {
126                         SETTING_TRACE("ERROR!! canNOT find"SETTING_ECORE_EVAS_SO_PATH);
127                         return false;
128                 }
129         }
130
131         if (!d_evas_handle) {
132         d_evas_handle = dlopen(SETTING_EVAS_SO_PATH,  RTLD_LAZY);
133                 if (!d_evas_handle) {
134                         SETTING_TRACE("ERROR!! canNOT find"SETTING_EVAS_SO_PATH);
135                         return false;
136                 }
137         }
138
139         DYM_FUNC_LOADING(error, d_evas_handle, d_evas_init, "evas_init");
140         DYM_FUNC_LOADING(error, d_evas_handle, d_evas_object_image_add, "evas_object_image_add");
141         DYM_FUNC_LOADING(error, d_evas_handle, d_evas_object_image_file_set, "evas_object_image_file_set");
142         DYM_FUNC_LOADING(error, d_evas_handle, d_evas_object_image_load_error_get, "evas_object_image_load_error_get");
143         DYM_FUNC_LOADING(error, d_evas_handle, d_evas_shutdown, "evas_shutdown");
144
145         DYM_FUNC_LOADING(error, d_ecore_evas_handle, d_ecore_evas_new, "ecore_evas_new");
146         DYM_FUNC_LOADING(error, d_ecore_evas_handle, d_ecore_evas_get, "ecore_evas_get");
147         DYM_FUNC_LOADING(error, d_ecore_evas_handle, d_ecore_evas_free, "ecore_evas_free");
148
149         return true;
150 }
151
152 void close_dym_efl()
153 {
154         DYM_CLOSE_HANDLE(d_evas_handle);
155         DYM_CLOSE_HANDLE(d_ecore_evas_handle);
156 }
157
158 int loading_dym_efl_eext()
159 {
160         char *error = NULL;
161
162         if (!d_efl_eext_handle) {
163                 d_efl_eext_handle = dlopen(SETTING_EFL_EXTENSION_SO_PATH, RTLD_LAZY);
164                 if (!d_efl_eext_handle) {
165                         SETTING_TRACE("ERROR!! canNOT find "SETTING_EFL_EXTENSION_SO_PATH);
166                         return false;
167                 }
168         }
169
170
171         DYM_FUNC_LOADING(error, d_efl_eext_handle, d_eext_config_font_set, "eext_config_font_set");
172
173         return true;
174 }
175
176 /* Returned family name should be free'd manually. */
177 char *__get_main_font_family_name_by_alias(char *alias)
178 {
179         SETTING_TRACE_BEGIN;
180         FcFontSet *set = NULL;
181         FcPattern *pat = NULL;
182         FcConfig *font_config = NULL;
183         FcChar8 *family = NULL;
184         char *ret = NULL;
185         FcResult res = 0;
186
187         if (!loading_dym_font())
188                 return NULL;
189
190         font_config = d_FcInitLoadConfigAndFonts();
191         if (font_config == NULL) {
192                 DYM_CLOSE_HANDLE(d_font_handle);
193                 return ret;
194         }
195
196         pat = d_FcPatternBuild(0, FC_FAMILY, FcTypeString, alias, (char *)0);
197
198         if (pat == NULL) {
199                 if (font_config != NULL) {
200                         d_FcConfigDestroy(font_config);
201                         font_config = NULL;
202                 }
203                 DYM_CLOSE_HANDLE(d_font_handle);
204                 return ret;
205         }
206
207         d_FcConfigSubstitute(font_config, pat, FcMatchPattern);
208         d_FcDefaultSubstitute(pat);
209
210         /* do matching */
211         set = d_FcFontSort(font_config, pat, FcTrue, NULL, &res);
212
213         if (set != NULL && (set->nfont > 0)) {
214                 d_FcPatternGetString(set->fonts[0], FC_FAMILY, 0, &family);
215                 ret = g_strdup((char *)family);
216
217                 d_FcFontSetDestroy(set);
218                 set = NULL;
219         }
220
221         if (set != NULL) {
222                 d_FcFontSetDestroy(set);
223                 set = NULL;
224         }
225
226         if (pat != NULL) {
227                 d_FcPatternDestroy(pat);
228                 pat = NULL;
229         }
230
231         if (font_config != NULL) {
232                 d_FcConfigDestroy(font_config);
233                 font_config = NULL;
234         }
235         DYM_CLOSE_HANDLE(d_font_handle);
236         return ret;
237 }
238
239 /*  LCOV_EXCL_START */
240 bool __is_supported_image_type_load(char *path)
241 {
242         SETTING_TRACE_BEGIN;
243         loading_dym_efl();
244
245         Ecore_Evas      *ee;
246         Evas            *evas;
247
248         if (!d_evas_init()) {
249                 close_dym_efl();
250                 return false;
251         }
252
253         ee = d_ecore_evas_new(NULL, 0, 0, 100, 100, NULL);
254         evas = d_ecore_evas_get(ee);
255
256         Evas_Object *img = d_evas_object_image_add(evas);
257         d_evas_object_image_file_set(img, path, NULL);
258         Evas_Load_Error ret = d_evas_object_image_load_error_get(img);
259
260         bool result = false;
261         if (ret == EVAS_LOAD_ERROR_NONE) {
262                 SETTING_TRACE("%s - OK", path);
263                 result = true;
264         } else {
265                 SETTING_TRACE("%s - NO", path);
266                 result = false;
267         }
268         d_ecore_evas_free(ee);
269         d_evas_shutdown();
270         close_dym_efl();
271         return result;
272 }
273 /*  LCOV_EXCL_STOP */
274
275 /*  LCOV_EXCL_START */
276 void font_config_set_notification()
277 {
278         SETTING_TRACE_BEGIN;
279 #if 0
280         /* notification */
281         Ecore_X_Window ecore_win = ecore_x_window_root_first_get();
282         Ecore_X_Atom atom = ecore_x_atom_get("FONT_TYPE_change");
283         ecore_x_window_prop_string_set(ecore_win, atom, "tizen");
284 #endif
285 }
286 /*  LCOV_EXCL_STOP */
287
288 /*  LCOV_EXCL_START */
289 int __is_available_font(char *font_name)
290 {
291         SETTING_TRACE_BEGIN;
292         FcObjectSet *os = NULL;
293         FcFontSet *fs = NULL;
294         FcPattern *pat = NULL;
295         FcConfig *font_config = NULL;
296         int ret = 0;
297
298         if (!loading_dym_font())
299                 return -1;
300
301         if (font_name == NULL) {
302                 DYM_CLOSE_HANDLE(d_font_handle);
303                 return -1;
304         }
305
306         font_config = d_FcInitLoadConfigAndFonts();
307
308         /*setting_retvm_if(font_config == NULL, NULL, "Failed: FcInitLoadConfigAndFonts"); */
309
310         char *locale = setlocale(0, NULL);
311
312         pat = d_FcPatternCreate();
313
314         os = d_FcObjectSetBuild(FC_FAMILY, FC_FILE, FC_FAMILYLANG, (char *) 0);
315
316         if (os) {
317                 fs = d_FcFontList(font_config, pat, os);
318                 d_FcObjectSetDestroy(os);
319                 os = NULL;
320         }
321
322         if (pat) {
323                 d_FcPatternDestroy(pat);
324                 pat = NULL;
325         }
326
327         if (fs) {
328                 int j;
329                 SETTING_TRACE("fs->nfont = %d", fs->nfont);
330
331                 for (j = 0; j < fs->nfont; j++) {
332                         FcChar8 *family = NULL;
333                         FcChar8 *file = NULL;
334
335                         if (d_FcPatternGetString(fs->fonts[j], FC_FILE, 0, &file) == FcResultMatch) {
336                                 int preload_path_len = strlen(SETTING_FONT_PRELOAD_FONT_PATH);
337                                 int download_path_len = strlen(SETTING_FONT_DOWNLOADED_FONT_PATH);
338
339                                 if (file && (!strncmp((const char *)file, SETTING_FONT_PRELOAD_FONT_PATH, preload_path_len)
340                                                 || !strncmp((const char *)file, SETTING_FONT_DOWNLOADED_FONT_PATH, download_path_len))) {
341                                         char *family_result = NULL;
342                                         FcChar8 *lang = NULL;
343                                         int id = 0;
344                                         if (d_FcPatternGetString(fs->fonts[j], FC_FAMILY, id, &family) != FcResultMatch) {
345                                                 break;
346                                         }
347                                         if (d_FcPatternGetString(fs->fonts[j], FC_FAMILYLANG, id, &lang) != FcResultMatch) {
348                                                 break;
349                                         }
350                                         family_result = (char *)family;
351
352                                         /* Find proper family name for current locale. */
353                                         while (locale && family && lang) {
354                                                 if (!strncmp(locale, (char *)lang, strlen((char *)lang))) {
355                                                         family_result = (char *)family;
356                                                         break;
357                                                 }
358
359                                                 /* I will set english as default family language. */
360                                                 /* If there is no proper family language for current locale, */
361                                                 /* we have to show the english family name. */
362                                                 if (!strcmp((char *)lang, (char *)"en")) {
363                                                         family_result = (char *)family;
364                                                 }
365                                                 id++;
366                                                 if (d_FcPatternGetString(fs->fonts[j], FC_FAMILY, id, &family) != FcResultMatch) {
367                                                         break;
368                                                 }
369                                                 if (d_FcPatternGetString(fs->fonts[j], FC_FAMILYLANG, id, &lang) != FcResultMatch) {
370                                                         break;
371                                                 }
372                                         }
373
374                                         if (family_result) {
375                                                 SETTING_TRACE("-------- FONT - family_result = %s", (char *)family_result);
376                                                 if (strcmp(family_result, font_name) == 0) {
377                                                         ret = 1;
378                                                         break;
379                                                 }
380                                         }
381                                 }
382                         }
383                 }
384                 d_FcFontSetDestroy(fs);
385                 fs = NULL;
386         }
387         d_FcConfigDestroy(font_config);
388         font_config = NULL;
389         DYM_CLOSE_HANDLE(d_font_handle);
390         return ret;
391 }
392 /*  LCOV_EXCL_STOP */
393
394
395 char *_get_default_font()
396 {
397         return __get_main_font_family_name_by_alias(SETTING_FONT_TIZEN_DEFAULT_FONT_ALIAS);
398 }
399
400
401 /*  LCOV_EXCL_START */
402 bool font_config_set(char *font_name)
403 {
404         SETTING_TRACE_BEGIN;
405         if (!loading_dym_efl_eext())
406                 return false;
407         int font_size = __font_size_get();
408         bool ret = d_eext_config_font_set(font_name, font_size);
409         DYM_CLOSE_HANDLE(d_efl_eext_handle);
410         return ret;
411 }
412 /*  LCOV_EXCL_STOP */
413
414 /*  LCOV_EXCL_START */
415 void __font_size_set()
416 {
417         SETTING_TRACE_BEGIN;
418         if (!loading_dym_efl_eext())
419                 return;
420         int font_size = __font_size_get();
421         char *font_name = NULL;
422         font_name = vconf_get_str(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_NAME);
423
424         d_eext_config_font_set(font_name, font_size);
425
426         DYM_CLOSE_HANDLE(d_efl_eext_handle);
427         g_free(font_name);
428 }
429 /*  LCOV_EXCL_STOP */
430
431 /*  LCOV_EXCL_START */
432 static int __font_size_get()
433 {
434         SETTING_TRACE_BEGIN;
435         int font_size = -1;
436
437         int vconf_value = -1;
438         if (vconf_get_int(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, &vconf_value)) {
439                 return -1;
440         }
441
442         switch (vconf_value) {
443         case SYSTEM_SETTINGS_FONT_SIZE_SMALL:
444                 font_size = SMALL_FONT_DPI;
445                 break;
446         case SYSTEM_SETTINGS_FONT_SIZE_NORMAL:
447                 font_size = MIDDLE_FONT_DPI;
448                 break;
449         case SYSTEM_SETTINGS_FONT_SIZE_LARGE:
450                 font_size = LARGE_FONT_DPI;
451                 break;
452         case SYSTEM_SETTINGS_FONT_SIZE_HUGE:
453                 font_size = HUGE_FONT_DPI;
454                 break;
455         case SYSTEM_SETTINGS_FONT_SIZE_GIANT:
456                 font_size = GIANT_FONT_DPI;
457                 break;
458         default:
459                 font_size = MIDDLE_FONT_DPI;
460                 break;
461         }
462         return font_size;
463 }
464 /*  LCOV_EXCL_STOP */