4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2000, Thomas Leonard, <tal197@ecs.soton.ac.uk>.
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)
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
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 */
35 #include <X11/Xatom.h>
37 #include "collection.h"
43 #include "gui_support.h"
48 #define MAXURILEN 4096 /* Longest URI to allow */
50 /* Static prototypes */
51 static void create_uri_list(GString
*string
,
52 Collection
*collection
,
53 FilerWindow
*filer_window
);
54 static gboolean
drag_drop(GtkWidget
*widget
,
55 GdkDragContext
*context
,
59 FilerWindow
*filer_window
);
60 static gboolean
provides(GdkDragContext
*context
, GdkAtom target
);
61 static void set_xds_prop(GdkDragContext
*context
, char *text
);
62 static gboolean
drag_motion(GtkWidget
*widget
,
63 GdkDragContext
*context
,
67 FilerWindow
*filer_window
);
68 static void drag_leave(GtkWidget
*widget
,
69 GdkDragContext
*context
,
71 FilerWindow
*filer_window
);
72 static void drag_data_received(GtkWidget
*widget
,
73 GdkDragContext
*context
,
76 GtkSelectionData
*selection_data
,
79 FilerWindow
*filer_window
);
80 static void got_data_xds_reply(GtkWidget
*widget
,
81 GdkDragContext
*context
,
82 GtkSelectionData
*selection_data
,
84 FilerWindow
*filer_window
);
85 static void got_data_raw(GtkWidget
*widget
,
86 GdkDragContext
*context
,
87 GtkSelectionData
*selection_data
,
89 static GSList
*uri_list_to_gslist(char *uri_list
);
90 static void got_uri_list(GtkWidget
*widget
,
91 GdkDragContext
*context
,
92 GtkSelectionData
*selection_data
,
94 static GtkWidget
*create_options();
95 static void update_options();
96 static void set_options();
97 static void save_options();
98 static char *load_no_hostnames(char *data
);
99 static char *drag_to_icons(char *data
);
100 static void drag_end(GtkWidget
*widget
,
101 GdkDragContext
*context
,
102 FilerWindow
*filer_window
);
104 /* Possible values for drop_dest_type (can also be NULL).
105 * In either case, drop_dest_path is the app/file/dir to use.
107 static char *drop_dest_prog
= "drop_dest_prog"; /* Run a program */
108 static char *drop_dest_dir
= "drop_dest_dir"; /* Save to path */
110 static OptionsSection options
=
112 N_("Drag and Drop options"),
126 GdkAtom XdndDirectSave0
;
127 GdkAtom xa_text_plain
;
128 GdkAtom text_uri_list
;
129 GdkAtom application_octet_stream
;
133 XdndDirectSave0
= gdk_atom_intern("XdndDirectSave0", FALSE
);
134 xa_text_plain
= gdk_atom_intern("text/plain", FALSE
);
135 text_uri_list
= gdk_atom_intern("text/uri-list", FALSE
);
136 application_octet_stream
= gdk_atom_intern("application/octet-stream",
139 options_sections
= g_slist_prepend(options_sections
, &options
);
140 option_register("dnd_no_hostnames", load_no_hostnames
);
141 option_register("dnd_drag_to_icons", drag_to_icons
);
146 static gboolean o_no_hostnames
= FALSE
;
147 static gboolean o_drag_to_icons
= TRUE
;
148 static GtkWidget
*toggle_no_hostnames
;
149 static GtkWidget
*toggle_drag_to_icons
;
151 /* Build up some option widgets to go in the options dialog, but don't
154 static GtkWidget
*create_options()
156 GtkWidget
*vbox
, *label
;
158 vbox
= gtk_vbox_new(FALSE
, 0);
159 gtk_container_set_border_width(GTK_CONTAINER(vbox
), 4);
161 label
= gtk_label_new(_("Some older applications don't support XDND "
162 "fully and may need to have this option turned on. "
163 "Use this if dragging files to an application shows "
164 "a + sign on the pointer but the drop doesn't work."));
165 gtk_label_set_line_wrap(GTK_LABEL(label
), TRUE
);
166 gtk_box_pack_start(GTK_BOX(vbox
), label
, FALSE
, TRUE
, 0);
168 toggle_no_hostnames
=
169 gtk_check_button_new_with_label(_("Don't use hostnames"));
170 gtk_box_pack_start(GTK_BOX(vbox
), toggle_no_hostnames
, FALSE
, TRUE
, 0);
172 toggle_drag_to_icons
=
173 gtk_check_button_new_with_label(_("Allow dragging to icons in "
175 gtk_box_pack_start(GTK_BOX(vbox
), toggle_drag_to_icons
, FALSE
, TRUE
, 0);
180 static void update_options()
182 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_no_hostnames
),
184 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_drag_to_icons
),
188 static void set_options()
190 o_no_hostnames
= gtk_toggle_button_get_active(
191 GTK_TOGGLE_BUTTON(toggle_no_hostnames
));
192 o_drag_to_icons
= gtk_toggle_button_get_active(
193 GTK_TOGGLE_BUTTON(toggle_drag_to_icons
));
196 static void save_options()
198 option_write("dnd_no_hostnames", o_no_hostnames
? "1" : "0");
199 option_write("dnd_drag_to_icons", o_drag_to_icons
? "1" : "0");
202 static char *load_no_hostnames(char *data
)
204 o_no_hostnames
= atoi(data
) != 0;
208 static char *drag_to_icons(char *data
)
210 o_drag_to_icons
= atoi(data
) != 0;
214 /* SUPPORT FUNCTIONS */
216 /* Set the XdndDirectSave0 property on the source window for this context */
217 static void set_xds_prop(GdkDragContext
*context
, char *text
)
219 gdk_property_change(context
->source_window
,
222 GDK_PROP_MODE_REPLACE
,
227 static char *get_xds_prop(GdkDragContext
*context
)
232 if (gdk_property_get(context
->source_window
,
238 &length
, &prop_text
) && prop_text
)
240 /* Terminate the string */
241 prop_text
= g_realloc(prop_text
, length
+ 1);
242 prop_text
[length
] = '\0';
249 /* Is the sender willing to supply this target type? */
250 static gboolean
provides(GdkDragContext
*context
, GdkAtom target
)
252 GList
*targets
= context
->targets
;
254 while (targets
&& ((GdkAtom
) targets
->data
!= target
))
255 targets
= targets
->next
;
257 return targets
!= NULL
;
260 /* Convert a list of URIs into a list of strings.
261 * Lines beginning with # are skipped.
262 * The text block passed in is zero terminated (after the final CRLF)
264 static GSList
*uri_list_to_gslist(char *uri_list
)
274 linebreak
= strchr(uri_list
, 13);
276 if (!linebreak
|| linebreak
[1] != 10)
278 delayed_error("uri_list_to_gslist",
279 _("Incorrect or missing line "
280 "break in text/uri-list data"));
284 length
= linebreak
- uri_list
;
286 if (length
&& uri_list
[0] != '#')
288 uri
= g_malloc(sizeof(char) * (length
+ 1));
289 strncpy(uri
, uri_list
, length
);
291 list
= g_slist_append(list
, uri
);
294 uri_list
= linebreak
+ 2;
300 /* Append all the URIs in the selection to the string */
301 static void create_uri_list(GString
*string
,
302 Collection
*collection
,
303 FilerWindow
*filer_window
)
308 leader
= g_string_new("file://");
310 g_string_append(leader
, our_host_name());
311 g_string_append(leader
, filer_window
->path
);
312 if (leader
->str
[leader
->len
- 1] != '/')
313 g_string_append_c(leader
, '/');
315 num_selected
= collection
->number_selected
;
317 for (i
= 0; num_selected
> 0; i
++)
319 if (collection
->items
[i
].selected
)
321 DirItem
*item
= (DirItem
*) collection
->items
[i
].data
;
323 g_string_append(string
, leader
->str
);
324 g_string_append(string
, item
->leafname
);
325 g_string_append(string
, "\r\n");
330 g_string_free(leader
, TRUE
);
333 /* DRAGGING FROM US */
335 /* The user has held the mouse button down over an item and moved -
338 * We always provide text/uri-list. If we are dragging a single, regular file
339 * then we also offer application/octet-stream and the type of the file.
341 void drag_selection(Collection
*collection
,
342 GdkEventMotion
*event
,
343 gint number_selected
,
348 GdkDragContext
*context
;
349 GtkTargetList
*target_list
;
350 GtkTargetEntry target_table
[] =
352 {"text/uri-list", 0, TARGET_URI_LIST
},
353 {"application/octet-stream", 0, TARGET_RAW
},
357 GdkDragAction actions
;
359 if (number_selected
== 1)
360 item
= selected_item(collection
);
364 widget
= GTK_WIDGET(collection
);
366 if (item
&& item
->mime_type
)
368 MIME_type
*t
= item
->mime_type
;
370 target_table
[2].target
= g_strconcat(t
->media_type
, "/",
372 target_list
= gtk_target_list_new(target_table
, 3);
373 g_free(target_table
[2].target
);
376 target_list
= gtk_target_list_new(target_table
, 1);
378 if (event
->state
& GDK_BUTTON1_MASK
)
379 actions
= GDK_ACTION_COPY
| GDK_ACTION_MOVE
| GDK_ACTION_LINK
;
381 actions
= GDK_ACTION_MOVE
;
383 context
= gtk_drag_begin(widget
,
386 (event
->state
& GDK_BUTTON1_MASK
) ? 1 :
387 (event
->state
& GDK_BUTTON2_MASK
) ? 2 : 3,
390 image
= item
? item
->image
: default_pixmap
[TYPE_MULTIPLE
];
392 gtk_drag_set_icon_pixmap(context
,
393 gtk_widget_get_colormap(widget
),
399 static void drag_end(GtkWidget
*widget
,
400 GdkDragContext
*context
,
401 FilerWindow
*filer_window
)
403 collection_clear_selection(filer_window
->collection
);
406 /* Called when a remote app wants us to send it some data.
407 * TODO: Maybe we should handle errors better (ie, let the remote app know
408 * the drag has failed)?
410 void drag_data_get(GtkWidget
*widget
,
411 GdkDragContext
*context
,
412 GtkSelectionData
*selection_data
,
415 FilerWindow
*filer_window
)
417 char *to_send
= "E"; /* Default to sending an error */
418 long to_send_length
= 1;
419 gboolean delete_once_sent
= FALSE
;
420 GdkAtom type
= XA_STRING
;
427 item
= selected_item(filer_window
->collection
);
428 if (item
&& load_file(make_path(filer_window
->path
,
429 item
->leafname
)->str
,
430 &to_send
, &to_send_length
))
432 delete_once_sent
= TRUE
;
433 type
= selection_data
->target
;
436 g_warning("drag_data_get: Can't find selected item\n");
438 case TARGET_URI_LIST
:
439 string
= g_string_new(NULL
);
440 create_uri_list(string
,
443 to_send
= string
->str
;
444 to_send_length
= string
->len
;
445 delete_once_sent
= TRUE
;
446 g_string_free(string
, FALSE
);
449 delayed_error("drag_data_get",
450 _("Internal error - bad info type"));
454 gtk_selection_data_set(selection_data
,
460 if (delete_once_sent
)
466 /* Set up this filer window as a drop target. Called once, when the
467 * filer window is first created.
469 void drag_set_dest(FilerWindow
*filer_window
)
471 GtkWidget
*widget
= GTK_WIDGET(filer_window
->collection
);
472 GtkTargetEntry target_table
[] =
474 {"text/uri-list", 0, TARGET_URI_LIST
},
475 {"XdndDirectSave0", 0, TARGET_XDS
},
476 {"application/octet-stream", 0, TARGET_RAW
},
479 gtk_drag_dest_set(widget
,
480 0, /* GTK_DEST_DEFAULT_MOTION, */
482 sizeof(target_table
) / sizeof(*target_table
),
483 GDK_ACTION_COPY
| GDK_ACTION_MOVE
484 | GDK_ACTION_LINK
| GDK_ACTION_PRIVATE
);
486 gtk_signal_connect(GTK_OBJECT(widget
), "drag_motion",
487 GTK_SIGNAL_FUNC(drag_motion
), filer_window
);
488 gtk_signal_connect(GTK_OBJECT(widget
), "drag_leave",
489 GTK_SIGNAL_FUNC(drag_leave
), filer_window
);
490 gtk_signal_connect(GTK_OBJECT(widget
), "drag_drop",
491 GTK_SIGNAL_FUNC(drag_drop
), filer_window
);
492 gtk_signal_connect(GTK_OBJECT(widget
), "drag_data_received",
493 GTK_SIGNAL_FUNC(drag_data_received
), filer_window
);
495 gtk_signal_connect(GTK_OBJECT(widget
), "drag_end",
496 GTK_SIGNAL_FUNC(drag_end
), filer_window
);
499 /* Called during the drag when the mouse is in a widget registered
500 * as a drop target. Returns TRUE if we can accept the drop.
502 static gboolean
drag_motion(GtkWidget
*widget
,
503 GdkDragContext
*context
,
507 FilerWindow
*filer_window
)
511 GdkDragAction action
= context
->suggested_action
;
512 char *new_path
= NULL
;
515 if (o_drag_to_icons
|| filer_window
->panel_type
!= PANEL_NO
)
516 item_number
= collection_get_item(filer_window
->collection
,
521 item
= item_number
>= 0
522 ? (DirItem
*) filer_window
->collection
->items
[item_number
].data
527 /* If we didn't drop onto a directory, application or
528 * executable file then act as though the drop is to the
531 if (item
->base_type
!= TYPE_DIRECTORY
532 && !(item
->flags
& ITEM_FLAG_EXEC_FILE
))
538 /* Drop onto the window background */
539 collection_set_cursor_item(filer_window
->collection
,
542 if (gtk_drag_get_source_widget(context
) == widget
)
545 if (access(filer_window
->path
, W_OK
) != 0)
546 goto out
; /* No write permission */
548 if (filer_window
->panel_type
!= PANEL_NO
)
550 if (context
->actions
& GDK_ACTION_LINK
)
552 action
= GDK_ACTION_LINK
;
553 type
= drop_dest_dir
;
557 type
= drop_dest_dir
;
560 new_path
= g_strdup(filer_window
->path
);
564 /* Drop onto a program/directory of some sort */
566 if (gtk_drag_get_source_widget(context
) == widget
)
568 Collection
*collection
= filer_window
->collection
;
570 if (collection
->items
[item_number
].selected
)
574 if (item
->base_type
== TYPE_DIRECTORY
&&
575 !(item
->flags
& ITEM_FLAG_APPDIR
))
577 if (provides(context
, text_uri_list
) ||
578 provides(context
, XdndDirectSave0
))
579 type
= drop_dest_dir
;
583 if (provides(context
, text_uri_list
) ||
584 provides(context
, application_octet_stream
))
585 type
= drop_dest_prog
;
590 new_path
= make_path(filer_window
->path
,
591 item
->leafname
)->str
;
592 collection_set_cursor_item(filer_window
->collection
,
598 g_dataset_set_data(context
, "drop_dest_type", type
);
601 gdk_drag_status(context
, action
, time
);
602 g_dataset_set_data_full(context
, "drop_dest_path",
603 g_strdup(new_path
), g_free
);
611 /* Remove panel highlights */
612 static void drag_leave(GtkWidget
*widget
,
613 GdkDragContext
*context
,
615 FilerWindow
*filer_window
)
617 collection_set_cursor_item(filer_window
->collection
, -1);
620 /* User has tried to drop some data on us. Decide what format we would
623 static gboolean
drag_drop(GtkWidget
*widget
,
624 GdkDragContext
*context
,
628 FilerWindow
*filer_window
)
631 char *leafname
= NULL
;
632 GdkAtom target
= GDK_NONE
;
634 char *dest_type
= NULL
;
636 g_print("[ filer_window = %s ]\n", filer_window
->path
);
638 dest_path
= g_dataset_get_data(context
, "drop_dest_path");
639 dest_type
= g_dataset_get_data(context
, "drop_dest_type");
641 g_return_val_if_fail(dest_path
!= NULL
, TRUE
);
643 if (dest_type
== drop_dest_dir
&& provides(context
, XdndDirectSave0
))
645 leafname
= get_xds_prop(context
);
648 if (strchr(leafname
, '/'))
650 error
= _("XDS protocol error: "
651 "leafname may not contain '/'\n");
660 uri
= g_string_new(NULL
);
661 g_string_sprintf(uri
, "file://%s%s",
665 set_xds_prop(context
, uri
->str
);
666 g_string_free(uri
, TRUE
);
668 target
= XdndDirectSave0
;
669 g_dataset_set_data_full(context
, "leafname",
675 "XdndDirectSave0 target provided, but the atom "
676 "XdndDirectSave0 (type text/plain) did not "
677 "contain a leafname\n");
679 else if (provides(context
, text_uri_list
))
680 target
= text_uri_list
;
681 else if (provides(context
, application_octet_stream
))
682 target
= application_octet_stream
;
685 if (dest_type
== drop_dest_dir
)
686 error
= _("Sorry - I require a target type of "
687 "text/uri-list or XdndDirectSave0.");
689 error
= _("Sorry - I require a target type of "
690 "text/uri-list or application/octet-stream.");
695 gtk_drag_finish(context
, FALSE
, FALSE
, time
); /* Failure */
697 delayed_error(PROJECT
, error
);
700 gtk_drag_get_data(widget
, context
, target
, time
);
705 /* Called when some data arrives from the remote app (which we asked for
708 static void drag_data_received(GtkWidget
*widget
,
709 GdkDragContext
*context
,
712 GtkSelectionData
*selection_data
,
715 FilerWindow
*filer_window
)
717 if (!selection_data
->data
)
720 gtk_drag_finish(context
, FALSE
, FALSE
, time
); /* Failure */
727 got_data_xds_reply(widget
, context
,
728 selection_data
, time
,
732 got_data_raw(widget
, context
, selection_data
, time
);
734 case TARGET_URI_LIST
:
735 got_uri_list(widget
, context
, selection_data
, time
);
738 gtk_drag_finish(context
, FALSE
, FALSE
, time
);
739 delayed_error("drag_data_received",
740 _("Unknown target"));
745 static void got_data_xds_reply(GtkWidget
*widget
,
746 GdkDragContext
*context
,
747 GtkSelectionData
*selection_data
,
749 FilerWindow
*filer_window
)
751 gboolean mark_unsafe
= TRUE
;
752 char response
= *selection_data
->data
;
756 dest_path
= g_dataset_get_data(context
, "drop_dest_path");
758 if (selection_data
->length
!= 1)
763 /* Sender couldn't save there - ask for another
766 if (provides(context
, application_octet_stream
))
768 mark_unsafe
= FALSE
; /* Wait and see */
770 gtk_drag_get_data(widget
, context
,
771 application_octet_stream
, time
);
774 error
= _("Remote app can't or won't send me "
777 else if (response
== 'S')
779 /* Success - data is saved */
780 mark_unsafe
= FALSE
; /* It really is safe */
781 gtk_drag_finish(context
, TRUE
, FALSE
, time
);
783 refresh_dirs(dest_path
);
785 else if (response
!= 'E')
787 error
= _("XDS protocol error: "
788 "return code should be 'S', 'F' or 'E'\n");
790 /* else: error has been reported by the sender */
794 set_xds_prop(context
, "");
795 /* Unsave also implies that the drag failed */
796 gtk_drag_finish(context
, FALSE
, FALSE
, time
);
800 delayed_error(PROJECT
, error
);
803 static void got_data_raw(GtkWidget
*widget
,
804 GdkDragContext
*context
,
805 GtkSelectionData
*selection_data
,
813 g_return_if_fail(selection_data
->data
!= NULL
);
815 dest_path
= g_dataset_get_data(context
, "drop_dest_path");
817 if (g_dataset_get_data(context
, "drop_dest_type") == drop_dest_prog
)
819 /* The data needs to be sent to an application */
820 run_with_data(dest_path
,
821 selection_data
->data
, selection_data
->length
);
822 gtk_drag_finish(context
, TRUE
, FALSE
, time
); /* Success! */
826 leafname
= g_dataset_get_data(context
, "leafname");
828 leafname
= _("UntitledData");
830 fd
= open(make_path(dest_path
, leafname
)->str
,
831 O_WRONLY
| O_CREAT
| O_EXCL
| O_NOCTTY
,
832 S_IRUSR
| S_IRGRP
| S_IROTH
|
833 S_IWUSR
| S_IWGRP
| S_IWOTH
);
836 error
= g_strerror(errno
);
840 selection_data
->data
,
841 selection_data
->length
) == -1)
842 error
= g_strerror(errno
);
844 if (close(fd
) == -1 && !error
)
845 error
= g_strerror(errno
);
847 refresh_dirs(dest_path
);
852 if (provides(context
, XdndDirectSave0
))
853 set_xds_prop(context
, "");
854 gtk_drag_finish(context
, FALSE
, FALSE
, time
); /* Failure */
855 delayed_error(_("Error saving file"), error
);
858 gtk_drag_finish(context
, TRUE
, FALSE
, time
); /* Success! */
861 /* We've got a list of URIs from somewhere (probably another filer window).
862 * If the files are on the local machine then try to copy them ourselves,
863 * otherwise, if there was only one file and application/octet-stream was
864 * provided, get the data via the X server.
866 static void got_uri_list(GtkWidget
*widget
,
867 GdkDragContext
*context
,
868 GtkSelectionData
*selection_data
,
874 gboolean send_reply
= TRUE
;
878 dest_path
= g_dataset_get_data(context
, "drop_dest_path");
879 type
= g_dataset_get_data(context
, "drop_dest_type");
881 g_return_if_fail(dest_path
!= NULL
);
883 uri_list
= uri_list_to_gslist(selection_data
->data
);
886 error
= _("No URIs in the text/uri-list (nothing to do!)");
887 else if (type
== drop_dest_prog
)
888 run_with_files(dest_path
, uri_list
);
889 else if ((!uri_list
->next
) && (!get_local_path(uri_list
->data
)))
891 /* There is one URI in the list, and it's not on the local
892 * machine. Get it via the X server if possible.
895 if (provides(context
, application_octet_stream
))
898 leaf
= strrchr(uri_list
->data
, '/');
902 leaf
= uri_list
->data
;
903 g_dataset_set_data_full(context
, "leafname",
904 g_strdup(leaf
), g_free
);
905 gtk_drag_get_data(widget
, context
,
906 application_octet_stream
, time
);
910 error
= _("Can't get data from remote machine "
911 "(application/octet-stream not provided)");
915 GSList
*local_paths
= NULL
;
918 /* Either one local URI, or a list. If everything in the list
919 * isn't local then we are stuck.
922 for (next_uri
= uri_list
; next_uri
; next_uri
= next_uri
->next
)
926 path
= get_local_path((char *) next_uri
->data
);
929 local_paths
= g_slist_append(local_paths
,
932 error
= _("Some of these files are on a "
933 "different machine - they will be "
939 error
= _("None of these files are on the local "
940 "machine - I can't operate on multiple "
941 "remote files - sorry.");
943 else if (context
->action
== GDK_ACTION_MOVE
)
944 action_move(local_paths
, dest_path
);
945 else if (context
->action
== GDK_ACTION_COPY
)
946 action_copy(local_paths
, dest_path
, NULL
);
947 else if (context
->action
== GDK_ACTION_LINK
)
948 action_link(local_paths
, dest_path
);
950 error
= _("Unknown action requested");
952 for (next
= local_paths
; next
; next
= next
->next
)
954 g_slist_free(local_paths
);
959 gtk_drag_finish(context
, FALSE
, FALSE
, time
); /* Failure */
960 delayed_error(_("Error getting file list"), error
);
963 gtk_drag_finish(context
, TRUE
, FALSE
, time
); /* Success! */
968 g_free(next_uri
->data
);
969 next_uri
= next_uri
->next
;
971 g_slist_free(uri_list
);