Coverage Report

Created: 2025-07-23 08:13

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