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