summaryrefslogtreecommitdiff
path: root/ui/qt5/datetimepage.cc
blob: fb027aa35c374157b06e834f62dd419de2141c75 (plain) (blame)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * datetimepage.cc - Implementation of the UI.SysMeta.DateTime page
 * horizon-qt5, the Qt 5 user interface for
 * Project Horizon
 *
 * Copyright (c) 2019 Adélie Linux and contributors.  All rights reserved.
 * This code is licensed under the AGPL 3.0 license, as noted in the
 * LICENSE-code file in the root directory of this repository.
 *
 * SPDX-License-Identifier: AGPL-3.0-only
 */

#include "datetimepage.hh"

#include <QCheckBox>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QTimeZone>
#include <QVBoxLayout>

#ifdef HAS_INSTALL_ENV
#   include <sys/capability.h>
#   include <time.h>
#endif  /* HAS_INSTALL_ENV */


TimeZone::TimeZone() : ianaName(), friendlyName(), offset(0) {}

TimeZone::TimeZone(QByteArray iana) {
    QTimeZone zone(iana);
    QString offset, longName, comment;
    this->ianaName = iana;

    offset = zone.displayName(QTimeZone::GenericTime, QTimeZone::OffsetName);
    this->friendlyName = "(" + offset + ") " + iana;
    longName = zone.displayName(QTimeZone::GenericTime, QTimeZone::LongName);
    if(longName != offset) {
        this->friendlyName += " - " + longName;
    }

    comment = zone.comment();
    if(comment.size() > 0 && comment != offset) {
        this->friendlyName += " (" + comment + ")";
    }

    this->offset = zone.standardTimeOffset(QDateTime::currentDateTimeUtc());
}


TimeZoneModel::TimeZoneModel(QWidget *parent) : QAbstractListModel(parent) {
    for(auto &iana : QTimeZone::availableTimeZoneIds()) {
        /* we don't support raw timezones because tzdata doesn't */
        if(iana.startsWith("UTC") && iana.size() > 3) continue;
        TimeZone tzObj(iana);
        zones.push_back(tzObj);
    }

    std::sort(zones.begin(), zones.end(), [](TimeZone tz1, TimeZone tz2) {
        if(tz1.offset < tz2.offset) return true;
        if(tz2.offset < tz1.offset) return false;
        return tz1.friendlyName < tz2.friendlyName;
    });
}

int TimeZoneModel::rowCount(const QModelIndex &) const {
    return zones.size();
}

QVariant TimeZoneModel::data(const QModelIndex &index, int role) const {
    if(!index.isValid()) {
        return QVariant();
    }

    if(index.row() > zones.size()) {
        return QVariant();
    }

    TimeZone zone = zones.at(index.row());

    switch(role)
    {
    case Qt::DisplayRole:
        return zone.friendlyName;
    case Qt::ToolTipRole:
        return QString(zone.ianaName);
    default:
        return QVariant();
    }
}

QVariant TimeZoneModel::headerData(int, Qt::Orientation, int role) const {
    if(role != Qt::DisplayRole) {
        return QVariant();
    }

    return QString("Time Zone");
}


/*! Try to gain CAP_SYS_TIME capability.
 * If we do, then enable the date/time edit boxes.
 * This does nothing in the Runtime Environment.
 */
void DateTimePage::maybeRaiseCap() {
#ifdef HAS_INSTALL_ENV
    cap_t captain;
    cap_value_t time_cap = CAP_SYS_TIME;

    if(!CAP_IS_SUPPORTED(CAP_SYS_TIME))
        return;

    captain = cap_get_proc();
    if(captain == nullptr)
        return;

    if(cap_set_flag(captain, CAP_EFFECTIVE, 1, &time_cap, CAP_SET) == -1) {
        cap_free(captain);
        return;
    }

    if(cap_set_proc(captain)) {
        cap_free(captain);
        return;
    }

    cap_free(captain);

    dateEdit->setEnabled(true);
    timeEdit->setEnabled(true);
#endif  /* HAS_INSTALL_ENV */
}

