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