nautilus-dnd.c 21.1 KB
Newer Older
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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* nautilus-dnd.c - Common Drag & drop handling code shared by the icon container
   and the list view.

   Copyright (C) 2000, 2001 Eazel, Inc.

   The Gnome Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Authors: Pavel Cisler <pavel@eazel.com>,
   	    Ettore Perazzoli <ettore@gnu.org>
*/

/* FIXME: This should really be back in Nautilus, not here in Eel. */

#include <config.h>
#include "nautilus-dnd.h"

32
#include "nautilus-program-choosing.h"
33
#include "nautilus-link.h"
34
#include <eel/eel-glib-extensions.h>
35
#include <eel/eel-gtk-extensions.h>
36 37 38 39 40
#include <eel/eel-string.h>
#include <eel/eel-vfs-extensions.h>
#include <gtk/gtkmain.h>
#include <gtk/gtkmenu.h>
#include <gtk/gtkseparatormenuitem.h>
41
#include <glib/gi18n.h>
42
#include <libgnomeui/gnome-uidefs.h>
Alexander Larsson's avatar
Alexander Larsson committed
43
#include <libnautilus-private/nautilus-file-utilities.h>
44 45 46 47 48 49 50
#include <stdio.h>
#include <string.h>

/* a set of defines stolen from the eel-icon-dnd.c file.
 * These are in microseconds.
 */
#define AUTOSCROLL_TIMEOUT_INTERVAL 100
51
#define AUTOSCROLL_INITIAL_DELAY 100000
52 53

/* drag this close to the view edge to start auto scroll*/
54
#define AUTO_SCROLL_MARGIN 30
55 56 57 58 59 60 61 62 63 64 65 66

/* the smallest amount of auto scroll used when we just enter the autoscroll
 * margin
 */
#define MIN_AUTOSCROLL_DELTA 5
	 
/* the largest amount of auto scroll used when we are right over the view
 * edge
 */
#define MAX_AUTOSCROLL_DELTA 50

void
67 68
nautilus_drag_init (NautilusDragInfo     *drag_info,
		    const GtkTargetEntry *drag_types,
69 70
		    int                   drag_type_count,
		    gboolean              add_text_targets)
71 72 73 74
{
	drag_info->target_list = gtk_target_list_new (drag_types,
						   drag_type_count);

75 76 77 78 79
	if (add_text_targets) {
		gtk_target_list_add_text_targets (drag_info->target_list,
						  NAUTILUS_ICON_DND_TEXT);
	}

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
	drag_info->drop_occured = FALSE;
	drag_info->need_to_destroy = FALSE;
}

void
nautilus_drag_finalize (NautilusDragInfo *drag_info)
{
	gtk_target_list_unref (drag_info->target_list);
	nautilus_drag_destroy_selection_list (drag_info->selection_list);

	g_free (drag_info);
}


/* Functions to deal with NautilusDragSelectionItems.  */

NautilusDragSelectionItem *
nautilus_drag_selection_item_new (void)
{
	return g_new0 (NautilusDragSelectionItem, 1);
}

static void
drag_selection_item_destroy (NautilusDragSelectionItem *item)
{
	g_free (item->uri);
	g_free (item);
}

void
nautilus_drag_destroy_selection_list (GList *list)
{
	GList *p;

	if (list == NULL)
		return;

	for (p = list; p != NULL; p = p->next)
		drag_selection_item_destroy (p->data);

	g_list_free (list);
}

