GSettings: small internal refactor
[glib.git] / gio / gemblemedicon.c
blob556fbe80809daaffd26feac7f745dbaea8bf7300
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
4 *
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Matthias Clasen <mclasen@redhat.com>
23 * Clemens N. Buss <cebuzz@gmail.com>
26 #include <config.h>
28 #include <string.h>
30 #include "gemblemedicon.h"
31 #include "glibintl.h"
32 #include "gioerror.h"
35 /**
36 * SECTION:gemblemedicon
37 * @short_description: Icon with emblems
38 * @include: gio/gio.h
39 * @see_also: #GIcon, #GLoadableIcon, #GThemedIcon, #GEmblem
41 * #GEmblemedIcon is an implementation of #GIcon that supports
42 * adding an emblem to an icon. Adding multiple emblems to an
43 * icon is ensured via g_emblemed_icon_add_emblem().
45 * Note that #GEmblemedIcon allows no control over the position
46 * of the emblems. See also #GEmblem for more information.
47 **/
49 enum {
50 PROP_GICON = 1,
51 NUM_PROPERTIES
54 struct _GEmblemedIconPrivate {
55 GIcon *icon;
56 GList *emblems;
59 static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
61 static void g_emblemed_icon_icon_iface_init (GIconIface *iface);
63 G_DEFINE_TYPE_WITH_CODE (GEmblemedIcon, g_emblemed_icon, G_TYPE_OBJECT,
64 G_ADD_PRIVATE (GEmblemedIcon)
65 G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
66 g_emblemed_icon_icon_iface_init))
69 static void
70 g_emblemed_icon_finalize (GObject *object)
72 GEmblemedIcon *emblemed;
74 emblemed = G_EMBLEMED_ICON (object);
76 g_clear_object (&emblemed->priv->icon);
77 g_list_free_full (emblemed->priv->emblems, g_object_unref);
79 (*G_OBJECT_CLASS (g_emblemed_icon_parent_class)->finalize) (object);
82 static void
83 g_emblemed_icon_set_property (GObject *object,
84 guint property_id,
85 const GValue *value,
86 GParamSpec *pspec)
88 GEmblemedIcon *self = G_EMBLEMED_ICON (object);
90 switch (property_id)
92 case PROP_GICON:
93 self->priv->icon = g_value_dup_object (value);
94 break;
95 default:
96 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
97 break;
101 static void
102 g_emblemed_icon_get_property (GObject *object,
103 guint property_id,
104 GValue *value,
105 GParamSpec *pspec)
107 GEmblemedIcon *self = G_EMBLEMED_ICON (object);
109 switch (property_id)
111 case PROP_GICON:
112 g_value_set_object (value, self->priv->icon);
113 break;
114 default:
115 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
116 break;
120 static void
121 g_emblemed_icon_class_init (GEmblemedIconClass *klass)
123 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
125 gobject_class->finalize = g_emblemed_icon_finalize;
126 gobject_class->set_property = g_emblemed_icon_set_property;
127 gobject_class->get_property = g_emblemed_icon_get_property;
129 properties[PROP_GICON] =
130 g_param_spec_object ("gicon",
131 P_("The base GIcon"),
132 P_("The GIcon to attach emblems to"),
133 G_TYPE_ICON,
134 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
136 g_object_class_install_properties (gobject_class, NUM_PROPERTIES, properties);
139 static void
140 g_emblemed_icon_init (GEmblemedIcon *emblemed)
142 emblemed->priv = g_emblemed_icon_get_instance_private (emblemed);
146 * g_emblemed_icon_new:
147 * @icon: a #GIcon
148 * @emblem: (allow-none): a #GEmblem, or %NULL
150 * Creates a new emblemed icon for @icon with the emblem @emblem.
152 * Returns: (transfer full) (type GEmblemedIcon): a new #GIcon
154 * Since: 2.18
156 GIcon *
157 g_emblemed_icon_new (GIcon *icon,
158 GEmblem *emblem)
160 GEmblemedIcon *emblemed;
162 g_return_val_if_fail (G_IS_ICON (icon), NULL);
163 g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
165 emblemed = G_EMBLEMED_ICON (g_object_new (G_TYPE_EMBLEMED_ICON,
166 "gicon", icon,
167 NULL));
169 if (emblem != NULL)
170 g_emblemed_icon_add_emblem (emblemed, emblem);
172 return G_ICON (emblemed);
177 * g_emblemed_icon_get_icon:
178 * @emblemed: a #GEmblemedIcon
180 * Gets the main icon for @emblemed.
182 * Returns: (transfer none): a #GIcon that is owned by @emblemed
184 * Since: 2.18
186 GIcon *
187 g_emblemed_icon_get_icon (GEmblemedIcon *emblemed)
189 g_return_val_if_fail (G_IS_EMBLEMED_ICON (emblemed), NULL);
191 return emblemed->priv->icon;
195 * g_emblemed_icon_get_emblems:
196 * @emblemed: a #GEmblemedIcon
198 * Gets the list of emblems for the @icon.
200 * Returns: (element-type Gio.Emblem) (transfer none): a #GList of
201 * #GEmblem <!-- -->s that is owned by @emblemed
203 * Since: 2.18
206 GList *
207 g_emblemed_icon_get_emblems (GEmblemedIcon *emblemed)
209 g_return_val_if_fail (G_IS_EMBLEMED_ICON (emblemed), NULL);
211 return emblemed->priv->emblems;
215 * g_emblemed_icon_clear_emblems:
216 * @emblemed: a #GEmblemedIcon
218 * Removes all the emblems from @icon.
220 * Since: 2.28
222 void
223 g_emblemed_icon_clear_emblems (GEmblemedIcon *emblemed)
225 g_return_if_fail (G_IS_EMBLEMED_ICON (emblemed));
227 if (emblemed->priv->emblems == NULL)
228 return;
230 g_list_free_full (emblemed->priv->emblems, g_object_unref);
231 emblemed->priv->emblems = NULL;
234 static gint
235 g_emblem_comp (GEmblem *a,
236 GEmblem *b)
238 guint hash_a = g_icon_hash (G_ICON (a));
239 guint hash_b = g_icon_hash (G_ICON (b));
241 if(hash_a < hash_b)
242 return -1;
244 if(hash_a == hash_b)
245 return 0;
247 return 1;
251 * g_emblemed_icon_add_emblem:
252 * @emblemed: a #GEmblemedIcon
253 * @emblem: a #GEmblem
255 * Adds @emblem to the #GList of #GEmblem <!-- -->s.
257 * Since: 2.18
259 void
260 g_emblemed_icon_add_emblem (GEmblemedIcon *emblemed,
261 GEmblem *emblem)
263 g_return_if_fail (G_IS_EMBLEMED_ICON (emblemed));
264 g_return_if_fail (G_IS_EMBLEM (emblem));
266 g_object_ref (emblem);
267 emblemed->priv->emblems = g_list_insert_sorted (emblemed->priv->emblems, emblem,
268 (GCompareFunc) g_emblem_comp);
271 static guint
272 g_emblemed_icon_hash (GIcon *icon)
274 GEmblemedIcon *emblemed = G_EMBLEMED_ICON (icon);
275 GList *list;
276 guint hash = g_icon_hash (emblemed->priv->icon);
278 for (list = emblemed->priv->emblems; list != NULL; list = list->next)
279 hash ^= g_icon_hash (G_ICON (list->data));
281 return hash;
284 static gboolean
285 g_emblemed_icon_equal (GIcon *icon1,
286 GIcon *icon2)
288 GEmblemedIcon *emblemed1 = G_EMBLEMED_ICON (icon1);
289 GEmblemedIcon *emblemed2 = G_EMBLEMED_ICON (icon2);
290 GList *list1, *list2;
292 if (!g_icon_equal (emblemed1->priv->icon, emblemed2->priv->icon))
293 return FALSE;
295 list1 = emblemed1->priv->emblems;
296 list2 = emblemed2->priv->emblems;
298 while (list1 && list2)
300 if (!g_icon_equal (G_ICON (list1->data), G_ICON (list2->data)))
301 return FALSE;
303 list1 = list1->next;
304 list2 = list2->next;
307 return list1 == NULL && list2 == NULL;
310 static gboolean
311 g_emblemed_icon_to_tokens (GIcon *icon,
312 GPtrArray *tokens,
313 gint *out_version)
315 GEmblemedIcon *emblemed_icon = G_EMBLEMED_ICON (icon);
316 GList *l;
317 char *s;
319 /* GEmblemedIcons are encoded as
321 * <encoded_icon> [<encoded_emblem_icon>]*
324 g_return_val_if_fail (out_version != NULL, FALSE);
326 *out_version = 0;
328 s = g_icon_to_string (emblemed_icon->priv->icon);
329 if (s == NULL)
330 return FALSE;
332 g_ptr_array_add (tokens, s);
334 for (l = emblemed_icon->priv->emblems; l != NULL; l = l->next)
336 GIcon *emblem_icon = G_ICON (l->data);
338 s = g_icon_to_string (emblem_icon);
339 if (s == NULL)
340 return FALSE;
342 g_ptr_array_add (tokens, s);
345 return TRUE;
348 static GIcon *
349 g_emblemed_icon_from_tokens (gchar **tokens,
350 gint num_tokens,
351 gint version,
352 GError **error)
354 GEmblemedIcon *emblemed_icon;
355 int n;
357 emblemed_icon = NULL;
359 if (version != 0)
361 g_set_error (error,
362 G_IO_ERROR,
363 G_IO_ERROR_INVALID_ARGUMENT,
364 _("Can't handle version %d of GEmblemedIcon encoding"),
365 version);
366 goto fail;
369 if (num_tokens < 1)
371 g_set_error (error,
372 G_IO_ERROR,
373 G_IO_ERROR_INVALID_ARGUMENT,
374 _("Malformed number of tokens (%d) in GEmblemedIcon encoding"),
375 num_tokens);
376 goto fail;
379 emblemed_icon = g_object_new (G_TYPE_EMBLEMED_ICON, NULL);
380 emblemed_icon->priv->icon = g_icon_new_for_string (tokens[0], error);
381 if (emblemed_icon->priv->icon == NULL)
382 goto fail;
384 for (n = 1; n < num_tokens; n++)
386 GIcon *emblem;
388 emblem = g_icon_new_for_string (tokens[n], error);
389 if (emblem == NULL)
390 goto fail;
392 if (!G_IS_EMBLEM (emblem))
394 g_set_error_literal (error,
395 G_IO_ERROR,
396 G_IO_ERROR_INVALID_ARGUMENT,
397 _("Expected a GEmblem for GEmblemedIcon"));
398 g_object_unref (emblem);
399 goto fail;
402 emblemed_icon->priv->emblems = g_list_append (emblemed_icon->priv->emblems, emblem);
405 return G_ICON (emblemed_icon);
407 fail:
408 if (emblemed_icon != NULL)
409 g_object_unref (emblemed_icon);
410 return NULL;
413 static GVariant *
414 g_emblemed_icon_serialize (GIcon *icon)
416 GEmblemedIcon *emblemed_icon = G_EMBLEMED_ICON (icon);
417 GVariantBuilder builder;
418 GVariant *icon_data;
419 GList *node;
421 icon_data = g_icon_serialize (emblemed_icon->priv->icon);
422 if (!icon_data)
423 return NULL;
425 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(va(va{sv}))"));
427 g_variant_builder_add (&builder, "v", icon_data);
428 g_variant_unref (icon_data);
430 g_variant_builder_open (&builder, G_VARIANT_TYPE ("a(va{sv})"));
431 for (node = emblemed_icon->priv->emblems; node != NULL; node = node->next)
433 icon_data = g_icon_serialize (node->data);
434 if (icon_data)
436 /* We know how emblems serialise, so do a tweak here to
437 * reduce some of the variant wrapping and redundant storage
438 * of 'emblem' over and again...
440 if (g_variant_is_of_type (icon_data, G_VARIANT_TYPE ("(sv)")))
442 const gchar *name;
443 GVariant *content;
445 g_variant_get (icon_data, "(&sv)", &name, &content);
447 if (g_str_equal (name, "emblem") && g_variant_is_of_type (content, G_VARIANT_TYPE ("(va{sv})")))
448 g_variant_builder_add (&builder, "@(va{sv})", content);
450 g_variant_unref (content);
453 g_variant_unref (icon_data);
456 g_variant_builder_close (&builder);
458 return g_variant_new ("(sv)", "emblemed", g_variant_builder_end (&builder));
461 static void
462 g_emblemed_icon_icon_iface_init (GIconIface *iface)
464 iface->hash = g_emblemed_icon_hash;
465 iface->equal = g_emblemed_icon_equal;
466 iface->to_tokens = g_emblemed_icon_to_tokens;
467 iface->from_tokens = g_emblemed_icon_from_tokens;
468 iface->serialize = g_emblemed_icon_serialize;