r110: Added a option for using RISC OS mouse bindings to the options box, although
[rox-filer.git] / ROX-Filer / src / dnd.c
blob98f4bcccff9174f75067d1723474de1403fbbbe6
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 1999, 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)
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 <stdlib.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <errno.h>
32 #include <X11/Xlib.h>
33 #include <X11/Xatom.h>
34 #include <gtk/gtk.h>
35 #include "collection.h"
37 #include "filer.h"
38 #include "action.h"
39 #include "pixmaps.h"
40 #include "gui_support.h"
41 #include "support.h"
42 #include "options.h"
44 #define MAXURILEN 4096 /* Longest URI to allow */
46 /* Static prototypes */
47 static char *get_dest_path(FilerWindow *filer_window, GdkDragContext *context);
48 static void create_uri_list(GString *string,
49 Collection *collection,
50 FilerWindow *filer_window);
51 static gboolean drag_drop(GtkWidget *widget,
52 GdkDragContext *context,
53 gint x,
54 gint y,
55 guint time);
56 static gboolean provides(GdkDragContext *context, GdkAtom target);
57 static void set_xds_prop(GdkDragContext *context, char *text);
58 static gboolean drag_motion(GtkWidget *widget,
59 GdkDragContext *context,
60 gint x,
61 gint y,
62 guint time);
63 static void drag_leave(GtkWidget *widget,
64 GdkDragContext *context);
65 static void drag_data_received(GtkWidget *widget,
66 GdkDragContext *context,
67 gint x,
68 gint y,
69 GtkSelectionData *selection_data,
70 guint info,
71 guint32 time);
72 static void got_data_xds_reply(GtkWidget *widget,
73 GdkDragContext *context,
74 GtkSelectionData *selection_data,
75 guint32 time);
76 static void got_data_raw(GtkWidget *widget,
77 GdkDragContext *context,
78 GtkSelectionData *selection_data,
79 guint32 time);
80 static GSList *uri_list_to_gslist(char *uri_list);
81 static void got_uri_list(GtkWidget *widget,
82 GdkDragContext *context,
83 GtkSelectionData *selection_data,
84 guint32 time);
85 static char *get_local_path(char *uri);
86 static void run_with_files(char *path, GSList *uri_list);
87 static GtkWidget *create_options();
88 static void update_options();
89 static void set_options();
90 static void save_options();
91 static char *load_no_hostnames(char *data);
93 /* Possible values for drop_dest_type (can also be NULL).
94 * In either case, drop_dest_path is the app/file/dir to use.
96 static char *drop_dest_prog = "drop_dest_prog"; /* Run a program */
97 static char *drop_dest_dir = "drop_dest_dir"; /* Save to path */
99 static OptionsSection options =
101 "Drag and Drop options",
102 create_options,
103 update_options,
104 set_options,
105 save_options
108 enum
110 TARGET_RAW,
111 TARGET_URI_LIST,
112 TARGET_XDS,
115 GdkAtom XdndDirectSave0;
116 GdkAtom xa_text_plain;
117 GdkAtom text_uri_list;
118 GdkAtom application_octet_stream;
120 void dnd_init()
122 XdndDirectSave0 = gdk_atom_intern("XdndDirectSave0", FALSE);
123 xa_text_plain = gdk_atom_intern("text/plain", FALSE);
124 text_uri_list = gdk_atom_intern("text/uri-list", FALSE);
125 application_octet_stream = gdk_atom_intern("application/octet-stream",
126 FALSE);
128 options_sections = g_slist_prepend(options_sections, &options);
129 option_register("dnd_no_hostnames", load_no_hostnames);
132 /* OPTIONS */
134 static gboolean o_no_hostnames = FALSE;
135 static GtkWidget *toggle_no_hostnames;
137 /* Build up some option widgets to go in the options dialog, but don't
138 * fill them in yet.
140 static GtkWidget *create_options()
142 GtkWidget *vbox, *label;
144 vbox = gtk_vbox_new(FALSE, 0);
145 gtk_container_set_border_width(GTK_CONTAINER(vbox), 4);
147 label = gtk_label_new("Some older applications don't support XDND "
148 "fully and may need to have this option turned on. "
149 "Use this if dragging files to an application shows "
150 "a + sign on the pointer but the drop doesn't work.");
151 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
152 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, TRUE, 0);
154 toggle_no_hostnames =
155 gtk_check_button_new_with_label("Don't use hostnames");
156 gtk_box_pack_start(GTK_BOX(vbox), toggle_no_hostnames, FALSE, TRUE, 0);
158 return vbox;
161 static void update_options()
163 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_no_hostnames),
164 o_no_hostnames);
167 static void set_options()
169 o_no_hostnames = gtk_toggle_button_get_active(
170 GTK_TOGGLE_BUTTON(toggle_no_hostnames));
173 static void save_options()
175 option_write("dnd_no_hostnames", o_no_hostnames ? "1" : "0");
178 static char *load_no_hostnames(char *data)
180 o_no_hostnames = atoi(data) != 0;
181 return NULL;
184 /* SUPPORT FUNCTIONS */
186 static char *get_dest_path(FilerWindow *filer_window, GdkDragContext *context)
188 char *path;
190 path = g_dataset_get_data(context, "drop_dest_path");
192 return path ? path : filer_window->path;
195 /* Set the XdndDirectSave0 property on the source window for this context */
196 static void set_xds_prop(GdkDragContext *context, char *text)
198 gdk_property_change(context->source_window,
199 XdndDirectSave0,
200 xa_text_plain, 8,
201 GDK_PROP_MODE_REPLACE,
202 text,
203 strlen(text));
206 static char *get_xds_prop(GdkDragContext *context)
208 guchar *prop_text;
209 gint length;
211 if (gdk_property_get(context->source_window,
212 XdndDirectSave0,
213 xa_text_plain,
214 0, MAXURILEN,
215 FALSE,
216 NULL, NULL,
217 &length, &prop_text) && prop_text)
219 /* Terminate the string */
220 prop_text = g_realloc(prop_text, length + 1);
221 prop_text[length] = '\0';
222 return prop_text;
225 return NULL;
228 /* Is the sender willing to supply this target type? */
229 static gboolean provides(GdkDragContext *context, GdkAtom target)
231 GList *targets = context->targets;
233 while (targets && ((GdkAtom) targets->data != target))
234 targets = targets->next;
236 return targets != NULL;
239 /* Convert a URI to a local pathname (or NULL if it isn't local).
240 * The returned pointer points inside the input string.
241 * Possible formats:
242 * /path
243 * ///path
244 * //host/path
245 * file://host/path
247 static char *get_local_path(char *uri)
249 char *host;
251 host = our_host_name();
253 if (*uri == '/')
255 char *path;
257 if (uri[1] != '/')
258 return uri; /* Just a local path - no host part */
260 path = strchr(uri + 2, '/');
261 if (!path)
262 return NULL; /* //something */
264 if (path - uri == 2)
265 return path; /* ///path */
266 if (strlen(host) == path - uri - 2 &&
267 strncmp(uri + 2, host, path - uri - 2) == 0)
268 return path; /* //myhost/path */
270 return NULL; /* From a different host */
272 else
274 if (strncasecmp(uri, "file:", 5))
275 return NULL; /* Don't know this format */
277 uri += 5;
279 if (*uri == '/')
280 return get_local_path(uri);
282 return NULL;
286 /* Convert a list of URIs into a list of strings.
287 * Lines beginning with # are skipped.
288 * The text block passed in is zero terminated (after the final CRLF)
290 static GSList *uri_list_to_gslist(char *uri_list)
292 GSList *list = NULL;
294 while (*uri_list)
296 char *linebreak;
297 char *uri;
298 int length;
300 linebreak = strchr(uri_list, 13);
302 if (!linebreak || linebreak[1] != 10)
304 delayed_error("uri_list_to_gslist",
305 "Incorrect or missing line break "
306 "in text/uri-list data");
307 return list;
310 length = linebreak - uri_list;
312 if (length && uri_list[0] != '#')
314 uri = g_malloc(sizeof(char) * (length + 1));
315 strncpy(uri, uri_list, length);
316 uri[length] = 0;
317 list = g_slist_append(list, uri);
320 uri_list = linebreak + 2;
323 return list;
326 /* Append all the URIs in the selection to the string */
327 static void create_uri_list(GString *string,
328 Collection *collection,
329 FilerWindow *filer_window)
331 GString *leader;
332 int i, num_selected;
334 leader = g_string_new("file://");
335 if (!o_no_hostnames)
336 g_string_append(leader, our_host_name());
337 g_string_append(leader, filer_window->path);
338 if (leader->str[leader->len - 1] != '/')
339 g_string_append_c(leader, '/');
341 num_selected = collection->number_selected;
343 for (i = 0; num_selected > 0; i++)
345 if (collection->items[i].selected)
347 FileItem *item = (FileItem *) collection->items[i].data;
349 g_string_append(string, leader->str);
350 g_string_append(string, item->leafname);
351 g_string_append(string, "\r\n");
352 num_selected--;
356 g_string_free(leader, TRUE);
359 /* DRAGGING FROM US */
361 /* The user has held the mouse button down over an item and moved -
362 * start a drag.
364 * We always provide text/uri-list. If we are dragging a single, regular file
365 * then we also offer application/octet-stream and the type of the file.
367 void drag_selection(Collection *collection,
368 GdkEventMotion *event,
369 gint number_selected,
370 gpointer user_data)
372 FilerWindow *filer_window = (FilerWindow *) user_data;
373 GtkWidget *widget;
374 MaskedPixmap *image;
375 GdkDragContext *context;
376 GtkTargetList *target_list;
377 GtkTargetEntry target_table[] =
379 {"text/uri-list", 0, TARGET_URI_LIST},
380 {"application/octet-stream", 0, TARGET_RAW},
381 {"", 0, TARGET_RAW},
383 FileItem *item;
385 if (number_selected == 1)
386 item = selected_item(collection);
387 else
388 item = NULL;
390 widget = GTK_WIDGET(collection);
392 if (item && item->mime_type)
394 MIME_type *t = item->mime_type;
396 target_table[2].target = g_strconcat(t->media_type, "/",
397 t->subtype, NULL);
398 target_list = gtk_target_list_new(target_table, 3);
399 g_free(target_table[2].target);
401 else
402 target_list = gtk_target_list_new(target_table, 1);
404 context = gtk_drag_begin(widget,
405 target_list,
406 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK,
407 (event->state & GDK_BUTTON1_MASK) ? 1 : 2,
408 (GdkEvent *) event);
409 g_dataset_set_data(context, "filer_window", filer_window);
411 image = item ? item->image : &default_pixmap[TYPE_MULTIPLE];
413 gtk_drag_set_icon_pixmap(context,
414 gtk_widget_get_colormap(widget),
415 image->pixmap,
416 image->mask,
417 0, 0);
420 /* Called when a remote app wants us to send it some data.
421 * TODO: Maybe we should handle errors better (ie, let the remote app know
422 * the drag has failed)?
424 void drag_data_get(GtkWidget *widget,
425 GdkDragContext *context,
426 GtkSelectionData *selection_data,
427 guint info,
428 guint32 time)
430 char *to_send = "E"; /* Default to sending an error */
431 long to_send_length = 1;
432 gboolean delete_once_sent = FALSE;
433 GdkAtom type = XA_STRING;
434 GString *string;
435 FilerWindow *filer_window;
436 FileItem *item;
438 filer_window = g_dataset_get_data(context, "filer_window");
439 g_return_if_fail(filer_window != NULL);
441 switch (info)
443 case TARGET_RAW:
444 item = selected_item(filer_window->collection);
445 if (item && load_file(make_path(filer_window->path,
446 item->leafname)->str,
447 &to_send, &to_send_length))
449 delete_once_sent = TRUE;
450 if (item->mime_type)
451 type = type_to_atom(item->mime_type);
452 else
453 type = application_octet_stream;
454 break;
456 return;
457 case TARGET_URI_LIST:
458 string = g_string_new(NULL);
459 create_uri_list(string,
460 COLLECTION(widget),
461 filer_window);
462 to_send = string->str;
463 to_send_length = string->len;
464 delete_once_sent = TRUE;
465 g_string_free(string, FALSE);
466 break;
467 default:
468 delayed_error("drag_data_get",
469 "Internal error - bad info type\n");
470 break;
473 gtk_selection_data_set(selection_data,
474 type,
476 to_send,
477 to_send_length);
479 if (delete_once_sent)
480 g_free(to_send);
483 /* DRAGGING TO US */
485 /* Set up this filer window as a drop target. Called once, when the
486 * filer window is first created.
488 void drag_set_dest(GtkWidget *widget, FilerWindow *filer_window)
490 GtkTargetEntry target_table[] =
492 {"text/uri-list", 0, TARGET_URI_LIST},
493 {"XdndDirectSave0", 0, TARGET_XDS},
494 {"application/octet-stream", 0, TARGET_RAW},
497 gtk_drag_dest_set(widget,
498 0, /* GTK_DEST_DEFAULT_MOTION, */
499 target_table,
500 sizeof(target_table) / sizeof(*target_table),
501 GDK_ACTION_COPY | GDK_ACTION_MOVE
502 | GDK_ACTION_LINK | GDK_ACTION_PRIVATE);
504 gtk_signal_connect(GTK_OBJECT(widget), "drag_motion",
505 GTK_SIGNAL_FUNC(drag_motion), filer_window);
506 gtk_signal_connect(GTK_OBJECT(widget), "drag_leave",
507 GTK_SIGNAL_FUNC(drag_leave), filer_window);
508 gtk_signal_connect(GTK_OBJECT(widget), "drag_drop",
509 GTK_SIGNAL_FUNC(drag_drop), filer_window);
510 gtk_signal_connect(GTK_OBJECT(widget), "drag_data_received",
511 GTK_SIGNAL_FUNC(drag_data_received), filer_window);
514 /* Decide if panel drag is OK, setting drop_dest_type and drop_dest_path
515 * on the context as needed.
517 static gboolean panel_drag_ok(FilerWindow *filer_window,
518 GdkDragContext *context,
519 int item)
521 FileItem *fileitem = NULL;
522 char *old_path;
523 char *new_path;
524 char *type = NULL; /* Quiet gcc */
526 if (item >= 0)
527 fileitem = (FileItem *)
528 filer_window->collection->items[item].data;
530 if (item == -1)
531 new_path = NULL; /* Drag to panel background - disallow */
532 else if (fileitem->flags & (ITEM_FLAG_APPDIR | ITEM_FLAG_EXEC_FILE))
534 if (provides(context, text_uri_list))
536 type = drop_dest_prog;
537 new_path = make_path(filer_window->path,
538 fileitem->leafname)->str;
540 else
541 new_path = NULL;
543 else if (fileitem->base_type == TYPE_DIRECTORY)
545 type = drop_dest_dir;
546 new_path = make_path(filer_window->path,
547 fileitem->leafname)->str;
549 else
550 new_path = NULL;
552 if (new_path && access(new_path, W_OK))
553 new_path = NULL;
555 old_path = g_dataset_get_data(context, "drop_dest_path");
556 if (old_path == new_path ||
557 (old_path && new_path && strcmp(old_path, new_path) == 0))
559 return new_path != NULL; /* Same as before */
562 if (new_path)
564 g_dataset_set_data(context, "drop_dest_type", type);
565 g_dataset_set_data_full(context, "drop_dest_path",
566 g_strdup(new_path), g_free);
568 else
570 item = -1;
571 g_dataset_set_data(context, "drop_dest_path", NULL);
574 collection_set_cursor_item(filer_window->collection, item);
576 return new_path != NULL;
579 /* Called during the drag when the mouse is in a widget registered
580 * as a drop target. Returns TRUE if we can accept the drop.
582 static gboolean drag_motion(GtkWidget *widget,
583 GdkDragContext *context,
584 gint x,
585 gint y,
586 guint time)
588 FilerWindow *filer_window;
589 int item;
591 filer_window = gtk_object_get_data(GTK_OBJECT(widget), "filer_window");
592 g_return_val_if_fail(filer_window != NULL, TRUE);
594 if (gtk_drag_get_source_widget(context) == widget)
595 return FALSE; /* Not within a single widget! */
597 if (filer_window->panel == FALSE)
599 if (access(filer_window->path, W_OK))
600 return FALSE; /* We can't write here */
601 gdk_drag_status(context, context->suggested_action, time);
602 return TRUE;
605 /* OK, this is a drag to a panel.
606 * Allow drags to directories, applications and X bit files only.
608 item = collection_get_item(filer_window->collection, x, y);
610 if (panel_drag_ok(filer_window, context, item))
612 gdk_drag_status(context, context->suggested_action, time);
613 return TRUE;
616 return FALSE;
619 /* Remove panel highlights */
620 static void drag_leave(GtkWidget *widget,
621 GdkDragContext *context)
623 FilerWindow *filer_window;
625 filer_window = gtk_object_get_data(GTK_OBJECT(widget), "filer_window");
626 g_return_if_fail(filer_window != NULL);
628 collection_set_cursor_item(filer_window->collection, -1);
631 /* User has tried to drop some data on us. Decide what format we would
632 * like the data in.
634 static gboolean drag_drop(GtkWidget *widget,
635 GdkDragContext *context,
636 gint x,
637 gint y,
638 guint time)
640 char *error = NULL;
641 char *leafname = NULL;
642 FilerWindow *filer_window;
643 GdkAtom target = GDK_NONE;
644 char *dest_path;
645 char *dest_type = NULL;
647 filer_window = gtk_object_get_data(GTK_OBJECT(widget), "filer_window");
648 g_return_val_if_fail(filer_window != NULL, TRUE);
650 dest_path = g_dataset_get_data(context, "drop_dest_path");
651 if (dest_path == NULL)
653 if (filer_window->panel)
654 error = "Bad drop on panel";
655 else
657 dest_path = filer_window->path;
658 dest_type = drop_dest_dir;
661 else
663 dest_type = g_dataset_get_data(context, "drop_dest_type");
666 if (error)
668 /* Do nothing */
670 else if (dest_type == drop_dest_dir
671 && provides(context, XdndDirectSave0))
673 leafname = get_xds_prop(context);
674 if (leafname)
676 if (strchr(leafname, '/'))
678 error = "XDS protocol error: "
679 "leafname may not contain '/'\n";
680 g_free(leafname);
682 leafname = NULL;
684 else
686 GString *uri;
688 uri = g_string_new(NULL);
689 g_string_sprintf(uri, "file://%s%s",
690 our_host_name(),
691 make_path(dest_path,
692 leafname)->str);
693 set_xds_prop(context, uri->str);
694 g_string_free(uri, TRUE);
696 target = XdndDirectSave0;
697 g_dataset_set_data_full(context, "leafname",
698 leafname, g_free);
701 else
702 error = "XdndDirectSave0 target provided, but the atom "
703 "XdndDirectSave0 (type text/plain) did not "
704 "contain a leafname\n";
706 else if (provides(context, text_uri_list))
707 target = text_uri_list;
708 else
710 if (dest_type == drop_dest_dir)
711 error = "Sorry - I require a target type of "
712 "text/uri-list or XdndDirectSave0.";
713 else
714 error = "Sorry - I require a target type of "
715 "text/uri-list.";
718 if (error)
720 gtk_drag_finish(context, FALSE, FALSE, time); /* Failure */
722 delayed_error("ROX-Filer", error);
724 else
725 gtk_drag_get_data(widget, context, target, time);
727 return TRUE;
730 /* Called when some data arrives from the remote app (which we asked for
731 * in drag_drop).
733 static void drag_data_received(GtkWidget *widget,
734 GdkDragContext *context,
735 gint x,
736 gint y,
737 GtkSelectionData *selection_data,
738 guint info,
739 guint32 time)
741 if (!selection_data->data)
743 /* Timeout? */
744 gtk_drag_finish(context, FALSE, FALSE, time); /* Failure */
745 return;
748 switch (info)
750 case TARGET_XDS:
751 got_data_xds_reply(widget, context,
752 selection_data, time);
753 break;
754 case TARGET_RAW:
755 got_data_raw(widget, context, selection_data, time);
756 break;
757 case TARGET_URI_LIST:
758 got_uri_list(widget, context, selection_data, time);
759 break;
760 default:
761 gtk_drag_finish(context, FALSE, FALSE, time);
762 delayed_error("drag_data_received", "Unknown target");
763 break;
767 static void got_data_xds_reply(GtkWidget *widget,
768 GdkDragContext *context,
769 GtkSelectionData *selection_data,
770 guint32 time)
772 gboolean mark_unsafe = TRUE;
773 char response = *selection_data->data;
774 char *error = NULL;
776 if (selection_data->length != 1)
777 response = '?';
779 if (response == 'F')
781 /* Sender couldn't save there - ask for another
782 * type if possible.
784 if (provides(context, application_octet_stream))
786 mark_unsafe = FALSE; /* Wait and see */
788 gtk_drag_get_data(widget, context,
789 application_octet_stream, time);
791 else
792 error = "Remote app can't or won't send me "
793 "the data - sorry";
795 else if (response == 'S')
797 FilerWindow *filer_window;
799 /* Success - data is saved */
800 mark_unsafe = FALSE; /* It really is safe */
801 gtk_drag_finish(context, TRUE, FALSE, time);
803 filer_window = gtk_object_get_data(GTK_OBJECT(widget),
804 "filer_window");
805 g_return_if_fail(filer_window != NULL);
807 update_dir(filer_window);
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("ROX-Filer", error);
827 static void got_data_raw(GtkWidget *widget,
828 GdkDragContext *context,
829 GtkSelectionData *selection_data,
830 guint32 time)
832 FilerWindow *filer_window;
833 char *leafname;
834 int fd;
835 char *error = NULL;
836 char *dest_path;
838 filer_window = gtk_object_get_data(GTK_OBJECT(widget), "filer_window");
839 g_return_if_fail(filer_window != NULL);
841 leafname = g_dataset_get_data(context, "leafname");
842 if (!leafname)
843 leafname = "UntitledData";
845 dest_path = get_dest_path(filer_window, context);
847 fd = open(make_path(dest_path, leafname)->str,
848 O_WRONLY | O_CREAT | O_EXCL | O_NOCTTY,
849 S_IRUSR | S_IRGRP | S_IROTH |
850 S_IWUSR | S_IWGRP | S_IWOTH);
852 if (fd == -1)
853 error = g_strerror(errno);
854 else
856 if (write(fd,
857 selection_data->data,
858 selection_data->length) == -1)
859 error = g_strerror(errno);
861 if (close(fd) == -1 && !error)
862 error = g_strerror(errno);
864 update_dir(filer_window);
867 if (error)
869 if (provides(context, XdndDirectSave0))
870 set_xds_prop(context, "");
871 gtk_drag_finish(context, FALSE, FALSE, time); /* Failure */
872 delayed_error("Error saving file", error);
874 else
875 gtk_drag_finish(context, TRUE, FALSE, time); /* Success! */
878 /* Execute this program, passing all the URIs in the list as arguments.
879 * URIs that are files on the local machine will be passed as simple
880 * pathnames. The uri_list should be freed after this function returns.
882 static void run_with_files(char *path, GSList *uri_list)
884 char **argv;
885 int argc = 0;
886 struct stat info;
888 if (stat(path, &info))
890 delayed_error("ROX-Filer", "Program not found - deleted?");
891 return;
894 argv = g_malloc(sizeof(char *) * (g_slist_length(uri_list) + 2));
896 if (S_ISDIR(info.st_mode))
897 argv[argc++] = make_path(path, "AppRun")->str;
898 else
899 argv[argc++] = path;
901 while (uri_list)
903 char *uri = (char *) uri_list->data;
904 char *local;
906 local = get_local_path(uri);
907 if (local)
908 argv[argc++] = local;
909 else
910 argv[argc++] = uri;
911 uri_list = uri_list->next;
914 argv[argc++] = NULL;
916 if (!spawn_full(argv, getenv("HOME"), 0))
917 delayed_error("ROX-Filer", "Failed to fork() child process");
920 /* We've got a list of URIs from somewhere (probably another filer window).
921 * If the files are on the local machine then try to copy them ourselves,
922 * otherwise, if there was only one file and application/octet-stream was
923 * provided, get the data via the X server.
925 static void got_uri_list(GtkWidget *widget,
926 GdkDragContext *context,
927 GtkSelectionData *selection_data,
928 guint32 time)
930 FilerWindow *filer_window;
931 GSList *uri_list;
932 char *error = NULL;
933 GSList *next_uri;
934 gboolean send_reply = TRUE;
935 char *dest_path;
936 char *type;
938 filer_window = gtk_object_get_data(GTK_OBJECT(widget), "filer_window");
939 g_return_if_fail(filer_window != NULL);
941 dest_path = get_dest_path(filer_window, context);
942 type = g_dataset_get_data(context, "drop_dest_type");
944 uri_list = uri_list_to_gslist(selection_data->data);
946 if (!uri_list)
947 error = "No URIs in the text/uri-list (nothing to do!)";
948 else if (type == drop_dest_prog)
949 run_with_files(dest_path, uri_list);
950 else if ((!uri_list->next) && (!get_local_path(uri_list->data)))
952 /* There is one URI in the list, and it's not on the local
953 * machine. Get it via the X server if possible.
956 if (provides(context, application_octet_stream))
958 char *leaf;
959 leaf = strrchr(uri_list->data, '/');
960 if (leaf)
961 leaf++;
962 else
963 leaf = uri_list->data;
964 g_dataset_set_data_full(context, "leafname",
965 g_strdup(leaf), g_free);
966 gtk_drag_get_data(widget, context,
967 application_octet_stream, time);
968 send_reply = FALSE;
970 else
971 error = "Can't get data from remote machine "
972 "(application/octet-stream not provided)";
974 else
976 GSList *local_paths = NULL;
977 GSList *next;
979 /* Either one local URI, or a list. If everything in the list
980 * isn't local then we are stuck.
983 for (next_uri = uri_list; next_uri; next_uri = next_uri->next)
985 char *path;
987 path = get_local_path((char *) next_uri->data);
989 if (path)
990 local_paths = g_slist_append(local_paths,
991 g_strdup(path));
992 else
993 error = "Some of these files are on a "
994 "different machine - they will be "
995 "ignored - sorry";
998 if (!local_paths)
1000 error = "None of these files are on the local machine "
1001 "- I can't operate on multiple remote files - "
1002 "sorry.";
1004 else if (context->action == GDK_ACTION_MOVE)
1005 action_move(local_paths, dest_path);
1006 else if (context->action == GDK_ACTION_COPY)
1007 action_copy(local_paths, dest_path);
1008 else if (context->action == GDK_ACTION_LINK)
1009 action_link(local_paths, dest_path);
1010 else
1011 error = "Unknown action requested";
1013 for (next = local_paths; next; next = next->next)
1014 g_free(next->data);
1015 g_slist_free(local_paths);
1018 if (error)
1020 gtk_drag_finish(context, FALSE, FALSE, time); /* Failure */
1021 delayed_error("Error getting file list", error);
1023 else if (send_reply)
1024 gtk_drag_finish(context, TRUE, FALSE, time); /* Success! */
1026 next_uri = uri_list;
1027 while (next_uri)
1029 g_free(next_uri->data);
1030 next_uri = next_uri->next;
1032 g_slist_free(uri_list);