http://mail.gnome.org/archives/gtk-devel-list/2007-October/msg00089.html
[platform/upstream/glib.git] / gio / gfileicon.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 "gfileicon.h"
26 #include "gsimpleasyncresult.h"
27
28 #include "gioalias.h"
29
30 /**
31  * SECTION:gfileicon
32  * @short_description: Icons for given files
33  * @see_also: #GIcon, #GLoadableIcon
34  * @include: gio/gfileicon.h
35  * 
36  * #GFileIcon gets the default icon for a #GFile.  
37  * 
38  **/
39
40 static void g_file_icon_icon_iface_init          (GIconIface          *iface);
41 static void g_file_icon_loadable_icon_iface_init (GLoadableIconIface  *iface);
42 static void g_file_icon_load_async               (GLoadableIcon       *icon,
43                                                   int                  size,
44                                                   GCancellable        *cancellable,
45                                                   GAsyncReadyCallback  callback,
46                                                   gpointer             user_data);
47
48 struct _GFileIcon
49 {
50   GObject parent_instance;
51
52   GFile *file;
53 };
54
55 struct _GFileIconClass
56 {
57   GObjectClass parent_class;
58 };
59
60 G_DEFINE_TYPE_WITH_CODE (GFileIcon, g_file_icon, G_TYPE_OBJECT,
61                          G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
62                                                 g_file_icon_icon_iface_init);
63                          G_IMPLEMENT_INTERFACE (G_TYPE_LOADABLE_ICON,
64                                                 g_file_icon_loadable_icon_iface_init);
65                          )
66   
67 static void
68 g_file_icon_finalize (GObject *object)
69 {
70   GFileIcon *icon;
71
72   icon = G_FILE_ICON (object);
73
74   g_object_unref (icon->file);
75   
76   if (G_OBJECT_CLASS (g_file_icon_parent_class)->finalize)
77     (*G_OBJECT_CLASS (g_file_icon_parent_class)->finalize) (object);
78 }
79
80 static void
81 g_file_icon_class_init (GFileIconClass *klass)
82 {
83   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
84   
85   gobject_class->finalize = g_file_icon_finalize;
86 }
87
88 static void
89 g_file_icon_init (GFileIcon *file)
90 {
91 }
92
93 /**
94  * g_file_icon_new:
95  * @file: a #GFile.
96  * 
97  * Creates a new icon for a file.
98  * 
99  * Returns: a #GIcon for the given @file, or %NULL on error.
100  **/
101 GIcon *
102 g_file_icon_new (GFile *file)
103 {
104   GFileIcon *icon;
105
106   g_return_val_if_fail (G_IS_FILE (file), NULL);
107
108   icon = g_object_new (G_TYPE_FILE_ICON, NULL);
109   icon->file = g_object_ref (file);
110   
111   return G_ICON (icon);
112 }
113
114 /**
115  * g_file_icon_get_file:
116  * @icon: a #GIcon.
117  * 
118  * Gets the #GFile associated with the given @icon.
119  * 
120  * Returns: a #GFile, or %NULL.
121  **/
122 GFile *
123 g_file_icon_get_file (GFileIcon *icon)
124 {
125   g_return_val_if_fail (G_IS_FILE_ICON (icon), NULL);
126
127   return icon->file;
128 }
129
130 static guint
131 g_file_icon_hash (GIcon *icon)
132 {
133   GFileIcon *file_icon = G_FILE_ICON (icon);
134
135   return g_file_hash (file_icon->file);
136 }
137
138 static gboolean
139 g_file_icon_equal (GIcon *icon1,
140                    GIcon *icon2)
141 {
142   GFileIcon *file1 = G_FILE_ICON (icon1);
143   GFileIcon *file2 = G_FILE_ICON (icon2);
144   
145   return g_file_equal (file1->file, file2->file);
146 }
147
148
149 static void
150 g_file_icon_icon_iface_init (GIconIface *iface)
151 {
152   iface->hash = g_file_icon_hash;
153   iface->equal = g_file_icon_equal;
154 }
155
156
157 static GInputStream *
158 g_file_icon_load (GLoadableIcon  *icon,
159                   int            size,
160                   char          **type,
161                   GCancellable   *cancellable,
162                   GError        **error)
163 {
164   GFileInputStream *stream;
165   GFileIcon *file_icon = G_FILE_ICON (icon);
166
167   stream = g_file_read (file_icon->file,
168                         cancellable,
169                         error);
170   
171   return G_INPUT_STREAM (stream);
172 }
173
174 typedef struct {
175   GLoadableIcon *icon;
176   GAsyncReadyCallback callback;
177   gpointer user_data;
178 } LoadData;
179
180 static void
181 load_data_free (LoadData *data)
182 {
183   g_object_unref (data->icon);
184   g_free (data);
185 }
186
187 static void
188 load_async_callback (GObject      *source_object,
189                      GAsyncResult *res,
190                      gpointer      user_data)
191 {
192   GFileInputStream *stream;
193   GError *error = NULL;
194   GSimpleAsyncResult *simple;
195   LoadData *data = user_data;
196
197   stream = g_file_read_finish (G_FILE (source_object), res, &error);
198   
199   if (stream == NULL)
200     {
201       simple = g_simple_async_result_new_from_error (G_OBJECT (data->icon),
202                                                      data->callback,
203                                                      data->user_data,
204                                                      error);
205       g_error_free (error);
206     }
207   else
208     {
209       simple = g_simple_async_result_new (G_OBJECT (data->icon),
210                                           data->callback,
211                                           data->user_data,
212                                           g_file_icon_load_async);
213       
214       g_simple_async_result_set_op_res_gpointer (simple,
215                                                  stream,
216                                                  g_object_unref);
217   }
218
219
220   g_simple_async_result_complete (simple);
221   
222   load_data_free (data);
223 }
224
225 static void
226 g_file_icon_load_async (GLoadableIcon       *icon,
227                         int                  size,
228                         GCancellable        *cancellable,
229                         GAsyncReadyCallback  callback,
230                         gpointer             user_data)
231 {
232   GFileIcon *file_icon = G_FILE_ICON (icon);
233   LoadData *data;
234
235   data = g_new0 (LoadData, 1);
236   data->icon = g_object_ref (icon);
237   data->callback = callback;
238   data->user_data = user_data;
239   
240   g_file_read_async (file_icon->file, 0,
241                      cancellable,
242                      load_async_callback, data);
243   
244 }
245
246 static GInputStream *
247 g_file_icon_load_finish (GLoadableIcon  *icon,
248                          GAsyncResult   *res,
249                          char          **type,
250                          GError        **error)
251 {
252   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
253   gpointer op;
254
255   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_icon_load_async);
256
257   if (type)
258     *type = NULL;
259   
260   op = g_simple_async_result_get_op_res_gpointer (simple);
261   if (op)
262     return g_object_ref (op);
263   
264   return NULL;
265 }
266
267 static void
268 g_file_icon_loadable_icon_iface_init (GLoadableIconIface *iface)
269 {
270   iface->load = g_file_icon_load;
271   iface->load_async = g_file_icon_load_async;
272   iface->load_finish = g_file_icon_load_finish;
273 }
274
275 #define __G_FILE_ICON_C__
276 #include "gioaliasdef.c"