DateTimePage::DateTimePage(QWidget *parent) : HorizonWizardPage(parent) {
    setTitle(tr("Date and Time Settings"));
    loadWatermark("intro");

    dateEdit = new QDateEdit(QDate::currentDate());
    dateEdit->setDisplayFormat("dd MMMM yyyy");
    dateEdit->setEnabled(false);
    timeEdit = new QTimeEdit(QTime::currentTime());
    timeEdit->setDisplayFormat("HH:mm:ss");
    timeEdit->setEnabled(false);

#ifdef HAS_INSTALL_ENV
    /* explanations:
     *
     * isEnabled check:
     *     to prevent wasting time in the case where we don't have perms
     *
     * Qt::UTC:
     *     CLOCK_REALTIME is always UTC, so we need the UTC form of the
     *     current date/time.
     */
    connect(dateEdit, &QDateEdit::dateChanged, [=](const QDate &date) {
        if(dateEdit->isEnabled() && date != QDate::currentDate()) {
            QDateTime *newDT = new QDateTime(date, QTime::currentTime(),
                                             Qt::UTC);
            struct timespec ts = {newDT->toSecsSinceEpoch(), 0};
            clock_settime(CLOCK_REALTIME, &ts);
        }
    });
    connect(timeEdit, &QTimeEdit::timeChanged, [=](const QTime &qtime) {
        if(timeEdit->isEnabled() && qtime != QTime::currentTime()) {
            QDateTime *newDT = new QDateTime(QDate::currentDate(), qtime);
            struct timespec ts = {newDT->toSecsSinceEpoch(), 0};
            clock_settime(CLOCK_REALTIME, &ts);
        }
    });
#endif  /* HAS_INSTALL_ENV */

    updateTimer = new QTimer(this);
    updateTimer->setInterval(1000);
    connect(updateTimer, &QTimer::timeout, [=]{
        dateEdit->setDate(QDate::currentDate());
        timeEdit->setTime(QTime::currentTime());
    });

    QHBoxLayout *dateTimeLayout = new QHBoxLayout;
    dateTimeLayout->addWidget(dateEdit);
    dateTimeLayout->addWidget(timeEdit);
    QGroupBox *dateTimeGroup = new QGroupBox(tr("Date and Time"));
    dateTimeGroup->setLayout(dateTimeLayout);

    QLineEdit *timeZoneSearch = new QLineEdit;
    timeZoneSearch->addAction(QIcon::fromTheme("edit-find"),
                              QLineEdit::LeadingPosition);
    timeZoneSearch->setPlaceholderText(tr("Search for a time zone"));
    sortModel = new QSortFilterProxyModel(this);
    sortModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
    sortModel->setSourceModel(&zoneModel);
    connect(timeZoneSearch, &QLineEdit::textChanged, [=](const QString &text) {
        sortModel->setFilterFixedString(text);
    });
    timeZoneList = new QListView;
    timeZoneList->setModel(sortModel);
    timeZoneList->setSelectionMode(QAbstractItemView::SingleSelection);
    connect(timeZoneList->selectionModel(), &QItemSelectionModel::currentChanged,
            [=]() {
        emit timezoneChanged();
    });

    registerField("timezone*", this, "selectedTimeZone", SIGNAL(timezoneChanged()));

    QVBoxLayout *timeZoneLayout = new QVBoxLayout;
    timeZoneLayout->addWidget(timeZoneSearch);
    timeZoneLayout->addWidget(timeZoneList);
    QGroupBox *timeZoneGroup = new QGroupBox(tr("Time Zone"));
    timeZoneGroup->setLayout(timeZoneLayout);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addStretch();
    mainLayout->addWidget(dateTimeGroup);
    mainLayout->addStretch();
    mainLayout->addWidget(timeZoneGroup);
    mainLayout->addStretch();
    setLayout(mainLayout);

    maybeRaiseCap();
}

QString DateTimePage::selectedTimeZone() {
    QModelIndex curr = timeZoneList->selectionModel()->currentIndex();
    return sortModel->data(curr, Qt::ToolTipRole).toString();
}

void DateTimePage::initializePage() {
    updateTimer->start();
}

void DateTimePage::cleanupPage() {
    updateTimer->stop();
}