File: DndHandler.vala

package info (click to toggle)
desktopfolder 1.0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 11,488 kB
  • sloc: xml: 207; sh: 9; makefile: 8; python: 6
file content (361 lines) | stat: -rw-r--r-- 14,253 bytes parent folder | download | duplicates (4)
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
/*
 *  Copyright (c) 2015-2017 elementary LLC (http://launchpad.net/elementary)
 *  NOTE: This is an adaptation of the DndHandler class from elementary Files project.
 *  Copyright (c) 2017-2019 José Amuedo (https://github.com/spheras)
 *
 *  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, either version 3 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, see <http://www.gnu.org/licenses/>.
 *
 *  Authors: Jeremy Wootten <jeremy@elementaryos.org>
 */

namespace DesktopFolder.DragnDrop {

    public class DndHandler : GLib.Object {
        Gdk.DragAction chosen = Gdk.DragAction.DEFAULT;

        /** singlenton instance */
        private static DndHandler dnd_handler = null;

        /**
         * @constructor
         * @description private constructor for singlenton
         */
        private DndHandler () {
        }

        /**
         * @name get_instance
         * @description singlenton pattern
         */
        public static DndHandler get_instance () {
            if (dnd_handler == null) {
                dnd_handler = new DndHandler ();
            }
            return dnd_handler;
        }

        public bool dnd_perform (Gtk.Widget widget,
            DndView                         drop_target,
            GLib.List <GLib.File>           drop_file_list,
            Gdk.DragAction                  action) {
            // debug("DndHandler-dnd_perform!!!");
            /*
               debug("size:%d",(int)drop_file_list.length());
               debug("file:"+drop_file_list.nth(0).data.get_basename());
             */

            if (drop_target.is_folder ()) {
                DesktopFolder.DragnDrop.Util.copy_move (drop_file_list,
                    drop_target.get_target_location (),
                    action,
                    widget,
                    null,
                    null);
                return true;
            }
            return false;
        }

        public Gdk.DragAction ? drag_drop_action_ask (Gtk.Widget dest_widget,
            Gtk.ApplicationWindow win,
            Gdk.DragAction possible_actions) {
            // debug("DndHandler-drag_drop_action_ask!!!");
            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_at_pointer (null);
            loop.run ();
            Gtk.grab_remove (ask_menu);

            return this.chosen;
        }

        private void add_action (Gtk.ApplicationWindow win) {
            // debug("DndHandler-add_action!!!");
            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) {
            // debug("DndHandler-remove_action!!!");
            win.remove_action ("choice");
        }

        private Gtk.Menu build_menu (Gdk.DragAction possible_actions) {
            // debug("DndHandler-build_menu!!!");
            var menu = new Gtk.Menu ();

            build_and_append_menu_item (menu, DesktopFolder.Lang.DROP_MENU_MOVE, Gdk.DragAction.MOVE, possible_actions);
            build_and_append_menu_item (menu, DesktopFolder.Lang.DROP_MENU_COPY, Gdk.DragAction.COPY, possible_actions);
            build_and_append_menu_item (menu, DesktopFolder.Lang.DROP_MENU_LINK, Gdk.DragAction.LINK, possible_actions);

            menu.append (new Gtk.SeparatorMenuItem ());
            menu.append (new Gtk.MenuItem.with_label (DesktopFolder.Lang.DROP_MENU_CANCEL));

            return menu;
        }

        private void build_and_append_menu_item (Gtk.Menu menu, string label, Gdk.DragAction ? action, Gdk.DragAction possible_actions) {
            // debug("DndHandler-build_and_append_menu_item!!!");
            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) {
            // debug("DndHandler-on_choice!!!");
            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) {
            // debug("DndHandler-get_source_filename!!!");
            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!!!");
            // 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,
            DragnDrop.DndView                              drop_target,
            Gtk.SelectionData                              selection) {
            // debug("DndHandler-handle_xdnddirectsave!!!");
            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, DragnDrop.DndView drop_target, Gtk.SelectionData selection) {
            // debug("DndHandler-handle_netscape_url!!!");
            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,
            DragnDrop.DndView                            drop_target,
            GLib.List <GLib.File>                        drop_file_list,
            Gdk.DragAction                               possible_actions,
            Gdk.DragAction                               suggested_action,
            uint32                                       timestamp) {

            // debug("DndHandler-handle_file_drag_actions!!!");
            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;

            // debug("DndHandler-selection_data_is_uri_list!!!");
            if (info == 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) {
            // debug("DndHandler-data_to_string!!!");
            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 <DndView>                                                 file_list,
            string                                                              prefix = "") {

            // debug("DndHandler-set_selection_data_from_file_list!!!");
            GLib.StringBuilder sb = new GLib.StringBuilder (prefix);

            if (file_list != null && file_list.data != null && file_list.data is DragnDrop.DndView) {
                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 ();
                    // debug("target->%s",target);
                    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");
            }

            selection_data.@set (selection_data.get_target (),
                8,
                sb.data);

        }

        public static void set_selection_data_from_file_list_2 (Gtk.SelectionData selection_data,
            GLib.List <DragnDrop.DndView>                                         file_list,
            string                                                                prefix = "") {

            // debug("DndHandler-set_selection_data_from_file_list!!!");
            GLib.StringBuilder sb = new GLib.StringBuilder (prefix);

            if (file_list != null && file_list.data != null && file_list.data is DragnDrop.DndView) {
                bool in_recent = true; // file_list.data.is_recent_uri_scheme ();

                file_list.@foreach ((file) => {
                    var target = in_recent ? Util.get_display_target_uri (file.get_file ()) : file.get_file ().get_uri ();
                    // debug("target->%s",target);
                    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");
            }

            selection_data.@set (selection_data.get_target (),
                8,
                sb.data);

        }

    }
}