GList *
nautilus_drag_build_selection_list (GtkSelectionData *data)
{
	GList *result;
	const guchar *p, *oldp;
	int size;

	result = NULL;
	oldp = data->data;
	size = data->length;

	while (size > 0) {
		NautilusDragSelectionItem *item;
		guint len;

		/* The list is in the form:

		   name\rx:y:width:height\r\n

		   The geometry information after the first \r is optional.  */

		/* 1: Decode name. */

		p = memchr (oldp, '\r', size);
		if (p == NULL) {
			break;
		}

		item = nautilus_drag_selection_item_new ();

		len = p - oldp;

		item->uri = g_malloc (len + 1);
		memcpy (item->uri, oldp, len);
		item->uri[len] = 0;

		p++;
		if (*p == '\n' || *p == '\0') {
			result = g_list_prepend (result, item);
			if (p == 0) {
				g_warning ("Invalid x-special/gnome-icon-list data received: "
					   "missing newline character.");
				break;
			} else {
				oldp = p + 1;
				continue;
			}
		}

		size -= p - oldp;
		oldp = p;

		/* 2: Decode geometry information.  */

		item->got_icon_position = sscanf (p, "%d:%d:%d:%d%*s",
						  &item->icon_x, &item->icon_y,
						  &item->icon_width, &item->icon_height) == 4;
		if (!item->got_icon_position) {
			g_warning ("Invalid x-special/gnome-icon-list data received: "
				   "invalid icon position specification.");
		}

		result = g_list_prepend (result, item);

		p = memchr (p, '\r', size);
		if (p == NULL || p[1] != '\n') {
			g_warning ("Invalid x-special/gnome-icon-list data received: "
				   "missing newline character.");
			if (p == NULL) {
				break;
			}
		} else {
			p += 2;
		}

		size -= p - oldp;
		oldp = p;
	}

	return result;
}


gboolean
nautilus_drag_items_local (const char *target_uri_string, const GList *selection_list)
{
	/* check if the first item on the list has target_uri_string as a parent
	 * FIXME:
	 * we should really test each item but that would be slow for large selections
	 * and currently dropped items can only be from the same container
	 */
Alexander Larsson's avatar
Alexander Larsson committed
214
	GFile *target, *item, *parent;
215 216 217 218 219 220 221
	gboolean result;

	/* must have at least one item */
	g_assert (selection_list);

	result = FALSE;

Alexander Larsson's avatar
Alexander Larsson committed
222
	target = g_file_new_for_uri (target_uri_string);
223

Alexander Larsson's avatar
Alexander Larsson committed
224 225 226 227 228 229 230 231
	/* get the parent URI of the first item in the selection */
	item = g_file_new_for_uri (((NautilusDragSelectionItem *)selection_list->data)->uri);
	parent = g_file_get_parent (item);
	g_object_unref (item);
	
	if (parent != NULL) {
		result = g_file_equal (parent, target);
		g_object_unref (parent);
Alexander Larsson's avatar
Alexander Larsson committed
232
	}
233 234 235 236 237 238 239 240 241 242 243 244
	
	return result;
}

gboolean
nautilus_drag_items_in_trash (const GList *selection_list)
{
	/* check if the first item on the list is in trash.
	 * FIXME:
	 * we should really test each item but that would be slow for large selections
	 * and currently dropped items can only be from the same container
	 */
Alexander Larsson's avatar
Alexander Larsson committed
245
	return eel_uri_is_trash (((NautilusDragSelectionItem *)selection_list->data)->uri);
246 247
}

Alexander Larsson's avatar
Alexander Larsson committed
248 249 250 251
gboolean
nautilus_drag_items_on_desktop (const GList *selection_list)
{
	char *uri;
Alexander Larsson's avatar
Alexander Larsson committed
252
	GFile *desktop, *item, *parent;
Alexander Larsson's avatar
Alexander Larsson committed
253 254 255 256 257 258 259 260 261 262 263 264
	gboolean result;
	
	/* check if the first item on the list is in trash.
	 * FIXME:
	 * we should really test each item but that would be slow for large selections
	 * and currently dropped items can only be from the same container
	 */
	uri = ((NautilusDragSelectionItem *)selection_list->data)->uri;
	if (eel_uri_is_desktop (uri)) {
		return TRUE;
	}
	
Alexander Larsson's avatar
Alexander Larsson committed
265 266 267 268 269 270 271
	desktop = nautilus_get_desktop_location ();
	
	item = g_file_new_for_uri (uri);
	parent = g_file_get_parent (item);
	g_object_unref (item);

	result = FALSE;
Alexander Larsson's avatar
Alexander Larsson committed
272
	
Alexander Larsson's avatar
Alexander Larsson committed
273 274 275 276 277
	if (parent) {
		result = g_file_equal (desktop, parent);
		g_object_unref (parent);
	}
	g_object_unref (desktop);
Alexander Larsson's avatar
Alexander Larsson committed
278 279 280 281 282
	
	return result;
	
}

