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