Debian Codegraph @

src/utils/dragndrop/DndHandler.vala

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