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