Coverage Report

Created: 2026-07-25 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/glib/gio/gemblem.c
Line
Count
Source
1
/* GIO - GLib Input, Output and Streaming Library
2
 *
3
 * Copyright (C) 2008 Clemens N. Buss <cebuzz@gmail.com>
4
 *
5
 * SPDX-License-Identifier: LGPL-2.1-or-later
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
21
#include <config.h>
22
23
#include "gicon.h"
24
#include "gemblem.h"
25
#include "glibintl.h"
26
#include "gioenums.h"
27
#include "gioenumtypes.h"
28
#include "gioerror.h"
29
#include <stdint.h>
30
#include <stdlib.h>
31
#include <string.h>
32
33
34
/**
35
 * GEmblem:
36
 *
37
 * `GEmblem` is an implementation of [iface@Gio.Icon] that supports
38
 * having an emblem, which is an icon with additional properties.
39
 * It can than be added to a [class@Gio.EmblemedIcon].
40
 *
41
 * Currently, only metainformation about the emblem's origin is
42
 * supported. More may be added in the future.
43
 */
44
45
static void g_emblem_iface_init (GIconIface *iface);
46
47
struct _GEmblem
48
{
49
  GObject parent_instance;
50
51
  GIcon *icon;
52
  GEmblemOrigin origin;
53
};
54
55
enum
56
{
57
  PROP_0_GEMBLEM,
58
  PROP_ICON,
59
  PROP_ORIGIN
60
};
61
62
0
G_DEFINE_TYPE_WITH_CODE (GEmblem, g_emblem, G_TYPE_OBJECT,
63
0
                         G_IMPLEMENT_INTERFACE (G_TYPE_ICON, g_emblem_iface_init))
64
0
65
0
static void
66
0
g_emblem_get_property (GObject    *object,
67
0
                       guint       prop_id,
68
0
                       GValue     *value,
69
0
                       GParamSpec *pspec)
70
0
{
71
0
  GEmblem *emblem = G_EMBLEM (object);
72
73
0
  switch (prop_id)
74
0
    {
75
0
      case PROP_ICON:
76
0
        g_value_set_object (value, emblem->icon);
77
0
  break;
78
79
0
      case PROP_ORIGIN:
80
0
        g_value_set_enum (value, (int) emblem->origin);
81
0
        break;
82
83
0
      default:
84
0
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
85
0
        break;
86
0
  }
87
0
}
88
89
static void
90
g_emblem_set_property (GObject      *object,
91
                       guint         prop_id,
92
                       const GValue *value,
93
                       GParamSpec   *pspec)