283
GdkDragAction
284
nautilus_drag_default_drop_action_for_netscape_url (GdkDragContext *context)
285 286 287 288 289 290 291 292 293 294 295 296 297 298
{
	/* Mozilla defaults to copy, but unless thats the
	   only allowed thing (enforced by ctrl) we want to ASK */
	if (context->suggested_action == GDK_ACTION_COPY &&
	    context->actions != GDK_ACTION_COPY) {
		return GDK_ACTION_ASK;
	} else if (context->suggested_action == GDK_ACTION_MOVE) {
		/* Don't support move */
		return GDK_ACTION_COPY;
	}
	
	return context->suggested_action;
}

Alexander Larsson's avatar
Alexander Larsson committed
299 300 301 302 303 304 305 306
static gboolean
check_same_fs (GFile *file1, GFile *file2)
{
	GFileInfo *info1, *info2;
	const char *id1, *id2;
	gboolean res;
	
	info1 = g_file_query_info (file1,
307
				   G_FILE_ATTRIBUTE_ID_FILESYSTEM,
Alexander Larsson's avatar
Alexander Larsson committed
308 309 310 311 312 313
				   0, NULL, NULL);

	if (info1 == NULL) {
		return FALSE;
	}

314
	id1 = g_file_info_get_attribute_string (info1, G_FILE_ATTRIBUTE_ID_FILESYSTEM);
Alexander Larsson's avatar
Alexander Larsson committed
315 316 317 318 319 320
	if (id1 == NULL) {
		g_object_unref (info1);
		return FALSE;
	}
	
	info2 = g_file_query_info (file2,
321
				   G_FILE_ATTRIBUTE_ID_FILESYSTEM,
Alexander Larsson's avatar
Alexander Larsson committed
322 323 324 325 326 327
				   0, NULL, NULL);
	if (info2 == NULL) {
		g_object_unref (info1);
		return FALSE;
	}

328
	id2 = g_file_info_get_attribute_string (info2, G_FILE_ATTRIBUTE_ID_FILESYSTEM);
Alexander Larsson's avatar
Alexander Larsson committed
329 330 331 332 333 334 335 336 337 338 339 340 341
	if (id2 == NULL) {
		g_object_unref (info1);
		g_object_unref (info2);
		return FALSE;
	}

	res = strcmp (id1, id2) == 0;
	
	g_object_unref (info1);
	g_object_unref (info2);
	
	return res;
}
Alexander Larsson's avatar
Alexander Larsson committed
342

343 344
void
nautilus_drag_default_drop_action_for_icons (GdkDragContext *context,
Alexander Larsson's avatar
Alexander Larsson committed
345 346
					     const char *target_uri_string, const GList *items,
					     int *action)
