Coverage Report

Created: 2025-07-01 07:09

/src/glib/gio/gemblem.c
Line
Count
Source (jump to first uncovered line)
1
/* GIO - GLib Input, Output and Streaming Library
2
 *
3
 * Copyright (C) 2008 Clemens N. Buss <cebuzz@gmail.com>
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General
16
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
#include <config.h>
20
21
#include "gicon.h"
22
#include "gemblem.h"
23
#include "glibintl.h"
24
#include "gioenums.h"
25
#include "gioenumtypes.h"
26
#include "gioerror.h"
27
#include <stdlib.h>
28
#include <string.h>
29
30
31
/**
32
 * SECTION:gemblem
33
 * @short_description: An object for emblems
34
 * @include: gio/gio.h
35
 * @see_also: #GIcon, #GEmblemedIcon, #GLoadableIcon, #GThemedIcon
36
 *
37
 * #GEmblem is an implementation of #GIcon that supports
38
 * having an emblem, which is an icon with additional properties.
39
 * It can than be added to a #GEmblemedIcon.
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
struct _GEmblemClass
56
{
57
  GObjectClass parent_class;
58
};
59
60
enum
61
{
62
  PROP_0_GEMBLEM,
63
  PROP_ICON,
64
  PROP_ORIGIN
65
};
66
67
G_DEFINE_TYPE_WITH_CODE (GEmblem, g_emblem, G_TYPE_OBJECT,
68
                         G_IMPLEMENT_INTERFACE (G_TYPE_ICON, g_emblem_iface_init))
69
70
static void
71
g_emblem_get_property (GObject    *object,
72
                       guint       prop_id,
73
                       GValue     *value,
74
                       GParamSpec *pspec)
75
0
{
76
0
  GEmblem *emblem = G_EMBLEM (object);
77
78
0
  switch (prop_id)
79
0
    {
80
0
      case PROP_ICON:
81
0
        g_value_set_object (value, emblem->icon);
82
0
  break;
83
84
0
      case PROP_ORIGIN:
85
0
        g_value_set_enum (value, emblem->origin);
86
0
        break;
87
88
0
      default:
89
0
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
90
0
        break;
91
0
  }
92
0
}
93
94
static void
95
g_emblem_set_property (GObject      *object,
96
                       guint         prop_id,
97
                       const GValue *value,
98
                       GParamSpec   *pspec)
99
0
{
100
0
  GEmblem *emblem = G_EMBLEM (object);
101
102
0
  switch (prop_id)
103
0
    {
104
0
      case PROP_ICON:
105
0
        emblem->icon = g_value_dup_object (value);
106
0
        break;
107
108
0
      case PROP_ORIGIN:
109
0
        emblem->origin = g_value_get_enum (value);
110
0
        break;
111
112
0
      default:
113
0
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
114
0
        break;
115
0
    }
116
0
}
117
118
static void
119
g_emblem_finalize (GObject *object)
120
0
{
121
0
  GEmblem *emblem = G_EMBLEM (object);
122
123
0
  if (emblem->icon)
124
0
    g_object_unref (emblem->icon);
125
126
0
  (*G_OBJECT_CLASS (g_emblem_parent_class)->finalize) (object);
127
0
}
128
129
static void
130
g_emblem_class_init (GEmblemClass *klass)
131
0
{
132
0
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
133
134
0
  gobject_class->finalize = g_emblem_finalize;
135
0
  gobject_class->set_property = g_emblem_set_property;
136
0
  gobject_class->get_property = g_emblem_get_property;
137
138
0
  g_object_class_install_property (gobject_class,
139
0
                                   PROP_ORIGIN,
140
0
                                   g_param_spec_enum ("origin",
141
0
                                                      P_("GEmblem’s origin"),
142
0
                                                      P_("Tells which origin the emblem is derived from"),
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
0
  g_object_class_install_property (gobject_class,
148
0
                                   PROP_ICON,
149
0
                                   g_param_spec_object ("icon",
150
0
                                                      P_("The icon of the emblem"),
151
0
                                                      P_("The actual icon of the emblem"),
152
0
                                                      G_TYPE_OBJECT,
153
0
                                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
154
155
0
}
156
157
static void
158
g_emblem_init (GEmblem *emblem)
159
0
{
160
0
}
161
162
/**
163
 * g_emblem_new:
164
 * @icon: a GIcon containing the icon.
165
 *
166
 * Creates a new emblem for @icon.
167
 *
168
 * Returns: a new #GEmblem.
169
 *
170
 * Since: 2.18
171
 */
