Settings: Support for launching settings apps as root

This commit is contained in:
ne0ndrag0n 2022-10-03 20:09:34 -04:00 committed by Linus Groh
parent 7fc22896ee
commit 2affe67c75
Notes: sideshowbarker 2024-07-17 06:09:44 +09:00

View file

@ -21,6 +21,12 @@
#include <stdio.h>
#include <unistd.h>
// Modeled after PlaylistWidget.
enum class SettingsAppsModelCustomRole {
_DONOTUSE = (int)GUI::ModelRole::Custom,
RequiresRoot
};
class SettingsAppsModel final : public GUI::Model {
public:
SettingsAppsModel()
@ -45,21 +51,26 @@ public:
virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role) const override
{
auto& app = m_apps[index.row()];
if (role == GUI::ModelRole::Icon) {
if (role == GUI::ModelRole::Icon)
return app->icon();
}
if (role == GUI::ModelRole::Display) {
String name;
if (app->name().ends_with(" Settings"sv)) {
if (app->name().ends_with(" Settings"sv))
name = app->name().substring(0, app->name().length() - " Settings"sv.length());
} else {
else
name = app->name();
}
return name;
}
if (role == GUI::ModelRole::Custom) {
if (role == GUI::ModelRole::Custom)
return app->executable();
}
if (role == static_cast<GUI::ModelRole>(SettingsAppsModelCustomRole::RequiresRoot))
return app->requires_root();
return {};
}
@ -101,9 +112,14 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
icon_view->on_activation = [&](GUI::ModelIndex const& index) {
auto executable = model->data(index, GUI::ModelRole::Custom).as_string();
auto requires_root = model->data(index, static_cast<GUI::ModelRole>(SettingsAppsModelCustomRole::RequiresRoot)).as_bool();
auto launch_origin_rect = icon_view->to_widget_rect(icon_view->content_rect(index)).translated(icon_view->screen_relative_rect().location());
setenv("__libgui_launch_origin_rect", String::formatted("{},{},{},{}", launch_origin_rect.x(), launch_origin_rect.y(), launch_origin_rect.width(), launch_origin_rect.height()).characters(), 1);
if (requires_root)
GUI::Process::spawn_or_show_error(window, "/bin/Escalator"sv, Array { executable });
else
GUI::Process::spawn_or_show_error(window, executable);
};