347 348
{
	gboolean same_fs;
349
	gboolean target_is_source_parent;
Alexander Larsson's avatar
Alexander Larsson committed
350
	GFile *target, *dropped;
351 352 353
	GdkDragAction actions;

	if (target_uri_string == NULL) {
354
		*action = 0;
355 356 357 358 359
		return;
	}

	actions = context->actions & (GDK_ACTION_MOVE | GDK_ACTION_COPY);
	if (actions == 0) {
360 361
		 /* We can't use copy or move, just go with the suggested action. */
		*action = context->suggested_action;
362 363 364
		return;
	}

365 366 367 368 369 370
	if (context->suggested_action == GDK_ACTION_ASK) {
		/* Don't override ask */
		*action = context->suggested_action;
		return;
	}
	
371 372 373 374 375
	/*
	 * Check for trash URI.  We do a find_directory for any Trash directory.
	 * Passing 0 permissions as gnome-vfs would override the permissions
	 * passed with 700 while creating .Trash directory
	 */
376 377
	if (eel_uri_is_trash (target_uri_string)) {
		/* Only move to Trash */
378 379 380
		if (actions & GDK_ACTION_MOVE) {
			*action = GDK_ACTION_MOVE;
		}
381

382 383
		return;

Alexander Larsson's avatar
Alexander Larsson committed
384 385 386 387 388 389 390
	} else if (g_str_has_prefix (target_uri_string, NAUTILUS_COMMAND_SPECIFIER)
			|| g_str_has_prefix (target_uri_string, NAUTILUS_DESKTOP_COMMAND_SPECIFIER)) {
		if (actions & GDK_ACTION_MOVE) {
			*action = GDK_ACTION_MOVE;
		}
		return;
	} else if (eel_uri_is_desktop (target_uri_string)) {
Alexander Larsson's avatar
Alexander Larsson committed
391
		target = nautilus_get_desktop_location ();
392
	} else {
Alexander Larsson's avatar
Alexander Larsson committed
393
		target = g_file_new_for_uri (target_uri_string);
394 395 396
	}

	/* Compare the first dropped uri with the target uri for same fs match. */
Alexander Larsson's avatar
Alexander Larsson committed
397 398
	dropped = g_file_new_for_uri (((NautilusDragSelectionItem *)items->data)->uri);
	same_fs = check_same_fs (target, dropped);
399
	target_is_source_parent = FALSE;
Alexander Larsson's avatar
Alexander Larsson committed
400
	target_is_source_parent = g_file_contains_file (target, dropped);	
401
	
402
	if (same_fs || target_is_source_parent) {
403 404 405 406 407
		if (actions & GDK_ACTION_MOVE) {
			*action = GDK_ACTION_MOVE;
		} else {
			*action = context->suggested_action;
		}
408
	} else {
409
		if (actions & GDK_ACTION_COPY) {
410 411 412 413
			*action = GDK_ACTION_COPY;
		} else {
			*action = context->suggested_action;
		}
414
	}
Alexander Larsson's avatar
Alexander Larsson committed
415 416 417 418

	g_object_unref (target);
	g_object_unref (dropped);
	
419 420 421 422 423 424 425 426 427 428 429 430 431
}

/* Encode a "x-special/gnome-icon-list" selection.
   Along with the URIs of the dragged files, this encodes
   the location and size of each icon relative to the cursor.
*/
static void
add_one_gnome_icon (const char *uri, int x, int y, int w, int h, 
		    gpointer data)
{
	GString *result;

	result = (GString *) data;
432 433 434

	g_string_append_printf (result, "%s\r%d:%d:%hu:%hu\r\n",
				uri, x, y, w, h);
435 436
}

437 438 439 440
/*
 * Cf. #48423
 */
#ifdef THIS_WAS_REALLY_BROKEN
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
static gboolean
is_path_that_gnome_uri_list_extract_filenames_can_parse (const char *path)
{
	if (path == NULL || path [0] == '\0') {
		return FALSE;
	}

	/* It strips leading and trailing spaces. So it can't handle
	 * file names with leading and trailing spaces.
	 */
	if (g_ascii_isspace (path [0])) {
		return FALSE;
	}
	if (g_ascii_isspace (path [strlen (path) - 1])) {
		return FALSE;
	}

	/* # works as a comment delimiter, and \r and \n are used to
	 * separate the lines, so it can't handle file names with any
	 * of these.
	 */
	if (strchr (path, '#') != NULL
	    || strchr (path, '\r') != NULL
	    || strchr (path, '\n') != NULL) {
		return FALSE;
	}

	return TRUE;
}

/* Encode a "text/plain" selection; this is a broken URL -- just
 * "file:" with a path after it (no escaping or anything). We are
 * trying to make the old gnome_uri_list_extract_filenames function
 * happy, so this is coded to its idiosyncrasises.
 */
static void
add_one_compatible_uri (const char *uri, int x, int y, int w, int h, gpointer data)
{
	GString *result;
	char *local_path;
	
	result = (GString *) data;

	/* For URLs that do not have a file: scheme, there's no harm
	 * in passing the real URL. But for URLs that do have a file:
	 * scheme, we have to send a URL that will work with the old
	 * gnome-libs function or nothing will be able to understand
	 * it.
	 */
	if (!eel_istr_has_prefix (uri, "file:")) {
		g_string_append (result, uri);
		g_string_append (result, "\r\n");
	} else {
Alexander Larsson's avatar
Alexander Larsson committed
494
		local_path = g_filename_from_uri (uri, NULL, NULL);
495 496 497 498 499 500 501 502 503 504 505 506 507 508

		/* Check for characters that confuse the old
		 * gnome_uri_list_extract_filenames implementation, and just leave
		 * out any paths with those in them.
		 */
		if (is_path_that_gnome_uri_list_extract_filenames_can_parse (local_path)) {
			g_string_append (result, "file:");
			g_string_append (result, local_path);
			g_string_append (result, "\r\n");
		}

		g_free (local_path);
	}
}
509 510 511 512 513 514 515 516 517 518 519 520
#endif

