nautilus-file-operations.c 69.8 KB
Newer Older
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2

3
/* nautilus-file-operations.c - Nautilus file operations.
Ettore Perazzoli's avatar
Ettore Perazzoli committed
4

Elliot Lee's avatar
Elliot Lee committed
5
   Copyright (C) 1999, 2000 Free Software Foundation
6
   Copyright (C) 2000, 2001 Eazel, Inc.
Ettore Perazzoli's avatar
Ettore Perazzoli committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

   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.
   
23 24
   Authors: Ettore Perazzoli <ettore@gnu.org> 
            Pavel Cisler <pavel@eazel.com> 
25
 */
Ettore Perazzoli's avatar
Ettore Perazzoli committed
26 27

#include <config.h>
28
#include "nautilus-file-operations.h"
Ettore Perazzoli's avatar
Ettore Perazzoli committed
29

30 31
#include "nautilus-file-operations-progress.h"
#include "nautilus-lib-self-check-functions.h"
Ramiro Estrugo's avatar
Ramiro Estrugo committed
32 33 34 35 36

#include <eel/eel-gdk-font-extensions.h>
#include <eel/eel-glib-extensions.h>
#include <eel/eel-gtk-extensions.h>
#include <eel/eel-stock-dialogs.h>
Ramiro Estrugo's avatar
Ramiro Estrugo committed
37 38
#include <eel/eel-vfs-extensions.h>

Ettore Perazzoli's avatar
Ettore Perazzoli committed
39
#include <gnome.h>
40
#include <gtk/gtklabel.h>
41
#include <libgnomevfs/gnome-vfs-async-ops.h>
42
#include <libgnomevfs/gnome-vfs-find-directory.h>
43 44
#include <libgnomevfs/gnome-vfs-ops.h>
#include <libgnomevfs/gnome-vfs-result.h>
Pavel Cisler's avatar
Pavel Cisler committed
45
#include <libgnomevfs/gnome-vfs-uri.h>
46
#include <libgnomevfs/gnome-vfs-utils.h>
47
#include <libnautilus-extensions/nautilus-file-changes-queue.h>
Ramiro Estrugo's avatar
Ramiro Estrugo committed
48 49 50
#include "nautilus-global-preferences.h"
#include "nautilus-link.h"
#include "nautilus-trash-monitor.h"
Ettore Perazzoli's avatar
Ettore Perazzoli committed
51

52
typedef enum {
53 54 55 56 57 58 59 60
	TRANSFER_MOVE,
	TRANSFER_COPY,
	TRANSFER_DUPLICATE,
	TRANSFER_MOVE_TO_TRASH,
	TRANSFER_EMPTY_TRASH,
	TRANSFER_DELETE,
	TRANSFER_LINK
} TransferKind;
61

62 63
/* Copy engine callback state */
typedef struct {
Ettore Perazzoli's avatar
Ettore Perazzoli committed
64
	GnomeVFSAsyncHandle *handle;
65
	NautilusFileOperationsProgress *progress_dialog;
Pavel Cisler's avatar
Pavel Cisler committed
66
	const char *operation_title;	/* "Copying files" */
67
	const char *action_label;	/* "Files copied:" */
Pavel Cisler's avatar
Pavel Cisler committed
68 69 70
	const char *progress_verb;	/* "Copying" */
	const char *preparation_name;	/* "Preparing To Copy..." */
	const char *cleanup_name;	/* "Finishing Move..." */
Ettore Perazzoli's avatar
Ettore Perazzoli committed
71 72
	GnomeVFSXferErrorMode error_mode;
	GnomeVFSXferOverwriteMode overwrite_mode;
Pavel Cisler's avatar
Pavel Cisler committed
73
	GtkWidget *parent_view;
74
	TransferKind kind;
75
	gboolean show_progress_dialog;
76
	void (* done_callback) (GHashTable *debuting_uris, gpointer data);
77
	gpointer done_callback_data;
78 79
	GHashTable *debuting_uris;
	gboolean cancelled;	
80
} TransferInfo;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
81

82 83 84 85 86 87 88 89
static TransferInfo *
transfer_info_new (GtkWidget *parent_view)
{
	TransferInfo *result;
	
	result = g_new0 (TransferInfo, 1);
	result->parent_view = parent_view;
	
Ramiro Estrugo's avatar
Ramiro Estrugo committed
90
	eel_nullify_when_destroyed (&result->parent_view);
91 92 93 94 95 96 97
	
	return result;
}

