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