1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
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>
30 #include "gemblemedicon.h"
36 * SECTION:gemblemedicon
37 * @short_description: Icon with emblems
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.
54 struct _GEmblemedIconPrivate {
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))
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);
83 g_emblemed_icon_set_property (GObject *object,
88 GEmblemedIcon *self = G_EMBLEMED_ICON (object);
93 self->priv->icon = g_value_dup_object (value);
96 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
102 g_emblemed_icon_get_property (GObject *object,
107 GEmblemedIcon *self = G_EMBLEMED_ICON (object);
112 g_value_set_object (value, self->priv->icon);
115 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
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"),
134 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
136 g_object_class_install_properties (gobject_class, NUM_PROPERTIES, properties);
140 g_emblemed_icon_init (GEmblemedIcon *emblemed)
142 emblemed->priv = g_emblemed_icon_get_instance_private (emblemed);
146 * g_emblemed_icon_new:
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
157 g_emblemed_icon_new (GIcon *icon,
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,
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
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
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.
223 g_emblemed_icon_clear_emblems (GEmblemedIcon *emblemed)
225 g_return_if_fail (G_IS_EMBLEMED_ICON (emblemed));
227 if (emblemed->priv->emblems == NULL)
230 g_list_free_full (emblemed->priv->emblems, g_object_unref);
231 emblemed->priv->emblems = NULL;
235 g_emblem_comp (GEmblem *a,
238 guint hash_a = g_icon_hash (G_ICON (a));
239 guint hash_b = g_icon_hash (G_ICON (b));
251 * g_emblemed_icon_add_emblem:
252 * @emblemed: a #GEmblemedIcon
253 * @emblem: a #GEmblem
255 * Adds @emblem to the #GList of #GEmblem <!-- -->s.
260 g_emblemed_icon_add_emblem (GEmblemedIcon *emblemed,
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);
272 g_emblemed_icon_hash (GIcon *icon)
274 GEmblemedIcon *emblemed = G_EMBLEMED_ICON (icon);
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));
285 g_emblemed_icon_equal (GIcon *icon1,
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))
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)))
307 return list1 == NULL && list2 == NULL;
311 g_emblemed_icon_to_tokens (GIcon *icon,
315 GEmblemedIcon *emblemed_icon = G_EMBLEMED_ICON (icon);
319 /* GEmblemedIcons are encoded as
321 * <encoded_icon> [<encoded_emblem_icon>]*
324 g_return_val_if_fail (out_version != NULL, FALSE);
328 s = g_icon_to_string (emblemed_icon->priv->icon);
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);
342 g_ptr_array_add (tokens, s);
349 g_emblemed_icon_from_tokens (gchar **tokens,
354 GEmblemedIcon *emblemed_icon;
357 emblemed_icon = NULL;
363 G_IO_ERROR_INVALID_ARGUMENT,
364 _("Can't handle version %d of GEmblemedIcon encoding"),
373 G_IO_ERROR_INVALID_ARGUMENT,
374 _("Malformed number of tokens (%d) in GEmblemedIcon encoding"),
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)
384 for (n = 1; n < num_tokens; n++)
388 emblem = g_icon_new_for_string (tokens[n], error);
392 if (!G_IS_EMBLEM (emblem))
394 g_set_error_literal (error,
396 G_IO_ERROR_INVALID_ARGUMENT,
397 _("Expected a GEmblem for GEmblemedIcon"));
398 g_object_unref (emblem);
402 emblemed_icon->priv->emblems = g_list_append (emblemed_icon->priv->emblems, emblem);
405 return G_ICON (emblemed_icon);
408 if (emblemed_icon != NULL)
409 g_object_unref (emblemed_icon);
414 g_emblemed_icon_serialize (GIcon *icon)
416 GEmblemedIcon *emblemed_icon = G_EMBLEMED_ICON (icon);
417 GVariantBuilder builder;
421 icon_data = g_icon_serialize (emblemed_icon->priv->icon);
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);
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)")))
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));
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;