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