Forgotten commit
[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
33 #include "gioalias.h"
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
79   (*G_OBJECT_CLASS (g_emblemed_icon_parent_class)->finalize) (object);
80 }
81
82 static void
83 g_emblemed_icon_class_init (GEmblemedIconClass *klass)
84 {
85   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
86   gobject_class->finalize = g_emblemed_icon_finalize;
87 }
88
89 static void
90 g_emblemed_icon_init (GEmblemedIcon *emblemed)
91 {
92 }
93
94 /**
95  * g_emblemed_icon_new:
96  * @icon: a #GIcon
97  * @emblem: a #GEmblem
98  *
99  * Creates a new emblemed icon for @icon with the emblem @emblem.
100  *
101  * Returns: a new #GIcon
102  *
103  * Since: 2.18
104  **/
105 GIcon *
106 g_emblemed_icon_new (GIcon   *icon,
107                      GEmblem *emblem)
108 {
109   GEmblemedIcon *emblemed;
110   
111   g_return_val_if_fail (G_IS_ICON (icon), NULL);
112   g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
113   g_return_val_if_fail (G_IS_EMBLEM (emblem), NULL);
114
115   emblemed = G_EMBLEMED_ICON (g_object_new (G_TYPE_EMBLEMED_ICON, NULL));
116   emblemed->icon = g_object_ref (icon);
117   
118   g_emblemed_icon_add_emblem (emblemed, emblem);
119
120   return G_ICON (emblemed);
121 }
122
123
124 /**
125  * g_emblemed_icon_get_icon:
126  * @emblemed: a #GEmblemedIcon
127  *
128  * Gets the main icon for @emblemed.
129  *
130  * Returns: a #GIcon that is owned by @emblemed
131  *
132  * Since: 2.18
133  **/
134 GIcon *
135 g_emblemed_icon_get_icon (GEmblemedIcon *emblemed)
136 {
137   g_return_val_if_fail (G_IS_EMBLEMED_ICON (emblemed), NULL);
138
139   return emblemed->icon;
140 }
141
142 /**
143  * g_emblemed_icon_get_emblems:
144  * @emblemed: a #GEmblemedIcon
145  *
146  * Gets the list of emblems for the @icon.
147  *
148  * Returns: a #GList of #GEmblem <!-- -->s that is owned by @emblemed
149  *
150  * Since: 2.18
151  **/
152
153 GList *
154 g_emblemed_icon_get_emblems (GEmblemedIcon *emblemed)
155 {
156   g_return_val_if_fail (G_IS_EMBLEMED_ICON (emblemed), NULL);
157
158   return emblemed->emblems;
159 }
160
161
162 /**
163  * g_emblemed_icon_add_emblem:
164  * @emblemed: a #GEmblemedIcon
165  * @emblem: a #GEmblem
166  *
167  * Adds @emblem to the #GList of #GEmblem <!-- -->s.
168  *
169  * Returns: a #GList of #GEmblem <!-- -->s that is owned by @emblemed
170  *
171  * Since: 2.18
172  **/
173 void 
174 g_emblemed_icon_add_emblem (GEmblemedIcon *emblemed,
175                             GEmblem       *emblem)
176 {
177   g_return_if_fail (G_IS_EMBLEMED_ICON (emblemed));
178   g_return_if_fail (G_IS_EMBLEM (emblem));
179
180   g_object_ref (emblem);
181   emblemed->emblems = g_list_append (emblemed->emblems, emblem);
182 }
183
184 static guint
185 g_emblemed_icon_hash (GIcon *icon)
186 {
187   GEmblemedIcon *emblemed = G_EMBLEMED_ICON (icon);
188   GList *list;
189   guint hash = g_icon_hash (emblemed->icon);
190
191   for (list = emblemed->emblems; list != NULL; list = list->next)
192     hash ^= g_icon_hash (G_ICON (list->data));
193
194   return hash;
195 }
196
197 static gint
198 g_emblem_comp (GEmblem *a,
199                GEmblem *b)
200 {
201   guint hash_a = g_icon_hash (G_ICON (a));
202   guint hash_b = g_icon_hash (G_ICON (b));
203
204   if(hash_a < hash_b)
205     return -1;
206
207   if(hash_a == hash_b)
208     return 0;
209
210   return 1;
211 }
212
213 static gboolean
214 g_emblemed_icon_equal (GIcon *icon1,
215                        GIcon *icon2)
216 {
217   GEmblemedIcon *emblemed1 = G_EMBLEMED_ICON (icon1);
218   GEmblemedIcon *emblemed2 = G_EMBLEMED_ICON (icon2);
219   GList *list1, *list2;
220
221   if (!g_icon_equal (emblemed1->icon, emblemed2->icon))
222     return FALSE;
223
224   list1 = emblemed1->emblems;
225   list2 = emblemed2->emblems;
226
227   list1 = g_list_sort (list1, (GCompareFunc) g_emblem_comp);
228   list2 = g_list_sort (list2, (GCompareFunc) g_emblem_comp);
229
230   while (list1 && list2)
231   {
232     if (!g_icon_equal (G_ICON (list1->data), G_ICON (list2->data)))
233         return FALSE;
234     
235     list1 = list1->next;
236     list2 = list2->next;
237   }
238   
239   return list1 == NULL && list2 == NULL;
240 }
241
242 static void
243 g_emblemed_icon_icon_iface_init (GIconIface *iface)
244 {
245   iface->hash = g_emblemed_icon_hash;
246   iface->equal = g_emblemed_icon_equal;
247 }
248
249 #define __G_EMBLEMED_ICON_C__
250 #include "gioaliasdef.c"