summaryrefslogtreecommitdiff
path: root/capplets/common/file-transfer-dialog.c
blob: dc6a924dd32950971c3cba22dafa7542e17a0b19 (plain)
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
596
597
598
599
/* 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 <gtk/gtk.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
{
	GtkWidget *progress;
	GtkWidget *status;
	guint nth;
	guint total;
	GCancellable *cancellable;
};

#define FILE_TRANSFER_DIALOG_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object), file_transfer_dialog_get_type (), FileTransferDialogPrivate))

typedef struct _FileTransferJob
{
	FileTransferDialog *dialog;
	GtkDialog *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;
	GtkDialog *overwrite_dialog;
} FileTransferData;

G_DEFINE_TYPE (FileTransferDialog, file_transfer_dialog, GTK_TYPE_DIALOG)

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);
	gtk_progress_bar_set_text (GTK_PROGRESS_BAR (dlg->priv->progress), str);
	g_free (str);
}

static void
file_transfer_dialog_response (GtkDialog *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;
	GtkWindow *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);
		gtk_label_set_markup (GTK_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:
		gtk_progress_bar_set_fraction (GTK_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)
		{
			gtk_window_set_title (GTK_WINDOW (dlg), gtk_window_get_title (parent));
			gtk_window_set_transient_for (GTK_WINDOW (dlg), parent);
		}
		else
			gtk_window_set_title (GTK_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;

	GTK_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);

        g_type_class_add_private (klass, sizeof (FileTransferDialogPrivate));
}

static void
file_transfer_dialog_init (FileTransferDialog *dlg)
{
	GtkWidget *vbox;
	GtkWidget *hbox;
	GtkWidget *progress_vbox;
	char      *markup;
	GtkWidget *content_area;

	content_area = gtk_dialog_get_content_area (GTK_DIALOG (dlg));
	dlg->priv = FILE_TRANSFER_DIALOG_GET_PRIVATE (dlg);
	dlg->priv->cancellable = g_cancellable_new ();

	gtk_container_set_border_width (GTK_CONTAINER (content_area), 4);
	gtk_box_set_spacing (GTK_BOX (content_area), 4);

	gtk_widget_set_size_request (GTK_WIDGET (dlg), 350, -1);

	vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
	gtk_container_set_border_width (GTK_CONTAINER (vbox), 6);
	gtk_box_pack_start (GTK_BOX (content_area), vbox, TRUE, TRUE, 0);

	dlg->priv->status = gtk_label_new (NULL);
	markup = g_strconcat ("<big><b>", _("Copying files"), "</b></big>", NULL);
	gtk_label_set_markup (GTK_LABEL (dlg->priv->status), markup);
	g_free (markup);

	gtk_label_set_xalign (GTK_LABEL (dlg->priv->status), 0.0);
	gtk_label_set_yalign (GTK_LABEL (dlg->priv->status), 0.0);

	gtk_box_pack_start (GTK_BOX (vbox), dlg->priv->status, FALSE, FALSE, 0);

	hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
	gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);

	progress_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
	gtk_box_pack_start (GTK_BOX (vbox), progress_vbox, FALSE, FALSE, 0);

	dlg->priv->progress = gtk_progress_bar_new ();
	gtk_box_pack_start (GTK_BOX (progress_vbox),
			    dlg->priv->progress, FALSE, FALSE, 0);

	gtk_dialog_add_button (GTK_DIALOG (dlg),
			       "gtk-cancel", GTK_RESPONSE_CANCEL);

	gtk_container_set_border_width (GTK_CONTAINER (dlg), 6);

	gtk_widget_show_all (content_area);
}

GtkWidget*
file_transfer_dialog_new (void)
{
	return GTK_WIDGET (g_object_new (file_transfer_dialog_get_type (),
					 NULL));
}

GtkWidget*
file_transfer_dialog_new_with_parent (GtkWindow *parent)
{
	return GTK_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)
		gtk_widget_destroy (GTK_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;
	GtkDialog *dialog;

	dialog = data->overwrite_dialog;

	if (dialog != NULL) {
	} else {
		GtkWidget *button;

		dialog = GTK_DIALOG (gtk_message_dialog_new (GTK_WINDOW (data->dialog),
							     GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION,
							     GTK_BUTTONS_NONE,
							     _("File '%s' already exists. Do you want to overwrite it?"),
							     data->target));

		gtk_dialog_add_button (dialog, _("_Skip"), GTK_RESPONSE_NO);
		gtk_dialog_add_button (dialog, _("Overwrite _All"), GTK_RESPONSE_APPLY);

		button = gtk_button_new_with_label (_("_Overwrite"));
		gtk_button_set_image (GTK_BUTTON (button),
				      gtk_image_new_from_icon_name ("gtk-apply",
								    GTK_ICON_SIZE_BUTTON));
		gtk_dialog_add_action_widget (dialog, button, GTK_RESPONSE_YES);
		gtk_widget_show (button);

		data->overwrite_dialog = dialog;
	}

	data->response = gtk_dialog_run (dialog);

	gtk_widget_hide (GTK_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 = GTK_RESPONSE_NONE;
				g_io_scheduler_job_send_to_mainloop (io_job,
								     file_transfer_dialog_overwrite,
								     &data,
								     NULL);

				if (data.response == GTK_RESPONSE_YES) {
					retry = TRUE;
					copy_flags |= G_FILE_COPY_OVERWRITE;
				} else if (data.response == GTK_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);
}