-
-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathDndHandler.vala
More file actions
486 lines (400 loc) · 20.5 KB
/
DndHandler.vala
File metadata and controls
486 lines (400 loc) · 20.5 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/***
Copyright (c) 2015-2018 elementary LLC <https://elementary.io>
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 <jeremywootten@gmail.com>
***/
namespace Files {
public class DndHandler : GLib.Object {
Gdk.DragAction chosen = Gdk.DragAction.DEFAULT;
public DndHandler () {}
public bool dnd_perform (Gtk.Widget widget,
Files.File drop_target,
GLib.List<GLib.File> drop_file_list,
Gdk.DragAction action)
requires (drop_target != null && drop_file_list != null) {
if (drop_target.is_folder ()) {
Files.FileOperations.copy_move_link.begin (
drop_file_list,
drop_target.get_target_location (),
action,
widget,
null
);
return true;
} else if (drop_target.is_executable ()) {
try {
drop_target.execute (drop_file_list);
return true;
} catch (Error e) {
unowned string target_name = drop_target.get_display_name ();
PF.Dialogs.show_error_dialog (_("Failed to execute \"%s\"").printf (target_name),
e.message,
null);
return false;
}
}
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 ((Gtk.ApplicationWindow)win);
});
ask_menu.popup_at_pointer (null);
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.Window source_window) {
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 (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.Window source_window, 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 (source_window,
property_name,
property_type,
8,
Gdk.PropMode.REPLACE,
uri.data,
uri.length);
}
public bool handle_xdnddirectsave (Gdk.Window source_window,
Files.File drop_target,
Gtk.SelectionData selection) {
bool success = false;
if (selection != null &&
selection.get_length () == 1 && //No other way to get length?
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 */
success = true;
break;
case 'S':
/* XdndDirectSave "Success" */
success = true;
break;
default:
warning ("Unhandled XdndDirectSave result %s", result.to_string ());
break;
}
}
if (!success) {
set_source_uri (source_window, "");
}
return success;
}
public bool handle_netscape_url (Gdk.Window source_window, Files.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,
Files.File drop_target,
GLib.List<GLib.File> drop_file_list,
Gdk.DragAction possible_actions,
Gdk.DragAction suggested_action,
Gtk.ApplicationWindow win,
uint32 timestamp) {
bool success = false;
Gdk.DragAction action = suggested_action;
if (drop_file_list != null) {
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);
}
} else {
critical ("Attempt to drop null file list");
}
return success;
}
public static bool selection_data_is_uri_list (Gtk.SelectionData selection_data, uint info, out string? text) {
text = null;
if (info == Files.TargetType.TEXT_URI_LIST &&
selection_data != null &&
selection_data.get_length () > 0 && //No other way to get length?
selection_data.get_format () == 8) {
/* selection_data.get_data () does not work for some reason (returns nothing) */
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;
}
// Used when TargetType is TEXT_URI_LIST
public static void set_selection_data_as_file_list (Gtk.SelectionData selection_data,
GLib.List<Files.File> file_list,
string prefix = "") {
GLib.StringBuilder sb = new GLib.StringBuilder (prefix);
set_stringbuilder_from_file_list (
sb,
file_list,
TargetType.TEXT_URI_LIST
);
selection_data.@set (selection_data.get_target (),
8,
sb.data);
}
// Used when TargetType is STRING
public static void set_selection_data_as_text (Gtk.SelectionData selection_data,
GLib.List<Files.File> file_list,
string prefix = "") {
GLib.StringBuilder sb = new GLib.StringBuilder (prefix);
set_stringbuilder_from_file_list (
sb,
file_list,
TargetType.STRING
);
sb.truncate (sb.len - 2); /* Do not want "\r\n" at end when pasting into text*/
selection_data.set_text (sb.str, (int)(sb.len));
}
private static void set_stringbuilder_from_file_list (GLib.StringBuilder sb,
GLib.List<Files.File> file_list,
TargetType type) {
if (file_list != null && file_list.data != null && file_list.data is Files.File) {
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 ();
if (type == TargetType.STRING) {
target = FileUtils.sanitize_path (target, null, false);
}
//Leave it to recipient to quote if required when receiving a uri list by DnD
//Terminal app applies quotes for uris but not text entries for example
sb.append (target);
sb.append ("\r\n"); /* Drop onto Filezilla does not work without the "\r" */
});
} else {
warning ("Invalid file list for drag and drop ignored");
}
}
public static Gdk.DragAction file_accepts_drop (Files.File dest,
GLib.List<GLib.File> drop_file_list, // read-only
Gdk.DragAction selected_action,
Gdk.DragAction possible_actions,
out Gdk.DragAction suggested_action_return) {
warning ("checking file accepts drop");
var actions = possible_actions;
var suggested_action = selected_action;
var target_location = dest.get_target_location ();
suggested_action_return = Gdk.DragAction.PRIVATE;
if (drop_file_list == null || drop_file_list.data == null) {
warning ("drop file list empty - return DEFAULT");
return Gdk.DragAction.DEFAULT;
}
if (dest.is_folder ()) {
if (!dest.is_writable ()) {
warning ("Folder not writable - return DEFAULT");
actions = Gdk.DragAction.DEFAULT;
} else {
warning ("target is writable folder - check valid actions");
/* Modify actions and suggested_action according to source files */
actions &= valid_actions_for_file_list (target_location,
drop_file_list,
ref suggested_action);
}
} else if (dest.is_executable ()) {
warning ("target is executable file - actions |= COPY|MOVE|LINK|PRIVATE");
actions |= (Gdk.DragAction.COPY |
Gdk.DragAction.MOVE |
Gdk.DragAction.LINK |
Gdk.DragAction.PRIVATE);
} else {
warning ("target is not a valid DnD target - not folder and not executable");
actions = Gdk.DragAction.DEFAULT;
}
if (actions == Gdk.DragAction.DEFAULT) { // No point asking if no other valid actions
warning ("Cannot accept drop - return now");
return Gdk.DragAction.DEFAULT;
} else if (FileUtils.location_is_in_trash (target_location)) { // cannot copy or link to trash
warning ("target is trash - cannot copy or link");
actions &= ~(Gdk.DragAction.COPY | Gdk.DragAction.LINK);
}
if (suggested_action in actions) {
suggested_action_return = suggested_action;
} else if (Gdk.DragAction.ASK in actions) {
suggested_action_return = Gdk.DragAction.ASK;
} else if (Gdk.DragAction.COPY in actions) {
suggested_action_return = Gdk.DragAction.COPY;
} else if (Gdk.DragAction.LINK in actions) {
suggested_action_return = Gdk.DragAction.LINK;
} else if (Gdk.DragAction.MOVE in actions) {
suggested_action_return = Gdk.DragAction.MOVE;
}
warning ("actions - %s, suggested - %s", actions.to_string (), suggested_action_return.to_string ());
return actions;
}
private const uint MAX_FILES_CHECKED = 100; // Max checked copied from gof_file.c version
private static Gdk.DragAction valid_actions_for_file_list (GLib.File target_location,
GLib.List<GLib.File> drop_file_list,
ref Gdk.DragAction suggested_action) {
warning ("checking valid actions for file list");
var valid_actions = Gdk.DragAction.DEFAULT |
Gdk.DragAction.COPY |
Gdk.DragAction.MOVE |
Gdk.DragAction.LINK;
/* Check the first MAX_FILES_CHECKED and let
* the operation fail for file the same as target if it is
* buried in a large selection. We can normally assume that all source files
* come from the same folder, but drops from outside Files could be from multiple
* folders. The valid actions are the lowest common denominator.
*/
uint count = 0;
bool from_trash = false;
foreach (var drop_file in drop_file_list) {
if (FileUtils.location_is_in_trash (drop_file)) {
from_trash = true;
if (FileUtils.location_is_in_trash (target_location)) {
warning ("file in trash - cannot DnD");
valid_actions = Gdk.DragAction.DEFAULT; // No DnD within trash
}
}
var parent = drop_file.get_parent ();
if (parent != null && parent.equal (target_location)) {
warning ("file in destination - only LINK");
valid_actions &= Gdk.DragAction.LINK; // Only LINK is valid
}
var scheme = drop_file.get_uri_scheme ();
if (scheme == null || !scheme.has_prefix ("file")) {
warning ("file not local - cannot LINK");
valid_actions &= ~(Gdk.DragAction.LINK); // Can only LINK local files
}
if (++count > MAX_FILES_CHECKED ||
valid_actions == Gdk.DragAction.DEFAULT) {
warning ("No valid action for file - stop checking files");
break;
}
}
/* Modify Gtk suggested COPY action to MOVE if source is trash or dest is in
* same filesystem and if MOVE is a valid action. We assume that it is not possible
* to drop files both from remote and local filesystems simultaneously
*/
if ((Gdk.DragAction.COPY in valid_actions && Gdk.DragAction.MOVE in valid_actions) &&
suggested_action == Gdk.DragAction.COPY &&
(from_trash || FileUtils.same_file_system (drop_file_list.first ().data, target_location))) {
warning ("Same filesystem - suggesting MOVE");
suggested_action = Gdk.DragAction.MOVE;
}
if (valid_actions != Gdk.DragAction.DEFAULT) {
valid_actions |= Gdk.DragAction.ASK; // Allow ASK if there is a possible action
}
warning ("valid actions - %s", valid_actions.to_string ());
return valid_actions;
}
}
}