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.1 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, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Alexander Larsson <alexl@redhat.com>
19  */
20 
21 #include "config.h"
22 
23 #include "gfileicon.h"
24 #include "gfile.h"
25 #include "gicon.h"
26 #include "glibintl.h"
27 #include "gloadableicon.h"
28 #include "ginputstream.h"
29 #include "gtask.h"
30 #include "gioerror.h"
31 
32 
33 /**
34  * SECTION:gfileicon
35  * @short_description: Icons pointing to an image file
36  * @include: gio/gio.h
37  * @see_also: #GIcon, #GLoadableIcon
38  *
39  * #GFileIcon specifies an icon by pointing to an image file
40  * to be used as icon.
41  *
42  **/
43 
44 static void g_file_icon_icon_iface_init          (GIconIface          *iface);
45 static void g_file_icon_loadable_icon_iface_init (GLoadableIconIface  *iface);
46 static void g_file_icon_load_async               (GLoadableIcon       *icon,
47 						  int                  size,
48 						  GCancellable        *cancellable,
49 						  GAsyncReadyCallback  callback,
50 						  gpointer             user_data);
51 
52 struct _GFileIcon
53 {
54   GObject parent_instance;
55 
56   GFile *file;
57 };
58 
59 struct _GFileIconClass
60 {
61   GObjectClass parent_class;
62 };
63 
64 enum
65 {
66   PROP_0,
67   PROP_FILE
68 };
69 
G_DEFINE_TYPE_WITH_CODE(GFileIcon,g_file_icon,G_TYPE_OBJECT,G_IMPLEMENT_INTERFACE (G_TYPE_ICON,g_file_icon_icon_iface_init)G_IMPLEMENT_INTERFACE (G_TYPE_LOADABLE_ICON,g_file_icon_loadable_icon_iface_init))70 G_DEFINE_TYPE_WITH_CODE (GFileIcon, g_file_icon, G_TYPE_OBJECT,
71                          G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
72                                                 g_file_icon_icon_iface_init)
73                          G_IMPLEMENT_INTERFACE (G_TYPE_LOADABLE_ICON,
74                                                 g_file_icon_loadable_icon_iface_init))
75 
76 static void
77 g_file_icon_get_property (GObject    *object,
78                           guint       prop_id,
79                           GValue     *value,
80                           GParamSpec *pspec)
81 {
82   GFileIcon *icon = G_FILE_ICON (object);
83 
84   switch (prop_id)
85     {
86       case PROP_FILE:
87         g_value_set_object (value, icon->file);
88         break;
89 
90       default:
91         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
92     }
93 }
94 
95 static void
g_file_icon_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)96 g_file_icon_set_property (GObject      *object,
97                           guint         prop_id,
98                           const GValue *value,
99                           GParamSpec   *pspec)
100 {
101   GFileIcon *icon = G_FILE_ICON (object);
102 
103   switch (prop_id)
104     {
105       case PROP_FILE:
106         icon->file = G_FILE (g_value_dup_object (value));
107         break;
108 
109       default:
110         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
111     }
112 }
113 
114 static void
g_file_icon_constructed(GObject * object)115 g_file_icon_constructed (GObject *object)
116 {
117 #ifndef G_DISABLE_ASSERT
118   GFileIcon *icon = G_FILE_ICON (object);
119 #endif
120 
121   G_OBJECT_CLASS (g_file_icon_parent_class)->constructed (object);
122 
123   /* Must have be set during construction */
124   g_assert (icon->file != NULL);
125 }
126 
127 static void
g_file_icon_finalize(GObject * object)128 g_file_icon_finalize (GObject *object)
129 {
130   GFileIcon *icon;
131 
132   icon = G_FILE_ICON (object);
133 
134   if (icon->file)
135     g_object_unref (icon->file);
136 
137   G_OBJECT_CLASS (g_file_icon_parent_class)->finalize (object);
138 }
139 
140 static void
g_file_icon_class_init(GFileIconClass * klass)141 g_file_icon_class_init (GFileIconClass *klass)
142 {
143   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
144 
145   gobject_class->get_property = g_file_icon_get_property;
146   gobject_class->set_property = g_file_icon_set_property;
147   gobject_class->finalize = g_file_icon_finalize;
148   gobject_class->constructed = g_file_icon_constructed;
149 
150   /**
151    * GFileIcon:file:
152    *
153    * The file containing the icon.
154    */
155   g_object_class_install_property (gobject_class, PROP_FILE,
156                                    g_param_spec_object ("file",
157                                                         P_("file"),
158                                                         P_("The file containing the icon"),
159                                                         G_TYPE_FILE,
160                                                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
161 }
162 
163 static void
g_file_icon_init(GFileIcon * file)164 g_file_icon_init (GFileIcon *file)
165 {
166 }
167 
168 /**
169  * g_file_icon_new:
170  * @file: a #GFile.
171  *
172  * Creates a new icon for a file.
173  *
174  * Returns: (transfer full) (type GFileIcon): a #GIcon for the given
175  *   @file, or %NULL on error.
176  **/
177 GIcon *
g_file_icon_new(GFile * file)178 g_file_icon_new (GFile *file)
179 {
180   g_return_val_if_fail (G_IS_FILE (file), NULL);
181 
182   return G_ICON (g_object_new (G_TYPE_FILE_ICON, "file", file, NULL));
183 }
184 
185 /**
186  * g_file_icon_get_file:
187  * @icon: a #GIcon.
188  *
189  * Gets the #GFile associated with the given @icon.
190  *
191  * Returns: (transfer none): a #GFile.
192  **/
193 GFile *
g_file_icon_get_file(GFileIcon * icon)194 g_file_icon_get_file (GFileIcon *icon)
195 {
196   g_return_val_if_fail (G_IS_FILE_ICON (icon), NULL);
197 
198   return icon->file;
199 }
200 
201 static guint
g_file_icon_hash(GIcon * icon)202 g_file_icon_hash (GIcon *icon)
203 {
204   GFileIcon *file_icon = G_FILE_ICON (icon);
205 
206   return g_file_hash (file_icon->file);
207 }
208 
209 static gboolean
g_file_icon_equal(GIcon * icon1,GIcon * icon2)210 g_file_icon_equal (GIcon *icon1,
211 		   GIcon *icon2)
212 {
213   GFileIcon *file1 = G_FILE_ICON (icon1);
214   GFileIcon *file2 = G_FILE_ICON (icon2);
215 
216   return g_file_equal (file1->file, file2->file);
217 }
218 
219 static gboolean
g_file_icon_to_tokens(GIcon * icon,GPtrArray * tokens,gint * out_version)220 g_file_icon_to_tokens (GIcon *icon,
221 		       GPtrArray *tokens,
222                        gint  *out_version)
223 {
224   GFileIcon *file_icon = G_FILE_ICON (icon);
225 
226   g_return_val_if_fail (out_version != NULL, FALSE);
227 
228   *out_version = 0;
229 
230   g_ptr_array_add (tokens, g_file_get_uri (file_icon->file));
231   return TRUE;
232 }
233 
234 static GIcon *
g_file_icon_from_tokens(gchar ** tokens,gint num_tokens,gint version,GError ** error)235 g_file_icon_from_tokens (gchar  **tokens,
236                          gint     num_tokens,
237                          gint     version,
238                          GError **error)
239 {
240   GIcon *icon;
241   GFile *file;
242 
243   icon = NULL;
244 
245   if (version != 0)
246     {
247       g_set_error (error,
248                    G_IO_ERROR,
249                    G_IO_ERROR_INVALID_ARGUMENT,
250                    _("Can’t handle version %d of GFileIcon encoding"),
251                    version);
252       goto out;
253     }
254 
255   if (num_tokens != 1)
256     {
257       g_set_error_literal (error,
258                            G_IO_ERROR,
259                            G_IO_ERROR_INVALID_ARGUMENT,
260                            _("Malformed input data for GFileIcon"));
261       goto out;
262     }
263 
264   file = g_file_new_for_uri (tokens[0]);
265   icon = g_file_icon_new (file);
266   g_object_unref (file);
267 
268  out:
269   return icon;
270 }
271 
272 static GVariant *
g_file_icon_serialize(GIcon * icon)273 g_file_icon_serialize (GIcon *icon)
274 {
275   GFileIcon *file_icon = G_FILE_ICON (icon);
276 
277   return g_variant_new ("(sv)", "file", g_variant_new_take_string (g_file_get_uri (file_icon->file)));
278 }
279 
280 static void
g_file_icon_icon_iface_init(GIconIface * iface)281 g_file_icon_icon_iface_init (GIconIface *iface)
282 {
283   iface->hash = g_file_icon_hash;
284   iface->equal = g_file_icon_equal;
285   iface->to_tokens = g_file_icon_to_tokens;
286   iface->from_tokens = g_file_icon_from_tokens;
287   iface->serialize = g_file_icon_serialize;
288 }
289 
290 
291 static GInputStream *
g_file_icon_load(GLoadableIcon * icon,int size,char ** type,GCancellable * cancellable,GError ** error)292 g_file_icon_load (GLoadableIcon  *icon,
293 		  int            size,
294 		  char          **type,
295 		  GCancellable   *cancellable,
296 		  GError        **error)
297 {
298   GFileInputStream *stream;
299   GFileIcon *file_icon = G_FILE_ICON (icon);
300 
301   stream = g_file_read (file_icon->file,
302 			cancellable,
303 			error);
304 
305   if (stream && type)
306     *type = NULL;
307 
308   return G_INPUT_STREAM (stream);
309 }
310 
311 static void
load_async_callback(GObject * source_object,GAsyncResult * res,gpointer user_data)312 load_async_callback (GObject      *source_object,
313 		     GAsyncResult *res,
314 		     gpointer      user_data)
315 {
316   GFileInputStream *stream;
317   GError *error = NULL;
318   GTask *task = user_data;
319 
320   stream = g_file_read_finish (G_FILE (source_object), res, &error);
321   if (stream == NULL)
322     g_task_return_error (task, error);
323   else
324     g_task_return_pointer (task, stream, g_object_unref);
325   g_object_unref (task);
326 }
327 
328 static void
g_file_icon_load_async(GLoadableIcon * icon,int size,GCancellable * cancellable,GAsyncReadyCallback callback,gpointer user_data)329 g_file_icon_load_async (GLoadableIcon       *icon,
330                         int                  size,
331                         GCancellable        *cancellable,
332                         GAsyncReadyCallback  callback,
333                         gpointer             user_data)
334 {
335   GFileIcon *file_icon = G_FILE_ICON (icon);
336   GTask *task;
337 
338   task = g_task_new (icon, cancellable, callback, user_data);
339   g_task_set_source_tag (task, g_file_icon_load_async);
340 
341   g_file_read_async (file_icon->file, 0,
342                      cancellable,
343                      load_async_callback, task);
344 }
345 
346 static GInputStream *
g_file_icon_load_finish(GLoadableIcon * icon,GAsyncResult * res,char ** type,GError ** error)347 g_file_icon_load_finish (GLoadableIcon  *icon,
348 			 GAsyncResult   *res,
349 			 char          **type,
350 			 GError        **error)
351 {
352   g_return_val_if_fail (g_task_is_valid (res, icon), NULL);
353 
354   if (type)
355     *type = NULL;
356 
357   return g_task_propagate_pointer (G_TASK (res), error);
358 }
359 
360 static void
g_file_icon_loadable_icon_iface_init(GLoadableIconIface * iface)361 g_file_icon_loadable_icon_iface_init (GLoadableIconIface *iface)
362 {
363   iface->load = g_file_icon_load;
364   iface->load_async = g_file_icon_load_async;
365   iface->load_finish = g_file_icon_load_finish;
366 }
367