94
0
{
95
0
  GEmblem *emblem = G_EMBLEM (object);
96
97
0
  switch (prop_id)
98
0
    {
99
0
      case PROP_ICON:
100
0
        emblem->icon = g_value_dup_object (value);
101
0
        break;
102
103
0
      case PROP_ORIGIN:
104
0
        emblem->origin = (GEmblemOrigin) g_value_get_enum (value);
105
0
        break;
106
107
0
      default:
108
0
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
109
0
        break;
110
0
    }
111
0
}
112
113
static void
114
g_emblem_finalize (GObject *object)
115
0
{
116
0
  GEmblem *emblem = G_EMBLEM (object);
117
118
0
  if (emblem->icon)
119
0
    g_object_unref (emblem->icon);
120
121
0
  (*G_OBJECT_CLASS (g_emblem_parent_class)->finalize) (object);
122
0
}
123
124
static void
125
g_emblem_class_init (GEmblemClass *klass)
126
0
{
127
0
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
128
129
0
  gobject_class->finalize = g_emblem_finalize;
130
0
  gobject_class->set_property = g_emblem_set_property;
131
0
  gobject_class->get_property = g_emblem_get_property;
132
133
  /**
134
   * GEmblem:origin:
135
   *
136
   * The origin the emblem is derived from.
137
   *
138
   * Since: 2.18
139
   */
140
0
  g_object_class_install_property (gobject_class,
141
0
                                   PROP_ORIGIN,
142
0
                                   g_param_spec_enum ("origin", NULL, NULL,
143
0
                                                      G_TYPE_EMBLEM_ORIGIN,
144
0
                                                      G_EMBLEM_ORIGIN_UNKNOWN,
145
0
                                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
146
147
  /**
148
   * GEmblem:icon:
149
   *
150
   * The actual icon of the emblem.
151
   *
152
   * Since: 2.18
153
   */
154
0
  g_object_class_install_property (gobject_class,
155
0
                                   PROP_ICON,
156
0
                                   g_param_spec_object ("icon", NULL, NULL,
157
0
                                                      G_TYPE_OBJECT,
158
0
                                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
159
160
0
}
161
162
static void
163
g_emblem_init (GEmblem *emblem)
164
0
{
165
0
}
166
167
/**
168
 * g_emblem_new:
169
 * @icon: a GIcon containing the icon.
170
 *
171
 * Creates a new emblem for @icon.
172
 *
173
 * Returns: a new #GEmblem.
174
 *
175
 * Since: 2.18
176
 */
177
GEmblem *
178
g_emblem_new (GIcon *icon)
179
0
{
180
0
  GEmblem* emblem;
181
182
0
  g_return_val_if_fail (icon != NULL, NULL);
183
0
  g_return_val_if_fail (G_IS_ICON (icon), NULL);
184
0
  g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
185
186
0
  emblem = g_object_new (G_TYPE_EMBLEM, NULL);
187
0
  emblem->icon = g_object_ref (icon);
188
0
  emblem->origin = G_EMBLEM_ORIGIN_UNKNOWN;
189
190
0
  return emblem;
191
0
}
192
193
/**
194
 * g_emblem_new_with_origin:
195
 * @icon: a GIcon containing the icon.
196
 * @origin: a GEmblemOrigin enum defining the emblem's origin
197
 *
198
 * Creates a new emblem for @icon.
199
 *
200
 * Returns: a new #GEmblem.
201
 *
202
 * Since: 2.18
203
 */
204
GEmblem *
205
g_emblem_new_with_origin (GIcon         *icon,
206
                          GEmblemOrigin  origin)
207
0
{
208
0
  GEmblem* emblem;
209
210
0
  g_return_val_if_fail (icon != NULL, NULL);
211
0
  g_return_val_if_fail (G_IS_ICON (icon), NULL);
212
0
  g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
213
214
0
  emblem = g_object_new (G_TYPE_EMBLEM, NULL);
215
0
  emblem->icon = g_object_ref (icon);
216
0
  emblem->origin = origin;
217
218
0
  return emblem;
219
0
}
220
221
/**
222
 * g_emblem_get_icon:
223
 * @emblem: a #GEmblem from which the icon should be extracted.
224
 *
225
 * Gives back the icon from @emblem.
226
 *
227
 * Returns: (transfer none): a #GIcon. The returned object belongs to
228
 *          the emblem and should not be modified or freed.
229
 *
230
 * Since: 2.18
231
 */
232
GIcon *
233
g_emblem_get_icon (GEmblem *emblem)
234
0
{
235
0
  g_return_val_if_fail (G_IS_EMBLEM (emblem), NULL);
236
237
0
  return emblem->icon;
238
0
}
239
240
241
/**
242
 * g_emblem_get_origin:
243
 * @emblem: a #GEmblem
244
 *
245
 * Gets the origin of the emblem.
246
 *
247
 * Returns: (transfer none): the origin of the emblem
248
 *
249
 * Since: 2.18
250
 */
251
GEmblemOrigin
252
g_emblem_get_origin (GEmblem *emblem)
253
0
{
254
0
  g_return_val_if_fail (G_IS_EMBLEM (emblem), G_EMBLEM_ORIGIN_UNKNOWN);
255
256
0
  return emblem->origin;
257
0
}
258
259
static guint
260
g_emblem_hash (GIcon *icon)
261
0
{
262
0
  GEmblem *emblem = G_EMBLEM (icon);
263
0
  guint hash;
264
265
0
  hash  = g_icon_hash (g_emblem_get_icon (emblem));
266
0
  hash ^= emblem->origin;
267
268
0
  return hash;
269
0
}
270
271
static gboolean
272
g_emblem_equal (GIcon *icon1,
273
                GIcon *icon2)
274
0
{
275
0
  GEmblem *emblem1 = G_EMBLEM (icon1);
276
0
  GEmblem *emblem2 = G_EMBLEM (icon2);
277
278
0
  return emblem1->origin == emblem2->origin &&
279
0
         g_icon_equal (emblem1->icon, emblem2->icon);
280
0
}
281
282
static gboolean
283
g_emblem_to_tokens (GIcon *icon,
284
        GPtrArray *tokens,
285
        gint  *out_version)
286
0
{
287
0
  GEmblem *emblem = G_EMBLEM (icon);
288
0
  char *s;
289
290
  /* GEmblem are encoded as
291
   *
292
   * <origin> <icon>
293
   */
294
295
0
  g_return_val_if_fail (out_version != NULL, FALSE);
296
297
0
  *out_version = 0;
298
299
0
  s = g_icon_to_string (emblem->icon);
300
0
  if (s == NULL)
301
0
    return FALSE;
302
303
0
  g_ptr_array_add (tokens, s);
304
305
0
  s = g_strdup_printf ("%d", emblem->origin);
306
0
  g_ptr_array_add (tokens, s);
307
308
0
  return TRUE;
309
0
}
310
311
static GIcon *
312
g_emblem_from_tokens (gchar  **tokens,
313
          gint     num_tokens,
314
          gint     version,
315
          GError **error)
316
0
{
317
0
  GEmblem *emblem;
318
0
  GIcon *icon;
319
0
  GEmblemOrigin origin;
320
0
  uint64_t origin_parsed;
321
322
  /* This is guaranteed by the GIcon interface */
323
0
  g_assert (num_tokens >= 0);
324
325
0
  emblem = NULL;
326
327
0
  if (version != 0)
328
0
    {
329
0
      g_set_error (error,
330
0
                   G_IO_ERROR,
331
0
                   G_IO_ERROR_INVALID_ARGUMENT,
332
0
                   _("Can’t handle version %d of GEmblem encoding"),
333
0
                   version);
334
0
      return NULL;
335
0
    }
336
337
0
  if (num_tokens != 2)
338
0
    {
339
0
      g_set_error (error,
340
0
                   G_IO_ERROR,
341
0
                   G_IO_ERROR_INVALID_ARGUMENT,
342
0
                   _("Malformed number of tokens (%d) in GEmblem encoding"),
343
0
                   num_tokens);
344
0
      return NULL;
345
0
    }
346
347
0
  icon = g_icon_new_for_string (tokens[0], error);
348
349
0
  if (icon == NULL)
350
0
    return NULL;
351
352
0
  if (!g_ascii_string_to_unsigned (tokens[1], 10, G_EMBLEM_ORIGIN_UNKNOWN, G_EMBLEM_ORIGIN_TAG,
353
0
                                   &origin_parsed, NULL) ||
354
0
      (origin_parsed != G_EMBLEM_ORIGIN_UNKNOWN &&
355
0
       origin_parsed != G_EMBLEM_ORIGIN_DEVICE &&
356
0
       origin_parsed != G_EMBLEM_ORIGIN_LIVEMETADATA &&
357
0
       origin_parsed != G_EMBLEM_ORIGIN_TAG))
358
0
    {
359
0
      g_object_unref (icon);
360
0
      g_set_error (error,
361
0
                   G_IO_ERROR,
362
0
                   G_IO_ERROR_INVALID_ARGUMENT,
363
0
                   _("Malformed origin (%s) in GEmblem encoding"),
364
0
                   tokens[1]);
365
0
      return NULL;
366
0
    }
367
368
0
  origin = (GEmblemOrigin) origin_parsed;
369
370
0
  emblem = g_emblem_new_with_origin (icon, origin);
371
0
  g_object_unref (icon);
372
373
0
  return G_ICON (emblem);
374
0
}
375
376
static GVariant *
377
g_emblem_serialize (GIcon *icon)
378
0
{
379
0
  GEmblem *emblem = G_EMBLEM (icon);
380
0
  GVariant *icon_data;
381
0
  GEnumValue *origin;
382
0
  GVariant *result;
383
384
0
  icon_data = g_icon_serialize (emblem->icon);
385
0
  if (!icon_data)
386
0
    return NULL;
387
388
0
  origin = g_enum_get_value (g_type_class_peek (G_TYPE_EMBLEM_ORIGIN), (int) emblem->origin);
389
0
  result = g_variant_new_parsed ("('emblem', <(%v, {'origin': <%s>})>)",
390
0
                                 icon_data, origin ? origin->value_nick : "unknown");
391
0
  g_variant_unref (icon_data);
392
393
0
  return result;
394
0
}
395
396
static void
397
g_emblem_iface_init (GIconIface *iface)
398
0
{
399
0
  iface->hash  = g_emblem_hash;
400
0
  iface->equal = g_emblem_equal;
401
0
  iface->to_tokens = g_emblem_to_tokens;
402
0
  iface->from_tokens = g_emblem_from_tokens;
403
0
  iface->serialize = g_emblem_serialize;
404
0
}