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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* xfer.c - Bonobo::Desktop::FileOperationService transfer service.
Copyright (C) 1999, 2000 Free Software Foundation
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public
License along with this program; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Authors:
Ettore Perazzoli <ettore@gnu.org>
Pavel Cisler <pavel@eazel.com>
*/
#include <config.h>
#include <gnome.h>
#include "dfos-xfer.h"
#include "fm-directory-view.h"
typedef struct XferInfo {
GnomeVFSAsyncHandle *handle;
GtkWidget *progress_dialog;
const char *operation_name;
const char *preparation_name;
GnomeVFSXferErrorMode error_mode;
GnomeVFSXferOverwriteMode overwrite_mode;
GtkWidget *parent_view;
} XferInfo;
static XferInfo *
xfer_info_new (GnomeVFSAsyncHandle *handle,
GnomeVFSXferOptions options,
GnomeVFSXferErrorMode error_mode,
GnomeVFSXferOverwriteMode overwrite_mode)
{
XferInfo *info;
info = g_new (XferInfo, 1);
info->handle = handle;
info->error_mode = error_mode;
info->overwrite_mode = overwrite_mode;
info->progress_dialog = NULL;
info->parent_view = NULL;
return info;
}
static void
xfer_dialog_clicked_callback (DFOSXferProgressDialog *dialog,
int button_number,
gpointer data)
{
XferInfo *info;
info = (XferInfo *) data;
gnome_vfs_async_cancel (info->handle);
gtk_widget_destroy (GTK_WIDGET (dialog));
}
static void
create_xfer_dialog (const GnomeVFSXferProgressInfo *progress_info,
XferInfo *xfer_info)
{
g_return_if_fail (xfer_info->progress_dialog == NULL);
xfer_info->progress_dialog
= dfos_xfer_progress_dialog_new ("Transfer in progress",
"",
1,
1);
gtk_signal_connect (GTK_OBJECT (xfer_info->progress_dialog),
"clicked",
GTK_SIGNAL_FUNC (xfer_dialog_clicked_callback),
xfer_info);
gtk_widget_show (xfer_info->progress_dialog);
}
static int
handle_xfer_ok (const GnomeVFSXferProgressInfo *progress_info,
XferInfo *xfer_info)
{
switch (progress_info->phase) {
case GNOME_VFS_XFER_PHASE_INITIAL:
create_xfer_dialog (progress_info, xfer_info);
return TRUE;
case GNOME_VFS_XFER_PHASE_COLLECTING:
dfos_xfer_progress_dialog_set_operation_string
(DFOS_XFER_PROGRESS_DIALOG
(xfer_info->progress_dialog),
xfer_info->preparation_name);
return TRUE;
case GNOME_VFS_XFER_PHASE_READYTOGO:
dfos_xfer_progress_dialog_set_operation_string
(DFOS_XFER_PROGRESS_DIALOG
(xfer_info->progress_dialog),
xfer_info->operation_name);
dfos_xfer_progress_dialog_set_total
(DFOS_XFER_PROGRESS_DIALOG
(xfer_info->progress_dialog),
progress_info->files_total,
progress_info->bytes_total);
return TRUE;
case GNOME_VFS_XFER_PHASE_XFERRING:
case GNOME_VFS_XFER_PHASE_OPENSOURCE:
case GNOME_VFS_XFER_PHASE_OPENTARGET:
if (progress_info->bytes_copied == 0) {
dfos_xfer_progress_dialog_new_file
(DFOS_XFER_PROGRESS_DIALOG
(xfer_info->progress_dialog),
progress_info->source_name,
progress_info->target_name,
progress_info->file_index,
progress_info->file_size);
} else {
dfos_xfer_progress_dialog_update
(DFOS_XFER_PROGRESS_DIALOG
(xfer_info->progress_dialog),
MIN (progress_info->bytes_copied,
progress_info->bytes_total),
MIN (progress_info->total_bytes_copied,
progress_info->bytes_total));
}
return TRUE;
case GNOME_VFS_XFER_PHASE_FILECOMPLETED:
/* FIXME? */
return TRUE;
case GNOME_VFS_XFER_PHASE_COMPLETED:
gtk_widget_destroy (xfer_info->progress_dialog);
g_free (xfer_info);
return TRUE;
default:
return TRUE;
}
}
static int
handle_xfer_vfs_error (const GnomeVFSXferProgressInfo *progress_info,
XferInfo *xfer_info)
{
/* Notice that the error mode in `xfer_info' is the one we have been
requested, but the transfer is always performed in mode
`GNOME_VFS_XFER_ERROR_MODE_QUERY'. */
int result;
char *text;
switch (xfer_info->error_mode) {
case GNOME_VFS_XFER_ERROR_MODE_QUERY:
/* transfer error, prompt the user to continue or stop */
text = g_strdup_printf ( _("Error %s copying file %s. \n"
"Would you like to continue?"),
gnome_vfs_result_to_string (progress_info->vfs_status),
progress_info->source_name);
result = file_operation_alert (xfer_info->parent_view, text,
_("File copy error"),
_("Skip"), _("Retry"), _("Stop"));
switch (result) {
case 0:
return GNOME_VFS_XFER_ERROR_ACTION_SKIP;
case 1:
return GNOME_VFS_XFER_ERROR_ACTION_ABORT;
case 2:
return GNOME_VFS_XFER_ERROR_ACTION_RETRY;
}
case GNOME_VFS_XFER_ERROR_MODE_ABORT:
default:
dfos_xfer_progress_dialog_freeze (DFOS_XFER_PROGRESS_DIALOG
(xfer_info->progress_dialog));
dfos_xfer_progress_dialog_thaw (DFOS_XFER_PROGRESS_DIALOG
(xfer_info->progress_dialog));
gtk_widget_destroy (xfer_info->progress_dialog);
return GNOME_VFS_XFER_ERROR_ACTION_ABORT;
}
}
static int
handle_xfer_overwrite (const GnomeVFSXferProgressInfo *progress_info,
XferInfo *xfer_info)
{
/* transfer conflict, prompt the user to replace or skip */
int result;
char *text;
text = g_strdup_printf ( _("File %s already exists. \n"
"Would you like to replace it?"),
progress_info->target_name);
result = file_operation_alert (xfer_info->parent_view, text,
_("File copy conflict"),
_("Replace All"), _("Replace"), _("Skip"));
switch (result) {
case 0:
return GNOME_VFS_XFER_OVERWRITE_ACTION_REPLACE_ALL;
case 1:
return GNOME_VFS_XFER_OVERWRITE_ACTION_REPLACE;
case 2:
return GNOME_VFS_XFER_OVERWRITE_ACTION_SKIP;
}
return 0;
}
static int
handle_xfer_duplicate (GnomeVFSXferProgressInfo *progress_info,
XferInfo *xfer_info)
{
char *old_name = progress_info->duplicate_name;
if (progress_info->duplicate_count < 2) {
progress_info->duplicate_name = g_strdup_printf ("%s (copy)",
progress_info->duplicate_name);
} else {
progress_info->duplicate_name = g_strdup_printf ("%s (copy %d)",
progress_info->duplicate_name,
progress_info->duplicate_count);
}
g_free (old_name);
return GNOME_VFS_XFER_ERROR_ACTION_SKIP;
}
static int
sync_xfer_callback (
GnomeVFSXferProgressInfo *progress_info,
gpointer data)
{
XferInfo *xfer_info;
xfer_info = (XferInfo *) data;
switch (progress_info->status) {
case GNOME_VFS_XFER_PROGRESS_STATUS_OK:
return handle_xfer_ok (progress_info, xfer_info);
case GNOME_VFS_XFER_PROGRESS_STATUS_VFSERROR:
return handle_xfer_vfs_error (progress_info, xfer_info);
case GNOME_VFS_XFER_PROGRESS_STATUS_OVERWRITE:
return handle_xfer_overwrite (progress_info, xfer_info);
case GNOME_VFS_XFER_PROGRESS_STATUS_DUPLICATE:
return handle_xfer_duplicate (progress_info, xfer_info);
default:
g_warning (_("Unknown GnomeVFSXferProgressStatus %d"),
progress_info->status);
return FALSE;
}
}
static int
xfer_callback (GnomeVFSAsyncHandle *handle,
GnomeVFSXferProgressInfo *progress_info,
gpointer data)
{
return sync_xfer_callback (progress_info, data);
}
void
dfos_xfer (DFOS *dfos,
const gchar *source_directory_uri,
GList *source_file_name_list,
const gchar *target_directory_uri,
GList *target_file_name_list,
GnomeVFSXferOptions options,
GnomeVFSXferErrorMode error_mode,
GnomeVFSXferOverwriteMode overwrite_mode)
{
GnomeVFSResult result;
XferInfo *xfer_info;
xfer_info = xfer_info_new (NULL, options, overwrite_mode, error_mode);
result = gnome_vfs_async_xfer (&xfer_info->handle,
source_directory_uri,
source_file_name_list,
target_directory_uri,
target_file_name_list,
options,
GNOME_VFS_XFER_ERROR_MODE_QUERY,
overwrite_mode,
xfer_callback,
xfer_info);
if (result != GNOME_VFS_OK) {
gchar *message;
GtkWidget *dialog;
message = g_strdup_printf (_("The transfer between\n%s\nand\n%s\ncould not be started:\n%s"),
source_directory_uri,
target_directory_uri,
gnome_vfs_result_to_string (result));
/* FIXME: signals and all that. */
dialog = gnome_error_dialog (message);
gtk_widget_show (dialog);
g_free (message);
g_free (xfer_info);
}
}
int
file_operation_alert (GtkWidget *parent_view, const char *text,
const char *title, const char *button_1_text,
const char *button_2_text, const char *button_3_text)
{
GtkWidget *dialog;
GtkWidget *prompt_widget;
GtkWidget *top_widget;
/* Don't use GNOME_STOCK_BUTTON_CANCEL because the
* red X is confusing in this context.
*/
dialog = gnome_dialog_new (title,
button_1_text,
button_2_text,
button_3_text,
NULL);
gnome_dialog_set_close (GNOME_DIALOG (dialog), TRUE);
gnome_dialog_close_hides (GNOME_DIALOG (dialog), TRUE);
top_widget = gtk_widget_get_toplevel (parent_view);
g_assert (GTK_IS_WINDOW (top_widget));
gnome_dialog_set_parent (GNOME_DIALOG (dialog), GTK_WINDOW (top_widget));
prompt_widget = gtk_label_new (text);
gtk_box_pack_start (GTK_BOX (GNOME_DIALOG (dialog)->vbox),
prompt_widget,
TRUE, TRUE, GNOME_PAD);
gtk_widget_show_all (dialog);
return gnome_dialog_run (GNOME_DIALOG (dialog));
}
void
fs_xfer (const GList *item_uris,
const GdkPoint *relative_item_points,
const char *target_dir,
int copy_action,
GtkWidget *view)
{
const GList *p;
GList *item_names;
GnomeVFSXferOptions move_options;
gchar *source_dir;
GnomeVFSURI *source_dir_uri;
GnomeVFSURI *target_dir_uri;
XferInfo *xfer_info;
const char *target_dir_uri_text;
g_assert (item_uris != NULL);
item_names = NULL;
source_dir_uri = NULL;
/* convert URI list to a source parent URI and a list of names */
for (p = item_uris; p != NULL; p = p->next) {
GnomeVFSURI *item_uri;
const gchar *item_name;
item_uri = gnome_vfs_uri_new (p->data);
item_name = gnome_vfs_uri_get_basename (item_uri);
item_names = g_list_append (item_names, g_strdup (item_name));
if (source_dir_uri == NULL)
source_dir_uri = gnome_vfs_uri_get_parent (item_uri);
gnome_vfs_uri_unref (item_uri);
}
source_dir = gnome_vfs_uri_to_string (source_dir_uri, GNOME_VFS_URI_HIDE_NONE);
if (target_dir != NULL) {
target_dir_uri = gnome_vfs_uri_new (target_dir);
target_dir_uri_text = target_dir;
} else {
/* assume duplication */
target_dir_uri = gnome_vfs_uri_ref (source_dir_uri);
target_dir_uri_text = gnome_vfs_uri_to_string (target_dir_uri,
GNOME_VFS_URI_HIDE_NONE);
}
/* figure out the right move/copy mode */
move_options = GNOME_VFS_XFER_RECURSIVE;
if (gnome_vfs_uri_equal (target_dir_uri, source_dir_uri)) {
g_assert (copy_action == GDK_ACTION_COPY);
move_options |= GNOME_VFS_XFER_USE_UNIQUE_NAMES;
} else if (copy_action == GDK_ACTION_MOVE) {
move_options |= GNOME_VFS_XFER_REMOVESOURCE;
}
/* set up the copy/move parameters */
xfer_info = g_new (XferInfo, 1);
xfer_info->parent_view = view;
xfer_info->progress_dialog = NULL;
if ((move_options & GNOME_VFS_XFER_REMOVESOURCE) != 0) {
xfer_info->operation_name = _("Moving");
xfer_info->preparation_name =_("Preparing To Move...");
} else {
xfer_info->operation_name = _("Copying");
xfer_info->preparation_name =_("Preparing To Copy...");
}
xfer_info->error_mode = GNOME_VFS_XFER_ERROR_MODE_QUERY;
xfer_info->overwrite_mode = GNOME_VFS_XFER_OVERWRITE_MODE_QUERY;
gnome_vfs_async_xfer (&xfer_info->handle, source_dir, item_names,
target_dir_uri_text, NULL,
move_options, GNOME_VFS_XFER_ERROR_MODE_QUERY,
GNOME_VFS_XFER_OVERWRITE_MODE_QUERY,
&xfer_callback, xfer_info);
if (!target_dir)
g_free ((char *)target_dir_uri_text);
gnome_vfs_uri_unref (target_dir_uri);
gnome_vfs_uri_unref (source_dir_uri);
g_free (source_dir);
}
|