File: gvfsicon.c

package info (click to toggle)
gvfs 1.30.4-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 22,580 kB
  • ctags: 12,211
  • sloc: ansic: 113,948; sh: 4,957; xml: 2,353; makefile: 1,601; python: 1,448; sed: 16
file content (316 lines) | stat: -rw-r--r-- 8,474 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* GIO - GLib Input, Output and Streaming Library
 *
 * Copyright (C) 2008 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Author: David Zeuthen <davidz@redhat.com>
 */

#include <config.h>
#include <string.h>
#include <glib/gi18n-lib.h>

#include "gvfsicon.h"

static void g_vfs_icon_icon_iface_init          (GIconIface          *iface);

/* Because of the way dependencies are currently set up, the
 * GLoadableIcon interface is in client/gvfsiconloadable.c and is
 * added in g_io_module_load() in client/gdaemonvfs.c.
 */

enum
{
  PROP_0,
  PROP_MOUNT_SPEC,
  PROP_ICON_ID
};

G_DEFINE_TYPE_EXTENDED (GVfsIcon,
			g_vfs_icon,
			G_TYPE_OBJECT,
			0,
			G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
					       g_vfs_icon_icon_iface_init))

static void
g_vfs_icon_get_property (GObject    *object,
                          guint       prop_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
  GVfsIcon *icon = G_VFS_ICON (object);

  switch (prop_id)
    {
    case PROP_MOUNT_SPEC:
      g_value_set_boxed (value, icon->mount_spec);
      break;

    case PROP_ICON_ID:
      g_value_set_string (value, icon->icon_id);
      break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
g_vfs_icon_set_property (GObject      *object,
                         guint         prop_id,
                         const GValue *value,
                         GParamSpec   *pspec)
{
  GVfsIcon *icon = G_VFS_ICON (object);

  switch (prop_id)
    {
    case PROP_MOUNT_SPEC:
      icon->mount_spec = g_mount_spec_ref (g_value_get_boxed (value));
      break;

    case PROP_ICON_ID:
      icon->icon_id = g_strdup (g_value_get_string (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
g_vfs_icon_finalize (GObject *object)
{
  GVfsIcon *vfs_icon;

  vfs_icon = G_VFS_ICON (object);

  if (vfs_icon->mount_spec != NULL)
    g_mount_spec_unref (vfs_icon->mount_spec);
  g_free (vfs_icon->icon_id);

  G_OBJECT_CLASS (g_vfs_icon_parent_class)->finalize (object);
}

static void
g_vfs_icon_class_init (GVfsIconClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->get_property = g_vfs_icon_get_property;
  gobject_class->set_property = g_vfs_icon_set_property;
  gobject_class->finalize = g_vfs_icon_finalize;

  /**
   * GVfsIcon:mount-spec:
   *
   * The mount spec.
   */
  g_object_class_install_property (gobject_class,
                                   PROP_MOUNT_SPEC,
                                   g_param_spec_boxed ("mount-spec",
                                                       "Mount Spec",
                                                       "Mount Spec",
                                                       G_TYPE_MOUNT_SPEC,
                                                       G_PARAM_CONSTRUCT_ONLY |
                                                       G_PARAM_READWRITE |
                                                       G_PARAM_STATIC_NAME |
                                                       G_PARAM_STATIC_BLURB |
                                                       G_PARAM_STATIC_NICK));

  /**
   * GVfsIcon:icon-id:
   *
   * The id of the icon.
   */
  g_object_class_install_property (gobject_class,
                                   PROP_ICON_ID,
                                   g_param_spec_string ("icon-id",
                                                       "Icon identifier",
                                                       "Icon identifier",
                                                       NULL,
                                                       G_PARAM_CONSTRUCT_ONLY |
                                                       G_PARAM_READWRITE |
                                                       G_PARAM_STATIC_NAME |
                                                       G_PARAM_STATIC_BLURB |
                                                       G_PARAM_STATIC_NICK));
}

static void
g_vfs_icon_init (GVfsIcon *file)
{
}

GMountSpec *
g_vfs_icon_get_mount_spec (GVfsIcon *vfs_icon)
{
  g_return_val_if_fail (G_VFS_IS_ICON (vfs_icon), NULL);
  return g_mount_spec_ref (vfs_icon->mount_spec);
}

const gchar *
g_vfs_icon_get_icon_id (GVfsIcon *vfs_icon)
{
  g_return_val_if_fail (G_VFS_IS_ICON (vfs_icon), NULL);
  return vfs_icon->icon_id;
}


GIcon *
g_vfs_icon_new (GMountSpec  *mount_spec,
                const gchar *icon_id)
{
  return G_ICON (g_object_new (G_VFS_TYPE_ICON,
                               "mount-spec", mount_spec,
                               "icon-id", icon_id,
                               NULL));
}

static guint
g_vfs_icon_hash (GIcon *icon)
{
  GVfsIcon *vfs_icon = G_VFS_ICON (icon);

  return g_mount_spec_hash (vfs_icon->mount_spec) ^ g_str_hash (vfs_icon->icon_id);
}

static int
safe_strcmp (const char *a,
             const char *b)
{
  if (a == NULL)
    a = "";
  if (b == NULL)
    b = "";

  return strcmp (a, b);
}

static gboolean
g_vfs_icon_equal (GIcon *icon1,
                  GIcon *icon2)
{
  GVfsIcon *vfs1 = G_VFS_ICON (icon1);
  GVfsIcon *vfs2 = G_VFS_ICON (icon2);

  return g_mount_spec_equal (vfs1->mount_spec, vfs2->mount_spec) &&
    (safe_strcmp (vfs1->icon_id, vfs2->icon_id) == 0);
}

static gboolean
g_vfs_icon_to_tokens (GIcon *icon,
		      GPtrArray *tokens,
                      gint  *out_version)
{
  GVfsIcon *vfs_icon = G_VFS_ICON (icon);
  char *s;

  g_return_val_if_fail (out_version != NULL, FALSE);

  *out_version = 0;

  s = g_mount_spec_to_string (vfs_icon->mount_spec);
  g_ptr_array_add (tokens, s);
  g_ptr_array_add (tokens, g_strdup (vfs_icon->icon_id));

  return TRUE;
}

static GIcon *
g_vfs_icon_from_tokens (gchar  **tokens,
                        gint     num_tokens,
                        gint     version,
                        GError **error)
{
  GMountSpec *mount_spec;
  GIcon *icon;

  icon = NULL;

  if (version != 0)
    {
      g_set_error (error,
                   G_IO_ERROR,
                   G_IO_ERROR_INVALID_ARGUMENT,
                   _("Can't handle version %d of GVfsIcon encoding"),
                   version);
      goto out;
    }

  if (num_tokens != 2)
    {
      g_set_error_literal (error,
                           G_IO_ERROR,
                           G_IO_ERROR_INVALID_ARGUMENT,
                           _("Malformed input data for GVfsIcon"));
      goto out;
    }

  mount_spec = g_mount_spec_new_from_string (tokens[0], error);
  if (mount_spec == NULL)
    goto out;

  icon = g_vfs_icon_new (mount_spec, tokens[1]);
  g_mount_spec_unref (mount_spec);

 out:
  return icon;
}

static GVariant *
g_vfs_icon_serialize (GIcon *icon)
{
  GVfsIcon *vfs_icon = G_VFS_ICON (icon);

  return g_variant_new ("(@ss)",
                        g_variant_new_take_string (g_mount_spec_to_string (vfs_icon->mount_spec)),
                        vfs_icon->icon_id);
}

GIcon *
g_vfs_icon_deserialize (GVariant *value)
{
  const gchar *mount_spec_str;
  const gchar *id_str;
  GMountSpec *mount_spec;
  GIcon *icon;

  if (!g_variant_is_of_type (value, G_VARIANT_TYPE ("(ss)")))
    return NULL;

  g_variant_get (value, "(&s&s)", &mount_spec_str, &id_str);

  mount_spec = g_mount_spec_new_from_string (mount_spec_str, NULL);
  if (mount_spec == NULL)
    return NULL;

  icon = g_vfs_icon_new (mount_spec, id_str);
  g_mount_spec_unref (mount_spec);

  return icon;
}

static void
g_vfs_icon_icon_iface_init (GIconIface *iface)
{
  iface->hash = g_vfs_icon_hash;
  iface->equal = g_vfs_icon_equal;
  iface->to_tokens = g_vfs_icon_to_tokens;
  iface->from_tokens = g_vfs_icon_from_tokens;
  iface->serialize = g_vfs_icon_serialize;
}