1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2008 Clemens N. Buss <cebuzz@gmail.com>
5 * SPDX-License-Identifier: LGPL-2.1-or-later
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/>.
27 #include "gioenumtypes.h"
36 * `GEmblem` is an implementation of [iface@Gio.Icon] that supports
37 * having an emblem, which is an icon with additional properties.
38 * It can than be added to a [class@Gio.EmblemedIcon].
40 * Currently, only metainformation about the emblem's origin is
41 * supported. More may be added in the future.
44 static void g_emblem_iface_init (GIconIface *iface);
48 GObject parent_instance;
56 GObjectClass parent_class;
66 G_DEFINE_TYPE_WITH_CODE (GEmblem, g_emblem, G_TYPE_OBJECT,
67 G_IMPLEMENT_INTERFACE (G_TYPE_ICON, g_emblem_iface_init))
70 g_emblem_get_property (GObject *object,
75 GEmblem *emblem = G_EMBLEM (object);
80 g_value_set_object (value, emblem->icon);
84 g_value_set_enum (value, emblem->origin);
88 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
94 g_emblem_set_property (GObject *object,
99 GEmblem *emblem = G_EMBLEM (object);
104 emblem->icon = g_value_dup_object (value);
108 emblem->origin = g_value_get_enum (value);
112 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
118 g_emblem_finalize (GObject *object)
120 GEmblem *emblem = G_EMBLEM (object);
123 g_object_unref (emblem->icon);
125 (*G_OBJECT_CLASS (g_emblem_parent_class)->finalize) (object);
129 g_emblem_class_init (GEmblemClass *klass)
131 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
133 gobject_class->finalize = g_emblem_finalize;
134 gobject_class->set_property = g_emblem_set_property;
135 gobject_class->get_property = g_emblem_get_property;
140 * The origin the emblem is derived from.
144 g_object_class_install_property (gobject_class,
146 g_param_spec_enum ("origin", NULL, NULL,
147 G_TYPE_EMBLEM_ORIGIN,
148 G_EMBLEM_ORIGIN_UNKNOWN,
149 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
154 * The actual icon of the emblem.
158 g_object_class_install_property (gobject_class,
160 g_param_spec_object ("icon", NULL, NULL,
162 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
167 g_emblem_init (GEmblem *emblem)
173 * @icon: a GIcon containing the icon.
175 * Creates a new emblem for @icon.
177 * Returns: a new #GEmblem.
182 g_emblem_new (GIcon *icon)
186 g_return_val_if_fail (icon != NULL, NULL);
187 g_return_val_if_fail (G_IS_ICON (icon), NULL);
188 g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
190 emblem = g_object_new (G_TYPE_EMBLEM, NULL);
191 emblem->icon = g_object_ref (icon);
192 emblem->origin = G_EMBLEM_ORIGIN_UNKNOWN;
198 * g_emblem_new_with_origin:
199 * @icon: a GIcon containing the icon.
200 * @origin: a GEmblemOrigin enum defining the emblem's origin
202 * Creates a new emblem for @icon.
204 * Returns: a new #GEmblem.
209 g_emblem_new_with_origin (GIcon *icon,
210 GEmblemOrigin origin)
214 g_return_val_if_fail (icon != NULL, NULL);
215 g_return_val_if_fail (G_IS_ICON (icon), NULL);
216 g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
218 emblem = g_object_new (G_TYPE_EMBLEM, NULL);
219 emblem->icon = g_object_ref (icon);
220 emblem->origin = origin;
227 * @emblem: a #GEmblem from which the icon should be extracted.
229 * Gives back the icon from @emblem.
231 * Returns: (transfer none): a #GIcon. The returned object belongs to
232 * the emblem and should not be modified or freed.
237 g_emblem_get_icon (GEmblem *emblem)
239 g_return_val_if_fail (G_IS_EMBLEM (emblem), NULL);
246 * g_emblem_get_origin:
247 * @emblem: a #GEmblem
249 * Gets the origin of the emblem.
251 * Returns: (transfer none): the origin of the emblem
256 g_emblem_get_origin (GEmblem *emblem)
258 g_return_val_if_fail (G_IS_EMBLEM (emblem), G_EMBLEM_ORIGIN_UNKNOWN);
260 return emblem->origin;
264 g_emblem_hash (GIcon *icon)
266 GEmblem *emblem = G_EMBLEM (icon);
269 hash = g_icon_hash (g_emblem_get_icon (emblem));
270 hash ^= emblem->origin;
276 g_emblem_equal (GIcon *icon1,
279 GEmblem *emblem1 = G_EMBLEM (icon1);
280 GEmblem *emblem2 = G_EMBLEM (icon2);
282 return emblem1->origin == emblem2->origin &&
283 g_icon_equal (emblem1->icon, emblem2->icon);
287 g_emblem_to_tokens (GIcon *icon,
291 GEmblem *emblem = G_EMBLEM (icon);
294 /* GEmblem are encoded as
299 g_return_val_if_fail (out_version != NULL, FALSE);
303 s = g_icon_to_string (emblem->icon);
307 g_ptr_array_add (tokens, s);
309 s = g_strdup_printf ("%d", emblem->origin);
310 g_ptr_array_add (tokens, s);
316 g_emblem_from_tokens (gchar **tokens,
323 GEmblemOrigin origin;
331 G_IO_ERROR_INVALID_ARGUMENT,
332 _("Can’t handle version %d of GEmblem encoding"),
341 G_IO_ERROR_INVALID_ARGUMENT,
342 _("Malformed number of tokens (%d) in GEmblem encoding"),
347 icon = g_icon_new_for_string (tokens[0], error);
352 origin = atoi (tokens[1]);
354 emblem = g_emblem_new_with_origin (icon, origin);
355 g_object_unref (icon);
357 return G_ICON (emblem);
361 g_emblem_serialize (GIcon *icon)
363 GEmblem *emblem = G_EMBLEM (icon);
368 icon_data = g_icon_serialize (emblem->icon);
372 origin = g_enum_get_value (g_type_class_peek (G_TYPE_EMBLEM_ORIGIN), emblem->origin);
373 result = g_variant_new_parsed ("('emblem', <(%v, {'origin': <%s>})>)",
374 icon_data, origin ? origin->value_nick : "unknown");
375 g_variant_unref (icon_data);
381 g_emblem_iface_init (GIconIface *iface)
383 iface->hash = g_emblem_hash;
384 iface->equal = g_emblem_equal;
385 iface->to_tokens = g_emblem_to_tokens;
386 iface->from_tokens = g_emblem_from_tokens;
387 iface->serialize = g_emblem_serialize;