1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
/***
Copyright (c) 2015-2017 elementary LLC (http://launchpad.net/elementary)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, Inc.,; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
Authors: Jeremy Wootten <jeremy@elementaryos.org>
***/
namespace Marlin {
public class DndHandler : GLib.Object {
Gdk.DragAction chosen = Gdk.DragAction.DEFAULT;
public DndHandler () {}
public bool dnd_perform (Gtk.Widget widget,
GOF.File drop_target,
GLib.List<GLib.File> drop_file_list,
Gdk.DragAction action) {
if (drop_target.is_folder ()) {
Marlin.FileOperations.copy_move (drop_file_list,
null,
drop_target.get_target_location (),
action,
widget,
null,
null);
return true;
} else if (drop_target.is_executable ()) {
GLib.Error error;
if (!drop_target.execute (widget.get_screen (), drop_file_list, out error)) {
Eel.show_error_dialog (_("Failed to execute \"%s\"").printf (drop_target.get_display_name ()),
error.message,
null);
return false;
} else
return true;
}
return false;
}
public Gdk.DragAction? drag_drop_action_ask (Gtk.Widget dest_widget,
Gtk.ApplicationWindow win,
Gdk.DragAction possible_actions) {
this.chosen = Gdk.DragAction.DEFAULT;
add_action (win);
var ask_menu = build_menu (possible_actions);
ask_menu.set_screen (dest_widget.get_screen ());
ask_menu.show_all ();
var loop = new GLib.MainLoop (null, false);
ask_menu.deactivate.connect (() => {
if (loop.is_running ())
loop.quit ();
remove_action (win);
});
ask_menu.popup (null, null, null, 0, Gdk.CURRENT_TIME);
loop.run ();
Gtk.grab_remove (ask_menu);
return this.chosen;
}
private void add_action (Gtk.ApplicationWindow win) {
var action = new GLib.SimpleAction ("choice", GLib.VariantType.STRING);
action.activate.connect (this.on_choice);
win.add_action (action);
}
private void remove_action (Gtk.ApplicationWindow win) {
win.remove_action ("choice");
}
private Gtk.Menu build_menu (Gdk.DragAction possible_actions) {
var menu = new Gtk.Menu ();
build_and_append_menu_item (menu, _("Move Here"), Gdk.DragAction.MOVE, possible_actions);
build_and_append_menu_item (menu, _("Copy Here"), Gdk.DragAction.COPY, possible_actions);
build_and_append_menu_item (menu, _("Link Here"), Gdk.DragAction.LINK, possible_actions);
menu.append (new Gtk.SeparatorMenuItem ());
menu.append (new Gtk.MenuItem.with_label (_("Cancel")));
return menu;
}
private void build_and_append_menu_item (Gtk.Menu menu, string label, Gdk.DragAction? action, Gdk.DragAction possible_actions) {
if ((possible_actions & action) != 0) {
var item = new Gtk.MenuItem.with_label (label);
item.activate.connect (() => {
this.chosen = action;
});
menu.append (item);
}
}
public void on_choice (GLib.Variant? param) {
if (param == null || !param.is_of_type (GLib.VariantType.STRING)) {
critical ("Invalid variant type in DndHandler Menu");
return;
}
string choice = param.get_string ();
switch (choice) {
case "move":
this.chosen = Gdk.DragAction.MOVE;
break;
case "copy":
this.chosen = Gdk.DragAction.COPY;
break;
case "link":
this.chosen = Gdk.DragAction.LINK;
break;
case "background": /* not implemented yet */
case "cancel":
default:
this.chosen = Gdk.DragAction.DEFAULT;
break;
}
}
public string? get_source_filename (Gdk.DragContext context) {
uchar []? data = null;
Gdk.Atom property_name = Gdk.Atom.intern_static_string ("XdndDirectSave0");
Gdk.Atom property_type = Gdk.Atom.intern_static_string ("text/plain");
bool exists = Gdk.property_get (context.get_source_window (),
property_name,
property_type,
0, /* offset into property to start getting */
1024, /* max bytes of data to retrieve */
0, /* do not delete after retrieving */
null, null, /* actual property type and format got disregarded */
out data
);
if (exists && data != null) {
string name = DndHandler.data_to_string (data);
if (GLib.Path.DIR_SEPARATOR.to_string () in name) {
warning ("invalid source filename");
return null; /* not a valid filename */
} else
return name;
} else {
warning ("source file does not exist");
return null;
}
}
public void set_source_uri (Gdk.DragContext context, string uri) {
debug ("DNDHANDLER: set source uri to %s", uri);
Gdk.Atom property_name = Gdk.Atom.intern_static_string ("XdndDirectSave0");
Gdk.Atom property_type = Gdk.Atom.intern_static_string ("text/plain");
Gdk.property_change (context.get_source_window (),
property_name,
property_type,
8,
Gdk.PropMode.REPLACE,
uri.data,
uri.length);
}
public bool handle_xdnddirectsave (Gdk.DragContext context,
GOF.File drop_target,
Gtk.SelectionData selection) {
bool success = false;
if (selection.get_length () == 1 && selection.get_format () == 8) {
uchar result = selection.get_data ()[0];
switch (result) {
case 'F':
/* No fallback for XdndDirectSave stage (3), result "F" ("Failed") yet */
break;
case 'E':
/* No fallback for XdndDirectSave stage (3), result "E" ("Error") yet.
* Note this result may be obtained even if the file was successfully saved */
break;
case 'S':
/* XdndDirectSave "Success" */
success = true;
break;
default:
warning ("Unhandled XdndDirectSave result %s", result.to_string ());
break;
}
}
if (!success)
set_source_uri (context, "");
return success;
}
public bool handle_netscape_url (Gdk.DragContext context, GOF.File drop_target, Gtk.SelectionData selection) {
string [] parts = (selection.get_text ()).split ("\n");
/* _NETSCAPE_URL looks like this: "$URL\n$TITLE" - should be 2 parts */
if (parts.length != 2)
return false;
/* NETSCAPE URLs are not currently handled. No current bug reports */
return false;
}
public bool handle_file_drag_actions (Gtk.Widget dest_widget,
Gtk.ApplicationWindow win,
Gdk.DragContext context,
GOF.File drop_target,
GLib.List<GLib.File> drop_file_list,
Gdk.DragAction possible_actions,
Gdk.DragAction suggested_action,
uint32 timestamp) {
bool success = false;
Gdk.DragAction action = suggested_action;
if ((possible_actions & Gdk.DragAction.ASK) != 0)
action = drag_drop_action_ask (dest_widget, win, possible_actions);
if (action != Gdk.DragAction.DEFAULT) {
success = dnd_perform (dest_widget,
drop_target,
drop_file_list,
action);
}
return success;
}
public static bool selection_data_is_uri_list (Gtk.SelectionData selection_data, uint info, out string? text) {
text = null;
if (info == Marlin.TargetType.TEXT_URI_LIST &&
selection_data.get_format () == 8 &&
selection_data.get_length () > 0) {
text = DndHandler.data_to_string (selection_data.get_data_with_length ());
}
debug ("DNDHANDLER selection data is uri list returning %s", (text != null).to_string ());
return (text != null);
}
public static string data_to_string (uchar [] cdata) {
var sb = new StringBuilder ("");
foreach (uchar u in cdata)
sb.append_c ((char)u);
return sb.str;
}
public static void set_selection_data_from_file_list (Gtk.SelectionData selection_data,
GLib.List<GOF.File> file_list,
string? prefix = "") {
GLib.StringBuilder sb = new GLib.StringBuilder (prefix);
bool in_recent = file_list.data.is_recent_uri_scheme ();
file_list.@foreach ((file) => {
var target = in_recent ? file.get_display_target_uri () : file.get_target_location ().get_uri ();
sb.append (target);
sb.append ("\r\n"); /* Drop onto Filezilla does not work without the "\r" */
});
selection_data.@set (selection_data.get_target (),
8,
sb.data);
}
}
}
|