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