Added. Added. Added. Added.
[platform/upstream/glib.git] / gio / gthemedicon.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
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  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include <config.h>
24
25 #include "gthemedicon.h"
26
27 #include "gioalias.h"
28
29 /**
30  * SECTION:gthemedicon
31  * @short_description: Themed Icons
32  * @see_also: #GIcon, #GLoadableIcon
33  *
34  * #GThemedIcon is an implementation of #GIcon for themed icons.
35  **/
36
37 static void g_themed_icon_icon_iface_init (GIconIface       *iface);
38
39 struct _GThemedIcon
40 {
41   GObject parent_instance;
42   
43   char **names;
44 };
45
46 struct _GThemedIconClass
47 {
48   GObjectClass parent_class;
49 };
50
51 G_DEFINE_TYPE_WITH_CODE (GThemedIcon, g_themed_icon, G_TYPE_OBJECT,
52                          G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
53                                                 g_themed_icon_icon_iface_init))
54   
55 static void
56 g_themed_icon_finalize (GObject *object)
57 {
58   GThemedIcon *themed;
59
60   themed = G_THEMED_ICON (object);
61
62   g_strfreev (themed->names);
63   
64   if (G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize)
65     (*G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize) (object);
66 }
67
68 static void
69 g_themed_icon_class_init (GThemedIconClass *klass)
70 {
71   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
72   
73   gobject_class->finalize = g_themed_icon_finalize;
74 }
75
76 static void
77 g_themed_icon_init (GThemedIcon *themed)
78 {
79 }
80
81 /**
82  * g_themed_icon_new:
83  * @iconname: a string containing an icon name.
84  * 
85  * Creates a new themed icon for @iconname.
86  * 
87  * Returns: a new #GThemedIcon.
88  **/
89 GIcon *
90 g_themed_icon_new (const char *iconname)
91 {
92   GThemedIcon *themed;
93
94   g_return_val_if_fail (iconname != NULL, NULL);
95
96   themed = g_object_new (G_TYPE_THEMED_ICON, NULL);
97   themed->names = g_new (char *, 2);
98   themed->names[0] = g_strdup (iconname);
99   themed->names[1] = NULL;
100   
101   return G_ICON (themed);
102 }
103
104 /**
105  * g_themed_icon_new_from_names:
106  * @iconnames: an array of strings containing icon names.
107  * @len: the number of elements in the @iconnames array.
108  * 
109  * Creates a new themed icon for @iconnames.
110  * 
111  * Returns: a new #GThemedIcon.
112  **/
113 GIcon *
114 g_themed_icon_new_from_names (char **iconnames, int len)
115 {
116   GThemedIcon *themed;
117   int i;
118
119   g_return_val_if_fail (iconnames != NULL, NULL);
120   
121   themed = g_object_new (G_TYPE_THEMED_ICON, NULL);
122   if (len == -1)
123     themed->names = g_strdupv (iconnames);
124   else
125     {
126       themed->names = g_new (char *, len + 1);
127       for (i = 0; i < len; i++)
128         themed->names[i] = g_strdup (iconnames[i]);
129       themed->names[i] = NULL;
130     }
131   
132   
133   return G_ICON (themed);
134 }
135
136 /**
137  * g_themed_icon_get_names:
138  * @icon: a #GThemedIcon.
139  * 
140  * Gets the names of icons from within @icon.
141  * 
142  * Returns: a list of icon names.
143  **/
144 const char * const *
145 g_themed_icon_get_names (GThemedIcon *icon)
146 {
147   g_return_val_if_fail (G_IS_THEMED_ICON (icon), NULL);
148   return (const char * const *)icon->names;
149 }
150
151 static guint
152 g_themed_icon_hash (GIcon *icon)
153 {
154   GThemedIcon *themed = G_THEMED_ICON (icon);
155   guint hash;
156   int i;
157
158   hash = 0;
159
160   for (i = 0; themed->names[i] != NULL; i++)
161     hash ^= g_str_hash (themed->names[i]);
162   
163   return hash;
164 }
165
166 static gboolean
167 g_themed_icon_equal (GIcon *icon1,
168                     GIcon *icon2)
169 {
170   GThemedIcon *themed1 = G_THEMED_ICON (icon1);
171   GThemedIcon *themed2 = G_THEMED_ICON (icon2);
172   int i;
173
174   for (i = 0; themed1->names[i] != NULL && themed2->names[i] != NULL; i++)
175     {
176       if (!g_str_equal (themed1->names[i], themed2->names[i]))
177         return FALSE;
178     }
179
180   return themed1->names[i] == NULL && themed2->names[i] == NULL;
181 }
182
183 static void
184 g_themed_icon_icon_iface_init (GIconIface *iface)
185 {
186   iface->hash = g_themed_icon_hash;
187   iface->equal = g_themed_icon_equal;
188 }
189
190 #define __G_THEMED_ICON_C__
191 #include "gioaliasdef.c"