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