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