From d36555983995c31d492bc77f94f8ace7aa9b33b4 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 20 Jan 2022 08:23:55 -0500 Subject: ClockSettings: Add a GUI application to set the system time zone This application can be expanded with other clock-related options. For an initial iteration, it has just an option to change the time zone. --- Userland/Applications/CMakeLists.txt | 1 + Userland/Applications/ClockSettings/CMakeLists.txt | 17 +++++++ .../ClockSettings/ClockSettingsWidget.cpp | 53 ++++++++++++++++++++++ .../ClockSettings/ClockSettingsWidget.gml | 40 ++++++++++++++++ .../ClockSettings/ClockSettingsWidget.h | 27 +++++++++++ Userland/Applications/ClockSettings/main.cpp | 34 ++++++++++++++ 6 files changed, 172 insertions(+) create mode 100644 Userland/Applications/ClockSettings/CMakeLists.txt create mode 100644 Userland/Applications/ClockSettings/ClockSettingsWidget.cpp create mode 100644 Userland/Applications/ClockSettings/ClockSettingsWidget.gml create mode 100644 Userland/Applications/ClockSettings/ClockSettingsWidget.h create mode 100644 Userland/Applications/ClockSettings/main.cpp (limited to 'Userland') diff --git a/Userland/Applications/CMakeLists.txt b/Userland/Applications/CMakeLists.txt index b9ccd64c6e..281212205b 100644 --- a/Userland/Applications/CMakeLists.txt +++ b/Userland/Applications/CMakeLists.txt @@ -7,6 +7,7 @@ add_subdirectory(BrowserSettings) add_subdirectory(Calculator) add_subdirectory(Calendar) add_subdirectory(CharacterMap) +add_subdirectory(ClockSettings) add_subdirectory(CrashReporter) add_subdirectory(Debugger) add_subdirectory(DisplaySettings) diff --git a/Userland/Applications/ClockSettings/CMakeLists.txt b/Userland/Applications/ClockSettings/CMakeLists.txt new file mode 100644 index 0000000000..8979e0cb1d --- /dev/null +++ b/Userland/Applications/ClockSettings/CMakeLists.txt @@ -0,0 +1,17 @@ +serenity_component( + ClockSettings + RECOMMENDED + TARGETS ClockSettings +) + +compile_gml(ClockSettingsWidget.gml ClockSettingsWidgetGML.h clock_settings_widget_gml) + +set(SOURCES + main.cpp + ClockSettingsWidget.cpp + ClockSettingsWidget.h + ClockSettingsWidgetGML.h +) + +serenity_app(ClockSettings ICON app-analog-clock) # FIXME: Create a ClockSettings icon. +target_link_libraries(ClockSettings LibGUI LibMain) diff --git a/Userland/Applications/ClockSettings/ClockSettingsWidget.cpp b/Userland/Applications/ClockSettings/ClockSettingsWidget.cpp new file mode 100644 index 0000000000..b851bcd906 --- /dev/null +++ b/Userland/Applications/ClockSettings/ClockSettingsWidget.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2022, Tim Flynn + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "ClockSettingsWidget.h" +#include +#include +#include +#include +#include +#include +#include + +using StringViewListModel = GUI::ItemListModel>; + +ClockSettingsWidget::ClockSettingsWidget() +{ + load_from_gml(clock_settings_widget_gml); + + static auto time_zones = TimeZone::all_time_zones(); + m_time_zone = TimeZone::current_time_zone(); + + m_time_zone_combo_box = *find_descendant_of_type_named("time_zone_input"); + m_time_zone_combo_box->set_only_allow_values_from_model(true); + m_time_zone_combo_box->set_model(*StringViewListModel::create(time_zones)); + m_time_zone_combo_box->set_text(m_time_zone); +} + +void ClockSettingsWidget::reset_default_values() +{ + m_time_zone = "UTC"sv; + m_time_zone_combo_box->set_text(m_time_zone); + set_time_zone(); +} + +void ClockSettingsWidget::apply_settings() +{ + m_time_zone = m_time_zone_combo_box->text(); + set_time_zone(); +} + +void ClockSettingsWidget::set_time_zone() const +{ + 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(argv), environ))) { + perror("posix_spawn"); + exit(1); + } +} diff --git a/Userland/Applications/ClockSettings/ClockSettingsWidget.gml b/Userland/Applications/ClockSettings/ClockSettingsWidget.gml new file mode 100644 index 0000000000..3772d49899 --- /dev/null +++ b/Userland/Applications/ClockSettings/ClockSettingsWidget.gml @@ -0,0 +1,40 @@ +@GUI::Frame { + fill_with_background_color: true + + layout: @GUI::VerticalBoxLayout { + margins: [10] + spacing: 5 + } + + @GUI::GroupBox { + title: "Time Zone Settings" + fixed_height: 120 + + layout: @GUI::VerticalBoxLayout { + margins: [16, 8, 8] + spacing: 16 + } + + @GUI::Label { + text: "Change the system's time zone used for the clock and other applications." + text_alignment: "TopLeft" + } + + @GUI::Widget { + layout: @GUI::HorizontalBoxLayout { + spacing: 4 + } + + @GUI::Label { + text: "Time Zone:" + fixed_width: 80 + name: "time_zone_label" + text_alignment: "CenterLeft" + } + + @GUI::ComboBox { + name: "time_zone_input" + } + } + } +} diff --git a/Userland/Applications/ClockSettings/ClockSettingsWidget.h b/Userland/Applications/ClockSettings/ClockSettingsWidget.h new file mode 100644 index 0000000000..9315da7f25 --- /dev/null +++ b/Userland/Applications/ClockSettings/ClockSettingsWidget.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2022, Tim Flynn + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include +#include +#include + +class ClockSettingsWidget final : public GUI::SettingsWindow::Tab { + C_OBJECT(ClockSettingsWidget) + +private: + ClockSettingsWidget(); + + virtual void apply_settings() override; + virtual void reset_default_values() override; + void set_time_zone() const; + + String m_time_zone; + RefPtr m_time_zone_combo_box; +}; diff --git a/Userland/Applications/ClockSettings/main.cpp b/Userland/Applications/ClockSettings/main.cpp new file mode 100644 index 0000000000..ec69a4f08b --- /dev/null +++ b/Userland/Applications/ClockSettings/main.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022, Tim Flynn + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "ClockSettingsWidget.h" +#include +#include +#include +#include +#include + +ErrorOr serenity_main(Main::Arguments arguments) +{ + TRY(Core::System::pledge("stdio rpath recvfd sendfd unix proc exec")); + + auto app = TRY(GUI::Application::try_create(arguments)); + + TRY(Core::System::pledge("stdio rpath recvfd sendfd proc exec")); + TRY(Core::System::unveil("/res", "r")); + TRY(Core::System::unveil("/bin/timezone", "x")); + TRY(Core::System::unveil("/etc/timezone", "r")); + TRY(Core::System::unveil(nullptr, nullptr)); + + auto app_icon = GUI::Icon::default_icon("app-analog-clock"); // FIXME: Create a ClockSettings icon. + + auto window = TRY(GUI::SettingsWindow::create("Clock Settings", GUI::SettingsWindow::ShowDefaultsButton::Yes)); + (void)TRY(window->add_tab("Clock")); + window->set_icon(app_icon.bitmap_for_size(16)); + + window->show(); + return app->exec(); +} -- cgit v1.2.3