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