static void
transfer_info_destroy (TransferInfo *transfer_info)
{
Ramiro Estrugo's avatar
Ramiro Estrugo committed
98
	eel_nullify_cancel (&transfer_info->parent_view);
99 100
	
	if (transfer_info->progress_dialog != NULL) {
101
		nautilus_file_operations_progress_done (transfer_info->progress_dialog);
102 103 104
	}
	
	if (transfer_info->debuting_uris != NULL) {
105 106
		eel_g_hash_table_destroy_deep_custom
			(transfer_info->debuting_uris, (GFunc) g_free, NULL, NULL, NULL);
107 108 109 110 111
	}
	
	g_free (transfer_info);
}

112 113 114 115 116 117 118 119 120 121 122 123
/* Struct used to control applying icon positions to 
 * top level items during a copy, drag, new folder creation and
 * link creation
 */
typedef struct {
	GdkPoint *icon_positions;
	int last_icon_position_index;
	GList *uris;
	const GList *last_uri;
} IconPositionIterator;

static IconPositionIterator *
124
icon_position_iterator_new (GArray *icon_positions, const GList *uris)
125 126
{
	IconPositionIterator *result;
127
	guint index;
128

129
	g_assert (icon_positions->len == g_list_length ((GList *)uris));
130 131
	result = g_new (IconPositionIterator, 1);
	
132
	/* make our own copy of the icon locations */
133
	result->icon_positions = g_new (GdkPoint, icon_positions->len);
134 135 136
	for (index = 0; index < icon_positions->len; index++) {
		result->icon_positions[index] = g_array_index (icon_positions, GdkPoint, index);
	}
137 138
	result->last_icon_position_index = 0;

Ramiro Estrugo's avatar
Ramiro Estrugo committed
139
	result->uris = eel_g_str_list_copy ((GList *)uris);
140 141 142 143 144 145 146 147 148 149 150 151 152
	result->last_uri = result->uris;

	return result;
}

static void
icon_position_iterator_free (IconPositionIterator *position_iterator)
{
	if (position_iterator == NULL) {
		return;
	}
	
	g_free (position_iterator->icon_positions);
Ramiro Estrugo's avatar
Ramiro Estrugo committed
153
	eel_g_list_free_deep (position_iterator->uris);
154 155 156
	g_free (position_iterator);
}

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
static gboolean
icon_position_iterator_get_next (IconPositionIterator *position_iterator,
				 const char *next_uri,
				 GdkPoint *point)
{
	if (position_iterator == NULL) {
		return FALSE;
	}
		
	for (;;) {
		if (position_iterator->last_uri == NULL) {
			/* we are done, no more points left */
			return FALSE;
		}

		/* Scan for the next point that matches the source_name
		 * uri.
		 */
		if (strcmp ((const char *) position_iterator->last_uri->data, 
			    next_uri) == 0) {
			break;
		}
		
		/* Didn't match -- a uri must have been skipped by the copy 
		 * engine because of a name conflict. All we need to do is 
		 * skip ahead too.
		 */
		position_iterator->last_uri = position_iterator->last_uri->next;
		position_iterator->last_icon_position_index++; 
	}

	/* apply the location to the target file */
	*point = position_iterator->icon_positions
		[position_iterator->last_icon_position_index];

	/* advance to the next point for next time */
	position_iterator->last_uri = position_iterator->last_uri->next;
	position_iterator->last_icon_position_index++; 

	return TRUE;
}

199 200 201 202 203 204 205 206 207 208
/* Hack to get the GdkFont used by a GtkLabel in an error dialog.
 * We need to do this because the string truncation needs to be
 * done before a dialog is instantiated.
 * 
 * This is probably not super fast but it is not a problem in the
 * context we are using it, truncating a string while displaying an
 * error dialog.
 */
static GdkFont *
get_label_font (void)
209
{
210 211
	GtkWidget *label;
	GtkStyle *style;
212 213 214 215 216 217
	GdkFont *font;

	/* FIXME: Does this really pick up the correct font if the rc
	 * file has a special case for the particular dialog we are
	 * preparing?
	 */
218 219 220

	label = gtk_label_new ("");
	style = gtk_widget_get_style (label);
221 222 223
	font = style->font;
	gdk_font_ref (font);
	gtk_object_sink (GTK_OBJECT (label));
224

225
	return font;
226 227 228
}

