| 1 |
/* |
| 2 |
* NOTE: This is an adaptation of the ClipboardManager class from elementary Files project. |
| 3 |
* |
| 4 |
* Copyright (c) 2016 elementary LLC (http://launchpad.net/elementary) |
| 5 |
* |
| 6 |
* Based on C code imported from Thunar |
| 7 |
* Copyright (c) 2005-2006 Benedikt Meurer <[email protected]> |
| 8 |
* Copyright (c) 2009 Jannis Pohlmann <[email protected]>* |
| 9 |
* Copyright (c) 2017-2019 José Amuedo (https://github.com/spheras) |
| 10 |
* |
| 11 |
* This program is free software: you can redistribute it and/or modify |
| 12 |
* it under the terms of the GNU General Public License as published by |
| 13 |
* the Free Software Foundation, either version 3 of the License, or |
| 14 |
* (at your option) any later version. |
| 15 |
* |
| 16 |
* This program is distributed in the hope that it will be useful, |
| 17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
* GNU General Public License for more details. |
| 20 |
* |
| 21 |
* You should have received a copy of the GNU General Public License |
| 22 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 |
* |
| 24 |
* Author(s): Jeremy Wootten <[email protected]> |
| 25 |
* |
| 26 |
*/ |
| 27 |
|
| 28 |
namespace DesktopFolder.Clipboard { |
| 29 |
|
| 30 |
public class ClipboardManager : GLib.Object { |
| 31 |
private enum ClipboardTarget { |
| 32 |
GNOME_COPIED_FILES, |
| 33 |
UTF8_STRING |
| 34 |
} |
| 35 |
|
| 36 |
private static GLib.Quark marlin_clipboard_manager_quark = GLib.Quark.from_string ("marlin-clipboard-manager"); |
| 37 |
private static Gdk.Atom x_special_gnome_copied_files = Gdk.Atom.intern_static_string ("x-special/gnome-copied-files"); |
| 38 |
private const Gtk.TargetEntry[] clipboard_targets = { |
| 39 |
{ "x-special/gnome-copied-files", 0, ClipboardTarget.GNOME_COPIED_FILES }, |
| 40 |
{ "UTF8_STRING", 0, ClipboardTarget.UTF8_STRING } |
| 41 |
}; |
| 42 |
|
| 43 |
private Gtk.Clipboard clipboard; |
| 44 |
private GLib.List <ClipboardFile> files = null; |
| 45 |
|
| 46 |
private bool files_cutted = false; |
| 47 |
|
| 48 |
/** Returns TRUE if the contents of the clipboard can be pasted into a folder. |
| 49 |
**/ |
| 50 |
public bool can_paste { get; private set; default = false; } |
| 51 |
|
| 52 |
public signal void changed (); |
| 53 |
|
| 54 |
private ClipboardManager (Gtk.Clipboard _clipboard) { |
| 55 |
clipboard = _clipboard; |
| 56 |
clipboard.set_qdata (marlin_clipboard_manager_quark, this); |
| 57 |
|
| 58 |
clipboard.owner_change.connect (owner_changed); |
| 59 |
} |
| 60 |
|
| 61 |
public static ClipboardManager ? get_for_display (Gdk.Display ? display = Gdk.Display.get_default ()) { |
| 62 |
if (display == null) { |
| 63 |
critical ("ClipboardManager cannot find display"); |
| 64 |
assert_not_reached (); |
| 65 |
} |
| 66 |
|
| 67 |
var clipboard = Gtk.Clipboard.get_for_display (display, Gdk.SELECTION_CLIPBOARD); |
| 68 |
ClipboardManager ? manager = clipboard.get_qdata (marlin_clipboard_manager_quark); |
| 69 |
|
| 70 |
if (manager != null) { |
| 71 |
return manager; |
| 72 |
} else { |
| 73 |
return new ClipboardManager (clipboard); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
~ClipboardManager () { |
| 78 |
release_pending_files (); |
| 79 |
clipboard.owner_change.disconnect (owner_changed); |
| 80 |
} |
| 81 |
|
| 82 |
/** If @file is null, returns whether there are ANY cut files |
| 83 |
* otherwise whether @file is amongst the cut files |
| 84 |
**/ |
| 85 |
public bool has_cutted_file (ClipboardFile ? file) { |
| 86 |
// debug("has_cutted_file!!!"); |
| 87 |
return files_cutted && (file == null || has_file (file)); |
| 88 |
} |
| 89 |
|
| 90 |
public bool has_file (ClipboardFile file) { |
| 91 |
// debug("has_file!!!"); |
| 92 |
return files != null && (files.find (file) != null); |
| 93 |
} |
| 94 |
|
| 95 |
public void copy_files (GLib.List <ClipboardFile> files) { |
| 96 |
// debug("copy_files!!!"); |
| 97 |
transfer_files (true, files); |
| 98 |
} |
| 99 |
|
| 100 |
public void cut_files (GLib.List <ClipboardFile> files) { |
| 101 |
// debug("cut_files!!!"); |
| 102 |
transfer_files (false, files); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* @target_file : the #GFile of the folder to which the contents on the clipboard |
| 107 |
* should be pasted. |
| 108 |
* @widget : a #GtkWidget, on which to perform the paste or %NULL if no widget is |
| 109 |
* known. |
| 110 |
* @new_files_callback : a #GCallback to connect to the job's "new-files" signal, |
| 111 |
* which will be emitted when the job finishes with the |
| 112 |
* list of #GFile<!---->s created by the job, or |
| 113 |
* %NULL if you're not interested in the signal. |
| 114 |
* |
| 115 |
* Pastes the contents from the clipboard to the directory |
| 116 |
* referenced by @target_file. |
| 117 |
**/ |
| 118 |
public void paste_files (GLib.File target_file, |
| 119 |
Gtk.Widget ? widget = null, |
| 120 |
GLib.Callback ? new_files_callback = null) { |
| 121 |
|
| 122 |
// debug("paste_files!!!"); |
| 123 |
/** |
| 124 |
* @cb the clipboard. |
| 125 |
* @sd selection_data returned from the clipboard. |
| 126 |
**/ |
| 127 |
clipboard.request_contents (x_special_gnome_copied_files, (cb, sd) => { |
| 128 |
contents_received (sd, target_file, widget, new_files_callback); |
| 129 |
}); |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
private void contents_received (Gtk.SelectionData sd, |
| 134 |
GLib.File target_file, |
| 135 |
Gtk.Widget ? widget = null, |
| 136 |
GLib.Callback ? new_files_callback = null) { |
| 137 |
|
| 138 |
// debug("contents_received!!!"); |
| 139 |
|
| 140 |
/* check whether the retrieval worked */ |
| 141 |
string ? text; |
| 142 |
|
| 143 |
if (!DragnDrop.DndHandler.selection_data_is_uri_list (sd, DragnDrop.TargetType.TEXT_URI_LIST, out text)) { |
| 144 |
warning ("Selection data not uri_list in Marlin.ClipboardManager contents_received"); |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
if (text == null) { |
| 149 |
warning ("Empty selection data in Marlin.ClipboardManager contents_received"); |
| 150 |
DesktopFolder.Util.show_error_dialog (null, DesktopFolder.Lang.CLIPBOARD_EMPTY); |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
Gdk.DragAction action = 0; |
| 155 |
if (text.has_prefix ("copy")) { |
| 156 |
action = Gdk.DragAction.COPY; |
| 157 |
text = text.substring (4); |
| 158 |
} else if (text.has_prefix ("cut")) { |
| 159 |
action = Gdk.DragAction.MOVE; |
| 160 |
text = text.substring (3); |
| 161 |
} else { |
| 162 |
warning ("Invalid selection data in Marlin.ClipboardManager contents_received"); |
| 163 |
DesktopFolder.Util.show_error_dialog (null, DesktopFolder.Lang.CLIPBOARD_EMPTY); |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
List <File> file_list = DragnDrop.Util.list_new_from_string (text); |
| 168 |
|
| 169 |
if (file_list != null) { |
| 170 |
DragnDrop.Util.copy_move (file_list, |
| 171 |
target_file, |
| 172 |
action, |
| 173 |
widget, |
| 174 |
new_files_callback, |
| 175 |
widget); |
| 176 |
} |
| 177 |
|
| 178 |
/* clear the clipboard if it contained "cutted data" |
| 179 |
* (gtk_clipboard_clear takes care of not clearing |
| 180 |
* the selection if we don't own it) |
| 181 |
*/ |
| 182 |
if (action != Gdk.DragAction.COPY) { |
| 183 |
clipboard.clear (); |
| 184 |
} |
| 185 |
/* check the contents of the clipboard again if either the Xserver or |
| 186 |
* our GTK+ version doesn't support the XFixes extension */ |
| 187 |
if (!clipboard.get_display ().supports_selection_notification ()) { |
| 188 |
owner_changed (null); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
private void owner_changed (Gdk.Event ? owner_change_event) { |
| 193 |
// debug("owner_changed!!!"); |
| 194 |
clipboard.request_contents (Gdk.Atom.intern_static_string ("TARGETS"), (cb, sd) => { |
| 195 |
can_paste = false; |
| 196 |
Gdk.Atom[] targets = null; |
| 197 |
|
| 198 |
sd.get_targets (out targets); |
| 199 |
foreach (var target in targets) { |
| 200 |
if (target == x_special_gnome_copied_files) { |
| 201 |
can_paste = true; |
| 202 |
break; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
/* notify listeners that we have a new clipboard state */ |
| 207 |
changed (); |
| 208 |
notify_property ("can-paste"); |
| 209 |
}); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Sets the clipboard to contain @files_for_transfer and marks them to be copied |
| 214 |
* or moved according to @copy when the user pastes from the clipboard. |
| 215 |
**/ |
| 216 |
private void transfer_files (bool copy, GLib.List <ClipboardFile> files_for_transfer) { |
| 217 |
// debug("transfer_files!!!"); |
| 218 |
release_pending_files (); |
| 219 |
files_cutted = !copy; |
| 220 |
|
| 221 |
/* setup the new file list */ |
| 222 |
foreach (var file in files_for_transfer) { |
| 223 |
files.prepend (file); |
| 224 |
file.on_delete.connect (on_file_destroyed); |
| 225 |
} |
| 226 |
|
| 227 |
/* acquire the Clipboard ownership */ |
| 228 |
clipboard.set_with_owner (clipboard_targets, get_callback, clear_callback, this); |
| 229 |
|
| 230 |
/* Need to fake a "owner-change" event here if the Xserver doesn't support clipboard notification */ |
| 231 |
if (!clipboard.get_display ().supports_selection_notification ()) { |
| 232 |
owner_changed (null); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
private void on_file_destroyed (ClipboardFile file) { |
| 237 |
// debug("on_file_destroyed!!!"); |
| 238 |
file.on_delete.disconnect (on_file_destroyed); |
| 239 |
files.remove (file); |
| 240 |
} |
| 241 |
|
| 242 |
public static void get_callback (Gtk.Clipboard cb, Gtk.SelectionData sd, uint target_info, void * parent) { |
| 243 |
// debug("get_callback!!!"); |
| 244 |
var manager = parent as ClipboardManager; |
| 245 |
if (manager == null || manager.clipboard != cb) { |
| 246 |
return; |
| 247 |
} |
| 248 |
|
| 249 |
switch (target_info) { |
| 250 |
case ClipboardTarget.GNOME_COPIED_FILES: |
| 251 |
string prefix = manager.files_cutted ? "cut" : "copy"; |
| 252 |
DragnDrop.DndHandler.set_selection_data_from_file_list (sd, |
| 253 |
manager.files, |
| 254 |
prefix); |
| 255 |
break; |
| 256 |
case ClipboardTarget.UTF8_STRING: /* Not clear what this is for */ |
| 257 |
var str = manager.file_list_to_string (); |
| 258 |
sd.set_text (str, str.length); |
| 259 |
break; |
| 260 |
default: |
| 261 |
assert_not_reached (); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
private string file_list_to_string () { |
| 266 |
// debug("file_list_to_string!!!"); |
| 267 |
var sb = new StringBuilder (""); |
| 268 |
uint count = 0; |
| 269 |
uint file_count = files.length (); |
| 270 |
foreach (var file in files) { |
| 271 |
var loc = file.get_target_location (); |
| 272 |
var pn = loc.get_parse_name (); |
| 273 |
if (pn != null) { |
| 274 |
sb.append (pn); |
| 275 |
} else { |
| 276 |
sb.append (loc.get_uri ()); |
| 277 |
} |
| 278 |
|
| 279 |
if (count < file_count) { |
| 280 |
sb.append ("\n"); |
| 281 |
} |
| 282 |
count++; |
| 283 |
} |
| 284 |
return sb.str; |
| 285 |
} |
| 286 |
|
| 287 |
public static void clear_callback (Gtk.Clipboard cb, void * parent) { |
| 288 |
// debug("clear_callback!!!"); |
| 289 |
var manager = (ClipboardManager) parent; |
| 290 |
if (manager == null || manager.clipboard != cb) { |
| 291 |
return; |
| 292 |
} |
| 293 |
|
| 294 |
manager.release_pending_files (); |
| 295 |
} |
| 296 |
|
| 297 |
private void release_pending_files () { |
| 298 |
// debug("release_pending_files!!!"); |
| 299 |
foreach (var file in this.files) { |
| 300 |
file.on_delete.disconnect (on_file_destroyed); |
| 301 |
} |
| 302 |
|
| 303 |
files = null; |
| 304 |
} |
| 305 |
|
| 306 |
} |
| 307 |
} |