1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 05:24:58 +00:00

AppFile: Add spawn_with_escalation

This commit is contained in:
Hugh Davenport 2024-01-05 17:20:33 +13:00 committed by Andreas Kling
parent 83f1775f15
commit c4abb367a4
2 changed files with 42 additions and 0 deletions

View file

@ -11,8 +11,10 @@
#include <LibCore/ConfigFile.h>
#include <LibCore/DirIterator.h>
#include <LibCore/Process.h>
#include <LibCore/StandardPaths.h>
#include <LibDesktop/AppFile.h>
#include <LibFileSystem/FileSystem.h>
#include <LibGUI/MessageBox.h>
namespace Desktop {
@ -179,4 +181,41 @@ bool AppFile::spawn(ReadonlySpan<StringView> arguments) const
return true;
}
bool AppFile::spawn_with_escalation(ReadonlySpan<StringView> user_arguments) const
{
if (!is_valid())
return false;
StringView exe;
Vector<StringView, 2> args;
// FIXME: These single quotes won't be enough for executables with single quotes in their name.
auto pls_with_executable = ByteString::formatted("/bin/pls '{}'", executable());
if (run_in_terminal() && !requires_root()) {
exe = "/bin/Terminal"sv;
args = { "-e"sv, executable().view() };
} else if (!run_in_terminal() && requires_root()) {
exe = "/bin/Escalator"sv;
args = { executable().view() };
} else if (run_in_terminal() && requires_root()) {
exe = "/bin/Terminal"sv;
args = { "-e"sv, pls_with_executable.view() };
} else {
exe = executable().view();
}
args.extend(Vector(user_arguments));
auto pid = Core::Process::spawn(exe, args.span(),
working_directory().is_empty() ? Core::StandardPaths::home_directory() : working_directory());
if (pid.is_error())
return false;
return true;
}
void AppFile::spawn_with_escalation_or_show_error(GUI::Window& window, ReadonlySpan<StringView> arguments) const
{
if (!spawn_with_escalation(arguments))
GUI::MessageBox::show_error(&window, ByteString::formatted("Failed to spawn {} with escalation", executable()));
}
}

View file

@ -10,6 +10,7 @@
#include <LibCore/ConfigFile.h>
#include <LibGUI/FileIconProvider.h>
#include <LibGUI/Icon.h>
#include <LibGUI/Window.h>
namespace Desktop {
@ -44,6 +45,8 @@ public:
Vector<ByteString> launcher_file_types() const;
Vector<ByteString> launcher_protocols() const;
bool spawn(ReadonlySpan<StringView> arguments = {}) const;
bool spawn_with_escalation(ReadonlySpan<StringView> arguments = {}) const;
void spawn_with_escalation_or_show_error(GUI::Window&, ReadonlySpan<StringView> arguments = {}) const;
private:
explicit AppFile(StringView path);