emblemedicon: add docs for _clear_emblems()
[platform/upstream/glib.git] / gio / gemblemedicon.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2
3 /* GIO - GLib Input, Output and Streaming Library
4  * 
5  * Copyright (C) 2006-2007 Red Hat, Inc.
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 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, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * Author: Matthias Clasen <mclasen@redhat.com>
23  *         Clemens N. Buss <cebuzz@gmail.com>
24  */
25
26 #include <config.h>
27
28 #include <string.h>
29
30 #include "gemblemedicon.h"
31 #include "glibintl.h"
32 #include "gioerror.h"
33
34
35 /**
36  * SECTION:gemblemedicon
37  * @short_description: Icon with emblems
38  * @include: gio/gio.h
39  * @see_also: #GIcon, #GLoadableIcon, #GThemedIcon, #GEmblem
40  *
41  * #GEmblemedIcon is an implementation of #GIcon that supports
42  * adding an emblem to an icon. Adding multiple emblems to an
43  * icon is ensured via g_emblemed_icon_add_emblem(). 
44  *
45  * Note that #GEmblemedIcon allows no control over the position
46  * of the emblems. See also #GEmblem for more information.
47  **/
48
49 static void g_emblemed_icon_icon_iface_init (GIconIface *iface);
50
51 struct _GEmblemedIcon
52 {
53   GObject parent_instance;
54
55   GIcon *icon;
56   GList *emblems;
57 };
58
59 struct _GEmblemedIconClass
60 {
61   GObjectClass parent_class;
62 };
63
64 G_DEFINE_TYPE_WITH_CODE (GEmblemedIcon, g_emblemed_icon, G_TYPE_OBJECT,
65                          G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
66                          g_emblemed_icon_icon_iface_init))
67
68
69 static void
70 g_emblemed_icon_finalize (GObject *object)
71 {
72   GEmblemedIcon *emblemed;
73
74   emblemed = G_EMBLEMED_ICON (object);
75
76   g_object_unref (emblemed->icon);
77   g_list_foreach (emblemed->emblems, (GFunc) g_object_unref, NULL);
78   g_list_free (emblemed->emblems);
79
80   (*G_OBJECT_CLASS (g_emblemed_icon_parent_class)->finalize) (object);
81 }
82
83 static void
84 g_emblemed_icon_class_init (GEmblemedIconClass *klass)
85 {
86   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
87   gobject_class->finalize = g_emblemed_icon_finalize;
88 }
89
90 static void
91 g_emblemed_icon_init (GEmblemedIcon *emblemed)
92 {
93 }
94
95 /**
96  * g_emblemed_icon_new:
97  * @icon: a #GIcon
98  * @emblem: (allow-none): a #GEmblem, or %NULL
99  *
100  * Creates a new emblemed icon for @icon with the emblem @emblem.
101  *
102  * Returns: (transfer full): a new #GIcon
103  *
104  * Since: 2.18
105  **/
106 GIcon *
107 g_emblemed_icon_new (GIcon   *icon,
108                      GEmblem *emblem)
109 {
110   GEmblemedIcon *emblemed;
111   
112   g_return_val_if_fail (G_IS_ICON (icon), NULL);
113   g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
114
115   emblemed = G_EMBLEMED_ICON (g_object_new (G_TYPE_EMBLEMED_ICON, NULL));
116   emblemed->icon = g_object_ref (icon);
117
118   if (emblem != NULL)
119     g_emblemed_icon_add_emblem (emblemed, emblem);
120
121   return G_ICON (emblemed);
122 }
123
124
125 /**
126  * g_emblemed_icon_get_icon:
127  * @emblemed: a #GEmblemedIcon
128  *
129  * Gets the main icon for @emblemed.
130  *
131  * Returns: (transfer full): a #GIcon that is owned by @emblemed
132  *
133  * Since: 2.18
134  **/
135 GIcon *
136 g_emblemed_icon_get_icon (GEmblemedIcon *emblemed)
137 {
138   g_return_val_if_fail (G_IS_EMBLEMED_ICON (emblemed), NULL);
139
140   return emblemed->icon;
141 }
142
143 /**
144  * g_emblemed_icon_get_emblems:
145  * @emblemed: a #GEmblemedIcon
146  *
147  * Gets the list of emblems for the @icon.
148  *
149  * Returns: (element-type utf8) (transfer none): a #GList of #GEmblem <!-- -->s that
150  * is owned by @emblemed
151  *
152  * Since: 2.18
153  **/
154
155 GList *
156 g_emblemed_icon_get_emblems (GEmblemedIcon *emblemed)
157 {
158   g_return_val_if_fail (G_IS_EMBLEMED_ICON (emblemed), NULL);
159
160   return emblemed->emblems;
161 }
162
163 /**
164  * g_emblemed_icon_clear_emblems:
165  * @emblemed: a #GEmblemedIcon
166  *
167  * Remove alls the emblems from @icon.
168  *
169  * Since: 2.28
170  **/
171 void
172 g_emblemed_icon_clear_emblems (GEmblemedIcon *emblemed)
173 {
174   g_return_if_fail (G_IS_EMBLEMED_ICON (emblemed));
175
176   if (emblemed->emblems == NULL)
177     return;
178
179   g_list_free_full (emblemed->emblems, g_object_unref);
180   emblemed->emblems = NULL;
181 }
182
183 static gint
184 g_emblem_comp (GEmblem *a,
185                GEmblem *b)
186 {
187   guint hash_a = g_icon_hash (G_ICON (a));
188   guint hash_b = g_icon_hash (G_ICON (b));
189
190   if(hash_a < hash_b)
191     return -1;
192
193   if(hash_a == hash_b)
194     return 0;
195
196   return 1;
197 }
198
199 /**
200  * g_emblemed_icon_add_emblem:
201  * @emblemed: a #GEmblemedIcon
202  * @emblem: a #GEmblem
203  *
204  * Adds @emblem to the #GList of #GEmblem <!-- -->s.
205  *
206  * Since: 2.18
207  **/
208 void 
209 g_emblemed_icon_add_emblem (GEmblemedIcon *emblemed,
210                             GEmblem       *emblem)
211 {
212   g_return_if_fail (G_IS_EMBLEMED_ICON (emblemed));
213   g_return_if_fail (G_IS_EMBLEM (emblem));
214
215   g_object_ref (emblem);
216   emblemed->emblems = g_list_insert_sorted (emblemed->emblems, emblem,
217                                             (GCompareFunc) g_emblem_comp);
218 }
219
220 static guint
221 g_emblemed_icon_hash (GIcon *icon)
222 {
223   GEmblemedIcon *emblemed = G_EMBLEMED_ICON (icon);
224   GList *list;
225   guint hash = g_icon_hash (emblemed->icon);
226
227   for (list = emblemed->emblems; list != NULL; list = list->next)
228     hash ^= g_icon_hash (G_ICON (list->data));
229
230   return hash;
231 }
232
233 static gboolean
234 g_emblemed_icon_equal (GIcon *icon1,
235                        GIcon *icon2)
236 {
237   GEmblemedIcon *emblemed1 = G_EMBLEMED_ICON (icon1);
238   GEmblemedIcon *emblemed2 = G_EMBLEMED_ICON (icon2);
239   GList *list1, *list2;
240
241   if (!g_icon_equal (emblemed1->icon, emblemed2->icon))
242     return FALSE;
243
244   list1 = emblemed1->emblems;
245   list2 = emblemed2->emblems;
246
247   while (list1 && list2)
248   {
249     if (!g_icon_equal (G_ICON (list1->data), G_ICON (list2->data)))
250         return FALSE;
251     
252     list1 = list1->next;
253     list2 = list2->next;
254   }
255   
256   return list1 == NULL && list2 == NULL;
257 }
258
259 static gboolean
260 g_emblemed_icon_to_tokens (GIcon *icon,
261                            GPtrArray *tokens,
262                            gint  *out_version)
263 {
264   GEmblemedIcon *emblemed_icon = G_EMBLEMED_ICON (icon);
265   GList *l;
266   char *s;
267
268   /* GEmblemedIcons are encoded as
269    *
270    *   <encoded_icon> [<encoded_emblem_icon>]*
271    */
272
273   g_return_val_if_fail (out_version != NULL, FALSE);
274
275   *out_version = 0;
276
277   s = g_icon_to_string (emblemed_icon->icon);
278   if (s == NULL)
279     return FALSE;
280
281   g_ptr_array_add (tokens, s);
282
283   for (l = emblemed_icon->emblems; l != NULL; l = l->next)
284     {
285       GIcon *emblem_icon = G_ICON (l->data);
286
287       s = g_icon_to_string (emblem_icon);
288       if (s == NULL)
289         return FALSE;
290       
291       g_ptr_array_add (tokens, s);
292     }
293
294   return TRUE;
295 }
296
297 static GIcon *
298 g_emblemed_icon_from_tokens (gchar  **tokens,
299                              gint     num_tokens,
300                              gint     version,
301                              GError **error)
302 {
303   GEmblemedIcon *emblemed_icon;
304   int n;
305
306   emblemed_icon = NULL;
307
308   if (version != 0)
309     {
310       g_set_error (error,
311                    G_IO_ERROR,
312                    G_IO_ERROR_INVALID_ARGUMENT,
313                    _("Can't handle version %d of GEmblemedIcon encoding"),
314                    version);
315       goto fail;
316     }
317
318   if (num_tokens < 1)
319     {
320       g_set_error (error,
321                    G_IO_ERROR,
322                    G_IO_ERROR_INVALID_ARGUMENT,
323                    _("Malformed number of tokens (%d) in GEmblemedIcon encoding"),
324                    num_tokens);
325       goto fail;
326     }
327
328   emblemed_icon = g_object_new (G_TYPE_EMBLEMED_ICON, NULL);
329   emblemed_icon->icon = g_icon_new_for_string (tokens[0], error);
330   if (emblemed_icon->icon == NULL)
331     goto fail;
332
333   for (n = 1; n < num_tokens; n++)
334     {
335       GIcon *emblem;
336
337       emblem = g_icon_new_for_string (tokens[n], error);
338       if (emblem == NULL)
339         goto fail;
340
341       if (!G_IS_EMBLEM (emblem))
342         {
343           g_set_error_literal (error,
344                                G_IO_ERROR,
345                                G_IO_ERROR_INVALID_ARGUMENT,
346                                _("Expected a GEmblem for GEmblemedIcon"));
347           g_object_unref (emblem);
348           goto fail;
349         }
350
351       emblemed_icon->emblems = g_list_append (emblemed_icon->emblems, emblem);
352     }
353
354   return G_ICON (emblemed_icon);
355
356  fail:
357   if (emblemed_icon != NULL)
358     g_object_unref (emblemed_icon);
359   return NULL;
360 }
361
362 static void
363 g_emblemed_icon_icon_iface_init (GIconIface *iface)
364 {
365   iface->hash = g_emblemed_icon_hash;
366   iface->equal = g_emblemed_icon_equal;
367   iface->to_tokens = g_emblemed_icon_to_tokens;
368   iface->from_tokens = g_emblemed_icon_from_tokens;
369 }