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