| 1 |
/* font-manager-string-set.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-string-set.h" |
| 22 |
|
| 23 |
/** |
| 24 |
* SECTION: font-manager-string-set |
| 25 |
* @short_description: Set of unique strings |
| 26 |
* @title: String Set |
| 27 |
* @include: font-manager-string-set.h |
| 28 |
* |
| 29 |
* #FontManagerStringSet provides a convenient way to store and access a set of strings. |
| 30 |
*/ |
| 31 |
|
| 32 |
typedef struct |
| 33 |
{ |
| 34 |
GPtrArray *strings; |
| 35 |
} |
| 36 |
FontManagerStringSetPrivate; |
| 37 |
|
| 38 |
G_DEFINE_TYPE_WITH_PRIVATE(FontManagerStringSet, font_manager_string_set, G_TYPE_OBJECT) |
| 39 |
|
| 40 |
enum |
| 41 |
{ |
| 42 |
PROP_RESERVED, |
| 43 |
PROP_SIZE, |
| 44 |
N_PROPERTIES |
| 45 |
}; |
| 46 |
|
| 47 |
enum |
| 48 |
{ |
| 49 |
CHANGED, |
| 50 |
NUM_SIGNALS |
| 51 |
}; |
| 52 |
|
| 53 |
static guint signals[NUM_SIGNALS]; |
| 54 |
|
| 55 |
static void |
| 56 |
font_manager_string_set_dispose (GObject *gobject) |
| 57 |
{ |
| 58 |
g_return_if_fail(gobject != NULL); |
| 59 |
FontManagerStringSet *self = FONT_MANAGER_STRING_SET(gobject); |
| 60 |
FontManagerStringSetPrivate *priv = font_manager_string_set_get_instance_private(self); |
| 61 |
if (priv->strings) |
| 62 |
g_ptr_array_free(priv->strings, TRUE); |
| 63 |
G_OBJECT_CLASS(font_manager_string_set_parent_class)->dispose(gobject); |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
static void |
| 68 |
font_manager_string_set_get_property (GObject *gobject, |
| 69 |
guint property_id, |
| 70 |
GValue *value, |
| 71 |
GParamSpec *pspec) |
| 72 |
{ |
| 73 |
g_return_if_fail(gobject != NULL); |
| 74 |
FontManagerStringSet *self = FONT_MANAGER_STRING_SET(gobject); |
| 75 |
|
| 76 |
switch (property_id) { |
| 77 |
case PROP_SIZE: |
| 78 |
g_value_set_uint(value, font_manager_string_set_size(self)); |
| 79 |
break; |
| 80 |
default: |
| 81 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); |
| 82 |
break; |
| 83 |
} |
| 84 |
|
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
static void |
| 89 |
font_manager_string_set_class_init (FontManagerStringSetClass *klass) |
| 90 |
{ |
| 91 |
GObjectClass *object_class = G_OBJECT_CLASS(klass); |
| 92 |
object_class->dispose = font_manager_string_set_dispose; |
| 93 |
object_class->get_property = font_manager_string_set_get_property; |
| 94 |
|
| 95 |
/** |
| 96 |
* FontManagerStringSet:size |
| 97 |
* |
| 98 |
* Number of strings contained in this set |
| 99 |
*/ |
| 100 |
g_object_class_install_property(object_class, |
| 101 |
PROP_SIZE, |
| 102 |
g_param_spec_uint("size", |
| 103 |
NULL, |
| 104 |
"Number of entries", |
| 105 |
0, G_MAXUINT, 0, |
| 106 |
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 107 |
|
| 108 |
/** |
| 109 |
* FontManagerStringSet:changed: |
| 110 |
* |
| 111 |
* Emitted whenever the number of strings in set changes |
| 112 |
*/ |
| 113 |
signals[CHANGED] = g_signal_new(g_intern_static_string("changed"), |
| 114 |
G_TYPE_FROM_CLASS(object_class), |
| 115 |
G_SIGNAL_RUN_LAST, |
| 116 |
G_STRUCT_OFFSET(FontManagerStringSetClass, changed), |
| 117 |
NULL, NULL, NULL, |
| 118 |
G_TYPE_NONE, 0); |
| 119 |
|
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
static void |
| 124 |
font_manager_string_set_init (FontManagerStringSet *self) |
| 125 |
{ |
| 126 |
g_return_if_fail(self != NULL); |
| 127 |
FontManagerStringSetPrivate *priv = font_manager_string_set_get_instance_private(self); |
| 128 |
priv->strings = g_ptr_array_new_with_free_func((GDestroyNotify) g_free); |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* font_manager_string_set_add: |
| 134 |
* @self: #FontManagerStringSet |
| 135 |
* @str: string to add to #FontManagerStringSet |
| 136 |
*/ |
| 137 |
void |
| 138 |
font_manager_string_set_add (FontManagerStringSet *self, const gchar *str) |
| 139 |
{ |
| 140 |
g_return_if_fail(self != NULL); |
| 141 |
g_return_if_fail(str != NULL); |
| 142 |
FontManagerStringSetPrivate *priv = font_manager_string_set_get_instance_private(self); |
| 143 |
if (!font_manager_string_set_contains(self, str)) |
| 144 |
g_ptr_array_add(priv->strings, g_strdup(str)); |
| 145 |
g_signal_emit(self, signals[CHANGED], 0); |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* font_manager_string_set_add_all: |
| 151 |
* @self: #FontManagerStringSet |
| 152 |
* @add: (transfer none): #FontManagerStringSet to add to @self |
| 153 |
*/ |
| 154 |
void |
| 155 |
font_manager_string_set_add_all (FontManagerStringSet *self, FontManagerStringSet *add) |
| 156 |
{ |
| 157 |
g_return_if_fail(self != NULL); |
| 158 |
g_object_freeze_notify(G_OBJECT(self)); |
| 159 |
guint n_strings = font_manager_string_set_size(add); |
| 160 |
for (guint i = 0; i < n_strings; i++) |
| 161 |
font_manager_string_set_add(self, font_manager_string_set_get(add, i)); |
| 162 |
g_object_thaw_notify(G_OBJECT(self)); |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* font_manager_string_set_contains: |
| 168 |
* @self: #FontManagerStringSet |
| 169 |
* @str: string to look for in #FontManagerStringSet |
| 170 |
* |
| 171 |
* Returns: %TRUE if @self contains str |
| 172 |
*/ |
| 173 |
gboolean |
| 174 |
font_manager_string_set_contains (FontManagerStringSet *self, const gchar *str) |
| 175 |
{ |
| 176 |
if (!self || !str || font_manager_string_set_size(self) < 1) |
| 177 |
return FALSE; |
| 178 |
FontManagerStringSetPrivate *priv = font_manager_string_set_get_instance_private(self); |
| 179 |
return g_ptr_array_find_with_equal_func(priv->strings, str, (GEqualFunc) g_str_equal, NULL); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* font_manager_string_set_contains_all: |
| 184 |
* @self: #FontManagerStringSet |
| 185 |
* @contents: (transfer none): #FontManagerStringSet to check against |
| 186 |
* |
| 187 |
* Returns: %TRUE if all strings in @contents are contained in @self |
| 188 |
*/ |
| 189 |
gboolean |
| 190 |
font_manager_string_set_contains_all (FontManagerStringSet *self, FontManagerStringSet *contents) |
| 191 |
{ |
| 192 |
g_return_val_if_fail(self != NULL, FALSE); |
| 193 |
guint n_strings = font_manager_string_set_size(contents); |
| 194 |
for (guint i = 0; i < n_strings; i++) |
| 195 |
if (!font_manager_string_set_contains(self, font_manager_string_set_get(contents, i))) |
| 196 |
return FALSE; |
| 197 |
return TRUE; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* font_manager_string_set_remove: |
| 202 |
* @self: #FontManagerStringSet |
| 203 |
* @str: string to remove from #FontManagerStringSet |
| 204 |
*/ |
| 205 |
void |
| 206 |
font_manager_string_set_remove (FontManagerStringSet *self, const gchar *str) |
| 207 |
{ |
| 208 |
g_return_if_fail(self != NULL); |
| 209 |
FontManagerStringSetPrivate *priv = font_manager_string_set_get_instance_private(self); |
| 210 |
guint index; |
| 211 |
if (g_ptr_array_find_with_equal_func(priv->strings, str, (GEqualFunc) g_str_equal, &index)) |
| 212 |
g_ptr_array_remove_index(priv->strings, index); |
| 213 |
g_signal_emit(self, signals[CHANGED], 0); |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* font_manager_string_set_remove_all: |
| 219 |
* @self: #FontManagerStringSet |
| 220 |
* @remove: (transfer none): #FontManagerStringSet containing strings to remove |
| 221 |
*/ |
| 222 |
void |
| 223 |
font_manager_string_set_remove_all (FontManagerStringSet *self, FontManagerStringSet *remove) |
| 224 |
{ |
| 225 |
g_return_if_fail(self != NULL); |
| 226 |
g_object_freeze_notify(G_OBJECT(self)); |
| 227 |
guint n_strings = font_manager_string_set_size(remove); |
| 228 |
for (guint i = 0; i < n_strings; i++) |
| 229 |
font_manager_string_set_remove(self, font_manager_string_set_get(remove, i)); |
| 230 |
g_object_thaw_notify(G_OBJECT(self)); |
| 231 |
return; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* font_manager_string_set_retain_all: |
| 236 |
* @self: #FontManagerStringSet |
| 237 |
* @retain: (transfer none): #FontManagerStringSet |
| 238 |
* |
| 239 |
* Remove any elements not contained in @retain |
| 240 |
*/ |
| 241 |
void |
| 242 |
font_manager_string_set_retain_all (FontManagerStringSet *self, FontManagerStringSet *retain) |
| 243 |
{ |
| 244 |
g_return_if_fail(self != NULL); |
| 245 |
FontManagerStringSetPrivate *priv = font_manager_string_set_get_instance_private(self); |
| 246 |
GPtrArray *tmp = g_ptr_array_new_with_free_func((GDestroyNotify) g_free); |
| 247 |
guint n_strings = font_manager_string_set_size(retain); |
| 248 |
for (guint i = 0; i < n_strings; i++) { |
| 249 |
guint index; |
| 250 |
const gchar *entry = font_manager_string_set_get(retain, i); |
| 251 |
if (g_ptr_array_find_with_equal_func(priv->strings, entry, (GEqualFunc) g_str_equal, &index)) |
| 252 |
g_ptr_array_add(tmp, g_ptr_array_steal_index_fast(priv->strings, index)); |
| 253 |
} |
| 254 |
g_ptr_array_free(priv->strings, TRUE); |
| 255 |
priv->strings = tmp; |
| 256 |
g_signal_emit(self, signals[CHANGED], 0); |
| 257 |
return; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* font_manager_string_set_size: |
| 262 |
* @self: #FontManagerStringSet |
| 263 |
* |
| 264 |
* Returns: Returns the number of strings contained in @self |
| 265 |
*/ |
| 266 |
guint |
| 267 |
font_manager_string_set_size (FontManagerStringSet *self) |
| 268 |
{ |
| 269 |
g_return_val_if_fail(self != NULL, 0); |
| 270 |
FontManagerStringSetPrivate *priv = font_manager_string_set_get_instance_private(self); |
| 271 |
return priv->strings->len; |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* font_manager_string_set_list: |
| 276 |
* @self: #FontManagerStringSet |
| 277 |
* |
| 278 |
* Returns: (element-type utf8) (transfer full): A #GList containing |
| 279 |
* the contents of #FontManagerStringSet. |
| 280 |
* Use #g_list_free_full(list, #g_free) when done using the list. |
| 281 |
*/ |
| 282 |
GList * |
| 283 |
font_manager_string_set_list (FontManagerStringSet *self) |
| 284 |
{ |
| 285 |
g_return_val_if_fail(self != NULL, NULL); |
| 286 |
FontManagerStringSetPrivate *priv = font_manager_string_set_get_instance_private(self); |
| 287 |
GList *result = NULL; |
| 288 |
for (guint i = 0; i < priv->strings->len; i++) |
| 289 |
result = g_list_prepend(result, g_strdup(g_ptr_array_index(priv->strings, i))); |
| 290 |
result = g_list_reverse(result); |
| 291 |
return result; |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* font_manager_string_set_foreach: |
| 296 |
* @self: #FontManagerStringSet |
| 297 |
* @func: (scope call): #GFunc to call for each string in the set |
| 298 |
* @user_data: user data to pass to the function |
| 299 |
* |
| 300 |
* Calls a function for each string of a #FontManagerStringSet. |
| 301 |
* @func must not add elements to or remove elements from the #FontManagerStringSet. |
| 302 |
*/ |
| 303 |
void |
| 304 |
font_manager_string_set_foreach(FontManagerStringSet *self, GFunc func, gpointer user_data) |
| 305 |
{ |
| 306 |
g_return_if_fail(self != NULL); |
| 307 |
FontManagerStringSetPrivate *priv = font_manager_string_set_get_instance_private(self); |
| 308 |
g_ptr_array_foreach(priv->strings, func, user_data); |
| 309 |
return; |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* font_manager_string_set_clear: |
| 314 |
* @self: #FontManagerStringSet |
| 315 |
* |
| 316 |
* Clear all strings from @self |
| 317 |
*/ |
| 318 |
void |
| 319 |
font_manager_string_set_clear (FontManagerStringSet *self) |
| 320 |
{ |
| 321 |
g_return_if_fail(self != NULL); |
| 322 |
FontManagerStringSetPrivate *priv = font_manager_string_set_get_instance_private(self); |
| 323 |
g_ptr_array_remove_range(priv->strings, 0, priv->strings->len); |
| 324 |
g_signal_emit(self, signals[CHANGED], 0); |
| 325 |
return; |
| 326 |
} |
| 327 |
|
| 328 |
gint |
| 329 |
compare_func (gconstpointer a, gconstpointer b) |
| 330 |
{ |
| 331 |
g_return_val_if_fail((a != NULL && b != NULL), 0); |
| 332 |
g_autofree gchar *s1 = g_utf8_collate_key_for_filename((gchar *) a, -1); |
| 333 |
g_autofree gchar *s2 = g_utf8_collate_key_for_filename((gchar *) b, -1); |
| 334 |
return g_strcmp0(s1, s2); |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* font_manager_string_set_sort: |
| 339 |
* @self: #FontManagerStringSet |
| 340 |
* |
| 341 |
* Sorts the set, using @compare_func |
| 342 |
*/ |
| 343 |
void |
| 344 |
font_manager_string_set_sort(FontManagerStringSet *self) |
| 345 |
{ |
| 346 |
g_return_if_fail(self != NULL); |
| 347 |
FontManagerStringSetPrivate *priv = font_manager_string_set_get_instance_private(self); |
| 348 |
g_ptr_array_sort_values(priv->strings, compare_func); |
| 349 |
return; |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* font_manager_string_set_get: |
| 354 |
* @self: a #FontManagerStringSet |
| 355 |
* @index: index of entry to retrieve |
| 356 |
* |
| 357 |
* Returns: (transfer none) (nullable): A string which is owned by #FontManagerStringSet |
| 358 |
* and should not be modified or freed. %NULL on error or if index could not be retrieved. |
| 359 |
*/ |
| 360 |
const gchar * |
| 361 |
font_manager_string_set_get (FontManagerStringSet *self, guint index) |
| 362 |
{ |
| 363 |
g_return_val_if_fail(self != NULL, NULL); |
| 364 |
FontManagerStringSetPrivate *priv = font_manager_string_set_get_instance_private(self); |
| 365 |
g_return_val_if_fail(index >= 0 && index < priv->strings->len, NULL); |
| 366 |
return g_ptr_array_index(priv->strings, index); |
| 367 |
} |
| 368 |
|
| 369 |
static void |
| 370 |
add_string_to_array (gchar *str, GStrvBuilder *strv) |
| 371 |
{ |
| 372 |
g_strv_builder_add(strv, str); |
| 373 |
return; |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* font_manager_string_set_to_strv: |
| 378 |
* @self: a #FontManagerStringSet |
| 379 |
* |
| 380 |
* Returns: (array zero-terminated=1) (element-type utf8) (transfer full): |
| 381 |
* The returned value should be freed with #g_strfreev() when no longer needed. |
| 382 |
*/ |
| 383 |
GStrv |
| 384 |
font_manager_string_set_to_strv (FontManagerStringSet *self) |
| 385 |
{ |
| 386 |
g_autoptr(GStrvBuilder) strv = g_strv_builder_new(); |
| 387 |
font_manager_string_set_foreach(self, (GFunc) add_string_to_array, strv); |
| 388 |
return g_strv_builder_end(strv); |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* font_manager_string_set_new: |
| 393 |
* |
| 394 |
* Returns: (transfer full): A newly-created #FontManagerStringSet. |
| 395 |
* Free the returned object using #g_object_unref(). |
| 396 |
**/ |
| 397 |
FontManagerStringSet * |
| 398 |
font_manager_string_set_new (void) |
| 399 |
{ |
| 400 |
return g_object_new(FONT_MANAGER_TYPE_STRING_SET, NULL); |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* font_manager_string_set_new_from_strv: |
| 405 |
* @strv: (array zero-terminated=1) (element-type utf8) (not nullable) (transfer none) : |
| 406 |
* %NULL terminated array of strings to include in the returned set |
| 407 |
* |
| 408 |
* Returns: (transfer full): A newly-created #FontManagerStringSet. |
| 409 |
* Free the returned object using #g_object_unref(). |
| 410 |
**/ |
| 411 |
FontManagerStringSet * |
| 412 |
font_manager_string_set_new_from_strv (GStrv strv) |
| 413 |
{ |
| 414 |
FontManagerStringSet *set = font_manager_string_set_new(); |
| 415 |
for (int i = 0; strv[i] != NULL; i++) |
| 416 |
font_manager_string_set_add(set, strv[i]); |
| 417 |
return set; |
| 418 |
} |
| 419 |
|
| 420 |
|