static void
add_one_uri (const char *uri, int x, int y, int w, int h, gpointer data)
{
	GString *result;
	
	result = (GString *) data;

	g_string_append (result, uri);
	g_string_append (result, "\r\n");
}
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543

/* Common function for drag_data_get_callback calls.
 * Returns FALSE if it doesn't handle drag data */
gboolean
nautilus_drag_drag_data_get (GtkWidget *widget,
			GdkDragContext *context,
			GtkSelectionData *selection_data,
			guint info,
			guint32 time,
			gpointer container_context,
			NautilusDragEachSelectedItemIterator each_selected_item_iterator)
{
	GString *result;
		
	switch (info) {
	case NAUTILUS_ICON_DND_GNOME_ICON_LIST:
		result = g_string_new (NULL);
		(* each_selected_item_iterator) (add_one_gnome_icon, container_context, result);
		break;
		
	case NAUTILUS_ICON_DND_URI_LIST:
	case NAUTILUS_ICON_DND_TEXT:
		result = g_string_new (NULL);
544
		(* each_selected_item_iterator) (add_one_uri, container_context, result);
545
		break;
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 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
	default:
		return FALSE;
	}
	
	gtk_selection_data_set (selection_data,
				selection_data->target,
				8, result->str, result->len);

	return TRUE;
}

typedef struct
{
	GMainLoop *loop;
	GdkDragAction chosen;
} DropActionMenuData;

static void
menu_deactivate_callback (GtkWidget *menu,
			  gpointer   data)
{
	DropActionMenuData *damd;
	
	damd = data;

	if (g_main_loop_is_running (damd->loop))
		g_main_loop_quit (damd->loop);
}

static void
drop_action_activated_callback (GtkWidget  *menu_item,
				gpointer    data)
{
	DropActionMenuData *damd;
	
	damd = data;

	damd->chosen = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menu_item),
							   "action"));

	if (g_main_loop_is_running (damd->loop))
		g_main_loop_quit (damd->loop);
}

static void
append_drop_action_menu_item (GtkWidget          *menu,
			      const char         *text,
			      GdkDragAction       action,
			      gboolean            sensitive,
			      DropActionMenuData *damd)
{
	GtkWidget *menu_item;

	menu_item = gtk_menu_item_new_with_mnemonic (text);
	gtk_widget_set_sensitive (menu_item, sensitive);
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);

	g_object_set_data (G_OBJECT (menu_item),
			   "action",
			   GINT_TO_POINTER (action));
	
	g_signal_connect (menu_item, "activate",
			  G_CALLBACK (drop_action_activated_callback),
			  damd);

	gtk_widget_show (menu_item);
}

/* Pops up a menu of actions to perform on dropped files */
GdkDragAction
617 618
nautilus_drag_drop_action_ask (GtkWidget *widget,
			       GdkDragAction actions)
