chore: use base::Environment in Linux MoveItemToTrash() (#19367)

* chore: use base::Environment in MoveItemToTrash() Linux impl

* chore: remove unnecessary local function XDGUtil()

* chore: tweak code comment

* fix: remove errant reference
This commit is contained in:
Charles Kerr 2019-07-23 12:25:58 -05:00 committed by Shelley Vohr
parent 5dcac23aea
commit 38507974d6

View file

@ -16,11 +16,10 @@
#include "url/gurl.h" #include "url/gurl.h"
#define ELECTRON_TRASH "ELECTRON_TRASH" #define ELECTRON_TRASH "ELECTRON_TRASH"
#define ELECTRON_DEFAULT_TRASH "gio"
namespace { namespace {
bool XDGUtilV(const std::vector<std::string>& argv, const bool wait_for_exit) { bool XDGUtil(const std::vector<std::string>& argv, const bool wait_for_exit) {
base::LaunchOptions options; base::LaunchOptions options;
options.allow_new_privs = true; options.allow_new_privs = true;
// xdg-open can fall back on mailcap which eventually might plumb through // xdg-open can fall back on mailcap which eventually might plumb through
@ -44,22 +43,12 @@ bool XDGUtilV(const std::vector<std::string>& argv, const bool wait_for_exit) {
return true; return true;
} }
bool XDGUtil(const std::string& util,
const std::string& arg,
const bool wait_for_exit) {
std::vector<std::string> argv;
argv.push_back(util);
argv.push_back(arg);
return XDGUtilV(argv, wait_for_exit);
}
bool XDGOpen(const std::string& path, const bool wait_for_exit) { bool XDGOpen(const std::string& path, const bool wait_for_exit) {
return XDGUtil("xdg-open", path, wait_for_exit); return XDGUtil({"xdg-open", path}, wait_for_exit);
} }
bool XDGEmail(const std::string& email, const bool wait_for_exit) { bool XDGEmail(const std::string& email, const bool wait_for_exit) {
return XDGUtil("xdg-email", email, wait_for_exit); return XDGUtil({"xdg-email", email}, wait_for_exit);
} }
} // namespace } // namespace
@ -94,44 +83,35 @@ void OpenExternal(const GURL& url,
} }
bool MoveItemToTrash(const base::FilePath& full_path) { bool MoveItemToTrash(const base::FilePath& full_path) {
std::unique_ptr<base::Environment> env(base::Environment::Create());
// find the trash method
std::string trash; std::string trash;
if (getenv(ELECTRON_TRASH) != NULL) { if (!env->GetVar(ELECTRON_TRASH, &trash)) {
trash = getenv(ELECTRON_TRASH);
} else {
// Determine desktop environment and set accordingly. // Determine desktop environment and set accordingly.
std::unique_ptr<base::Environment> env(base::Environment::Create()); const auto desktop_env(base::nix::GetDesktopEnvironment(env.get()));
base::nix::DesktopEnvironment desktop_env(
base::nix::GetDesktopEnvironment(env.get()));
if (desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE4 || if (desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE4 ||
desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE5) { desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE5) {
trash = "kioclient5"; trash = "kioclient5";
} else if (desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE3) { } else if (desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE3) {
trash = "kioclient"; trash = "kioclient";
} else {
trash = ELECTRON_DEFAULT_TRASH;
} }
} }
// build the invocation
std::vector<std::string> argv; std::vector<std::string> argv;
const auto& filename = full_path.value();
if (trash.compare("kioclient5") == 0 || trash.compare("kioclient") == 0) { if (trash == "kioclient5" || trash == "kioclient") {
argv.push_back(trash); argv = {trash, "move", filename, "trash:/"};
argv.push_back("move"); } else if (trash == "trash-cli") {
argv.push_back(full_path.value()); argv = {"trash-put", filename};
argv.push_back("trash:/"); } else if (trash == "gvfs-trash") {
} else if (trash.compare("trash-cli") == 0) { argv = {"gvfs-trash", filename}; // deprecated, but still exists
argv.push_back("trash-put");
argv.push_back(full_path.value());
} else if (trash.compare("gvfs-trash") == 0) {
// retain support for deprecated gvfs-trash
argv.push_back("gvfs-trash");
argv.push_back(full_path.value());
} else { } else {
argv.push_back(ELECTRON_DEFAULT_TRASH); argv = {"gio", "trash", filename};
argv.push_back("trash");
argv.push_back(full_path.value());
} }
return XDGUtilV(argv, true);
return XDGUtil(argv, true);
} }
void Beep() { void Beep() {