summaryrefslogtreecommitdiff
path: root/Userland/Applications/ClockSettings/ClockSettingsWidget.cpp
blob: b851bcd906cbef7fabf347ca70b7dba39cc54e83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 * Copyright (c) 2022, Tim Flynn <trflynn89@pm.me>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "ClockSettingsWidget.h"
#include <Applications/ClockSettings/ClockSettingsWidgetGML.h>
#include <LibGUI/ComboBox.h>
#include <LibGUI/ItemListModel.h>
#include <LibGUI/Label.h>
#include <LibTimeZone/TimeZone.h>
#include <spawn.h>
#include <unistd.h>

using StringViewListModel = GUI::ItemListModel<StringView, Span<StringView const>>;

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<GUI::ComboBox>("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<char**>(argv), environ))) {
        perror("posix_spawn");
        exit(1);
    }
}