• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
G_DEFINE_TYPE_WITH_CODE(GEmblem,g_emblem,G_TYPE_OBJECT,G_IMPLEMENT_INTERFACE (G_TYPE_ICON,g_emblem_iface_init))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 {
76   GEmblem *emblem = G_EMBLEM (object);
77 
78   switch (prop_id)
79     {
80       case PROP_ICON:
81         g_value_set_object (value, emblem->icon);
82 	break;
83 
84       case PROP_ORIGIN:
85         g_value_set_enum (value, emblem->origin);
86         break;
87 
88       default:
89         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
90         break;
91   }
92 }
93 
94 static void
g_emblem_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)95 g_emblem_set_property (GObject      *object,
96                        guint         prop_id,
97                        const GValue *value,
98                        GParamSpec   *pspec)
99 {
100   GEmblem *emblem = G_EMBLEM (object);
101 
102   switch (prop_id)
103     {
104       case PROP_ICON:
105         emblem->icon = g_value_dup_object (value);
106         break;
107 
108       case PROP_ORIGIN:
109         emblem->origin = g_value_get_enum (value);
110         break;
111 
112       default:
113         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
114         break;
115     }
116 }
117 
118 static void
g_emblem_finalize(GObject * object)119 g_emblem_finalize (GObject *object)
120 {
121   GEmblem *emblem = G_EMBLEM (object);
122 
123   if (emblem->icon)
124     g_object_unref (emblem->icon);
125 
126   (*G_OBJECT_CLASS (g_emblem_parent_class)->finalize) (object);
127 }
128 
129 static void
g_emblem_class_init(GEmblemClass * klass)130 g_emblem_class_init (GEmblemClass *klass)
131 {
132   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
133 
134   gobject_class->finalize = g_emblem_finalize;
135   gobject_class->set_property = g_emblem_set_property;
136   gobject_class->get_property = g_emblem_get_property;
137 
138   g_object_class_install_property (gobject_class,
139                                    PROP_ORIGIN,
140                                    g_param_spec_enum ("origin",
141                                                       P_("GEmblem’s origin"),
142                                                       P_("Tells which origin the emblem is derived from"),
143                                                       G_TYPE_EMBLEM_ORIGIN,
144                                                       G_EMBLEM_ORIGIN_UNKNOWN,
145                                                       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
146 
147   g_object_class_install_property (gobject_class,
148                                    PROP_ICON,
149                                    g_param_spec_object ("icon",
150                                                       P_("The icon of the emblem"),
151                                                       P_("The actual icon of the emblem"),
152                                                       G_TYPE_OBJECT,
153                                                       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
154 
155 }
156 
157 static void
g_emblem_init(GEmblem * emblem)158 g_emblem_init (GEmblem *emblem)
159 {
160 }
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 *
g_emblem_new(GIcon * icon)173 g_emblem_new (GIcon *icon)
174 {
175   GEmblem* emblem;
176 
177   g_return_val_if_fail (icon != NULL, NULL);
178   g_return_val_if_fail (G_IS_ICON (icon), NULL);
179   g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
180 
181   emblem = g_object_new (G_TYPE_EMBLEM, NULL);
182   emblem->icon = g_object_ref (icon);
183   emblem->origin = G_EMBLEM_ORIGIN_UNKNOWN;
184 
185   return emblem;
186 }
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 *
g_emblem_new_with_origin(GIcon * icon,GEmblemOrigin origin)200 g_emblem_new_with_origin (GIcon         *icon,
201                           GEmblemOrigin  origin)
202 {
203   GEmblem* emblem;
204 
205   g_return_val_if_fail (icon != NULL, NULL);
206   g_return_val_if_fail (G_IS_ICON (icon), NULL);
207   g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
208 
209   emblem = g_object_new (G_TYPE_EMBLEM, NULL);
210   emblem->icon = g_object_ref (icon);
211   emblem->origin = origin;
212 
213   return emblem;
214 }
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 *
g_emblem_get_icon(GEmblem * emblem)228 g_emblem_get_icon (GEmblem *emblem)
229 {
230   g_return_val_if_fail (G_IS_EMBLEM (emblem), NULL);
231 
232   return emblem->icon;
233 }
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
g_emblem_get_origin(GEmblem * emblem)247 g_emblem_get_origin (GEmblem *emblem)
248 {
249   g_return_val_if_fail (G_IS_EMBLEM (emblem), G_EMBLEM_ORIGIN_UNKNOWN);
250 
251   return emblem->origin;
252 }
253 
254 static guint
g_emblem_hash(GIcon * icon)255 g_emblem_hash (GIcon *icon)
256 {
257   GEmblem *emblem = G_EMBLEM (icon);
258   guint hash;
259 
260   hash  = g_icon_hash (g_emblem_get_icon (emblem));
261   hash ^= emblem->origin;
262 
263   return hash;
264 }
265 
266 static gboolean
g_emblem_equal(GIcon * icon1,GIcon * icon2)267 g_emblem_equal (GIcon *icon1,
268                 GIcon *icon2)
269 {
270   GEmblem *emblem1 = G_EMBLEM (icon1);
271   GEmblem *emblem2 = G_EMBLEM (icon2);
272 
273   return emblem1->origin == emblem2->origin &&
274          g_icon_equal (emblem1->icon, emblem2->icon);
275 }
276 
277 static gboolean
g_emblem_to_tokens(GIcon * icon,GPtrArray * tokens,gint * out_version)278 g_emblem_to_tokens (GIcon *icon,
279 		    GPtrArray *tokens,
280 		    gint  *out_version)
281 {
282   GEmblem *emblem = G_EMBLEM (icon);
283   char *s;
284 
285   /* GEmblem are encoded as
286    *
287    * <origin> <icon>
288    */
289 
290   g_return_val_if_fail (out_version != NULL, FALSE);
291 
292   *out_version = 0;
293 
294   s = g_icon_to_string (emblem->icon);
295   if (s == NULL)
296     return FALSE;
297 
298   g_ptr_array_add (tokens, s);
299 
300   s = g_strdup_printf ("%d", emblem->origin);
301   g_ptr_array_add (tokens, s);
302 
303   return TRUE;
304 }
305 
306 static GIcon *
g_emblem_from_tokens(gchar ** tokens,gint num_tokens,gint version,GError ** error)307 g_emblem_from_tokens (gchar  **tokens,
308 		      gint     num_tokens,
309 		      gint     version,
310 		      GError **error)
311 {
312   GEmblem *emblem;
313   GIcon *icon;
314   GEmblemOrigin origin;
315 
316   emblem = NULL;
317 
318   if (version != 0)
319     {
320       g_set_error (error,
321                    G_IO_ERROR,
322                    G_IO_ERROR_INVALID_ARGUMENT,
323                    _("Can’t handle version %d of GEmblem encoding"),
324                    version);
325       return NULL;
326     }
327 
328   if (num_tokens != 2)
329     {
330       g_set_error (error,
331                    G_IO_ERROR,
332                    G_IO_ERROR_INVALID_ARGUMENT,
333                    _("Malformed number of tokens (%d) in GEmblem encoding"),
334                    num_tokens);
335       return NULL;
336     }
337 
338   icon = g_icon_new_for_string (tokens[0], error);
339 
340   if (icon == NULL)
341     return NULL;
342 
343   origin = atoi (tokens[1]);
344 
345   emblem = g_emblem_new_with_origin (icon, origin);
346   g_object_unref (icon);
347 
348   return G_ICON (emblem);
349 }
350 
351 static GVariant *
g_emblem_serialize(GIcon * icon)352 g_emblem_serialize (GIcon *icon)
353 {
354   GEmblem *emblem = G_EMBLEM (icon);
355   GVariant *icon_data;
356   GEnumValue *origin;
357   GVariant *result;
358 
359   icon_data = g_icon_serialize (emblem->icon);
360   if (!icon_data)
361     return NULL;
362 
363   origin = g_enum_get_value (g_type_class_peek (G_TYPE_EMBLEM_ORIGIN), emblem->origin);
364   result = g_variant_new_parsed ("('emblem', <(%v, {'origin': <%s>})>)",
365                                  icon_data, origin ? origin->value_nick : "unknown");
366   g_variant_unref (icon_data);
367 
368   return result;
369 }
370 
371 static void
g_emblem_iface_init(GIconIface * iface)372 g_emblem_iface_init (GIconIface *iface)
373 {
374   iface->hash  = g_emblem_hash;
375   iface->equal = g_emblem_equal;
376   iface->to_tokens = g_emblem_to_tokens;
377   iface->from_tokens = g_emblem_from_tokens;
378   iface->serialize = g_emblem_serialize;
379 }
380