Make GFileIcon more robust
[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 "gtask.h"
32 #include "gioerror.h"
33
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   if (icon->file)
124     g_object_unref (icon->file);
125
126   G_OBJECT_CLASS (g_file_icon_parent_class)->finalize (object);
127 }
128
129 static void
130 g_file_icon_class_init (GFileIconClass *klass)
131 {
132   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
133   
134   gobject_class->get_property = g_file_icon_get_property;
135   gobject_class->set_property = g_file_icon_set_property;
136   gobject_class->finalize = g_file_icon_finalize;
137
138   /**
139    * GFileIcon:file:
140    *
141    * The file containing the icon.
142    */
143   g_object_class_install_property (gobject_class, PROP_FILE,
144                                    g_param_spec_object ("file",
145                                                         P_("file"),
146                                                         P_("The file containing the icon"),
147                                                         G_TYPE_FILE,
148                                                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
149 }
150
151 static void
152 g_file_icon_init (GFileIcon *file)
153 {
154 }
155
156 /**
157  * g_file_icon_new:
158  * @file: a #GFile.
159  * 
160  * Creates a new icon for a file.
161  * 
162  * Returns: (transfer full) (type GFileIcon): a #GIcon for the given
163  *   @file, or %NULL on error.
164  **/
165 GIcon *
166 g_file_icon_new (GFile *file)
167 {
168   g_return_val_if_fail (G_IS_FILE (file), NULL);
169
170   return G_ICON (g_object_new (G_TYPE_FILE_ICON, "file", file, NULL));
171 }
172
173 /**
174  * g_file_icon_get_file:
175  * @icon: a #GIcon.
176  * 
177  * Gets the #GFile associated with the given @icon.
178  * 
179  * Returns: (transfer none): a #GFile, or %NULL.
180  **/
181 GFile *
182 g_file_icon_get_file (GFileIcon *icon)
183 {
184   g_return_val_if_fail (G_IS_FILE_ICON (icon), NULL);
185
186   return icon->file;
187 }
188
189 static guint
190 g_file_icon_hash (GIcon *icon)
191 {
192   GFileIcon *file_icon = G_FILE_ICON (icon);
193
194   return g_file_hash (file_icon->file);
195 }
196
197 static gboolean
198 g_file_icon_equal (GIcon *icon1,
199                    GIcon *icon2)
200 {
201   GFileIcon *file1 = G_FILE_ICON (icon1);
202   GFileIcon *file2 = G_FILE_ICON (icon2);
203   
204   return g_file_equal (file1->file, file2->file);
205 }
206
207 static gboolean
208 g_file_icon_to_tokens (GIcon *icon,
209                        GPtrArray *tokens,
210                        gint  *out_version)
211 {
212   GFileIcon *file_icon = G_FILE_ICON (icon);
213
214   g_return_val_if_fail (out_version != NULL, FALSE);
215
216   *out_version = 0;
217
218   g_ptr_array_add (tokens, g_file_get_uri (file_icon->file));
219   return TRUE;
220 }
221
222 static GIcon *
223 g_file_icon_from_tokens (gchar  **tokens,
224                          gint     num_tokens,
225                          gint     version,
226                          GError **error)
227 {
228   GIcon *icon;
229   GFile *file;
230
231   icon = NULL;
232
233   if (version != 0)
234     {
235       g_set_error (error,
236                    G_IO_ERROR,
237                    G_IO_ERROR_INVALID_ARGUMENT,
238                    _("Can't handle version %d of GFileIcon encoding"),
239                    version);
240       goto out;
241     }
242
243   if (num_tokens != 1)
244     {
245       g_set_error_literal (error,
246                            G_IO_ERROR,
247                            G_IO_ERROR_INVALID_ARGUMENT,
248                            _("Malformed input data for GFileIcon"));
249       goto out;
250     }
251
252   file = g_file_new_for_uri (tokens[0]);
253   icon = g_file_icon_new (file);
254   g_object_unref (file);
255
256  out:
257   return icon;
258 }
259
260 static GVariant *
261 g_file_icon_serialize (GIcon *icon)
262 {
263   GFileIcon *file_icon = G_FILE_ICON (icon);
264
265   return g_variant_new ("(sv)", "file", g_variant_new_take_string (g_file_get_uri (file_icon->file)));
266 }
267
268 static void
269 g_file_icon_icon_iface_init (GIconIface *iface)
270 {
271   iface->hash = g_file_icon_hash;
272   iface->equal = g_file_icon_equal;
273   iface->to_tokens = g_file_icon_to_tokens;
274   iface->from_tokens = g_file_icon_from_tokens;
275   iface->serialize = g_file_icon_serialize;
276 }
277
278
279 static GInputStream *
280 g_file_icon_load (GLoadableIcon  *icon,
281                   int            size,
282                   char          **type,
283                   GCancellable   *cancellable,
284                   GError        **error)
285 {
286   GFileInputStream *stream;
287   GFileIcon *file_icon = G_FILE_ICON (icon);
288
289   stream = g_file_read (file_icon->file,
290                         cancellable,
291                         error);
292
293   if (stream && type)
294     *type = NULL;
295   
296   return G_INPUT_STREAM (stream);
297 }
298
299 static void
300 load_async_callback (GObject      *source_object,
301                      GAsyncResult *res,
302                      gpointer      user_data)
303 {
304   GFileInputStream *stream;
305   GError *error = NULL;
306   GTask *task = user_data;
307
308   stream = g_file_read_finish (G_FILE (source_object), res, &error);
309   if (stream == NULL)
310     g_task_return_error (task, error);
311   else
312     g_task_return_pointer (task, stream, g_object_unref);
313   g_object_unref (task);
314 }
315
316 static void
317 g_file_icon_load_async (GLoadableIcon       *icon,
318                         int                  size,
319                         GCancellable        *cancellable,
320                         GAsyncReadyCallback  callback,
321                         gpointer             user_data)
322 {
323   GFileIcon *file_icon = G_FILE_ICON (icon);
324   GTask *task;
325
326   task = g_task_new (icon, cancellable, callback, user_data);
327   
328   g_file_read_async (file_icon->file, 0,
329                      cancellable,
330                      load_async_callback, task);
331 }
332
333 static GInputStream *
334 g_file_icon_load_finish (GLoadableIcon  *icon,
335                          GAsyncResult   *res,
336                          char          **type,
337                          GError        **error)
338 {
339   g_return_val_if_fail (g_task_is_valid (res, icon), NULL);
340
341   if (type)
342     *type = NULL;
343   
344   return g_task_propagate_pointer (G_TASK (res), error);
345 }
346
347 static void
348 g_file_icon_loadable_icon_iface_init (GLoadableIconIface *iface)
349 {
350   iface->load = g_file_icon_load;
351   iface->load_async = g_file_icon_load_async;
352   iface->load_finish = g_file_icon_load_finish;
353 }