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 static gboolean
provides(GdkDragContext
*context
, GdkAtom target
);
60 static void set_xds_prop(GdkDragContext
*context
, char *text
);
61 static gboolean
drag_motion(GtkWidget
*widget
,
62 GdkDragContext
*context
,
66 static void drag_leave(GtkWidget
*widget
,
67 GdkDragContext
*context
);
68 static void drag_data_received(GtkWidget
*widget
,
69 GdkDragContext
*context
,
72 GtkSelectionData
*selection_data
,
75 static void got_data_xds_reply(GtkWidget
*widget
,
76 GdkDragContext
*context
,
77 GtkSelectionData
*selection_data
,
79 static void got_data_raw(GtkWidget
*widget
,
80 GdkDragContext
*context
,
81 GtkSelectionData
*selection_data
,
83 static GSList
*uri_list_to_gslist(char *uri_list
);
84 static void got_uri_list(GtkWidget
*widget
,
85 GdkDragContext
*context
,
86 GtkSelectionData
*selection_data
,
88 static GtkWidget
*create_options();
89 static void update_options();
90 static void set_options();
91 static void save_options();
92 static char *load_no_hostnames(char *data
);
93 static char *drag_to_icons(char *data
);
95 /* Possible values for drop_dest_type (can also be NULL).
96 * In either case, drop_dest_path is the app/file/dir to use.
98 static char *drop_dest_prog
= "drop_dest_prog"; /* Run a program */
99 static char *drop_dest_dir
= "drop_dest_dir"; /* Save to path */
101 static OptionsSection options
=
103 "Drag and Drop options",
117 GdkAtom XdndDirectSave0
;
118 GdkAtom xa_text_plain
;
119 GdkAtom text_uri_list
;
120 GdkAtom application_octet_stream
;
124 XdndDirectSave0
= gdk_atom_intern("XdndDirectSave0", FALSE
);
125 xa_text_plain
= gdk_atom_intern("text/plain", FALSE
);
126 text_uri_list
= gdk_atom_intern("text/uri-list", FALSE
);
127 application_octet_stream
= gdk_atom_intern("application/octet-stream",
130 options_sections
= g_slist_prepend(options_sections
, &options
);
131 option_register("dnd_no_hostnames", load_no_hostnames
);
132 option_register("dnd_drag_to_icons", drag_to_icons
);
137 static gboolean o_no_hostnames
= FALSE
;
138 static gboolean o_drag_to_icons
= TRUE
;
139 static GtkWidget
*toggle_no_hostnames
;
140 static GtkWidget
*toggle_drag_to_icons
;
142 /* Build up some option widgets to go in the options dialog, but don't
145 static GtkWidget
*create_options()
147 GtkWidget
*vbox
, *label
;
149 vbox
= gtk_vbox_new(FALSE
, 0);
150 gtk_container_set_border_width(GTK_CONTAINER(vbox
), 4);
152 label
= gtk_label_new("Some older applications don't support XDND "
153 "fully and may need to have this option turned on. "
154 "Use this if dragging files to an application shows "
155 "a + sign on the pointer but the drop doesn't work.");
156 gtk_label_set_line_wrap(GTK_LABEL(label
), TRUE
);
157 gtk_box_pack_start(GTK_BOX(vbox
), label
, FALSE
, TRUE
, 0);
159 toggle_no_hostnames
=
160 gtk_check_button_new_with_label("Don't use hostnames");
161 gtk_box_pack_start(GTK_BOX(vbox
), toggle_no_hostnames
, FALSE
, TRUE
, 0);
163 toggle_drag_to_icons
=
164 gtk_check_button_new_with_label("Allow dragging to icons in "
166 gtk_box_pack_start(GTK_BOX(vbox
), toggle_drag_to_icons
, FALSE
, TRUE
, 0);
171 static void update_options()
173 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_no_hostnames
),
175 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_drag_to_icons
),
179 static void set_options()
181 o_no_hostnames
= gtk_toggle_button_get_active(
182 GTK_TOGGLE_BUTTON(toggle_no_hostnames
));
183 o_drag_to_icons
= gtk_toggle_button_get_active(
184 GTK_TOGGLE_BUTTON(toggle_drag_to_icons
));
187 static void save_options()
189 option_write("dnd_no_hostnames", o_no_hostnames
? "1" : "0");
190 option_write("dnd_drag_to_icons", o_drag_to_icons
? "1" : "0");
193 static char *load_no_hostnames(char *data
)
195 o_no_hostnames
= atoi(data
) != 0;
199 static char *drag_to_icons(char *data
)
201 o_drag_to_icons
= atoi(data
) != 0;
205 /* SUPPORT FUNCTIONS */
207 /* Set the XdndDirectSave0 property on the source window for this context */
208 static void set_xds_prop(GdkDragContext
*context
, char *text
)
210 gdk_property_change(context
->source_window
,
213 GDK_PROP_MODE_REPLACE
,
218 static char *get_xds_prop(GdkDragContext
*context
)
223 if (gdk_property_get(context
->source_window
,
229 &length
, &prop_text
) && prop_text
)
231 /* Terminate the string */
232 prop_text
= g_realloc(prop_text
, length
+ 1);
233 prop_text
[length
] = '\0';
240 /* Is the sender willing to supply this target type? */
241 static gboolean
provides(GdkDragContext
*context
, GdkAtom target
)
243 GList
*targets
= context
->targets
;
245 while (targets
&& ((GdkAtom
) targets
->data
!= target
))
246 targets
= targets
->next
;
248 return targets
!= NULL
;
251 /* Convert a list of URIs into a list of strings.
252 * Lines beginning with # are skipped.
253 * The text block passed in is zero terminated (after the final CRLF)
255 static GSList
*uri_list_to_gslist(char *uri_list
)
265 linebreak
= strchr(uri_list
, 13);
267 if (!linebreak
|| linebreak
[1] != 10)
269 delayed_error("uri_list_to_gslist",
270 "Incorrect or missing line break "
271 "in text/uri-list data");
275 length
= linebreak
- uri_list
;
277 if (length
&& uri_list
[0] != '#')
279 uri
= g_malloc(sizeof(char) * (length
+ 1));
280 strncpy(uri
, uri_list
, length
);
282 list
= g_slist_append(list
, uri
);
285 uri_list
= linebreak
+ 2;
291 /* Append all the URIs in the selection to the string */
292 static void create_uri_list(GString
*string
,
293 Collection
*collection
,
294 FilerWindow
*filer_window
)
299 leader
= g_string_new("file://");
301 g_string_append(leader
, our_host_name());
302 g_string_append(leader
, filer_window
->path
);
303 if (leader
->str
[leader
->len
- 1] != '/')
304 g_string_append_c(leader
, '/');
306 num_selected
= collection
->number_selected
;
308 for (i
= 0; num_selected
> 0; i
++)
310 if (collection
->items
[i
].selected
)
312 DirItem
*item
= (DirItem
*) collection
->items
[i
].data
;
314 g_string_append(string
, leader
->str
);
315 g_string_append(string
, item
->leafname
);
316 g_string_append(string
, "\r\n");
321 g_string_free(leader
, TRUE
);
324 /* DRAGGING FROM US */
326 /* The user has held the mouse button down over an item and moved -
329 * We always provide text/uri-list. If we are dragging a single, regular file
330 * then we also offer application/octet-stream and the type of the file.
332 void drag_selection(Collection
*collection
,
333 GdkEventMotion
*event
,
334 gint number_selected
,
337 FilerWindow
*filer_window
= (FilerWindow
*) user_data
;
340 GdkDragContext
*context
;
341 GtkTargetList
*target_list
;
342 GtkTargetEntry target_table
[] =
344 {"text/uri-list", 0, TARGET_URI_LIST
},
345 {"application/octet-stream", 0, TARGET_RAW
},
350 if (number_selected
== 1)
351 item
= selected_item(collection
);
355 widget
= GTK_WIDGET(collection
);
357 if (item
&& item
->mime_type
)
359 MIME_type
*t
= item
->mime_type
;
361 target_table
[2].target
= g_strconcat(t
->media_type
, "/",
363 target_list
= gtk_target_list_new(target_table
, 3);
364 g_free(target_table
[2].target
);
367 target_list
= gtk_target_list_new(target_table
, 1);
369 context
= gtk_drag_begin(widget
,
371 GDK_ACTION_COPY
| GDK_ACTION_MOVE
| GDK_ACTION_LINK
,
372 (event
->state
& GDK_BUTTON1_MASK
) ? 1 : 2,
374 g_dataset_set_data(context
, "filer_window", filer_window
);
376 image
= item
? item
->image
: default_pixmap
[TYPE_MULTIPLE
];
378 gtk_drag_set_icon_pixmap(context
,
379 gtk_widget_get_colormap(widget
),
385 /* Called when a remote app wants us to send it some data.
386 * TODO: Maybe we should handle errors better (ie, let the remote app know
387 * the drag has failed)?
389 void drag_data_get(GtkWidget
*widget
,
390 GdkDragContext
*context
,
391 GtkSelectionData
*selection_data
,
395 char *to_send
= "E"; /* Default to sending an error */
396 long to_send_length
= 1;
397 gboolean delete_once_sent
= FALSE
;
398 GdkAtom type
= XA_STRING
;
400 FilerWindow
*filer_window
;
403 filer_window
= g_dataset_get_data(context
, "filer_window");
404 g_return_if_fail(filer_window
!= NULL
);
409 item
= selected_item(filer_window
->collection
);
410 if (item
&& load_file(make_path(filer_window
->path
,
411 item
->leafname
)->str
,
412 &to_send
, &to_send_length
))
414 delete_once_sent
= TRUE
;
415 type
= selection_data
->type
;
418 g_warning("drag_data_get: Can't find selected item\n");
420 case TARGET_URI_LIST
:
421 string
= g_string_new(NULL
);
422 create_uri_list(string
,
425 to_send
= string
->str
;
426 to_send_length
= string
->len
;
427 delete_once_sent
= TRUE
;
428 g_string_free(string
, FALSE
);
431 delayed_error("drag_data_get",
432 "Internal error - bad info type\n");
436 gtk_selection_data_set(selection_data
,
442 if (delete_once_sent
)
445 collection_clear_selection(filer_window
->collection
);
450 /* Set up this filer window as a drop target. Called once, when the
451 * filer window is first created.
453 void drag_set_dest(FilerWindow
*filer_window
)
455 GtkWidget
*widget
= GTK_WIDGET(filer_window
->collection
);
456 GtkTargetEntry target_table
[] =
458 {"text/uri-list", 0, TARGET_URI_LIST
},
459 {"XdndDirectSave0", 0, TARGET_XDS
},
460 {"application/octet-stream", 0, TARGET_RAW
},
463 gtk_drag_dest_set(widget
,
464 0, /* GTK_DEST_DEFAULT_MOTION, */
466 sizeof(target_table
) / sizeof(*target_table
),
467 GDK_ACTION_COPY
| GDK_ACTION_MOVE
468 | GDK_ACTION_LINK
| GDK_ACTION_PRIVATE
);
470 gtk_signal_connect(GTK_OBJECT(widget
), "drag_motion",
471 GTK_SIGNAL_FUNC(drag_motion
), filer_window
);
472 gtk_signal_connect(GTK_OBJECT(widget
), "drag_leave",
473 GTK_SIGNAL_FUNC(drag_leave
), filer_window
);
474 gtk_signal_connect(GTK_OBJECT(widget
), "drag_drop",
475 GTK_SIGNAL_FUNC(drag_drop
), filer_window
);
476 gtk_signal_connect(GTK_OBJECT(widget
), "drag_data_received",
477 GTK_SIGNAL_FUNC(drag_data_received
), filer_window
);
480 /* Called during the drag when the mouse is in a widget registered
481 * as a drop target. Returns TRUE if we can accept the drop.
483 static gboolean
drag_motion(GtkWidget
*widget
,
484 GdkDragContext
*context
,
489 FilerWindow
*filer_window
;
492 GdkDragAction action
= context
->suggested_action
;
493 char *new_path
= NULL
;
496 filer_window
= gtk_object_get_data(GTK_OBJECT(widget
), "filer_window");
497 g_return_val_if_fail(filer_window
!= NULL
, TRUE
);
499 if (o_drag_to_icons
|| filer_window
->panel_type
!= PANEL_NO
)
500 item_number
= collection_get_item(filer_window
->collection
,
505 item
= item_number
>= 0
506 ? (DirItem
*) filer_window
->collection
->items
[item_number
].data
511 /* If we didn't drop onto a directory, application or
512 * executable file then act as though the drop is to the
515 if (item
->base_type
!= TYPE_DIRECTORY
516 && !(item
->flags
& ITEM_FLAG_EXEC_FILE
))
522 /* Drop onto the window background */
523 collection_set_cursor_item(filer_window
->collection
,
526 if (gtk_drag_get_source_widget(context
) == widget
)
529 if (access(filer_window
->path
, W_OK
) != 0)
530 goto out
; /* No write permission */
532 if (filer_window
->panel_type
!= PANEL_NO
)
534 if (context
->actions
& GDK_ACTION_LINK
)
536 action
= GDK_ACTION_LINK
;
537 type
= drop_dest_dir
;
541 type
= drop_dest_dir
;
544 new_path
= g_strdup(filer_window
->path
);
548 /* Drop onto a program/directory of some sort */
550 if (gtk_drag_get_source_widget(context
) == widget
)
552 Collection
*collection
= filer_window
->collection
;
554 if (collection
->items
[item_number
].selected
)
558 if (item
->base_type
== TYPE_DIRECTORY
&&
559 !(item
->flags
& ITEM_FLAG_APPDIR
))
561 if (provides(context
, text_uri_list
) ||
562 provides(context
, XdndDirectSave0
))
563 type
= drop_dest_dir
;
567 if (provides(context
, text_uri_list
) ||
568 provides(context
, application_octet_stream
))
569 type
= drop_dest_prog
;
574 new_path
= make_path(filer_window
->path
,
575 item
->leafname
)->str
;
576 collection_set_cursor_item(filer_window
->collection
,
582 g_dataset_set_data(context
, "drop_dest_type", type
);
585 gdk_drag_status(context
, action
, time
);
586 g_dataset_set_data_full(context
, "drop_dest_path",
587 g_strdup(new_path
), g_free
);
595 /* Remove panel highlights */
596 static void drag_leave(GtkWidget
*widget
,
597 GdkDragContext
*context
)
599 FilerWindow
*filer_window
;
601 filer_window
= gtk_object_get_data(GTK_OBJECT(widget
), "filer_window");
602 g_return_if_fail(filer_window
!= NULL
);
604 collection_set_cursor_item(filer_window
->collection
, -1);
607 /* User has tried to drop some data on us. Decide what format we would
610 static gboolean
drag_drop(GtkWidget
*widget
,
611 GdkDragContext
*context
,
617 char *leafname
= NULL
;
618 FilerWindow
*filer_window
;
619 GdkAtom target
= GDK_NONE
;
621 char *dest_type
= NULL
;
623 filer_window
= gtk_object_get_data(GTK_OBJECT(widget
), "filer_window");
624 g_return_val_if_fail(filer_window
!= NULL
, TRUE
);
626 dest_path
= g_dataset_get_data(context
, "drop_dest_path");
627 dest_type
= g_dataset_get_data(context
, "drop_dest_type");
629 g_return_val_if_fail(dest_path
!= NULL
, TRUE
);
631 if (dest_type
== drop_dest_dir
&& provides(context
, XdndDirectSave0
))
633 leafname
= get_xds_prop(context
);
636 if (strchr(leafname
, '/'))
638 error
= "XDS protocol error: "
639 "leafname may not contain '/'\n";
648 uri
= g_string_new(NULL
);
649 g_string_sprintf(uri
, "file://%s%s",
653 set_xds_prop(context
, uri
->str
);
654 g_string_free(uri
, TRUE
);
656 target
= XdndDirectSave0
;
657 g_dataset_set_data_full(context
, "leafname",
662 error
= "XdndDirectSave0 target provided, but the atom "
663 "XdndDirectSave0 (type text/plain) did not "
664 "contain a leafname\n";
666 else if (provides(context
, text_uri_list
))
667 target
= text_uri_list
;
668 else if (provides(context
, application_octet_stream
))
669 target
= application_octet_stream
;
672 if (dest_type
== drop_dest_dir
)
673 error
= "Sorry - I require a target type of "
674 "text/uri-list or XdndDirectSave0.";
676 error
= "Sorry - I require a target type of "
677 "text/uri-list or application/octet-stream.";
682 gtk_drag_finish(context
, FALSE
, FALSE
, time
); /* Failure */
684 delayed_error("ROX-Filer", error
);
687 gtk_drag_get_data(widget
, context
, target
, time
);
692 /* Called when some data arrives from the remote app (which we asked for
695 static void drag_data_received(GtkWidget
*widget
,
696 GdkDragContext
*context
,
699 GtkSelectionData
*selection_data
,
703 if (!selection_data
->data
)
706 gtk_drag_finish(context
, FALSE
, FALSE
, time
); /* Failure */
713 got_data_xds_reply(widget
, context
,
714 selection_data
, time
);
717 got_data_raw(widget
, context
, selection_data
, time
);
719 case TARGET_URI_LIST
:
720 got_uri_list(widget
, context
, selection_data
, time
);
723 gtk_drag_finish(context
, FALSE
, FALSE
, time
);
724 delayed_error("drag_data_received", "Unknown target");
729 static void got_data_xds_reply(GtkWidget
*widget
,
730 GdkDragContext
*context
,
731 GtkSelectionData
*selection_data
,
734 gboolean mark_unsafe
= TRUE
;
735 char response
= *selection_data
->data
;
739 dest_path
= g_dataset_get_data(context
, "drop_dest_path");
741 if (selection_data
->length
!= 1)
746 /* Sender couldn't save there - ask for another
749 if (provides(context
, application_octet_stream
))
751 mark_unsafe
= FALSE
; /* Wait and see */
753 gtk_drag_get_data(widget
, context
,
754 application_octet_stream
, time
);
757 error
= "Remote app can't or won't send me "
760 else if (response
== 'S')
762 FilerWindow
*filer_window
;
764 /* Success - data is saved */
765 mark_unsafe
= FALSE
; /* It really is safe */
766 gtk_drag_finish(context
, TRUE
, FALSE
, time
);
768 filer_window
= gtk_object_get_data(GTK_OBJECT(widget
),
770 g_return_if_fail(filer_window
!= NULL
);
772 refresh_dirs(dest_path
);
774 else if (response
!= 'E')
776 error
= "XDS protocol error: "
777 "return code should be 'S', 'F' or 'E'\n";
779 /* else: error has been reported by the sender */
783 set_xds_prop(context
, "");
784 /* Unsave also implies that the drag failed */
785 gtk_drag_finish(context
, FALSE
, FALSE
, time
);
789 delayed_error("ROX-Filer", error
);
792 static void got_data_raw(GtkWidget
*widget
,
793 GdkDragContext
*context
,
794 GtkSelectionData
*selection_data
,
802 g_return_if_fail(selection_data
->data
!= NULL
);
804 dest_path
= g_dataset_get_data(context
, "drop_dest_path");
806 if (g_dataset_get_data(context
, "drop_dest_type") == drop_dest_prog
)
808 /* The data needs to be sent to an application */
809 run_with_data(dest_path
,
810 selection_data
->data
, selection_data
->length
);
811 gtk_drag_finish(context
, TRUE
, FALSE
, time
); /* Success! */
815 leafname
= g_dataset_get_data(context
, "leafname");
817 leafname
= "UntitledData";
819 fd
= open(make_path(dest_path
, leafname
)->str
,
820 O_WRONLY
| O_CREAT
| O_EXCL
| O_NOCTTY
,
821 S_IRUSR
| S_IRGRP
| S_IROTH
|
822 S_IWUSR
| S_IWGRP
| S_IWOTH
);
825 error
= g_strerror(errno
);
829 selection_data
->data
,
830 selection_data
->length
) == -1)
831 error
= g_strerror(errno
);
833 if (close(fd
) == -1 && !error
)
834 error
= g_strerror(errno
);
836 refresh_dirs(dest_path
);
841 if (provides(context
, XdndDirectSave0
))
842 set_xds_prop(context
, "");
843 gtk_drag_finish(context
, FALSE
, FALSE
, time
); /* Failure */
844 delayed_error("Error saving file", error
);
847 gtk_drag_finish(context
, TRUE
, FALSE
, time
); /* Success! */
850 /* We've got a list of URIs from somewhere (probably another filer window).
851 * If the files are on the local machine then try to copy them ourselves,
852 * otherwise, if there was only one file and application/octet-stream was
853 * provided, get the data via the X server.
855 static void got_uri_list(GtkWidget
*widget
,
856 GdkDragContext
*context
,
857 GtkSelectionData
*selection_data
,
863 gboolean send_reply
= TRUE
;
867 dest_path
= g_dataset_get_data(context
, "drop_dest_path");
868 type
= g_dataset_get_data(context
, "drop_dest_type");
870 g_return_if_fail(dest_path
!= NULL
);
872 uri_list
= uri_list_to_gslist(selection_data
->data
);
875 error
= "No URIs in the text/uri-list (nothing to do!)";
876 else if (type
== drop_dest_prog
)
877 run_with_files(dest_path
, uri_list
);
878 else if ((!uri_list
->next
) && (!get_local_path(uri_list
->data
)))
880 /* There is one URI in the list, and it's not on the local
881 * machine. Get it via the X server if possible.
884 if (provides(context
, application_octet_stream
))
887 leaf
= strrchr(uri_list
->data
, '/');
891 leaf
= uri_list
->data
;
892 g_dataset_set_data_full(context
, "leafname",
893 g_strdup(leaf
), g_free
);
894 gtk_drag_get_data(widget
, context
,
895 application_octet_stream
, time
);
899 error
= "Can't get data from remote machine "
900 "(application/octet-stream not provided)";
904 GSList
*local_paths
= NULL
;
907 /* Either one local URI, or a list. If everything in the list
908 * isn't local then we are stuck.
911 for (next_uri
= uri_list
; next_uri
; next_uri
= next_uri
->next
)
915 path
= get_local_path((char *) next_uri
->data
);
918 local_paths
= g_slist_append(local_paths
,
921 error
= "Some of these files are on a "
922 "different machine - they will be "
928 error
= "None of these files are on the local machine "
929 "- I can't operate on multiple remote files - "
932 else if (context
->action
== GDK_ACTION_MOVE
)
933 action_move(local_paths
, dest_path
);
934 else if (context
->action
== GDK_ACTION_COPY
)
935 action_copy(local_paths
, dest_path
, NULL
);
936 else if (context
->action
== GDK_ACTION_LINK
)
937 action_link(local_paths
, dest_path
);
939 error
= "Unknown action requested";
941 for (next
= local_paths
; next
; next
= next
->next
)
943 g_slist_free(local_paths
);
948 gtk_drag_finish(context
, FALSE
, FALSE
, time
); /* Failure */
949 delayed_error("Error getting file list", error
);
952 gtk_drag_finish(context
, TRUE
, FALSE
, time
); /* Success! */
957 g_free(next_uri
->data
);
958 next_uri
= next_uri
->next
;
960 g_slist_free(uri_list
);