Applications: Use spawn_or_show_error() for common spawn pattern

This commit is contained in:
MacDue 2022-05-26 23:28:58 +01:00 committed by Linus Groh
parent 5e5a055455
commit 5fd5a03d1f
Notes: sideshowbarker 2024-07-17 10:29:13 +09:00
6 changed files with 18 additions and 44 deletions

View File

@ -11,6 +11,7 @@
#include <LibCore/DirIterator.h>
#include <LibCore/ElapsedTimer.h>
#include <LibCore/File.h>
#include <LibCore/Process.h>
#include <LibCore/StandardPaths.h>
#include <LibDesktop/Launcher.h>
#include <LibGUI/Clipboard.h>
@ -48,15 +49,10 @@ void FileResult::activate() const
void TerminalResult::activate() const
{
pid_t pid;
char const* argv[] = { "Terminal", "-k", "-e", title().characters(), nullptr };
if ((errno = posix_spawn(&pid, "/bin/Terminal", nullptr, nullptr, const_cast<char**>(argv), environ))) {
perror("posix_spawn");
} else {
if (disown(pid) < 0)
perror("disown");
}
// 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 incase it has to open a error message box).
(void)Core::Process::spawn("/bin/Terminal", Array { "-k", "-e", title().characters() });
}
void URLResult::activate() const

View File

@ -15,6 +15,7 @@
#include <LibGUI/Layout.h>
#include <LibGUI/Margins.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Process.h>
#include <LibGfx/Palette.h>
#include <LibTimeZone/TimeZone.h>
#include <LibUnicode/DateTimeFormat.h>
@ -156,13 +157,7 @@ Optional<Gfx::FloatPoint> TimeZoneSettingsWidget::compute_time_zone_location() c
return Gfx::FloatPoint { mercadian_x, mercadian_y };
}
void TimeZoneSettingsWidget::set_time_zone() const
void TimeZoneSettingsWidget::set_time_zone()
{
pid_t child_pid = 0;
char const* argv[] = { "/bin/timezone", m_time_zone.characters(), nullptr };
if ((errno = posix_spawn(&child_pid, "/bin/timezone", nullptr, nullptr, const_cast<char**>(argv), environ))) {
perror("posix_spawn");
exit(1);
}
GUI::Process::spawn_or_show_error(window(), "/bin/timezone", Array { m_time_zone.characters() });
}

View File

@ -26,7 +26,7 @@ private:
void set_time_zone_location();
Optional<Gfx::FloatPoint> compute_time_zone_location() const;
void set_time_zone() const;
void set_time_zone();
String m_time_zone;
RefPtr<GUI::ComboBox> m_time_zone_combo_box;

View File

@ -31,6 +31,7 @@
#include <LibGUI/Label.h>
#include <LibGUI/LinkLabel.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/Process.h>
#include <LibGUI/Progressbar.h>
#include <LibGUI/TabWidget.h>
#include <LibGUI/TextEditor.h>
@ -264,14 +265,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto& debug_button = *widget->find_descendant_of_type_named<GUI::Button>("debug_button");
debug_button.set_icon(TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-hack-studio.png")));
debug_button.on_click = [&](int) {
pid_t child;
const char* argv[4] = { "HackStudio", "-c", coredump_path, nullptr };
if ((errno = posix_spawn(&child, "/bin/HackStudio", nullptr, nullptr, const_cast<char**>(argv), environ))) {
perror("posix_spawn");
} else {
if (disown(child) < 0)
perror("disown");
}
GUI::Process::spawn_or_show_error(window, "/bin/HackStudio", Array { "-c", coredump_path });
};
auto& save_backtrace_button = *widget->find_descendant_of_type_named<GUI::Button>("save_backtrace_button");

View File

@ -21,6 +21,7 @@
#include <LibGUI/Label.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/Model.h>
#include <LibGUI/Process.h>
#include <LibGUI/Widget.h>
#include <LibGUI/Window.h>
#include <LibGfx/Font/FontDatabase.h>
@ -281,12 +282,6 @@ void KeyboardSettingsWidget::apply_settings()
void KeyboardSettingsWidget::set_keymaps(Vector<String> const& keymaps, String const& active_keymap)
{
pid_t child_pid;
auto keymaps_string = String::join(',', keymaps);
char const* argv[] = { "/bin/keymap", "-s", keymaps_string.characters(), "-m", active_keymap.characters(), nullptr };
if ((errno = posix_spawn(&child_pid, "/bin/keymap", nullptr, nullptr, const_cast<char**>(argv), environ))) {
perror("posix_spawn");
exit(1);
}
GUI::Process::spawn_or_show_error(window(), "/bin/keymap", Array { "-s", keymaps_string.characters(), "-m", active_keymap.characters() });
}

View File

@ -38,6 +38,7 @@
#include <LibGUI/Menubar.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Process.h>
#include <LibGUI/SeparatorWidget.h>
#include <LibGUI/SortingProxyModel.h>
#include <LibGUI/StackWidget.h>
@ -470,17 +471,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
"&Profile Process", { Mod_Ctrl, Key_P },
Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-profiler.png").release_value_but_fixme_should_propagate_errors(), [&](auto&) {
pid_t pid = selected_id(ProcessModel::Column::PID);
if (pid != -1) {
auto pid_string = String::number(pid);
pid_t child;
const char* argv[] = { "/bin/Profiler", "--pid", pid_string.characters(), nullptr };
if ((errno = posix_spawn(&child, "/bin/Profiler", nullptr, nullptr, const_cast<char**>(argv), environ))) {
perror("posix_spawn");
} else {
if (disown(child) < 0)
perror("disown");
}
}
if (pid == -1)
return;
auto pid_string = String::number(pid);
GUI::Process::spawn_or_show_error(window, "/bin/Profiler", Array { "--pid", pid_string.characters() });
},
&process_table_view);