chain up unconditionally in finalize() and dispose(). Also don't
[platform/upstream/glib.git] / gio / gthemedicon.c
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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
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 "glibintl.h"
29
30 #include "gioalias.h"
31
32 /**
33  * SECTION:gthemedicon
34  * @short_description: Icon theming support
35  * @include: gio/gio.h
36  * @see_also: #GIcon, #GLoadableIcon
37  *
38  * #GThemedIcon is an implementation of #GIcon that supports icon themes.
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 gtk_icon_theme_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     **names;
54   gboolean   use_default_fallbacks;
55 };
56
57 struct _GThemedIconClass
58 {
59   GObjectClass parent_class;
60 };
61
62 enum
63 {
64   PROP_0,
65   PROP_NAME,
66   PROP_NAMES,
67   PROP_USE_DEFAULT_FALLBACKS
68 };
69
70 G_DEFINE_TYPE_WITH_CODE (GThemedIcon, g_themed_icon, G_TYPE_OBJECT,
71                          G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
72                                                 g_themed_icon_icon_iface_init))
73
74 static void
75 g_themed_icon_get_property (GObject    *object,
76                             guint       prop_id,
77                             GValue     *value,
78                             GParamSpec *pspec)
79 {
80   GThemedIcon *icon = G_THEMED_ICON (object);
81
82   switch (prop_id)
83     {
84       case PROP_NAMES:
85         g_value_set_boxed (value, icon->names);
86         break;
87
88       default:
89         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
90     }
91 }
92
93 static void
94 g_themed_icon_set_property (GObject      *object,
95                             guint         prop_id,
96                             const GValue *value,
97                             GParamSpec   *pspec)
98 {
99   GThemedIcon *icon = G_THEMED_ICON (object);
100   gchar **names;
101   const gchar *name;
102
103   switch (prop_id)
104     {
105       case PROP_NAME:
106         name = g_value_get_string (value);
107
108         if (!name)
109           break;
110
111         if (icon->names)
112           g_strfreev (icon->names);
113
114         icon->names = g_new (char *, 2);
115         icon->names[0] = g_strdup (name);
116         icon->names[1] = NULL;
117         break;
118
119       case PROP_NAMES:
120         names = g_value_dup_boxed (value);
121
122         if (!names)
123           break;
124
125         if (icon->names)
126           g_strfreev (icon->names);
127
128         icon->names = names;
129         break;
130
131       case PROP_USE_DEFAULT_FALLBACKS:
132         icon->use_default_fallbacks = g_value_get_boolean (value);
133         break;
134
135       default:
136         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
137     }
138 }
139
140 static void
141 g_themed_icon_constructed (GObject *object)
142 {
143   GThemedIcon *themed = G_THEMED_ICON (object);
144
145   g_return_if_fail (themed->names != NULL && themed->names[0] != NULL);
146
147   if (themed->use_default_fallbacks)
148     {
149       int i = 0, dashes = 0;
150       const char *p;
151       char *dashp;
152       char *last;
153
154       p = themed->names[0];
155       while (*p)
156         {
157           if (*p == '-')
158             dashes++;
159           p++;
160         }
161
162       last = g_strdup (themed->names[0]);
163
164       g_strfreev (themed->names);
165
166       themed->names = g_new (char *, dashes + 1 + 1);
167       themed->names[i++] = last;
168
169       while ((dashp = strrchr (last, '-')) != NULL)
170         themed->names[i++] = last = g_strndup (last, dashp - last);
171
172       themed->names[i++] = NULL;
173     }
174 }
175
176 static void
177 g_themed_icon_finalize (GObject *object)
178 {
179   GThemedIcon *themed;
180
181   themed = G_THEMED_ICON (object);
182
183   g_strfreev (themed->names);
184
185   G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize (object);
186 }
187
188 static void
189 g_themed_icon_class_init (GThemedIconClass *klass)
190 {
191   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
192   
193   gobject_class->finalize = g_themed_icon_finalize;
194   gobject_class->constructed = g_themed_icon_constructed;
195   gobject_class->set_property = g_themed_icon_set_property;
196   gobject_class->get_property = g_themed_icon_get_property;
197
198   /**
199    * GThemedIcon:name:
200    *
201    * The icon name.
202    */
203   g_object_class_install_property (gobject_class, PROP_NAME,
204                                    g_param_spec_string ("name",
205                                                         _("name"),
206                                                         _("The name of the icon"),
207                                                         NULL,
208                                                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
209
210   /**
211    * GThemedIcon:names:
212    *
213    * A %NULL-terminated array of icon names.
214    */
215   g_object_class_install_property (gobject_class, PROP_NAMES,
216                                    g_param_spec_boxed ("names",
217                                                        _("names"),
218                                                        _("An array containing the icon names"),
219                                                        G_TYPE_STRV,
220                                                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
221
222   /**
223    * GThemedIcon:use-default-fallbacks:
224    *
225    * Whether to use the default fallbacks found by shortening the icon name 
226    * at '-' characters. If the "names" array has more than one element, 
227    * ignores any past the first.
228    *
229    * For example, if the icon name was "gnome-dev-cdrom-audio", the array 
230    * would become
231    * |[
232    * {
233    *   "gnome-dev-cdrom-audio",
234    *   "gnome-dev-cdrom",
235    *   "gnome-dev",
236    *   "gnome",
237    *   NULL
238    * };
239    * ]|
240    */
241   g_object_class_install_property (gobject_class, PROP_USE_DEFAULT_FALLBACKS,
242                                    g_param_spec_boolean ("use-default-fallbacks",
243                                                          _("use default fallbacks"),
244                                                          _("Whether to use default fallbacks found by shortening the name at '-' characters. Ignores names after the first if multiple names are given."),
245                                                          FALSE,
246                                                          G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
247 }
248
249 static void
250 g_themed_icon_init (GThemedIcon *themed)
251 {
252   themed->names = NULL;
253 }
254
255 /**
256  * g_themed_icon_new:
257  * @iconname: a string containing an icon name.
258  * 
259  * Creates a new themed icon for @iconname.
260  * 
261  * Returns: a new #GThemedIcon.
262  **/
263 GIcon *
264 g_themed_icon_new (const char *iconname)
265 {
266   g_return_val_if_fail (iconname != NULL, NULL);
267
268   return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, NULL));
269 }
270
271 /**
272  * g_themed_icon_new_from_names:
273  * @iconnames: an array of strings containing icon names.
274  * @len: the length of the @iconnames array, or -1 if @iconnames is 
275  *     %NULL-terminated
276  * 
277  * Creates a new themed icon for @iconnames.
278  * 
279  * Returns: a new #GThemedIcon
280  **/
281 GIcon *
282 g_themed_icon_new_from_names (char **iconnames,
283                               int    len)
284 {
285   GIcon *icon = icon;
286
287   g_return_val_if_fail (iconnames != NULL, NULL);
288
289   if (len >= 0)
290     {
291       char **names;
292       int i;
293
294       names = g_new (char *, len + 1);
295
296       for (i = 0; i < len; i++)
297         names[i] = iconnames[i];
298
299       names[i] = NULL;
300
301       icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", names, NULL));
302
303       g_free (names);
304     }
305   else
306     icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", iconnames, NULL));
307
308   return icon;
309 }
310
311 /**
312  * g_themed_icon_new_with_default_fallbacks:
313  * @iconname: a string containing an icon name
314  *
315  * Creates a new themed icon for @iconname, and all the names
316  * that can be created by shortening @iconname at '-' characters.
317  * 
318  * In the following example, @icon1 and @icon2 are equivalent:
319  * |[
320  * const char *names[] = { 
321  *   "gnome-dev-cdrom-audio",
322  *   "gnome-dev-cdrom",
323  *   "gnome-dev",
324  *   "gnome"
325  * };
326  *
327  * icon1 = g_themed_icon_new_from_names (names, 4);
328  * icon2 = g_themed_icon_new_with_default_fallbacks ("gnome-dev-cdrom-audio");
329  * ]|
330  *
331  * Returns: a new #GThemedIcon.
332  */
333 GIcon *
334 g_themed_icon_new_with_default_fallbacks (const char *iconname)
335 {
336   g_return_val_if_fail (iconname != NULL, NULL);
337
338   return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, "use-default-fallbacks", TRUE, NULL));
339 }
340
341
342 /**
343  * g_themed_icon_get_names:
344  * @icon: a #GThemedIcon.
345  * 
346  * Gets the names of icons from within @icon.
347  * 
348  * Returns: a list of icon names.
349  **/
350 const char * const *
351 g_themed_icon_get_names (GThemedIcon *icon)
352 {
353   g_return_val_if_fail (G_IS_THEMED_ICON (icon), NULL);
354   return (const char * const *)icon->names;
355 }
356
357 /**
358  * g_themed_icon_append_name:
359  * @icon: a #GThemedIcon
360  * @iconname: name of icon to append to list of icons from within @icon.
361  *
362  * Append a name to the list of icons from within @icon.
363  *
364  * <note><para>
365  * Note that doing so invalidates the hash computed by prior calls
366  * to g_icon_hash().
367  * </para></note>
368  */
369 void
370 g_themed_icon_append_name (GThemedIcon *icon, 
371                            const char  *iconname)
372 {
373   guint num_names;
374
375   g_return_if_fail (G_IS_THEMED_ICON (icon));
376   g_return_if_fail (iconname != NULL);
377
378   num_names = g_strv_length (icon->names);
379   icon->names = g_realloc (icon->names, sizeof (char*) * (num_names + 2));
380   icon->names[num_names] = g_strdup (iconname);
381   icon->names[num_names + 1] = NULL;
382
383   g_object_notify (G_OBJECT (icon), "names");
384 }
385
386 /**
387  * g_themed_icon_prepend_name:
388  * @icon: a #GThemedIcon
389  * @iconname: name of icon to prepend to list of icons from within @icon.
390  *
391  * Prepend a name to the list of icons from within @icon.
392  *
393  * <note><para>
394  * Note that doing so invalidates the hash computed by prior calls
395  * to g_icon_hash().
396  * </para></note>
397  *
398  * Since: 2.18
399  */
400 void
401 g_themed_icon_prepend_name (GThemedIcon *icon, 
402                             const char  *iconname)
403 {
404   guint num_names;
405   gchar **names;
406   gint i;
407
408   g_return_if_fail (G_IS_THEMED_ICON (icon));
409   g_return_if_fail (iconname != NULL);
410
411   num_names = g_strv_length (icon->names);
412   names = g_new (char*, num_names + 2);
413   for (i = 0; icon->names[i]; i++)
414     names[i + 1] = icon->names[i];
415   names[0] = g_strdup (iconname);
416   names[num_names + 1] = NULL;
417
418   g_free (icon->names);
419   icon->names = names;
420
421   g_object_notify (G_OBJECT (icon), "names");
422 }
423
424 static guint
425 g_themed_icon_hash (GIcon *icon)
426 {
427   GThemedIcon *themed = G_THEMED_ICON (icon);
428   guint hash;
429   int i;
430
431   hash = 0;
432
433   for (i = 0; themed->names[i] != NULL; i++)
434     hash ^= g_str_hash (themed->names[i]);
435   
436   return hash;
437 }
438
439 static gboolean
440 g_themed_icon_equal (GIcon *icon1,
441                      GIcon *icon2)
442 {
443   GThemedIcon *themed1 = G_THEMED_ICON (icon1);
444   GThemedIcon *themed2 = G_THEMED_ICON (icon2);
445   int i;
446
447   for (i = 0; themed1->names[i] != NULL && themed2->names[i] != NULL; i++)
448     {
449       if (!g_str_equal (themed1->names[i], themed2->names[i]))
450         return FALSE;
451     }
452
453   return themed1->names[i] == NULL && themed2->names[i] == NULL;
454 }
455
456 static void
457 g_themed_icon_icon_iface_init (GIconIface *iface)
458 {
459   iface->hash = g_themed_icon_hash;
460   iface->equal = g_themed_icon_equal;
461 }
462
463 #define __G_THEMED_ICON_C__
464 #include "gioaliasdef.c"