Debian Codegraph @

lib/fontconfig/font-manager-selections.c

raw
1 /* font-manager-selections.c
2 *
3 * Copyright (C) 2009-2025 Jerry Casiano
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.
17 *
18 * If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
19 */
20
21 #include "font-manager-selections.h"
22
23 /**
24 * SECTION: font-manager-selections
25 * @short_description: Fontconfig font selection configuration
26 * @title: Font Selection
27 * @include: font-manager-selections.h
28 *
29 * Base class for generating fontconfig configuration files which modify
30 * font selections.
31 */
32
33 typedef struct
34 {
35 gchar *config_dir;
36 gchar *target_file;
37 gchar *target_element;
38 }
39 FontManagerSelectionsPrivate;
40
41 G_DEFINE_TYPE_WITH_PRIVATE(FontManagerSelections, font_manager_selections, FONT_MANAGER_TYPE_STRING_SET)
42
43 enum
44 {
45 PROP_RESERVED,
46 PROP_CONFIG_DIR,
47 PROP_TARGET_FILE,
48 PROP_TARGET_ELEMENT,
49 N_PROPERTIES
50 };
51
52 static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
53
54 #define DEFAULT_PARAM_FLAGS (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
55
56 static void
57 font_manager_selections_dispose (GObject *gobject)
58 {
59 g_return_if_fail(gobject != NULL);
60 FontManagerSelections *self = FONT_MANAGER_SELECTIONS(gobject);
61 FontManagerSelectionsPrivate *priv = font_manager_selections_get_instance_private(self);
62 g_clear_pointer(&priv->config_dir, g_free);
63 g_clear_pointer(&priv->target_file, g_free);
64 g_clear_pointer(&priv->target_element, g_free);
65 G_OBJECT_CLASS(font_manager_selections_parent_class)->dispose(gobject);
66 return;
67 }
68
69 static void
70 font_manager_selections_get_property (GObject *gobject,
71 guint property_id,
72 GValue *value,
73 GParamSpec *pspec)
74 {
75 g_return_if_fail(gobject != NULL);
76 FontManagerSelections *self = FONT_MANAGER_SELECTIONS(gobject);
77 FontManagerSelectionsPrivate *priv = font_manager_selections_get_instance_private(self);
78
79 switch (property_id) {
80 case PROP_CONFIG_DIR:
81 g_value_set_string(value, priv->config_dir);
82 break;
83 case PROP_TARGET_FILE:
84 g_value_set_string(value, priv->target_file);
85 break;
86 case PROP_TARGET_ELEMENT:
87 g_value_set_string(value, priv->target_element);
88 break;
89 default:
90 G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec);
91 break;
92 }
93
94 return;
95 }
96
97 static void
98 font_manager_selections_set_property (GObject *gobject,
99 guint property_id,
100 const GValue *value,
101 GParamSpec *pspec)
102 {
103 g_return_if_fail(gobject != NULL);
104 FontManagerSelections *self = FONT_MANAGER_SELECTIONS(gobject);
105 FontManagerSelectionsPrivate *priv = font_manager_selections_get_instance_private(self);
106
107 switch (property_id) {
108 case PROP_CONFIG_DIR:
109 g_free(priv->config_dir);
110 priv->config_dir = g_value_dup_string(value);
111 break;
112 case PROP_TARGET_FILE:
113 g_free(priv->target_file);
114 priv->target_file = g_value_dup_string(value);
115 break;
116 case PROP_TARGET_ELEMENT:
117 g_free(priv->target_element);
118 priv->target_element = g_value_dup_string(value);
119 break;
120 default:
121 G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec);
122 break;
123 }
124
125 return;
126 }
127
128 static void
129 font_manager_selections_parse_selections (FontManagerSelections *self,
130 xmlNode *selections)
131 {
132 g_return_if_fail(self != NULL);
133 g_return_if_fail(selections != NULL);
134 for (xmlNode *iter = selections; iter != NULL; iter = iter->next) {
135 if (iter->type != XML_ELEMENT_NODE)
136 continue;
137 xmlChar *content = xmlNodeGetContent(iter);
138 if (!content)
139 continue;
140 content = (xmlChar *) g_strstrip((gchar *) content);
141 if (content && g_strcmp0((const char *) content, "") != 0)
142 font_manager_string_set_add(FONT_MANAGER_STRING_SET(self), (const gchar *) content);
143 xmlFree(content);
144 }
145 return;
146 }
147
148 static void
149 font_manager_selections_write_selections (FontManagerSelections *self,
150 FontManagerXmlWriter *writer)
151 {
152 g_return_if_fail(self != NULL);
153 g_return_if_fail(writer != NULL);
154 FontManagerSelectionsPrivate *priv = font_manager_selections_get_instance_private(self);
155 GList *selections = font_manager_string_set_list(FONT_MANAGER_STRING_SET(self));
156 font_manager_xml_writer_add_selections(writer, priv->target_element, selections);
157 g_list_free_full(selections, g_free);
158 return;
159 }
160
161 static xmlNodePtr
162 font_manager_selections_get_selections (FontManagerSelections *self, xmlDocPtr doc)
163 {
164 g_return_val_if_fail(self != NULL, NULL);
165 g_return_val_if_fail(doc != NULL, NULL);
166 FontManagerSelectionsPrivate *priv = font_manager_selections_get_instance_private(self);
167 xmlNode *root = xmlDocGetRootElement(doc);
168 if (root == NULL)
169 return NULL;
170 for (xmlNode *iter = root->children; iter != NULL; iter = iter->next) {
171 if (iter->type != XML_ELEMENT_NODE)
172 continue;
173 if (g_strcmp0((const char *) iter->name, "selectfont") == 0) {
174 for (xmlNode *result = iter->children; result != NULL; result = result->next) {
175 if (g_strcmp0((const char *) result->name, (const char *) priv->target_element) == 0)
176 return result->children;
177 }
178 }
179 }
180 return NULL;
181 }
182
183 static void
184 font_manager_selections_class_init (FontManagerSelectionsClass *klass)
185 {
186 GObjectClass *object_class = G_OBJECT_CLASS(klass);
187 object_class->get_property = font_manager_selections_get_property;
188 object_class->set_property = font_manager_selections_set_property;
189 object_class->dispose = font_manager_selections_dispose;
190
191 klass->load = font_manager_selections_load;
192 klass->save = font_manager_selections_save;
193 klass->parse_selections = font_manager_selections_parse_selections;
194 klass->write_selections = font_manager_selections_write_selections;
195 klass->get_selections = font_manager_selections_get_selections;
196
197 /**
198 * FontManagerSelections:config-dir:
199 *
200 * Should be set to one of the directories monitored by Fontconfig
201 * for configuration files and writeable by the user.
202 */
203 obj_properties[PROP_CONFIG_DIR] = g_param_spec_string("config-dir",
204 NULL,
205 "Fontconfig configuration directory",
206 NULL,
207 DEFAULT_PARAM_FLAGS);
208
209 /**
210 * FontManagerSelections:target-file:
211 *
212 * Should be set to a filename in the form \[7\]\[0-9\]-*.conf
213 */
214 obj_properties[PROP_TARGET_FILE] = g_param_spec_string("target-file",
215 NULL,
216 "Name of fontconfig configuration file",
217 NULL,
218 DEFAULT_PARAM_FLAGS);
219
220 /**
221 * FontManagerSelections:target-element:
222 *
223 * Valid values: acceptfont or rejectfont
224 */
225 obj_properties[PROP_TARGET_ELEMENT] = g_param_spec_string("target-element",
226 NULL,
227 "A valid selectfont element",
228 NULL,
229 DEFAULT_PARAM_FLAGS);
230
231 g_object_class_install_properties(object_class, N_PROPERTIES, obj_properties);
232 return;
233 }
234
235 static void
236 font_manager_selections_init (FontManagerSelections *self)
237 {
238 g_return_if_fail(self != NULL);
239 FontManagerSelectionsPrivate *priv = font_manager_selections_get_instance_private(self);
240 priv->config_dir = NULL;
241 priv->target_element = NULL;
242 priv->target_file = NULL;
243 return;
244 }
245
246 /**
247 * font_manager_selections_load:
248 * @self: #FontManagerSelections
249 *
250 * Load @target_file from @config_dir
251 *
252 * Returns: %TRUE if configuration was loaded successfully.
253 */
254 gboolean
255 font_manager_selections_load (FontManagerSelections *self)
256 {
257 g_return_val_if_fail(self != NULL, FALSE);
258
259 font_manager_string_set_clear(FONT_MANAGER_STRING_SET(self));
260
261 g_autofree gchar *filepath = font_manager_selections_get_filepath(self);
262 if (filepath == NULL || !font_manager_exists(filepath))
263 return FALSE;
264
265 xmlDoc *doc = xmlReadFile(filepath, NULL, 0);
266
267 /* Empty file */
268 if (doc == NULL)
269 return FALSE;
270
271 xmlNode *selections = FONT_MANAGER_SELECTIONS_GET_CLASS(self)->get_selections(self, doc);
272 if (selections != NULL)
273 FONT_MANAGER_SELECTIONS_GET_CLASS(self)->parse_selections(self, selections);
274
275 xmlFreeDoc(doc);
276
277 return TRUE;
278 }
279
280 /**
281 * font_manager_selections_save:
282 * @self: #FontManagerSelections
283 *
284 * Saves current selections to @target_file in @config_dir
285 *
286 * Returns: %TRUE if configuration was saved successfully.
287 */
288 gboolean
289 font_manager_selections_save (FontManagerSelections *self)
290 {
291 g_return_val_if_fail(self != NULL, FALSE);
292 g_autofree gchar * filepath = font_manager_selections_get_filepath(self);
293 g_return_val_if_fail(filepath != NULL, FALSE);
294 g_autoptr(FontManagerXmlWriter) writer = font_manager_xml_writer_new();
295 font_manager_xml_writer_open(writer, filepath);
296 if (font_manager_string_set_size(FONT_MANAGER_STRING_SET(self)))
297 FONT_MANAGER_SELECTIONS_GET_CLASS(self)->write_selections(self, writer);
298 gboolean result = font_manager_xml_writer_close(writer);
299 return result;
300 }
301
302 /**
303 * font_manager_selections_get_filepath:
304 * @self: #FontManagerSelections
305 *
306 * Returns: (transfer full) (nullable): A newly allocated string containing the full
307 * filepath to current configuration file or %NULL. Free the result using #g_free().
308 */
309 gchar *
310 font_manager_selections_get_filepath (FontManagerSelections *self)
311 {
312 g_return_val_if_fail(self != NULL, NULL);
313 FontManagerSelectionsPrivate *priv = font_manager_selections_get_instance_private(self);
314 if (priv->config_dir == NULL || priv->target_file == NULL)
315 return NULL;
316 return g_build_filename(priv->config_dir, priv->target_file, NULL);
317 }
318
319 /**
320 * font_manager_selections_new:
321 *
322 * Returns: (transfer full): A newly created #FontManagerSelections.
323 * Free the returned object using #g_object_unref().
324 */
325 FontManagerSelections *
326 font_manager_selections_new (void)
327 {
328 return g_object_new(FONT_MANAGER_TYPE_SELECTIONS, NULL);
329 }
330