1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:07:34 +00:00

Assistant: Enable escalation of commands that need it

Ideal for applications such as PartitionEditor. Previously an error
about how it needs to run as root was displayed which differed from
the behaviour when clicking from the system menus.

Use the new AppFile::spawn_with_escalation to perform the escalation
This commit is contained in:
Hugh Davenport 2024-01-05 17:24:11 +13:00 committed by Andreas Kling
parent 45c35267f6
commit bba53441ef
3 changed files with 21 additions and 20 deletions

View file

@ -11,11 +11,11 @@
#include <AK/URL.h>
#include <LibCore/Directory.h>
#include <LibCore/ElapsedTimer.h>
#include <LibCore/Process.h>
#include <LibCore/StandardPaths.h>
#include <LibDesktop/Launcher.h>
#include <LibGUI/Clipboard.h>
#include <LibGUI/FileIconProvider.h>
#include <LibGUI/Process.h>
#include <LibJS/Bytecode/Interpreter.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/ValueInlines.h>
@ -29,7 +29,7 @@
namespace Assistant {
void AppResult::activate() const
void AppResult::activate(GUI::Window& window) const
{
if (chdir(Core::StandardPaths::home_directory().characters()) < 0) {
perror("chdir");
@ -37,29 +37,29 @@ void AppResult::activate() const
}
auto arguments_list = m_arguments.split_view(' ');
m_app_file->spawn(arguments_list.span());
m_app_file->spawn_with_escalation_or_show_error(window, arguments_list.span());
}
void CalculatorResult::activate() const
void CalculatorResult::activate(GUI::Window& window) const
{
(void)window;
GUI::Clipboard::the().set_plain_text(title());
}
void FileResult::activate() const
void FileResult::activate(GUI::Window& window) const
{
(void)window;
Desktop::Launcher::open(URL::create_with_file_scheme(title()));
}
void TerminalResult::activate() const
void TerminalResult::activate(GUI::Window& window) const
{
// FIXME: This should be a GUI::Process::spawn_or_show_error(), however this is a
// Assistant::Result object, which does not have access to the application's GUI::Window* pointer
// (which spawn_or_show_error() needs in case it has to open a error message box).
(void)Core::Process::spawn("/bin/Terminal"sv, Array { "-k", "-e", title().characters() });
GUI::Process::spawn_or_show_error(&window, "/bin/Terminal"sv, Array { "-k", "-e", title().characters() });
}
void URLResult::activate() const
void URLResult::activate(GUI::Window& window) const
{
(void)window;
Desktop::Launcher::open(URL::create_with_url_or_path(title()));
}

View file

@ -11,6 +11,7 @@
#include <AK/URL.h>
#include <LibDesktop/AppFile.h>
#include <LibGUI/Desktop.h>
#include <LibGUI/Window.h>
#include <LibJS/Runtime/VM.h>
#include <LibThreading/BackgroundAction.h>
#include <typeinfo>
@ -23,7 +24,7 @@ class Result : public RefCounted<Result> {
public:
virtual ~Result() = default;
virtual void activate() const = 0;
virtual void activate(GUI::Window&) const = 0;
virtual Gfx::Bitmap const* bitmap() const = 0;
@ -61,7 +62,7 @@ public:
{
}
~AppResult() override = default;
void activate() const override;
void activate(GUI::Window&) const override;
virtual Gfx::Bitmap const* bitmap() const override { return m_bitmap; }
@ -79,7 +80,7 @@ public:
{
}
~CalculatorResult() override = default;
void activate() const override;
void activate(GUI::Window&) const override;
virtual Gfx::Bitmap const* bitmap() const override { return m_bitmap; }
@ -94,7 +95,7 @@ public:
{
}
~FileResult() override = default;
void activate() const override;
void activate(GUI::Window&) const override;
virtual Gfx::Bitmap const* bitmap() const override;
};
@ -107,7 +108,7 @@ public:
{
}
~TerminalResult() override = default;
void activate() const override;
void activate(GUI::Window&) const override;
virtual Gfx::Bitmap const* bitmap() const override { return m_bitmap; }
@ -123,7 +124,7 @@ public:
{
}
~URLResult() override = default;
void activate() const override;
void activate(GUI::Window&) const override;
virtual Gfx::Bitmap const* bitmap() const override { return m_bitmap; }

View file

@ -205,7 +205,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (!app_state.selected_index.has_value())
return;
lockfile.release();
app_state.results[app_state.selected_index.value()]->activate();
app_state.results[app_state.selected_index.value()]->activate(window);
GUI::Application::the()->quit();
};
text_box.on_up_pressed = [&]() {
@ -251,8 +251,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
match.set_icon(result->bitmap());
match.set_text(String::from_byte_string(result->title()).release_value_but_fixme_should_propagate_errors());
match.set_tooltip(result->tooltip());
match.on_click = [&result](auto) {
result->activate();
match.on_click = [&](auto) {
result->activate(window);
GUI::Application::the()->quit();
};
}