46a013779b96d5ac5b60f0a3ea4e9a4f71bc0633
[platform/upstream/glib.git] / gio / gemblem.c
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 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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
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 {
78   GEmblem *emblem = G_EMBLEM (object);
79
80   switch (prop_id)
81     {
82       case PROP_ICON:
83         g_value_set_object (value, emblem->icon);
84         break;
85
86       case PROP_ORIGIN:
87         g_value_set_enum (value, emblem->origin);
88         break;
89
90       default:
91         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
92         break;
93   }
94 }
95
96 static void
97 g_emblem_set_property (GObject      *object,
98                        guint         prop_id,
99                        const GValue *value,
100                        GParamSpec   *pspec)
101 {
102   GEmblem *emblem = G_EMBLEM (object);
103
104   switch (prop_id)
105     {
106       case PROP_ICON:
107         emblem->icon = g_value_dup_object (value);
108         break;
109
110       case PROP_ORIGIN:
111         emblem->origin = g_value_get_enum (value);
112         break;
113
114       default:
115         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
116         break;
117     }
118 }
119
120 static void
121 g_emblem_finalize (GObject *object)
122 {
123   GEmblem *emblem = G_EMBLEM (object);
124
125   g_object_unref (emblem->icon);
126
127   (*G_OBJECT_CLASS (g_emblem_parent_class)->finalize) (object);
128 }
129
130 static void
131 g_emblem_class_init (GEmblemClass *klass)
132 {
133   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
134
135   gobject_class->finalize = g_emblem_finalize;
136   gobject_class->set_property = g_emblem_set_property;
137   gobject_class->get_property = g_emblem_get_property;
138
139   g_object_class_install_property (gobject_class,
140                                    PROP_ORIGIN,
141                                    g_param_spec_enum ("origin",
142                                                       P_("GEmblem's origin"),
143                                                       P_("Tells which origin the emblem is derived from"),
144                                                       G_TYPE_EMBLEM_ORIGIN,
145                                                       G_EMBLEM_ORIGIN_UNKNOWN,
146                                                       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
147
148   g_object_class_install_property (gobject_class,
149                                    PROP_ICON,
150                                    g_param_spec_object ("icon",
151                                                       P_("The icon of the emblem"),
152                                                       P_("The actual icon of the emblem"),
153                                                       G_TYPE_OBJECT,
154                                                       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
155
156 }
157
158 static void
159 g_emblem_init (GEmblem *emblem)
160 {
161 }
162
163 /**
164  * g_emblem_new:
165  * @icon: a GIcon containing the icon.
166  *
167  * Creates a new emblem for @icon.
168  *
169  * Returns: a new #GEmblem.
170  *
171  * Since: 2.18
172  */
173 GEmblem *
174 g_emblem_new (GIcon *icon)
175 {
176   GEmblem* emblem;
177
178   g_return_val_if_fail (icon != NULL, NULL);
179   g_return_val_if_fail (G_IS_ICON (icon), NULL);
180   g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
181
182   emblem = g_object_new (G_TYPE_EMBLEM, NULL);
183   emblem->icon = g_object_ref (icon);
184   emblem->origin = G_EMBLEM_ORIGIN_UNKNOWN;
185
186   return emblem;
187 }
188
189 /**
190  * g_emblem_new_with_origin:
191  * @icon: a GIcon containing the icon.
192  * @origin: a GEmblemOrigin enum defining the emblem's origin
193  *
194  * Creates a new emblem for @icon.
195  *
196  * Returns: a new #GEmblem.
197  *
198  * Since: 2.18
199  */
200 GEmblem *
201 g_emblem_new_with_origin (GIcon         *icon,
202                           GEmblemOrigin  origin)
203 {
204   GEmblem* emblem;
205
206   g_return_val_if_fail (icon != NULL, NULL);
207   g_return_val_if_fail (G_IS_ICON (icon), NULL);
208   g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
209
210   emblem = g_object_new (G_TYPE_EMBLEM, NULL);
211   emblem->icon = g_object_ref (icon);
212   emblem->origin = origin;
213
214   return emblem;
215 }
216
217 /**
218  * g_emblem_get_icon:
219  * @emblem: a #GEmblem from which the icon should be extracted.
220  *
221  * Gives back the icon from @emblem.
222  *
223  * Returns: a #GIcon. The returned object belongs to the emblem
224  *    and should not be modified or freed.
225  *
226  * Since: 2.18
227  */
228 GIcon *
229 g_emblem_get_icon (GEmblem *emblem)
230 {
231   g_return_val_if_fail (G_IS_EMBLEM (emblem), NULL);
232
233   return emblem->icon;
234 }
235
236
237 /**
238  * g_emblem_get_origin:
239  * @emblem: a #GEmblem
240  *
241  * Gets the origin of the emblem.
242  *
243  * Returns: the origin of the emblem
244  *
245  * Since: 2.18
246  */
247 GEmblemOrigin
248 g_emblem_get_origin (GEmblem *emblem)
249 {
250   g_return_val_if_fail (G_IS_EMBLEM (emblem), G_EMBLEM_ORIGIN_UNKNOWN);
251
252   return emblem->origin;
253 }
254
255 static guint
256 g_emblem_hash (GIcon *icon)
257 {
258   GEmblem *emblem = G_EMBLEM (icon);
259   guint hash;
260
261   hash  = g_icon_hash (g_emblem_get_icon (emblem));
262   hash ^= emblem->origin;
263
264   return hash;
265 }
266
267 static gboolean
268 g_emblem_equal (GIcon *icon1,
269                 GIcon *icon2)
270 {
271   GEmblem *emblem1 = G_EMBLEM (icon1);
272   GEmblem *emblem2 = G_EMBLEM (icon2);
273
274   return emblem1->origin == emblem2->origin &&
275          g_icon_equal (emblem1->icon, emblem2->icon);
276 }
277
278 static gboolean
279 g_emblem_to_tokens (GIcon *icon,
280                     GPtrArray *tokens,
281                     gint  *out_version)
282 {
283   GEmblem *emblem = G_EMBLEM (icon);
284   char *s;
285
286   /* GEmblem are encoded as
287    *
288    * <origin> <icon>
289    */
290
291   g_return_val_if_fail (out_version != NULL, FALSE);
292
293   *out_version = 0;
294
295   s = g_icon_to_string (emblem->icon);
296   if (s == NULL)
297     return FALSE;
298
299   g_ptr_array_add (tokens, s);
300
301   s = g_strdup_printf ("%d", emblem->origin);
302   g_ptr_array_add (tokens, s);
303
304   return TRUE;
305 }
306
307 static GIcon *
308 g_emblem_from_tokens (gchar  **tokens,
309                       gint     num_tokens,
310                       gint     version,
311                       GError **error)
312 {
313   GEmblem *emblem;
314   GIcon *icon;
315   GEmblemOrigin origin;
316
317   emblem = NULL;
318
319   if (version != 0)
320     {
321       g_set_error (error,
322                    G_IO_ERROR,
323                    G_IO_ERROR_INVALID_ARGUMENT,
324                    _("Can't handle version %d of GEmblem encoding"),
325                    version);
326       return NULL;
327     }
328
329   if (num_tokens != 2)
330     {
331       g_set_error (error,
332                    G_IO_ERROR,
333                    G_IO_ERROR_INVALID_ARGUMENT,
334                    _("Malformed number of tokens (%d) in GEmblem encoding"),
335                    num_tokens);
336       return NULL;
337     }
338
339   icon = g_icon_new_for_string (tokens[0], error);
340
341   if (icon == NULL)
342     return NULL;
343
344   origin = atoi (tokens[1]);
345
346   emblem = g_emblem_new_with_origin (icon, origin);
347   g_object_unref (icon);
348
349   return G_ICON (emblem);
350 }
351
352 static void
353 g_emblem_iface_init (GIconIface *iface)
354 {
355   iface->hash  = g_emblem_hash;
356   iface->equal = g_emblem_equal;
357   iface->to_tokens = g_emblem_to_tokens;
358   iface->from_tokens = g_emblem_from_tokens;
359 }