r240: Tidied up the pixmap code - now uses ImLib functions if possible.
[rox-filer.git] / ROX-Filer / src / dnd.c
blob8896f5691dce83bffbb1a8084cd5931e70a91a77
1 /*
2 * $Id$
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)
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 <unistd.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <string.h>
34 #include <X11/Xlib.h>
35 #include <X11/Xatom.h>
36 #include <gtk/gtk.h>
37 #include "collection.h"
39 #include "dnd.h"
40 #include "filer.h"
41 #include "action.h"
42 #include "pixmaps.h"
43 #include "gui_support.h"
44 #include "support.h"
45 #include "options.h"
46 #include "run.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,
56 gint x,
57 gint y,
58 guint time);
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,
63 gint x,
64 gint y,
65 guint time);
66 static void drag_leave(GtkWidget *widget,
67 GdkDragContext *context);
68 static void drag_data_received(GtkWidget *widget,
69 GdkDragContext *context,
70 gint x,
71 gint y,
72 GtkSelectionData *selection_data,
73 guint info,
74 guint32 time);
75 static void got_data_xds_reply(GtkWidget *widget,
76 GdkDragContext *context,
77 GtkSelectionData *selection_data,
78 guint32 time);
79 static void got_data_raw(GtkWidget *widget,
80 GdkDragContext *context,
81 GtkSelectionData *selection_data,
82 guint32 time);
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,
87 guint32 time);
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",
104 create_options,
105 update_options,
106 set_options,
107 save_options
110 enum
112 TARGET_RAW,
113 TARGET_URI_LIST,
114 TARGET_XDS,
117 GdkAtom XdndDirectSave0;
118 GdkAtom xa_text_plain;
119 GdkAtom text_uri_list;
120 GdkAtom application_octet_stream;
122 void dnd_init()
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",
128 FALSE);
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);
135 /* OPTIONS */
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
143 * fill them in yet.
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 "
165 "filer windows");
166 gtk_box_pack_start(GTK_BOX(vbox), toggle_drag_to_icons, FALSE, TRUE, 0);
168 return vbox;
171 static void update_options()
173 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_no_hostnames),
174 o_no_hostnames);
175 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_drag_to_icons),
176 o_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;
196 return NULL;
199 static char *drag_to_icons(char *data)
201 o_drag_to_icons = atoi(data) != 0;
202 return NULL;
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,
211 XdndDirectSave0,
212 xa_text_plain, 8,
213 GDK_PROP_MODE_REPLACE,
214 text,
215 strlen(text));
218 static char *get_xds_prop(GdkDragContext *context)
220 guchar *prop_text;
221 gint length;
223 if (gdk_property_get(context->source_window,
224 XdndDirectSave0,
225 xa_text_plain,
226 0, MAXURILEN,
227 FALSE,
228 NULL, NULL,
229 &length, &prop_text) && prop_text)
231 /* Terminate the string */
232 prop_text = g_realloc(prop_text, length + 1);
233 prop_text[length] = '\0';
234 return prop_text;
237 return NULL;
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)
257 GSList *list = NULL;
259 while (*uri_list)
261 char *linebreak;
262 char *uri;
263 int length;
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");
272 return list;
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);
281 uri[length] = 0;
282 list = g_slist_append(list, uri);
285 uri_list = linebreak + 2;
288 return list;
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)
296 GString *leader;
297 int i, num_selected;
299 leader = g_string_new("file://");
300 if (!o_no_hostnames)
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");
317 num_selected--;
321 g_string_free(leader, TRUE);
324 /* DRAGGING FROM US */
326 /* The user has held the mouse button down over an item and moved -
327 * start a drag.
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,
335 gpointer user_data)
337 FilerWindow *filer_window = (FilerWindow *) user_data;
338 GtkWidget *widget;
339 MaskedPixmap *image;
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},
346 {"", 0, TARGET_RAW},
348 DirItem *item;
350 if (number_selected == 1)
351 item = selected_item(collection);
352 else
353 item = NULL;
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, "/",
362 t->subtype, NULL);
363 target_list = gtk_target_list_new(target_table, 3);
364 g_free(target_table[2].target);
366 else
367 target_list = gtk_target_list_new(target_table, 1);
369 context = gtk_drag_begin(widget,
370 target_list,
371 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK,
372 (event->state & GDK_BUTTON1_MASK) ? 1 : 2,
373 (GdkEvent *) event);
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),
380 image->pixmap,
381 image->mask,
382 0, 0);
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,
392 guint info,
393 guint32 time)
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;
399 GString *string;
400 FilerWindow *filer_window;
401 DirItem *item;
403 filer_window = g_dataset_get_data(context, "filer_window");
404 g_return_if_fail(filer_window != NULL);
406 switch (info)
408 case TARGET_RAW:
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;
416 break;
418 g_warning("drag_data_get: Can't find selected item\n");
419 return;
420 case TARGET_URI_LIST:
421 string = g_string_new(NULL);
422 create_uri_list(string,
423 COLLECTION(widget),
424 filer_window);
425 to_send = string->str;
426 to_send_length = string->len;
427 delete_once_sent = TRUE;
428 g_string_free(string, FALSE);
429 break;
430 default:
431 delayed_error("drag_data_get",
432 "Internal error - bad info type\n");
433 break;
436 gtk_selection_data_set(selection_data,
437 type,
439 to_send,
440 to_send_length);
442 if (delete_once_sent)
443 g_free(to_send);
445 collection_clear_selection(filer_window->collection);
448 /* DRAGGING TO US */
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, */
465 target_table,
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,
485 gint x,
486 gint y,
487 guint time)
489 FilerWindow *filer_window;
490 DirItem *item;
491 int item_number;
492 GdkDragAction action = context->suggested_action;
493 char *new_path = NULL;
494 char *type = 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,
501 x, y);
502 else
503 item_number = -1;
505 item = item_number >= 0
506 ? (DirItem *) filer_window->collection->items[item_number].data
507 : NULL;
509 if (item)
511 /* If we didn't drop onto a directory, application or
512 * executable file then act as though the drop is to the
513 * window background.
515 if (item->base_type != TYPE_DIRECTORY
516 && !(item->flags & ITEM_FLAG_EXEC_FILE))
517 item = NULL;
520 if (!item)
522 /* Drop onto the window background */
523 collection_set_cursor_item(filer_window->collection,
524 -1);
526 if (gtk_drag_get_source_widget(context) == widget)
527 goto out;
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;
540 else
541 type = drop_dest_dir;
543 if (type)
544 new_path = g_strdup(filer_window->path);
546 else
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)
555 goto out;
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;
565 else
567 if (provides(context, text_uri_list) ||
568 provides(context, application_octet_stream))
569 type = drop_dest_prog;
572 if (type)
574 new_path = make_path(filer_window->path,
575 item->leafname)->str;
576 collection_set_cursor_item(filer_window->collection,
577 item_number);
581 out:
582 g_dataset_set_data(context, "drop_dest_type", type);
583 if (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);
589 else
590 g_free(new_path);
592 return type != NULL;
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
608 * like the data in.
610 static gboolean drag_drop(GtkWidget *widget,
611 GdkDragContext *context,
612 gint x,
613 gint y,
614 guint time)
616 char *error = NULL;
617 char *leafname = NULL;
618 FilerWindow *filer_window;
619 GdkAtom target = GDK_NONE;
620 char *dest_path;
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);
634 if (leafname)
636 if (strchr(leafname, '/'))
638 error = "XDS protocol error: "
639 "leafname may not contain '/'\n";
640 g_free(leafname);
642 leafname = NULL;
644 else
646 GString *uri;
648 uri = g_string_new(NULL);
649 g_string_sprintf(uri, "file://%s%s",
650 our_host_name(),
651 make_path(dest_path,
652 leafname)->str);
653 set_xds_prop(context, uri->str);
654 g_string_free(uri, TRUE);
656 target = XdndDirectSave0;
657 g_dataset_set_data_full(context, "leafname",
658 leafname, g_free);
661 else
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;
670 else
672 if (dest_type == drop_dest_dir)
673 error = "Sorry - I require a target type of "
674 "text/uri-list or XdndDirectSave0.";
675 else
676 error = "Sorry - I require a target type of "
677 "text/uri-list or application/octet-stream.";
680 if (error)
682 gtk_drag_finish(context, FALSE, FALSE, time); /* Failure */
684 delayed_error("ROX-Filer", error);
686 else
687 gtk_drag_get_data(widget, context, target, time);
689 return TRUE;
692 /* Called when some data arrives from the remote app (which we asked for
693 * in drag_drop).
695 static void drag_data_received(GtkWidget *widget,
696 GdkDragContext *context,
697 gint x,
698 gint y,
699 GtkSelectionData *selection_data,
700 guint info,
701 guint32 time)
703 if (!selection_data->data)
705 /* Timeout? */
706 gtk_drag_finish(context, FALSE, FALSE, time); /* Failure */
707 return;
710 switch (info)
712 case TARGET_XDS:
713 got_data_xds_reply(widget, context,
714 selection_data, time);
715 break;
716 case TARGET_RAW:
717 got_data_raw(widget, context, selection_data, time);
718 break;
719 case TARGET_URI_LIST:
720 got_uri_list(widget, context, selection_data, time);
721 break;
722 default:
723 gtk_drag_finish(context, FALSE, FALSE, time);
724 delayed_error("drag_data_received", "Unknown target");
725 break;
729 static void got_data_xds_reply(GtkWidget *widget,
730 GdkDragContext *context,
731 GtkSelectionData *selection_data,
732 guint32 time)
734 gboolean mark_unsafe = TRUE;
735 char response = *selection_data->data;
736 char *error = NULL;
737 char *dest_path;
739 dest_path = g_dataset_get_data(context, "drop_dest_path");
741 if (selection_data->length != 1)
742 response = '?';
744 if (response == 'F')
746 /* Sender couldn't save there - ask for another
747 * type if possible.
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);
756 else
757 error = "Remote app can't or won't send me "
758 "the data - sorry";
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),
769 "filer_window");
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 */
781 if (mark_unsafe)
783 set_xds_prop(context, "");
784 /* Unsave also implies that the drag failed */
785 gtk_drag_finish(context, FALSE, FALSE, time);
788 if (error)
789 delayed_error("ROX-Filer", error);
792 static void got_data_raw(GtkWidget *widget,
793 GdkDragContext *context,
794 GtkSelectionData *selection_data,
795 guint32 time)
797 char *leafname;
798 int fd;
799 char *error = NULL;
800 char *dest_path;
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! */
812 return;
815 leafname = g_dataset_get_data(context, "leafname");
816 if (!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);
824 if (fd == -1)
825 error = g_strerror(errno);
826 else
828 if (write(fd,
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);
839 if (error)
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);
846 else
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,
858 guint32 time)
860 GSList *uri_list;
861 char *error = NULL;
862 GSList *next_uri;
863 gboolean send_reply = TRUE;
864 char *dest_path;
865 char *type;
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);
874 if (!uri_list)
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))
886 char *leaf;
887 leaf = strrchr(uri_list->data, '/');
888 if (leaf)
889 leaf++;
890 else
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);
896 send_reply = FALSE;
898 else
899 error = "Can't get data from remote machine "
900 "(application/octet-stream not provided)";
902 else
904 GSList *local_paths = NULL;
905 GSList *next;
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)
913 char *path;
915 path = get_local_path((char *) next_uri->data);
917 if (path)
918 local_paths = g_slist_append(local_paths,
919 g_strdup(path));
920 else
921 error = "Some of these files are on a "
922 "different machine - they will be "
923 "ignored - sorry";
926 if (!local_paths)
928 error = "None of these files are on the local machine "
929 "- I can't operate on multiple remote files - "
930 "sorry.";
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);
938 else
939 error = "Unknown action requested";
941 for (next = local_paths; next; next = next->next)
942 g_free(next->data);
943 g_slist_free(local_paths);
946 if (error)
948 gtk_drag_finish(context, FALSE, FALSE, time); /* Failure */
949 delayed_error("Error getting file list", error);
951 else if (send_reply)
952 gtk_drag_finish(context, TRUE, FALSE, time); /* Success! */
954 next_uri = uri_list;
955 while (next_uri)
957 g_free(next_uri->data);
958 next_uri = next_uri->next;
960 g_slist_free(uri_list);