Просмотр исходного кода

AppFile: Add spawn_with_escalation

Hugh Davenport 1 год назад
Родитель
Сommit
c4abb367a4
2 измененных файлов с 42 добавлено и 0 удалено
  1. 39 0
      Userland/Libraries/LibDesktop/AppFile.cpp
  2. 3 0
      Userland/Libraries/LibDesktop/AppFile.h

+ 39 - 0
Userland/Libraries/LibDesktop/AppFile.cpp

@@ -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()));
+}
+
 }

+ 3 - 0
Userland/Libraries/LibDesktop/AppFile.h

@@ -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);