Branch data Line data Source code
1 : : /* GIO - GLib Input, Output and Streaming Library
2 : : *
3 : : * Copyright (C) 2006-2007 Red Hat, Inc.
4 : : *
5 : : * SPDX-License-Identifier: LGPL-2.1-or-later
6 : : *
7 : : * This library is free software; you can redistribute it and/or
8 : : * modify it under the terms of the GNU Lesser General Public
9 : : * License as published by the Free Software Foundation; either
10 : : * version 2.1 of the License, or (at your option) any later version.
11 : : *
12 : : * This library is distributed in the hope that it will be useful,
13 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : * Lesser General Public License for more details.
16 : : *
17 : : * You should have received a copy of the GNU Lesser General
18 : : * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 : : *
20 : : * Author: Alexander Larsson <alexl@redhat.com>
21 : : */
22 : :
23 : : #include "config.h"
24 : :
25 : : #include <string.h>
26 : :
27 : : #include "gthemedicon.h"
28 : : #include "gicon.h"
29 : : #include "gioerror.h"
30 : : #include "glibintl.h"
31 : :
32 : :
33 : : /**
34 : : * GThemedIcon:
35 : : *
36 : : * `GThemedIcon` is an implementation of [iface@Gio.Icon] that supports icon
37 : : * themes.
38 : : *
39 : : * `GThemedIcon` contains a list of all of the icons present in an icon
40 : : * theme, so that icons can be looked up quickly. `GThemedIcon` does
41 : : * not provide actual pixmaps for icons, just the icon names.
42 : : * Ideally something like [method@Gtk.IconTheme.choose_icon] should be used to
43 : : * resolve the list of names so that fallback icons work nicely with
44 : : * themes that inherit other themes.
45 : : **/
46 : :
47 : : static void g_themed_icon_icon_iface_init (GIconIface *iface);
48 : :
49 : : struct _GThemedIcon
50 : : {
51 : : GObject parent_instance;
52 : :
53 : : char **init_names;
54 : : char **names;
55 : : gboolean use_default_fallbacks;
56 : : };
57 : :
58 : : struct _GThemedIconClass
59 : : {
60 : : GObjectClass parent_class;
61 : : };
62 : :
63 : : enum
64 : : {
65 : : PROP_0,
66 : : PROP_NAME,
67 : : PROP_NAMES,
68 : : PROP_USE_DEFAULT_FALLBACKS
69 : : };
70 : :
71 : : static void g_themed_icon_update_names (GThemedIcon *themed);
72 : :
73 : 2180 : G_DEFINE_TYPE_WITH_CODE (GThemedIcon, g_themed_icon, G_TYPE_OBJECT,
74 : : G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
75 : : g_themed_icon_icon_iface_init))
76 : :
77 : : static void
78 : 35 : g_themed_icon_get_property (GObject *object,
79 : : guint prop_id,
80 : : GValue *value,
81 : : GParamSpec *pspec)
82 : : {
83 : 35 : GThemedIcon *icon = G_THEMED_ICON (object);
84 : :
85 : 35 : switch (prop_id)
86 : : {
87 : 17 : case PROP_NAMES:
88 : 17 : g_value_set_boxed (value, icon->init_names);
89 : 17 : break;
90 : :
91 : 18 : case PROP_USE_DEFAULT_FALLBACKS:
92 : 18 : g_value_set_boolean (value, icon->use_default_fallbacks);
93 : 18 : break;
94 : :
95 : 0 : default:
96 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
97 : : }
98 : 35 : }
99 : :
100 : : static void
101 : 900 : g_themed_icon_set_property (GObject *object,
102 : : guint prop_id,
103 : : const GValue *value,
104 : : GParamSpec *pspec)
105 : : {
106 : 900 : GThemedIcon *icon = G_THEMED_ICON (object);
107 : : gchar **names;
108 : : const gchar *name;
109 : :
110 : 900 : switch (prop_id)
111 : : {
112 : 300 : case PROP_NAME:
113 : 300 : name = g_value_get_string (value);
114 : :
115 : 300 : if (!name)
116 : 172 : break;
117 : :
118 : 128 : if (icon->init_names)
119 : 0 : g_strfreev (icon->init_names);
120 : :
121 : 128 : icon->init_names = g_new (char *, 2);
122 : 128 : icon->init_names[0] = g_strdup (name);
123 : 128 : icon->init_names[1] = NULL;
124 : 128 : break;
125 : :
126 : 300 : case PROP_NAMES:
127 : 300 : names = g_value_dup_boxed (value);
128 : :
129 : 300 : if (!names)
130 : 128 : break;
131 : :
132 : 172 : if (icon->init_names)
133 : 0 : g_strfreev (icon->init_names);
134 : :
135 : 172 : icon->init_names = names;
136 : 172 : break;
137 : :
138 : 300 : case PROP_USE_DEFAULT_FALLBACKS:
139 : 300 : icon->use_default_fallbacks = g_value_get_boolean (value);
140 : 300 : break;
141 : :
142 : 0 : default:
143 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
144 : : }
145 : 900 : }
146 : :
147 : : static void
148 : 300 : g_themed_icon_constructed (GObject *object)
149 : : {
150 : 300 : g_themed_icon_update_names (G_THEMED_ICON (object));
151 : 300 : }
152 : :
153 : : static void
154 : 300 : g_themed_icon_finalize (GObject *object)
155 : : {
156 : : GThemedIcon *themed;
157 : :
158 : 300 : themed = G_THEMED_ICON (object);
159 : :
160 : 300 : g_strfreev (themed->init_names);
161 : 300 : g_strfreev (themed->names);
162 : :
163 : 300 : G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize (object);
164 : 300 : }
165 : :
166 : : static void
167 : 26 : g_themed_icon_class_init (GThemedIconClass *klass)
168 : : {
169 : 26 : GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
170 : :
171 : 26 : gobject_class->finalize = g_themed_icon_finalize;
172 : 26 : gobject_class->constructed = g_themed_icon_constructed;
173 : 26 : gobject_class->set_property = g_themed_icon_set_property;
174 : 26 : gobject_class->get_property = g_themed_icon_get_property;
175 : :
176 : : /**
177 : : * GThemedIcon:name:
178 : : *
179 : : * The icon name.
180 : : */
181 : 26 : g_object_class_install_property (gobject_class, PROP_NAME,
182 : : g_param_spec_string ("name", NULL, NULL,
183 : : NULL,
184 : : G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
185 : :
186 : : /**
187 : : * GThemedIcon:names:
188 : : *
189 : : * A %NULL-terminated array of icon names.
190 : : */
191 : 26 : g_object_class_install_property (gobject_class, PROP_NAMES,
192 : : g_param_spec_boxed ("names", NULL, NULL,
193 : : G_TYPE_STRV,
194 : : G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
195 : :
196 : : /**
197 : : * GThemedIcon:use-default-fallbacks:
198 : : *
199 : : * Whether to use the default fallbacks found by shortening the icon name
200 : : * at '-' characters. If the "names" array has more than one element,
201 : : * ignores any past the first.
202 : : *
203 : : * For example, if the icon name was "gnome-dev-cdrom-audio", the array
204 : : * would become
205 : : * |[<!-- language="C" -->
206 : : * {
207 : : * "gnome-dev-cdrom-audio",
208 : : * "gnome-dev-cdrom",
209 : : * "gnome-dev",
210 : : * "gnome",
211 : : * NULL
212 : : * };
213 : : * ]|
214 : : */
215 : 26 : g_object_class_install_property (gobject_class, PROP_USE_DEFAULT_FALLBACKS,
216 : : g_param_spec_boolean ("use-default-fallbacks", NULL, NULL,
217 : : FALSE,
218 : : G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
219 : 26 : }
220 : :
221 : : static void
222 : 300 : g_themed_icon_init (GThemedIcon *themed)
223 : : {
224 : 300 : themed->init_names = NULL;
225 : 300 : themed->names = NULL;
226 : 300 : }
227 : :
228 : : /**
229 : : * g_themed_icon_update_names:
230 : : * @themed: a #GThemedIcon.
231 : : *
232 : : * Update the actual icon name list, based on the requested names (from
233 : : * construction, or later added with g_themed_icon_prepend_name() and
234 : : * g_themed_icon_append_name()).
235 : : * The order of the list matters, indicating priority:
236 : : *
237 : : * - The first requested icon is first in priority.
238 : : * - If "use-default-fallbacks" is #TRUE, then it is followed by all its
239 : : * fallbacks (starting from top to lower context levels).
240 : : * - Then next requested icons, and optionally their fallbacks, follow.
241 : : * - Finally all the style variants (symbolic or regular, opposite to whatever
242 : : * is the requested style) follow in the same order.
243 : : *
244 : : * An icon is not added twice in the list if it was previously added.
245 : : *
246 : : * For instance, if requested names are:
247 : : * [ "some-icon-symbolic", "some-other-icon" ]
248 : : * and use-default-fallbacks is TRUE, the final name list shall be:
249 : : * [ "some-icon-symbolic", "some-symbolic", "some-other-icon",
250 : : * "some-other", "some", "some-icon", "some-other-icon-symbolic",
251 : : * "some-other-symbolic" ]
252 : : *
253 : : * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon
254 : : **/
255 : : static void
256 : 311 : g_themed_icon_update_names (GThemedIcon *themed)
257 : : {
258 : 311 : GList *names = NULL;
259 : 311 : GList *variants = NULL;
260 : : GList *iter;
261 : : guint i;
262 : :
263 : 311 : g_return_if_fail (themed->init_names != NULL && themed->init_names[0] != NULL);
264 : :
265 : 997 : for (i = 0; themed->init_names[i]; i++)
266 : : {
267 : : gchar *name;
268 : : gboolean is_symbolic;
269 : :
270 : 686 : is_symbolic = g_str_has_suffix (themed->init_names[i], "-symbolic");
271 : 686 : if (is_symbolic)
272 : 203 : name = g_strndup (themed->init_names[i], strlen (themed->init_names[i]) - 9);
273 : : else
274 : 966 : name = g_strdup (themed->init_names[i]);
275 : :
276 : 686 : if (g_list_find_custom (names, name, (GCompareFunc) g_strcmp0))
277 : : {
278 : 30 : g_free (name);
279 : 30 : continue;
280 : : }
281 : :
282 : 656 : if (is_symbolic)
283 : 346 : names = g_list_prepend (names, g_strdup (themed->init_names[i]));
284 : : else
285 : 483 : names = g_list_prepend (names, name);
286 : :
287 : 656 : if (themed->use_default_fallbacks)
288 : : {
289 : : char *dashp;
290 : : char *last;
291 : :
292 : 18 : last = name;
293 : :
294 : 47 : while ((dashp = strrchr (last, '-')) != NULL)
295 : : {
296 : 29 : gchar *tmp = last;
297 : : gchar *fallback;
298 : :
299 : 29 : last = g_strndup (last, (size_t) (dashp - last));
300 : 29 : if (is_symbolic)
301 : : {
302 : 10 : g_free (tmp);
303 : 10 : fallback = g_strdup_printf ("%s-symbolic", last);
304 : : }
305 : : else
306 : 19 : fallback = last;
307 : 29 : if (g_list_find_custom (names, fallback, (GCompareFunc) g_strcmp0))
308 : : {
309 : 0 : g_free (fallback);
310 : 0 : break;
311 : : }
312 : 29 : names = g_list_prepend (names, fallback);
313 : : }
314 : 18 : if (is_symbolic)
315 : 6 : g_free (last);
316 : : }
317 : 638 : else if (is_symbolic)
318 : 167 : g_free (name);
319 : : }
320 : 996 : for (iter = names; iter; iter = iter->next)
321 : : {
322 : 685 : gchar *name = (gchar *) iter->data;
323 : : gchar *variant;
324 : : gboolean is_symbolic;
325 : :
326 : 685 : is_symbolic = g_str_has_suffix (name, "-symbolic");
327 : 685 : if (is_symbolic)
328 : 183 : variant = g_strndup (name, strlen (name) - 9);
329 : : else
330 : 502 : variant = g_strdup_printf ("%s-symbolic", name);
331 : 1052 : if (g_list_find_custom (names, variant, (GCompareFunc) g_strcmp0) ||
332 : 367 : g_list_find_custom (variants, variant, (GCompareFunc) g_strcmp0))
333 : : {
334 : 318 : g_free (variant);
335 : 318 : continue;
336 : : }
337 : :
338 : 367 : variants = g_list_prepend (variants, variant);
339 : : }
340 : 311 : names = g_list_reverse (names);
341 : :
342 : 311 : g_strfreev (themed->names);
343 : 311 : themed->names = g_new (char *, g_list_length (names) + g_list_length (variants) + 1);
344 : :
345 : 996 : for (iter = names, i = 0; iter; iter = iter->next, i++)
346 : 685 : themed->names[i] = iter->data;
347 : 678 : for (iter = variants; iter; iter = iter->next, i++)
348 : 367 : themed->names[i] = iter->data;
349 : 311 : themed->names[i] = NULL;
350 : :
351 : 311 : g_list_free (names);
352 : 311 : g_list_free (variants);
353 : :
354 : 311 : g_object_notify (G_OBJECT (themed), "names");
355 : : }
356 : :
357 : : /**
358 : : * g_themed_icon_new:
359 : : * @iconname: a string containing an icon name.
360 : : *
361 : : * Creates a new themed icon for @iconname.
362 : : *
363 : : * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon.
364 : : **/
365 : : GIcon *
366 : 116 : g_themed_icon_new (const char *iconname)
367 : : {
368 : 116 : g_return_val_if_fail (iconname != NULL, NULL);
369 : :
370 : 116 : return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, NULL));
371 : : }
372 : :
373 : : /**
374 : : * g_themed_icon_new_from_names:
375 : : * @iconnames: (array length=len): an array of strings containing icon names.
376 : : * @len: the length of the @iconnames array, or -1 if @iconnames is
377 : : * %NULL-terminated
378 : : *
379 : : * Creates a new themed icon for @iconnames.
380 : : *
381 : : * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon
382 : : **/
383 : : GIcon *
384 : 172 : g_themed_icon_new_from_names (char **iconnames,
385 : : int len)
386 : : {
387 : : GIcon *icon;
388 : :
389 : 172 : g_return_val_if_fail (iconnames != NULL, NULL);
390 : :
391 : 172 : if (len >= 0)
392 : : {
393 : : char **names;
394 : : size_t i;
395 : :
396 : 171 : names = g_new (char *, (size_t) len + 1);
397 : :
398 : 703 : for (i = 0; i < (size_t) len; i++)
399 : 532 : names[i] = iconnames[i];
400 : :
401 : 171 : names[i] = NULL;
402 : :
403 : 171 : icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", names, NULL));
404 : :
405 : 171 : g_free (names);
406 : : }
407 : : else
408 : 1 : icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", iconnames, NULL));
409 : :
410 : 172 : return icon;
411 : : }
412 : :
413 : : /**
414 : : * g_themed_icon_new_with_default_fallbacks:
415 : : * @iconname: a string containing an icon name
416 : : *
417 : : * Creates a new themed icon for @iconname, and all the names
418 : : * that can be created by shortening @iconname at '-' characters.
419 : : *
420 : : * In the following example, @icon1 and @icon2 are equivalent:
421 : : * |[<!-- language="C" -->
422 : : * const char *names[] = {
423 : : * "gnome-dev-cdrom-audio",
424 : : * "gnome-dev-cdrom",
425 : : * "gnome-dev",
426 : : * "gnome"
427 : : * };
428 : : *
429 : : * icon1 = g_themed_icon_new_from_names (names, 4);
430 : : * icon2 = g_themed_icon_new_with_default_fallbacks ("gnome-dev-cdrom-audio");
431 : : * ]|
432 : : *
433 : : * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon.
434 : : */
435 : : GIcon *
436 : 12 : g_themed_icon_new_with_default_fallbacks (const char *iconname)
437 : : {
438 : 12 : g_return_val_if_fail (iconname != NULL, NULL);
439 : :
440 : 12 : return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, "use-default-fallbacks", TRUE, NULL));
441 : : }
442 : :
443 : :
444 : : /**
445 : : * g_themed_icon_get_names:
446 : : * @icon: a #GThemedIcon.
447 : : *
448 : : * Gets the names of icons from within @icon.
449 : : *
450 : : * Returns: (transfer none): a list of icon names.
451 : : */
452 : : const char * const *
453 : 7 : g_themed_icon_get_names (GThemedIcon *icon)
454 : : {
455 : 7 : g_return_val_if_fail (G_IS_THEMED_ICON (icon), NULL);
456 : 7 : return (const char * const *)icon->names;
457 : : }
458 : :
459 : : /**
460 : : * g_themed_icon_append_name:
461 : : * @icon: a #GThemedIcon
462 : : * @iconname: name of icon to append to list of icons from within @icon.
463 : : *
464 : : * Append a name to the list of icons from within @icon.
465 : : *
466 : : * Note that doing so invalidates the hash computed by prior calls
467 : : * to g_icon_hash().
468 : : */
469 : : void
470 : 10 : g_themed_icon_append_name (GThemedIcon *icon,
471 : : const char *iconname)
472 : : {
473 : : guint num_names;
474 : :
475 : 10 : g_return_if_fail (G_IS_THEMED_ICON (icon));
476 : 10 : g_return_if_fail (iconname != NULL);
477 : :
478 : 10 : num_names = g_strv_length (icon->init_names);
479 : 10 : icon->init_names = g_realloc (icon->init_names, sizeof (char*) * (num_names + 2));
480 : 10 : icon->init_names[num_names] = g_strdup (iconname);
481 : 10 : icon->init_names[num_names + 1] = NULL;
482 : :
483 : 10 : g_themed_icon_update_names (icon);
484 : : }
485 : :
486 : : /**
487 : : * g_themed_icon_prepend_name:
488 : : * @icon: a #GThemedIcon
489 : : * @iconname: name of icon to prepend to list of icons from within @icon.
490 : : *
491 : : * Prepend a name to the list of icons from within @icon.
492 : : *
493 : : * Note that doing so invalidates the hash computed by prior calls
494 : : * to g_icon_hash().
495 : : *
496 : : * Since: 2.18
497 : : */
498 : : void
499 : 1 : g_themed_icon_prepend_name (GThemedIcon *icon,
500 : : const char *iconname)
501 : : {
502 : : guint num_names;
503 : : gchar **names;
504 : : gint i;
505 : :
506 : 1 : g_return_if_fail (G_IS_THEMED_ICON (icon));
507 : 1 : g_return_if_fail (iconname != NULL);
508 : :
509 : 1 : num_names = g_strv_length (icon->init_names);
510 : 1 : names = g_new (char*, num_names + 2);
511 : 2 : for (i = 0; icon->init_names[i]; i++)
512 : 1 : names[i + 1] = icon->init_names[i];
513 : 1 : names[0] = g_strdup (iconname);
514 : 1 : names[num_names + 1] = NULL;
515 : :
516 : 1 : g_free (icon->init_names);
517 : 1 : icon->init_names = names;
518 : :
519 : 1 : g_themed_icon_update_names (icon);
520 : : }
521 : :
522 : : static guint
523 : 18 : g_themed_icon_hash (GIcon *icon)
524 : : {
525 : 18 : GThemedIcon *themed = G_THEMED_ICON (icon);
526 : : guint hash;
527 : : int i;
528 : :
529 : 18 : hash = 0;
530 : :
531 : 72 : for (i = 0; themed->names[i] != NULL; i++)
532 : 54 : hash ^= g_str_hash (themed->names[i]);
533 : :
534 : 18 : return hash;
535 : : }
536 : :
537 : : static gboolean
538 : 23 : g_themed_icon_equal (GIcon *icon1,
539 : : GIcon *icon2)
540 : : {
541 : 23 : GThemedIcon *themed1 = G_THEMED_ICON (icon1);
542 : 23 : GThemedIcon *themed2 = G_THEMED_ICON (icon2);
543 : : int i;
544 : :
545 : 107 : for (i = 0; themed1->names[i] != NULL && themed2->names[i] != NULL; i++)
546 : : {
547 : 84 : if (!g_str_equal (themed1->names[i], themed2->names[i]))
548 : 0 : return FALSE;
549 : : }
550 : :
551 : 23 : return themed1->names[i] == NULL && themed2->names[i] == NULL;
552 : : }
553 : :
554 : :
555 : : static gboolean
556 : 15 : g_themed_icon_to_tokens (GIcon *icon,
557 : : GPtrArray *tokens,
558 : : gint *out_version)
559 : : {
560 : 15 : GThemedIcon *themed_icon = G_THEMED_ICON (icon);
561 : : int n;
562 : :
563 : 15 : g_return_val_if_fail (out_version != NULL, FALSE);
564 : :
565 : 15 : *out_version = 0;
566 : :
567 : 101 : for (n = 0; themed_icon->names[n] != NULL; n++)
568 : 86 : g_ptr_array_add (tokens,
569 : 172 : g_strdup (themed_icon->names[n]));
570 : :
571 : 15 : return TRUE;
572 : : }
573 : :
574 : : static GIcon *
575 : 6 : g_themed_icon_from_tokens (gchar **tokens,
576 : : gint num_tokens,
577 : : gint version,
578 : : GError **error)
579 : : {
580 : : GIcon *icon;
581 : : gchar **names;
582 : : size_t n;
583 : :
584 : : /* This is guaranteed by the GIcon interface */
585 : 6 : g_assert (num_tokens >= 0);
586 : :
587 : 6 : icon = NULL;
588 : :
589 : 6 : if (version != 0)
590 : : {
591 : 0 : g_set_error (error,
592 : : G_IO_ERROR,
593 : : G_IO_ERROR_INVALID_ARGUMENT,
594 : : _("Can’t handle version %d of GThemedIcon encoding"),
595 : : version);
596 : 0 : goto out;
597 : : }
598 : :
599 : 6 : names = g_new0 (gchar *, (size_t) num_tokens + 1);
600 : 36 : for (n = 0; n < (size_t) num_tokens; n++)
601 : 30 : names[n] = tokens[n];
602 : 6 : names[n] = NULL;
603 : :
604 : 6 : icon = g_themed_icon_new_from_names (names, num_tokens);
605 : 6 : g_free (names);
606 : :
607 : 6 : out:
608 : 6 : return icon;
609 : : }
610 : :
611 : : static GVariant *
612 : 12 : g_themed_icon_serialize (GIcon *icon)
613 : : {
614 : 12 : GThemedIcon *themed_icon = G_THEMED_ICON (icon);
615 : :
616 : 12 : return g_variant_new ("(sv)", "themed", g_variant_new ("^as", themed_icon->names));
617 : : }
618 : :
619 : : static void
620 : 26 : g_themed_icon_icon_iface_init (GIconIface *iface)
621 : : {
622 : 26 : iface->hash = g_themed_icon_hash;
623 : 26 : iface->equal = g_themed_icon_equal;
624 : 26 : iface->to_tokens = g_themed_icon_to_tokens;
625 : 26 : iface->from_tokens = g_themed_icon_from_tokens;
626 : 26 : iface->serialize = g_themed_icon_serialize;
627 : 26 : }
|