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