172
GEmblem *
173
g_emblem_new (GIcon *icon)
174
0
{
175
0
  GEmblem* emblem;
176
177
0
  g_return_val_if_fail (icon != NULL, NULL);
178
0
  g_return_val_if_fail (G_IS_ICON (icon), NULL);
179
0
  g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
180
181
0
  emblem = g_object_new (G_TYPE_EMBLEM, NULL);
182
0
  emblem->icon = g_object_ref (icon);
183
0
  emblem->origin = G_EMBLEM_ORIGIN_UNKNOWN;
184
185
0
  return emblem;
186
0
}
187
188
/**
189
 * g_emblem_new_with_origin:
190
 * @icon: a GIcon containing the icon.
191
 * @origin: a GEmblemOrigin enum defining the emblem's origin
192
 *
193
 * Creates a new emblem for @icon.
194
 *
195
 * Returns: a new #GEmblem.
196
 *
197
 * Since: 2.18
198
 */
199
GEmblem *
200
g_emblem_new_with_origin (GIcon         *icon,
201
                          GEmblemOrigin  origin)
202
0
{
203
0
  GEmblem* emblem;
204
205
0
  g_return_val_if_fail (icon != NULL, NULL);
206
0
  g_return_val_if_fail (G_IS_ICON (icon), NULL);
207
0
  g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
208
209
0
  emblem = g_object_new (G_TYPE_EMBLEM, NULL);
210
0
  emblem->icon = g_object_ref (icon);
211
0
  emblem->origin = origin;
212
213
0
  return emblem;
214
0
}
215
216
/**
217
 * g_emblem_get_icon:
218
 * @emblem: a #GEmblem from which the icon should be extracted.
219
 *
220
 * Gives back the icon from @emblem.
221
 *
222
 * Returns: (transfer none): a #GIcon. The returned object belongs to
223
 *          the emblem and should not be modified or freed.
224
 *
225
 * Since: 2.18
226
 */
227
GIcon *
228
g_emblem_get_icon (GEmblem *emblem)
229
0
{
230
0
  g_return_val_if_fail (G_IS_EMBLEM (emblem), NULL);
231
232
0
  return emblem->icon;
233
0
}
234
235
236
/**
237
 * g_emblem_get_origin:
238
 * @emblem: a #GEmblem
239
 *
240
 * Gets the origin of the emblem.
241
 *
242
 * Returns: (transfer none): the origin of the emblem
243
 *
244
 * Since: 2.18
245
 */
246
GEmblemOrigin
247
g_emblem_get_origin (GEmblem *emblem)
248
0
{
249
0
  g_return_val_if_fail (G_IS_EMBLEM (emblem), G_EMBLEM_ORIGIN_UNKNOWN);
250
251
0
  return emblem->origin;
252
0
}
253
254
static guint
255
g_emblem_hash (GIcon *icon)
256
0
{
257
0
  GEmblem *emblem = G_EMBLEM (icon);
258
0
  guint hash;
259
260
0
  hash  = g_icon_hash (g_emblem_get_icon (emblem));
261
0
  hash ^= emblem->origin;
262
263
0
  return hash;
264
0
}
265
266
static gboolean
267
g_emblem_equal (GIcon *icon1,
268
                GIcon *icon2)