static char *
229
ellipsize_string_for_dialog (const char *str)
230 231
{
	GdkFont *font;
232 233
	int maximum_width;
	char *result;
234

235
	/* get a nice length to ellipsize to, based on the font */
236
	font = get_label_font ();
237
	maximum_width = gdk_string_width (font, "MMMMMMMMMMMMMMMMMMMMMM");
238 239

	result = eel_string_ellipsize (str, font, maximum_width, EEL_ELLIPSIZE_MIDDLE);
240
	gdk_font_unref (font);
241

242 243
	return result;
}
244

245 246 247 248
static char *
format_and_ellipsize_uri_for_dialog (const char *uri)
{
	char *unescaped, *result;
249

Ramiro Estrugo's avatar
Ramiro Estrugo committed
250
	unescaped = eel_format_uri_for_display (uri);
251
	result = ellipsize_string_for_dialog (unescaped);
252
	g_free (unescaped);
253

254 255 256 257
	return result;
}

static char *
258
extract_and_ellipsize_file_name_for_dialog (const char *uri)
259
{
260 261
	const char *last_part;
	char *unescaped, *result;
262
	
263
	last_part = g_basename (uri);
264
	g_return_val_if_fail (last_part != NULL, NULL);
265 266 267 268

	unescaped = gnome_vfs_unescape_string_for_display (last_part);
	result = ellipsize_string_for_dialog (unescaped);
	g_free (unescaped);
269

270 271 272
	return result;
}

