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