1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2008 Clemens N. Buss <cebuzz@gmail.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 #include "gioenumtypes.h"
33 * @short_description: An object for emblems
35 * @see_also: #GIcon, #GEmblemedIcon, #GLoadableIcon, #GThemedIcon
37 * #GEmblem is an implementation of #GIcon that supports
38 * having an emblem, which is an icon with additional properties.
39 * It can than be added to a #GEmblemedIcon.
41 * Currently, only metainformation about the emblem's origin is
42 * supported. More may be added in the future.
45 static void g_emblem_iface_init (GIconIface *iface);
49 GObject parent_instance;
57 GObjectClass parent_class;
67 G_DEFINE_TYPE_WITH_CODE (GEmblem, g_emblem, G_TYPE_OBJECT,
68 G_IMPLEMENT_INTERFACE (G_TYPE_ICON, g_emblem_iface_init))
71 g_emblem_get_property (GObject *object,
76 GEmblem *emblem = G_EMBLEM (object);
81 g_value_set_object (value, emblem->icon);
85 g_value_set_enum (value, emblem->origin);
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
95 g_emblem_set_property (GObject *object,
100 GEmblem *emblem = G_EMBLEM (object);
105 emblem->icon = g_value_dup_object (value);
109 emblem->origin = g_value_get_enum (value);
113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
119 g_emblem_finalize (GObject *object)
121 GEmblem *emblem = G_EMBLEM (object);
124 g_object_unref (emblem->icon);
126 (*G_OBJECT_CLASS (g_emblem_parent_class)->finalize) (object);
130 g_emblem_class_init (GEmblemClass *klass)
132 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
134 gobject_class->finalize = g_emblem_finalize;
135 gobject_class->set_property = g_emblem_set_property;
136 gobject_class->get_property = g_emblem_get_property;
138 g_object_class_install_property (gobject_class,
140 g_param_spec_enum ("origin",
141 P_("GEmblem's origin"),
142 P_("Tells which origin the emblem is derived from"),
143 G_TYPE_EMBLEM_ORIGIN,
144 G_EMBLEM_ORIGIN_UNKNOWN,
145 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
147 g_object_class_install_property (gobject_class,
149 g_param_spec_object ("icon",
150 P_("The icon of the emblem"),
151 P_("The actual icon of the emblem"),
153 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
158 g_emblem_init (GEmblem *emblem)
164 * @icon: a GIcon containing the icon.
166 * Creates a new emblem for @icon.
168 * Returns: a new #GEmblem.
173 g_emblem_new (GIcon *icon)
177 g_return_val_if_fail (icon != NULL, NULL);
178 g_return_val_if_fail (G_IS_ICON (icon), NULL);
179 g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
181 emblem = g_object_new (G_TYPE_EMBLEM, NULL);
182 emblem->icon = g_object_ref (icon);
183 emblem->origin = G_EMBLEM_ORIGIN_UNKNOWN;
189 * g_emblem_new_with_origin:
190 * @icon: a GIcon containing the icon.
191 * @origin: a GEmblemOrigin enum defining the emblem's origin
193 * Creates a new emblem for @icon.
195 * Returns: a new #GEmblem.
200 g_emblem_new_with_origin (GIcon *icon,
201 GEmblemOrigin origin)
205 g_return_val_if_fail (icon != NULL, NULL);
206 g_return_val_if_fail (G_IS_ICON (icon), NULL);
207 g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
209 emblem = g_object_new (G_TYPE_EMBLEM, NULL);
210 emblem->icon = g_object_ref (icon);
211 emblem->origin = origin;
218 * @emblem: a #GEmblem from which the icon should be extracted.
220 * Gives back the icon from @emblem.
222 * Returns: (transfer none): a #GIcon. The returned object belongs to
223 * the emblem and should not be modified or freed.
228 g_emblem_get_icon (GEmblem *emblem)
230 g_return_val_if_fail (G_IS_EMBLEM (emblem), NULL);
237 * g_emblem_get_origin:
238 * @emblem: a #GEmblem
240 * Gets the origin of the emblem.
242 * Returns: (transfer none): the origin of the emblem
247 g_emblem_get_origin (GEmblem *emblem)
249 g_return_val_if_fail (G_IS_EMBLEM (emblem), G_EMBLEM_ORIGIN_UNKNOWN);
251 return emblem->origin;
255 g_emblem_hash (GIcon *icon)
257 GEmblem *emblem = G_EMBLEM (icon);
260 hash = g_icon_hash (g_emblem_get_icon (emblem));
261 hash ^= emblem->origin;
267 g_emblem_equal (GIcon *icon1,
270 GEmblem *emblem1 = G_EMBLEM (icon1);
271 GEmblem *emblem2 = G_EMBLEM (icon2);
273 return emblem1->origin == emblem2->origin &&
274 g_icon_equal (emblem1->icon, emblem2->icon);
278 g_emblem_to_tokens (GIcon *icon,
282 GEmblem *emblem = G_EMBLEM (icon);
285 /* GEmblem are encoded as
290 g_return_val_if_fail (out_version != NULL, FALSE);
294 s = g_icon_to_string (emblem->icon);
298 g_ptr_array_add (tokens, s);
300 s = g_strdup_printf ("%d", emblem->origin);
301 g_ptr_array_add (tokens, s);
307 g_emblem_from_tokens (gchar **tokens,
314 GEmblemOrigin origin;
322 G_IO_ERROR_INVALID_ARGUMENT,
323 _("Can't handle version %d of GEmblem encoding"),
332 G_IO_ERROR_INVALID_ARGUMENT,
333 _("Malformed number of tokens (%d) in GEmblem encoding"),
338 icon = g_icon_new_for_string (tokens[0], error);
343 origin = atoi (tokens[1]);
345 emblem = g_emblem_new_with_origin (icon, origin);
346 g_object_unref (icon);
348 return G_ICON (emblem);
352 g_emblem_serialize (GIcon *icon)
354 GEmblem *emblem = G_EMBLEM (icon);
359 icon_data = g_icon_serialize (emblem->icon);
363 origin = g_enum_get_value (g_type_class_peek (G_TYPE_EMBLEM_ORIGIN), emblem->origin);
364 result = g_variant_new_parsed ("('emblem', <(%v, {'origin': <%s>})>)",
365 icon_data, origin ? origin->value_nick : "unknown");
366 g_variant_unref (icon_data);
372 g_emblem_iface_init (GIconIface *iface)
374 iface->hash = g_emblem_hash;
375 iface->equal = g_emblem_equal;
376 iface->to_tokens = g_emblem_to_tokens;
377 iface->from_tokens = g_emblem_from_tokens;
378 iface->serialize = g_emblem_serialize;