Merge branch '976-disable-assert-checks' into 'master'
[glib.git] / gio / gemblem.c
blobe8c4d4e63b1792fd7b7f9729ec0d0c8a1ebb991a
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.1 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/>.
19 #include <config.h>
21 #include "gicon.h"
22 #include "gemblem.h"
23 #include "glibintl.h"
24 #include "gioenums.h"
25 #include "gioenumtypes.h"
26 #include "gioerror.h"
27 #include <stdlib.h>
28 #include <string.h>
31 /**
32 * SECTION:gemblem
33 * @short_description: An object for emblems
34 * @include: gio/gio.h
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);
47 struct _GEmblem
49 GObject parent_instance;
51 GIcon *icon;
52 GEmblemOrigin origin;
55 struct _GEmblemClass
57 GObjectClass parent_class;
60 enum
62 PROP_0_GEMBLEM,
63 PROP_ICON,
64 PROP_ORIGIN
67 G_DEFINE_TYPE_WITH_CODE (GEmblem, g_emblem, G_TYPE_OBJECT,
68 G_IMPLEMENT_INTERFACE (G_TYPE_ICON, g_emblem_iface_init))
70 static void
71 g_emblem_get_property (GObject *object,
72 guint prop_id,
73 GValue *value,
74 GParamSpec *pspec)
76 GEmblem *emblem = G_EMBLEM (object);
78 switch (prop_id)
80 case PROP_ICON:
81 g_value_set_object (value, emblem->icon);
82 break;
84 case PROP_ORIGIN:
85 g_value_set_enum (value, emblem->origin);
86 break;
88 default:
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
90 break;
94 static void
95 g_emblem_set_property (GObject *object,
96 guint prop_id,
97 const GValue *value,
98 GParamSpec *pspec)
100 GEmblem *emblem = G_EMBLEM (object);
102 switch (prop_id)
104 case PROP_ICON:
105 emblem->icon = g_value_dup_object (value);
106 break;
108 case PROP_ORIGIN:
109 emblem->origin = g_value_get_enum (value);
110 break;
112 default:
113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
114 break;
118 static void
119 g_emblem_finalize (GObject *object)
121 GEmblem *emblem = G_EMBLEM (object);
123 if (emblem->icon)
124 g_object_unref (emblem->icon);
126 (*G_OBJECT_CLASS (g_emblem_parent_class)->finalize) (object);
129 static void
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,
139 PROP_ORIGIN,
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,
148 PROP_ICON,
149 g_param_spec_object ("icon",
150 P_("The icon of the emblem"),
151 P_("The actual icon of the emblem"),
152 G_TYPE_OBJECT,
153 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
157 static void
158 g_emblem_init (GEmblem *emblem)
163 * g_emblem_new:
164 * @icon: a GIcon containing the icon.
166 * Creates a new emblem for @icon.
168 * Returns: a new #GEmblem.
170 * Since: 2.18
172 GEmblem *
173 g_emblem_new (GIcon *icon)
175 GEmblem* emblem;
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;
185 return emblem;
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.
197 * Since: 2.18
199 GEmblem *
200 g_emblem_new_with_origin (GIcon *icon,
201 GEmblemOrigin origin)
203 GEmblem* emblem;
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;
213 return emblem;
217 * g_emblem_get_icon:
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.
225 * Since: 2.18
227 GIcon *
228 g_emblem_get_icon (GEmblem *emblem)
230 g_return_val_if_fail (G_IS_EMBLEM (emblem), NULL);
232 return emblem->icon;
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
244 * Since: 2.18
246 GEmblemOrigin
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;
254 static guint
255 g_emblem_hash (GIcon *icon)
257 GEmblem *emblem = G_EMBLEM (icon);
258 guint hash;
260 hash = g_icon_hash (g_emblem_get_icon (emblem));
261 hash ^= emblem->origin;
263 return hash;
266 static gboolean
267 g_emblem_equal (GIcon *icon1,
268 GIcon *icon2)
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);
277 static gboolean
278 g_emblem_to_tokens (GIcon *icon,
279 GPtrArray *tokens,
280 gint *out_version)
282 GEmblem *emblem = G_EMBLEM (icon);
283 char *s;
285 /* GEmblem are encoded as
287 * <origin> <icon>
290 g_return_val_if_fail (out_version != NULL, FALSE);
292 *out_version = 0;
294 s = g_icon_to_string (emblem->icon);
295 if (s == NULL)
296 return FALSE;
298 g_ptr_array_add (tokens, s);
300 s = g_strdup_printf ("%d", emblem->origin);
301 g_ptr_array_add (tokens, s);
303 return TRUE;
306 static GIcon *
307 g_emblem_from_tokens (gchar **tokens,
308 gint num_tokens,
309 gint version,
310 GError **error)
312 GEmblem *emblem;
313 GIcon *icon;
314 GEmblemOrigin origin;
316 emblem = NULL;
318 if (version != 0)
320 g_set_error (error,
321 G_IO_ERROR,
322 G_IO_ERROR_INVALID_ARGUMENT,
323 _("Can’t handle version %d of GEmblem encoding"),
324 version);
325 return NULL;
328 if (num_tokens != 2)
330 g_set_error (error,
331 G_IO_ERROR,
332 G_IO_ERROR_INVALID_ARGUMENT,
333 _("Malformed number of tokens (%d) in GEmblem encoding"),
334 num_tokens);
335 return NULL;
338 icon = g_icon_new_for_string (tokens[0], error);
340 if (icon == NULL)
341 return NULL;
343 origin = atoi (tokens[1]);
345 emblem = g_emblem_new_with_origin (icon, origin);
346 g_object_unref (icon);
348 return G_ICON (emblem);
351 static GVariant *
352 g_emblem_serialize (GIcon *icon)
354 GEmblem *emblem = G_EMBLEM (icon);
355 GVariant *icon_data;
356 GEnumValue *origin;
357 GVariant *result;
359 icon_data = g_icon_serialize (emblem->icon);
360 if (!icon_data)
361 return NULL;
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);
368 return result;
371 static void
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;