e642c3e9a641f10436daf57bf8deb65747801101
[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
101   switch (prop_id)
102     {
103       case PROP_NAME:
104         if (icon->names)
105           g_strfreev (icon->names);
106         icon->names = g_new (char *, 2);
107         icon->names[0] = g_value_dup_string (value);
108         icon->names[1] = NULL;
109         break;
110
111       case PROP_NAMES:
112         if (icon->names)
113           g_strfreev (icon->names);
114         icon->names = g_value_dup_boxed (value);
115         break;
116
117       case PROP_USE_DEFAULT_FALLBACKS:
118         icon->use_default_fallbacks = g_value_get_boolean (value);
119         break;
120
121       default:
122         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
123     }
124 }
125
126 static void
127 g_themed_icon_constructed (GObject *object)
128 {
129   GThemedIcon *themed = G_THEMED_ICON (object);
130
131   g_return_if_fail (themed->names != NULL && themed->names[0] != NULL);
132
133   if (themed->use_default_fallbacks)
134     {
135       int i = 0, dashes = 0;
136       const char *p;
137       char *dashp;
138       char *last;
139
140       p = themed->names[0];
141       while (*p)
142         {
143           if (*p == '-')
144             dashes++;
145           p++;
146         }
147
148       last = g_strdup (themed->names[0]);
149
150       g_strfreev (themed->names);
151
152       themed->names = g_new (char *, dashes + 1 + 1);
153       themed->names[i++] = last;
154
155       while ((dashp = strrchr (last, '-')) != NULL)
156         themed->names[i++] = last = g_strndup (last, dashp - last);
157
158       themed->names[i++] = NULL;
159     }
160 }
161
162 static void
163 g_themed_icon_finalize (GObject *object)
164 {
165   GThemedIcon *themed;
166
167   themed = G_THEMED_ICON (object);
168
169   g_strfreev (themed->names);
170   
171   if (G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize)
172     (*G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize) (object);
173 }
174
175 static void
176 g_themed_icon_class_init (GThemedIconClass *klass)
177 {
178   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
179   
180   gobject_class->finalize = g_themed_icon_finalize;
181   gobject_class->constructed = g_themed_icon_constructed;
182   gobject_class->set_property = g_themed_icon_set_property;
183   gobject_class->get_property = g_themed_icon_get_property;
184
185   /**
186    * GThemedIcon:name:
187    *
188    * The icon name.
189    */
190   g_object_class_install_property (gobject_class, PROP_NAMES,
191                                    g_param_spec_string ("name",
192                                                         _("name"),
193                                                         _("The name of the icon"),
194                                                         NULL,
195                                                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
196
197   /**
198    * GThemedIcon:names:
199    *
200    * A %NULL-terminated array of icon names.
201    */
202   g_object_class_install_property (gobject_class, PROP_NAMES,
203                                    g_param_spec_boxed ("names",
204                                                        _("names"),
205                                                        _("An array containing the icon names"),
206                                                        G_TYPE_STRV,
207                                                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
208
209   /**
210    * GThemedIcon:use-default-fallbacks:
211    *
212    * Whether to use the default fallbacks found by shortening the icon name 
213    * at '-' characters. If the "names" array has more than one element, 
214    * ignores any past the first.
215    *
216    * For example, if the icon name was "gnome-dev-cdrom-audio", the array 
217    * would become
218    * |[
219    * {
220    *   "gnome-dev-cdrom-audio",
221    *   "gnome-dev-cdrom",
222    *   "gnome-dev",
223    *   "gnome",
224    *   NULL
225    * };
226    * ]|
227    */
228   g_object_class_install_property (gobject_class, PROP_USE_DEFAULT_FALLBACKS,
229                                    g_param_spec_boolean ("use-default-fallbacks",
230                                                          _("use default fallbacks"),
231                                                          _("Whether to use default fallbacks found by shortening the name at '-' characters. Ignores names after the first if multiple names are given."),
232                                                          FALSE,
233                                                          G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
234 }
235
236 static void
237 g_themed_icon_init (GThemedIcon *themed)
238 {
239   themed->names = NULL;
240 }
241
242 /**
243  * g_themed_icon_new:
244  * @iconname: a string containing an icon name.
245  * 
246  * Creates a new themed icon for @iconname.
247  * 
248  * Returns: a new #GThemedIcon.
249  **/
250 GIcon *
251 g_themed_icon_new (const char *iconname)
252 {
253   g_return_val_if_fail (iconname != NULL, NULL);
254
255   return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, NULL));
256 }
257
258 /**
259  * g_themed_icon_new_from_names:
260  * @iconnames: an array of strings containing icon names.
261  * @len: the length of the @iconnames array, or -1 if @iconnames is 
262  *     %NULL-terminated
263  * 
264  * Creates a new themed icon for @iconnames.
265  * 
266  * Returns: a new #GThemedIcon
267  **/
268 GIcon *
269 g_themed_icon_new_from_names (char **iconnames,
270                               int    len)
271 {
272   GIcon *icon = icon;
273
274   g_return_val_if_fail (iconnames != NULL, NULL);
275
276   if (len >= 0)
277     {
278       char **names;
279       int i;
280
281       names = g_malloc (len + 1);
282
283       for (i = 0; i < len; i++)
284         names[i] = iconnames[i];
285
286       names[i] = NULL;
287
288       icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", names, NULL));
289
290       g_free (names);
291     }
292   else
293     icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", iconnames, NULL));
294
295   return icon;
296 }
297
298 /**
299  * g_themed_icon_new_with_default_fallbacks:
300  * @iconname: a string containing an icon name
301  *
302  * Creates a new themed icon for @iconname, and all the names
303  * that can be created by shortening @iconname at '-' characters.
304  * 
305  * In the following example, @icon1 and @icon2 are equivalent:
306  * |[
307  * const char *names[] = { 
308  *   "gnome-dev-cdrom-audio",
309  *   "gnome-dev-cdrom",
310  *   "gnome-dev",
311  *   "gnome"
312  * };
313  *
314  * icon1 = g_themed_icon_new_from_names (names, 4);
315  * icon2 = g_themed_icon_new_with_default_fallbacks ("gnome-dev-cdrom-audio");
316  * ]|
317  *
318  * Returns: a new #GThemedIcon.
319  */
320 GIcon *
321 g_themed_icon_new_with_default_fallbacks (const char *iconname)
322 {
323   g_return_val_if_fail (iconname != NULL, NULL);
324
325   return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, "use-default-fallbacks", TRUE, NULL));
326 }
327
328
329 /**
330  * g_themed_icon_get_names:
331  * @icon: a #GThemedIcon.
332  * 
333  * Gets the names of icons from within @icon.
334  * 
335  * Returns: a list of icon names.
336  **/
337 const char * const *
338 g_themed_icon_get_names (GThemedIcon *icon)
339 {
340   g_return_val_if_fail (G_IS_THEMED_ICON (icon), NULL);
341   return (const char * const *)icon->names;
342 }
343
344 /**
345  * g_themed_icon_append_name:
346  * @icon: a #GThemedIcon
347  * @iconname: name of icon to append to list of icons from within @icon.
348  *
349  * Append a name to the list of icons from within @icon.
350  *
351  * <note><para>
352  * Note that doing so invalidates the hash computed by prior calls
353  * to g_icon_hash().
354  * </para></note>
355  */
356 void
357 g_themed_icon_append_name (GThemedIcon *icon, const char *iconname)
358 {
359   guint num_names;
360
361   g_return_if_fail (G_IS_THEMED_ICON (icon));
362   g_return_if_fail (iconname != NULL);
363
364   num_names = g_strv_length (icon->names);
365   icon->names = g_realloc (icon->names, sizeof (char*) * (num_names + 2));
366   icon->names[num_names] = g_strdup (iconname);
367   icon->names[num_names + 1] = NULL;
368
369   g_object_notify (G_OBJECT (icon), "names");
370 }
371
372 static guint
373 g_themed_icon_hash (GIcon *icon)
374 {
375   GThemedIcon *themed = G_THEMED_ICON (icon);
376   guint hash;
377   int i;
378
379   hash = 0;
380
381   for (i = 0; themed->names[i] != NULL; i++)
382     hash ^= g_str_hash (themed->names[i]);
383   
384   return hash;
385 }
386
387 static gboolean
388 g_themed_icon_equal (GIcon *icon1,
389                      GIcon *icon2)
390 {
391   GThemedIcon *themed1 = G_THEMED_ICON (icon1);
392   GThemedIcon *themed2 = G_THEMED_ICON (icon2);
393   int i;
394
395   for (i = 0; themed1->names[i] != NULL && themed2->names[i] != NULL; i++)
396     {
397       if (!g_str_equal (themed1->names[i], themed2->names[i]))
398         return FALSE;
399     }
400
401   return themed1->names[i] == NULL && themed2->names[i] == NULL;
402 }
403
404 static void
405 g_themed_icon_icon_iface_init (GIconIface *iface)
406 {
407   iface->hash = g_themed_icon_hash;
408   iface->equal = g_themed_icon_equal;
409 }
410
411 #define __G_THEMED_ICON_C__
412 #include "gioaliasdef.c"