| 1 |
/* font-manager-aliases.c |
| 2 |
* |
| 3 |
* Copyright (C) 2009-2024 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-aliases.h" |
| 22 |
|
| 23 |
/** |
| 24 |
* SECTION: font-manager-aliases |
| 25 |
* @short_description: Fontconfig alias configuration |
| 26 |
* @title: Aliases |
| 27 |
* @include: font-manager-aliases.h |
| 28 |
* |
| 29 |
* #FontManagerAliases holds a set of #FontManagerAliasElement and provides |
| 30 |
* methods for loading and saving from/to a fontconfig configuration file. |
| 31 |
*/ |
| 32 |
|
| 33 |
struct _FontManagerAliases |
| 34 |
{ |
| 35 |
GObject parent; |
| 36 |
}; |
| 37 |
|
| 38 |
typedef struct |
| 39 |
{ |
| 40 |
gchar *config_dir; |
| 41 |
gchar *target_file; |
| 42 |
|
| 43 |
GHashTable *hash_table; |
| 44 |
} |
| 45 |
FontManagerAliasesPrivate; |
| 46 |
|
| 47 |
G_DEFINE_TYPE_WITH_PRIVATE(FontManagerAliases, font_manager_aliases, G_TYPE_OBJECT) |
| 48 |
|
| 49 |
enum |
| 50 |
{ |
| 51 |
PROP_RESERVED, |
| 52 |
PROP_CONFIG_DIR, |
| 53 |
PROP_TARGET_FILE, |
| 54 |
N_PROPERTIES |
| 55 |
}; |
| 56 |
|
| 57 |
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, }; |
| 58 |
|
| 59 |
static void |
| 60 |
font_manager_aliases_dispose (GObject *gobject) |
| 61 |
{ |
| 62 |
g_return_if_fail(gobject != NULL); |
| 63 |
FontManagerAliases *self = FONT_MANAGER_ALIASES(gobject); |
| 64 |
FontManagerAliasesPrivate *priv = font_manager_aliases_get_instance_private(self); |
| 65 |
g_clear_pointer(&priv->config_dir, g_free); |
| 66 |
g_clear_pointer(&priv->target_file, g_free); |
| 67 |
g_clear_pointer(&priv->hash_table, g_hash_table_destroy); |
| 68 |
G_OBJECT_CLASS(font_manager_aliases_parent_class)->dispose(gobject); |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
static void |
| 73 |
font_manager_aliases_get_property (GObject *gobject, |
| 74 |
guint property_id, |
| 75 |
GValue *value, |
| 76 |
GParamSpec *pspec) |
| 77 |
{ |
| 78 |
g_return_if_fail(gobject != NULL); |
| 79 |
FontManagerAliases *self = FONT_MANAGER_ALIASES(gobject); |
| 80 |
FontManagerAliasesPrivate *priv = font_manager_aliases_get_instance_private(self); |
| 81 |
switch (property_id) { |
| 82 |
case PROP_CONFIG_DIR: |
| 83 |
g_value_set_string(value, priv->config_dir); |
| 84 |
break; |
| 85 |
case PROP_TARGET_FILE: |
| 86 |
g_value_set_string(value, priv->target_file); |
| 87 |
break; |
| 88 |
default: |
| 89 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); |
| 90 |
break; |
| 91 |
} |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
static void |
| 96 |
font_manager_aliases_set_property (GObject *gobject, |
| 97 |
guint property_id, |
| 98 |
const GValue *value, |
| 99 |
GParamSpec *pspec) |
| 100 |
{ |
| 101 |
g_return_if_fail(gobject != NULL); |
| 102 |
FontManagerAliases *self = FONT_MANAGER_ALIASES(gobject); |
| 103 |
FontManagerAliasesPrivate *priv = font_manager_aliases_get_instance_private(self); |
| 104 |
switch (property_id) { |
| 105 |
case PROP_CONFIG_DIR: |
| 106 |
g_free(priv->config_dir); |
| 107 |
priv->config_dir = g_value_dup_string(value); |
| 108 |
break; |
| 109 |
case PROP_TARGET_FILE: |
| 110 |
g_free(priv->target_file); |
| 111 |
priv->target_file = g_value_dup_string(value); |
| 112 |
break; |
| 113 |
default: |
| 114 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); |
| 115 |
break; |
| 116 |
} |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
static void |
| 121 |
font_manager_aliases_class_init (FontManagerAliasesClass *klass) |
| 122 |
{ |
| 123 |
GObjectClass *object_class = G_OBJECT_CLASS(klass); |
| 124 |
object_class->dispose = font_manager_aliases_dispose; |
| 125 |
object_class->get_property = font_manager_aliases_get_property; |
| 126 |
object_class->set_property = font_manager_aliases_set_property; |
| 127 |
|
| 128 |
/** |
| 129 |
* FontManagerAliases:config-dir: |
| 130 |
* |
| 131 |
* Should be set to one of the directories monitored by Fontconfig |
| 132 |
* for configuration files and writeable by the user. |
| 133 |
*/ |
| 134 |
obj_properties[PROP_CONFIG_DIR] = g_param_spec_string("config-dir", |
| 135 |
NULL, |
| 136 |
"Fontconfig configuration directory", |
| 137 |
NULL, |
| 138 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); |
| 139 |
|
| 140 |
/** |
| 141 |
* FontManagerAliases:target-file: |
| 142 |
* |
| 143 |
* Should be set to a filename in the form \[3\]\[0-9\]-*.conf |
| 144 |
*/ |
| 145 |
obj_properties[PROP_TARGET_FILE] = g_param_spec_string("target-file", |
| 146 |
NULL, |
| 147 |
"Name of fontconfig configuration file", |
| 148 |
NULL, |
| 149 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); |
| 150 |
|
| 151 |
g_object_class_install_properties(object_class, N_PROPERTIES, obj_properties); |
| 152 |
|
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
static void |
| 157 |
font_manager_aliases_init (FontManagerAliases *self) |
| 158 |
{ |
| 159 |
g_return_if_fail(self != NULL); |
| 160 |
FontManagerAliasesPrivate *priv = font_manager_aliases_get_instance_private(self); |
| 161 |
priv->config_dir = font_manager_get_user_fontconfig_directory(); |
| 162 |
priv->target_file = g_strdup("39-Aliases.conf"); |
| 163 |
priv->hash_table = g_hash_table_new_full(g_str_hash, |
| 164 |
g_str_equal, |
| 165 |
(GDestroyNotify) g_free, |
| 166 |
(GDestroyNotify) g_object_unref); |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
static void |
| 171 |
_xml_writer_add_alias_element(FontManagerXmlWriter *writer, |
| 172 |
FontManagerStringSet *hashset, |
| 173 |
const gchar *type) |
| 174 |
{ |
| 175 |
GList *families = font_manager_string_set_list(hashset); |
| 176 |
font_manager_xml_writer_start_element(writer, type); |
| 177 |
font_manager_xml_writer_add_elements(writer, "family", families); |
| 178 |
font_manager_xml_writer_end_element(writer); |
| 179 |
g_list_free_full(families, g_free); |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
static void |
| 184 |
xml_writer_add_alias_element (FontManagerXmlWriter *writer, |
| 185 |
FontManagerAliasElement *alias) |
| 186 |
{ |
| 187 |
g_autofree gchar *family = NULL; |
| 188 |
g_autoptr(FontManagerStringSet) p = NULL; |
| 189 |
g_autoptr(FontManagerStringSet) a = NULL; |
| 190 |
g_autoptr(FontManagerStringSet) d = NULL; |
| 191 |
g_object_get(alias, "family", &family, "prefer", &p, "accept", &a, "default", &d, NULL); |
| 192 |
g_return_if_fail(family != NULL); |
| 193 |
font_manager_xml_writer_start_element(writer, "alias"); |
| 194 |
font_manager_xml_writer_write_attribute(writer, "binding", "strong"); |
| 195 |
font_manager_xml_writer_write_element(writer, "family", family); |
| 196 |
if (p) |
| 197 |
_xml_writer_add_alias_element(writer, p, "prefer"); |
| 198 |
if (a) |
| 199 |
_xml_writer_add_alias_element(writer, a, "accept"); |
| 200 |
if (d) |
| 201 |
_xml_writer_add_alias_element(writer, d, "default"); |
| 202 |
font_manager_xml_writer_end_element(writer); |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
static void |
| 207 |
parse_alias_node (FontManagerAliases *self, xmlNodePtr alias_node) |
| 208 |
{ |
| 209 |
FontManagerAliasesPrivate *priv = font_manager_aliases_get_instance_private(self); |
| 210 |
FontManagerAliasElement *ae = font_manager_alias_element_new(NULL); |
| 211 |
xmlChar *family = NULL; |
| 212 |
for (xmlNodePtr iter = alias_node->children; iter != NULL; iter = iter->next) { |
| 213 |
if (iter->type != XML_ELEMENT_NODE) |
| 214 |
continue; |
| 215 |
if (g_strcmp0((const char *) iter->name, "family") == 0) { |
| 216 |
family = xmlNodeGetContent(iter); |
| 217 |
g_object_set(ae, "family", family, NULL); |
| 218 |
} else { |
| 219 |
GObjectClass *object_class = G_OBJECT_GET_CLASS(ae); |
| 220 |
GParamSpec *pspec = g_object_class_find_property(object_class, (const gchar *) iter->name); |
| 221 |
if (pspec == NULL) |
| 222 |
continue; |
| 223 |
g_autoptr(FontManagerStringSet) hashset = font_manager_string_set_new(); |
| 224 |
for (xmlNodePtr _iter = iter->children; _iter != NULL; _iter = _iter->next) { |
| 225 |
if (g_strcmp0((const char *) _iter->name, "family") == 0) { |
| 226 |
xmlChar *content = xmlNodeGetContent(_iter); |
| 227 |
font_manager_string_set_add(hashset, (const gchar *) content); |
| 228 |
xmlFree(content); |
| 229 |
} |
| 230 |
} |
| 231 |
g_object_set(ae, g_param_spec_get_name(pspec), hashset, NULL); |
| 232 |
} |
| 233 |
} |
| 234 |
gchar *tmp = g_strdup((const gchar *) family); |
| 235 |
g_hash_table_insert(priv->hash_table, tmp, ae); |
| 236 |
if (family) |
| 237 |
xmlFree(family); |
| 238 |
return; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* font_manager_aliases_get: |
| 243 |
* @self: #FontManagerAliases |
| 244 |
* @family: Font family name. |
| 245 |
* |
| 246 |
* Returns: (transfer none): #FontManagerAliasElement for @family or %NULL. |
| 247 |
* The result may be modified but should not be freed. |
| 248 |
*/ |
| 249 |
FontManagerAliasElement * |
| 250 |
font_manager_aliases_get (FontManagerAliases *self, const gchar *family) |
| 251 |
{ |
| 252 |
g_return_val_if_fail(self != NULL, FALSE); |
| 253 |
FontManagerAliasesPrivate *priv = font_manager_aliases_get_instance_private(self); |
| 254 |
return g_hash_table_lookup(priv->hash_table, family); |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* font_manager_aliases_contains: |
| 259 |
* @self: #FontManagerAliases |
| 260 |
* @family: family to check for |
| 261 |
* |
| 262 |
* Returns: %TRUE if a #FontManagerAliasElement for @family exists in @self |
| 263 |
*/ |
| 264 |
gboolean |
| 265 |
font_manager_aliases_contains (FontManagerAliases *self, const gchar *family) |
| 266 |
{ |
| 267 |
g_return_val_if_fail(self != NULL, FALSE); |
| 268 |
FontManagerAliasesPrivate *priv = font_manager_aliases_get_instance_private(self); |
| 269 |
return (g_hash_table_lookup(priv->hash_table, family) != NULL); |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* font_manager_aliases_add: |
| 274 |
* @self: #FontManagerAliases |
| 275 |
* @family: family name |
| 276 |
* |
| 277 |
* Returns: %TRUE if a new #FontManagerAliasElement for @family was added successfully |
| 278 |
*/ |
| 279 |
gboolean |
| 280 |
font_manager_aliases_add (FontManagerAliases *self, const gchar *family) |
| 281 |
{ |
| 282 |
g_return_val_if_fail(self != NULL, FALSE); |
| 283 |
FontManagerAliasesPrivate *priv = font_manager_aliases_get_instance_private(self); |
| 284 |
FontManagerAliasElement *element = font_manager_alias_element_new(family); |
| 285 |
g_hash_table_insert(priv->hash_table, g_strdup(family), element); |
| 286 |
return g_hash_table_contains(priv->hash_table, family); |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* font_manager_aliases_add_element: |
| 291 |
* @self: #FontManagerAliases |
| 292 |
* @element: (transfer full): #FontManagerAliasElement |
| 293 |
* |
| 294 |
* Returns: %TRUE if @element was added successfully |
| 295 |
*/ |
| 296 |
gboolean |
| 297 |
font_manager_aliases_add_element (FontManagerAliases *self, FontManagerAliasElement *element) |
| 298 |
{ |
| 299 |
g_return_val_if_fail(self != NULL, FALSE); |
| 300 |
FontManagerAliasesPrivate *priv = font_manager_aliases_get_instance_private(self); |
| 301 |
gchar *tmp = NULL; |
| 302 |
g_object_get(element, "family", &tmp, NULL); |
| 303 |
g_hash_table_insert(priv->hash_table, tmp, element); |
| 304 |
return g_hash_table_contains(priv->hash_table, tmp); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* font_manager_aliases_remove: |
| 309 |
* @self: #FontManagerAliases |
| 310 |
* @family: family name |
| 311 |
* |
| 312 |
* Returns: %TRUE if the #FontManagerAliasElement for @family was removed successfully |
| 313 |
*/ |
| 314 |
gboolean |
| 315 |
font_manager_aliases_remove (FontManagerAliases *self, const gchar *family) |
| 316 |
{ |
| 317 |
g_return_val_if_fail(self != NULL, FALSE); |
| 318 |
FontManagerAliasesPrivate *priv = font_manager_aliases_get_instance_private(self); |
| 319 |
return g_hash_table_remove(priv->hash_table, family); |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* font_manager_aliases_load: |
| 324 |
* @self: #FontManagerAliases |
| 325 |
* |
| 326 |
* Returns: %TRUE if current configuration was successfully loaded |
| 327 |
*/ |
| 328 |
gboolean |
| 329 |
font_manager_aliases_load (FontManagerAliases *self) |
| 330 |
{ |
| 331 |
g_return_val_if_fail(self != NULL, FALSE); |
| 332 |
FontManagerAliasesPrivate *priv = font_manager_aliases_get_instance_private(self); |
| 333 |
g_hash_table_remove_all(priv->hash_table); |
| 334 |
|
| 335 |
g_autofree gchar *filepath = font_manager_aliases_get_filepath(self); |
| 336 |
if (filepath == NULL) |
| 337 |
return FALSE; |
| 338 |
|
| 339 |
g_autoptr(GFile) file = g_file_new_for_path(filepath); |
| 340 |
if (!g_file_query_exists(file, NULL)) |
| 341 |
return FALSE; |
| 342 |
|
| 343 |
xmlInitParser(); |
| 344 |
xmlDoc *doc = xmlReadFile(filepath, NULL, 0); |
| 345 |
|
| 346 |
if (doc == NULL) { |
| 347 |
/* Empty file */ |
| 348 |
return FALSE; |
| 349 |
} |
| 350 |
|
| 351 |
xmlXPathContextPtr ctx = xmlXPathNewContext(doc); |
| 352 |
xmlXPathObjectPtr res = xmlXPathEvalExpression((const xmlChar *) "//alias", ctx); |
| 353 |
for (int i = 0; i < xmlXPathNodeSetGetLength(res->nodesetval); i++) |
| 354 |
parse_alias_node(self, xmlXPathNodeSetItem(res->nodesetval, i)); |
| 355 |
|
| 356 |
xmlFreeDoc(doc); |
| 357 |
xmlXPathFreeContext(ctx); |
| 358 |
xmlXPathFreeObject(res); |
| 359 |
return TRUE; |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* font_manager_aliases_save: |
| 364 |
* @self: #FontManagerAliases |
| 365 |
* |
| 366 |
* Returns: %TRUE if current configuration was successfully saved to file |
| 367 |
*/ |
| 368 |
gboolean |
| 369 |
font_manager_aliases_save (FontManagerAliases *self) |
| 370 |
{ |
| 371 |
g_return_val_if_fail(self != NULL, FALSE); |
| 372 |
g_autofree gchar *filepath = font_manager_aliases_get_filepath(self); |
| 373 |
g_return_val_if_fail(filepath != NULL, FALSE); |
| 374 |
FontManagerAliasesPrivate *priv = font_manager_aliases_get_instance_private(self); |
| 375 |
g_autoptr(FontManagerXmlWriter) writer = font_manager_xml_writer_new(); |
| 376 |
font_manager_xml_writer_open(writer, filepath); |
| 377 |
GList *aliases = g_hash_table_get_values(priv->hash_table); |
| 378 |
for (GList *iter = aliases; iter != NULL; iter = iter->next) |
| 379 |
xml_writer_add_alias_element(writer, iter->data); |
| 380 |
g_list_free(aliases); |
| 381 |
gboolean result = font_manager_xml_writer_close(writer); |
| 382 |
return result; |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* font_manager_aliases_list: |
| 387 |
* @self: #FontManagerAliases |
| 388 |
* |
| 389 |
* Returns: (nullable) (transfer container) (element-type FontManagerAliasElement): |
| 390 |
* #GList of #FontManagerAliasElement or %NULL. Free the returned #GList using #g_list_free(). |
| 391 |
*/ |
| 392 |
GList * |
| 393 |
font_manager_aliases_list (FontManagerAliases *self) |
| 394 |
{ |
| 395 |
g_return_val_if_fail(self != NULL, FALSE); |
| 396 |
FontManagerAliasesPrivate *priv = font_manager_aliases_get_instance_private(self); |
| 397 |
return g_hash_table_get_values(priv->hash_table); |
| 398 |
} |
| 399 |
|
| 400 |
/** |
| 401 |
* font_manager_aliases_get_filepath: |
| 402 |
* @self: #FontManagerAliases |
| 403 |
* |
| 404 |
* Returns: (transfer full) (nullable): A newly allocated string containing the full |
| 405 |
* filepath to current configuration file or %NULL. Free the result using #g_free(). |
| 406 |
*/ |
| 407 |
gchar * |
| 408 |
font_manager_aliases_get_filepath (FontManagerAliases *self) |
| 409 |
{ |
| 410 |
g_return_val_if_fail(self != NULL, NULL); |
| 411 |
FontManagerAliasesPrivate *priv = font_manager_aliases_get_instance_private(self); |
| 412 |
if (priv->config_dir == NULL || priv->target_file == NULL) |
| 413 |
return NULL; |
| 414 |
return g_build_filename(priv->config_dir, priv->target_file, NULL); |
| 415 |
} |
| 416 |
|
| 417 |
/** |
| 418 |
* font_manager_aliases_new: |
| 419 |
* |
| 420 |
* Returns: (transfer full): A newly created #FontManagerAliases. |
| 421 |
* Free the returned object using #g_object_unref(). |
| 422 |
*/ |
| 423 |
FontManagerAliases * |
| 424 |
font_manager_aliases_new (void) |
| 425 |
{ |
| 426 |
return g_object_new(FONT_MANAGER_TYPE_ALIASES, NULL); |
| 427 |
} |