change to get font type API with reading vconf directly.
[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 <string.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <sys/un.h>
11 #include <sys/socket.h>
12 #include <systemd/sd-login.h>
13 #include <fcntl.h>
14
15 #define EFL_CONFIG_SOCK_PATH "/run/user_ext/%d"
16 #define EFL_CONFIG_SOCKET_FILE ".efl-config.sock"
17
18 #include <regex.h>
19
20 #include <dlog.h>
21 #include <vconf.h>
22
23 #include <glib.h>
24 #include <libxml/xmlmemory.h>
25 #include <libxml/parser.h>
26
27 #include <fontconfig/fontconfig.h>
28
29 #include <Elementary.h>
30 #include <Evas.h>
31 #include <Ecore_Evas.h>
32
33 #include <pkgmgr-info.h>
34
35 #include <system_settings.h>
36 #include <system_settings_private.h>
37
38 #include <tzplatform_config.h>
39 #include <system_settings_util.h>
40
41 #ifdef TIZEN_WEARABLE
42 #define SMALL_FONT_DPI                                          (-90)
43 #endif
44 #ifdef TIZEN_MOBILE
45 #define SMALL_FONT_DPI                                          (-80)
46 #endif
47 #define MIDDLE_FONT_DPI                                         (-100)
48 #ifdef TIZEN_WEARABLE
49 #define LARGE_FONT_DPI                                          (-110)
50 #endif
51 #ifdef TIZEN_MOBILE
52 #define LARGE_FONT_DPI                                          (-150)
53 #endif
54 #define HUGE_FONT_DPI                                           (-190)
55 #define GIANT_FONT_DPI                                          (-250)
56
57 #define SETTING_FONT_PRELOAD_FONT_PATH _TZ_SYS_RO_SHARE"/fonts"
58 #define SETTING_FONT_DOWNLOADED_FONT_PATH _TZ_SYS_SHARE"/fonts"
59
60 #define SETTING_FONT_CONF_FILE _TZ_SYS_ETC"/fonts/conf.avail/99-tizen.conf"
61
62 #define SETTING_FONT_TIZEN_FONT_ALIAS "Tizen"
63 #define SETTING_FONT_TIZEN_DEFAULT_FONT_ALIAS "TizenDefaultFont"
64
65 static int __font_size_get();
66
67 /* Returned family name should be free'd manually. */
68 char *__get_main_font_family_name_by_alias(char *alias)
69 {
70         SETTING_TRACE_BEGIN;
71         FcFontSet *set = NULL;
72         FcPattern *pat = NULL;
73         FcConfig *font_config = NULL;
74         FcChar8 *family = NULL;
75         char *ret = NULL;
76         FcResult res = 0;
77
78         font_config = FcInitLoadConfigAndFonts();
79         if (font_config == NULL)
80                 return ret;
81
82         pat = FcPatternBuild(0, FC_FAMILY, FcTypeString, alias, (char *)0);
83
84         if (pat == NULL) {
85                 if (font_config != NULL) {
86                         FcConfigDestroy(font_config);
87                         font_config = NULL;
88                 }
89                 return ret;
90         }
91
92         FcConfigSubstitute(font_config, pat, FcMatchPattern);
93         FcDefaultSubstitute(pat);
94
95         /* do matching */
96         set = FcFontSort(font_config, pat, FcTrue, NULL, &res);
97
98         if (set != NULL && (set->nfont > 0)) {
99                 FcPatternGetString(set->fonts[0], FC_FAMILY, 0, &family);
100                 ret = g_strdup((char *)family);
101
102                 FcFontSetDestroy(set);
103                 set = NULL;
104         }
105
106         if (set != NULL) {
107                 FcFontSetDestroy(set);
108                 set = NULL;
109         }
110
111         if (pat != NULL) {
112                 FcPatternDestroy(pat);
113                 pat = NULL;
114         }
115
116         if (font_config != NULL) {
117                 FcConfigDestroy(font_config);
118                 font_config = NULL;
119         }
120
121         return ret;
122 }
123
124 /*  LCOV_EXCL_START */
125 bool __is_supported_image_type_load(char *path)
126 {
127         SETTING_TRACE_BEGIN;
128         evas_init();
129         Ecore_Evas      *ee;
130         Evas            *evas;
131
132         ee = ecore_evas_new(NULL, 0, 0, 100, 100, NULL);
133         evas = ecore_evas_get(ee);
134
135         Evas_Object *img = evas_object_image_add(evas);
136         evas_object_image_file_set(img, path, NULL);
137         Evas_Load_Error ret = evas_object_image_load_error_get(img);
138
139         bool result = false;
140         if (ret == EVAS_LOAD_ERROR_NONE) {
141                 SETTING_TRACE("%s - OK", path);
142                 result = true;
143         } else {
144                 SETTING_TRACE("%s - NO", path);
145                 result = false;
146         }
147         ecore_evas_free(ee);
148         evas_shutdown();
149         return result;
150 }
151 /*  LCOV_EXCL_STOP */
152
153 char *_get_cur_font()
154 {
155         return __get_main_font_family_name_by_alias(SETTING_FONT_TIZEN_FONT_ALIAS);
156 }
157
158 /*  LCOV_EXCL_START */
159 void font_config_set_notification()
160 {
161         SETTING_TRACE_BEGIN;
162 #if 0
163         /* notification */
164         Ecore_X_Window ecore_win = ecore_x_window_root_first_get();
165         Ecore_X_Atom atom = ecore_x_atom_get("FONT_TYPE_change");
166         ecore_x_window_prop_string_set(ecore_win, atom, "tizen");
167 #endif
168 }
169 /*  LCOV_EXCL_STOP */
170
171 /*  LCOV_EXCL_START */
172 int __is_available_font(char *font_name)
173 {
174         SETTING_TRACE_BEGIN;
175         FcObjectSet *os = NULL;
176         FcFontSet *fs = NULL;
177         FcPattern *pat = NULL;
178         FcConfig *font_config = NULL;
179
180         int ret = 0;
181
182         if (font_name == NULL)
183                 return -1;
184
185         font_config = FcInitLoadConfigAndFonts();
186
187         /*setting_retvm_if(font_config == NULL, NULL, "Failed: FcInitLoadConfigAndFonts"); */
188
189         char *locale = setlocale(0, NULL);
190
191         pat = FcPatternCreate();
192
193         os = FcObjectSetBuild(FC_FAMILY, FC_FILE, FC_FAMILYLANG, (char *) 0);
194
195         if (os) {
196                 fs = FcFontList(font_config, pat, os);
197                 FcObjectSetDestroy(os);
198                 os = NULL;
199         }
200
201         if (pat) {
202                 FcPatternDestroy(pat);
203                 pat = NULL;
204         }
205
206         if (fs) {
207                 int j;
208                 SETTING_TRACE("fs->nfont = %d", fs->nfont);
209
210                 for (j = 0; j < fs->nfont; j++) {
211                         FcChar8 *family = NULL;
212                         FcChar8 *file = NULL;
213
214                         if (FcPatternGetString(fs->fonts[j], FC_FILE, 0, &file) == FcResultMatch) {
215                                 int preload_path_len = strlen(SETTING_FONT_PRELOAD_FONT_PATH);
216                                 int download_path_len = strlen(SETTING_FONT_DOWNLOADED_FONT_PATH);
217
218                                 if (file && (!strncmp((const char *)file, SETTING_FONT_PRELOAD_FONT_PATH, preload_path_len)
219                                                 || !strncmp((const char *)file, SETTING_FONT_DOWNLOADED_FONT_PATH, download_path_len))) {
220                                         char *family_result = NULL;
221                                         FcChar8 *lang = NULL;
222                                         int id = 0;
223                                         if (FcPatternGetString(fs->fonts[j], FC_FAMILY, id, &family) != FcResultMatch) {
224                                                 break;
225                                         }
226                                         if (FcPatternGetString(fs->fonts[j], FC_FAMILYLANG, id, &lang) != FcResultMatch) {
227                                                 break;
228                                         }
229                                         family_result = (char *)family;
230
231                                         /* Find proper family name for current locale. */
232                                         while (locale && family && lang) {
233                                                 if (!strncmp(locale, (char *)lang, strlen((char *)lang))) {
234                                                         family_result = (char *)family;
235                                                         break;
236                                                 }
237
238                                                 /* I will set english as default family language. */
239                                                 /* If there is no proper family language for current locale, */
240                                                 /* we have to show the english family name. */
241                                                 if (!strcmp((char *)lang, (char *)"en")) {
242                                                         family_result = (char *)family;
243                                                 }
244                                                 id++;
245                                                 if (FcPatternGetString(fs->fonts[j], FC_FAMILY, id, &family) != FcResultMatch) {
246                                                         break;
247                                                 }
248                                                 if (FcPatternGetString(fs->fonts[j], FC_FAMILYLANG, id, &lang) != FcResultMatch) {
249                                                         break;
250                                                 }
251                                         }
252
253                                         if (family_result) {
254                                                 SETTING_TRACE("-------- FONT - family_result = %s", (char *)family_result);
255                                                 if (strcmp(family_result, font_name) == 0) {
256                                                         ret = 1;
257                                                         break;
258                                                 }
259                                         }
260                                 }
261                         }
262                 }
263                 FcFontSetDestroy(fs);
264                 fs = NULL;
265         }
266         FcConfigDestroy(font_config);
267         font_config = NULL;
268         return ret;
269 }
270 /*  LCOV_EXCL_STOP */
271
272
273 char *_get_default_font()
274 {
275         return __get_main_font_family_name_by_alias(SETTING_FONT_TIZEN_DEFAULT_FONT_ALIAS);
276 }
277
278 bool _is_in_system_session()
279 {
280         SETTING_TRACE_BEGIN;
281         int ret = 0;
282         char *slice = NULL;
283
284         ret = sd_pid_get_slice(getpid(), &slice);
285         if (0 <= ret && slice) {
286                 if (0 == strncmp(slice, "system.slice", strlen("system.slice"))) {
287                         free(slice);
288                         return true;
289                 }
290         } else {
291                 SETTING_TRACE("sd_pid_get_slice() Fail(%d)", ret);
292         }
293
294         free(slice);
295         return false;
296 }
297
298 bool _get_active_uid(uid_t *uid)
299 {
300         SETTING_TRACE_BEGIN;
301         int active_user_count = 0;
302         uid_t *active_user_list = NULL;
303
304         active_user_count = sd_get_active_uids(&active_user_list);
305         /* the number of active users is 1 in tizen3.0 */
306         if (1 == active_user_count && active_user_list) {
307                 *uid = active_user_list[0];
308                 SETTING_TRACE("active uid = %d", *uid);
309         } else {
310                 SETTING_TRACE("sd_get_active_uids() Fail(%d)", active_user_count);
311                 free(active_user_list);
312                 return false;
313         }
314
315         return true;
316 }
317
318 bool
319 _efl_config_set(char *msg)
320 {
321         SETTING_TRACE_BEGIN;
322         char sock_file[1024] = {0};
323         uid_t active_uid = getuid();
324         bool ret = false;
325
326         if (_is_in_system_session() == true) {
327                 if (_get_active_uid(&active_uid) == false) {
328                         SETTING_TRACE("get active uid fail");
329                         return ret;
330                 }
331         }
332
333         snprintf(sock_file, sizeof(sock_file), EFL_CONFIG_SOCK_PATH"/%s",
334                         active_uid,
335                         EFL_CONFIG_SOCKET_FILE);
336
337         do {
338                 int r, size, flags;
339                 struct sockaddr_un server_addr;
340
341                 int fd = socket(PF_UNIX, SOCK_STREAM, 0);
342                 if (fd < 0) {
343                         SETTING_TRACE("socket[%d] error :%d", fd, errno);
344                         break;
345                 }
346
347                 flags = fcntl(fd, F_GETFL, 0);
348                 if (flags == -1)
349                         flags = 0;
350                 r = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
351                 if (0 != r)
352                         SETTING_TRACE("fcntl fail : %d", errno);
353
354                 bzero(&server_addr, sizeof(server_addr));
355                 server_addr.sun_family = AF_UNIX;
356                 snprintf(server_addr.sun_path, sizeof(server_addr.sun_path), "%s",
357                                 sock_file);
358
359                 r = connect(fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
360                 if (r != 0) {
361                         SETTING_TRACE("connect error : %d, %d", r, errno);
362                         break;
363                 }
364
365                 size = write(fd, msg, strlen(msg));
366                 if (size <= 0) {
367                         SETTING_TRACE("write fail : %d", errno);
368                 } else {
369                         ret = true;
370                 }
371                 close(fd);
372         } while (0);
373         return ret;
374 }
375
376 /*  LCOV_EXCL_START */
377 bool font_config_set(char *font_name)
378 {
379         SETTING_TRACE_BEGIN;
380         char buf[255] = { 0 };
381         int font_size = __font_size_get();
382         bool ret;
383
384         snprintf(buf, sizeof(buf), "font_name: %s, font_size: %d", font_name, font_size);
385         ret = _efl_config_set(buf);
386
387         return ret;
388 }
389 /*  LCOV_EXCL_STOP */
390
391 /*  LCOV_EXCL_START */
392 void __font_size_set()
393 {
394         SETTING_TRACE_BEGIN;
395         char buf[255] = { 0 };
396         int font_size = __font_size_get();
397         char *font_name = NULL;
398
399         font_name = vconf_get_str(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_NAME);
400
401         snprintf(buf, sizeof(buf), "font_name: %s, font_size: %d", font_name, font_size);
402         _efl_config_set(buf);
403
404         g_free(font_name);
405 }
406 /*  LCOV_EXCL_STOP */
407
408 /*  LCOV_EXCL_START */
409 static int __font_size_get()
410 {
411         SETTING_TRACE_BEGIN;
412         int font_size = -1;
413
414         int vconf_value = -1;
415         if (vconf_get_int(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, &vconf_value)) {
416                 return -1;
417         }
418
419         switch (vconf_value) {
420         case SYSTEM_SETTINGS_FONT_SIZE_SMALL:
421                 font_size = SMALL_FONT_DPI;
422                 break;
423         case SYSTEM_SETTINGS_FONT_SIZE_NORMAL:
424                 font_size = MIDDLE_FONT_DPI;
425                 break;
426         case SYSTEM_SETTINGS_FONT_SIZE_LARGE:
427                 font_size = LARGE_FONT_DPI;
428                 break;
429         case SYSTEM_SETTINGS_FONT_SIZE_HUGE:
430                 font_size = HUGE_FONT_DPI;
431                 break;
432         case SYSTEM_SETTINGS_FONT_SIZE_GIANT:
433                 font_size = GIANT_FONT_DPI;
434                 break;
435         default:
436                 font_size = MIDDLE_FONT_DPI;
437                 break;
438         }
439         return font_size;
440 }
441 /*  LCOV_EXCL_STOP */