diff options
author | Poommetee Ketson <poommetee@protonmail.com> | 2017-10-02 23:52:09 +0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-02 23:52:09 +0700 |
commit | 478fd21e07fd4092e3a099556d4004e34ae31177 (patch) | |
tree | 45f4cee59e852440f43b2e9e8c17e879053f8eb7 /platform | |
parent | 34ea27138072446947ee12bfcaba288f9ff825e5 (diff) | |
parent | 20918587d39c5c9c370e3b4beccf883f553d9b3e (diff) |
Merge pull request #11575 from marcelofg55/move_path_to_trash
FileSystemDock will now remove files/dirs to trashcan using OS::move_to_trash
Diffstat (limited to 'platform')
-rw-r--r-- | platform/osx/os_osx.h | 2 | ||||
-rw-r--r-- | platform/osx/os_osx.mm | 13 | ||||
-rw-r--r-- | platform/windows/os_windows.cpp | 27 | ||||
-rw-r--r-- | platform/windows/os_windows.h | 2 | ||||
-rw-r--r-- | platform/x11/os_x11.cpp | 72 | ||||
-rw-r--r-- | platform/x11/os_x11.h | 2 |
6 files changed, 118 insertions, 0 deletions
diff --git a/platform/osx/os_osx.h b/platform/osx/os_osx.h index 1f76b7f990..2662e40561 100644 --- a/platform/osx/os_osx.h +++ b/platform/osx/os_osx.h @@ -227,6 +227,8 @@ public: void disable_crash_handler(); bool is_disable_crash_handler() const; + virtual Error move_to_trash(const String &p_path); + OS_OSX(); }; diff --git a/platform/osx/os_osx.mm b/platform/osx/os_osx.mm index 16bef349dc..6502001100 100644 --- a/platform/osx/os_osx.mm +++ b/platform/osx/os_osx.mm @@ -1910,6 +1910,19 @@ int OS_OSX::get_power_percent_left() { return power_manager->get_power_percent_left(); } +Error OS_OSX::move_to_trash(const String &p_path) { + NSFileManager *fm = [NSFileManager defaultManager]; + NSURL *url = [NSURL fileURLWithPath:@(p_path.utf8().get_data())]; + NSError *err; + + if (![fm trashItemAtURL:url resultingItemURL:nil error:&err]) { + ERR_PRINTS("trashItemAtURL error: " + String(err.localizedDescription.UTF8String)); + return FAILED; + } + + return OK; +} + OS_OSX *OS_OSX::singleton = NULL; OS_OSX::OS_OSX() { diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index db7cd0b938..d715c51a71 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -2373,6 +2373,33 @@ bool OS_Windows::is_disable_crash_handler() const { return crash_handler.is_disabled(); } +Error OS_Windows::move_to_trash(const String &p_path) { + SHFILEOPSTRUCTA sf; + TCHAR *from = new TCHAR[p_path.length() + 2]; + strcpy(from, p_path.utf8().get_data()); + from[p_path.length()] = 0; + from[p_path.length() + 1] = 0; + + sf.hwnd = hWnd; + sf.wFunc = FO_DELETE; + sf.pFrom = from; + sf.pTo = NULL; + sf.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION; + sf.fAnyOperationsAborted = FALSE; + sf.hNameMappings = NULL; + sf.lpszProgressTitle = NULL; + + int ret = SHFileOperation(&sf); + delete[] from; + + if (ret) { + ERR_PRINTS("SHFileOperation error: " + itos(ret)); + return FAILED; + } + + return OK; +} + OS_Windows::OS_Windows(HINSTANCE _hInstance) { key_event_pos = 0; diff --git a/platform/windows/os_windows.h b/platform/windows/os_windows.h index 8a955aa224..bc1dc318cb 100644 --- a/platform/windows/os_windows.h +++ b/platform/windows/os_windows.h @@ -290,6 +290,8 @@ public: void disable_crash_handler(); bool is_disable_crash_handler() const; + virtual Error move_to_trash(const String &p_path); + OS_Windows(HINSTANCE _hInstance); ~OS_Windows(); }; diff --git a/platform/x11/os_x11.cpp b/platform/x11/os_x11.cpp index 599c0d2278..041666a594 100644 --- a/platform/x11/os_x11.cpp +++ b/platform/x11/os_x11.cpp @@ -35,6 +35,7 @@ #include "servers/physics/physics_server_sw.h" #include "servers/visual/visual_server_raster.h" #include "servers/visual/visual_server_wrap_mt.h" +#include <mntent.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -2175,6 +2176,77 @@ bool OS_X11::is_disable_crash_handler() const { return crash_handler.is_disabled(); } +static String get_mountpoint(const String &p_path) { + struct stat s; + if (stat(p_path.utf8().get_data(), &s)) { + return ""; + } + + dev_t dev = s.st_dev; + FILE *fd = setmntent("/proc/mounts", "r"); + if (!fd) { + return ""; + } + + struct mntent mnt; + char buf[1024]; + size_t buflen = 1024; + while (getmntent_r(fd, &mnt, buf, buflen)) { + if (!stat(mnt.mnt_dir, &s) && s.st_dev == dev) { + endmntent(fd); + return String(mnt.mnt_dir); + } + } + + endmntent(fd); + return ""; +} + +Error OS_X11::move_to_trash(const String &p_path) { + String trashcan = ""; + String mnt = get_mountpoint(p_path); + + if (mnt != "") { + String path(mnt + "/.Trash-" + itos(getuid()) + "/files"); + struct stat s; + if (!stat(path.utf8().get_data(), &s)) { + trashcan = path; + } + } + + if (trashcan == "") { + char *dhome = getenv("XDG_DATA_HOME"); + if (dhome) { + trashcan = String(dhome) + "/Trash/files"; + } + } + + if (trashcan == "") { + char *home = getenv("HOME"); + if (home) { + trashcan = String(home) + "/.local/share/Trash/files"; + } + } + + if (trashcan == "") { + ERR_PRINTS("move_to_trash: Could not determine trashcan location"); + return FAILED; + } + + List<String> args; + args.push_back("-p"); + args.push_back(trashcan); + Error err = execute("/bin/mkdir", args, true); + if (err == OK) { + List<String> args2; + args2.push_back(p_path); + args2.push_back(trashcan); + err = execute("/bin/mv", args2, true); + } + + return err; +} + OS_X11::OS_X11() { #ifdef PULSEAUDIO_ENABLED diff --git a/platform/x11/os_x11.h b/platform/x11/os_x11.h index a0338dacf1..0d5c272ed4 100644 --- a/platform/x11/os_x11.h +++ b/platform/x11/os_x11.h @@ -273,6 +273,8 @@ public: void disable_crash_handler(); bool is_disable_crash_handler() const; + virtual Error move_to_trash(const String &p_path); + OS_X11(); }; |