619 620 621 622 623 624 625 626 627
{
	GtkWidget *menu;
	GtkWidget *menu_item;
	DropActionMenuData damd;
	
	/* Create the menu and set the sensitivity of the items based on the
	 * allowed actions.
	 */
	menu = gtk_menu_new ();
628
	gtk_menu_set_screen (GTK_MENU (menu), gtk_widget_get_screen (widget));
629
	
630
	append_drop_action_menu_item (menu, _("_Move Here"),
631 632 633 634
				      GDK_ACTION_MOVE,
				      (actions & GDK_ACTION_MOVE) != 0,
				      &damd);

635
	append_drop_action_menu_item (menu, _("_Copy Here"),
636 637 638 639
				      GDK_ACTION_COPY,
				      (actions & GDK_ACTION_COPY) != 0,
				      &damd);
	
640
	append_drop_action_menu_item (menu, _("_Link Here"),
641 642 643 644
				      GDK_ACTION_LINK,
				      (actions & GDK_ACTION_LINK) != 0,
				      &damd);

645 646 647 648 649
	append_drop_action_menu_item (menu, _("Set as _Background"),
				      NAUTILUS_DND_ACTION_SET_AS_BACKGROUND,
				      (actions & NAUTILUS_DND_ACTION_SET_AS_BACKGROUND) != 0,
				      &damd);

650
	eel_gtk_menu_append_separator (GTK_MENU (menu));
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
	
	menu_item = gtk_menu_item_new_with_mnemonic (_("Cancel"));
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
	gtk_widget_show (menu_item);
	
	damd.chosen = 0;
	damd.loop = g_main_loop_new (NULL, FALSE);

	g_signal_connect (menu, "deactivate",
			  G_CALLBACK (menu_deactivate_callback),
			  &damd);
	
	gtk_grab_add (menu);
	
	gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
			NULL, NULL, 0, GDK_CURRENT_TIME);
	
	g_main_loop_run (damd.loop);

	gtk_grab_remove (menu);
	
	g_main_loop_unref (damd.loop);
	
	gtk_object_sink (GTK_OBJECT (menu));
	
	return damd.chosen;
}

679
GdkDragAction
680 681
nautilus_drag_drop_background_ask (GtkWidget *widget, 
				   GdkDragAction actions)
682 683 684 685 686 687 688 689 690
{
	GtkWidget *menu;
	GtkWidget *menu_item;
	DropActionMenuData damd;
	
	/* Create the menu and set the sensitivity of the items based on the
	 * allowed actions.
	 */
	menu = gtk_menu_new ();
691
	gtk_menu_set_screen (GTK_MENU (menu), gtk_widget_get_screen (widget));
692
	
693
	append_drop_action_menu_item (menu, _("Set as background for _all folders"),
694 695 696 697
				      NAUTILUS_DND_ACTION_SET_AS_GLOBAL_BACKGROUND,
				      (actions & NAUTILUS_DND_ACTION_SET_AS_GLOBAL_BACKGROUND) != 0,
				      &damd);

698
	append_drop_action_menu_item (menu, _("Set as background for _this folder"),
699 700
				      NAUTILUS_DND_ACTION_SET_AS_FOLDER_BACKGROUND,
				      (actions & NAUTILUS_DND_ACTION_SET_AS_FOLDER_BACKGROUND) != 0,
701 702
				      &damd);

703
	eel_gtk_menu_append_separator (GTK_MENU (menu));
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
	
	menu_item = gtk_menu_item_new_with_mnemonic (_("Cancel"));
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
	gtk_widget_show (menu_item);
	
	damd.chosen = 0;
	damd.loop = g_main_loop_new (NULL, FALSE);

	g_signal_connect (menu, "deactivate",
			  G_CALLBACK (menu_deactivate_callback),
			  &damd);
	
	gtk_grab_add (menu);
	
	gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
			NULL, NULL, 0, GDK_CURRENT_TIME);
	
	g_main_loop_run (damd.loop);

	gtk_grab_remove (menu);
	
	g_main_loop_unref (damd.loop);
	
	gtk_object_sink (GTK_OBJECT (menu));
	
	return damd.chosen;	
}

732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
gboolean
nautilus_drag_autoscroll_in_scroll_region (GtkWidget *widget)
{
	float x_scroll_delta, y_scroll_delta;

	nautilus_drag_autoscroll_calculate_delta (widget, &x_scroll_delta, &y_scroll_delta);

	return x_scroll_delta != 0 || y_scroll_delta != 0;
}


