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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
/* 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"
#include "gvfsiconloadable.h"
#include "gmounttracker.h"
#include "gvfsdaemondbus.h"
#include "gdaemonvfs.h"
#include "gdaemonfileinputstream.h"
#include "gvfsdaemonprotocol.h"
#include "gvfsdbusutils.h"
/* see comment in common/giconvfs.c for why the GLoadableIcon interface is here */
static DBusMessage *
create_empty_message (GVfsIcon *vfs_icon,
const char *op,
GMountInfo **mount_info_out,
GError **error)
{
DBusMessage *message;
GMountInfo *mount_info;
mount_info = _g_daemon_vfs_get_mount_info_sync (vfs_icon->mount_spec,
"/",
error);
if (mount_info == NULL)
return NULL;
if (mount_info_out)
*mount_info_out = g_mount_info_ref (mount_info);
message =
dbus_message_new_method_call (mount_info->dbus_id,
mount_info->object_path,
G_VFS_DBUS_MOUNT_INTERFACE,
op);
_g_dbus_message_append_args (message, G_DBUS_TYPE_CSTRING, &(vfs_icon->icon_id), 0);
g_mount_info_unref (mount_info);
return message;
}
static DBusMessage *
do_sync_path_call (GVfsIcon *vfs_icon,
const char *op,
GMountInfo **mount_info_out,
DBusConnection **connection_out,
GCancellable *cancellable,
GError **error,
int first_arg_type,
...)
{
DBusMessage *message, *reply;
va_list var_args;
GError *my_error;
retry:
message = create_empty_message (vfs_icon, op, mount_info_out, error);
if (!message)
return NULL;
va_start (var_args, first_arg_type);
_g_dbus_message_append_args_valist (message,
first_arg_type,
var_args);
va_end (var_args);
my_error = NULL;
reply = _g_vfs_daemon_call_sync (message,
connection_out,
NULL, NULL, NULL,
cancellable, &my_error);
dbus_message_unref (message);
if (reply == NULL)
{
if (g_error_matches (my_error, G_VFS_ERROR, G_VFS_ERROR_RETRY))
{
g_error_free (my_error);
goto retry;
}
g_propagate_error (error, my_error);
}
return reply;
}
static GInputStream *
g_vfs_icon_load (GLoadableIcon *icon,
int size,
char **type,
GCancellable *cancellable,
GError **error)
{
DBusConnection *connection;
int fd;
DBusMessage *reply;
guint32 fd_id;
dbus_bool_t can_seek;
reply = do_sync_path_call (G_VFS_ICON (icon),
G_VFS_DBUS_MOUNT_OP_OPEN_ICON_FOR_READ,
NULL,
&connection,
cancellable,
error,
0);
if (reply == NULL)
return NULL;
if (!dbus_message_get_args (reply, NULL,
DBUS_TYPE_UINT32, &fd_id,
DBUS_TYPE_BOOLEAN, &can_seek,
DBUS_TYPE_INVALID))
{
dbus_message_unref (reply);
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
_("Invalid return value from %s"), "open_icon_for_read");
return NULL;
}
dbus_message_unref (reply);
fd = _g_dbus_connection_get_fd_sync (connection, fd_id);
if (fd == -1)
{
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
_("Didn't get stream file descriptor"));
return NULL;
}
return G_INPUT_STREAM (g_daemon_file_input_stream_new (fd, can_seek));
}
typedef void (*AsyncPathCallCallback) (DBusMessage *reply,
DBusConnection *connection,
GSimpleAsyncResult *result,
GCancellable *cancellable,
gpointer callback_data);
typedef struct {
GSimpleAsyncResult *result;
GVfsIcon *vfs_icon;
char *op;
GCancellable *cancellable;
DBusMessage *args;
AsyncPathCallCallback callback;
gpointer callback_data;
GDestroyNotify notify;
} AsyncPathCall;
static void
async_path_call_free (AsyncPathCall *data)
{
if (data->notify)
data->notify (data->callback_data);
if (data->result)
g_object_unref (data->result);
g_object_unref (data->vfs_icon);
g_free (data->op);
if (data->cancellable)
g_object_unref (data->cancellable);
if (data->args)
dbus_message_unref (data->args);
g_free (data);
}
static void
async_path_call_done (DBusMessage *reply,
DBusConnection *connection,
GError *io_error,
gpointer _data)
{
AsyncPathCall *data = _data;
GSimpleAsyncResult *result;
if (io_error != NULL)
{
g_simple_async_result_set_from_error (data->result, io_error);
_g_simple_async_result_complete_with_cancellable (data->result, data->cancellable);
async_path_call_free (data);
}
else
{
result = data->result;
g_object_weak_ref (G_OBJECT (result), (GWeakNotify)async_path_call_free, data);
data->result = NULL;
data->callback (reply, connection,
result,
data->cancellable,
data->callback_data);
/* Free data here, or later if callback ref:ed the result */
g_object_unref (result);
}
}
static void
do_async_path_call_callback (GMountInfo *mount_info,
gpointer _data,
GError *error)
{
AsyncPathCall *data = _data;
DBusMessage *message;
DBusMessageIter arg_source, arg_dest;
if (error != NULL)
{
g_simple_async_result_set_from_error (data->result, error);
_g_simple_async_result_complete_with_cancellable (data->result, data->cancellable);
async_path_call_free (data);
return;
}
message =
dbus_message_new_method_call (mount_info->dbus_id,
mount_info->object_path,
G_VFS_DBUS_MOUNT_INTERFACE,
data->op);
_g_dbus_message_append_args (message, G_DBUS_TYPE_CSTRING, &(data->vfs_icon->icon_id), 0);
/* Append more args from data->args */
if (data->args)
{
dbus_message_iter_init (data->args, &arg_source);
dbus_message_iter_init_append (message, &arg_dest);
_g_dbus_message_iter_copy (&arg_dest, &arg_source);
}
_g_vfs_daemon_call_async (message,
async_path_call_done, data,
data->cancellable);
dbus_message_unref (message);
}
static void
do_async_path_call (GVfsIcon *vfs_icon,
const char *op,
GCancellable *cancellable,
GAsyncReadyCallback op_callback,
gpointer op_callback_data,
AsyncPathCallCallback callback,
gpointer callback_data,
GDestroyNotify notify,
int first_arg_type,
...)
{
va_list var_args;
AsyncPathCall *data;
data = g_new0 (AsyncPathCall, 1);
data->result = g_simple_async_result_new (G_OBJECT (vfs_icon),
op_callback, op_callback_data,
NULL);
data->vfs_icon = g_object_ref (vfs_icon);
data->op = g_strdup (op);
if (cancellable)
data->cancellable = g_object_ref (cancellable);
data->callback = callback;
data->callback_data = callback_data;
data->notify = notify;
if (first_arg_type != 0)
{
data->args = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
if (data->args == NULL)
_g_dbus_oom ();
va_start (var_args, first_arg_type);
_g_dbus_message_append_args_valist (data->args,
first_arg_type,
var_args);
va_end (var_args);
}
_g_daemon_vfs_get_mount_info_async (vfs_icon->mount_spec,
"/",
do_async_path_call_callback,
data);
}
typedef struct {
GSimpleAsyncResult *result;
GCancellable *cancellable;
gboolean can_seek;
} GetFDData;
static void
load_async_get_fd_cb (int fd,
gpointer callback_data)
{
GetFDData *data = callback_data;
GFileInputStream *stream;
if (fd == -1)
{
g_simple_async_result_set_error (data->result,
G_IO_ERROR, G_IO_ERROR_FAILED,
_("Couldn't get stream file descriptor"));
}
else
{
stream = g_daemon_file_input_stream_new (fd, data->can_seek);
g_simple_async_result_set_op_res_gpointer (data->result, stream, g_object_unref);
}
_g_simple_async_result_complete_with_cancellable (data->result,
data->cancellable);
g_object_unref (data->result);
if (data->cancellable)
g_object_unref (data->cancellable);
g_free (data);
}
static void
load_async_cb (DBusMessage *reply,
DBusConnection *connection,
GSimpleAsyncResult *result,
GCancellable *cancellable,
gpointer callback_data)
{
guint32 fd_id;
dbus_bool_t can_seek;
GetFDData *get_fd_data;
if (!dbus_message_get_args (reply, NULL,
DBUS_TYPE_UINT32, &fd_id,
DBUS_TYPE_BOOLEAN, &can_seek,
DBUS_TYPE_INVALID))
{
g_simple_async_result_set_error (result,
G_IO_ERROR, G_IO_ERROR_FAILED,
_("Invalid return value from %s"), "open");
_g_simple_async_result_complete_with_cancellable (result, cancellable);
return;
}
get_fd_data = g_new0 (GetFDData, 1);
get_fd_data->result = g_object_ref (result);
if (cancellable)
get_fd_data->cancellable = g_object_ref (cancellable);
get_fd_data->can_seek = can_seek;
_g_dbus_connection_get_fd_async (connection, fd_id,
load_async_get_fd_cb, get_fd_data);
}
static void
g_vfs_icon_load_async (GLoadableIcon *icon,
int size,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
do_async_path_call (G_VFS_ICON (icon),
G_VFS_DBUS_MOUNT_OP_OPEN_ICON_FOR_READ,
cancellable,
callback, user_data,
load_async_cb, NULL, NULL,
0);
}
static GInputStream *
g_vfs_icon_load_finish (GLoadableIcon *icon,
GAsyncResult *res,
char **type,
GError **error)
{
GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
gpointer op;
op = g_simple_async_result_get_op_res_gpointer (simple);
if (op)
return g_object_ref (op);
return NULL;
}
static void
g_vfs_icon_loadable_icon_iface_init (GLoadableIconIface *iface)
{
iface->load = g_vfs_icon_load;
iface->load_async = g_vfs_icon_load_async;
iface->load_finish = g_vfs_icon_load_finish;
}
void
_g_vfs_icon_add_loadable_interface (void)
{
static const GInterfaceInfo g_implement_interface_info = {
(GInterfaceInitFunc) g_vfs_icon_loadable_icon_iface_init
};
g_type_add_interface_static (G_VFS_TYPE_ICON, G_TYPE_LOADABLE_ICON, &g_implement_interface_info);
}
|