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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595 | /* file-transfer-dialog.c
* Copyright (C) 2002 Ximian, Inc.
*
* Written by Rachel Hestilow <hestilow@ximian.com>
* Jens Granseuer <jensgr@gmx.net>
*
* 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, 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., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <glib.h>
#include <glib/gi18n.h>
#include <ctk/ctk.h>
#include <gio/gio.h>
#include <limits.h>
#include "file-transfer-dialog.h"
enum
{
PROP_0,
PROP_FROM_URI,
PROP_TO_URI,
PROP_FRACTION_COMPLETE,
PROP_NTH_URI,
PROP_TOTAL_URIS,
PROP_PARENT
};
enum
{
CANCEL,
DONE,
LAST_SIGNAL
};
guint file_transfer_dialog_signals[LAST_SIGNAL] = {0, };
struct _FileTransferDialogPrivate
{
CtkWidget *progress;
CtkWidget *status;
guint nth;
guint total;
GCancellable *cancellable;
};
typedef struct _FileTransferJob
{
FileTransferDialog *dialog;
CtkDialog *overwrite_dialog;
GSList *source_files;
GSList *target_files;
FileTransferDialogOptions options;
} FileTransferJob;
/* structure passed to the various callbacks */
typedef struct {
FileTransferDialog *dialog;
gchar *source;
gchar *target;
guint current_file;
guint total_files;
goffset current_bytes;
goffset total_bytes;
gint response;
CtkDialog *overwrite_dialog;
} FileTransferData;
G_DEFINE_TYPE_WITH_PRIVATE (FileTransferDialog, file_transfer_dialog, CTK_TYPE_DIALOG)<--- There is an unknown macro here somewhere. Configuration is required. If G_DEFINE_TYPE_WITH_PRIVATE is a macro then please configure it.
static void
file_transfer_dialog_update_num_files (FileTransferDialog *dlg)
{
gchar *str;
if (dlg->priv->total <= 1)
return;
str = g_strdup_printf (_("Copying file: %u of %u"),
dlg->priv->nth, dlg->priv->total);
ctk_progress_bar_set_text (CTK_PROGRESS_BAR (dlg->priv->progress), str);
g_free (str);
}
static void
file_transfer_dialog_response (CtkDialog *dlg, gint response_id)
{
FileTransferDialog *dialog = FILE_TRANSFER_DIALOG (dlg);
g_cancellable_cancel (dialog->priv->cancellable);
}
static void
file_transfer_dialog_finalize (GObject *object)
{
FileTransferDialog *dlg = FILE_TRANSFER_DIALOG (object);
if (dlg->priv->cancellable)
{
g_object_unref (dlg->priv->cancellable);
dlg->priv->cancellable = NULL;
}
G_OBJECT_CLASS (file_transfer_dialog_parent_class)->finalize (object);
}
static void
file_transfer_dialog_set_prop (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
FileTransferDialog *dlg = FILE_TRANSFER_DIALOG (object);
GFile *file;
gchar *str;
gchar *str2;
gchar *base;
gchar *escaped;
CtkWindow *parent;
guint n;
switch (prop_id)
{
case PROP_FROM_URI:
file = g_file_new_for_uri (g_value_get_string (value));
base = g_file_get_basename (file);
escaped = g_uri_escape_string (base, G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, TRUE);
str = g_strdup_printf (_("Copying '%s'"), escaped);
str2 = g_strdup_printf ("<big><b>%s</b></big>", str);
ctk_label_set_markup (CTK_LABEL (dlg->priv->status), str2);
g_free (base);
g_free (escaped);
g_free (str);
g_free (str2);
g_object_unref (file);
break;
case PROP_TO_URI:
break;
case PROP_FRACTION_COMPLETE:
ctk_progress_bar_set_fraction (CTK_PROGRESS_BAR (dlg->priv->progress), g_value_get_double (value));
break;
case PROP_NTH_URI:
n = g_value_get_uint (value);
if (n != dlg->priv->nth)
{
dlg->priv->nth = g_value_get_uint (value);
file_transfer_dialog_update_num_files (dlg);
}
break;
case PROP_TOTAL_URIS:
n = g_value_get_uint (value);
if (n != dlg->priv->nth)
{
dlg->priv->total = g_value_get_uint (value);
file_transfer_dialog_update_num_files (dlg);
}
break;
case PROP_PARENT:
parent = g_value_get_pointer (value);
if (parent)
{
ctk_window_set_title (CTK_WINDOW (dlg), ctk_window_get_title (parent));
ctk_window_set_transient_for (CTK_WINDOW (dlg), parent);
}
else
ctk_window_set_title (CTK_WINDOW (dlg),
_("Copying files"));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
file_transfer_dialog_get_prop (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
FileTransferDialog *dlg = FILE_TRANSFER_DIALOG (object);
switch (prop_id)
{
case PROP_NTH_URI:
g_value_set_uint (value, dlg->priv->nth);
break;
case PROP_TOTAL_URIS:
g_value_set_uint (value, dlg->priv->total);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
file_transfer_dialog_class_init (FileTransferDialogClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = file_transfer_dialog_finalize;
object_class->get_property = file_transfer_dialog_get_prop;
object_class->set_property = file_transfer_dialog_set_prop;
CTK_DIALOG_CLASS (klass)->response = file_transfer_dialog_response;
g_object_class_install_property
(object_class, PROP_PARENT,
g_param_spec_pointer ("parent",
_("Parent Window"),
_("Parent window of the dialog"),
G_PARAM_READWRITE));
g_object_class_install_property
(object_class, PROP_FROM_URI,
g_param_spec_string ("from_uri",
_("From URI"),
_("URI currently transferring from"),
NULL,
G_PARAM_READWRITE));
g_object_class_install_property
(object_class, PROP_TO_URI,
g_param_spec_string ("to_uri",
_("To URI"),
_("URI currently transferring to"),
NULL,
G_PARAM_WRITABLE));
g_object_class_install_property
(object_class, PROP_FRACTION_COMPLETE,
g_param_spec_double ("fraction_complete",
_("Fraction completed"),
_("Fraction of transfer currently completed"),
0, 1, 0,
G_PARAM_WRITABLE));
g_object_class_install_property
(object_class, PROP_NTH_URI,
g_param_spec_uint ("nth_uri",
_("Current URI index"),
_("Current URI index - starts from 1"),
1, INT_MAX, 1,
G_PARAM_READWRITE));
g_object_class_install_property
(object_class, PROP_TOTAL_URIS,
g_param_spec_uint ("total_uris",
_("Total URIs"),
_("Total number of URIs"),
1, INT_MAX, 1,
G_PARAM_READWRITE));
file_transfer_dialog_signals[CANCEL] =
g_signal_new ("cancel",
G_TYPE_FROM_CLASS (object_class),
G_SIGNAL_RUN_FIRST,
0,
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
file_transfer_dialog_signals[DONE] =
g_signal_new ("done",
G_TYPE_FROM_CLASS (object_class),
G_SIGNAL_RUN_LAST,
0,
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}
static void
file_transfer_dialog_init (FileTransferDialog *dlg)
{
CtkWidget *vbox;
CtkWidget *hbox;
CtkWidget *progress_vbox;
char *markup;
CtkWidget *content_area;
content_area = ctk_dialog_get_content_area (CTK_DIALOG (dlg));
dlg->priv = file_transfer_dialog_get_instance_private (dlg);
dlg->priv->cancellable = g_cancellable_new ();
ctk_container_set_border_width (CTK_CONTAINER (content_area), 4);
ctk_box_set_spacing (CTK_BOX (content_area), 4);
ctk_widget_set_size_request (CTK_WIDGET (dlg), 350, -1);
vbox = ctk_box_new (CTK_ORIENTATION_VERTICAL, 6);
ctk_container_set_border_width (CTK_CONTAINER (vbox), 6);
ctk_box_pack_start (CTK_BOX (content_area), vbox, TRUE, TRUE, 0);
dlg->priv->status = ctk_label_new (NULL);
markup = g_strconcat ("<big><b>", _("Copying files"), "</b></big>", NULL);
ctk_label_set_markup (CTK_LABEL (dlg->priv->status), markup);
g_free (markup);
ctk_label_set_xalign (CTK_LABEL (dlg->priv->status), 0.0);
ctk_label_set_yalign (CTK_LABEL (dlg->priv->status), 0.0);
ctk_box_pack_start (CTK_BOX (vbox), dlg->priv->status, FALSE, FALSE, 0);
hbox = ctk_box_new (CTK_ORIENTATION_HORIZONTAL, 0);
ctk_box_pack_start (CTK_BOX (vbox), hbox, TRUE, TRUE, 0);
progress_vbox = ctk_box_new (CTK_ORIENTATION_VERTICAL, 0);
ctk_box_pack_start (CTK_BOX (vbox), progress_vbox, FALSE, FALSE, 0);
dlg->priv->progress = ctk_progress_bar_new ();
ctk_box_pack_start (CTK_BOX (progress_vbox),
dlg->priv->progress, FALSE, FALSE, 0);
ctk_dialog_add_button (CTK_DIALOG (dlg),
"ctk-cancel", CTK_RESPONSE_CANCEL);
ctk_container_set_border_width (CTK_CONTAINER (dlg), 6);
ctk_widget_show_all (content_area);
}
CtkWidget*
file_transfer_dialog_new (void)
{
return CTK_WIDGET (g_object_new (file_transfer_dialog_get_type (),
NULL));
}
CtkWidget*
file_transfer_dialog_new_with_parent (CtkWindow *parent)
{
return CTK_WIDGET (g_object_new (file_transfer_dialog_get_type (),
"parent", parent, NULL));
}
static gboolean
file_transfer_job_update (gpointer user_data)
{
FileTransferData *data = user_data;
gdouble fraction;
gdouble current_fraction;
if (data->total_bytes == 0)
current_fraction = 0.0;
else
current_fraction = ((gdouble) data->current_bytes) / data->total_bytes;
fraction = ((gdouble) data->current_file - 1) / data->total_files +
(1.0 / data->total_files) * current_fraction;
g_object_set (data->dialog,
"from_uri", data->source,
"to_uri", data->target,
"nth_uri", data->current_file,
"fraction_complete", fraction,
NULL);
return FALSE;
}
static void
file_transfer_job_progress (goffset current_bytes,
goffset total_bytes,
gpointer user_data)
{
FileTransferData *data = user_data;
data->current_bytes = current_bytes;
data->total_bytes = total_bytes;
file_transfer_job_update (data);
}
static void
file_transfer_job_destroy (FileTransferJob *job)
{
g_object_unref (job->dialog);
g_slist_foreach (job->source_files, (GFunc) g_object_unref, NULL);
g_slist_foreach (job->target_files, (GFunc) g_object_unref, NULL);
g_slist_free (job->source_files);
g_slist_free (job->target_files);
if (job->overwrite_dialog != NULL)
ctk_widget_destroy (CTK_WIDGET (job->overwrite_dialog));
g_free (job);
}
static gboolean
file_transfer_dialog_done (FileTransferDialog *dialog)
{
g_signal_emit (dialog,
file_transfer_dialog_signals[DONE],
0, NULL);
return FALSE;
}
static gboolean
file_transfer_dialog_cancel (FileTransferDialog *dialog)
{
g_signal_emit (dialog,
file_transfer_dialog_signals[CANCEL],
0, NULL);
return FALSE;
}
static gboolean
file_transfer_dialog_overwrite (gpointer user_data)
{
FileTransferData *data = user_data;
CtkDialog *dialog;
dialog = data->overwrite_dialog;
if (dialog != NULL) {
} else {
CtkWidget *button;
dialog = CTK_DIALOG (ctk_message_dialog_new (CTK_WINDOW (data->dialog),
CTK_DIALOG_MODAL, CTK_MESSAGE_QUESTION,
CTK_BUTTONS_NONE,
_("File '%s' already exists. Do you want to overwrite it?"),
data->target));
ctk_dialog_add_button (dialog, _("_Skip"), CTK_RESPONSE_NO);
ctk_dialog_add_button (dialog, _("Overwrite _All"), CTK_RESPONSE_APPLY);
button = ctk_button_new_with_label (_("_Overwrite"));
ctk_button_set_image (CTK_BUTTON (button),
ctk_image_new_from_icon_name ("ctk-apply",
CTK_ICON_SIZE_BUTTON));
ctk_dialog_add_action_widget (dialog, button, CTK_RESPONSE_YES);
ctk_widget_show (button);
data->overwrite_dialog = dialog;
}
data->response = ctk_dialog_run (dialog);
ctk_widget_hide (CTK_WIDGET (dialog));
return FALSE;
}
/* TODO: support transferring directories recursively? */
static gboolean
file_transfer_job_schedule (GIOSchedulerJob *io_job,
GCancellable *cancellable,
FileTransferJob *job)
{
GFile *source, *target;
gboolean success;
GFileCopyFlags copy_flags = G_FILE_COPY_NONE;
FileTransferData data;
GError *error;
gboolean retry;
/* take the first file from the list and copy it */
source = job->source_files->data;
job->source_files = g_slist_delete_link (job->source_files, job->source_files);
target = job->target_files->data;
job->target_files = g_slist_delete_link (job->target_files, job->target_files);
data.dialog = job->dialog;
data.overwrite_dialog = job->overwrite_dialog;
data.current_file = job->dialog->priv->nth + 1;
data.total_files = job->dialog->priv->total;
data.current_bytes = data.total_bytes = 0;
data.source = g_file_get_basename (source);
data.target = g_file_get_basename (target);
g_io_scheduler_job_send_to_mainloop (io_job,
file_transfer_job_update,
&data,
NULL);
if (job->options & FILE_TRANSFER_DIALOG_OVERWRITE)
copy_flags |= G_FILE_COPY_OVERWRITE;
do {
retry = FALSE;
error = NULL;
success = g_file_copy (source, target,
copy_flags,
job->dialog->priv->cancellable,
file_transfer_job_progress,
&data,
&error);
if (error != NULL)
{
if (error->domain == G_IO_ERROR &&
error->code == G_IO_ERROR_EXISTS)
{
/* since the job is run in a thread, we cannot simply run
* a dialog here and need to defer it to the mainloop */
data.response = CTK_RESPONSE_NONE;
g_io_scheduler_job_send_to_mainloop (io_job,
file_transfer_dialog_overwrite,
&data,
NULL);
if (data.response == CTK_RESPONSE_YES) {
retry = TRUE;
copy_flags |= G_FILE_COPY_OVERWRITE;
} else if (data.response == CTK_RESPONSE_APPLY) {
retry = TRUE;
job->options |= FILE_TRANSFER_DIALOG_OVERWRITE;
copy_flags |= G_FILE_COPY_OVERWRITE;
} else {
success = TRUE;
}
job->overwrite_dialog = data.overwrite_dialog;
}
g_error_free (error);
}
} while (retry);
g_object_unref (source);
g_object_unref (target);
g_free (data.source);
g_free (data.target);
if (success)
{
if (job->source_files == NULL)
{
g_io_scheduler_job_send_to_mainloop_async (io_job,
(GSourceFunc) file_transfer_dialog_done,
g_object_ref (job->dialog),
g_object_unref);
return FALSE;
}
}
else /* error on copy or cancelled */
{
g_io_scheduler_job_send_to_mainloop_async (io_job,
(GSourceFunc) file_transfer_dialog_cancel,
g_object_ref (job->dialog),
g_object_unref);
return FALSE;
}
/* more work to do... */
return TRUE;
}
void
file_transfer_dialog_copy_async (FileTransferDialog *dlg,
GList *source_files,
GList *target_files,
FileTransferDialogOptions options,
int priority)
{
FileTransferJob *job;
GList *l;
guint n;
job = g_new0 (FileTransferJob, 1);
job->dialog = g_object_ref (dlg);
job->options = options;
/* we need to copy the list contents for private use */
n = 0;
for (l = g_list_last (source_files); l; l = l->prev, ++n)
{
job->source_files = g_slist_prepend (job->source_files,
g_object_ref (l->data));
}
for (l = g_list_last (target_files); l; l = l->prev)
{
job->target_files = g_slist_prepend (job->target_files,
g_object_ref (l->data));
}
g_object_set (dlg, "total_uris", n, NULL);
g_io_scheduler_push_job ((GIOSchedulerJobFunc) file_transfer_job_schedule,
job,
(GDestroyNotify) file_transfer_job_destroy,
priority,
dlg->priv->cancellable);
}
|