| 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 |
/** |
| 19 |
* ATTENTION! MOST OF THIS CODE HAS BEEN TAKEN AND ADAPTED FROM ELEMENTARY OS FILES PROJECT. |
| 20 |
*/ |
| 21 |
namespace DesktopFolder.DragnDrop { |
| 22 |
|
| 23 |
public enum TargetType { |
| 24 |
STRING, |
| 25 |
TEXT_URI_LIST, |
| 26 |
XDND_DIRECT_SAVE0, |
| 27 |
NETSCAPE_URL |
| 28 |
} |
| 29 |
|
| 30 |
public const Gtk.TargetEntry[] drag_targets = { |
| 31 |
{ "text/plain", Gtk.TargetFlags.SAME_APP, DragnDrop.TargetType.STRING }, |
| 32 |
{ "text/uri-list", Gtk.TargetFlags.SAME_APP, DragnDrop.TargetType.TEXT_URI_LIST } |
| 33 |
}; |
| 34 |
|
| 35 |
public const Gtk.TargetEntry[] drop_targets = { |
| 36 |
{ "text/uri-list", Gtk.TargetFlags.SAME_APP, DragnDrop.TargetType.TEXT_URI_LIST }, |
| 37 |
{ "text/uri-list", Gtk.TargetFlags.OTHER_APP, DragnDrop.TargetType.TEXT_URI_LIST }, |
| 38 |
{ "XdndDirectSave0", Gtk.TargetFlags.OTHER_APP, DragnDrop.TargetType.XDND_DIRECT_SAVE0 }, |
| 39 |
{ "_NETSCAPE_URL", Gtk.TargetFlags.OTHER_APP, DragnDrop.TargetType.NETSCAPE_URL } |
| 40 |
}; |
| 41 |
|
| 42 |
public const Gdk.DragAction file_drag_actions = (Gdk.DragAction.COPY | Gdk.DragAction.MOVE | Gdk.DragAction.LINK); |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* A set of Util DragNDrop functions |
| 49 |
*/ |
| 50 |
namespace Util { |
| 51 |
|
| 52 |
/** |
| 53 |
* @name is_folder |
| 54 |
* @description check if a certain file is a folder or not |
| 55 |
* @param File file the file to check |
| 56 |
* @return boole true->yes, the file is a folder |
| 57 |
*/ |
| 58 |
public bool is_folder (File file) { |
| 59 |
// debug("Util-is_folder"); |
| 60 |
FileType type = file.query_file_type (FileQueryInfoFlags.NOFOLLOW_SYMLINKS); |
| 61 |
return type == FileType.DIRECTORY; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* @name is_tarshed |
| 66 |
* @description check whether the file is in trash or not |
| 67 |
* @return bool true->yes, it is in trash |
| 68 |
*/ |
| 69 |
public bool is_trashed (File file) { |
| 70 |
// debug("Util-is_trashed"); |
| 71 |
return file.has_uri_scheme ("trash"); |
| 72 |
} |
| 73 |
|
| 74 |
public string get_display_target_uri (File file) { |
| 75 |
// debug("Util-get_display_target_uri"); |
| 76 |
try { |
| 77 |
var fileInfo = file.query_info (FileAttribute.STANDARD_TARGET_URI, FileQueryInfoFlags.NOFOLLOW_SYMLINKS); |
| 78 |
var uri = fileInfo.get_attribute_as_string (FileAttribute.STANDARD_TARGET_URI); |
| 79 |
|
| 80 |
if (uri == null) { |
| 81 |
uri = "" + file.get_uri (); |
| 82 |
} |
| 83 |
|
| 84 |
return uri; |
| 85 |
} catch (Error error) { |
| 86 |
stderr.printf ("Error: %s\n", error.message); |
| 87 |
return ""; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
public bool same_filesystem (File file_a, File file_b) { |
| 92 |
// debug("Util-same_filesystem"); |
| 93 |
string filesystem_id_a; |
| 94 |
string filesystem_id_b; |
| 95 |
|
| 96 |
FileInfo ainfo = null; |
| 97 |
FileInfo binfo = null; |
| 98 |
try { |
| 99 |
ainfo = file_a.query_filesystem_info ("*", null); |
| 100 |
binfo = file_b.query_filesystem_info ("*", null); |
| 101 |
} catch (Error e) { |
| 102 |
stderr.printf ("Error: %s\n", e.message); |
| 103 |
} |
| 104 |
|
| 105 |
/* return false if we have no information about one of the files */ |
| 106 |
if (ainfo == null || binfo == null) |
| 107 |
return false; |
| 108 |
|
| 109 |
/* determine the filesystem IDs */ |
| 110 |
filesystem_id_a = ainfo.get_attribute_string (FileAttribute.ID_FILESYSTEM); |
| 111 |
|
| 112 |
filesystem_id_b = binfo.get_attribute_string (FileAttribute.ID_FILESYSTEM); |
| 113 |
|
| 114 |
/* compare the filesystem IDs */ |
| 115 |
return filesystem_id_a == filesystem_id_b; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* gof_file_accepts_drop (imported from thunar): |
| 120 |
* @file : a #GOFFile instance. |
| 121 |
* @file_list : the list of #GFile<!---->s that will be droppped. |
| 122 |
* @context : the current #GdkDragContext, which is used for the drop. |
| 123 |
* @suggested_action_return : return location for the suggested #GdkDragAction or %NULL. |
| 124 |
* |
| 125 |
* Checks whether @file can accept @path_list for the given @context and |
| 126 |
* returns the #GdkDragAction<!---->s that can be used or 0 if no actions |
| 127 |
* apply. |
| 128 |
* |
| 129 |
* If any #GdkDragAction<!---->s apply and @suggested_action_return is not |
| 130 |
* %NULL, the suggested #GdkDragAction for this drop will be stored to the |
| 131 |
* location pointed to by @suggested_action_return. |
| 132 |
* |
| 133 |
* Return value: the #GdkDragAction<!---->s supported for the drop or |
| 134 |
* 0 if no drop is possible. |
| 135 |
**/ |
| 136 |
|
| 137 |
Gdk.DragAction file_accepts_drop (File file, |
| 138 |
List <File> file_list, |
| 139 |
Gdk.DragContext context, |
| 140 |
out Gdk.DragAction suggested_action_return) { |
| 141 |
// debug("Util-file_accepts_drop"); |
| 142 |
Gdk.DragAction suggested_action; |
| 143 |
Gdk.DragAction actions; |
| 144 |
File ofile; |
| 145 |
File parent_file; |
| 146 |
uint n; |
| 147 |
int i = 0; |
| 148 |
suggested_action_return = Gdk.DragAction.PRIVATE; |
| 149 |
|
| 150 |
/* we can never drop an empty list */ |
| 151 |
if (file_list == null) |
| 152 |
return 0; |
| 153 |
|
| 154 |
/* default to whatever GTK+ thinks for the suggested action */ |
| 155 |
suggested_action = context.get_suggested_action (); |
| 156 |
|
| 157 |
/* check if we have a writable directory here or an executable file */ |
| 158 |
if (is_folder (file)) { // TODO && gof_file_is_writable (file)) |
| 159 |
// debug("Util-file_accepts_drop"); |
| 160 |
/* determine the possible actions */ |
| 161 |
actions = context.get_actions () & (Gdk.DragAction.COPY | Gdk.DragAction.MOVE |
| 162 |
| Gdk.DragAction.LINK | Gdk.DragAction.ASK); |
| 163 |
|
| 164 |
|
| 165 |
/* check up to 100 of the paths (just in case somebody tries to |
| 166 |
* drag around his music collection with 5000 files). |
| 167 |
*/ |
| 168 |
|
| 169 |
for (n = 0, i = 0; i < file_list.length () && n < 100; i++, n++) { // lp = file_list, n = 0; lp != NULL && n < 100; lp = lp->next, ++n) |
| 170 |
File fi = file_list.nth (i).data; |
| 171 |
/* we cannot drop a file on itself */ |
| 172 |
if (file.equal (fi)) |
| 173 |
return 0; |
| 174 |
|
| 175 |
/* check whether source and destination are the same */ |
| 176 |
parent_file = fi.get_parent (); |
| 177 |
if (parent_file != null) { |
| 178 |
if (file.equal (parent_file)) { |
| 179 |
// this should be forbidden, but I dont know why, it is being memorized in some way, |
| 180 |
// it is asked only once, and we cannot unset in the future.. |
| 181 |
// So, we allow copy/move/link and after, it is discarded if it was really the same folder. |
| 182 |
// TODO, need to be further investigated. |
| 183 |
parent_file.unref (); |
| 184 |
suggested_action = Gdk.DragAction.COPY; // Gdk.DragAction.ASK; |
| 185 |
actions = Gdk.DragAction.COPY | Gdk.DragAction.MOVE | Gdk.DragAction.LINK; // Gdk.DragAction.ASK|Gdk.DragAction.LINK; |
| 186 |
} else |
| 187 |
parent_file.unref (); |
| 188 |
} |
| 189 |
|
| 190 |
/* Make these tests at the end so that any changes are not reversed subsequently */ |
| 191 |
string scheme; |
| 192 |
scheme = fi.get_uri_scheme (); |
| 193 |
if (!scheme.has_prefix ("file")) { |
| 194 |
/* do not allow symbolic links from remote filesystems */ |
| 195 |
actions &= ~(Gdk.DragAction.LINK); |
| 196 |
} |
| 197 |
|
| 198 |
/* copy/move/link within the trash not possible */ |
| 199 |
if (is_trashed (fi) && is_trashed (file)) |
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
/* if the source offers both copy and move and the GTK+ suggested action is copy, try to |
| 204 |
* be smart telling whether we should copy or move by default by checking whether the |
| 205 |
* source and target are on the same disk. */ |
| 206 |
if ((actions & (Gdk.DragAction.COPY | Gdk.DragAction.MOVE)) != 0 |
| 207 |
&& (suggested_action == Gdk.DragAction.COPY)) { |
| 208 |
/* default to move as suggested action */ |
| 209 |
suggested_action = Gdk.DragAction.MOVE; |
| 210 |
|
| 211 |
/* check for up to 100 files, for the reason state above */ |
| 212 |
for (i = 0, n = 0; i < file_list.length () && n < 100; i++, n++) { // for (lp = file_list, n = 0; lp != NULL && n < 100; lp = lp->next, ++n) |
| 213 |
File fi = file_list.nth (i).data; |
| 214 |
/* dropping from the trash always suggests move */ |
| 215 |
if (is_trashed (fi)) |
| 216 |
break; |
| 217 |
|
| 218 |
/* determine the cached version of the source file */ |
| 219 |
ofile = fi; // gof_file_get(lp->data); |
| 220 |
FileInfo oinfo = null; |
| 221 |
try { |
| 222 |
oinfo = ofile.query_filesystem_info ("*", null); |
| 223 |
} catch (Error error) { |
| 224 |
stderr.printf ("Error: %s\n", error.message); |
| 225 |
} |
| 226 |
|
| 227 |
/* we have only move if we know the source and both the source and the target |
| 228 |
* are on the same disk, and the source file is owned by the current user. |
| 229 |
*/ |
| 230 |
if (ofile == null |
| 231 |
|| !same_filesystem (file, ofile) |
| 232 |
|| (oinfo != null)) { |
| 233 |
// && ofile->uid > -1 |
| 234 |
// && ofile->uid != effective_user_id )) |
| 235 |
/* default to copy and get outa here */ |
| 236 |
suggested_action = Gdk.DragAction.COPY; |
| 237 |
break; |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
} else if (!is_folder (file)) { // && gof_file_is_executable (file)) |
| 242 |
/* determine the possible actions */ |
| 243 |
actions = context.get_actions () & (Gdk.DragAction.COPY | Gdk.DragAction.MOVE | Gdk.DragAction.LINK |
| 244 |
| Gdk.DragAction.PRIVATE); |
| 245 |
} else { |
| 246 |
debug ("Not a valid drop target"); |
| 247 |
return 0; |
| 248 |
} |
| 249 |
|
| 250 |
/* Make these tests at the end so that any changes are not reversed subsequently */ |
| 251 |
string scheme; |
| 252 |
scheme = file.get_uri_scheme (); |
| 253 |
/* do not allow symbolic links to remote filesystems */ |
| 254 |
if (!scheme.has_prefix ("file")) |
| 255 |
actions &= ~(Gdk.DragAction.LINK); |
| 256 |
|
| 257 |
/* cannot create symbolic links in the trash or copy to the trash */ |
| 258 |
if (is_trashed (file)) |
| 259 |
actions &= ~(Gdk.DragAction.COPY | Gdk.DragAction.LINK); |
| 260 |
|
| 261 |
if (actions == Gdk.DragAction.ASK) { |
| 262 |
/* No point in asking if there are no allowed actions */ |
| 263 |
return 0; |
| 264 |
} |
| 265 |
|
| 266 |
/* determine the preferred action based on the context */ |
| 267 |
/* determine a working action */ |
| 268 |
if (actions != 0) { |
| 269 |
suggested_action_return = suggested_action; |
| 270 |
} else if ((actions & Gdk.DragAction.ASK) != 0) { |
| 271 |
suggested_action_return = Gdk.DragAction.ASK; |
| 272 |
} else if ((actions & Gdk.DragAction.COPY) != 0) { |
| 273 |
suggested_action_return = Gdk.DragAction.COPY; |
| 274 |
} else if ((actions & Gdk.DragAction.LINK) != 0) { |
| 275 |
suggested_action_return = Gdk.DragAction.LINK; |
| 276 |
} else if ((actions & Gdk.DragAction.MOVE) != 0) { |
| 277 |
suggested_action_return = Gdk.DragAction.MOVE; |
| 278 |
} else { |
| 279 |
suggested_action_return = Gdk.DragAction.PRIVATE; |
| 280 |
} |
| 281 |
/* yeppa, we can drop here */ |
| 282 |
return actions; |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* eel_g_file_list_new_from_string: |
| 287 |
* @string : a string representation of an URI list. |
| 288 |
* |
| 289 |
* Splits an URI list conforming to the text/uri-list |
| 290 |
* mime type defined in RFC 2483 into individual URIs, |
| 291 |
* discarding any comments and whitespace. The resulting |
| 292 |
* list will hold one #GFile for each URI. |
| 293 |
* |
| 294 |
* If @string contains no URIs, this function |
| 295 |
* will return %NULL. |
| 296 |
* |
| 297 |
* Return value: the list of #GFile<!---->s or %NULL. |
| 298 |
**/ |
| 299 |
public List <File> list_new_from_string (string str) { |
| 300 |
// debug("Util-list_new_from_string"); |
| 301 |
List <File> list = new List <File>(); |
| 302 |
string[] uris; |
| 303 |
uris = Uri.list_extract_uris (str); |
| 304 |
|
| 305 |
for (int n = 0; uris != null && uris[n] != null; ++n) { |
| 306 |
list.append (File.new_for_uri (uris[n])); |
| 307 |
} |
| 308 |
|
| 309 |
return list; |
| 310 |
} |
| 311 |
|
| 312 |
public void copy_move (List <File> files, |
| 313 |
File target_dir, |
| 314 |
Gdk.DragAction action, |
| 315 |
Gtk.Widget ? parent_view, |
| 316 |
GLib.Callback ? done_callback, |
| 317 |
Gtk.Widget ? done_callback_data) { |
| 318 |
|
| 319 |
if (target_dir.query_exists ()) { |
| 320 |
for (int i = 0; files != null && i < files.length (); i++) { |
| 321 |
File f = files.nth (i).data; |
| 322 |
if (f.query_exists ()) { |
| 323 |
|
| 324 |
string path = f.get_path (); |
| 325 |
string new_name = f.get_basename (); |
| 326 |
string target_path = target_dir.get_path (); |
| 327 |
// string link_to = DesktopFolder.Lang.LINK_TO; |
| 328 |
|
| 329 |
if ((path.has_prefix ("/usr/local/share/applications/") || |
| 330 |
path.has_prefix ("/usr/share/") || |
| 331 |
path.contains ("/.local/share/applications/")) && |
| 332 |
path.has_suffix (".desktop")) { |
| 333 |
// we don't move user desktop launchers |
| 334 |
if (action == Gdk.DragAction.MOVE) { |
| 335 |
action = Gdk.DragAction.COPY; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
debug (@"$action, $(f.get_parent ().get_path ()) -> $target_path"); |
| 340 |
|
| 341 |
if (action == Gdk.DragAction.COPY) { |
| 342 |
File final_target = File.new_for_path (target_path + "/" + f.get_basename ()); |
| 343 |
|
| 344 |
DesktopFolder.ProgressDialog pd = new DesktopFolder.ProgressDialog (DesktopFolder.Lang.DRAGNDROP_FILE_OPERATIONS, (Gtk.Window)parent_view); |
| 345 |
GLib.Cancellable cancellable = pd.start (); |
| 346 |
|
| 347 |
try { |
| 348 |
new Thread <int> .try ("DesktopFolder File Operation", () => { |
| 349 |
try { |
| 350 |
DesktopFolder.Util.copy_recursive (f, |
| 351 |
final_target, |
| 352 |
GLib.FileCopyFlags.NONE, |
| 353 |
cancellable, |
| 354 |
(file) => { |
| 355 |
string message = DesktopFolder.Lang.DRAGNDROP_COPYING; |
| 356 |
message = message + " " + file.get_path (); |
| 357 |
pd.show_action (message); |
| 358 |
}); |
| 359 |
} catch (Error e) { |
| 360 |
cancellable.cancel (); |
| 361 |
stderr.printf ("Error: %s\n", e.message); |
| 362 |
} |
| 363 |
pd.stop (); |
| 364 |
return 0; |
| 365 |
}); |
| 366 |
|
| 367 |
if (done_callback != null) |
| 368 |
done_callback (); |
| 369 |
|
| 370 |
} catch (Error error) { |
| 371 |
cancellable.cancel (); |
| 372 |
pd.stop (); |
| 373 |
stderr.printf ("Error: %s\n", error.message); |
| 374 |
} |
| 375 |
} else if (action == Gdk.DragAction.MOVE) { |
| 376 |
try { |
| 377 |
File final_target = File.new_for_path (target_path + "/" + f.get_basename ()); |
| 378 |
f.move (final_target, FileCopyFlags.NONE, null, null); |
| 379 |
if (done_callback != null) |
| 380 |
done_callback (); |
| 381 |
} catch (Error error) { |
| 382 |
stderr.printf ("Error: %s\n", error.message); |
| 383 |
} |
| 384 |
} else if (action == Gdk.DragAction.LINK) { |
| 385 |
try { |
| 386 |
var target_file = File.new_for_path (@"$target_path/$new_name"); |
| 387 |
if (!target_file.query_exists ()) { |
| 388 |
var command = @"ln -s \"$(f.get_path ())\" \"$target_path/$new_name\""; |
| 389 |
var appinfo = AppInfo.create_from_commandline (command, null, AppInfoCreateFlags.SUPPORTS_URIS); |
| 390 |
appinfo.launch_uris (null, null); |
| 391 |
} |
| 392 |
} catch (Error error) { |
| 393 |
stderr.printf ("Error: %s\n", error.message); |
| 394 |
} |
| 395 |
} else { |
| 396 |
debug ("unknown action!"); |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
} |
| 404 |
} |