269
0
{
270
0
  GEmblem *emblem1 = G_EMBLEM (icon1);
271
0
  GEmblem *emblem2 = G_EMBLEM (icon2);
272
273
0
  return emblem1->origin == emblem2->origin &&
274
0
         g_icon_equal (emblem1->icon, emblem2->icon);
275
0
}
276
277
static gboolean
278
g_emblem_to_tokens (GIcon *icon,
279
        GPtrArray *tokens,
280
        gint  *out_version)
281
0
{
282
0
  GEmblem *emblem = G_EMBLEM (icon);
283
0
  char *s;
284
285
  /* GEmblem are encoded as
286
   *
287
   * <origin> <icon>
288
   */
289
290
0
  g_return_val_if_fail (out_version != NULL, FALSE);
291
292
0
  *out_version = 0;
293
294
0
  s = g_icon_to_string (emblem->icon);
295
0
  if (s == NULL)
296
0
    return FALSE;
297
298
0
  g_ptr_array_add (tokens, s);
299
300
0
  s = g_strdup_printf ("%d", emblem->origin);
301
0
  g_ptr_array_add (tokens, s);
302
303
0
  return TRUE;
304
0
}
305
306
static GIcon *
307
g_emblem_from_tokens (gchar  **tokens,
308
          gint     num_tokens,
309
          gint     version,
310
          GError **error)
311
0
{
312
0
  GEmblem *emblem;
313
0
  GIcon *icon;
314
0
  GEmblemOrigin origin;
315
316
0
  emblem = NULL;
317
318
0
  if (version != 0)
319
0
    {
320
0
      g_set_error (error,
321
0
                   G_IO_ERROR,
322
0
                   G_IO_ERROR_INVALID_ARGUMENT,
323
0
                   _("Can’t handle version %d of GEmblem encoding"),
324
0
                   version);
325
0
      return NULL;
326
0
    }
327
328
0
  if (num_tokens != 2)
329
0
    {
330
0
      g_set_error (error,
331
0
                   G_IO_ERROR,
332
0
                   G_IO_ERROR_INVALID_ARGUMENT,
333
0
                   _("Malformed number of tokens (%d) in GEmblem encoding"),
334
0
                   num_tokens);
335
0
      return NULL;
336
0
    }
337
338
0
  icon = g_icon_new_for_string (tokens[0], error);
339
340
0
  if (icon == NULL)
341
0
    return NULL;
342
343
0
  origin = atoi (tokens[1]);
344
345
0
  emblem = g_emblem_new_with_origin (icon, origin);
346
0
  g_object_unref (icon);
347
348
0
  return G_ICON (emblem);
349
0
}
350
351
static GVariant *
352
g_emblem_serialize (GIcon *icon)
353
0
{
354
0
  GEmblem *emblem = G_EMBLEM (icon);
355
0
  GVariant *icon_data;
356
0
  GEnumValue *origin;
357
0
  GVariant *result;
358
359
0
  icon_data = g_icon_serialize (emblem->icon);
360
0
  if (!icon_data)
361
0
    return NULL;
362
363
0
  origin = g_enum_get_value (g_type_class_peek (G_TYPE_EMBLEM_ORIGIN), emblem->origin);
364
0
  result = g_variant_new_parsed ("('emblem', <(%v, {'origin': <%s>})>)",
365
0
                                 icon_data, origin ? origin->value_nick : "unknown");
366
0
  g_variant_unref (icon_data);
367
368
0
  return result;
369
0
}
370
371
static void
372
g_emblem_iface_init (GIconIface *iface)
373
0
{
374
0
  iface->hash  = g_emblem_hash;
375
0
  iface->equal = g_emblem_equal;
376
0
  iface->to_tokens = g_emblem_to_tokens;
377
0
  iface->from_tokens = g_emblem_from_tokens;
378
0
  iface->serialize = g_emblem_serialize;
379
0
}