[refactoring] separate big files
[platform/core/api/system-settings.git] / src / sst_font.c
1 /*
2  * Copyright (c) 2011-2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "sst_font.h"
17
18 #include <dlfcn.h>
19 #include <libxml/tree.h>
20 #include <vconf.h>
21 #include "sst.h"
22
23 static int dl_is_available_font(char *str)
24 {
25         void *handle = NULL;
26         char *error;
27         int ret = false;
28         int (*check_available_font)(char *font_name);
29
30         handle = dlopen(SETTING_UTILS_SO_FILE_PATH, RTLD_LAZY);
31         if (!handle) {
32                 SST_SECURE_TRACE("ERROR!! canNOT find libsystem-settings-util.so");
33                 return false;
34         }
35
36         check_available_font = dlsym(handle, "__is_available_font");
37         if ((error = dlerror()) != NULL) {
38                 SST_SECURE_TRACE("ERROR!! canNOT find __is_available_font function at libsystem-settings-util.so");
39                 if (handle)
40                         dlclose(handle);
41                 return false;
42         }
43         ret = check_available_font(str);
44         if (handle)
45                 dlclose(handle);
46         return ret;
47 }
48
49 static void dl_font_size_set()
50 {
51         void *handle = NULL;
52         char *error;
53         void(*set_font_size)();
54
55         handle = dlopen(SETTING_UTILS_SO_FILE_PATH, RTLD_LAZY);
56         if (!handle) {
57                 SST_SECURE_TRACE("ERROR!! canNOT find libsystem-settings-util.so");
58                 return;
59         }
60
61         set_font_size = dlsym(handle, "__font_size_set");
62         if ((error = dlerror()) != NULL) {
63                 SST_SECURE_TRACE("ERROR!! canNOT find __font_size_set function at libsystem-settings-util.so");
64                 if (handle)
65                         dlclose(handle);
66                 return;
67         }
68         set_font_size();
69         if (handle)
70                 dlclose(handle);
71         return;
72 }
73
74 static void dl_font_config_set_notification()
75 {
76         void *handle = NULL;
77         char *error;
78         void (*set_font_nodification)();
79
80         handle = dlopen(SETTING_UTILS_SO_FILE_PATH, RTLD_LAZY);
81         if (!handle) {
82                 SST_SECURE_TRACE("ERROR!! canNOT find libsystem-settings-util.so");
83                 return;
84         }
85
86         set_font_nodification = dlsym(handle, "font_config_set_notification");
87         if ((error = dlerror()) != NULL) {
88                 SST_SECURE_TRACE("ERROR!! canNOT find font_config_set_notification function at libsystem-settings-util.so");
89                 if (handle)
90                         dlclose(handle);
91                 return;
92         }
93         set_font_nodification();
94         if (handle)
95                 dlclose(handle);
96         return;
97 }
98
99 static bool dl_font_config_set(char *font_name)
100 {
101         void *handle = NULL;
102         char *error;
103         bool ret = false;
104         bool (*check_font_type)(char *font_name);
105
106         handle = dlopen(SETTING_UTILS_SO_FILE_PATH, RTLD_LAZY);
107         if (!handle) {
108                 SST_SECURE_TRACE("ERROR!! canNOT find libsystem-settings-util.so");
109                 return false;
110         }
111
112         check_font_type = dlsym(handle, "font_config_set");
113         if ((error = dlerror()) != NULL) {
114                 SST_SECURE_TRACE("ERROR!! canNOT find font_config_set function at libsystem-settings-util.so");
115                 if (handle)
116                         dlclose(handle);
117                 return false;
118         }
119         ret = check_font_type(font_name);
120         if (handle)
121                 dlclose(handle);
122         return ret;
123 }
124
125 static char *dl_get_default_font_info()
126 {
127         void *handle = NULL;
128         char *error;
129         char *ret = NULL;
130         char *(*get_font_info)();
131
132         handle = dlopen(SETTING_UTILS_SO_FILE_PATH, RTLD_LAZY);
133         if (!handle) {
134                 SST_SECURE_TRACE("ERROR!! canNOT find libsystem-settings-util.so");
135                 return false;
136         }
137
138         get_font_info = dlsym(handle, "_get_default_font");
139
140         if ((error = dlerror()) != NULL) {
141                 SST_SECURE_TRACE("ERROR!! canNOT find _get_default_font function at libsystem-settings-util.so");
142                 if (handle)
143                         dlclose(handle);
144                 return false;
145         }
146         ret = get_font_info();
147         if (handle)
148                 dlclose(handle);
149         return ret;
150 }
151
152 int system_setting_set_font_size(system_setting_h item, void *value)
153 {
154         int *vconf_value;
155         vconf_value = *(int **)value;
156
157         if (*vconf_value < 0 || *vconf_value > SYSTEM_SETTINGS_FONT_SIZE_GIANT)
158                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
159
160         if (vconf_set_int(item->vconf_key, *vconf_value))
161                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
162
163         dl_font_size_set();
164         return SYSTEM_SETTINGS_ERROR_NONE;
165 }
166
167 static void* font_conf_doc_parse(char *doc_name, char *font_name)
168 {
169         xmlDocPtr doc = NULL;
170         xmlNodePtr cur = NULL;
171         xmlNodePtr cur2 = NULL;
172         xmlNodePtr cur3 = NULL;
173         xmlChar *key = NULL;
174
175         doc = xmlParseFile(doc_name);
176
177         cur = xmlDocGetRootElement(doc);
178
179         if (cur == NULL) {
180                 xmlFreeDoc(doc);
181                 doc = NULL;
182                 return NULL;
183         }
184
185         if (xmlStrcmp(cur->name, (const xmlChar*)"fontconfig")) {
186                 xmlFreeDoc(doc);
187                 doc = NULL;
188                 return NULL;
189         }
190
191         cur = cur->xmlChildrenNode;
192
193         bool is_changed = false;
194         while (cur != NULL) {
195                 if ((!xmlStrcmp(cur->name, (const xmlChar*)"match"))) {
196                         cur2 = cur->xmlChildrenNode;
197                         while (cur2 != NULL) {
198                                 if ((!xmlStrcmp(cur2->name, (const xmlChar*)"edit"))) {
199                                         xmlChar *name = xmlGetProp(cur2, (const xmlChar*)"name");
200                                         /* if name is not 'family', break */
201                                         if (xmlStrcmp(name, (const xmlChar*)"family")) {
202                                                 xmlFree(name);
203                                                 name = NULL;
204                                                 break;
205                                         }
206                                         xmlFree(name);
207                                         name = NULL;
208
209                                         cur3 = cur2->xmlChildrenNode;
210                                         while (cur3 != NULL) {
211                                                 if ((!xmlStrcmp(cur3->name, (const xmlChar*)"string"))) {
212                                                         xmlNodeSetContent(cur3->xmlChildrenNode, (const xmlChar*)font_name);
213                                                         key = xmlNodeListGetString(doc, cur3->xmlChildrenNode, 1);
214                                                         xmlFree(key);
215                                                         key = NULL;
216                                                         is_changed = true;
217                                                 }
218                                                 cur3 = cur3->next;
219                                         }
220                                 }
221                                 cur2 = cur2->next;
222                         }
223                 } else if ((!xmlStrcmp(cur->name, (const xmlChar*)"alias"))) {
224                         cur2 = cur->xmlChildrenNode;
225                         while (cur2 != NULL) {
226                                 if ((!xmlStrcmp(cur2->name, (const xmlChar*)"family"))) {
227                                         xmlNodeSetContent(cur2->xmlChildrenNode, (const xmlChar*)font_name);
228                                         key = xmlNodeListGetString(doc, cur2->xmlChildrenNode, 1);
229                                         xmlFree(key);
230                                         key = NULL;
231                                         is_changed = true;
232                                 } else if ((!xmlStrcmp(cur2->name, (const xmlChar*)"prefer"))) {
233                                         cur3 = cur2->xmlChildrenNode;
234                                         while (cur3 != NULL) {
235                                                 if ((!xmlStrcmp(cur3->name, (const xmlChar*)"family"))) {
236                                                         xmlNodeSetContent(cur3->xmlChildrenNode, (const xmlChar*)font_name);
237                                                         key = xmlNodeListGetString(doc, cur3->xmlChildrenNode, 1);
238                                                         xmlFree(key);
239                                                         key = NULL;
240                                                         is_changed = true;
241                                                         cur3 = cur3->next;
242                                                         return doc;
243                                                 }
244                                                 cur3 = cur3->next;
245                                         }
246                                 }
247                                 cur2 = cur2->next;
248                         }
249                 }
250                 cur = cur->next;
251         }
252
253         if (is_changed) {
254                 return doc;
255         } else {
256                 xmlFreeDoc(doc);
257                 doc = NULL;
258                 return NULL;
259         }
260 }
261
262 int system_setting_set_font_type(system_setting_h item, void *value)
263 {
264         char *font_name = NULL;
265         font_name = (char*)value;
266
267         /* get current font list */
268         int is_found = dl_is_available_font(font_name);
269
270         if (is_found == 1) {
271                 SST_SECURE_TRACE("found font : %s ", font_name);
272         } else {
273                 SST_SECURE_TRACE(" NOT found font : %s ", font_name);
274                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
275         }
276
277         bool bsave = dl_font_config_set(font_name);
278
279         if (!bsave) {
280                 SST_SECURE_TRACE(" font type save error by font_config_set() ");
281                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
282         } else {
283                 SST_SECURE_TRACE(" save OK - font_config_set() ");
284         }
285
286         xmlDocPtr doc = font_conf_doc_parse(SETTING_FONT_CONF_FILE, font_name);
287         if (doc != NULL) {
288                 xmlSaveFormatFile(SETTING_FONT_CONF_FILE, doc, 0);
289                 xmlFreeDoc(doc);
290                 doc = NULL;
291         }
292
293         dl_font_config_set_notification();
294
295         char *vconf_value;
296         vconf_value = (char*)value;
297
298         if (vconf_set_str(item->vconf_key, vconf_value))
299                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
300
301         return SYSTEM_SETTINGS_ERROR_NONE;
302 }
303
304 int system_setting_get_default_font_type(system_setting_h item, void **value)
305 {
306         char *font_name = dl_get_default_font_info();
307         if (font_name) {
308                 *value = (void*)font_name;
309                 return SYSTEM_SETTINGS_ERROR_NONE;
310         } else {
311                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
312         }
313 }