1998-11-10 05:28:34 +03:00
|
|
|
/* Drag and Drop functionality for the Midnight Commander
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 The Free Software Foundation
|
|
|
|
*
|
|
|
|
* Authors: Federico Mena <federico@nuclecu.unam.mx>
|
|
|
|
* Miguel de Icaza <miguel@nuclecu.unam.mx>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <sys/stat.h>
|
1998-11-20 08:14:38 +03:00
|
|
|
#include <sys/types.h>
|
1998-11-10 05:28:34 +03:00
|
|
|
#include "file.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "panel.h"
|
|
|
|
#include "gscreen.h"
|
|
|
|
#include "../vfs/vfs.h"
|
1998-11-11 03:05:12 +03:00
|
|
|
#include <gdk/gdkprivate.h>
|
|
|
|
#include "gdnd.h"
|
|
|
|
|
1998-11-10 05:28:34 +03:00
|
|
|
|
1998-11-11 03:05:12 +03:00
|
|
|
/* The menu of DnD actions */
|
|
|
|
static GnomeUIInfo actions[] = {
|
|
|
|
GNOMEUIINFO_ITEM_NONE (N_("Move here"), NULL, NULL),
|
|
|
|
GNOMEUIINFO_ITEM_NONE (N_("Copy here"), NULL, NULL),
|
|
|
|
GNOMEUIINFO_ITEM_NONE (N_("Link here"), NULL, NULL),
|
|
|
|
GNOMEUIINFO_SEPARATOR,
|
1998-12-17 11:39:16 +03:00
|
|
|
GNOMEUIINFO_ITEM_NONE (N_("Cancel drag"), NULL, NULL),
|
|
|
|
GNOMEUIINFO_END
|
1998-11-11 03:05:12 +03:00
|
|
|
};
|
1998-11-10 05:28:34 +03:00
|
|
|
|
|
|
|
/* Pops up a menu of actions to perform on dropped files */
|
|
|
|
static GdkDragAction
|
|
|
|
get_action (void)
|
|
|
|
{
|
1998-11-11 03:05:12 +03:00
|
|
|
GtkWidget *menu;
|
|
|
|
int a;
|
|
|
|
GdkDragAction action;
|
|
|
|
|
|
|
|
menu = gnome_popup_menu_new (actions);
|
|
|
|
a = gnome_popup_menu_do_popup_modal (menu, NULL, NULL, NULL, NULL);
|
|
|
|
|
1998-12-06 01:53:30 +03:00
|
|
|
switch (a) {
|
1998-11-11 03:05:12 +03:00
|
|
|
case 0:
|
|
|
|
action = GDK_ACTION_MOVE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
action = GDK_ACTION_COPY;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
action = GDK_ACTION_LINK;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
action = GDK_ACTION_ASK; /* Magic value to indicate cancellation */
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_widget_destroy (menu);
|
|
|
|
|
|
|
|
return action;
|
1998-11-10 05:28:34 +03:00
|
|
|
}
|
|
|
|
|
1998-11-29 10:50:44 +03:00
|
|
|
/*
|
|
|
|
* Looks for a panel that has the specified window for its list
|
|
|
|
* display. It is used to figure out if we are receiving a drop from
|
|
|
|
* a panel on this MC process. If no panel is found, it returns NULL.
|
1998-11-10 05:28:34 +03:00
|
|
|
*/
|
|
|
|
static WPanel *
|
1998-11-28 06:12:13 +03:00
|
|
|
find_panel_owning_window (GdkDragContext *context)
|
1998-11-10 05:28:34 +03:00
|
|
|
{
|
|
|
|
GList *list;
|
|
|
|
WPanel *panel;
|
1998-12-06 00:54:25 +03:00
|
|
|
GtkWidget *source_widget, *toplevel_widget;
|
1998-11-10 05:28:34 +03:00
|
|
|
|
1998-11-28 06:12:13 +03:00
|
|
|
source_widget = gtk_drag_get_source_widget (context);
|
|
|
|
if (!source_widget)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We will scan the list of existing WPanels. We
|
|
|
|
* uniformize the thing by pulling the toplevel
|
|
|
|
* widget for each WPanel and compare this to the
|
|
|
|
* toplevel source_widget
|
|
|
|
*/
|
1998-12-06 00:54:25 +03:00
|
|
|
toplevel_widget = gtk_widget_get_toplevel (source_widget);
|
1998-11-10 05:28:34 +03:00
|
|
|
|
|
|
|
for (list = containers; list; list = list->next) {
|
1998-11-28 06:12:13 +03:00
|
|
|
GtkWidget *panel_toplevel_widget;
|
|
|
|
|
1998-11-10 05:28:34 +03:00
|
|
|
panel = ((PanelContainer *) list->data)->panel;
|
|
|
|
|
1998-11-28 06:12:13 +03:00
|
|
|
panel_toplevel_widget = panel->xwindow;
|
1998-11-10 05:28:34 +03:00
|
|
|
|
1998-12-06 00:54:25 +03:00
|
|
|
if (panel->xwindow == toplevel_widget){
|
1998-12-05 02:39:41 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now a WPanel actually contains a number of
|
|
|
|
* drag sources. If the drag source is the
|
|
|
|
* Tree, we must report that it was not the
|
|
|
|
* contents of the WPanel
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (source_widget == panel->tree)
|
|
|
|
return NULL;
|
|
|
|
|
1998-11-10 05:28:34 +03:00
|
|
|
return panel;
|
1998-12-05 02:39:41 +03:00
|
|
|
}
|
1998-11-10 05:28:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1998-11-29 10:50:44 +03:00
|
|
|
/*
|
|
|
|
* Performs a drop action on the specified panel. Only supports copy
|
|
|
|
* and move operations. The files are moved or copied to the
|
|
|
|
* specified destination directory.
|
1998-11-10 05:28:34 +03:00
|
|
|
*/
|
|
|
|
static void
|
1998-12-30 03:32:41 +03:00
|
|
|
perform_action_on_panel (WPanel *source_panel, GdkDragAction action, char *destdir, int ask)
|
1998-11-10 05:28:34 +03:00
|
|
|
{
|
|
|
|
switch (action) {
|
|
|
|
case GDK_ACTION_COPY:
|
1998-12-30 03:32:41 +03:00
|
|
|
if (ask)
|
|
|
|
panel_operate (source_panel, OP_COPY, destdir);
|
|
|
|
else
|
|
|
|
panel_operate_def (source_panel, OP_COPY, destdir);
|
1998-11-10 05:28:34 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GDK_ACTION_MOVE:
|
1998-12-30 03:32:41 +03:00
|
|
|
if (ask)
|
|
|
|
panel_operate (source_panel, OP_MOVE, destdir);
|
|
|
|
else
|
|
|
|
panel_operate_def (source_panel, OP_MOVE, destdir);
|
1998-11-10 05:28:34 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finish up */
|
|
|
|
|
|
|
|
update_one_panel_widget (source_panel, FALSE, UP_KEEPSEL);
|
1998-12-17 07:51:24 +03:00
|
|
|
|
|
|
|
if (action == GDK_ACTION_MOVE)
|
|
|
|
panel_update_contents (source_panel);
|
1998-11-10 05:28:34 +03:00
|
|
|
}
|
|
|
|
|
1998-11-29 10:50:44 +03:00
|
|
|
/*
|
|
|
|
* Performs handling of symlinks via drag and drop. This should go
|
|
|
|
* away when operation windows support links.
|
1998-11-10 05:28:34 +03:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
perform_links (GList *names, char *destdir)
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
char *dest_name;
|
|
|
|
|
|
|
|
for (; names; names = names->next) {
|
|
|
|
name = names->data;
|
|
|
|
if (strncmp (name, "file:", 5) == 0)
|
|
|
|
name += 5;
|
|
|
|
|
|
|
|
dest_name = g_concat_dir_and_file (destdir, x_basename (name));
|
|
|
|
mc_symlink (name, dest_name);
|
|
|
|
g_free (dest_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-11-29 10:50:44 +03:00
|
|
|
/*
|
|
|
|
* Performs a drop action manually, by going through the list of files
|
|
|
|
* to operate on. The files are copied or moved to the specified
|
|
|
|
* directory. This should also encompass symlinking when the file
|
1998-11-10 05:28:34 +03:00
|
|
|
* operations window supports links.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
perform_action (GList *names, GdkDragAction action, char *destdir)
|
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
char *name;
|
|
|
|
char *dest_name;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case GDK_ACTION_COPY:
|
|
|
|
create_op_win (OP_COPY, FALSE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GDK_ACTION_MOVE:
|
|
|
|
create_op_win (OP_MOVE, FALSE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
|
|
|
|
|
|
|
file_mask_defaults ();
|
|
|
|
|
|
|
|
for (; names; names = names->next) {
|
|
|
|
name = names->data;
|
|
|
|
if (strncmp (name, "file:", 5) == 0)
|
|
|
|
name += 5;
|
|
|
|
|
|
|
|
dest_name = g_concat_dir_and_file (destdir, x_basename (name));
|
|
|
|
|
|
|
|
do {
|
|
|
|
result = mc_stat (name, &s);
|
|
|
|
|
|
|
|
if (result != 0) {
|
|
|
|
/* FIXME: this error message sucks */
|
|
|
|
if (file_error (_("Could not stat %s\n%s"), dest_name) != FILE_RETRY)
|
|
|
|
result = 0;
|
1998-12-14 04:36:24 +03:00
|
|
|
} else {
|
|
|
|
long count = 0;
|
|
|
|
double bytes = 0;
|
1998-12-05 02:39:41 +03:00
|
|
|
|
1998-12-14 04:36:24 +03:00
|
|
|
if (S_ISDIR (s.st_mode)) {
|
|
|
|
if (action == GDK_ACTION_COPY)
|
|
|
|
copy_dir_dir (
|
|
|
|
name, dest_name,
|
|
|
|
TRUE, FALSE,
|
|
|
|
FALSE, FALSE,
|
|
|
|
&count, &bytes);
|
|
|
|
else
|
|
|
|
move_dir_dir (
|
|
|
|
name, dest_name,
|
|
|
|
&count, &bytes);
|
|
|
|
} else {
|
|
|
|
if (action == GDK_ACTION_COPY)
|
|
|
|
copy_file_file (
|
|
|
|
name, dest_name,
|
|
|
|
TRUE,
|
1998-12-30 03:32:41 +03:00
|
|
|
&count, &bytes, 1);
|
1998-12-14 04:36:24 +03:00
|
|
|
else
|
|
|
|
move_file_file (
|
|
|
|
name, dest_name,
|
|
|
|
&count, &bytes);
|
1998-11-10 05:28:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (result != 0);
|
|
|
|
|
|
|
|
g_free (dest_name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy_op_win ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gdnd_drop_on_directory:
|
|
|
|
* @context: The drag context received from the drag_data_received callback
|
|
|
|
* @selection_data: The selection data from the drag_data_received callback
|
|
|
|
* @dirname: The name of the directory to drop onto
|
|
|
|
*
|
|
|
|
* Extracts an URI list from the selection data and drops all the files in the
|
|
|
|
* specified directory.
|
|
|
|
*
|
|
|
|
* Return Value: TRUE if the drop was sucessful, FALSE if it was not.
|
|
|
|
**/
|
|
|
|
int
|
|
|
|
gdnd_drop_on_directory (GdkDragContext *context, GtkSelectionData *selection_data, char *destdir)
|
|
|
|
{
|
|
|
|
GdkDragAction action;
|
|
|
|
WPanel *source_panel;
|
|
|
|
GList *names;
|
|
|
|
|
|
|
|
if (context->suggested_action == GDK_ACTION_ASK) {
|
|
|
|
action = get_action ();
|
|
|
|
|
|
|
|
if (action == GDK_ACTION_ASK)
|
|
|
|
return FALSE;
|
1998-12-30 03:32:41 +03:00
|
|
|
|
1998-11-10 05:28:34 +03:00
|
|
|
} else
|
|
|
|
action = context->suggested_action;
|
|
|
|
|
|
|
|
/* If we are dragging from a file panel, we can display a nicer status display */
|
1998-11-29 10:50:44 +03:00
|
|
|
source_panel = find_panel_owning_window (context);
|
1998-11-10 05:28:34 +03:00
|
|
|
|
1998-12-06 00:54:25 +03:00
|
|
|
printf ("Panel found for this source: %p\n", source_panel);
|
1998-11-10 05:28:34 +03:00
|
|
|
/* Symlinks do not use file.c */
|
|
|
|
|
|
|
|
if (source_panel && action != GDK_ACTION_LINK)
|
1998-12-30 03:32:41 +03:00
|
|
|
perform_action_on_panel (source_panel, action, destdir, context->suggested_action == GDK_ACTION_ASK);
|
1998-11-10 05:28:34 +03:00
|
|
|
else {
|
|
|
|
names = gnome_uri_list_extract_uris (selection_data->data);
|
|
|
|
|
|
|
|
if (action == GDK_ACTION_LINK)
|
|
|
|
perform_links (names, destdir);
|
|
|
|
else
|
|
|
|
perform_action (names, action, destdir);
|
|
|
|
|
|
|
|
gnome_uri_list_free_strings (names);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|