diff options
author | thatlittlegit <personal@thatlittlegit.tk> | 2020-02-23 10:38:57 -0500 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-02-23 22:03:03 +0100 |
commit | 30556a0a9349110f3ecfdb60379f6838289a6295 (patch) | |
tree | 1d980ec3b5d571f01f2e584506e2721cf4243f63 /Applications/SystemMenu | |
parent | 9784ab99d2d0e7a2e89107ff6c7feb5b9a247ca5 (diff) | |
download | serenity-30556a0a9349110f3ecfdb60379f6838289a6295.zip |
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.
Diffstat (limited to 'Applications/SystemMenu')
-rw-r--r-- | Applications/SystemMenu/Makefile | 3 | ||||
-rw-r--r-- | Applications/SystemMenu/PowerDialog.cpp | 121 | ||||
-rw-r--r-- | Applications/SystemMenu/PowerDialog.h | 39 | ||||
-rw-r--r-- | Applications/SystemMenu/main.cpp | 14 |
4 files changed, 175 insertions, 2 deletions
diff --git a/Applications/SystemMenu/Makefile b/Applications/SystemMenu/Makefile index 0132cdd40c..58a9369e16 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/SystemMenu/PowerDialog.cpp b/Applications/SystemMenu/PowerDialog.cpp new file mode 100644 index 0000000000..1d28de0d58 --- /dev/null +++ b/Applications/SystemMenu/PowerDialog.cpp @@ -0,0 +1,121 @@ +/* + * 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 <AK/String.h> +#include <AK/Vector.h> +#include <LibGUI/BoxLayout.h> +#include <LibGUI/Button.h> +#include <LibGUI/Desktop.h> +#include <LibGUI/Label.h> +#include <LibGUI/RadioButton.h> +#include <LibGUI/Widget.h> +#include <LibGfx/Font.h> + +#include "PowerDialog.h" + +struct PowerOption { + String title; + Vector<char const*> cmd; + bool enabled; + bool default_action; +}; + +static const Vector<PowerOption> options = { + { "Shut down", { "/bin/shutdown", "--now", nullptr }, true, true }, + { "Restart", { "/bin/reboot", nullptr }, true, false }, + { "Log out", {}, false, false }, + { "Sleep", {}, false, false }, +}; + +Vector<char const*> PowerDialog::show() +{ + auto rc = PowerDialog::construct()->exec(); + if (rc < 0) + return {}; + + return options[rc].cmd; +} + +PowerDialog::PowerDialog(Core::Object* parent) + : GUI::Dialog(parent) +{ + Gfx::Rect rect({ 0, 0, 180, 180 + ((options.size() - 3) * 16) }); + rect.center_within(GUI::Desktop::the().rect()); + 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(); + set_main_widget(main); + main->set_layout(make<GUI::VerticalBoxLayout>()); + main->layout()->set_margins({ 8, 8, 8, 8 }); + main->layout()->set_spacing(8); + main->set_fill_with_background_color(true); + + auto header = GUI::Label::construct(main.ptr()); + header->set_text("What would you like to do?"); + header->set_preferred_size(0, 16); + header->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); + header->set_font(Gfx::Font::default_bold_font()); + + int selected = -1; + for (int i = 0; i < options.size(); i++) { + auto action = options[i]; + auto radio = GUI::RadioButton::construct(main); + radio->set_enabled(action.enabled); + radio->set_text(action.title); + + radio->on_checked = [&selected, i](auto) { + selected = i; + }; + + if (action.default_action) { + radio->set_checked(true); + selected = i; + } + } + + auto button_box = GUI::Widget::construct(main.ptr()); + button_box->set_layout(make<GUI::HorizontalBoxLayout>()); + button_box->layout()->set_spacing(8); + + auto ok_button = GUI::Button::construct(button_box); + 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 = [this](auto&) { + done(-1); + }; + cancel_button->set_text("Cancel"); +} + +PowerDialog::~PowerDialog() +{ +} diff --git a/Applications/SystemMenu/PowerDialog.h b/Applications/SystemMenu/PowerDialog.h new file mode 100644 index 0000000000..c36e033aa9 --- /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 <AK/Vector.h> +#include <LibCore/Object.h> +#include <LibGUI/Dialog.h> + +class PowerDialog : public GUI::Dialog { + C_OBJECT(PowerDialog) +public: + static Vector<char const*> show(); + +private: + PowerDialog(Core::Object* parent = nullptr); + ~PowerDialog(); +}; diff --git a/Applications/SystemMenu/main.cpp b/Applications/SystemMenu/main.cpp index cb2bd7201e..cc9b0cf265 100644 --- a/Applications/SystemMenu/main.cpp +++ b/Applications/SystemMenu/main.cpp @@ -40,6 +40,8 @@ #include <sys/utsname.h> #include <unistd.h> +#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<GUI::Menu> build_system_menu() })); system_menu->add_separator(); system_menu->add_action(GUI::Action::create("Exit...", [](auto&) { + Vector<char const*> command = PowerDialog::show(); + + if (command.size() == 0) + return; + if (fork() == 0) { - execl("/bin/SystemDialog", "/bin/SystemDialog", nullptr); + execv(command[0], const_cast<char* const*>(command.data())); ASSERT_NOT_REACHED(); } })); |