From 30556a0a9349110f3ecfdb60379f6838289a6295 Mon Sep 17 00:00:00 2001 From: thatlittlegit Date: Sun, 23 Feb 2020 10:38:57 -0500 Subject: [PATCH] SystemMenu: Move SystemDialog into SystemMenu and remove INI config I probably would've done INI config removal in another commit, but it fit well here because I didn't want to pledge wpath for SystemMenu if I didn't need to. Frankly, that's something that I think should be done: allow ConfigFile to be used read-only. --- Applications/SystemDialog/Makefile | 8 -- Applications/SystemMenu/Makefile | 3 +- .../main.cpp => SystemMenu/PowerDialog.cpp} | 131 +++++------------- Applications/SystemMenu/PowerDialog.h | 39 ++++++ Applications/SystemMenu/main.cpp | 14 +- Base/etc/SystemDialog.ini | 12 -- .../16x16/{app-systemdialog.png => power.png} | Bin Kernel/build-root-filesystem.sh | 1 - 8 files changed, 87 insertions(+), 121 deletions(-) delete mode 100755 Applications/SystemDialog/Makefile rename Applications/{SystemDialog/main.cpp => SystemMenu/PowerDialog.cpp} (52%) create mode 100644 Applications/SystemMenu/PowerDialog.h delete mode 100644 Base/etc/SystemDialog.ini rename Base/res/icons/16x16/{app-systemdialog.png => power.png} (100%) diff --git a/Applications/SystemDialog/Makefile b/Applications/SystemDialog/Makefile deleted file mode 100755 index ce009bc506f..00000000000 --- a/Applications/SystemDialog/Makefile +++ /dev/null @@ -1,8 +0,0 @@ -OBJS = \ - main.o - -PROGRAM = SystemDialog - -LIB_DEPS = GUI Gfx IPC Core - -include ../../Makefile.common diff --git a/Applications/SystemMenu/Makefile b/Applications/SystemMenu/Makefile index 0132cdd40c2..58a9369e16d 100644 --- a/Applications/SystemMenu/Makefile +++ b/Applications/SystemMenu/Makefile @@ -1,5 +1,6 @@ OBJS = \ - main.o + main.o \ + PowerDialog.o PROGRAM = SystemMenu diff --git a/Applications/SystemDialog/main.cpp b/Applications/SystemMenu/PowerDialog.cpp similarity index 52% rename from Applications/SystemDialog/main.cpp rename to Applications/SystemMenu/PowerDialog.cpp index ffbf86b83e2..1d28de0d58d 100644 --- a/Applications/SystemDialog/main.cpp +++ b/Applications/SystemMenu/PowerDialog.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2020, the SerenityOS developers. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,102 +26,51 @@ #include #include -#include -#include #include #include #include -#include #include #include #include #include -#include -#include -#include -struct DialogOption { +#include "PowerDialog.h" + +struct PowerOption { String title; - String cmd; + Vector cmd; bool enabled; bool default_action; }; -Vector get_options() +static const Vector options = { + { "Shut down", { "/bin/shutdown", "--now", nullptr }, true, true }, + { "Restart", { "/bin/reboot", nullptr }, true, false }, + { "Log out", {}, false, false }, + { "Sleep", {}, false, false }, +}; + +Vector PowerDialog::show() { - Vector options; - auto config = Core::ConfigFile::get_for_system("SystemDialog"); - for (auto title : config->groups()) { - dbg() << "title = " << title; - auto command = config->read_entry(title, "command", ""); - dbg() << "\tcommand=" << command; - auto enabled = config->read_bool_entry(title, "enabled", true); - dbg() << "\tenabled=" << enabled; - auto default_entry = config->read_bool_entry(title, "default", false); - dbg() << "\tdefault=" << default_entry; + auto rc = PowerDialog::construct()->exec(); + if (rc < 0) + return {}; - ASSERT(!(command == "" && enabled)); - - options.append({ title, command, enabled, default_entry }); - } - return options; + return options[rc].cmd; } -int main(int argc, char** argv) +PowerDialog::PowerDialog(Core::Object* parent) + : GUI::Dialog(parent) { - Vector options; - - if (pledge("stdio shared_buffer rpath wpath cpath unix fattr exec", nullptr) < 0) { - perror("pledge"); - return 1; - } - - if (unveil("/etc/SystemDialog.ini", "rwc") < 0) { - perror("unveil"); - return 1; - } - - if (unveil("/tmp", "rwc") < 0) { - perror("unveil"); - return 1; - } - - if (unveil("/res", "r") < 0) { - perror("unveil"); - return 1; - } - - if (unveil("/bin/Shell", "rx") < 0) { - perror("unveil"); - return 1; - } - - unveil(nullptr, nullptr); - - GUI::Application app(argc, argv); - - if (pledge("stdio shared_buffer rpath wpath cpath exec", nullptr) < 0) { - perror("pledge"); - return 1; - } - - options = get_options(); - - if (pledge("stdio shared_buffer rpath exec", nullptr) < 0) { - perror("pledge"); - return 1; - } - - auto dialog = GUI::Dialog::construct(nullptr); Gfx::Rect rect({ 0, 0, 180, 180 + ((options.size() - 3) * 16) }); rect.center_within(GUI::Desktop::the().rect()); - dialog->set_rect(rect); - dialog->set_resizable(false); - dialog->set_title("SerenityOS"); - dialog->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-systemdialog.png")); + set_rect(rect); + set_resizable(false); + set_title("SerenityOS"); + set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/power.png")); auto main = GUI::Widget::construct(); - dialog->set_main_widget(main); + set_main_widget(main); main->set_layout(make()); main->layout()->set_margins({ 8, 8, 8, 8 }); main->layout()->set_spacing(8); @@ -133,7 +82,7 @@ int main(int argc, char** argv) header->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); header->set_font(Gfx::Font::default_bold_font()); - int selected; + int selected = -1; for (int i = 0; i < options.size(); i++) { auto action = options[i]; auto radio = GUI::RadioButton::construct(main); @@ -155,32 +104,18 @@ int main(int argc, char** argv) button_box->layout()->set_spacing(8); auto ok_button = GUI::Button::construct(button_box); - ok_button->on_click = [&](auto&) { - dialog->done(true); + ok_button->on_click = [this, &selected](auto&) { + done(selected); }; ok_button->set_text("OK"); auto cancel_button = GUI::Button::construct(button_box); - cancel_button->on_click = [&](auto&) { - dialog->done(false); + cancel_button->on_click = [this](auto&) { + done(-1); }; cancel_button->set_text("Cancel"); - - dialog->exec(); - - if (pledge("stdio shared_buffer exec", nullptr) < 0) { - perror("pledge"); - return 1; - } - - if (!dialog->result()) - return 0; - - // TODO Don't rely on the shell - auto command = options[selected].cmd.characters(); - dbg() << command; - if (execl("/bin/Shell", "/bin/Shell", "-c", command, NULL) < 0) { - perror("execl"); - return 1; - } +} + +PowerDialog::~PowerDialog() +{ } diff --git a/Applications/SystemMenu/PowerDialog.h b/Applications/SystemMenu/PowerDialog.h new file mode 100644 index 00000000000..c36e033aa98 --- /dev/null +++ b/Applications/SystemMenu/PowerDialog.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include + +class PowerDialog : public GUI::Dialog { + C_OBJECT(PowerDialog) +public: + static Vector show(); + +private: + PowerDialog(Core::Object* parent = nullptr); + ~PowerDialog(); +}; diff --git a/Applications/SystemMenu/main.cpp b/Applications/SystemMenu/main.cpp index cb2bd7201e2..cc9b0cf265f 100644 --- a/Applications/SystemMenu/main.cpp +++ b/Applications/SystemMenu/main.cpp @@ -40,6 +40,8 @@ #include #include +#include "PowerDialog.h" + //#define SYSTEM_MENU_DEBUG struct AppMetadata { @@ -88,6 +90,11 @@ int main(int argc, char** argv) return 1; } + if (unveil("/etc/PowerOptions.ini", "r")) { + perror("unveil"); + return 1; + } + unveil(nullptr, nullptr); return app.exec(); @@ -193,8 +200,13 @@ NonnullRefPtr build_system_menu() })); system_menu->add_separator(); system_menu->add_action(GUI::Action::create("Exit...", [](auto&) { + Vector command = PowerDialog::show(); + + if (command.size() == 0) + return; + if (fork() == 0) { - execl("/bin/SystemDialog", "/bin/SystemDialog", nullptr); + execv(command[0], const_cast(command.data())); ASSERT_NOT_REACHED(); } })); diff --git a/Base/etc/SystemDialog.ini b/Base/etc/SystemDialog.ini deleted file mode 100644 index 1e074a0b582..00000000000 --- a/Base/etc/SystemDialog.ini +++ /dev/null @@ -1,12 +0,0 @@ -[Shut Down] -command=/bin/shutdown --now -default=1 - -[Reboot] -command=/bin/reboot - -[Log Out] -enabled=0 - -[Suspend] -enabled=0 diff --git a/Base/res/icons/16x16/app-systemdialog.png b/Base/res/icons/16x16/power.png similarity index 100% rename from Base/res/icons/16x16/app-systemdialog.png rename to Base/res/icons/16x16/power.png diff --git a/Kernel/build-root-filesystem.sh b/Kernel/build-root-filesystem.sh index 14b4ccbb102..0f7701dec0d 100755 --- a/Kernel/build-root-filesystem.sh +++ b/Kernel/build-root-filesystem.sh @@ -128,7 +128,6 @@ cp ../Applications/HexEditor/HexEditor mnt/bin/HexEditor cp ../Applications/PaintBrush/PaintBrush mnt/bin/PaintBrush cp ../Applications/QuickShow/QuickShow mnt/bin/QuickShow cp ../Applications/Piano/Piano mnt/bin/Piano -cp ../Applications/SystemDialog/SystemDialog mnt/bin/SystemDialog cp ../Applications/SystemMenu/SystemMenu mnt/bin/SystemMenu cp ../Applications/ChanViewer/ChanViewer mnt/bin/ChanViewer cp ../Applications/Calculator/Calculator mnt/bin/Calculator