Browse Source

Settings: Support for launching settings apps as root

ne0ndrag0n 2 years ago
parent
commit
2affe67c75
1 changed files with 24 additions and 8 deletions
  1. 24 8
      Userland/Applications/Settings/main.cpp

+ 24 - 8
Userland/Applications/Settings/main.cpp

@@ -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,10 +112,15 @@ 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);
-        GUI::Process::spawn_or_show_error(window, executable);
+
+        if (requires_root)
+            GUI::Process::spawn_or_show_error(window, "/bin/Escalator"sv, Array { executable });
+        else
+            GUI::Process::spawn_or_show_error(window, executable);
     };
 
     auto statusbar = TRY(main_widget->try_add<GUI::Statusbar>());