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"
35 * @short_description: An object for emblems
37 * @see_also: #GIcon, #GEmblemedIcon, #GLoadableIcon, #GThemedIcon
39 * #GEmblem is an implementation of #GIcon that supports
40 * having an emblem, which is an icon with additional properties.
41 * It can than be added to a #GEmblemedIcon.
43 * Currently, only metainformation about the emblem's origin is
44 * supported. More may be added in the future.
47 static void g_emblem_iface_init (GIconIface *iface);
51 GObject parent_instance;
59 GObjectClass parent_class;
69 G_DEFINE_TYPE_WITH_CODE (GEmblem, g_emblem, G_TYPE_OBJECT,
70 G_IMPLEMENT_INTERFACE (G_TYPE_ICON, g_emblem_iface_init))
73 g_emblem_get_property (GObject *object,
78 GEmblem *emblem = G_EMBLEM (object);
83 g_value_set_object (value, emblem->icon);
87 g_value_set_enum (value, emblem->origin);
91 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
97 g_emblem_set_property (GObject *object,
102 GEmblem *emblem = G_EMBLEM (object);
107 emblem->icon = g_value_dup_object (value);
111 emblem->origin = g_value_get_enum (value);
115 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
121 g_emblem_finalize (GObject *object)
123 GEmblem *emblem = G_EMBLEM (object);
126 g_object_unref (emblem->icon);
128 (*G_OBJECT_CLASS (g_emblem_parent_class)->finalize) (object);
132 g_emblem_class_init (GEmblemClass *klass)
134 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
136 gobject_class->finalize = g_emblem_finalize;
137 gobject_class->set_property = g_emblem_set_property;
138 gobject_class->get_property = g_emblem_get_property;
140 g_object_class_install_property (gobject_class,
142 g_param_spec_enum ("origin",
143 P_("GEmblem’s origin"),
144 P_("Tells which origin the emblem is derived from"),
145 G_TYPE_EMBLEM_ORIGIN,
146 G_EMBLEM_ORIGIN_UNKNOWN,
147 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
149 g_object_class_install_property (gobject_class,
151 g_param_spec_object ("icon",
152 P_("The icon of the emblem"),
153 P_("The actual icon of the emblem"),
155 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
160 g_emblem_init (GEmblem *emblem)
166 * @icon: a GIcon containing the icon.
168 * Creates a new emblem for @icon.
170 * Returns: a new #GEmblem.
175 g_emblem_new (GIcon *icon)
179 g_return_val_if_fail (icon != NULL, NULL);
180 g_return_val_if_fail (G_IS_ICON (icon), NULL);
181 g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
183 emblem = g_object_new (G_TYPE_EMBLEM, NULL);
184 emblem->icon = g_object_ref (icon);
185 emblem->origin = G_EMBLEM_ORIGIN_UNKNOWN;
191 * g_emblem_new_with_origin:
192 * @icon: a GIcon containing the icon.
193 * @origin: a GEmblemOrigin enum defining the emblem's origin
195 * Creates a new emblem for @icon.
197 * Returns: a new #GEmblem.
202 g_emblem_new_with_origin (GIcon *icon,
203 GEmblemOrigin origin)
207 g_return_val_if_fail (icon != NULL, NULL);
208 g_return_val_if_fail (G_IS_ICON (icon), NULL);
209 g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
211 emblem = g_object_new (G_TYPE_EMBLEM, NULL);
212 emblem->icon = g_object_ref (icon);
213 emblem->origin = origin;
220 * @emblem: a #GEmblem from which the icon should be extracted.
222 * Gives back the icon from @emblem.
224 * Returns: (transfer none): a #GIcon. The returned object belongs to
225 * the emblem and should not be modified or freed.
230 g_emblem_get_icon (GEmblem *emblem)
232 g_return_val_if_fail (G_IS_EMBLEM (emblem), NULL);
239 * g_emblem_get_origin:
240 * @emblem: a #GEmblem
242 * Gets the origin of the emblem.
244 * Returns: (transfer none): the origin of the emblem
249 g_emblem_get_origin (GEmblem *emblem)
251 g_return_val_if_fail (G_IS_EMBLEM (emblem), G_EMBLEM_ORIGIN_UNKNOWN);
253 return emblem->origin;
257 g_emblem_hash (GIcon *icon)
259 GEmblem *emblem = G_EMBLEM (icon);
262 hash = g_icon_hash (g_emblem_get_icon (emblem));
263 hash ^= emblem->origin;
269 g_emblem_equal (GIcon *icon1,
272 GEmblem *emblem1 = G_EMBLEM (icon1);
273 GEmblem *emblem2 = G_EMBLEM (icon2);
275 return emblem1->origin == emblem2->origin &&
276 g_icon_equal (emblem1->icon, emblem2->icon);
280 g_emblem_to_tokens (GIcon *icon,
284 GEmblem *emblem = G_EMBLEM (icon);
287 /* GEmblem are encoded as
292 g_return_val_if_fail (out_version != NULL, FALSE);
296 s = g_icon_to_string (emblem->icon);
300 g_ptr_array_add (tokens, s);
302 s = g_strdup_printf ("%d", emblem->origin);
303 g_ptr_array_add (tokens, s);
309 g_emblem_from_tokens (gchar **tokens,
316 GEmblemOrigin origin;
324 G_IO_ERROR_INVALID_ARGUMENT,
325 _("Can’t handle version %d of GEmblem encoding"),
334 G_IO_ERROR_INVALID_ARGUMENT,
335 _("Malformed number of tokens (%d) in GEmblem encoding"),
340 icon = g_icon_new_for_string (tokens[0], error);
345 origin = atoi (tokens[1]);
347 emblem = g_emblem_new_with_origin (icon, origin);
348 g_object_unref (icon);
350 return G_ICON (emblem);
354 g_emblem_serialize (GIcon *icon)
356 GEmblem *emblem = G_EMBLEM (icon);
361 icon_data = g_icon_serialize (emblem->icon);
365 origin = g_enum_get_value (g_type_class_peek (G_TYPE_EMBLEM_ORIGIN), emblem->origin);
366 result = g_variant_new_parsed ("('emblem', <(%v, {'origin': <%s>})>)",
367 icon_data, origin ? origin->value_nick : "unknown");
368 g_variant_unref (icon_data);
374 g_emblem_iface_init (GIconIface *iface)
376 iface->hash = g_emblem_hash;
377 iface->equal = g_emblem_equal;
378 iface->to_tokens = g_emblem_to_tokens;
379 iface->from_tokens = g_emblem_from_tokens;
380 iface->serialize = g_emblem_serialize;