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.1 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, see <http://www.gnu.org/licenses/>.
20 * Author: Matthias Clasen <mclasen@redhat.com>
21 * Clemens N. Buss <cebuzz@gmail.com>
28 #include "gemblemedicon.h"
34 * SECTION:gemblemedicon
35 * @short_description: Icon with emblems
37 * @see_also: #GIcon, #GLoadableIcon, #GThemedIcon, #GEmblem
39 * #GEmblemedIcon is an implementation of #GIcon that supports
40 * adding an emblem to an icon. Adding multiple emblems to an
41 * icon is ensured via g_emblemed_icon_add_emblem().
43 * Note that #GEmblemedIcon allows no control over the position
44 * of the emblems. See also #GEmblem for more information.
52 struct _GEmblemedIconPrivate
{
57 static GParamSpec
*properties
[NUM_PROPERTIES
] = { NULL
, };
59 static void g_emblemed_icon_icon_iface_init (GIconIface
*iface
);
61 G_DEFINE_TYPE_WITH_CODE (GEmblemedIcon
, g_emblemed_icon
, G_TYPE_OBJECT
,
62 G_ADD_PRIVATE (GEmblemedIcon
)
63 G_IMPLEMENT_INTERFACE (G_TYPE_ICON
,
64 g_emblemed_icon_icon_iface_init
))
68 g_emblemed_icon_finalize (GObject
*object
)
70 GEmblemedIcon
*emblemed
;
72 emblemed
= G_EMBLEMED_ICON (object
);
74 g_clear_object (&emblemed
->priv
->icon
);
75 g_list_free_full (emblemed
->priv
->emblems
, g_object_unref
);
77 (*G_OBJECT_CLASS (g_emblemed_icon_parent_class
)->finalize
) (object
);
81 g_emblemed_icon_set_property (GObject
*object
,
86 GEmblemedIcon
*self
= G_EMBLEMED_ICON (object
);
91 self
->priv
->icon
= g_value_dup_object (value
);
94 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
100 g_emblemed_icon_get_property (GObject
*object
,
105 GEmblemedIcon
*self
= G_EMBLEMED_ICON (object
);
110 g_value_set_object (value
, self
->priv
->icon
);
113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
119 g_emblemed_icon_class_init (GEmblemedIconClass
*klass
)
121 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
123 gobject_class
->finalize
= g_emblemed_icon_finalize
;
124 gobject_class
->set_property
= g_emblemed_icon_set_property
;
125 gobject_class
->get_property
= g_emblemed_icon_get_property
;
127 properties
[PROP_GICON
] =
128 g_param_spec_object ("gicon",
129 P_("The base GIcon"),
130 P_("The GIcon to attach emblems to"),
132 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
| G_PARAM_STATIC_STRINGS
);
134 g_object_class_install_properties (gobject_class
, NUM_PROPERTIES
, properties
);
138 g_emblemed_icon_init (GEmblemedIcon
*emblemed
)
140 emblemed
->priv
= g_emblemed_icon_get_instance_private (emblemed
);
144 * g_emblemed_icon_new:
146 * @emblem: (nullable): a #GEmblem, or %NULL
148 * Creates a new emblemed icon for @icon with the emblem @emblem.
150 * Returns: (transfer full) (type GEmblemedIcon): a new #GIcon
155 g_emblemed_icon_new (GIcon
*icon
,
158 GEmblemedIcon
*emblemed
;
160 g_return_val_if_fail (G_IS_ICON (icon
), NULL
);
161 g_return_val_if_fail (!G_IS_EMBLEM (icon
), NULL
);
163 emblemed
= G_EMBLEMED_ICON (g_object_new (G_TYPE_EMBLEMED_ICON
,
168 g_emblemed_icon_add_emblem (emblemed
, emblem
);
170 return G_ICON (emblemed
);
175 * g_emblemed_icon_get_icon:
176 * @emblemed: a #GEmblemedIcon
178 * Gets the main icon for @emblemed.
180 * Returns: (transfer none): a #GIcon that is owned by @emblemed
185 g_emblemed_icon_get_icon (GEmblemedIcon
*emblemed
)
187 g_return_val_if_fail (G_IS_EMBLEMED_ICON (emblemed
), NULL
);
189 return emblemed
->priv
->icon
;
193 * g_emblemed_icon_get_emblems:
194 * @emblemed: a #GEmblemedIcon
196 * Gets the list of emblems for the @icon.
198 * Returns: (element-type Gio.Emblem) (transfer none): a #GList of
199 * #GEmblems that is owned by @emblemed
205 g_emblemed_icon_get_emblems (GEmblemedIcon
*emblemed
)
207 g_return_val_if_fail (G_IS_EMBLEMED_ICON (emblemed
), NULL
);
209 return emblemed
->priv
->emblems
;
213 * g_emblemed_icon_clear_emblems:
214 * @emblemed: a #GEmblemedIcon
216 * Removes all the emblems from @icon.
221 g_emblemed_icon_clear_emblems (GEmblemedIcon
*emblemed
)
223 g_return_if_fail (G_IS_EMBLEMED_ICON (emblemed
));
225 if (emblemed
->priv
->emblems
== NULL
)
228 g_list_free_full (emblemed
->priv
->emblems
, g_object_unref
);
229 emblemed
->priv
->emblems
= NULL
;
233 g_emblem_comp (GEmblem
*a
,
236 guint hash_a
= g_icon_hash (G_ICON (a
));
237 guint hash_b
= g_icon_hash (G_ICON (b
));
249 * g_emblemed_icon_add_emblem:
250 * @emblemed: a #GEmblemedIcon
251 * @emblem: a #GEmblem
253 * Adds @emblem to the #GList of #GEmblems.
258 g_emblemed_icon_add_emblem (GEmblemedIcon
*emblemed
,
261 g_return_if_fail (G_IS_EMBLEMED_ICON (emblemed
));
262 g_return_if_fail (G_IS_EMBLEM (emblem
));
264 g_object_ref (emblem
);
265 emblemed
->priv
->emblems
= g_list_insert_sorted (emblemed
->priv
->emblems
, emblem
,
266 (GCompareFunc
) g_emblem_comp
);
270 g_emblemed_icon_hash (GIcon
*icon
)
272 GEmblemedIcon
*emblemed
= G_EMBLEMED_ICON (icon
);
274 guint hash
= g_icon_hash (emblemed
->priv
->icon
);
276 for (list
= emblemed
->priv
->emblems
; list
!= NULL
; list
= list
->next
)
277 hash
^= g_icon_hash (G_ICON (list
->data
));
283 g_emblemed_icon_equal (GIcon
*icon1
,
286 GEmblemedIcon
*emblemed1
= G_EMBLEMED_ICON (icon1
);
287 GEmblemedIcon
*emblemed2
= G_EMBLEMED_ICON (icon2
);
288 GList
*list1
, *list2
;
290 if (!g_icon_equal (emblemed1
->priv
->icon
, emblemed2
->priv
->icon
))
293 list1
= emblemed1
->priv
->emblems
;
294 list2
= emblemed2
->priv
->emblems
;
296 while (list1
&& list2
)
298 if (!g_icon_equal (G_ICON (list1
->data
), G_ICON (list2
->data
)))
305 return list1
== NULL
&& list2
== NULL
;
309 g_emblemed_icon_to_tokens (GIcon
*icon
,
313 GEmblemedIcon
*emblemed_icon
= G_EMBLEMED_ICON (icon
);
317 /* GEmblemedIcons are encoded as
319 * <encoded_icon> [<encoded_emblem_icon>]*
322 g_return_val_if_fail (out_version
!= NULL
, FALSE
);
326 s
= g_icon_to_string (emblemed_icon
->priv
->icon
);
330 g_ptr_array_add (tokens
, s
);
332 for (l
= emblemed_icon
->priv
->emblems
; l
!= NULL
; l
= l
->next
)
334 GIcon
*emblem_icon
= G_ICON (l
->data
);
336 s
= g_icon_to_string (emblem_icon
);
340 g_ptr_array_add (tokens
, s
);
347 g_emblemed_icon_from_tokens (gchar
**tokens
,
352 GEmblemedIcon
*emblemed_icon
;
355 emblemed_icon
= NULL
;
361 G_IO_ERROR_INVALID_ARGUMENT
,
362 _("Can’t handle version %d of GEmblemedIcon encoding"),
371 G_IO_ERROR_INVALID_ARGUMENT
,
372 _("Malformed number of tokens (%d) in GEmblemedIcon encoding"),
377 emblemed_icon
= g_object_new (G_TYPE_EMBLEMED_ICON
, NULL
);
378 emblemed_icon
->priv
->icon
= g_icon_new_for_string (tokens
[0], error
);
379 if (emblemed_icon
->priv
->icon
== NULL
)
382 for (n
= 1; n
< num_tokens
; n
++)
386 emblem
= g_icon_new_for_string (tokens
[n
], error
);
390 if (!G_IS_EMBLEM (emblem
))
392 g_set_error_literal (error
,
394 G_IO_ERROR_INVALID_ARGUMENT
,
395 _("Expected a GEmblem for GEmblemedIcon"));
396 g_object_unref (emblem
);
400 emblemed_icon
->priv
->emblems
= g_list_append (emblemed_icon
->priv
->emblems
, emblem
);
403 return G_ICON (emblemed_icon
);
406 if (emblemed_icon
!= NULL
)
407 g_object_unref (emblemed_icon
);
412 g_emblemed_icon_serialize (GIcon
*icon
)
414 GEmblemedIcon
*emblemed_icon
= G_EMBLEMED_ICON (icon
);
415 GVariantBuilder builder
;
419 icon_data
= g_icon_serialize (emblemed_icon
->priv
->icon
);
423 g_variant_builder_init (&builder
, G_VARIANT_TYPE ("(va(va{sv}))"));
425 g_variant_builder_add (&builder
, "v", icon_data
);
426 g_variant_unref (icon_data
);
428 g_variant_builder_open (&builder
, G_VARIANT_TYPE ("a(va{sv})"));
429 for (node
= emblemed_icon
->priv
->emblems
; node
!= NULL
; node
= node
->next
)
431 icon_data
= g_icon_serialize (node
->data
);
434 /* We know how emblems serialise, so do a tweak here to
435 * reduce some of the variant wrapping and redundant storage
436 * of 'emblem' over and again...
438 if (g_variant_is_of_type (icon_data
, G_VARIANT_TYPE ("(sv)")))
443 g_variant_get (icon_data
, "(&sv)", &name
, &content
);
445 if (g_str_equal (name
, "emblem") && g_variant_is_of_type (content
, G_VARIANT_TYPE ("(va{sv})")))
446 g_variant_builder_add (&builder
, "@(va{sv})", content
);
448 g_variant_unref (content
);
451 g_variant_unref (icon_data
);
454 g_variant_builder_close (&builder
);
456 return g_variant_new ("(sv)", "emblemed", g_variant_builder_end (&builder
));
460 g_emblemed_icon_icon_iface_init (GIconIface
*iface
)
462 iface
->hash
= g_emblemed_icon_hash
;
463 iface
->equal
= g_emblemed_icon_equal
;
464 iface
->to_tokens
= g_emblemed_icon_to_tokens
;
465 iface
->from_tokens
= g_emblemed_icon_from_tokens
;
466 iface
->serialize
= g_emblemed_icon_serialize
;