tests: Ensure GIO tests which need dbus-daemon are run
[glib.git] / gio / gemblemedicon.c
blob42d013dfa8313d57cc928af576af0c5b5543c49d
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
4 *
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>
24 #include <config.h>
26 #include <string.h>
28 #include "gemblemedicon.h"
29 #include "glibintl.h"
30 #include "gioerror.h"
33 /**
34 * SECTION:gemblemedicon
35 * @short_description: Icon with emblems
36 * @include: gio/gio.h
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.
45 **/
47 enum {
48 PROP_GICON = 1,
49 NUM_PROPERTIES
52 struct _GEmblemedIconPrivate {
53 GIcon *icon;
54 GList *emblems;
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))
67 static void
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);
80 static void
81 g_emblemed_icon_set_property (GObject *object,
82 guint property_id,
83 const GValue *value,
84 GParamSpec *pspec)
86 GEmblemedIcon *self = G_EMBLEMED_ICON (object);
88 switch (property_id)
90 case PROP_GICON:
91 self->priv->icon = g_value_dup_object (value);
92 break;
93 default:
94 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
95 break;
99 static void
100 g_emblemed_icon_get_property (GObject *object,
101 guint property_id,
102 GValue *value,
103 GParamSpec *pspec)
105 GEmblemedIcon *self = G_EMBLEMED_ICON (object);
107 switch (property_id)
109 case PROP_GICON:
110 g_value_set_object (value, self->priv->icon);
111 break;
112 default:
113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
114 break;
118 static void
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"),
131 G_TYPE_ICON,
132 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
134 g_object_class_install_properties (gobject_class, NUM_PROPERTIES, properties);
137 static void
138 g_emblemed_icon_init (GEmblemedIcon *emblemed)
140 emblemed->priv = g_emblemed_icon_get_instance_private (emblemed);
144 * g_emblemed_icon_new:
145 * @icon: a #GIcon
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
152 * Since: 2.18
154 GIcon *
155 g_emblemed_icon_new (GIcon *icon,
156 GEmblem *emblem)
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,
164 "gicon", icon,
165 NULL));
167 if (emblem != NULL)
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
182 * Since: 2.18
184 GIcon *
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
201 * Since: 2.18
204 GList *
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.
218 * Since: 2.28
220 void
221 g_emblemed_icon_clear_emblems (GEmblemedIcon *emblemed)
223 g_return_if_fail (G_IS_EMBLEMED_ICON (emblemed));
225 if (emblemed->priv->emblems == NULL)
226 return;
228 g_list_free_full (emblemed->priv->emblems, g_object_unref);
229 emblemed->priv->emblems = NULL;
232 static gint
233 g_emblem_comp (GEmblem *a,
234 GEmblem *b)
236 guint hash_a = g_icon_hash (G_ICON (a));
237 guint hash_b = g_icon_hash (G_ICON (b));
239 if(hash_a < hash_b)
240 return -1;
242 if(hash_a == hash_b)
243 return 0;
245 return 1;
249 * g_emblemed_icon_add_emblem:
250 * @emblemed: a #GEmblemedIcon
251 * @emblem: a #GEmblem
253 * Adds @emblem to the #GList of #GEmblems.
255 * Since: 2.18
257 void
258 g_emblemed_icon_add_emblem (GEmblemedIcon *emblemed,
259 GEmblem *emblem)
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);
269 static guint
270 g_emblemed_icon_hash (GIcon *icon)
272 GEmblemedIcon *emblemed = G_EMBLEMED_ICON (icon);
273 GList *list;
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));
279 return hash;
282 static gboolean
283 g_emblemed_icon_equal (GIcon *icon1,
284 GIcon *icon2)
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))
291 return FALSE;
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)))
299 return FALSE;
301 list1 = list1->next;
302 list2 = list2->next;
305 return list1 == NULL && list2 == NULL;
308 static gboolean
309 g_emblemed_icon_to_tokens (GIcon *icon,
310 GPtrArray *tokens,
311 gint *out_version)
313 GEmblemedIcon *emblemed_icon = G_EMBLEMED_ICON (icon);
314 GList *l;
315 char *s;
317 /* GEmblemedIcons are encoded as
319 * <encoded_icon> [<encoded_emblem_icon>]*
322 g_return_val_if_fail (out_version != NULL, FALSE);
324 *out_version = 0;
326 s = g_icon_to_string (emblemed_icon->priv->icon);
327 if (s == NULL)
328 return FALSE;
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);
337 if (s == NULL)
338 return FALSE;
340 g_ptr_array_add (tokens, s);
343 return TRUE;
346 static GIcon *
347 g_emblemed_icon_from_tokens (gchar **tokens,
348 gint num_tokens,
349 gint version,
350 GError **error)
352 GEmblemedIcon *emblemed_icon;
353 int n;
355 emblemed_icon = NULL;
357 if (version != 0)
359 g_set_error (error,
360 G_IO_ERROR,
361 G_IO_ERROR_INVALID_ARGUMENT,
362 _("Can’t handle version %d of GEmblemedIcon encoding"),
363 version);
364 goto fail;
367 if (num_tokens < 1)
369 g_set_error (error,
370 G_IO_ERROR,
371 G_IO_ERROR_INVALID_ARGUMENT,
372 _("Malformed number of tokens (%d) in GEmblemedIcon encoding"),
373 num_tokens);
374 goto fail;
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)
380 goto fail;
382 for (n = 1; n < num_tokens; n++)
384 GIcon *emblem;
386 emblem = g_icon_new_for_string (tokens[n], error);
387 if (emblem == NULL)
388 goto fail;
390 if (!G_IS_EMBLEM (emblem))
392 g_set_error_literal (error,
393 G_IO_ERROR,
394 G_IO_ERROR_INVALID_ARGUMENT,
395 _("Expected a GEmblem for GEmblemedIcon"));
396 g_object_unref (emblem);
397 goto fail;
400 emblemed_icon->priv->emblems = g_list_append (emblemed_icon->priv->emblems, emblem);
403 return G_ICON (emblemed_icon);
405 fail:
406 if (emblemed_icon != NULL)
407 g_object_unref (emblemed_icon);
408 return NULL;
411 static GVariant *
412 g_emblemed_icon_serialize (GIcon *icon)
414 GEmblemedIcon *emblemed_icon = G_EMBLEMED_ICON (icon);
415 GVariantBuilder builder;
416 GVariant *icon_data;
417 GList *node;
419 icon_data = g_icon_serialize (emblemed_icon->priv->icon);
420 if (!icon_data)
421 return NULL;
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);
432 if (icon_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)")))
440 const gchar *name;
441 GVariant *content;
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));
459 static void
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;