DateTime: add auto sync option

pull/26/head
reionwong 4 years ago
parent bdfb797c41
commit f942ce99c0

@ -45,6 +45,7 @@ set(SRCS
src/cursor/cursorthememodel.cpp
src/cursor/cursortheme.cpp
src/cursor/mouse.cpp
src/datetime/time.cpp
src/datetime/timezonedata.h
src/datetime/timezonemap.cpp
src/datetime/timedated_interface.cpp

@ -20,6 +20,7 @@
#include "cursor/cursorthememodel.h"
#include "cursor/mouse.h"
#include "datetime/time.h"
#include "datetime/timezonemap.h"
static QObject *passwordSingleton(QQmlEngine *engine, QJSEngine *scriptEngine)
@ -71,6 +72,7 @@ Application::Application(int &argc, char **argv)
qmlRegisterType<Fonts>(uri, 1, 0, "Fonts");
qmlRegisterType<PowerManager>(uri, 1, 0, "PowerManager");
qmlRegisterType<Mouse>(uri, 1, 0, "Mouse");
qmlRegisterType<Time>(uri, 1, 0, "Time");
qmlRegisterType<TimeZoneMap>(uri, 1, 0, "TimeZoneMap");
qmlRegisterSingletonType<Password>(uri, 1, 0, "Password", passwordSingleton);
qmlRegisterType<QAbstractItemModel>();

@ -0,0 +1,90 @@
/*
* Copyright (C) 2021 CutefishOS Team.
*
* Author: Reion Wong <reionwong@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "time.h"
#include "timedated_interface.h"
#include <QDateTime>
Time::Time(QObject *parent)
: QObject(parent)
{
OrgFreedesktopTimedate1Interface iface(QStringLiteral("org.freedesktop.timedate1"),
QStringLiteral("/org/freedesktop/timedate1"),
QDBusConnection::systemBus());
m_useNtp = iface.nTP();
}
bool Time::useNtp() const
{
return m_useNtp;
}
void Time::setUseNtp(bool enabled)
{
if (m_useNtp != enabled) {
m_useNtp = enabled;
save();
emit useNtpChanged();
}
}
void Time::save()
{
OrgFreedesktopTimedate1Interface iface(QStringLiteral("org.freedesktop.timedate1"),
QStringLiteral("/org/freedesktop/timedate1"),
QDBusConnection::systemBus());
auto reply = iface.SetNTP(m_useNtp, true);
if (!m_useNtp) {
QDateTime userTime;
userTime.setTime(currentTime());
userTime.setDate(currentDate());
qint64 timeDiff = userTime.toMSecsSinceEpoch() - QDateTime::currentMSecsSinceEpoch();
//*1000 for milliseconds -> microseconds
auto reply = iface.SetTime(timeDiff * 1000, true, true);
reply.waitForFinished();
}
}
QTime Time::currentTime() const
{
return m_currentTime;
}
void Time::setCurrentTime(const QTime &currentTime)
{
if (m_currentTime != currentTime) {
m_currentTime = currentTime;
emit currentDateChanged();
}
}
QDate Time::currentDate() const
{
return m_currentDate;
}
void Time::setCurrentDate(const QDate &currentDate)
{
if (m_currentDate != currentDate) {
m_currentDate = currentDate;
emit currentDateChanged();
}
}

@ -0,0 +1,59 @@
/*
* Copyright (C) 2021 CutefishOS Team.
*
* Author: Reion Wong <reionwong@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TIME_H
#define TIME_H
#include <QObject>
#include <QTime>
#include <QDate>
class Time : public QObject
{
Q_OBJECT
Q_PROPERTY(bool useNtp READ useNtp WRITE setUseNtp NOTIFY useNtpChanged)
Q_PROPERTY(QTime currentTime READ currentTime WRITE setCurrentTime NOTIFY currentTimeChanged)
Q_PROPERTY(QDate currentDate READ currentDate WRITE setCurrentDate NOTIFY currentDateChanged)
public:
explicit Time(QObject *parent = nullptr);
bool useNtp() const;
void setUseNtp(bool enabled);
Q_INVOKABLE void save();
QTime currentTime() const;
void setCurrentTime(const QTime &currentTime);
QDate currentDate() const;
void setCurrentDate(const QDate &currentDate);
signals:
void useNtpChanged();
void currentTimeChanged();
void currentDateChanged();
private:
bool m_useNtp;
QTime m_currentTime;
QDate m_currentDate;
};
#endif // TIME_H

@ -120,11 +120,21 @@ void TimeZoneMap::clicked(int x, int y, int width, int height)
void TimeZoneMap::setTimeZone(QString value)
{
OrgFreedesktopTimedate1Interface timedateIface(QStringLiteral("org.freedesktop.timedate1"),
if (value.isEmpty())
return;
OrgFreedesktopTimedate1Interface iface(QStringLiteral("org.freedesktop.timedate1"),
QStringLiteral("/org/freedesktop/timedate1"),
QDBusConnection::systemBus());
timedateIface.SetTimezone(value, true);
auto reply = iface.SetTimezone(value, true);
reply.waitForFinished();
if (reply.isError()) {
qWarning() << "Failed to set timezone" << reply.error().message();
return;
}
m_currentTimeZone = QTimeZone(value.toLatin1()).id();
emit currentTimeZoneChanged();
}

@ -35,6 +35,10 @@ ItemPage {
id: timeZoneMap
}
Time {
id: time
}
Scrollable {
anchors.fill: parent
contentHeight: layout.implicitHeight
@ -42,6 +46,27 @@ ItemPage {
ColumnLayout {
id: layout
anchors.fill: parent
spacing: FishUI.Units.largeSpacing
RoundedItem {
RowLayout {
Label {
text: qsTr("Auto Sync")
}
Item {
Layout.fillWidth: true
}
Switch {
Layout.fillHeight: true
rightPadding: 0
rightInset: 0
Component.onCompleted: checked = time.useNtp
onCheckedChanged: time.useNtp = checked
}
}
}
StandardButton {
Layout.fillWidth: true
@ -50,8 +75,8 @@ ItemPage {
RowLayout {
anchors.fill: parent
anchors.leftMargin: FishUI.Units.largeSpacing
anchors.rightMargin: FishUI.Units.largeSpacing
anchors.leftMargin: FishUI.Units.largeSpacing * 1.5
anchors.rightMargin: FishUI.Units.largeSpacing * 1.5
Label {
text: qsTr("Time Zone")

@ -524,7 +524,12 @@
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/qml/DateTime/Main.qml" line="57"/>
<location filename="../src/qml/DateTime/Main.qml" line="54"/>
<source>Auto Sync</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/qml/DateTime/Main.qml" line="82"/>
<source>Time Zone</source>
<translation type="unfinished"></translation>
</message>

@ -524,7 +524,12 @@
<translation></translation>
</message>
<message>
<location filename="../src/qml/DateTime/Main.qml" line="57"/>
<location filename="../src/qml/DateTime/Main.qml" line="54"/>
<source>Auto Sync</source>
<translation></translation>
</message>
<message>
<location filename="../src/qml/DateTime/Main.qml" line="82"/>
<source>Time Zone</source>
<translation></translation>
</message>

Loading…
Cancel
Save