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 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 g_themed_icon_get_property (GObject *object,
79 guint prop_id,
80 GValue *value,
81 GParamSpec *pspec)
82 {
83 GThemedIcon *icon = G_THEMED_ICON (object);
84
85 switch (prop_id)
86 {
87 case PROP_NAMES:
88 g_value_set_boxed (value, icon->init_names);
89 break;
90
91 case PROP_USE_DEFAULT_FALLBACKS:
92 g_value_set_boolean (value, icon->use_default_fallbacks);
93 break;
94
95 default:
96 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
97 }
98 }
99
100 static void
101 g_themed_icon_set_property (GObject *object,
102 guint prop_id,
103 const GValue *value,
104 GParamSpec *pspec)
105 {
106 GThemedIcon *icon = G_THEMED_ICON (object);
107 gchar **names;
108 const gchar *name;
109
110 switch (prop_id)
111 {
112 case PROP_NAME:
113 name = g_value_get_string (value);
114
115 if (!name)
116 break;
117
118 if (icon->init_names)
119 g_strfreev (icon->init_names);
120
121 icon->init_names = g_new (char *, 2);
122 icon->init_names[0] = g_strdup (name);
123 icon->init_names[1] = NULL;
124 break;
125
126 case PROP_NAMES:
127 names = g_value_dup_boxed (value);
128
129 if (!names)
130 break;
131
132 if (icon->init_names)
133 g_strfreev (icon->init_names);
134
135 icon->init_names = names;
136 break;
137
138 case PROP_USE_DEFAULT_FALLBACKS:
139 icon->use_default_fallbacks = g_value_get_boolean (value);
140 break;
141
142 default:
143 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
144 }
145 }
146
147 static void
148 g_themed_icon_constructed (GObject *object)
149 {
150 g_themed_icon_update_names (G_THEMED_ICON (object));
151 }
152
153 static void
154 g_themed_icon_finalize (GObject *object)
155 {
156 GThemedIcon *themed;
157
158 themed = G_THEMED_ICON (object);
159
160 g_strfreev (themed->init_names);
161 g_strfreev (themed->names);
162
163 G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize (object);
164 }
165
166 static void
167 g_themed_icon_class_init (GThemedIconClass *klass)
168 {
169 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
170
171 gobject_class->finalize = g_themed_icon_finalize;
172 gobject_class->constructed = g_themed_icon_constructed;
173 gobject_class->set_property = g_themed_icon_set_property;
174 gobject_class->get_property = g_themed_icon_get_property;
175
176 /**
177 * GThemedIcon:name:
178 *
179 * The icon name.
180 */
181 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 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 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 }
220
221 static void
222 g_themed_icon_init (GThemedIcon *themed)
223 {
224 themed->init_names = NULL;
225 themed->names = NULL;
226 }
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 * - The first requested icon is first in priority.
237 * - If "use-default-fallbacks" is #TRUE, then it is followed by all its
238 * fallbacks (starting from top to lower context levels).
239 * - Then next requested icons, and optionally their fallbacks, follow.
240 * - Finally all the style variants (symbolic or regular, opposite to whatever
241 * is the requested style) follow in the same order.
242 *
243 * An icon is not added twice in the list if it was previously added.
244 *
245 * For instance, if requested names are:
246 * [ "some-icon-symbolic", "some-other-icon" ]
247 * and use-default-fallbacks is TRUE, the final name list shall be:
248 * [ "some-icon-symbolic", "some-symbolic", "some-other-icon",
249 * "some-other", "some", "some-icon", "some-other-icon-symbolic",
250 * "some-other-symbolic" ]
251 *
252 * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon
253 **/
254 static void
255 g_themed_icon_update_names (GThemedIcon *themed)
256 {
257 GList *names = NULL;
258 GList *variants = NULL;
259 GList *iter;
260 guint i;
261
262 g_return_if_fail (themed->init_names != NULL && themed->init_names[0] != NULL);
263
264 for (i = 0; themed->init_names[i]; i++)
265 {
266 gchar *name;
267 gboolean is_symbolic;
268
269 is_symbolic = g_str_has_suffix (themed->init_names[i], "-symbolic");
270 if (is_symbolic)
271 name = g_strndup (themed->init_names[i], strlen (themed->init_names[i]) - 9);
272 else
273 name = g_strdup (themed->init_names[i]);
274
275 if (g_list_find_custom (names, name, (GCompareFunc) g_strcmp0))
276 {
277 g_free (name);
278 continue;
279 }
280
281 if (is_symbolic)
282 names = g_list_prepend (names, g_strdup (themed->init_names[i]));
283 else
284 names = g_list_prepend (names, name);
285
286 if (themed->use_default_fallbacks)
287 {
288 char *dashp;
289 char *last;
290
291 last = name;
292
293 while ((dashp = strrchr (last, '-')) != NULL)
294 {
295 gchar *tmp = last;
296 gchar *fallback;
297
298 last = g_strndup (last, dashp - last);
299 if (is_symbolic)
300 {
301 g_free (tmp);
302 fallback = g_strdup_printf ("%s-symbolic", last);
303 }
304 else
305 fallback = last;
306 if (g_list_find_custom (names, fallback, (GCompareFunc) g_strcmp0))
307 {
308 g_free (fallback);
309 break;
310 }
311 names = g_list_prepend (names, fallback);
312 }
313 if (is_symbolic)
314 g_free (last);
315 }
316 else if (is_symbolic)
317 g_free (name);
318 }
319 for (iter = names; iter; iter = iter->next)
320 {
321 gchar *name = (gchar *) iter->data;
322 gchar *variant;
323 gboolean is_symbolic;
324
325 is_symbolic = g_str_has_suffix (name, "-symbolic");
326 if (is_symbolic)
327 variant = g_strndup (name, strlen (name) - 9);
328 else
329 variant = g_strdup_printf ("%s-symbolic", name);
330 if (g_list_find_custom (names, variant, (GCompareFunc) g_strcmp0) ||
331 g_list_find_custom (variants, variant, (GCompareFunc) g_strcmp0))
332 {
333 g_free (variant);
334 continue;
335 }
336
337 variants = g_list_prepend (variants, variant);
338 }
339 names = g_list_reverse (names);
340
341 g_strfreev (themed->names);
342 themed->names = g_new (char *, g_list_length (names) + g_list_length (variants) + 1);
343
344 for (iter = names, i = 0; iter; iter = iter->next, i++)
345 themed->names[i] = iter->data;
346 for (iter = variants; iter; iter = iter->next, i++)
347 themed->names[i] = iter->data;
348 themed->names[i] = NULL;
349
350 g_list_free (names);
351 g_list_free (variants);
352
353 g_object_notify (G_OBJECT (themed), "names");
354 }
355
356 /**
357 * g_themed_icon_new:
358 * @iconname: a string containing an icon name.
359 *
360 * Creates a new themed icon for @iconname.
361 *
362 * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon.
363 **/
364 GIcon *
365 g_themed_icon_new (const char *iconname)
366 {
367 g_return_val_if_fail (iconname != NULL, NULL);
368
369 return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, NULL));
370 }
371
372 /**
373 * g_themed_icon_new_from_names:
374 * @iconnames: (array length=len): an array of strings containing icon names.
375 * @len: the length of the @iconnames array, or -1 if @iconnames is
376 * %NULL-terminated
377 *
378 * Creates a new themed icon for @iconnames.
379 *
380 * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon
381 **/
382 GIcon *
383 g_themed_icon_new_from_names (char **iconnames,
384 int len)
385 {
386 GIcon *icon;
387
388 g_return_val_if_fail (iconnames != NULL, NULL);
389
390 if (len >= 0)
391 {
392 char **names;
393 int i;
394
395 names = g_new (char *, len + 1);
396
397 for (i = 0; i < len; i++)
398 names[i] = iconnames[i];
399
400 names[i] = NULL;
401
402 icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", names, NULL));
403
404 g_free (names);
405 }
406 else
407 icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", iconnames, NULL));
408
409 return icon;
410 }
411
412 /**
413 * g_themed_icon_new_with_default_fallbacks:
414 * @iconname: a string containing an icon name
415 *
416 * Creates a new themed icon for @iconname, and all the names
417 * that can be created by shortening @iconname at '-' characters.
418 *
419 * In the following example, @icon1 and @icon2 are equivalent:
420 * |[<!-- language="C" -->
421 * const char *names[] = {
422 * "gnome-dev-cdrom-audio",
423 * "gnome-dev-cdrom",
424 * "gnome-dev",
425 * "gnome"
426 * };
427 *
428 * icon1 = g_themed_icon_new_from_names (names, 4);
429 * icon2 = g_themed_icon_new_with_default_fallbacks ("gnome-dev-cdrom-audio");
430 * ]|
431 *
432 * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon.
433 */
434 GIcon *
435 g_themed_icon_new_with_default_fallbacks (const char *iconname)
436 {
437 g_return_val_if_fail (iconname != NULL, NULL);
438
439 return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, "use-default-fallbacks", TRUE, NULL));
440 }
441
442
443 /**
444 * g_themed_icon_get_names:
445 * @icon: a #GThemedIcon.
446 *
447 * Gets the names of icons from within @icon.
448 *
449 * Returns: (transfer none): a list of icon names.
450 */
451 const char * const *
452 g_themed_icon_get_names (GThemedIcon *icon)
453 {
454 g_return_val_if_fail (G_IS_THEMED_ICON (icon), NULL);
455 return (const char * const *)icon->names;
456 }
457
458 /**
459 * g_themed_icon_append_name:
460 * @icon: a #GThemedIcon
461 * @iconname: name of icon to append to list of icons from within @icon.
462 *
463 * Append a name to the list of icons from within @icon.
464 *
465 * Note that doing so invalidates the hash computed by prior calls
466 * to g_icon_hash().
467 */
468 void
469 g_themed_icon_append_name (GThemedIcon *icon,
470 const char *iconname)
471 {
472 guint num_names;
473
474 g_return_if_fail (G_IS_THEMED_ICON (icon));
475 g_return_if_fail (iconname != NULL);
476
477 num_names = g_strv_length (icon->init_names);
478 icon->init_names = g_realloc (icon->init_names, sizeof (char*) * (num_names + 2));
479 icon->init_names[num_names] = g_strdup (iconname);
480 icon->init_names[num_names + 1] = NULL;
481
482 g_themed_icon_update_names (icon);
483 }
484
485 /**
486 * g_themed_icon_prepend_name:
487 * @icon: a #GThemedIcon
488 * @iconname: name of icon to prepend to list of icons from within @icon.
489 *
490 * Prepend a name to the list of icons from within @icon.
491 *
492 * Note that doing so invalidates the hash computed by prior calls
493 * to g_icon_hash().
494 *
495 * Since: 2.18
496 */
497 void
498 g_themed_icon_prepend_name (GThemedIcon *icon,
499 const char *iconname)
500 {
501 guint num_names;
502 gchar **names;
503 gint i;
504
505 g_return_if_fail (G_IS_THEMED_ICON (icon));
506 g_return_if_fail (iconname != NULL);
507
508 num_names = g_strv_length (icon->init_names);
509 names = g_new (char*, num_names + 2);
510 for (i = 0; icon->init_names[i]; i++)
511 names[i + 1] = icon->init_names[i];
512 names[0] = g_strdup (iconname);
513 names[num_names + 1] = NULL;
514
515 g_free (icon->init_names);
516 icon->init_names = names;
517
518 g_themed_icon_update_names (icon);
519 }
520
521 static guint
522 g_themed_icon_hash (GIcon *icon)
523 {
524 GThemedIcon *themed = G_THEMED_ICON (icon);
525 guint hash;
526 int i;
527
528 hash = 0;
529
530 for (i = 0; themed->names[i] != NULL; i++)
531 hash ^= g_str_hash (themed->names[i]);
532
533 return hash;
534 }
535
536 static gboolean
537 g_themed_icon_equal (GIcon *icon1,
538 GIcon *icon2)
539 {
540 GThemedIcon *themed1 = G_THEMED_ICON (icon1);
541 GThemedIcon *themed2 = G_THEMED_ICON (icon2);
542 int i;
543
544 for (i = 0; themed1->names[i] != NULL && themed2->names[i] != NULL; i++)
545 {
546 if (!g_str_equal (themed1->names[i], themed2->names[i]))
547 return FALSE;
548 }
549
550 return themed1->names[i] == NULL && themed2->names[i] == NULL;
551 }
552
553
554 static gboolean
555 g_themed_icon_to_tokens (GIcon *icon,
556 GPtrArray *tokens,
557 gint *out_version)
558 {
559 GThemedIcon *themed_icon = G_THEMED_ICON (icon);
560 int n;
561
562 g_return_val_if_fail (out_version != NULL, FALSE);
563
564 *out_version = 0;
565
566 for (n = 0; themed_icon->names[n] != NULL; n++)
567 g_ptr_array_add (tokens,
568 g_strdup (themed_icon->names[n]));
569
570 return TRUE;
571 }
572
573 static GIcon *
574 g_themed_icon_from_tokens (gchar **tokens,
575 gint num_tokens,
576 gint version,
577 GError **error)
578 {
579 GIcon *icon;
580 gchar **names;
581 int n;
582
583 icon = NULL;
584
585 if (version != 0)
586 {
587 g_set_error (error,
588 G_IO_ERROR,
589 G_IO_ERROR_INVALID_ARGUMENT,
590 _("Can’t handle version %d of GThemedIcon encoding"),
591 version);
592 goto out;
593 }
594
595 names = g_new0 (gchar *, num_tokens + 1);
596 for (n = 0; n < num_tokens; n++)
597 names[n] = tokens[n];
598 names[n] = NULL;
599
600 icon = g_themed_icon_new_from_names (names, num_tokens);
601 g_free (names);
602
603 out:
604 return icon;
605 }
606
607 static GVariant *
608 g_themed_icon_serialize (GIcon *icon)
609 {
610 GThemedIcon *themed_icon = G_THEMED_ICON (icon);
611
612 return g_variant_new ("(sv)", "themed", g_variant_new ("^as", themed_icon->names));
613 }
614
615 static void
616 g_themed_icon_icon_iface_init (GIconIface *iface)
617 {
618 iface->hash = g_themed_icon_hash;
619 iface->equal = g_themed_icon_equal;
620 iface->to_tokens = g_themed_icon_to_tokens;
621 iface->from_tokens = g_themed_icon_from_tokens;
622 iface->serialize = g_themed_icon_serialize;
623 }