Add include stdlib.h for free()
[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 <libxml/tree.h>
19 #include <stdlib.h>
20 #include <vconf.h>
21 #include "sst.h"
22 #include "sst_utils_wrapper.h"
23
24 int sst_font_set_size(sst_interface *iface, int value)
25 {
26         RETVM_IF(value < 0 || SYSTEM_SETTINGS_FONT_SIZE_GIANT < value,
27                 SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER, "Invalid size(%d)", value);
28
29         if (vconf_set_int(iface->vconf_key, value)) {
30                 ERR("vconf_set_int(%s, %d) Fail", iface->vconf_key, value);
31                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
32         }
33
34         sstu_set_font_size();
35         return SYSTEM_SETTINGS_ERROR_NONE;
36 }
37
38 static void* font_conf_doc_parse(const char *doc_name, const char *font_name)
39 {
40         xmlDocPtr doc = NULL;
41         xmlNodePtr cur = NULL;
42         xmlNodePtr cur2 = NULL;
43         xmlNodePtr cur3 = NULL;
44         xmlChar *key = NULL;
45
46         doc = xmlParseFile(doc_name);
47         cur = xmlDocGetRootElement(doc);
48         if (cur == NULL) {
49                 xmlFreeDoc(doc);
50                 doc = NULL;
51                 return NULL;
52         }
53
54         if (xmlStrcmp(cur->name, (const xmlChar*)"fontconfig")) {
55                 xmlFreeDoc(doc);
56                 doc = NULL;
57                 return NULL;
58         }
59
60         cur = cur->xmlChildrenNode;
61
62         bool is_changed = false;
63         while (cur != NULL) {
64                 if ((!xmlStrcmp(cur->name, (const xmlChar*)"match"))) {
65                         cur2 = cur->xmlChildrenNode;
66                         while (cur2 != NULL) {
67                                 if ((!xmlStrcmp(cur2->name, (const xmlChar*)"edit"))) {
68                                         xmlChar *name = xmlGetProp(cur2, (const xmlChar*)"name");
69                                         /* if name is not 'family', break */
70                                         if (xmlStrcmp(name, (const xmlChar*)"family")) {
71                                                 xmlFree(name);
72                                                 name = NULL;
73                                                 break;
74                                         }
75                                         xmlFree(name);
76                                         name = NULL;
77
78                                         cur3 = cur2->xmlChildrenNode;
79                                         while (cur3 != NULL) {
80                                                 if ((!xmlStrcmp(cur3->name, (const xmlChar*)"string"))) {
81                                                         xmlNodeSetContent(cur3->xmlChildrenNode, (const xmlChar*)font_name);
82                                                         key = xmlNodeListGetString(doc, cur3->xmlChildrenNode, 1);
83                                                         xmlFree(key);
84                                                         key = NULL;
85                                                         is_changed = true;
86                                                 }
87                                                 cur3 = cur3->next;
88                                         }
89                                 }
90                                 cur2 = cur2->next;
91                         }
92                 } else if ((!xmlStrcmp(cur->name, (const xmlChar*)"alias"))) {
93                         cur2 = cur->xmlChildrenNode;
94                         while (cur2 != NULL) {
95                                 if ((!xmlStrcmp(cur2->name, (const xmlChar*)"family"))) {
96                                         xmlNodeSetContent(cur2->xmlChildrenNode, (const xmlChar*)font_name);
97                                         key = xmlNodeListGetString(doc, cur2->xmlChildrenNode, 1);
98                                         xmlFree(key);
99                                         key = NULL;
100                                         is_changed = true;
101                                 } else if ((!xmlStrcmp(cur2->name, (const xmlChar*)"prefer"))) {
102                                         cur3 = cur2->xmlChildrenNode;
103                                         while (cur3 != NULL) {
104                                                 if ((!xmlStrcmp(cur3->name, (const xmlChar*)"family"))) {
105                                                         xmlNodeSetContent(cur3->xmlChildrenNode, (const xmlChar*)font_name);
106                                                         key = xmlNodeListGetString(doc, cur3->xmlChildrenNode, 1);
107                                                         xmlFree(key);
108                                                         key = NULL;
109                                                         is_changed = true;
110                                                         cur3 = cur3->next;
111                                                         return doc;
112                                                 }
113                                                 cur3 = cur3->next;
114                                         }
115                                 }
116                                 cur2 = cur2->next;
117                         }
118                 }
119                 cur = cur->next;
120         }
121
122         if (is_changed) {
123                 return doc;
124         } else {
125                 xmlFreeDoc(doc);
126                 doc = NULL;
127                 return NULL;
128         }
129 }
130
131 int sst_font_set_type(sst_interface *iface, const char *font_name)
132 {
133         /* get current font list */
134         int is_found = sstu_is_available_font(font_name);
135         if (FALSE == is_found) {
136                 ERR("NO font(%s)", font_name);
137                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
138         }
139
140         bool bsave = sstu_set_font_config(font_name);
141         if (!bsave) {
142                 ERR("sstu_set_font_config() Fail");
143                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
144         }
145
146         xmlDocPtr doc = font_conf_doc_parse(SETTING_FONT_CONF_FILE, font_name);
147         if (doc != NULL) {
148                 int ret = xmlSaveFormatFile(SETTING_FONT_CONF_FILE, doc, 0);
149                 xmlFreeDoc(doc);
150                 if (ret <= -1) {
151                         ERR("xmlSaveFormatFile() Fail");
152                         return SYSTEM_SETTINGS_ERROR_IO_ERROR;
153                 }
154         }
155
156         sstu_font_config_set_notification();
157
158         if (vconf_set_str(iface->vconf_key, font_name)) {
159                 ERR("vconf_set_str(%s, %s) Fail", iface->vconf_key, font_name);
160                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
161         }
162
163         return SYSTEM_SETTINGS_ERROR_NONE;
164 }
165
166 int sst_font_get_type(sst_interface *iface, char **value)
167 {
168         RETV_IF(NULL == iface, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
169         RETV_IF(NULL == iface->vconf_key, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
170
171         char *result = vconf_get_str(iface->vconf_key);
172         if (NULL == result) {
173                 ERR("vconf_get_str(%s) Fail(NULL)", iface->vconf_key);
174                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
175         }
176
177         if (SST_EQUAL == strcmp(result, "Default")) {
178                 free(result);
179                 return sst_font_get_default_type(iface, value);
180         } else {
181                 *value = result;
182         }
183
184         return SYSTEM_SETTINGS_ERROR_NONE;
185 }
186
187 int sst_font_get_default_type(sst_interface *iface, char **value)
188 {
189         char *font_name = sstu_get_default_font();
190         if (NULL == font_name) {
191                 ERR("sstu_get_default_font() Fail");
192                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
193         }
194
195         *value = font_name;
196         return SYSTEM_SETTINGS_ERROR_NONE;
197 }