void
nautilus_drag_autoscroll_calculate_delta (GtkWidget *widget, float *x_scroll_delta, float *y_scroll_delta)
{
	int x, y;

	g_assert (GTK_IS_WIDGET (widget));

	gdk_window_get_pointer (widget->window, &x, &y, NULL);

	/* Find out if we are anywhere close to the tree view edges
	 * to see if we need to autoscroll.
	 */
	*x_scroll_delta = 0;
	*y_scroll_delta = 0;
	
	if (x < AUTO_SCROLL_MARGIN) {
		*x_scroll_delta = (float)(x - AUTO_SCROLL_MARGIN);
	}

	if (x > widget->allocation.width - AUTO_SCROLL_MARGIN) {
		if (*x_scroll_delta != 0) {
			/* Already trying to scroll because of being too close to 
			 * the top edge -- must be the window is really short,
			 * don't autoscroll.
			 */
			return;
		}
		*x_scroll_delta = (float)(x - (widget->allocation.width - AUTO_SCROLL_MARGIN));
	}

	if (y < AUTO_SCROLL_MARGIN) {
		*y_scroll_delta = (float)(y - AUTO_SCROLL_MARGIN);
	}

	if (y > widget->allocation.height - AUTO_SCROLL_MARGIN) {
		if (*y_scroll_delta != 0) {
			/* Already trying to scroll because of being too close to 
			 * the top edge -- must be the window is really narrow,
			 * don't autoscroll.
			 */
			return;
		}
		*y_scroll_delta = (float)(y - (widget->allocation.height - AUTO_SCROLL_MARGIN));
	}

	if (*x_scroll_delta == 0 && *y_scroll_delta == 0) {
		/* no work */
		return;
	}

	/* Adjust the scroll delta to the proper acceleration values depending on how far
	 * into the sroll margins we are.
	 * FIXME bugzilla.eazel.com 2486:
	 * we could use an exponential acceleration factor here for better feel
	 */
	if (*x_scroll_delta != 0) {
		*x_scroll_delta /= AUTO_SCROLL_MARGIN;
		*x_scroll_delta *= (MAX_AUTOSCROLL_DELTA - MIN_AUTOSCROLL_DELTA);
		*x_scroll_delta += MIN_AUTOSCROLL_DELTA;
	}
	
	if (*y_scroll_delta != 0) {
		*y_scroll_delta /= AUTO_SCROLL_MARGIN;
		*y_scroll_delta *= (MAX_AUTOSCROLL_DELTA - MIN_AUTOSCROLL_DELTA);
		*y_scroll_delta += MIN_AUTOSCROLL_DELTA;
	}

}



void
nautilus_drag_autoscroll_start (NautilusDragInfo *drag_info,
				GtkWidget        *widget,
				GtkFunction       callback,
				gpointer          user_data)
{
	if (nautilus_drag_autoscroll_in_scroll_region (widget)) {
		if (drag_info->auto_scroll_timeout_id == 0) {
			drag_info->waiting_to_autoscroll = TRUE;
			drag_info->start_auto_scroll_in = eel_get_system_time() 
				+ AUTOSCROLL_INITIAL_DELAY;
			
826
			drag_info->auto_scroll_timeout_id = g_timeout_add
827 828 829 830 831 832
				(AUTOSCROLL_TIMEOUT_INTERVAL,
				 callback,
			 	 user_data);
		}
	} else {
		if (drag_info->auto_scroll_timeout_id != 0) {
833
			g_source_remove (drag_info->auto_scroll_timeout_id);
834 835 836 837 838 839 840 841 842
			drag_info->auto_scroll_timeout_id = 0;
		}
	}
}

void
nautilus_drag_autoscroll_stop (NautilusDragInfo *drag_info)
{
	if (drag_info->auto_scroll_timeout_id != 0) {
843
		g_source_remove (drag_info->auto_scroll_timeout_id);
844 845 846
		drag_info->auto_scroll_timeout_id = 0;
	}
}
847 848 849 850 851

gboolean
nautilus_drag_selection_includes_special_link (GList *selection_list)
{
	GList *node;
Alexander Larsson's avatar
Alexander Larsson committed
852
	char *uri;
853 854 855 856

	for (node = selection_list; node != NULL; node = node->next) {
		uri = ((NautilusDragSelectionItem *) node->data)->uri;

Alexander Larsson's avatar
Alexander Larsson committed
857 858
		if (eel_uri_is_desktop (uri)) {
			return TRUE;
859 860 861
		}
	}
	
Alexander Larsson's avatar
Alexander Larsson committed
862
	return FALSE;
863
}