[refactoring] reduce length of value name and log
[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 const char* const sst_utils_library = SETTING_UTILS_SO_FILE_PATH;
24
25 static int dl_is_available_font(char *str)
26 {
27         void *handle = NULL;
28         char *error;
29         int ret = 0;
30         int (*check_available_font)(char *font_name);
31
32         handle = dlopen(sst_utils_library, RTLD_LAZY | RTLD_GLOBAL);
33         if (NULL == handle) {
34                 ERR("dlopen(%s) Fail", sst_utils_library);
35                 return 0;
36         }
37
38         check_available_font = dlsym(handle, "sstu_is_available_font");
39         if ((error = dlerror()) != NULL) {
40                 ERR("dlsym(sstu_is_available_font) Fail(%s)", error);
41                 dlclose(handle);
42                 return 0;
43         }
44         ret = check_available_font(str);
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(sst_utils_library, RTLD_LAZY | RTLD_GLOBAL);
56         if (NULL == handle) {
57                 ERR("dlopen(%s) Fail", sst_utils_library);
58                 return;
59         }
60
61         set_font_size = dlsym(handle, "sstu_set_font_size");
62         if ((error = dlerror()) != NULL) {
63                 ERR("dlsym(sstu_set_font_size) Fail(%s)", error);
64                 dlclose(handle);
65                 return;
66         }
67         set_font_size();
68         dlclose(handle);
69         return;
70 }
71
72 static void dl_font_config_set_notification()
73 {
74         void *handle = NULL;
75         char *error;
76         void (*set_font_nodification)();
77
78         handle = dlopen(sst_utils_library, RTLD_LAZY | RTLD_GLOBAL);
79         if (NULL == handle) {
80                 ERR("dlopen(%s) Fail", sst_utils_library);
81                 return;
82         }
83
84         set_font_nodification = dlsym(handle, "sstu_font_config_set_notification");
85         if ((error = dlerror()) != NULL) {
86                 ERR("dlsym(sstu_font_config_set_notification) Fail(%s)", error);
87                 dlclose(handle);
88                 return;
89         }
90         set_font_nodification();
91         dlclose(handle);
92         return;
93 }
94
95 static bool dl_font_config_set(char *font_name)
96 {
97         void *handle = NULL;
98         char *error;
99         bool ret = false;
100         bool (*check_font_type)(char *font_name);
101
102         handle = dlopen(sst_utils_library, RTLD_LAZY | RTLD_GLOBAL);
103         if (NULL == handle) {
104                 ERR("dlopen(%s) Fail", sst_utils_library);
105                 return false;
106         }
107
108         check_font_type = dlsym(handle, "sstu_set_font_config");
109         if ((error = dlerror()) != NULL) {
110                 ERR("dlsym(sstu_set_font_config) Fail(%s)", error);
111                 dlclose(handle);
112                 return false;
113         }
114         ret = check_font_type(font_name);
115         dlclose(handle);
116         return ret;
117 }
118
119 static char *dl_get_default_font_info()
120 {
121         void *handle = NULL;
122         char *error;
123         char *ret = NULL;
124         char *(*get_font_info)();
125
126         handle = dlopen(sst_utils_library, RTLD_LAZY | RTLD_GLOBAL);
127         if (NULL == handle) {
128                 ERR("dlopen(%s) Fail", sst_utils_library);
129                 return NULL;
130         }
131
132         get_font_info = dlsym(handle, "sstu_get_default_font");
133         if ((error = dlerror()) != NULL) {
134                 ERR("dlsym(sstu_get_default_font) Fail(%s)", error);
135                 dlclose(handle);
136                 return NULL;
137         }
138         ret = get_font_info();
139         dlclose(handle);
140         return ret;
141 }
142
143 int sst_font_set_size(sst_interface *iface, void *value)
144 {
145         int *vconf_value;
146         vconf_value = *(int **)value;
147
148         RETVM_IF(*vconf_value < 0 || SYSTEM_SETTINGS_FONT_SIZE_GIANT < *vconf_value,
149                 SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER, "Invalid value(%d)", *vconf_value);
150
151         if (vconf_set_int(iface->vconf_key, *vconf_value)) {
152                 ERR("vconf_set_int(%s) Fail", iface->vconf_key);
153                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
154         }
155
156         dl_font_size_set();
157         return SYSTEM_SETTINGS_ERROR_NONE;
158 }
159
160 static void* font_conf_doc_parse(char *doc_name, char *font_name)
161 {
162         xmlDocPtr doc = NULL;
163         xmlNodePtr cur = NULL;
164         xmlNodePtr cur2 = NULL;
165         xmlNodePtr cur3 = NULL;
166         xmlChar *key = NULL;
167
168         doc = xmlParseFile(doc_name);
169
170         cur = xmlDocGetRootElement(doc);
171
172         if (cur == NULL) {
173                 xmlFreeDoc(doc);
174                 doc = NULL;
175                 return NULL;
176         }
177
178         if (xmlStrcmp(cur->name, (const xmlChar*)"fontconfig")) {
179                 xmlFreeDoc(doc);
180                 doc = NULL;
181                 return NULL;
182         }
183
184         cur = cur->xmlChildrenNode;
185
186         bool is_changed = false;
187         while (cur != NULL) {
188                 if ((!xmlStrcmp(cur->name, (const xmlChar*)"match"))) {
189                         cur2 = cur->xmlChildrenNode;
190                         while (cur2 != NULL) {
191                                 if ((!xmlStrcmp(cur2->name, (const xmlChar*)"edit"))) {
192                                         xmlChar *name = xmlGetProp(cur2, (const xmlChar*)"name");
193                                         /* if name is not 'family', break */
194                                         if (xmlStrcmp(name, (const xmlChar*)"family")) {
195                                                 xmlFree(name);
196                                                 name = NULL;
197                                                 break;
198                                         }
199                                         xmlFree(name);
200                                         name = NULL;
201
202                                         cur3 = cur2->xmlChildrenNode;
203                                         while (cur3 != NULL) {
204                                                 if ((!xmlStrcmp(cur3->name, (const xmlChar*)"string"))) {
205                                                         xmlNodeSetContent(cur3->xmlChildrenNode, (const xmlChar*)font_name);
206                                                         key = xmlNodeListGetString(doc, cur3->xmlChildrenNode, 1);
207                                                         xmlFree(key);
208                                                         key = NULL;
209                                                         is_changed = true;
210                                                 }
211                                                 cur3 = cur3->next;
212                                         }
213                                 }
214                                 cur2 = cur2->next;
215                         }
216                 } else if ((!xmlStrcmp(cur->name, (const xmlChar*)"alias"))) {
217                         cur2 = cur->xmlChildrenNode;
218                         while (cur2 != NULL) {
219                                 if ((!xmlStrcmp(cur2->name, (const xmlChar*)"family"))) {
220                                         xmlNodeSetContent(cur2->xmlChildrenNode, (const xmlChar*)font_name);
221                                         key = xmlNodeListGetString(doc, cur2->xmlChildrenNode, 1);
222                                         xmlFree(key);
223                                         key = NULL;
224                                         is_changed = true;
225                                 } else if ((!xmlStrcmp(cur2->name, (const xmlChar*)"prefer"))) {
226                                         cur3 = cur2->xmlChildrenNode;
227                                         while (cur3 != NULL) {
228                                                 if ((!xmlStrcmp(cur3->name, (const xmlChar*)"family"))) {
229                                                         xmlNodeSetContent(cur3->xmlChildrenNode, (const xmlChar*)font_name);
230                                                         key = xmlNodeListGetString(doc, cur3->xmlChildrenNode, 1);
231                                                         xmlFree(key);
232                                                         key = NULL;
233                                                         is_changed = true;
234                                                         cur3 = cur3->next;
235                                                         return doc;
236                                                 }
237                                                 cur3 = cur3->next;
238                                         }
239                                 }
240                                 cur2 = cur2->next;
241                         }
242                 }
243                 cur = cur->next;
244         }
245
246         if (is_changed) {
247                 return doc;
248         } else {
249                 xmlFreeDoc(doc);
250                 doc = NULL;
251                 return NULL;
252         }
253 }
254
255 int sst_font_set_type(sst_interface *iface, void *value)
256 {
257         char *font_name = NULL;
258         font_name = (char*)value;
259
260         /* get current font list */
261         int is_found = dl_is_available_font(font_name);
262         if (is_found == 1) {
263                 DBG("found font : %s ", font_name);
264         } else {
265                 ERR(" NOT found font : %s ", font_name);
266                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
267         }
268
269         bool bsave = dl_font_config_set(font_name);
270         if (!bsave) {
271                 ERR("dl_font_config_set() Fail");
272                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
273         }
274
275         xmlDocPtr doc = font_conf_doc_parse(SETTING_FONT_CONF_FILE, font_name);
276         if (doc != NULL) {
277                 xmlSaveFormatFile(SETTING_FONT_CONF_FILE, doc, 0);
278                 xmlFreeDoc(doc);
279                 doc = NULL;
280         }
281
282         dl_font_config_set_notification();
283
284         char *vconf_value;
285         vconf_value = (char*)value;
286         if (vconf_set_str(iface->vconf_key, vconf_value)) {
287                 ERR("vconf_set_str(%s) Fail", iface->vconf_key);
288                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
289         }
290
291         return SYSTEM_SETTINGS_ERROR_NONE;
292 }
293
294 int sst_font_get_default_type(sst_interface *iface, void **value)
295 {
296         char *font_name = dl_get_default_font_info();
297         if (NULL == font_name) {
298                 ERR("dl_get_default_font_info() Fail");
299                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
300         }
301
302         *value = (void*)font_name;
303         return SYSTEM_SETTINGS_ERROR_NONE;
304 }