/src/glib/gio/gemblemedicon.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ |
2 | | |
3 | | /* GIO - GLib Input, Output and Streaming Library |
4 | | * |
5 | | * Copyright (C) 2006-2007 Red Hat, Inc. |
6 | | * |
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. |
11 | | * |
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. |
16 | | * |
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/>. |
19 | | * |
20 | | * Author: Matthias Clasen <mclasen@redhat.com> |
21 | | * Clemens N. Buss <cebuzz@gmail.com> |
22 | | */ |
23 | | |
24 | | #include <config.h> |
25 | | |
26 | | #include <string.h> |
27 | | |
28 | | #include "gemblemedicon.h" |
29 | | #include "glibintl.h" |
30 | | #include "gioerror.h" |
31 | | |
32 | | |
33 | | /** |
34 | | * SECTION:gemblemedicon |
35 | | * @short_description: Icon with emblems |
36 | | * @include: gio/gio.h |
37 | | * @see_also: #GIcon, #GLoadableIcon, #GThemedIcon, #GEmblem |
38 | | * |
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(). |
42 | | * |
43 | | * Note that #GEmblemedIcon allows no control over the position |
44 | | * of the emblems. See also #GEmblem for more information. |
45 | | **/ |
46 | | |
47 | | enum { |
48 | | PROP_GICON = 1, |
49 | | NUM_PROPERTIES |
50 | | }; |
51 | | |
52 | | struct _GEmblemedIconPrivate { |
53 | | GIcon *icon; |
54 | | GList *emblems; |
55 | | }; |
56 | | |
57 | | static GParamSpec *properties[NUM_PROPERTIES] = { NULL, }; |
58 | | |
59 | | static void g_emblemed_icon_icon_iface_init (GIconIface *iface); |
60 | | |
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)) |
65 | | |
66 | | |
67 | | static void |
68 | | g_emblemed_icon_finalize (GObject *object) |
69 | 0 | { |
70 | 0 | GEmblemedIcon *emblemed; |
71 | |
|
72 | 0 | emblemed = G_EMBLEMED_ICON (object); |
73 | |
|
74 | 0 | g_clear_object (&emblemed->priv->icon); |
75 | 0 | g_list_free_full (emblemed->priv->emblems, g_object_unref); |
76 | |
|
77 | 0 | (*G_OBJECT_CLASS (g_emblemed_icon_parent_class)->finalize) (object); |
78 | 0 | } |
79 | | |
80 | | static void |
81 | | g_emblemed_icon_set_property (GObject *object, |
82 | | guint property_id, |
83 | | const GValue *value, |
84 | | GParamSpec *pspec) |
85 | 0 | { |
86 | 0 | GEmblemedIcon *self = G_EMBLEMED_ICON (object); |
87 | |
|
88 | 0 | switch (property_id) |
89 | 0 | { |
90 | 0 | case PROP_GICON: |
91 | 0 | self->priv->icon = g_value_dup_object (value); |
92 | 0 | break; |
93 | 0 | default: |
94 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
95 | 0 | break; |
96 | 0 | } |
97 | 0 | } |
98 | | |
99 | | static void |
100 | | g_emblemed_icon_get_property (GObject *object, |
101 | | guint property_id, |
102 | | GValue *value, |
103 | | GParamSpec *pspec) |
104 | 0 | { |
105 | 0 | GEmblemedIcon *self = G_EMBLEMED_ICON (object); |
106 | |
|
107 | 0 | switch (property_id) |
108 | 0 | { |
109 | 0 | case PROP_GICON: |
110 | 0 | g_value_set_object (value, self->priv->icon); |
111 | 0 | break; |
112 | 0 | default: |
113 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
114 | 0 | break; |
115 | 0 | } |
116 | 0 | } |
117 | | |
118 | | static void |
119 | | g_emblemed_icon_class_init (GEmblemedIconClass *klass) |
120 | 0 | { |
121 | 0 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
122 | |
|
123 | 0 | gobject_class->finalize = g_emblemed_icon_finalize; |
124 | 0 | gobject_class->set_property = g_emblemed_icon_set_property; |
125 | 0 | gobject_class->get_property = g_emblemed_icon_get_property; |
126 | |
|
127 | 0 | properties[PROP_GICON] = |
128 | 0 | g_param_spec_object ("gicon", |
129 | 0 | P_("The base GIcon"), |
130 | 0 | P_("The GIcon to attach emblems to"), |
131 | 0 | G_TYPE_ICON, |
132 | 0 | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS); |
133 | |
|
134 | 0 | g_object_class_install_properties (gobject_class, NUM_PROPERTIES, properties); |
135 | 0 | } |
136 | | |
137 | | static void |
138 | | g_emblemed_icon_init (GEmblemedIcon *emblemed) |
139 | 0 | { |
140 | 0 | emblemed->priv = g_emblemed_icon_get_instance_private (emblemed); |
141 | 0 | } |
142 | | |
143 | | /** |
144 | | * g_emblemed_icon_new: |
145 | | * @icon: a #GIcon |
146 | | * @emblem: (nullable): a #GEmblem, or %NULL |
147 | | * |
148 | | * Creates a new emblemed icon for @icon with the emblem @emblem. |
149 | | * |
150 | | * Returns: (transfer full) (type GEmblemedIcon): a new #GIcon |
151 | | * |
152 | | * Since: 2.18 |
153 | | **/ |
154 | | GIcon * |
155 | | g_emblemed_icon_new (GIcon *icon, |
156 | | GEmblem *emblem) |
157 | 0 | { |
158 | 0 | GEmblemedIcon *emblemed; |
159 | | |
160 | 0 | g_return_val_if_fail (G_IS_ICON (icon), NULL); |
161 | 0 | g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL); |
162 | | |
163 | 0 | emblemed = G_EMBLEMED_ICON (g_object_new (G_TYPE_EMBLEMED_ICON, |
164 | 0 | "gicon", icon, |
165 | 0 | NULL)); |
166 | |
|
167 | 0 | if (emblem != NULL) |
168 | 0 | g_emblemed_icon_add_emblem (emblemed, emblem); |
169 | |
|
170 | 0 | return G_ICON (emblemed); |
171 | 0 | } |
172 | | |
173 | | |
174 | | /** |
175 | | * g_emblemed_icon_get_icon: |
176 | | * @emblemed: a #GEmblemedIcon |
177 | | * |
178 | | * Gets the main icon for @emblemed. |
179 | | * |
180 | | * Returns: (transfer none): a #GIcon that is owned by @emblemed |
181 | | * |
182 | | * Since: 2.18 |
183 | | **/ |
184 | | GIcon * |
185 | | g_emblemed_icon_get_icon (GEmblemedIcon *emblemed) |
186 | 0 | { |
187 | 0 | g_return_val_if_fail (G_IS_EMBLEMED_ICON (emblemed), NULL); |
188 | | |
189 | 0 | return emblemed->priv->icon; |
190 | 0 | } |
191 | | |
192 | | /** |
193 | | * g_emblemed_icon_get_emblems: |
194 | | * @emblemed: a #GEmblemedIcon |
195 | | * |
196 | | * Gets the list of emblems for the @icon. |
197 | | * |
198 | | * Returns: (element-type Gio.Emblem) (transfer none): a #GList of |
199 | | * #GEmblems that is owned by @emblemed |
200 | | * |
201 | | * Since: 2.18 |
202 | | **/ |
203 | | |
204 | | GList * |
205 | | g_emblemed_icon_get_emblems (GEmblemedIcon *emblemed) |
206 | 0 | { |
207 | 0 | g_return_val_if_fail (G_IS_EMBLEMED_ICON (emblemed), NULL); |
208 | | |
209 | 0 | return emblemed->priv->emblems; |
210 | 0 | } |
211 | | |
212 | | /** |
213 | | * g_emblemed_icon_clear_emblems: |
214 | | * @emblemed: a #GEmblemedIcon |
215 | | * |
216 | | * Removes all the emblems from @icon. |
217 | | * |
218 | | * Since: 2.28 |
219 | | **/ |
220 | | void |
221 | | g_emblemed_icon_clear_emblems (GEmblemedIcon *emblemed) |
222 | 0 | { |
223 | 0 | g_return_if_fail (G_IS_EMBLEMED_ICON (emblemed)); |
224 | | |
225 | 0 | if (emblemed->priv->emblems == NULL) |
226 | 0 | return; |
227 | | |
228 | 0 | g_list_free_full (emblemed->priv->emblems, g_object_unref); |
229 | 0 | emblemed->priv->emblems = NULL; |
230 | 0 | } |
231 | | |
232 | | static gint |
233 | | g_emblem_comp (GEmblem *a, |
234 | | GEmblem *b) |
235 | 0 | { |
236 | 0 | guint hash_a = g_icon_hash (G_ICON (a)); |
237 | 0 | guint hash_b = g_icon_hash (G_ICON (b)); |
238 | |
|
239 | 0 | if(hash_a < hash_b) |
240 | 0 | return -1; |
241 | | |
242 | 0 | if(hash_a == hash_b) |
243 | 0 | return 0; |
244 | | |
245 | 0 | return 1; |
246 | 0 | } |
247 | | |
248 | | /** |
249 | | * g_emblemed_icon_add_emblem: |
250 | | * @emblemed: a #GEmblemedIcon |
251 | | * @emblem: a #GEmblem |
252 | | * |
253 | | * Adds @emblem to the #GList of #GEmblems. |
254 | | * |
255 | | * Since: 2.18 |
256 | | **/ |
257 | | void |
258 | | g_emblemed_icon_add_emblem (GEmblemedIcon *emblemed, |
259 | | GEmblem *emblem) |
260 | 0 | { |
261 | 0 | g_return_if_fail (G_IS_EMBLEMED_ICON (emblemed)); |
262 | 0 | g_return_if_fail (G_IS_EMBLEM (emblem)); |
263 | | |
264 | 0 | g_object_ref (emblem); |
265 | 0 | emblemed->priv->emblems = g_list_insert_sorted (emblemed->priv->emblems, emblem, |
266 | 0 | (GCompareFunc) g_emblem_comp); |
267 | 0 | } |
268 | | |
269 | | static guint |
270 | | g_emblemed_icon_hash (GIcon *icon) |
271 | 0 | { |
272 | 0 | GEmblemedIcon *emblemed = G_EMBLEMED_ICON (icon); |
273 | 0 | GList *list; |
274 | 0 | guint hash = g_icon_hash (emblemed->priv->icon); |
275 | |
|
276 | 0 | for (list = emblemed->priv->emblems; list != NULL; list = list->next) |
277 | 0 | hash ^= g_icon_hash (G_ICON (list->data)); |
278 | |
|
279 | 0 | return hash; |
280 | 0 | } |
281 | | |
282 | | static gboolean |
283 | | g_emblemed_icon_equal (GIcon *icon1, |
284 | | GIcon *icon2) |
285 | 0 | { |
286 | 0 | GEmblemedIcon *emblemed1 = G_EMBLEMED_ICON (icon1); |
287 | 0 | GEmblemedIcon *emblemed2 = G_EMBLEMED_ICON (icon2); |
288 | 0 | GList *list1, *list2; |
289 | |
|
290 | 0 | if (!g_icon_equal (emblemed1->priv->icon, emblemed2->priv->icon)) |
291 | 0 | return FALSE; |
292 | | |
293 | 0 | list1 = emblemed1->priv->emblems; |
294 | 0 | list2 = emblemed2->priv->emblems; |
295 | |
|
296 | 0 | while (list1 && list2) |
297 | 0 | { |
298 | 0 | if (!g_icon_equal (G_ICON (list1->data), G_ICON (list2->data))) |
299 | 0 | return FALSE; |
300 | | |
301 | 0 | list1 = list1->next; |
302 | 0 | list2 = list2->next; |
303 | 0 | } |
304 | | |
305 | 0 | return list1 == NULL && list2 == NULL; |
306 | 0 | } |
307 | | |
308 | | static gboolean |
309 | | g_emblemed_icon_to_tokens (GIcon *icon, |
310 | | GPtrArray *tokens, |
311 | | gint *out_version) |
312 | 0 | { |
313 | 0 | GEmblemedIcon *emblemed_icon = G_EMBLEMED_ICON (icon); |
314 | 0 | GList *l; |
315 | 0 | char *s; |
316 | | |
317 | | /* GEmblemedIcons are encoded as |
318 | | * |
319 | | * <encoded_icon> [<encoded_emblem_icon>]* |
320 | | */ |
321 | |
|
322 | 0 | g_return_val_if_fail (out_version != NULL, FALSE); |
323 | | |
324 | 0 | *out_version = 0; |
325 | |
|
326 | 0 | s = g_icon_to_string (emblemed_icon->priv->icon); |
327 | 0 | if (s == NULL) |
328 | 0 | return FALSE; |
329 | | |
330 | 0 | g_ptr_array_add (tokens, s); |
331 | |
|
332 | 0 | for (l = emblemed_icon->priv->emblems; l != NULL; l = l->next) |
333 | 0 | { |
334 | 0 | GIcon *emblem_icon = G_ICON (l->data); |
335 | |
|
336 | 0 | s = g_icon_to_string (emblem_icon); |
337 | 0 | if (s == NULL) |
338 | 0 | return FALSE; |
339 | | |
340 | 0 | g_ptr_array_add (tokens, s); |
341 | 0 | } |
342 | | |
343 | 0 | return TRUE; |
344 | 0 | } |
345 | | |
346 | | static GIcon * |
347 | | g_emblemed_icon_from_tokens (gchar **tokens, |
348 | | gint num_tokens, |
349 | | gint version, |
350 | | GError **error) |
351 | 0 | { |
352 | 0 | GEmblemedIcon *emblemed_icon; |
353 | 0 | int n; |
354 | |
|
355 | 0 | emblemed_icon = NULL; |
356 | |
|
357 | 0 | if (version != 0) |
358 | 0 | { |
359 | 0 | g_set_error (error, |
360 | 0 | G_IO_ERROR, |
361 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
362 | 0 | _("Can’t handle version %d of GEmblemedIcon encoding"), |
363 | 0 | version); |
364 | 0 | goto fail; |
365 | 0 | } |
366 | | |
367 | 0 | if (num_tokens < 1) |
368 | 0 | { |
369 | 0 | g_set_error (error, |
370 | 0 | G_IO_ERROR, |
371 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
372 | 0 | _("Malformed number of tokens (%d) in GEmblemedIcon encoding"), |
373 | 0 | num_tokens); |
374 | 0 | goto fail; |
375 | 0 | } |
376 | | |
377 | 0 | emblemed_icon = g_object_new (G_TYPE_EMBLEMED_ICON, NULL); |
378 | 0 | emblemed_icon->priv->icon = g_icon_new_for_string (tokens[0], error); |
379 | 0 | if (emblemed_icon->priv->icon == NULL) |
380 | 0 | goto fail; |
381 | | |
382 | 0 | for (n = 1; n < num_tokens; n++) |
383 | 0 | { |
384 | 0 | GIcon *emblem; |
385 | |
|
386 | 0 | emblem = g_icon_new_for_string (tokens[n], error); |
387 | 0 | if (emblem == NULL) |
388 | 0 | goto fail; |
389 | | |
390 | 0 | if (!G_IS_EMBLEM (emblem)) |
391 | 0 | { |
392 | 0 | g_set_error_literal (error, |
393 | 0 | G_IO_ERROR, |
394 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
395 | 0 | _("Expected a GEmblem for GEmblemedIcon")); |
396 | 0 | g_object_unref (emblem); |
397 | 0 | goto fail; |
398 | 0 | } |
399 | | |
400 | 0 | emblemed_icon->priv->emblems = g_list_append (emblemed_icon->priv->emblems, emblem); |
401 | 0 | } |
402 | | |
403 | 0 | return G_ICON (emblemed_icon); |
404 | | |
405 | 0 | fail: |
406 | 0 | if (emblemed_icon != NULL) |
407 | 0 | g_object_unref (emblemed_icon); |
408 | 0 | return NULL; |
409 | 0 | } |
410 | | |
411 | | static GVariant * |
412 | | g_emblemed_icon_serialize (GIcon *icon) |
413 | 0 | { |
414 | 0 | GEmblemedIcon *emblemed_icon = G_EMBLEMED_ICON (icon); |
415 | 0 | GVariantBuilder builder; |
416 | 0 | GVariant *icon_data; |
417 | 0 | GList *node; |
418 | |
|
419 | 0 | icon_data = g_icon_serialize (emblemed_icon->priv->icon); |
420 | 0 | if (!icon_data) |
421 | 0 | return NULL; |
422 | | |
423 | 0 | g_variant_builder_init (&builder, G_VARIANT_TYPE ("(va(va{sv}))")); |
424 | |
|
425 | 0 | g_variant_builder_add (&builder, "v", icon_data); |
426 | 0 | g_variant_unref (icon_data); |
427 | |
|
428 | 0 | g_variant_builder_open (&builder, G_VARIANT_TYPE ("a(va{sv})")); |
429 | 0 | for (node = emblemed_icon->priv->emblems; node != NULL; node = node->next) |
430 | 0 | { |
431 | 0 | icon_data = g_icon_serialize (node->data); |
432 | 0 | if (icon_data) |
433 | 0 | { |
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... |
437 | | */ |
438 | 0 | if (g_variant_is_of_type (icon_data, G_VARIANT_TYPE ("(sv)"))) |
439 | 0 | { |
440 | 0 | const gchar *name; |
441 | 0 | GVariant *content; |
442 | |
|
443 | 0 | g_variant_get (icon_data, "(&sv)", &name, &content); |
444 | |
|
445 | 0 | if (g_str_equal (name, "emblem") && g_variant_is_of_type (content, G_VARIANT_TYPE ("(va{sv})"))) |
446 | 0 | g_variant_builder_add (&builder, "@(va{sv})", content); |
447 | |
|
448 | 0 | g_variant_unref (content); |
449 | 0 | } |
450 | |
|
451 | 0 | g_variant_unref (icon_data); |
452 | 0 | } |
453 | 0 | } |
454 | 0 | g_variant_builder_close (&builder); |
455 | |
|
456 | 0 | return g_variant_new ("(sv)", "emblemed", g_variant_builder_end (&builder)); |
457 | 0 | } |
458 | | |
459 | | static void |
460 | | g_emblemed_icon_icon_iface_init (GIconIface *iface) |
461 | 0 | { |
462 | 0 | iface->hash = g_emblemed_icon_hash; |
463 | 0 | iface->equal = g_emblemed_icon_equal; |
464 | 0 | iface->to_tokens = g_emblemed_icon_to_tokens; |
465 | 0 | iface->from_tokens = g_emblemed_icon_from_tokens; |
466 | 0 | iface->serialize = g_emblemed_icon_serialize; |
467 | 0 | } |