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 | { |
70 | GEmblemedIcon *emblemed; |
71 | |
72 | emblemed = G_EMBLEMED_ICON (object); |
73 | |
74 | g_clear_object (&emblemed->priv->icon); |
75 | g_list_free_full (list: emblemed->priv->emblems, free_func: g_object_unref); |
76 | |
77 | (*G_OBJECT_CLASS (g_emblemed_icon_parent_class)->finalize) (object); |
78 | } |
79 | |
80 | static void |
81 | g_emblemed_icon_set_property (GObject *object, |
82 | guint property_id, |
83 | const GValue *value, |
84 | GParamSpec *pspec) |
85 | { |
86 | GEmblemedIcon *self = G_EMBLEMED_ICON (object); |
87 | |
88 | switch (property_id) |
89 | { |
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; |
96 | } |
97 | } |
98 | |
99 | static void |
100 | g_emblemed_icon_get_property (GObject *object, |
101 | guint property_id, |
102 | GValue *value, |
103 | GParamSpec *pspec) |
104 | { |
105 | GEmblemedIcon *self = G_EMBLEMED_ICON (object); |
106 | |
107 | switch (property_id) |
108 | { |
109 | case PROP_GICON: |
110 | g_value_set_object (value, v_object: self->priv->icon); |
111 | break; |
112 | default: |
113 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
114 | break; |
115 | } |
116 | } |
117 | |
118 | static void |
119 | g_emblemed_icon_class_init (GEmblemedIconClass *klass) |
120 | { |
121 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
122 | |
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; |
126 | |
127 | properties[PROP_GICON] = |
128 | g_param_spec_object (name: "gicon" , |
129 | P_("The base GIcon" ), |
130 | P_("The GIcon to attach emblems to" ), |
131 | G_TYPE_ICON, |
132 | flags: G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS); |
133 | |
134 | g_object_class_install_properties (oclass: gobject_class, n_pspecs: NUM_PROPERTIES, pspecs: properties); |
135 | } |
136 | |
137 | static void |
138 | g_emblemed_icon_init (GEmblemedIcon *emblemed) |
139 | { |
140 | emblemed->priv = g_emblemed_icon_get_instance_private (self: emblemed); |
141 | } |
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 | { |
158 | GEmblemedIcon *emblemed; |
159 | |
160 | g_return_val_if_fail (G_IS_ICON (icon), NULL); |
161 | g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL); |
162 | |
163 | emblemed = G_EMBLEMED_ICON (g_object_new (G_TYPE_EMBLEMED_ICON, |
164 | "gicon" , icon, |
165 | NULL)); |
166 | |
167 | if (emblem != NULL) |
168 | g_emblemed_icon_add_emblem (emblemed, emblem); |
169 | |
170 | return G_ICON (emblemed); |
171 | } |
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 | { |
187 | g_return_val_if_fail (G_IS_EMBLEMED_ICON (emblemed), NULL); |
188 | |
189 | return emblemed->priv->icon; |
190 | } |
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 | { |
207 | g_return_val_if_fail (G_IS_EMBLEMED_ICON (emblemed), NULL); |
208 | |
209 | return emblemed->priv->emblems; |
210 | } |
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 | { |
223 | g_return_if_fail (G_IS_EMBLEMED_ICON (emblemed)); |
224 | |
225 | if (emblemed->priv->emblems == NULL) |
226 | return; |
227 | |
228 | g_list_free_full (list: emblemed->priv->emblems, free_func: g_object_unref); |
229 | emblemed->priv->emblems = NULL; |
230 | } |
231 | |
232 | static gint |
233 | g_emblem_comp (GEmblem *a, |
234 | GEmblem *b) |
235 | { |
236 | guint hash_a = g_icon_hash (G_ICON (a)); |
237 | guint hash_b = g_icon_hash (G_ICON (b)); |
238 | |
239 | if(hash_a < hash_b) |
240 | return -1; |
241 | |
242 | if(hash_a == hash_b) |
243 | return 0; |
244 | |
245 | return 1; |
246 | } |
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 | { |
261 | g_return_if_fail (G_IS_EMBLEMED_ICON (emblemed)); |
262 | g_return_if_fail (G_IS_EMBLEM (emblem)); |
263 | |
264 | g_object_ref (emblem); |
265 | emblemed->priv->emblems = g_list_insert_sorted (list: emblemed->priv->emblems, data: emblem, |
266 | func: (GCompareFunc) g_emblem_comp); |
267 | } |
268 | |
269 | static guint |
270 | g_emblemed_icon_hash (GIcon *icon) |
271 | { |
272 | GEmblemedIcon *emblemed = G_EMBLEMED_ICON (icon); |
273 | GList *list; |
274 | guint hash = g_icon_hash (icon: emblemed->priv->icon); |
275 | |
276 | for (list = emblemed->priv->emblems; list != NULL; list = list->next) |
277 | hash ^= g_icon_hash (G_ICON (list->data)); |
278 | |
279 | return hash; |
280 | } |
281 | |
282 | static gboolean |
283 | g_emblemed_icon_equal (GIcon *icon1, |
284 | GIcon *icon2) |
285 | { |
286 | GEmblemedIcon *emblemed1 = G_EMBLEMED_ICON (icon1); |
287 | GEmblemedIcon *emblemed2 = G_EMBLEMED_ICON (icon2); |
288 | GList *list1, *list2; |
289 | |
290 | if (!g_icon_equal (icon1: emblemed1->priv->icon, icon2: emblemed2->priv->icon)) |
291 | return FALSE; |
292 | |
293 | list1 = emblemed1->priv->emblems; |
294 | list2 = emblemed2->priv->emblems; |
295 | |
296 | while (list1 && list2) |
297 | { |
298 | if (!g_icon_equal (G_ICON (list1->data), G_ICON (list2->data))) |
299 | return FALSE; |
300 | |
301 | list1 = list1->next; |
302 | list2 = list2->next; |
303 | } |
304 | |
305 | return list1 == NULL && list2 == NULL; |
306 | } |
307 | |
308 | static gboolean |
309 | g_emblemed_icon_to_tokens (GIcon *icon, |
310 | GPtrArray *tokens, |
311 | gint *out_version) |
312 | { |
313 | GEmblemedIcon *emblemed_icon = G_EMBLEMED_ICON (icon); |
314 | GList *l; |
315 | char *s; |
316 | |
317 | /* GEmblemedIcons are encoded as |
318 | * |
319 | * <encoded_icon> [<encoded_emblem_icon>]* |
320 | */ |
321 | |
322 | g_return_val_if_fail (out_version != NULL, FALSE); |
323 | |
324 | *out_version = 0; |
325 | |
326 | s = g_icon_to_string (icon: emblemed_icon->priv->icon); |
327 | if (s == NULL) |
328 | return FALSE; |
329 | |
330 | g_ptr_array_add (array: tokens, data: s); |
331 | |
332 | for (l = emblemed_icon->priv->emblems; l != NULL; l = l->next) |
333 | { |
334 | GIcon *emblem_icon = G_ICON (l->data); |
335 | |
336 | s = g_icon_to_string (icon: emblem_icon); |
337 | if (s == NULL) |
338 | return FALSE; |
339 | |
340 | g_ptr_array_add (array: tokens, data: s); |
341 | } |
342 | |
343 | return TRUE; |
344 | } |
345 | |
346 | static GIcon * |
347 | g_emblemed_icon_from_tokens (gchar **tokens, |
348 | gint num_tokens, |
349 | gint version, |
350 | GError **error) |
351 | { |
352 | GEmblemedIcon *emblemed_icon; |
353 | int n; |
354 | |
355 | emblemed_icon = NULL; |
356 | |
357 | if (version != 0) |
358 | { |
359 | g_set_error (err: error, |
360 | G_IO_ERROR, |
361 | code: G_IO_ERROR_INVALID_ARGUMENT, |
362 | _("Can’t handle version %d of GEmblemedIcon encoding" ), |
363 | version); |
364 | goto fail; |
365 | } |
366 | |
367 | if (num_tokens < 1) |
368 | { |
369 | g_set_error (err: error, |
370 | G_IO_ERROR, |
371 | code: G_IO_ERROR_INVALID_ARGUMENT, |
372 | _("Malformed number of tokens (%d) in GEmblemedIcon encoding" ), |
373 | num_tokens); |
374 | goto fail; |
375 | } |
376 | |
377 | emblemed_icon = g_object_new (G_TYPE_EMBLEMED_ICON, NULL); |
378 | emblemed_icon->priv->icon = g_icon_new_for_string (str: tokens[0], error); |
379 | if (emblemed_icon->priv->icon == NULL) |
380 | goto fail; |
381 | |
382 | for (n = 1; n < num_tokens; n++) |
383 | { |
384 | GIcon *emblem; |
385 | |
386 | emblem = g_icon_new_for_string (str: tokens[n], error); |
387 | if (emblem == NULL) |
388 | goto fail; |
389 | |
390 | if (!G_IS_EMBLEM (emblem)) |
391 | { |
392 | g_set_error_literal (err: error, |
393 | G_IO_ERROR, |
394 | code: G_IO_ERROR_INVALID_ARGUMENT, |
395 | _("Expected a GEmblem for GEmblemedIcon" )); |
396 | g_object_unref (object: emblem); |
397 | goto fail; |
398 | } |
399 | |
400 | emblemed_icon->priv->emblems = g_list_append (list: emblemed_icon->priv->emblems, data: emblem); |
401 | } |
402 | |
403 | return G_ICON (emblemed_icon); |
404 | |
405 | fail: |
406 | if (emblemed_icon != NULL) |
407 | g_object_unref (object: emblemed_icon); |
408 | return NULL; |
409 | } |
410 | |
411 | static GVariant * |
412 | g_emblemed_icon_serialize (GIcon *icon) |
413 | { |
414 | GEmblemedIcon *emblemed_icon = G_EMBLEMED_ICON (icon); |
415 | GVariantBuilder builder; |
416 | GVariant *icon_data; |
417 | GList *node; |
418 | |
419 | icon_data = g_icon_serialize (icon: emblemed_icon->priv->icon); |
420 | if (!icon_data) |
421 | return NULL; |
422 | |
423 | g_variant_builder_init (builder: &builder, G_VARIANT_TYPE ("(va(va{sv}))" )); |
424 | |
425 | g_variant_builder_add (builder: &builder, format_string: "v" , icon_data); |
426 | g_variant_unref (value: icon_data); |
427 | |
428 | g_variant_builder_open (builder: &builder, G_VARIANT_TYPE ("a(va{sv})" )); |
429 | for (node = emblemed_icon->priv->emblems; node != NULL; node = node->next) |
430 | { |
431 | icon_data = g_icon_serialize (icon: node->data); |
432 | if (icon_data) |
433 | { |
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 | if (g_variant_is_of_type (value: icon_data, G_VARIANT_TYPE ("(sv)" ))) |
439 | { |
440 | const gchar *name; |
441 | GVariant *content; |
442 | |
443 | g_variant_get (value: icon_data, format_string: "(&sv)" , &name, &content); |
444 | |
445 | if (g_str_equal (v1: name, v2: "emblem" ) && g_variant_is_of_type (value: content, G_VARIANT_TYPE ("(va{sv})" ))) |
446 | g_variant_builder_add (builder: &builder, format_string: "@(va{sv})" , content); |
447 | |
448 | g_variant_unref (value: content); |
449 | } |
450 | |
451 | g_variant_unref (value: icon_data); |
452 | } |
453 | } |
454 | g_variant_builder_close (builder: &builder); |
455 | |
456 | return g_variant_new (format_string: "(sv)" , "emblemed" , g_variant_builder_end (builder: &builder)); |
457 | } |
458 | |
459 | static void |
460 | g_emblemed_icon_icon_iface_init (GIconIface *iface) |
461 | { |
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; |
467 | } |
468 | |