forked from MX-Linux/mx-datetime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatetime.cpp
More file actions
322 lines (289 loc) · 10.9 KB
/
datetime.cpp
File metadata and controls
322 lines (289 loc) · 10.9 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// MX Date/Time application.
//
// Copyright (C) 2019 by AK-47
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// This file is part of mx-datetime.
#include <QDebug>
#include <QDateTime>
#include <QFileInfo>
#include <QProcess>
#include <QTextCharFormat>
#include <QMessageBox>
#include "datetime.h"
#include "ui_datetime.h"
#include "version.h"
MXDateTime::MXDateTime(QWidget *parent) :
QDialog(parent),
ui(new Ui::MXDateTime)
{
ui->setupUi(this);
QTextCharFormat tcfmt;
tcfmt.setFontPointSize(ui->calendar->font().pointSizeF() * 0.75);
ui->calendar->setHeaderTextFormat(tcfmt);
is_systemd = (QFileInfo("/usr/bin/timedatectl").isExecutable()
&& execute("pidof systemd"));
is_openrc = QFileInfo::exists("/run/openrc");
// timezone
ui->cmbTimeZone->clear();
QFile file("/usr/share/zoneinfo/zone.tab");
if (file.open(QFile::ReadOnly | QFile::Text)) {
while(!file.atEnd()) {
const QString line(file.readLine().trimmed());
if(!line.startsWith('#')) {
ui->cmbTimeZone->addItem(line.section("\t", 2, 2));
}
}
file.close();
}
ui->cmbTimeZone->model()->sort(0);
// load the system values into the UI
ui->timeEdit->setDateTime(QDateTime::currentDateTime()); // avoids the sudden jump
QTimer::singleShot(0, this, &MXDateTime::loadSysTimeConfig);
}
MXDateTime::~MXDateTime()
{
delete ui;
}
// USER INTERFACE
void MXDateTime::secUpdate()
{
secUpdating = true;
ui->timeEdit->setDateTime(QDateTime::currentDateTime().addSecs(timeDelta));
timer->setInterval(1000 - QTime::currentTime().msec());
ui->calendar->setSelectedDate(ui->timeEdit->date());
secUpdating = false;
}
void MXDateTime::on_timeEdit_dateTimeChanged(const QDateTime &dateTime)
{
ui->clock->setTime(dateTime.time());
if (!secUpdating) {
timeDelta = QDateTime::currentDateTime().secsTo(dateTime);
if (!calChanging) timeChanged = true;
}
}
void MXDateTime::on_calendar_clicked(const QDate &date)
{
calChanging = true;
ui->timeEdit->setDateTime(QDateTime(date, ui->timeEdit->time()));
calChanging = false;
}
void MXDateTime::on_btnReadRTC_clicked()
{
setClockLock(true);
const QString btext = ui->btnReadRTC->text();
ui->btnReadRTC->setText(tr("Reading..."));
QByteArray rtcout;
execute("/sbin/hwclock --verbose", &rtcout);
isRTCUTC = rtcout.contains("\nHardware clock is on UTC time\n");
if (isRTCUTC) ui->radRTCUTC->setChecked(true);
else ui->radRTCLocal->setChecked(true);
ui->txtRTC->setPlainText(QString(rtcout.trimmed()));
ui->btnReadRTC->setText(btext);
setClockLock(false);
}
void MXDateTime::on_btnSystemToRTC_clicked()
{
setClockLock(true);
if (execute("/sbin/hwclock --systohc")) {
QMessageBox::information(this, windowTitle(), "Success.");
} else {
QMessageBox::warning(this, windowTitle(), "Failure.");
}
setClockLock(false);
}
void MXDateTime::on_btnRTCToSystem_clicked()
{
setClockLock(true);
if (execute("/sbin/hwclock -hctosys")) {
QMessageBox::information(this, windowTitle(), "Success.");
} else {
QMessageBox::warning(this, windowTitle(), "Failure.");
}
setClockLock(false);
}
void MXDateTime::on_btnSyncNTP_clicked()
{
setClockLock(true);
QString btext = ui->btnSyncNTP->text();
ui->btnSyncNTP->setText(tr("Synchronizing..."));
if (execute("/usr/sbin/ntpdate -u 0.pool.ntp.org")) {
QMessageBox::information(this, windowTitle(), "Success.");
} else {
QMessageBox::warning(this, windowTitle(), "Failure.");
}
ui->btnSyncNTP->setText(btext);
setClockLock(false);
}
void MXDateTime::on_btnClose_clicked()
{
qApp->exit(0);
}
void MXDateTime::on_btnApply_clicked()
{
// Set the date, and optionally, the time
if (timeDelta) {
QString cmd;
if (is_systemd) cmd = "timedatectl set-time ";
else cmd = "date -s ";
if (timeChanged) cmd += ui->timeEdit->dateTime().toString(Qt::ISODate);
else {
cmd += ui->timeEdit->date().toString(Qt::ISODate) + "T"
+ QTime::currentTime().toString(Qt::ISODate);
}
qDebug() << "Time set:" << cmd;
execute(cmd);
}
// Set the time zone if changed.
if (ui->cmbTimeZone->currentIndex() != ixTimeZone) {
const QString &newzone = ui->cmbTimeZone->currentText();
if (is_systemd) execute("timedatectl set-timezone " + newzone);
else {
execute("ln -nfs /usr/share/zoneinfo/posix/" + newzone + "/etc/localtime");
QFile file("/etc/timezone");
if (file.open(QFile::WriteOnly | QFile::Text)) {
file.write(newzone.toUtf8());
file.close();
}
}
}
// NTP settings
const bool ntp = ui->chkNTP->isChecked();
if (ntp != enabledNTP) {
if(is_systemd) {
execute("timedatectl set-ntp " + QString(ntp?"1":"0"));
} else if (is_openrc) {
if (QFile::exists("/etc/init.d/ntpd")) {
execute("rc-update " + QString(ntp?"add":"del") + " ntpd");
}
}
}
// RTC settings
const bool rtcUTC = ui->radRTCUTC->isChecked();
if (rtcUTC != isRTCUTC) {
if (is_systemd) {
execute("timedatectl set-local-rtc " + QString(rtcUTC?"0":"1"));
} else if(is_openrc) {
if(QFile::exists("/etc/conf.d/hwclock")) {
execute("sed -i \"s/clock=.*/clock=\\\"UTC\\\"/\" /etc/conf.d/hwclock");
}
}
execute("/sbin/hwclock --systohc --" + QString(rtcUTC?"utc":"localtime"));
}
// Refresh the UI with newly set values
loadSysTimeConfig();
}
// HELPER FUNCTIONS
void MXDateTime::loadSysTimeConfig()
{
QFile file("/etc/timezone");
if (file.open(QFile::ReadOnly | QFile::Text)) {
const QString line(file.readLine().trimmed());
ixTimeZone = ui->cmbTimeZone->findText(line);
ui->cmbTimeZone->setCurrentIndex(ixTimeZone);
file.close();
}
enabledNTP = execute("bash -c \"timedatectl | grep NTP | grep yes\"");
ui->chkNTP->setChecked(enabledNTP);
if (!(is_systemd || is_openrc)) ui->chkNTP->setEnabled(false);
on_btnReadRTC_clicked();
timer = new QTimer(this);
timeDelta = 0;
secUpdating = true;
connect(timer, &QTimer::timeout, this, QOverload<>::of(&MXDateTime::secUpdate));
ui->timeEdit->setDateTime(QDateTime::currentDateTime());
timeChanged = false;
timer->start(1000 - QTime::currentTime().msec());
}
void MXDateTime::setClockLock(bool locked)
{
if (locked) qApp->setOverrideCursor(QCursor(Qt::BusyCursor));
ui->tabDateTime->setDisabled(locked);
ui->tabSync->setDisabled(locked);
ui->btnApply->setDisabled(locked);
ui->btnClose->setDisabled(locked);
if (!locked) qApp->restoreOverrideCursor();
}
bool MXDateTime::execute(const QString &cmd, QByteArray *output)
{
QProcess proc(this);
QEventLoop eloop;
connect(&proc, static_cast<void (QProcess::*)(int)>(&QProcess::finished), &eloop, &QEventLoop::quit);
proc.start(cmd);
if (!output) proc.closeReadChannel(QProcess::StandardOutput);
proc.closeWriteChannel();
eloop.exec();
disconnect(&proc, static_cast<void (QProcess::*)(int)>(&QProcess::finished), 0, 0);
if (output) *output = proc.readAllStandardOutput();
qDebug() << "Execute" << cmd;
return (proc.exitStatus() == QProcess::NormalExit && proc.exitCode() == 0);
}
void MXDateTime::on_btnAbout_clicked()
{
this->hide();
QString url = "/usr/share/doc/mx-datetime/license.html";
QMessageBox msgBox(QMessageBox::NoIcon,
tr("About MX Date & Time"), "<p align=\"center\"><b><h2>" +
tr("MX Date & Time") + "</h2></b></p><p align=\"center\">" + tr("Version: ") + VERSION + "</p><p align=\"center\"><h3>" +
tr("GUI program for setting the time and date in MX Linux") +
"</h3></p><p align=\"center\"><a href=\"http://mxlinux.org\">http://mxlinux.org</a><br /></p><p align=\"center\">" +
tr("Copyright (c) MX Linux") + "<br /><br /></p>");
QPushButton *btnLicense = msgBox.addButton(tr("License"), QMessageBox::HelpRole);
QPushButton *btnChangelog = msgBox.addButton(tr("Changelog"), QMessageBox::HelpRole);
QPushButton *btnCancel = msgBox.addButton(tr("Cancel"), QMessageBox::NoRole);
btnCancel->setIcon(QIcon::fromTheme("window-close"));
msgBox.exec();
if (msgBox.clickedButton() == btnLicense) {
QString url = "/usr/share/doc/mx-datetime/license.html";
QByteArray user;
execute("logname", &user);
user = user.trimmed();
QString env_run = "su " + user + " -c \"env XDG_RUNTIME_DIR=/run/user/$(id -u " + user + ")";
if (system("command -v mx-viewer") == 0) { // use mx-viewer if available
system(env_run.toUtf8() + "mx-viewer " + url.toUtf8() + " " + tr("License").toUtf8() + "\"&");
} else {
system(env_run.toUtf8() + "xdg-open " + url.toUtf8() + "\"&");
}
} else if (msgBox.clickedButton() == btnChangelog) {
QDialog *changelog = new QDialog(this);
changelog->resize(600, 500);
QTextEdit *text = new QTextEdit;
text->setReadOnly(true);
QByteArray rtcout;
execute("zless /usr/share/doc/" + QFileInfo(QCoreApplication::applicationFilePath()).fileName() + "/changelog.gz", &rtcout);
text->setText(rtcout);
QPushButton *btnClose = new QPushButton(tr("&Close"));
btnClose->setIcon(QIcon::fromTheme("window-close"));
connect(btnClose, &QPushButton::clicked, changelog, &QDialog::close);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(text);
layout->addWidget(btnClose);
changelog->setLayout(layout);
changelog->exec();
}
this->show();
}
void MXDateTime::on_btnHelp_clicked()
{
QString url = "/usr/share/doc/mx-datetime/help/mx-datetime.html";
QString exec = "xdg-open";
if (system("command -v mx-viewer") == 0) { // use mx-viewer if available
exec = "mx-viewer";
url += " " + tr("MX Date \\& Time Help");
}
QByteArray user;
execute("logname", &user);
user = user.trimmed();
QString cmd = QString("su " + user + " -c \"env XDG_RUNTIME_DIR=/run/user/$(id -u " + user + ") " + exec + " " + url + "\"&");
system(cmd.toUtf8());
}