Use g_return_val_if_fail() for developer-only messages
[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 "gioerror.h"
30 #include "glibintl.h"
31
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       case PROP_USE_DEFAULT_FALLBACKS:
90         g_value_set_boolean (value, icon->use_default_fallbacks);
91         break;
92
93       default:
94         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
95     }
96 }
97
98 static void
99 g_themed_icon_set_property (GObject      *object,
100                             guint         prop_id,
101                             const GValue *value,
102                             GParamSpec   *pspec)
103 {
104   GThemedIcon *icon = G_THEMED_ICON (object);
105   gchar **names;
106   const gchar *name;
107
108   switch (prop_id)
109     {
110       case PROP_NAME:
111         name = g_value_get_string (value);
112
113         if (!name)
114           break;
115
116         if (icon->names)
117           g_strfreev (icon->names);
118
119         icon->names = g_new (char *, 2);
120         icon->names[0] = g_strdup (name);
121         icon->names[1] = NULL;
122         break;
123
124       case PROP_NAMES:
125         names = g_value_dup_boxed (value);
126
127         if (!names)
128           break;
129
130         if (icon->names)
131           g_strfreev (icon->names);
132
133         icon->names = names;
134         break;
135
136       case PROP_USE_DEFAULT_FALLBACKS:
137         icon->use_default_fallbacks = g_value_get_boolean (value);
138         break;
139
140       default:
141         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142     }
143 }
144
145 static void
146 g_themed_icon_constructed (GObject *object)
147 {
148   GThemedIcon *themed = G_THEMED_ICON (object);
149
150   g_return_if_fail (themed->names != NULL && themed->names[0] != NULL);
151
152   if (themed->use_default_fallbacks)
153     {
154       int i = 0, dashes = 0;
155       const char *p;
156       char *dashp;
157       char *last;
158       gboolean is_symbolic;
159       char *name;
160       char **names;
161
162       is_symbolic = g_str_has_suffix (themed->names[0], "-symbolic");
163       if (is_symbolic)
164         name = g_strndup (themed->names[0], strlen (themed->names[0]) - 9);
165       else
166         name = g_strdup (themed->names[0]);
167
168       p = name;
169       while (*p)
170         {
171           if (*p == '-')
172             dashes++;
173           p++;
174         }
175
176       last = name;
177
178       g_strfreev (themed->names);
179
180       names = g_new (char *, dashes + 1 + 1);
181       names[i++] = last;
182
183       while ((dashp = strrchr (last, '-')) != NULL)
184         names[i++] = last = g_strndup (last, dashp - last);
185
186       names[i++] = NULL;
187
188       if (is_symbolic)
189         {
190           themed->names = g_new (char *, dashes + 1 + 1);
191           for (i = 0; names[i] != NULL; i++)
192             themed->names[i] = g_strconcat (names[i], "-symbolic", NULL);
193
194           themed->names[i] = NULL;
195           g_strfreev (names);
196         }
197       else
198         {
199           themed->names = names;
200         }
201     }
202 }
203
204 static void
205 g_themed_icon_finalize (GObject *object)
206 {
207   GThemedIcon *themed;
208
209   themed = G_THEMED_ICON (object);
210
211   g_strfreev (themed->names);
212
213   G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize (object);
214 }
215
216 static void
217 g_themed_icon_class_init (GThemedIconClass *klass)
218 {
219   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
220   
221   gobject_class->finalize = g_themed_icon_finalize;
222   gobject_class->constructed = g_themed_icon_constructed;
223   gobject_class->set_property = g_themed_icon_set_property;
224   gobject_class->get_property = g_themed_icon_get_property;
225
226   /**
227    * GThemedIcon:name:
228    *
229    * The icon name.
230    */
231   g_object_class_install_property (gobject_class, PROP_NAME,
232                                    g_param_spec_string ("name",
233                                                         P_("name"),
234                                                         P_("The name of the icon"),
235                                                         NULL,
236                                                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
237
238   /**
239    * GThemedIcon:names:
240    *
241    * A %NULL-terminated array of icon names.
242    */
243   g_object_class_install_property (gobject_class, PROP_NAMES,
244                                    g_param_spec_boxed ("names",
245                                                        P_("names"),
246                                                        P_("An array containing the icon names"),
247                                                        G_TYPE_STRV,
248                                                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
249
250   /**
251    * GThemedIcon:use-default-fallbacks:
252    *
253    * Whether to use the default fallbacks found by shortening the icon name 
254    * at '-' characters. If the "names" array has more than one element, 
255    * ignores any past the first.
256    *
257    * For example, if the icon name was "gnome-dev-cdrom-audio", the array 
258    * would become
259    * |[
260    * {
261    *   "gnome-dev-cdrom-audio",
262    *   "gnome-dev-cdrom",
263    *   "gnome-dev",
264    *   "gnome",
265    *   NULL
266    * };
267    * ]|
268    */
269   g_object_class_install_property (gobject_class, PROP_USE_DEFAULT_FALLBACKS,
270                                    g_param_spec_boolean ("use-default-fallbacks",
271                                                          P_("use default fallbacks"),
272                                                          P_("Whether to use default fallbacks found by shortening the name at '-' characters. Ignores names after the first if multiple names are given."),
273                                                          FALSE,
274                                                          G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
275 }
276
277 static void
278 g_themed_icon_init (GThemedIcon *themed)
279 {
280   themed->names = NULL;
281 }
282
283 /**
284  * g_themed_icon_new:
285  * @iconname: a string containing an icon name.
286  * 
287  * Creates a new themed icon for @iconname.
288  * 
289  * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon.
290  **/
291 GIcon *
292 g_themed_icon_new (const char *iconname)
293 {
294   g_return_val_if_fail (iconname != NULL, NULL);
295
296   return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, NULL));
297 }
298
299 /**
300  * g_themed_icon_new_from_names:
301  * @iconnames: (array length=len): an array of strings containing icon names.
302  * @len: the length of the @iconnames array, or -1 if @iconnames is 
303  *     %NULL-terminated
304  * 
305  * Creates a new themed icon for @iconnames.
306  * 
307  * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon
308  **/
309 GIcon *
310 g_themed_icon_new_from_names (char **iconnames,
311                               int    len)
312 {
313   GIcon *icon;
314
315   g_return_val_if_fail (iconnames != NULL, NULL);
316
317   if (len >= 0)
318     {
319       char **names;
320       int i;
321
322       names = g_new (char *, len + 1);
323
324       for (i = 0; i < len; i++)
325         names[i] = iconnames[i];
326
327       names[i] = NULL;
328
329       icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", names, NULL));
330
331       g_free (names);
332     }
333   else
334     icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", iconnames, NULL));
335
336   return icon;
337 }
338
339 /**
340  * g_themed_icon_new_with_default_fallbacks:
341  * @iconname: a string containing an icon name
342  *
343  * Creates a new themed icon for @iconname, and all the names
344  * that can be created by shortening @iconname at '-' characters.
345  * 
346  * In the following example, @icon1 and @icon2 are equivalent:
347  * |[
348  * const char *names[] = { 
349  *   "gnome-dev-cdrom-audio",
350  *   "gnome-dev-cdrom",
351  *   "gnome-dev",
352  *   "gnome"
353  * };
354  *
355  * icon1 = g_themed_icon_new_from_names (names, 4);
356  * icon2 = g_themed_icon_new_with_default_fallbacks ("gnome-dev-cdrom-audio");
357  * ]|
358  *
359  * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon.
360  */
361 GIcon *
362 g_themed_icon_new_with_default_fallbacks (const char *iconname)
363 {
364   g_return_val_if_fail (iconname != NULL, NULL);
365
366   return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, "use-default-fallbacks", TRUE, NULL));
367 }
368
369
370 /**
371  * g_themed_icon_get_names:
372  * @icon: a #GThemedIcon.
373  *
374  * Gets the names of icons from within @icon.
375  *
376  * Returns: (transfer none): a list of icon names.
377  */
378 const char * const *
379 g_themed_icon_get_names (GThemedIcon *icon)
380 {
381   g_return_val_if_fail (G_IS_THEMED_ICON (icon), NULL);
382   return (const char * const *)icon->names;
383 }
384
385 /**
386  * g_themed_icon_append_name:
387  * @icon: a #GThemedIcon
388  * @iconname: name of icon to append to list of icons from within @icon.
389  *
390  * Append a name to the list of icons from within @icon.
391  *
392  * <note><para>
393  * Note that doing so invalidates the hash computed by prior calls
394  * to g_icon_hash().
395  * </para></note>
396  */
397 void
398 g_themed_icon_append_name (GThemedIcon *icon, 
399                            const char  *iconname)
400 {
401   guint num_names;
402
403   g_return_if_fail (G_IS_THEMED_ICON (icon));
404   g_return_if_fail (iconname != NULL);
405
406   num_names = g_strv_length (icon->names);
407   icon->names = g_realloc (icon->names, sizeof (char*) * (num_names + 2));
408   icon->names[num_names] = g_strdup (iconname);
409   icon->names[num_names + 1] = NULL;
410
411   g_object_notify (G_OBJECT (icon), "names");
412 }
413
414 /**
415  * g_themed_icon_prepend_name:
416  * @icon: a #GThemedIcon
417  * @iconname: name of icon to prepend to list of icons from within @icon.
418  *
419  * Prepend a name to the list of icons from within @icon.
420  *
421  * <note><para>
422  * Note that doing so invalidates the hash computed by prior calls
423  * to g_icon_hash().
424  * </para></note>
425  *
426  * Since: 2.18
427  */
428 void
429 g_themed_icon_prepend_name (GThemedIcon *icon, 
430                             const char  *iconname)
431 {
432   guint num_names;
433   gchar **names;
434   gint i;
435
436   g_return_if_fail (G_IS_THEMED_ICON (icon));
437   g_return_if_fail (iconname != NULL);
438
439   num_names = g_strv_length (icon->names);
440   names = g_new (char*, num_names + 2);
441   for (i = 0; icon->names[i]; i++)
442     names[i + 1] = icon->names[i];
443   names[0] = g_strdup (iconname);
444   names[num_names + 1] = NULL;
445
446   g_free (icon->names);
447   icon->names = names;
448
449   g_object_notify (G_OBJECT (icon), "names");
450 }
451
452 static guint
453 g_themed_icon_hash (GIcon *icon)
454 {
455   GThemedIcon *themed = G_THEMED_ICON (icon);
456   guint hash;
457   int i;
458
459   hash = 0;
460
461   for (i = 0; themed->names[i] != NULL; i++)
462     hash ^= g_str_hash (themed->names[i]);
463   
464   return hash;
465 }
466
467 static gboolean
468 g_themed_icon_equal (GIcon *icon1,
469                      GIcon *icon2)
470 {
471   GThemedIcon *themed1 = G_THEMED_ICON (icon1);
472   GThemedIcon *themed2 = G_THEMED_ICON (icon2);
473   int i;
474
475   for (i = 0; themed1->names[i] != NULL && themed2->names[i] != NULL; i++)
476     {
477       if (!g_str_equal (themed1->names[i], themed2->names[i]))
478         return FALSE;
479     }
480
481   return themed1->names[i] == NULL && themed2->names[i] == NULL;
482 }
483
484
485 static gboolean
486 g_themed_icon_to_tokens (GIcon *icon,
487                          GPtrArray *tokens,
488                          gint  *out_version)
489 {
490   GThemedIcon *themed_icon = G_THEMED_ICON (icon);
491   int n;
492
493   g_return_val_if_fail (out_version != NULL, FALSE);
494
495   *out_version = 0;
496
497   for (n = 0; themed_icon->names[n] != NULL; n++)
498     g_ptr_array_add (tokens,
499                      g_strdup (themed_icon->names[n]));
500   
501   return TRUE;
502 }
503
504 static GIcon *
505 g_themed_icon_from_tokens (gchar  **tokens,
506                            gint     num_tokens,
507                            gint     version,
508                            GError **error)
509 {
510   GIcon *icon;
511   gchar **names;
512   int n;
513
514   g_return_val_if_fail (version == 0, NULL);
515   
516   names = g_new0 (gchar *, num_tokens + 1);
517   for (n = 0; n < num_tokens; n++)
518     names[n] = tokens[n];
519   names[n] = NULL;
520
521   icon = g_themed_icon_new_from_names (names, num_tokens);
522   g_free (names);
523
524   return icon;
525 }
526
527 static GVariant *
528 g_themed_icon_serialize (GIcon *icon)
529 {
530   GThemedIcon *themed_icon = G_THEMED_ICON (icon);
531
532   return g_variant_new ("(sv)", "themed", g_variant_new ("^as", themed_icon->names));
533 }
534
535 static void
536 g_themed_icon_icon_iface_init (GIconIface *iface)
537 {
538   iface->hash = g_themed_icon_hash;
539   iface->equal = g_themed_icon_equal;
540   iface->to_tokens = g_themed_icon_to_tokens;
541   iface->from_tokens = g_themed_icon_from_tokens;
542   iface->serialize = g_themed_icon_serialize;
543 }