Skip to content

Commit

Permalink
Applications: Use spawn_or_show_error() for common spawn pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
MacDue authored and linusg committed Jun 3, 2022
1 parent 5e5a055 commit 5fd5a03
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 44 deletions.
14 changes: 5 additions & 9 deletions Userland/Applications/Assistant/Providers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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
Expand Down
11 changes: 3 additions & 8 deletions Userland/Applications/ClockSettings/TimeZoneSettingsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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() });
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class TimeZoneSettingsWidget final : public GUI::SettingsWindow::Tab {

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;
Expand Down
10 changes: 2 additions & 8 deletions Userland/Applications/CrashReporter/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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() });
}
16 changes: 5 additions & 11 deletions Userland/Applications/SystemMonitor/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 5fd5a03

Please sign in to comment.