273
static GtkWidget *
274
parent_for_error_dialog (TransferInfo *transfer_info)
275
{
276
	if (transfer_info->progress_dialog != NULL) {
277
		return GTK_WIDGET (transfer_info->progress_dialog);
278 279
	}

280
	return transfer_info->parent_view;
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
static void
fit_rect_on_screen (GdkRectangle *rect)
{
	if (rect->x + rect->width > gdk_screen_width ()) {
		rect->x = gdk_screen_width () - rect->width;
	}

	if (rect->y + rect->height > gdk_screen_height ()) {
		rect->y = gdk_screen_height () - rect->height;
	}

	if (rect->x < 0) {
		rect->x = 0;
	}

	if (rect->y < 0) {
		rect->y = 0;
	}
}

static void
center_dialog_over_rect (GtkWindow *window, GdkRectangle rect)
{
	g_return_if_fail (GTK_WINDOW (window) != NULL);

	fit_rect_on_screen (&rect);

	gtk_widget_set_uposition (GTK_WIDGET (window), 
		rect.x + rect.width / 2 
			- GTK_WIDGET (window)->allocation.width / 2,
		rect.y + rect.height / 2
			- GTK_WIDGET (window)->allocation.height / 2);
}

static void
center_dialog_over_window (GtkWindow *window, GtkWindow *over)
{
	GdkRectangle rect;
	int x, y, w, h;

	g_return_if_fail (GTK_WINDOW (window) != NULL);
	g_return_if_fail (GTK_WINDOW (over) != NULL);

	gdk_window_get_root_origin (GTK_WIDGET (over)->window, &x, &y);
	gdk_window_get_size (GTK_WIDGET (over)->window, &w, &h);
	rect.x = x;
	rect.y = y;
	rect.width = w;
	rect.height = h;

	center_dialog_over_rect (window, rect);
}

336 337 338 339 340 341 342
static gboolean
handle_close_callback (GnomeDialog *dialog, TransferInfo *tranfer_info)
{
	tranfer_info->cancelled = TRUE;
	return FALSE;
}

Ettore Perazzoli's avatar
Ettore Perazzoli committed
343
static void
344
create_transfer_dialog (const GnomeVFSXferProgressInfo *progress_info,
345
			TransferInfo *transfer_info)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
346
{
347
	if (!transfer_info->show_progress_dialog) {
348
		return;
349
	}
350

351
	g_return_if_fail (transfer_info->progress_dialog == NULL);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
352

353
	transfer_info->progress_dialog = nautilus_file_operations_progress_new 
354
		(transfer_info->operation_title, "", "", "", 0, 0);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
355

356 357 358
	/* Treat clicking on the close box or use of the escape key
	 * the same as clicking cancel.
	 */
359
	gtk_signal_connect (GTK_OBJECT (transfer_info->progress_dialog),
Ettore Perazzoli's avatar
Ettore Perazzoli committed
360
			    "clicked",
361 362 363 364 365
			    GTK_SIGNAL_FUNC (gnome_dialog_close),
			    NULL);
	gtk_signal_connect (GTK_OBJECT (transfer_info->progress_dialog),
			    "close",
			    GTK_SIGNAL_FUNC (handle_close_callback),
366
			    transfer_info);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
367

368
	gtk_widget_show (GTK_WIDGET (transfer_info->progress_dialog));
369 370

	/* Make the progress dialog show up over the window we are copying into */
371 372
	if (transfer_info->parent_view != NULL) {
		center_dialog_over_window (GTK_WINDOW (transfer_info->progress_dialog), 
373
					   GTK_WINDOW (gtk_widget_get_toplevel (transfer_info->parent_view)));
374
	}
Ettore Perazzoli's avatar
Ettore Perazzoli committed
375 376
}

Pavel Cisler's avatar
Pavel Cisler committed
377
static void
378
progress_dialog_set_to_from_item_text (NautilusFileOperationsProgress *dialog,
379 380 381
				       const char *progress_verb,
				       const char *from_uri, const char *to_uri, 
				       gulong index, gulong size)
Pavel Cisler's avatar
Pavel Cisler committed
382 383 384 385 386 387 388 389
{
	char *item;
	char *from_path;
	char *to_path;
	char *progress_label_text;
	const char *from_prefix;
	const char *to_prefix;
	GnomeVFSURI *uri;
390
	int length;
Pavel Cisler's avatar
Pavel Cisler committed
391 392 393 394 395 396 397 398 399 400 401 402

	item = NULL;
	from_path = NULL;
	to_path = NULL;
	from_prefix = "";
	to_prefix = "";
	progress_label_text = NULL;

	if (from_uri != NULL) {
		uri = gnome_vfs_uri_new (from_uri);
		item = gnome_vfs_uri_extract_short_name (uri);
		from_path = gnome_vfs_uri_extract_dirname (uri);
403 404 405 406 407 408 409

		/* remove the last '/' */
		length = strlen (from_path);
		if (from_path [length - 1] == '/') {
			from_path [length - 1] = '\0';
		}
		
Pavel Cisler's avatar
Pavel Cisler committed
410 411 412
		gnome_vfs_uri_unref (uri);
		g_assert (progress_verb);
		progress_label_text = g_strdup_printf ("%s:", progress_verb);
413
		/* "From" dialog label, source path gets placed next to it in the dialog */
Pavel Cisler's avatar
Pavel Cisler committed
414 415 416 417
		from_prefix = _("From:");
	}

	if (to_uri != NULL) {
418
		uri = gnome_vfs_uri_new (to_uri);
Pavel Cisler's avatar
Pavel Cisler committed
419
		to_path = gnome_vfs_uri_extract_dirname (uri);
420 421 422 423 424 425 426

		/* remove the last '/' */
		length = strlen (to_path);
		if (to_path [length - 1] == '/') {
			to_path [length - 1] = '\0';
		}

Pavel Cisler's avatar
Pavel Cisler committed
427
		gnome_vfs_uri_unref (uri);
428
		/* "To" dialog label, source path gets placed next to it in the dialog */
Pavel Cisler's avatar
Pavel Cisler committed
429 430 431
		to_prefix = _("To:");
	}

432 433 434 435 436 437 438
	nautilus_file_operations_progress_new_file
		(dialog,
		 progress_label_text != NULL ? progress_label_text : "",
		 item != NULL ? item : "",
		 from_path != NULL ? from_path : "",
		 to_path != NULL ? to_path : "",
		 from_prefix, to_prefix, index, size);
Pavel Cisler's avatar
Pavel Cisler committed
439 440 441 442 443 444 445

	g_free (progress_label_text);
	g_free (item);
	g_free (from_path);
	g_free (to_path);
}

Pavel Cisler's avatar
Pavel Cisler committed
446
static int
447
handle_transfer_ok (const GnomeVFSXferProgressInfo *progress_info,
448
		    TransferInfo *transfer_info)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
449
{
450 451
	if (transfer_info->cancelled
		&& progress_info->phase != GNOME_VFS_XFER_PHASE_COMPLETED) {
452 453
		/* If cancelled, delete any partially copied files that are laying
		 * around and return.
454
		 */
455 456 457 458 459 460 461 462 463 464
		if (progress_info->bytes_total != progress_info->bytes_copied) {
			GList *delete_me = NULL;

			delete_me = g_list_append (delete_me, progress_info->target_name);

			nautilus_file_operations_delete (delete_me, transfer_info->parent_view);

			g_list_free (delete_me);
		}

465 466 467
		return 0;
	}
	
Ettore Perazzoli's avatar
Ettore Perazzoli committed
468
	switch (progress_info->phase) {
Pavel Cisler's avatar
Pavel Cisler committed
469
	case GNOME_VFS_XFER_PHASE_INITIAL:
470
		create_transfer_dialog (progress_info, transfer_info);
471
		return 1;
Pavel Cisler's avatar
Pavel Cisler committed
472 473

	case GNOME_VFS_XFER_PHASE_COLLECTING:
474
		if (transfer_info->progress_dialog != NULL) {
475
			nautilus_file_operations_progress_set_operation_string
476 477
				(transfer_info->progress_dialog,
				 transfer_info->preparation_name);
478
		}
479
		return 1;
Pavel Cisler's avatar
Pavel Cisler committed
480 481

	case GNOME_VFS_XFER_PHASE_READYTOGO:
482
		if (transfer_info->progress_dialog != NULL) {
483 484 485
			nautilus_file_operations_progress_set_operation_string
				(transfer_info->progress_dialog,
				 transfer_info->action_label);
486
			nautilus_file_operations_progress_set_total
487 488 489
				(transfer_info->progress_dialog,
				 progress_info->files_total,
				 progress_info->bytes_total);
490
		}
491
		return 1;
Pavel Cisler's avatar
Pavel Cisler committed
492
				 
493
	case GNOME_VFS_XFER_PHASE_DELETESOURCE:
494
		nautilus_file_changes_consume_changes (FALSE);
495
		if (transfer_info->progress_dialog != NULL) {
496 497 498 499 500 501 502
			progress_dialog_set_to_from_item_text
				(transfer_info->progress_dialog,
				 transfer_info->progress_verb,
				 progress_info->source_name,
				 NULL,
				 progress_info->file_index,
				 progress_info->file_size);
Pavel Cisler's avatar
Pavel Cisler committed
503

504
			nautilus_file_operations_progress_update_sizes
505
				(transfer_info->progress_dialog,
506
				 MIN (progress_info->bytes_copied, 
507
				      progress_info->bytes_total),
508
				 MIN (progress_info->total_bytes_copied,
509
				      progress_info->bytes_total));
510
		}
511
		return 1;
512 513

	case GNOME_VFS_XFER_PHASE_MOVING:
Pavel Cisler's avatar
Pavel Cisler committed
514 515
	case GNOME_VFS_XFER_PHASE_OPENSOURCE:
	case GNOME_VFS_XFER_PHASE_OPENTARGET:
516 517
		/* fall through */
	case GNOME_VFS_XFER_PHASE_COPYING:
518
		if (transfer_info->progress_dialog != NULL) {
519
			if (progress_info->bytes_copied == 0) {
520 521 522 523 524 525 526
				progress_dialog_set_to_from_item_text
					(transfer_info->progress_dialog,
					 transfer_info->progress_verb,
					 progress_info->source_name,
					 progress_info->target_name,
					 progress_info->file_index,
					 progress_info->file_size);
527
			} else {
528
				nautilus_file_operations_progress_update_sizes
529
					(transfer_info->progress_dialog,
530
					 MIN (progress_info->bytes_copied, 
531
					      progress_info->bytes_total),
532
					 MIN (progress_info->total_bytes_copied,
533
					      progress_info->bytes_total));
534
			}
Ettore Perazzoli's avatar
Ettore Perazzoli committed
535
		}
536
		return 1;
Pavel Cisler's avatar
Pavel Cisler committed
537

538
	case GNOME_VFS_XFER_PHASE_CLEANUP:
539
		if (transfer_info->progress_dialog != NULL) {
540 541
			nautilus_file_operations_progress_clear
				(transfer_info->progress_dialog);
542
			nautilus_file_operations_progress_set_operation_string
543 544
				(transfer_info->progress_dialog,
				 transfer_info->cleanup_name);
545
		}
546
		return 1;
547

Ettore Perazzoli's avatar
Ettore Perazzoli committed
548
	case GNOME_VFS_XFER_PHASE_COMPLETED:
549
		nautilus_file_changes_consume_changes (TRUE);
550
		if (transfer_info->done_callback != NULL) {
551 552 553 554
			transfer_info->done_callback (transfer_info->debuting_uris,
						      transfer_info->done_callback_data);
			/* done_callback now owns (will free) debuting_uris */
			transfer_info->debuting_uris = NULL;
555
		}
556 557

		transfer_info_destroy (transfer_info);
558
		return 1;
Pavel Cisler's avatar
Pavel Cisler committed
559

Ettore Perazzoli's avatar
Ettore Perazzoli committed
560
	default:
561
		return 1;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
562 563 564
	}
}

565 566 567 568 569 570 571 572
typedef enum {
	ERROR_READ_ONLY,
	ERROR_NOT_READABLE,
	ERROR_NOT_WRITABLE,
	ERROR_NOT_ENOUGH_PERMISSIONS,
	ERROR_NO_SPACE,
	ERROR_OTHER
} NautilusFileOperationsErrorKind;
573

574 575 576 577 578 579 580 581 582 583 584
typedef enum {
	ERROR_LOCATION_UNKNOWN,
	ERROR_LOCATION_SOURCE,
	ERROR_LOCATION_SOURCE_PARENT,
	ERROR_LOCATION_SOURCE_OR_PARENT,
	ERROR_LOCATION_TARGET
} NautilusFileOperationsErrorLocation;


static char *
build_error_string (const char *source_name, const char *target_name,
585 586 587 588
		    TransferKind operation_kind,
		    NautilusFileOperationsErrorKind error_kind,
		    NautilusFileOperationsErrorLocation error_location,
		    GnomeVFSResult error)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
589
{
590 591 592 593 594 595
	/* Avoid clever message composing here, just use brute force and
	 * duplicate the different flavors of error messages for all the
	 * possible permutations.
	 * That way localizers have an easier time and can even rearrange the
	 * order of the words in the messages easily.
	 */
Ettore Perazzoli's avatar
Ettore Perazzoli committed
596

597
	const char *error_string;
598
	char *result;
Pavel Cisler's avatar
Pavel Cisler committed
599

600 601
	error_string = NULL;
	result = NULL;
Pavel Cisler's avatar
Pavel Cisler committed
602

603
	if (error_location == ERROR_LOCATION_SOURCE_PARENT) {
604

605
		switch (operation_kind) {
606 607
		case TRANSFER_MOVE:
		case TRANSFER_MOVE_TO_TRASH:
608
			if (error_kind == ERROR_READ_ONLY) {
609
				error_string = _("Error while moving.\n\n"
610 611
						 "\"%s\" cannot be moved because it is on "
						 "a read-only disk.");
612 613
			}
			break;
614

615 616
		case TRANSFER_DELETE:
		case TRANSFER_EMPTY_TRASH:
617 618 619
			switch (error_kind) {
			case ERROR_NOT_ENOUGH_PERMISSIONS:
			case ERROR_NOT_WRITABLE:
620
				error_string = _("Error while deleting.\n\n"
621
						 "\"%s\" cannot be deleted because you do not have "
622 623 624 625
						 "permissions to modify its parent folder.");
				break;
			
			case ERROR_READ_ONLY:
626
				error_string = _("Error while deleting.\n\n"
627 628 629 630 631 632
						 "\"%s\" cannot be deleted because it is on "
						 "a read-only disk.");
				break;

			default:
				break;
633 634 635 636 637 638 639 640 641 642 643
			}
			break;

		default:
			g_assert_not_reached ();
			break;
		}
		
		if (error_string != NULL) {
			g_assert (source_name != NULL);
			result = g_strdup_printf (error_string, source_name);
644
		}
645

646
	} else if (error_location == ERROR_LOCATION_SOURCE_OR_PARENT) {
647

648 649
		g_assert (source_name != NULL);

650 651 652 653
		/* FIXME: Would be better if we could distinguish source vs parent permissions
		 * better somehow. The GnomeVFS copy engine would have to do some snooping
		 * after the failure in this case.
		 */
654
		switch (operation_kind) {
655
		case TRANSFER_MOVE:
656
			if (error_kind == ERROR_NOT_ENOUGH_PERMISSIONS) {
657
				error_string = _("Error while moving.\n\n"
658 659 660
						 "\"%s\" cannot be moved because you do not have "
						 "permissions to change it or its parent folder.");
			}
661
			break;
662
		case TRANSFER_MOVE_TO_TRASH:
663
			if (error_kind == ERROR_NOT_ENOUGH_PERMISSIONS) {
664
				error_string = _("Error while moving.\n\n"
665
						 "\"%s\" cannot be moved to the Trash because you do not have "
666 667
						 "permissions to change it or its parent folder.");
			}
668
			break;
669

670
		default:
671
			g_assert_not_reached ();
672 673
			break;
		}
674

675 676 677 678
		if (error_string != NULL) {
			g_assert (source_name != NULL);
			result = g_strdup_printf (error_string, source_name);
		}
679

680
	} else if (error_location == ERROR_LOCATION_SOURCE) {
681

682 683 684
		g_assert (source_name != NULL);

		switch (operation_kind) {
685 686
		case TRANSFER_COPY:
		case TRANSFER_DUPLICATE:
687
			if (error_kind == ERROR_NOT_READABLE) {
688
				error_string = _("Error while copying.\n\n"
689 690
						 "\"%s\" cannot be copied because you do not have "
						 "permissions to read it.");
691 692
			}
			break;
693

694 695 696
		default:
			g_assert_not_reached ();
			break;
697
		}
698

699 700 701
		if (error_string != NULL) {
			g_assert (source_name != NULL);
			result = g_strdup_printf (error_string, source_name);
702
		}
Pavel Cisler's avatar
Pavel Cisler committed
703

704 705 706 707
	} else if (error_location == ERROR_LOCATION_TARGET) {

		if (error_kind == ERROR_NO_SPACE) {
			switch (operation_kind) {
708 709
			case TRANSFER_COPY:
			case TRANSFER_DUPLICATE:
710
				error_string = _("Error while copying to \"%s\".\n\n"
711
				   		 "There is not enough space on the destination.");
712
				break;
713 714
			case TRANSFER_MOVE_TO_TRASH:
			case TRANSFER_MOVE:
715
				error_string = _("Error while moving to \"%s\".\n\n"
716
				   		 "There is not enough space on the destination.");
717
				break;
718
			case TRANSFER_LINK:
719
				error_string = _("Error while creating link in \"%s\".\n\n"
720
				   		 "There is not enough space on the destination.");
721 722 723 724 725 726 727
				break;
			default:
				g_assert_not_reached ();
				break;
			}
		} else {
			switch (operation_kind) {
728 729
			case TRANSFER_COPY:
			case TRANSFER_DUPLICATE:
730
				if (error_kind == ERROR_NOT_ENOUGH_PERMISSIONS) {
731
					error_string = _("Error while copying to \"%s\".\n\n"
732
					   		 "You do not have permissions to write to "
733
					   		 "this folder.");
734
				} else if (error_kind == ERROR_NOT_WRITABLE) {
735
					error_string = _("Error while copying to \"%s\".\n\n"
736
					   		 "The destination disk is read-only.");
737 738
				} 
				break;
739 740
			case TRANSFER_MOVE:
			case TRANSFER_MOVE_TO_TRASH:
741
				if (error_kind == ERROR_NOT_ENOUGH_PERMISSIONS) {
742
					error_string = _("Error while moving items to \"%s\".\n\n"
743
					   		 "You do not have permissions to write to "
744
					   		 "this folder.");
745
				} else if (error_kind == ERROR_NOT_WRITABLE) {
746
					error_string = _("Error while moving items to \"%s\".\n\n"
747
					   		 "The destination disk is read-only.");
748 749 750
				} 

				break;
751
			case TRANSFER_LINK:
752
				if (error_kind == ERROR_NOT_ENOUGH_PERMISSIONS) {
753
					error_string = _("Error while creating links in \"%s\".\n\n"
754
					   		 "You do not have permissions to write to "
755
					   		 "this folder.");
756
				} else if (error_kind == ERROR_NOT_WRITABLE) {
757
					error_string = _("Error while creating links in \"%s\".\n\n"
758
					   		 "The destination disk is read-only.");
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
				} 
				break;
			default:
				g_assert_not_reached ();
				break;
			}
		}
		if (error_string != NULL) {
			g_assert (target_name != NULL);
			result = g_strdup_printf (error_string, target_name);
		}
	}
	
	if (result == NULL) {
		/* None of the specific error messages apply, use a catch-all
		 * generic error
		 */
776 777 778 779 780 781
		g_message ("Please tell pavel@eazel.com that you hit case %s while doing "
			"a file operation.", gnome_vfs_result_to_string (error));

		/* FIXMEs: we need to consider a single item
		 * move/copy and not offer to continue in that case
		 */
782 783
		if (source_name != NULL) {
			switch (operation_kind) {
784 785
			case TRANSFER_COPY:
			case TRANSFER_DUPLICATE:
786
				error_string = _("Error \"%s\" while copying \"%s\".\n\n"
787 788
						 "Would you like to continue?");
				break;
789
			case TRANSFER_MOVE:
790
				error_string = _("Error \"%s\" while moving \"%s\".\n\n"
791 792
						 "Would you like to continue?");
				break;
793
			case TRANSFER_LINK:
794
				error_string = _("Error \"%s\" while creating a link to \"%s\".\n\n"
795 796
						 "Would you like to continue?");
				break;
797 798 799
			case TRANSFER_DELETE:
			case TRANSFER_EMPTY_TRASH:
			case TRANSFER_MOVE_TO_TRASH:
800
				error_string = _("Error \"%s\" while deleting \"%s\".\n\n"
801 802 803
						 "Would you like to continue?");
				break;
			default:
804
				g_assert_not_reached ();
805 806 807
				break;
			}
	
808
			result = g_strdup_printf (error_string, 
809 810
						  gnome_vfs_result_to_string (error),
						  source_name);
811
		} else {
812
			switch (operation_kind) {
813 814
			case TRANSFER_COPY:
			case TRANSFER_DUPLICATE:
815
				error_string = _("Error \"%s\" while copying.\n\n"
816 817
						 "Would you like to continue?");
				break;
818
			case TRANSFER_MOVE:
819
				error_string = _("Error \"%s\" while moving.\n\n"
820 821
						 "Would you like to continue?");
				break;
822
			case TRANSFER_LINK:
823
				error_string = _("Error \"%s\" while linking.\n\n"
824 825
						 "Would you like to continue?");
				break;
826 827 828
			case TRANSFER_DELETE:
			case TRANSFER_EMPTY_TRASH:
			case TRANSFER_MOVE_TO_TRASH:
829
				error_string = _("Error \"%s\" while deleting.\n\n"
830 831 832
						 "Would you like to continue?");
				break;
			default:
833
				g_assert_not_reached ();
834 835 836
				break;
			}
	
837
			result = g_strdup_printf (error_string, 
838
						  gnome_vfs_result_to_string (error));
839
		}
840 841 842
	}
	return result;
}
843

844
static int
845
handle_transfer_vfs_error (const GnomeVFSXferProgressInfo *progress_info,
846
			   TransferInfo *transfer_info)
847
{
848
	/* Notice that the error mode in `transfer_info' is the one we have been
849 850 851
         * requested, but the transfer is always performed in mode
         * `GNOME_VFS_XFER_ERROR_MODE_QUERY'.
         */
852

853 854 855
	int error_dialog_button_pressed;
	int error_dialog_result;
	char *text;
856 857
	char *formatted_source_name;
	char *formatted_target_name;
858 859 860 861
	const char *dialog_title;
	NautilusFileOperationsErrorKind error_kind;
	NautilusFileOperationsErrorLocation error_location;
	
862
	switch (transfer_info->error_mode) {
863 864 865 866
	case GNOME_VFS_XFER_ERROR_MODE_QUERY:

		/* transfer error, prompt the user to continue or stop */

867 868
		formatted_source_name = NULL;
		formatted_target_name = NULL;