r3777: Typo (spotted by Arnaud Calvo).
[rox-filer.git] / ROX-Filer / src / dnd.c
blob185789d4fd91ec49ba2c1d7af66fdf774488f1a0
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2005, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* dnd.c - code for handling drag and drop */
24 #include "config.h"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <sys/param.h>
33 #include <X11/Xlib.h>
34 #include <X11/Xatom.h>
35 #include <gtk/gtk.h>
36 #include <gdk/gdkx.h>
38 #include "global.h"
40 #include "view_iface.h"
41 #include "dnd.h"
42 #include "type.h"
43 #include "filer.h"
44 #include "action.h"
45 #include "pixmaps.h"
46 #include "gui_support.h"
47 #include "support.h"
48 #include "options.h"
49 #include "run.h"
50 #include "pinboard.h"
51 #include "dir.h"
52 #include "diritem.h"
53 #include "usericons.h"
54 #include "menu.h"
55 #include "bookmarks.h"
57 #define MAXURILEN 4096 /* Longest URI to allow */
59 gint drag_start_x, drag_start_y;
60 MotionType motion_state = MOTION_NONE;
62 static GList *prompt_local_paths = NULL;
63 static gchar *prompt_dest_path = NULL;
65 /* This keeps track of how many mouse buttons are currently down.
66 * We add a grab when it does 0->1 and release it on 1<-0.
68 * It may also be set to zero to disable the motion system (eg,
69 * when popping up a menu).
71 gint motion_buttons_pressed = 0;
73 /* Static prototypes */
74 static void set_xds_prop(GdkDragContext *context, const char *text);
75 static void desktop_drag_data_received(GtkWidget *widget,
76 GdkDragContext *context,
77 gint x,
78 gint y,
79 GtkSelectionData *selection_data,
80 guint info,
81 guint32 time,
82 FilerWindow *filer_window);
83 static void got_data_xds_reply(GtkWidget *widget,
84 GdkDragContext *context,
85 GtkSelectionData *selection_data,
86 guint32 time);
87 static void got_data_raw(GtkWidget *widget,
88 GdkDragContext *context,
89 GtkSelectionData *selection_data,
90 guint32 time);
91 static void got_uri_list(GtkWidget *widget,
92 GdkDragContext *context,
93 const char *selection_data,
94 guint32 time);
95 static gboolean drag_drop(GtkWidget *widget,
96 GdkDragContext *context,
97 gint x,
98 gint y,
99 guint time,
100 gpointer data);
101 static void drag_data_received(GtkWidget *widget,
102 GdkDragContext *context,
103 gint x,
104 gint y,
105 GtkSelectionData *selection_data,
106 guint info,
107 guint32 time,
108 gpointer user_data);
109 static gboolean spring_now(gpointer data);
110 static void spring_win_destroyed(GtkWidget *widget, gpointer data);
111 static void menuitem_response(gpointer data, guint action, GtkWidget *widget);
112 static void prompt_action(GList *paths, gchar *dest);
114 typedef enum {
115 MENU_COPY,
116 MENU_MOVE,
117 MENU_LINK,
118 } MenuActionType;
120 #undef N_
121 #define N_(x) x
122 static GtkItemFactoryEntry menu_def[] = {
123 {N_("Copy"), NULL, menuitem_response, MENU_COPY, NULL},
124 {N_("Move"), NULL, menuitem_response, MENU_MOVE, NULL},
125 {N_("Link"), NULL, menuitem_response, MENU_LINK, NULL},
127 static GtkWidget *dnd_menu = NULL;
129 /* Possible values for drop_dest_type (can also be NULL).
130 * In either case, drop_dest_path is the app/file/dir to use.
132 const char *drop_dest_prog = "drop_dest_prog"; /* Run a program */
133 const char *drop_dest_dir = "drop_dest_dir"; /* Save to path */
134 const char *drop_dest_pass_through = "drop_dest_pass"; /* Pass to parent */
135 const char *drop_dest_bookmark = "drop_dest_bookmark"; /* Add to bookmarks */
137 GdkAtom XdndDirectSave0;
138 GdkAtom xa_text_plain;
139 GdkAtom text_uri_list;
140 GdkAtom text_x_moz_url;
141 GdkAtom application_octet_stream;
142 GdkAtom xa_string; /* Not actually used for DnD, but the others are here! */
144 int spring_in_progress = 0; /* Non-zero changes filer_opendir slightly */
146 Option o_dnd_drag_to_icons;
147 Option o_dnd_spring_open;
148 static Option o_dnd_spring_delay;
149 static Option o_dnd_middle_menu;
150 Option o_dnd_left_menu;
151 static Option o_dnd_uri_handler;
153 void dnd_init(void)
155 XdndDirectSave0 = gdk_atom_intern("XdndDirectSave0", FALSE);
156 xa_text_plain = gdk_atom_intern("text/plain", FALSE);
157 text_uri_list = gdk_atom_intern("text/uri-list", FALSE);
158 text_x_moz_url = gdk_atom_intern("text/x-moz-url", FALSE);
159 application_octet_stream = gdk_atom_intern("application/octet-stream",
160 FALSE);
161 xa_string = gdk_atom_intern("STRING", FALSE);
163 option_add_int(&o_dnd_drag_to_icons, "dnd_drag_to_icons", 1);
164 option_add_int(&o_dnd_spring_open, "dnd_spring_open", 0);
165 option_add_int(&o_dnd_spring_delay, "dnd_spring_delay", 400);
166 option_add_int(&o_dnd_left_menu, "dnd_left_menu", TRUE);
167 option_add_int(&o_dnd_middle_menu, "dnd_middle_menu", TRUE);
169 option_add_string(&o_dnd_uri_handler, "dnd_uri_handler",
170 "xterm -e wget $1");
173 /* SUPPORT FUNCTIONS */
175 /* Set the XdndDirectSave0 property on the source window for this context */
176 static void set_xds_prop(GdkDragContext *context, const char *text)
178 gdk_property_change(context->source_window,
179 XdndDirectSave0,
180 xa_text_plain, 8,
181 GDK_PROP_MODE_REPLACE,
182 text,
183 strlen(text));
186 static char *get_xds_prop(GdkDragContext *context)
188 guchar *prop_text;
189 gint length;
191 if (gdk_property_get(context->source_window,
192 XdndDirectSave0,
193 xa_text_plain,
194 0, MAXURILEN,
195 FALSE,
196 NULL, NULL,
197 &length, &prop_text) && prop_text)
199 /* Terminate the string */
200 prop_text = g_realloc(prop_text, length + 1);
201 prop_text[length] = '\0';
202 /* Note: assuming UTF-8 (should convert here) */
203 return prop_text;
206 return NULL;
209 /* Is the sender willing to supply this target type? */
210 gboolean provides(GdkDragContext *context, GdkAtom target)
212 GList *targets = context->targets;
214 while (targets && ((GdkAtom) targets->data != target))
215 targets = targets->next;
217 return targets != NULL;
220 /* DRAGGING FROM US */
222 /* The user has held the mouse button down over a group of item and moved -
223 * start a drag. 'uri_list' is copied, so you can delete it straight away.
225 void drag_selection(GtkWidget *widget, GdkEventMotion *event, guchar *uri_list)
227 GdkPixbuf *pixbuf;
228 GdkDragContext *context;
229 GdkDragAction actions;
230 GtkTargetList *target_list;
231 GtkTargetEntry target_table[] = {
232 {"text/uri-list", 0, TARGET_URI_LIST},
233 {"UTF8_STRING", 0, TARGET_UTF8},
236 if (event->state & GDK_BUTTON1_MASK)
237 actions = GDK_ACTION_COPY | GDK_ACTION_MOVE
238 | GDK_ACTION_LINK | GDK_ACTION_ASK;
239 else
241 if (o_dnd_middle_menu.int_value)
242 actions = GDK_ACTION_ASK;
243 else
244 actions = GDK_ACTION_MOVE;
247 target_list = gtk_target_list_new(target_table,
248 G_N_ELEMENTS(target_table));
250 context = gtk_drag_begin(widget,
251 target_list,
252 actions,
253 (event->state & GDK_BUTTON1_MASK) ? 1 :
254 (event->state & GDK_BUTTON2_MASK) ? 2 : 3,
255 (GdkEvent *) event);
257 g_dataset_set_data_full(context, "uri_list",
258 g_strdup(uri_list), g_free);
260 pixbuf = gtk_widget_render_icon(widget, GTK_STOCK_DND_MULTIPLE,
261 GTK_ICON_SIZE_DIALOG, NULL);
262 gtk_drag_set_icon_pixbuf(context, pixbuf, 0, 0);
263 g_object_unref(pixbuf);
266 /* Copy/Load this item into another directory/application */
267 void drag_one_item(GtkWidget *widget,
268 GdkEventMotion *event,
269 const guchar *full_path,
270 DirItem *item,
271 MaskedPixmap *image)
273 guchar *uri, *tmp;
274 GdkDragContext *context;
275 GdkDragAction actions;
276 GtkTargetList *target_list;
277 GtkTargetEntry target_table[] = {
278 {"text/uri-list", 0, TARGET_URI_LIST},
279 {"UTF8_STRING", 0, TARGET_UTF8},
280 {"application/octet-stream", 0, TARGET_RAW},
281 {"", 0, TARGET_RAW},
284 g_return_if_fail(full_path != NULL);
285 g_return_if_fail(item != NULL);
287 if (!image)
288 image = di_image(item);
290 if (item->base_type == TYPE_FILE)
292 MIME_type *t = item->mime_type;
294 target_table[3].target = g_strconcat(t->media_type, "/",
295 t->subtype, NULL);
296 target_list = gtk_target_list_new(target_table,
297 G_N_ELEMENTS(target_table));
298 g_free(target_table[3].target);
300 else
301 target_list = gtk_target_list_new(target_table, 2);
303 if (event->state & GDK_BUTTON1_MASK)
304 actions = GDK_ACTION_COPY | GDK_ACTION_ASK
305 | GDK_ACTION_MOVE | GDK_ACTION_LINK;
306 else
308 if (o_dnd_middle_menu.int_value)
309 actions = GDK_ACTION_ASK;
310 else
311 actions = GDK_ACTION_MOVE;
314 context = gtk_drag_begin(widget,
315 target_list,
316 actions,
317 (event->state & GDK_BUTTON1_MASK) ? 1 :
318 (event->state & GDK_BUTTON2_MASK) ? 2 : 3,
319 (GdkEvent *) event);
321 g_dataset_set_data_full(context, "full_path",
322 g_strdup(full_path), g_free);
323 tmp = (char *) encode_path_as_uri(full_path);
324 uri = g_strconcat(tmp, "\r\n", NULL);
325 /*printf("%s\n", tmp);*/
326 g_free(tmp);
327 g_dataset_set_data_full(context, "uri_list", uri, g_free);
329 g_return_if_fail(image != NULL);
331 gtk_drag_set_icon_pixbuf(context, image->pixbuf, 0, 0);
334 /* Convert text/uri-list data to UTF8_STRING.
335 * g_free() the result.
337 static gchar *uri_list_to_utf8(const char *uri_list)
339 GString *new;
340 GList *uris, *next_uri;
341 char *string;
343 new = g_string_new(NULL);
345 uris = uri_list_to_glist(uri_list);
347 for (next_uri = uris; next_uri; next_uri = next_uri->next)
349 EscapedPath *uri = next_uri->data;
350 char *local;
352 local = get_local_path(uri);
354 if (new->len)
355 g_string_append_c(new, ' ');
357 if (local)
359 g_string_append(new, local);
360 g_free(local);
362 else
363 g_warning("Not local!\n");
365 g_free(uri);
368 if (uris)
369 g_list_free(uris);
371 string = new->str;
372 g_string_free(new, FALSE);
374 return string;
377 /* Called when a remote app wants us to send it some data.
378 * TODO: Maybe we should handle errors better (ie, let the remote app know
379 * the drag has failed)?
381 void drag_data_get(GtkWidget *widget,
382 GdkDragContext *context,
383 GtkSelectionData *selection_data,
384 guint info,
385 guint32 time,
386 gpointer data)
388 char *to_send = "E"; /* Default to sending an error */
389 long to_send_length = 1;
390 gboolean delete_once_sent = FALSE;
391 GdkAtom type;
392 guchar *path;
394 type = selection_data->target;
396 switch (info)
398 case TARGET_RAW:
399 path = g_dataset_get_data(context, "full_path");
400 if (path && load_file(path, &to_send, &to_send_length))
402 delete_once_sent = TRUE;
403 break;
405 g_warning("drag_data_get: Can't find path!\n");
406 return;
407 case TARGET_UTF8:
409 char *uri_list;
410 uri_list = g_dataset_get_data(context, "uri_list");
411 to_send = uri_list_to_utf8(uri_list);
412 to_send_length = strlen(to_send);
413 delete_once_sent = TRUE;
414 break;
416 case TARGET_URI_LIST:
417 to_send = g_dataset_get_data(context, "uri_list");
418 to_send_length = strlen(to_send);
419 type = text_uri_list; /* (needed for xine) */
420 delete_once_sent = FALSE;
421 break;
422 default:
423 delayed_error("drag_data_get: %s",
424 _("Internal error - bad info type"));
425 break;
428 gtk_selection_data_set(selection_data,
429 type,
431 to_send,
432 to_send_length);
434 if (delete_once_sent)
435 g_free(to_send);
438 /* DRAGGING TO US */
440 /* Set up this widget as a drop-target.
441 * Does not attach any motion handlers.
443 void make_drop_target(GtkWidget *widget, GtkDestDefaults defaults)
445 GtkTargetEntry target_table[] =
447 {"text/uri-list", 0, TARGET_URI_LIST},
448 {"text/x-moz-url", 0, TARGET_MOZ_URL},
449 {"XdndDirectSave0", 0, TARGET_XDS},
450 {"application/octet-stream", 0, TARGET_RAW},
453 gtk_drag_dest_set(widget,
454 defaults,
455 target_table,
456 sizeof(target_table) / sizeof(*target_table),
457 GDK_ACTION_COPY | GDK_ACTION_ASK | GDK_ACTION_MOVE
458 | GDK_ACTION_LINK | GDK_ACTION_PRIVATE);
460 g_signal_connect(widget, "drag_drop", G_CALLBACK(drag_drop), NULL);
461 g_signal_connect(widget, "drag_data_received",
462 G_CALLBACK(drag_data_received), NULL);
465 /* Like drag_set_dest, but for a pinboard-type widget */
466 void drag_set_pinboard_dest(GtkWidget *widget)
468 GtkTargetEntry target_table[] = {
469 {"text/uri-list", 0, TARGET_URI_LIST},
472 gtk_drag_dest_set(widget,
473 GTK_DEST_DEFAULT_DROP,
474 target_table,
475 sizeof(target_table) / sizeof(*target_table),
476 GDK_ACTION_LINK);
477 g_signal_connect(widget, "drag_data_received",
478 G_CALLBACK(desktop_drag_data_received), NULL);
481 /* item is the item the file is held over, NULL for directory background.
482 * 'item' may be NULL on exit if the drop should be treated as onto the
483 * background. Disallow drags to a selected icon before calling this.
485 * Returns NULL to reject the drop, or drop_dest_prog/drop_dest_dir to
486 * accept. Build the path based on item.
488 const guchar *dnd_motion_item(GdkDragContext *context, DirItem **item_p)
490 DirItem *item = *item_p;
492 if (item)
494 /* If we didn't drop onto a directory, application or
495 * executable file then act as though the drop is to the
496 * window background.
498 if (item->base_type != TYPE_DIRECTORY
499 && !(item->mime_type == application_executable))
501 item = NULL;
502 *item_p = NULL;
506 if (!item)
508 /* Drop onto the window background */
510 return drop_dest_dir;
513 /* Drop onto a program/directory of some sort */
515 if (item->base_type == TYPE_DIRECTORY &&
516 !(item->flags & ITEM_FLAG_APPDIR))
518 /* A normal directory */
519 if (provides(context, text_uri_list) ||
520 provides(context, text_x_moz_url) ||
521 provides(context, XdndDirectSave0))
522 return drop_dest_dir;
524 else
526 if (provides(context, text_uri_list) ||
527 provides(context, text_x_moz_url) ||
528 provides(context, application_octet_stream))
529 return drop_dest_prog;
532 return NULL;
535 /* User has tried to drop some data on us. Decide what format we would
536 * like the data in.
538 static gboolean drag_drop(GtkWidget *widget,
539 GdkDragContext *context,
540 gint x,
541 gint y,
542 guint time,
543 gpointer data)
545 const char *error = NULL;
546 char *leafname = NULL;
547 GdkAtom target = GDK_NONE;
548 char *dest_path;
549 char *dest_type = NULL;
551 dest_path = g_dataset_get_data(context, "drop_dest_path");
552 dest_type = g_dataset_get_data(context, "drop_dest_type");
554 if (dest_type == drop_dest_pass_through)
555 return FALSE; /* Let the parent widget handle it */
557 if (dest_type == drop_dest_bookmark)
559 if (provides(context, text_uri_list))
560 gtk_drag_get_data(widget, context, text_uri_list, time);
561 else
563 gtk_drag_finish(context, FALSE, FALSE, time);
564 delayed_error(_("Drag a directory here to "
565 "bookmark it."));
567 return TRUE;
570 g_return_val_if_fail(dest_path != NULL, TRUE);
572 if (dest_type == drop_dest_dir && provides(context, XdndDirectSave0))
574 leafname = get_xds_prop(context);
575 if (leafname)
577 if (strchr(leafname, '/'))
579 error = _("XDS protocol error: "
580 "leafname may not contain '/'\n");
581 null_g_free(&leafname);
583 else
585 char *dest_uri;
587 /* Not escaped. */
588 dest_uri = g_strconcat("file://",
589 our_host_name_for_dnd(),
590 dest_path, NULL);
592 set_xds_prop(context,
593 make_path(dest_uri, leafname));
595 g_free(dest_uri);
597 target = XdndDirectSave0;
598 g_dataset_set_data_full(context, "leafname",
599 leafname, g_free);
602 else
603 error = _(
604 "XdndDirectSave0 target provided, but the atom "
605 "XdndDirectSave0 (type text/plain) did not "
606 "contain a leafname\n");
608 else if (provides(context, text_uri_list))
609 target = text_uri_list;
610 else if (provides(context, text_x_moz_url))
611 target = text_x_moz_url;
612 else if (provides(context, application_octet_stream))
613 target = application_octet_stream;
614 else
616 if (dest_type == drop_dest_dir)
617 error = _("Sorry - I require a target type of "
618 "text/uri-list or XdndDirectSave0.");
619 else
620 error = _("Sorry - I require a target type of "
621 "text/uri-list or application/octet-stream.");
624 if (error)
626 gtk_drag_finish(context, FALSE, FALSE, time); /* Failure */
628 delayed_error("%s", error);
630 else
631 gtk_drag_get_data(widget, context, target, time);
633 return TRUE;
636 /* Called when a text/uri-list arrives */
637 static void desktop_drag_data_received(GtkWidget *widget,
638 GdkDragContext *context,
639 gint x,
640 gint y,
641 GtkSelectionData *selection_data,
642 guint info,
643 guint32 time,
644 FilerWindow *filer_window)
646 GList *uris, *next;
647 char *error_example = NULL;
648 gint dx, dy;
650 if (!selection_data->data)
652 /* Timeout? */
653 return;
656 if (pinboard_drag_in_progress)
658 pinboard_move_icons();
659 return;
662 gdk_window_get_position(widget->window, &dx, &dy);
663 x += dx;
664 y += dy;
666 uris = uri_list_to_glist(selection_data->data);
668 for (next = uris; next; next = next->next)
670 guchar *path;
672 path = get_local_path((EscapedPath *) next->data);
673 if (path)
675 pinboard_pin(path, NULL, x, y, NULL);
676 x += 64;
677 g_free(path);
679 else if (!error_example)
680 error_example = g_strdup(next->data);
682 g_free(next->data);
685 if (uris)
686 g_list_free(uris);
688 if (error_example)
690 delayed_error(_("Failed to add some items to the pinboard, "
691 "because they are on a remote machine. For example:\n"
692 "\n%s"), error_example);
693 g_free(error_example);
697 /* Convert Mozilla's text/x-moz-uri into a text/uri-list */
698 static void got_moz_uri(GtkWidget *widget,
699 GdkDragContext *context,
700 GtkSelectionData *selection_data,
701 guint32 time)
703 gchar *utf8, *uri_list, *eol;
705 utf8 = g_utf16_to_utf8((gunichar2 *) selection_data->data,
706 (glong) selection_data->length,
707 NULL, NULL, NULL);
709 eol = utf8 ? strchr(utf8, '\n') : NULL;
710 if (!eol)
712 delayed_error("Invalid UTF16 from text/x-moz-url target");
713 g_free(utf8);
714 gtk_drag_finish(context, FALSE, FALSE, time);
715 return;
718 *eol = '\0';
719 uri_list = g_strconcat(utf8, "\r\n", NULL);
720 g_free(utf8);
722 got_uri_list(widget, context, uri_list, time);
724 g_free(uri_list);
727 /* Called when some data arrives from the remote app (which we asked for
728 * in drag_drop).
730 static void drag_data_received(GtkWidget *widget,
731 GdkDragContext *context,
732 gint x,
733 gint y,
734 GtkSelectionData *selection_data,
735 guint info,
736 guint32 time,
737 gpointer user_data)
739 if (!selection_data->data)
741 /* Timeout? */
742 gtk_drag_finish(context, FALSE, FALSE, time); /* Failure */
743 return;
746 switch (info)
748 case TARGET_XDS:
749 got_data_xds_reply(widget, context,
750 selection_data, time);
751 break;
752 case TARGET_RAW:
753 got_data_raw(widget, context, selection_data, time);
754 break;
755 case TARGET_URI_LIST:
756 got_uri_list(widget, context, selection_data->data,
757 time);
758 break;
759 case TARGET_MOZ_URL:
760 got_moz_uri(widget, context, selection_data, time);
761 break;
762 default:
763 gtk_drag_finish(context, FALSE, FALSE, time);
764 delayed_error("drag_data_received: %s",
765 _("Unknown target"));
766 break;
770 static void got_data_xds_reply(GtkWidget *widget,
771 GdkDragContext *context,
772 GtkSelectionData *selection_data,
773 guint32 time)
775 gboolean mark_unsafe = TRUE;
776 char response = *selection_data->data;
777 const char *error = NULL;
778 char *dest_path;
780 dest_path = g_dataset_get_data(context, "drop_dest_path");
782 if (selection_data->length != 1)
783 response = '?';
785 if (response == 'F')
787 /* Sender couldn't save there - ask for another
788 * type if possible.
790 if (provides(context, application_octet_stream))
792 mark_unsafe = FALSE; /* Wait and see */
794 gtk_drag_get_data(widget, context,
795 application_octet_stream, time);
797 else
798 error = _("Remote app can't or won't send me "
799 "the data - sorry");
801 else if (response == 'S')
803 /* Success - data is saved */
804 mark_unsafe = FALSE; /* It really is safe */
805 gtk_drag_finish(context, TRUE, FALSE, time);
807 refresh_dirs(dest_path);
809 else if (response != 'E')
811 error = _("XDS protocol error: "
812 "return code should be 'S', 'F' or 'E'\n");
814 /* else: error has been reported by the sender */
816 if (mark_unsafe)
818 set_xds_prop(context, "");
819 /* Unsave also implies that the drag failed */
820 gtk_drag_finish(context, FALSE, FALSE, time);
823 if (error)
824 delayed_error("%s", error);
827 static void got_data_raw(GtkWidget *widget,
828 GdkDragContext *context,
829 GtkSelectionData *selection_data,
830 guint32 time)
832 const char *leafname;
833 int fd;
834 const char *error = NULL;
835 const char *dest_path;
837 g_return_if_fail(selection_data->data != NULL);
839 dest_path = g_dataset_get_data(context, "drop_dest_path");
841 if (context->action == GDK_ACTION_ASK)
843 gtk_drag_finish(context, FALSE, FALSE, time); /* Failure */
844 delayed_error(_("Sorry, can't display a menu of actions "
845 "for a remote file / raw data."));
846 return;
849 if (g_dataset_get_data(context, "drop_dest_type") == drop_dest_prog)
851 /* The data needs to be sent to an application */
852 run_with_data(dest_path,
853 selection_data->data, selection_data->length);
854 gtk_drag_finish(context, TRUE, FALSE, time); /* Success! */
855 return;
858 leafname = g_dataset_get_data(context, "leafname");
859 if (!leafname)
860 leafname = _("UntitledData");
862 fd = open(make_path(dest_path, leafname),
863 O_WRONLY | O_CREAT | O_EXCL | O_NOCTTY,
864 S_IRUSR | S_IRGRP | S_IROTH |
865 S_IWUSR | S_IWGRP | S_IWOTH);
867 if (fd == -1)
868 error = g_strerror(errno);
869 else
871 if (write(fd,
872 selection_data->data,
873 selection_data->length) == -1)
874 error = g_strerror(errno);
876 if (close(fd) == -1 && !error)
877 error = g_strerror(errno);
879 refresh_dirs(dest_path);
882 if (error)
884 if (provides(context, XdndDirectSave0))
885 set_xds_prop(context, "");
886 gtk_drag_finish(context, FALSE, FALSE, time); /* Failure */
887 delayed_error(_("Error saving file: %s"), error);
889 else
890 gtk_drag_finish(context, TRUE, FALSE, time); /* Success! */
893 static gboolean uri_is_local(const EscapedPath *uri)
895 char *path;
896 path = get_local_path(uri);
897 if (!path)
898 return FALSE;
899 g_free(path);
900 return TRUE;
903 /* Run the shell command 'command', replacing $1 with 'arg' */
904 static void run_with_argument(const char *dir,
905 const char *command,
906 const char *arg)
908 GPtrArray *argv;
910 argv = g_ptr_array_new();
912 g_ptr_array_add(argv, "sh");
913 g_ptr_array_add(argv, "-c");
914 g_ptr_array_add(argv, (char *) command);
915 g_ptr_array_add(argv, "sh");
916 g_ptr_array_add(argv, (char *) arg);
917 g_ptr_array_add(argv, NULL);
919 rox_spawn(dir, (const gchar **) argv->pdata);
921 g_ptr_array_free(argv, TRUE);
924 /* We've got a list of URIs from somewhere (probably another filer window).
925 * If the files are on the local machine then try to copy them ourselves,
926 * otherwise, if there was only one file and application/octet-stream was
927 * provided, get the data via the X server.
928 * For http:, https: or ftp: schemes, use the download handler.
930 static void got_uri_list(GtkWidget *widget,
931 GdkDragContext *context,
932 const char *selection_data,
933 guint32 time)
935 GList *uri_list;
936 const char *error = NULL;
937 GList *next_uri;
938 gboolean send_reply = TRUE;
939 char *dest_path;
940 char *type;
942 dest_path = g_dataset_get_data(context, "drop_dest_path");
943 type = g_dataset_get_data(context, "drop_dest_type");
945 uri_list = uri_list_to_glist(selection_data);
947 if (type == drop_dest_bookmark)
949 GList *next;
950 for (next = uri_list; next; next = next->next)
951 bookmarks_add_uri((EscapedPath *) next->data);
952 destroy_glist(&uri_list);
953 gtk_drag_finish(context, TRUE, FALSE, time); /* Success! */
954 return;
957 g_return_if_fail(dest_path != NULL);
959 if (!uri_list)
960 error = _("No URIs in the text/uri-list (nothing to do!)");
961 else if (context->action != GDK_ACTION_ASK && type == drop_dest_prog)
962 run_with_files(dest_path, uri_list);
963 else if ((!uri_list->next) && !uri_is_local(uri_list->data))
965 /* There is one URI in the list, and it's not on the local
966 * machine. Get it via the X server if possible.
969 if (provides(context, application_octet_stream))
971 char *leaf;
972 leaf = strrchr(uri_list->data, '/');
973 if (leaf)
974 leaf++;
975 else
976 leaf = uri_list->data;
977 g_dataset_set_data_full(context, "leafname",
978 unescape_uri((EscapedPath *) leaf), g_free);
979 gtk_drag_get_data(widget, context,
980 application_octet_stream, time);
981 send_reply = FALSE;
983 else if ((strncasecmp(uri_list->data, "http:", 5) == 0) ||
984 (strncasecmp(uri_list->data, "https:", 6) == 0) ||
985 (strncasecmp(uri_list->data, "ftp:", 4) == 0))
987 run_with_argument(dest_path,
988 o_dnd_uri_handler.value,
989 (char *) uri_list->data);
991 else
992 error = _("Can't get data from remote machine "
993 "(application/octet-stream not provided)");
995 else
997 GList *local_paths = NULL;
999 /* Either one local URI, or a list. If everything in the list
1000 * isn't local then we are stuck.
1003 for (next_uri = uri_list; next_uri; next_uri = next_uri->next)
1005 char *path;
1007 path = get_local_path((EscapedPath *) next_uri->data);
1008 /*printf("%s -> %s\n", (char *) next_uri->data,
1009 path? path: "NULL");*/
1011 if (path)
1012 local_paths = g_list_append(local_paths,
1013 path);
1014 else
1015 error = _("Some of these files are on a "
1016 "different machine - they will be "
1017 "ignored - sorry");
1020 if (!local_paths)
1022 error = _("None of these files are on the local "
1023 "machine - I can't operate on multiple "
1024 "remote files - sorry.");
1026 else if (context->action == GDK_ACTION_ASK)
1027 prompt_action(local_paths, dest_path);
1028 else if (context->action == GDK_ACTION_MOVE)
1029 action_move(local_paths, dest_path, NULL, -1);
1030 else if (context->action == GDK_ACTION_COPY)
1031 action_copy(local_paths, dest_path, NULL, -1);
1032 else if (context->action == GDK_ACTION_LINK)
1033 action_link(local_paths, dest_path, NULL);
1034 else
1035 error = _("Unknown action requested");
1037 destroy_glist(&local_paths);
1040 if (error)
1042 gtk_drag_finish(context, FALSE, FALSE, time); /* Failure */
1043 delayed_error(_("Error getting file list: %s"), error);
1045 else if (send_reply)
1046 gtk_drag_finish(context, TRUE, FALSE, time); /* Success! */
1048 destroy_glist(&uri_list);
1051 /* Called when an item from the ACTION_ASK menu is chosen */
1052 static void menuitem_response(gpointer data, guint action, GtkWidget *widget)
1054 if (action == MENU_MOVE)
1055 action_move(prompt_local_paths, prompt_dest_path, NULL, -1);
1056 else if (action == MENU_COPY)
1057 action_copy(prompt_local_paths, prompt_dest_path, NULL, -1);
1058 else if (action == MENU_LINK)
1059 action_link(prompt_local_paths, prompt_dest_path, NULL);
1062 /* When some local files are dropped somewhere with ACTION_ASK, this
1063 * function is called to display the menu.
1065 static void prompt_action(GList *paths, gchar *dest)
1067 GList *next;
1069 if (prompt_local_paths)
1071 destroy_glist(&prompt_local_paths);
1072 null_g_free(&prompt_dest_path);
1075 /* Make a copy of the arguments */
1076 for (next = paths; next; next = next->next)
1077 prompt_local_paths = g_list_append(prompt_local_paths,
1078 g_strdup((gchar *) next->data));
1079 prompt_dest_path = g_strdup(dest);
1081 if (!dnd_menu)
1083 GtkItemFactory *item_factory;
1085 item_factory = menu_create(menu_def,
1086 sizeof(menu_def) / sizeof(*menu_def),
1087 "<dnd>", NULL);
1088 dnd_menu = gtk_item_factory_get_widget(item_factory, "<dnd>");
1091 /* Shade 'Set Icon' if there are multiple files */
1092 menu_set_items_shaded(dnd_menu, g_list_length(paths) != 1, 4, 1);
1094 show_popup_menu(dnd_menu, gtk_get_current_event(), 1);
1098 /* SPRING-LOADING */
1100 /* This is the code that makes directories pop open if you hold a
1101 * file over them...
1103 * First, call dnd_spring_load(context) to arm the system.
1104 * After a timeout (1/2 a second) the dest_path directory will be
1105 * opened in a new window, unless dnd_spring_abort is called first.
1108 static gint spring_timeout = -1;
1109 static GdkDragContext *spring_context = NULL;
1110 static FilerWindow *spring_window = NULL;
1111 static FilerWindow *spring_src_window = NULL;
1113 void dnd_spring_load(GdkDragContext *context, FilerWindow *src_win)
1115 g_return_if_fail(context != NULL);
1117 if (!o_dnd_spring_open.int_value)
1118 return;
1120 if (spring_context)
1121 dnd_spring_abort();
1123 spring_context = context;
1124 g_object_ref(spring_context);
1125 spring_src_window = src_win;
1126 spring_timeout = gtk_timeout_add(
1127 o_dnd_spring_delay.int_value, spring_now, NULL);
1130 void dnd_spring_abort(void)
1132 if (!spring_context)
1133 return;
1135 g_object_unref(spring_context);
1136 spring_context = NULL;
1137 gtk_timeout_remove(spring_timeout);
1140 /* If all mod keys are released, no buttons are pressed, and the
1141 * mouse is outside the spring window, then close it.
1143 static gboolean spring_check_idle(gpointer data)
1145 int p_x, p_y;
1147 if (!spring_window)
1148 return FALSE;
1150 if (!get_pointer_xy(&p_x, &p_y))
1153 GdkWindow *win = spring_window->window->window;
1154 int x, y;
1155 int w, h;
1157 gdk_window_get_position(win, &x, &y);
1158 gdk_window_get_size(win, &w, &h);
1160 if (p_x < x || p_x > x + w || p_y < y || p_y > y + h)
1164 gtk_widget_destroy(spring_window->window);
1165 return FALSE; /* Got it! */
1168 return TRUE; /* Try again later */
1171 static gboolean spring_now(gpointer data)
1173 const char *type;
1174 const guchar *dest_path;
1175 gint x, y;
1177 g_return_val_if_fail(spring_context != NULL, FALSE);
1178 g_return_val_if_fail(!spring_in_progress, FALSE);
1180 type = g_dataset_get_data(spring_context, "drop_dest_type");
1181 if (type == drop_dest_bookmark)
1183 bookmarks_edit();
1184 goto out;
1187 dest_path = g_dataset_get_data(spring_context, "drop_dest_path");
1188 g_return_val_if_fail(dest_path != NULL, FALSE);
1191 * Note: Due to a bug in gtk, if a window disappears during
1192 * a drag and the pointer moves over where the window was,
1193 * the sender crashes! Therefore, do not close any windows
1194 * while dragging! (fixed in later versions)
1197 if (spring_window)
1198 gtk_widget_destroy(spring_window->window);
1201 get_pointer_xy(&x, &y);
1203 spring_in_progress++;
1204 if (spring_window)
1206 view_cursor_to_iter(spring_window->view, NULL);
1207 filer_change_to(spring_window, dest_path, NULL);
1208 /* DON'T move the window. Gtk+ sometimes doesn't
1209 * notice :-(
1212 else
1214 spring_window = filer_opendir(dest_path,
1215 spring_src_window, NULL);
1216 if (spring_window)
1218 gtk_timeout_add(500, spring_check_idle, NULL);
1219 g_signal_connect(spring_window->window, "destroy",
1220 G_CALLBACK(spring_win_destroyed), NULL);
1221 centre_window(spring_window->window->window, x, y);
1224 spring_in_progress--;
1226 out:
1227 dnd_spring_abort();
1229 return FALSE;
1232 static void spring_win_destroyed(GtkWidget *widget, gpointer data)
1234 spring_window = NULL;
1237 /* HANDLING MOTION EVENTS */
1239 /* If not-NULL, then this widget has a grab */
1240 static GtkWidget *motion_widget = NULL;
1242 /* If TRUE, we must gdk_pointer_ungrab() too when finishing */
1243 static gboolean motion_pointer_grab = FALSE;
1245 /* Call this on a button press event. It stores the mouse position
1246 * as the start of the new drag and returns TRUE if all is well.
1247 * Further motions events are disabled at this point - you must
1248 * then call dnd_motion_start() to set the type of motion expected.
1249 * Grabs the widget on the first press.
1251 * If the system is not ready to handle a motion event (because a
1252 * button is already held down?) it does nothing and returns FALSE.
1254 * If the event is not a single click then it simply returns TRUE.
1256 gboolean dnd_motion_press(GtkWidget *widget, GdkEventButton *event)
1258 if (event->type != GDK_BUTTON_PRESS)
1259 return TRUE; /* Not a click event! */
1261 motion_buttons_pressed++;
1262 if (motion_buttons_pressed == 1)
1264 /* g_print("[ grab! ]\n"); */
1265 gtk_grab_add(widget);
1266 motion_widget = widget;
1269 if (motion_state != MOTION_NONE)
1270 return FALSE; /* Ignore clicks - we're busy! */
1272 motion_state = MOTION_DISABLED;
1273 drag_start_x = event->x_root;
1274 drag_start_y = event->y_root;
1276 return TRUE;
1279 /* After the button press event, decide what kind of motion is expected.
1280 * If you don't call this then the motion system is disabled - call
1281 * dnd_motion_release() to reset it.
1283 * Note: If you open a popup menu or start DND call dnd_motion_ungrab()
1284 * instead.
1286 void dnd_motion_start(MotionType motion)
1288 g_return_if_fail(motion_state == MOTION_DISABLED);
1290 motion_state = motion;
1293 /* Call this on a button release event. If some buttons are still pressed,
1294 * returns TRUE and does nothing.
1296 * Otherwise, it resets the motion system to be ready again and returns TRUE.
1298 * If the motion system wasn't being used (MOTION_NONE) then it does nothing
1299 * and returns FALSE - process the release event yourself as it isn't part
1300 * of a motion. This also happens if a motion was primed but never happened.
1302 gboolean dnd_motion_release(GdkEventButton *event)
1304 MotionType motion = motion_state;
1305 gint drag_threshold;
1306 int dx, dy;
1308 if (motion_buttons_pressed == 0)
1309 return TRUE; /* We were disabled */
1311 if (motion_buttons_pressed == 1)
1312 dnd_motion_ungrab();
1313 else
1315 motion_buttons_pressed--;
1316 return TRUE;
1319 if (motion == MOTION_REPOSITION || motion == MOTION_DISABLED)
1320 return TRUE; /* Already done something - eat the event */
1322 /* Eat release events that happen too far from the click
1323 * source. Otherwise, allow the caller to treat this as a click
1324 * that never became a motion.
1326 dx = event->x_root - drag_start_x;
1327 dy = event->y_root - drag_start_y;
1329 g_object_get(gtk_settings_get_default(),
1330 "gtk-dnd-drag-threshold", &drag_threshold,
1331 NULL);
1333 return ABS(dx) > drag_threshold || ABS(dy) > drag_threshold;
1336 /* Use this to disable the motion system. The system will be reset once
1337 * all mouse buttons are released.
1339 void dnd_motion_disable(void)
1341 g_return_if_fail(motion_state != MOTION_NONE &&
1342 motion_state != MOTION_DISABLED);
1344 motion_state = MOTION_DISABLED;
1347 /* Use this if something else is going to grab the pointer so that
1348 * we won't get any more motion or release events.
1350 void dnd_motion_ungrab(void)
1352 if (motion_buttons_pressed > 0)
1354 if (motion_pointer_grab)
1356 gdk_pointer_ungrab(GDK_CURRENT_TIME);
1357 motion_pointer_grab = FALSE;
1358 /* g_print("[ ungrab_pointer ]\n"); */
1360 gtk_grab_remove(motion_widget);
1361 motion_widget = NULL;
1362 motion_buttons_pressed = 0;
1363 /* g_print("[ ungrab ]\n"); */
1366 motion_state = MOTION_NONE;
1369 /* Call this on motion events. If the mouse position is far enough
1370 * from the click position, returns TRUE and does dnd_motion_ungrab().
1371 * You should then start regular drag-and-drop.
1373 * Otherwise, returns FALSE.
1375 gboolean dnd_motion_moved(GdkEventMotion *event)
1377 gint drag_threshold;
1378 int dx, dy;
1380 g_object_get(gtk_settings_get_default(),
1381 "gtk-dnd-drag-threshold", &drag_threshold,
1382 NULL);
1384 dx = event->x_root - drag_start_x;
1385 dy = event->y_root - drag_start_y;
1387 if (ABS(dx) <= drag_threshold && ABS(dy) <= drag_threshold)
1388 return FALSE; /* Not far enough */
1390 dnd_motion_ungrab();
1392 return TRUE;
1395 /* Normally, the X server will automatically grab the pointer on a
1396 * button press and ungrab on release. However, if the grab widget
1397 * is reparented then call this to re-aquire the grab.
1399 void dnd_motion_grab_pointer(void)
1401 g_return_if_fail(motion_widget != NULL);
1403 gdk_pointer_grab(motion_widget->window, FALSE,
1404 GDK_POINTER_MOTION_MASK |
1405 GDK_BUTTON_RELEASE_MASK,
1406 FALSE, NULL, GDK_CURRENT_TIME);
1408 motion_pointer_grab = TRUE;