Coverage Report

Created: 2018-09-25 13:52

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