Debian Codegraph @

src/utils/dragndrop/DndBehaviour.vala

raw
1 /*
2 * Copyright (c) 2017-2019 José Amuedo (https://github.com/spheras)
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 namespace DesktopFolder.DragnDrop {
19
20 public class DndBehaviour {
21 private bool drop_occurred = false; /* whether the data was dropped */
22 private bool drop_data_ready = false; /* whether the drop data was received already */
23 private bool drag_has_begun = false;
24 /* NEVER USED
25 private bool dnd_disabled = false;
26 */
27
28 private unowned GLib.List <GLib.File> drag_file_list = null;
29 private GLib.List <GLib.File> drop_file_list = null; /* the list of URIs that are contained in the drop data */
30 private GLib.List <GLib.File> selected_files = null;
31 private Gdk.DragAction current_suggested_action = Gdk.DragAction.DEFAULT;
32 private Gdk.DragAction current_actions = Gdk.DragAction.DEFAULT;
33 private bool draggable = false;
34 private bool droppable = false;
35 private DndView view = null;
36 private Gdk.Atom current_target_type = Gdk.Atom.NONE;
37 private GLib.File drop_target_file = null;
38
39 /* drag support */
40 uint drag_scroll_timer_id = 0;
41 uint drag_enter_timer_id = 0;
42 /* NEVER USED
43 uint drag_timer_id = 0;
44 int drag_x = 0;
45 int drag_y = 0;
46 int drag_button;
47 */
48 protected int drag_delay = 300;
49 protected int drag_enter_delay = 1000;
50 protected bool should_activate = false;
51
52 public DndBehaviour (DndView view, bool draggable, bool droppable) {
53 this.view = view;
54 this.draggable = draggable;
55 this.droppable = droppable;
56 this.connect_drag_drop_signals ();
57 }
58
59 /**
60 * @name connect_drag_drop_signals
61 * @description connect with the drag and drop signals a widget
62 */
63 protected void connect_drag_drop_signals () {
64 // debug("DndBehaviour-Connect_drag_drop_signals");
65 if (this.droppable) {
66 /* Set up as drop site */
67 Gtk.drag_dest_set (this.view.get_widget (), Gtk.DestDefaults.MOTION, drop_targets, Gdk.DragAction.ASK | file_drag_actions);
68 this.view.get_widget ().drag_drop.connect (on_drag_drop);
69 this.view.get_widget ().drag_data_received.connect (on_drag_data_received);
70 this.view.get_widget ().drag_leave.connect (on_drag_leave);
71 this.view.get_widget ().drag_motion.connect (on_drag_motion);
72 }
73
74 if (this.draggable) {
75 /* Set up as drag source */
76 Gtk.drag_source_set (this.view.get_widget (), Gdk.ModifierType.BUTTON1_MASK, drag_targets, file_drag_actions);
77 this.view.get_widget ().drag_begin.connect (on_drag_begin);
78 this.view.get_widget ().drag_data_get.connect (on_drag_data_get);
79 this.view.get_widget ().drag_data_delete.connect (on_drag_data_delete);
80 this.view.get_widget ().drag_end.connect (on_drag_end);
81 }
82 }
83
84 public void on_drag_begin (Gdk.DragContext context) {
85 // debug("DndBehaviour-on_drag_begin");
86 drag_has_begun = true;
87 should_activate = false;
88
89 // calculating the icon
90 int x = 10;
91 int y = 10;
92 Gtk.Image image = this.view.get_image ();
93 GLib.Icon icon;
94 Gtk.IconSize iconSize;
95 image.get_gicon (out icon, out iconSize);
96 if (icon != null) {
97 Gtk.drag_set_icon_gicon (context, icon, x, y);
98 } else {
99 string icon_name;
100 image.get_icon_name (out icon_name, out iconSize);
101 if (icon_name != null) {
102 Gtk.drag_set_icon_name (context, icon_name, x, y);
103 } else {
104 Gdk.Pixbuf pixbuf = image.get_pixbuf ();
105 if (pixbuf != null) {
106 Gtk.drag_set_icon_pixbuf (context, pixbuf, x, y);
107 } else {
108 Gtk.drag_set_icon_default (context);
109 }
110 }
111 }
112
113 }
114
115 private void on_drag_data_get (Gdk.DragContext context,
116 Gtk.SelectionData selection_data,
117 uint info,
118 uint timestamp) {
119
120 // debug("DndBehaviour-on_drag_data_get");
121 drag_file_list = get_selected_files_for_transfer ();
122
123 if (drag_file_list == null) {
124 return;
125 }
126
127 // lets drag
128 unowned GLib.List <DragnDrop.DndView> list_of_files = null;
129
130 DndView[] selected_views = this.view.get_all_selected_views ();
131 for (int i = 0; i < selected_views.length; i++) {
132 list_of_files.prepend (selected_views[i]);
133 }
134
135 DndHandler.set_selection_data_from_file_list_2 (selection_data, list_of_files);
136 }
137
138 private void on_drag_data_delete (Gdk.DragContext context) {
139 // debug("DndBehaviour-on_drag_data_delete");
140 /* block real_view default handler because handled in on_drag_end */
141 GLib.Signal.stop_emission_by_name (this.view.get_widget (), "drag-data-delete");
142 }
143
144 private void on_drag_end (Gdk.DragContext context) {
145 // debug("DndBehaviour-on_drag_end");
146 // debug("on_drag_end");
147 cancel_timeout (ref drag_scroll_timer_id);
148 drag_file_list = null;
149 drop_target_file = null;
150 drop_file_list = null;
151 drop_data_ready = false;
152
153 current_suggested_action = Gdk.DragAction.DEFAULT;
154 current_actions = Gdk.DragAction.DEFAULT;
155 drag_has_begun = false;
156 drop_occurred = false;
157 this.view.on_drag_end ();
158 }
159
160 public unowned GLib.List <File> get_selected_files () {
161 // debug("DndBehaviour-get_selected_files");
162 return selected_files;
163 }
164
165 protected unowned GLib.List <File> get_selected_files_for_transfer (GLib.List <File> selection = get_selected_files ()) {
166 // debug("DndBehaviour-get_selected_files_for_transfer");
167 unowned GLib.List <File> list = null;
168 list.prepend (this.view.get_file ());
169
170 return list;
171 }
172
173 /**
174 * @name on_drag_drop
175 * @description the drag_drop event captured. @see drag_drop signal
176 * @param DragContext context @see drag_drop signal
177 * @param int x @see drag_drop signal
178 * @param int y @see drag_drop signal
179 * @param uint timestamp @see drag_drop signal
180 * @return bool @see drag_drop signal
181 */
182 private bool on_drag_drop (Gdk.DragContext context,
183 int x,
184 int y,
185 uint timestamp) {
186 // debug("DndBehaviour-on_drag_drop");
187 Gtk.TargetList list = null;
188 string ? uri = null;
189 bool ok_to_drop = false;
190
191 Gdk.Atom target = Gtk.drag_dest_find_target (this.view.get_widget (), context, list);
192
193 if (target == Gdk.Atom.intern_static_string ("XdndDirectSave0")) {
194 File target_file = this.view.get_file_at (x, y);
195 if (target_file != null) {
196 /* get XdndDirectSave file name from DnD source window */
197 string ? filename = DndHandler.get_instance ().get_source_filename (context);
198 if (filename != null) {
199 /* Get uri of source file when dropped */
200 uri = target_file.resolve_relative_path (filename).get_uri ();
201 /* Setup the XdndDirectSave property on the source window */
202 DndHandler.get_instance ().set_source_uri (context, uri);
203 ok_to_drop = true;
204 } else {
205 DesktopFolder.Util.show_error_dialog (DesktopFolder.Lang.CANT_DROP, DesktopFolder.Lang.CANT_DROP_INVALID_FILE_NAME);
206 }
207 }
208 } else
209 ok_to_drop = (target != Gdk.Atom.NONE);
210
211 if (ok_to_drop) {
212 drop_occurred = true;
213 /* request the drag data from the source (initiates
214 * saving in case of XdndDirectSave).*/
215 Gtk.drag_get_data (this.view.get_widget (), context, target, timestamp);
216 }
217
218 return ok_to_drop;
219 }
220
221 /**
222 * @name on_drag_data_received
223 * @description the drag_data_received event. @see drag_drop signal
224 * @param DragContext context @see drag_drop signal
225 * @param x int @see drag_drop signal
226 * @param y int @see drag_drop signal
227 * @param selectionData selection-data @see drag_drop signal
228 * @param uint info @see drag_drop signal
229 * @param uint timestamp @see drag_drop signal
230 */
231 private void on_drag_data_received (Gdk.DragContext context,
232 int x,
233 int y,
234 Gtk.SelectionData selection_data,
235 uint info,
236 uint timestamp) {
237 // debug("DndBehaviour-on-drag_data_received");
238 bool success = false;
239
240 if (!drop_data_ready) {
241 /* We don't have the drop data - extract uri list from selection data */
242 string ? text;
243 if (DndHandler.selection_data_is_uri_list (selection_data, info, out text)) {
244 drop_file_list = Util.list_new_from_string (text);
245 drop_data_ready = true;
246 }
247 }
248
249 if (drop_occurred && drop_data_ready) {
250 drop_occurred = false;
251 if (current_actions != Gdk.DragAction.DEFAULT) {
252 switch (info) {
253 case TargetType.XDND_DIRECT_SAVE0:
254 success = DndHandler.get_instance ().handle_xdnddirectsave (context,
255 this.view,
256 selection_data);
257 break;
258
259 case TargetType.NETSCAPE_URL:
260 success = DndHandler.get_instance ().handle_netscape_url (context,
261 this.view,
262 selection_data);
263 break;
264
265 case TargetType.TEXT_URI_LIST:
266 if ((current_actions & file_drag_actions) != 0) {
267 success = DndHandler.get_instance ().handle_file_drag_actions (this.view.get_widget (),
268 this.view.get_application_window (),
269 context,
270 this.view,
271 drop_file_list,
272 current_actions,
273 current_suggested_action,
274 timestamp);
275 }
276 break;
277
278 default:
279 break;
280 }
281 }
282 Gtk.drag_finish (context, success, false, timestamp);
283 on_drag_leave (context, timestamp);
284 }
285 }
286
287 private void on_drag_leave (Gdk.DragContext context, uint timestamp) {
288 // debug("DndBehaviour-on_drag_leave");
289 drop_data_ready = false;
290 this.view.on_drag_leave ();
291 }
292
293 private bool on_drag_motion (Gdk.DragContext context,
294 int x,
295 int y,
296 uint timestamp) {
297
298 this.view.on_drag_motion ();
299
300 // debug("DndBehaviour-on_drag_motion");
301 /* if we don't have drop data already ... */
302 if (!drop_data_ready && !get_drop_data (context, x, y, timestamp))
303 return false;
304 else
305 /* We have the drop data - check whether we can drop here*/
306 check_destination_actions_and_target_file (context, x, y, timestamp);
307
308 /*if (drag_scroll_timer_id == 0)
309 start_drag_scroll_timer (context);
310 */
311
312
313 // depending on the modifier pressed, we will copy move or link
314 Gdk.Keymap keymap = Gdk.Keymap.get_for_display (Gdk.Display.get_default ());
315 uint modifiers = keymap.get_modifier_state ();
316 if ((modifiers & Gdk.ModifierType.CONTROL_MASK) > 0) {
317 // lets copy
318 current_suggested_action = Gdk.DragAction.COPY;
319 } else if ((modifiers & Gdk.ModifierType.SHIFT_MASK) > 0 ||
320 (modifiers & Gdk.ModifierType.MOD1_MASK) > 0) {
321 // lets link
322 current_suggested_action = Gdk.DragAction.LINK;
323 } else {
324 // lets move
325 current_suggested_action = Gdk.DragAction.MOVE;
326 }
327
328 Gdk.drag_status (context, current_suggested_action, timestamp);
329
330 return true;
331 }
332
333 private GLib.File get_drop_target_file (int win_x, int win_y) {
334 // debug("DndBehaviour-get_drop_target_file");
335 GLib.File file = null;
336
337 file = this.view.get_file_at (win_x, win_y);
338 FileType type = file.query_file_type (FileQueryInfoFlags.NOFOLLOW_SYMLINKS);
339 bool is_dir = type == FileType.DIRECTORY;
340
341 /* can only drop onto folders and executables */
342 if (!is_dir) { // TODO && !file.is_executable ()) {
343 file = null;
344 }
345 return file;
346 }
347
348 protected void cancel_timeout (ref uint id) {
349 // debug("DndBehaviour-cancel_timeout");
350 if (id > 0) {
351 GLib.Source.remove (id);
352 id = 0;
353 }
354 }
355
356 private bool is_valid_drop_folder (File file) {
357 // debug("DndBehaviour-is_valid_drop_folder");
358 /* Cannot drop onto a file onto its parent or onto itself */
359 if ( // file.get_uri() != slot.uri &&
360 drag_file_list != null &&
361 drag_file_list.index (file) < 0)
362
363 return true;
364 else
365 return false;
366 }
367
368 private void check_destination_actions_and_target_file (Gdk.DragContext context, int x, int y, uint timestamp) {
369 // debug("DndBehaviour-check_destination_actions_and_target_file");
370 File file = get_drop_target_file (x, y);
371 string uri = file != null ? file.get_uri () : "";
372 string current_uri = drop_target_file != null ? drop_target_file.get_uri () : "";
373
374 Gdk.drag_status (context, Gdk.DragAction.MOVE, timestamp);
375 if (uri != current_uri) {
376 cancel_timeout (ref drag_enter_timer_id);
377 drop_target_file = file;
378 current_actions = Gdk.DragAction.DEFAULT;
379 current_suggested_action = Gdk.DragAction.DEFAULT;
380
381 if (file != null) {
382 if (current_target_type == Gdk.Atom.intern_static_string ("XdndDirectSave0")) {
383 current_suggested_action = Gdk.DragAction.COPY;
384 current_actions = current_suggested_action;
385 } else {
386 current_actions = Util.file_accepts_drop (file, drop_file_list, context, out current_suggested_action);
387 }
388
389 // highlight_drop_file (drop_target_file, current_actions, path);
390
391 if (Util.is_folder (file) && is_valid_drop_folder (file)) {
392 /* open the target folder after a short delay */
393 drag_enter_timer_id = GLib.Timeout.add_full (GLib.Priority.LOW,
394 drag_enter_delay,
395 () => {
396 // load_location (file.get_target_location ());
397 drag_enter_timer_id = 0;
398 return false;
399 });
400 }
401 }
402 }
403 }
404
405 private bool get_drop_data (Gdk.DragContext context, int x, int y, uint timestamp) {
406 // debug("DndBehaviour-get_drop_data");
407 Gtk.TargetList ? list = null;
408 Gdk.Atom target = Gtk.drag_dest_find_target (this.view.get_widget (), context, list);
409 bool result = false;
410 current_target_type = target;
411 /* Check if we can handle it yet */
412 if (target == Gdk.Atom.intern_static_string ("XdndDirectSave0") ||
413 target == Gdk.Atom.intern_static_string ("_NETSCAPE_URL")) {
414
415 /* Determine file at current position (if any) */
416 DndView file = this.view;
417
418 if (file != null &&
419 file.is_folder () &&
420 file.is_writable ()) {
421 // icon_renderer.@set ("drop-file", file);
422 // highlight_path (path);
423 drop_data_ready = true;
424 result = true;
425 }
426 } else if (target != Gdk.Atom.NONE)
427 /* request the drag data from the source */
428 Gtk.drag_get_data (this.view.get_widget (), context, target, timestamp); /* emits "drag_data_received" */
429
430 return result;
431 }
432
433 }
434 }