From c9043280ec12fcefbe8c0acd849a30217270480d Mon Sep 17 00:00:00 2001 From: MacDue Date: Mon, 13 Mar 2023 20:34:03 +0000 Subject: [PATCH] Taskbar: Use GUI::Process::spawn_or_show_error() to launch apps And while here make a partial fix for launching terminal apps that require root and contain spaces in their name/path. --- Userland/Services/Taskbar/main.cpp | 43 ++++++++++-------------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/Userland/Services/Taskbar/main.cpp b/Userland/Services/Taskbar/main.cpp index f3b996a25f..1e0d1bc305 100644 --- a/Userland/Services/Taskbar/main.cpp +++ b/Userland/Services/Taskbar/main.cpp @@ -176,42 +176,27 @@ ErrorOr> build_system_menu(GUI::Window& window) } auto parent_menu = app_category_menus.get(app.category).value_or(system_menu.ptr()); - parent_menu->add_action(GUI::Action::create(app.name, icon, [app_identifier](auto&) { + parent_menu->add_action(GUI::Action::create(app.name, icon, [app_identifier, &window](auto&) { dbgln("Activated app with ID {}", app_identifier); auto& app = g_apps[app_identifier]; - char const* argv[4] { nullptr, nullptr, nullptr, nullptr }; - auto pls_with_executable = DeprecatedString::formatted("/bin/pls {}", app.executable); + StringView executable; + Vector arguments; + // FIXME: These single quotes won't be enough for executables with single quotes in their name. + auto pls_with_executable = DeprecatedString::formatted("/bin/pls '{}'", app.executable); if (app.run_in_terminal && !app.requires_root) { - argv[0] = "/bin/Terminal"; - argv[1] = "-e"; - argv[2] = app.executable.characters(); + executable = "/bin/Terminal"sv; + arguments = { "-e", app.executable.characters() }; } else if (!app.run_in_terminal && app.requires_root) { - argv[0] = "/bin/Escalator"; - argv[1] = app.executable.characters(); + executable = "/bin/Escalator"sv; + arguments = { app.executable.characters() }; } else if (app.run_in_terminal && app.requires_root) { - argv[0] = "/bin/Terminal"; - argv[1] = "-e"; - argv[2] = pls_with_executable.characters(); + executable = "/bin/Terminal"sv; + arguments = { "-e", pls_with_executable.characters() }; } else { - argv[0] = app.executable.characters(); + executable = app.executable; } - - posix_spawn_file_actions_t spawn_actions; - posix_spawn_file_actions_init(&spawn_actions); - auto home_directory = Core::StandardPaths::home_directory(); - if (app.working_directory.is_empty()) - posix_spawn_file_actions_addchdir(&spawn_actions, home_directory.characters()); - else - posix_spawn_file_actions_addchdir(&spawn_actions, app.working_directory.characters()); - - pid_t child_pid; - if ((errno = posix_spawn(&child_pid, argv[0], &spawn_actions, nullptr, const_cast(argv), environ))) { - perror("posix_spawn"); - } else { - if (disown(child_pid) < 0) - perror("disown"); - } - posix_spawn_file_actions_destroy(&spawn_actions); + GUI::Process::spawn_or_show_error(&window, executable, arguments, + app.working_directory.is_empty() ? Core::StandardPaths::home_directory() : app.working_directory); })); ++app_identifier; }