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