Make GSettingsSchemaKey public
[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 *, 2 * dashes + 3);
191           for (i = 0; names[i] != NULL; i++)
192             {
193               themed->names[i] = g_strconcat (names[i], "-symbolic", NULL);
194               themed->names[dashes + 1 + i] = names[i];
195             }
196
197           themed->names[dashes + 1 + i] = NULL;
198           g_free (names);
199         }
200       else
201         {
202           themed->names = names;
203         }
204     }
205 }
206
207 static void
208 g_themed_icon_finalize (GObject *object)
209 {
210   GThemedIcon *themed;
211
212   themed = G_THEMED_ICON (object);
213
214   g_strfreev (themed->names);
215
216   G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize (object);
217 }
218
219 static void
220 g_themed_icon_class_init (GThemedIconClass *klass)
221 {
222   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
223   
224   gobject_class->finalize = g_themed_icon_finalize;
225   gobject_class->constructed = g_themed_icon_constructed;
226   gobject_class->set_property = g_themed_icon_set_property;
227   gobject_class->get_property = g_themed_icon_get_property;
228
229   /**
230    * GThemedIcon:name:
231    *
232    * The icon name.
233    */
234   g_object_class_install_property (gobject_class, PROP_NAME,
235                                    g_param_spec_string ("name",
236                                                         P_("name"),
237                                                         P_("The name of the icon"),
238                                                         NULL,
239                                                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
240
241   /**
242    * GThemedIcon:names:
243    *
244    * A %NULL-terminated array of icon names.
245    */
246   g_object_class_install_property (gobject_class, PROP_NAMES,
247                                    g_param_spec_boxed ("names",
248                                                        P_("names"),
249                                                        P_("An array containing the icon names"),
250                                                        G_TYPE_STRV,
251                                                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
252
253   /**
254    * GThemedIcon:use-default-fallbacks:
255    *
256    * Whether to use the default fallbacks found by shortening the icon name 
257    * at '-' characters. If the "names" array has more than one element, 
258    * ignores any past the first.
259    *
260    * For example, if the icon name was "gnome-dev-cdrom-audio", the array 
261    * would become
262    * |[
263    * {
264    *   "gnome-dev-cdrom-audio",
265    *   "gnome-dev-cdrom",
266    *   "gnome-dev",
267    *   "gnome",
268    *   NULL
269    * };
270    * ]|
271    */
272   g_object_class_install_property (gobject_class, PROP_USE_DEFAULT_FALLBACKS,
273                                    g_param_spec_boolean ("use-default-fallbacks",
274                                                          P_("use default fallbacks"),
275                                                          P_("Whether to use default fallbacks found by shortening the name at '-' characters. Ignores names after the first if multiple names are given."),
276                                                          FALSE,
277                                                          G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
278 }
279
280 static void
281 g_themed_icon_init (GThemedIcon *themed)
282 {
283   themed->names = NULL;
284 }
285
286 /**
287  * g_themed_icon_new:
288  * @iconname: a string containing an icon name.
289  * 
290  * Creates a new themed icon for @iconname.
291  * 
292  * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon.
293  **/
294 GIcon *
295 g_themed_icon_new (const char *iconname)
296 {
297   g_return_val_if_fail (iconname != NULL, NULL);
298
299   return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, NULL));
300 }
301
302 /**
303  * g_themed_icon_new_from_names:
304  * @iconnames: (array length=len): an array of strings containing icon names.
305  * @len: the length of the @iconnames array, or -1 if @iconnames is 
306  *     %NULL-terminated
307  * 
308  * Creates a new themed icon for @iconnames.
309  * 
310  * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon
311  **/
312 GIcon *
313 g_themed_icon_new_from_names (char **iconnames,
314                               int    len)
315 {
316   GIcon *icon;
317
318   g_return_val_if_fail (iconnames != NULL, NULL);
319
320   if (len >= 0)
321     {
322       char **names;
323       int i;
324
325       names = g_new (char *, len + 1);
326
327       for (i = 0; i < len; i++)
328         names[i] = iconnames[i];
329
330       names[i] = NULL;
331
332       icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", names, NULL));
333
334       g_free (names);
335     }
336   else
337     icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", iconnames, NULL));
338
339   return icon;
340 }
341
342 /**
343  * g_themed_icon_new_with_default_fallbacks:
344  * @iconname: a string containing an icon name
345  *
346  * Creates a new themed icon for @iconname, and all the names
347  * that can be created by shortening @iconname at '-' characters.
348  * 
349  * In the following example, @icon1 and @icon2 are equivalent:
350  * |[
351  * const char *names[] = { 
352  *   "gnome-dev-cdrom-audio",
353  *   "gnome-dev-cdrom",
354  *   "gnome-dev",
355  *   "gnome"
356  * };
357  *
358  * icon1 = g_themed_icon_new_from_names (names, 4);
359  * icon2 = g_themed_icon_new_with_default_fallbacks ("gnome-dev-cdrom-audio");
360  * ]|
361  *
362  * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon.
363  */
364 GIcon *
365 g_themed_icon_new_with_default_fallbacks (const char *iconname)
366 {
367   g_return_val_if_fail (iconname != NULL, NULL);
368
369   return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, "use-default-fallbacks", TRUE, NULL));
370 }
371
372
373 /**
374  * g_themed_icon_get_names:
375  * @icon: a #GThemedIcon.
376  *
377  * Gets the names of icons from within @icon.
378  *
379  * Returns: (transfer none): a list of icon names.
380  */
381 const char * const *
382 g_themed_icon_get_names (GThemedIcon *icon)
383 {
384   g_return_val_if_fail (G_IS_THEMED_ICON (icon), NULL);
385   return (const char * const *)icon->names;
386 }
387
388 /**
389  * g_themed_icon_append_name:
390  * @icon: a #GThemedIcon
391  * @iconname: name of icon to append to list of icons from within @icon.
392  *
393  * Append a name to the list of icons from within @icon.
394  *
395  * <note><para>
396  * Note that doing so invalidates the hash computed by prior calls
397  * to g_icon_hash().
398  * </para></note>
399  */
400 void
401 g_themed_icon_append_name (GThemedIcon *icon, 
402                            const char  *iconname)
403 {
404   guint num_names;
405
406   g_return_if_fail (G_IS_THEMED_ICON (icon));
407   g_return_if_fail (iconname != NULL);
408
409   num_names = g_strv_length (icon->names);
410   icon->names = g_realloc (icon->names, sizeof (char*) * (num_names + 2));
411   icon->names[num_names] = g_strdup (iconname);
412   icon->names[num_names + 1] = NULL;
413
414   g_object_notify (G_OBJECT (icon), "names");
415 }
416
417 /**
418  * g_themed_icon_prepend_name:
419  * @icon: a #GThemedIcon
420  * @iconname: name of icon to prepend to list of icons from within @icon.
421  *
422  * Prepend a name to the list of icons from within @icon.
423  *
424  * <note><para>
425  * Note that doing so invalidates the hash computed by prior calls
426  * to g_icon_hash().
427  * </para></note>
428  *
429  * Since: 2.18
430  */
431 void
432 g_themed_icon_prepend_name (GThemedIcon *icon, 
433                             const char  *iconname)
434 {
435   guint num_names;
436   gchar **names;
437   gint i;
438
439   g_return_if_fail (G_IS_THEMED_ICON (icon));
440   g_return_if_fail (iconname != NULL);
441
442   num_names = g_strv_length (icon->names);
443   names = g_new (char*, num_names + 2);
444   for (i = 0; icon->names[i]; i++)
445     names[i + 1] = icon->names[i];
446   names[0] = g_strdup (iconname);
447   names[num_names + 1] = NULL;
448
449   g_free (icon->names);
450   icon->names = names;
451
452   g_object_notify (G_OBJECT (icon), "names");
453 }
454
455 static guint
456 g_themed_icon_hash (GIcon *icon)
457 {
458   GThemedIcon *themed = G_THEMED_ICON (icon);
459   guint hash;
460   int i;
461
462   hash = 0;
463
464   for (i = 0; themed->names[i] != NULL; i++)
465     hash ^= g_str_hash (themed->names[i]);
466   
467   return hash;
468 }
469
470 static gboolean
471 g_themed_icon_equal (GIcon *icon1,
472                      GIcon *icon2)
473 {
474   GThemedIcon *themed1 = G_THEMED_ICON (icon1);
475   GThemedIcon *themed2 = G_THEMED_ICON (icon2);
476   int i;
477
478   for (i = 0; themed1->names[i] != NULL && themed2->names[i] != NULL; i++)
479     {
480       if (!g_str_equal (themed1->names[i], themed2->names[i]))
481         return FALSE;
482     }
483
484   return themed1->names[i] == NULL && themed2->names[i] == NULL;
485 }
486
487
488 static gboolean
489 g_themed_icon_to_tokens (GIcon *icon,
490                          GPtrArray *tokens,
491                          gint  *out_version)
492 {
493   GThemedIcon *themed_icon = G_THEMED_ICON (icon);
494   int n;
495
496   g_return_val_if_fail (out_version != NULL, FALSE);
497
498   *out_version = 0;
499
500   for (n = 0; themed_icon->names[n] != NULL; n++)
501     g_ptr_array_add (tokens,
502                      g_strdup (themed_icon->names[n]));
503   
504   return TRUE;
505 }
506
507 static GIcon *
508 g_themed_icon_from_tokens (gchar  **tokens,
509                            gint     num_tokens,
510                            gint     version,
511                            GError **error)
512 {
513   GIcon *icon;
514   gchar **names;
515   int n;
516
517   icon = NULL;
518
519   if (version != 0)
520     {
521       g_set_error (error,
522                    G_IO_ERROR,
523                    G_IO_ERROR_INVALID_ARGUMENT,
524                    _("Can't handle version %d of GThemedIcon encoding"),
525                    version);
526       goto out;
527     }
528   
529   names = g_new0 (gchar *, num_tokens + 1);
530   for (n = 0; n < num_tokens; n++)
531     names[n] = tokens[n];
532   names[n] = NULL;
533
534   icon = g_themed_icon_new_from_names (names, num_tokens);
535   g_free (names);
536
537  out:
538   return icon;
539 }
540
541 static GVariant *
542 g_themed_icon_serialize (GIcon *icon)
543 {
544   GThemedIcon *themed_icon = G_THEMED_ICON (icon);
545
546   return g_variant_new ("(sv)", "themed", g_variant_new ("^as", themed_icon->names));
547 }
548
549 static void
550 g_themed_icon_icon_iface_init (GIconIface *iface)
551 {
552   iface->hash = g_themed_icon_hash;
553   iface->equal = g_themed_icon_equal;
554   iface->to_tokens = g_themed_icon_to_tokens;
555   iface->from_tokens = g_themed_icon_from_tokens;
556   iface->serialize = g_themed_icon_serialize;
557 }