File: timesettings.cpp

package info (click to toggle)
plasma-settings 23.01.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,256 kB
  • sloc: cpp: 4,943; xml: 42; sh: 6; makefile: 3
file content (282 lines) | stat: -rw-r--r-- 8,335 bytes parent folder | download
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
/*

    SPDX-FileCopyrightText: 2005 S.R.Haque <srhaque@iee.org>.
    SPDX-FileCopyrightText: 2009 David Faure <faure@kde.org>
    SPDX-FileCopyrightText: 2011-2015 Sebastian Kügler <sebas@kde.org>
    SPDX-FileCopyrightText: 2015 David Edmundson <davidedmundson@kde.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "timesettings.h"
#include "timezonemodel.h"

#include <QDebug>
#include <QtCore/QDate>

#include <QDBusConnection>
#include <QDBusMessage>
#include <QStandardItemModel>
#include <QTimer>
#include <QVariant>

#include <KAboutData>
#include <KConfigGroup>
#include <KLocalizedString>
#include <KPluginFactory>
#include <KSharedConfig>
#include <utility>

#include "timedated_interface.h"

#define FORMAT24H "HH:mm:ss"
#define FORMAT12H "h:mm:ss ap"

K_PLUGIN_FACTORY_WITH_JSON(TimeSettingsFactory, "timesettings.json", registerPlugin<TimeSettings>();)

TimeSettings::TimeSettings(QObject *parent, const QVariantList &args)
    : KQuickAddons::ConfigModule(parent, args)
    , m_useNtp(true)
{
    qDebug() << "time settings init";
    m_timeZonesModel = nullptr;
    setTimeZone(QTimeZone::systemTimeZone().id());

    KAboutData *about = new KAboutData(QStringLiteral("kcm_mobile_time"), i18n("Date and Time"), QStringLiteral("0.1"), QString(), KAboutLicense::LGPL);
    about->addAuthor(i18n("Sebastian Kügler"), QString(), QStringLiteral("sebas@kde.org"));
    setAboutData(about);
    setButtons(Apply | Default);

    qmlRegisterAnonymousType<TimeZoneModel>("org.kde.timesettings", 1);
    qmlRegisterAnonymousType<TimeZoneFilterProxy>("org.kde.timesettings", 1);

    initSettings();
    initTimeZones();
    qDebug() << "TimeSettings module loaded.";
}

TimeSettings::~TimeSettings()
{
}

void TimeSettings::initTimeZones()
{
    auto *filterModel = new TimeZoneFilterProxy(this);
    filterModel->setSourceModel(new TimeZoneModel(filterModel));
    setTimeZonesModel(filterModel);
}

void TimeSettings::initSettings()
{
    m_localeConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::SimpleConfig);
    m_localeSettings = KConfigGroup(m_localeConfig, "Locale");

    setTimeFormat(m_localeSettings.readEntry("TimeFormat", QStringLiteral(FORMAT24H))); // FIXME?!

    OrgFreedesktopTimedate1Interface timeDatedIface(QStringLiteral("org.freedesktop.timedate1"),
                                                    QStringLiteral("/org/freedesktop/timedate1"),
                                                    QDBusConnection::systemBus());
    // the server list is not relevant for timesyncd, it fetches it from the network
    m_useNtp = timeDatedIface.nTP();
}

void TimeSettings::timeout()
{
    setCurrentTime(QTime::currentTime());
    setCurrentDate(QDate::currentDate());
    notify();
}

QString TimeSettings::currentTimeText()
{
    return m_currentTimeText;
}

QTime TimeSettings::currentTime() const
{
    return m_currentTime;
}

void TimeSettings::setCurrentTime(const QTime &currentTime)
{
    if (m_currentTime != currentTime) {
        m_currentTime = currentTime;
        m_currentTimeText = QLocale().toString(QTime::currentTime(), m_timeFormat);
        emit currentTimeChanged();
    }
}

QDate TimeSettings::currentDate() const
{
    return m_currentDate;
}

void TimeSettings::setCurrentDate(const QDate &currentDate)
{
    if (m_currentDate != currentDate) {
        m_currentDate = currentDate;
        emit currentDateChanged();
    }
}

bool TimeSettings::useNtp() const
{
    return m_useNtp;
}

void TimeSettings::setUseNtp(bool ntp)
{
    if (m_useNtp != ntp) {
        m_useNtp = ntp;
        saveTime();
        emit useNtpChanged();
    }
}

bool TimeSettings::saveTime()
{
    OrgFreedesktopTimedate1Interface timedateIface(QStringLiteral("org.freedesktop.timedate1"),
                                                   QStringLiteral("/org/freedesktop/timedate1"),
                                                   QDBusConnection::systemBus());

    bool rc = true;
    // final arg in each method is "user-interaction" i.e whether it's OK for polkit to ask for auth

    // we cannot send requests up front then block for all replies as we need NTP to be disabled before we can make a call to SetTime
    // timedated processes these in parallel and will return an error otherwise

    auto reply = timedateIface.SetNTP(m_useNtp, true);
    reply.waitForFinished();
    if (reply.isError()) {
        m_errorString = i18n("Unable to change NTP settings");
        emit errorStringChanged();
        qWarning() << "Failed to enable NTP" << reply.error().name() << reply.error().message();
        rc = false;
    }

    if (!useNtp()) {
        QDateTime userTime;
        userTime.setTime(currentTime());
        userTime.setDate(currentDate());
        qDebug() << "Setting userTime: " << userTime;
        qint64 timeDiff = userTime.toMSecsSinceEpoch() - QDateTime::currentMSecsSinceEpoch();
        //*1000 for milliseconds -> microseconds
        auto reply = timedateIface.SetTime(timeDiff * 1000, true, true);
        reply.waitForFinished();
        if (reply.isError()) {
            m_errorString = i18n("Unable to set current time");
            emit errorStringChanged();
            qWarning() << "Failed to set current time" << reply.error().name() << reply.error().message();
            rc = false;
        }
    }
    saveTimeZone(m_timezone);

    return rc;
}

void TimeSettings::saveTimeZone(const QString &newtimezone)
{
    qDebug() << "Saving timezone to config: " << newtimezone;
    OrgFreedesktopTimedate1Interface timedateIface(QStringLiteral("org.freedesktop.timedate1"),
                                                   QStringLiteral("/org/freedesktop/timedate1"),
                                                   QDBusConnection::systemBus());

    if (!newtimezone.isEmpty()) {
        qDebug() << "Setting timezone: " << newtimezone;
        auto reply = timedateIface.SetTimezone(newtimezone, true);
        reply.waitForFinished();
        if (reply.isError()) {
            m_errorString = i18n("Unable to set timezone");
            emit errorStringChanged();
            qWarning() << "Failed to set timezone" << reply.error().name() << reply.error().message();
        }
    }

    setTimeZone(newtimezone);
    emit timeZoneChanged();
    notify();
}

QString TimeSettings::timeFormat()
{
    return m_timeFormat;
}

void TimeSettings::setTimeFormat(const QString &timeFormat)
{
    if (m_timeFormat != timeFormat) {
        m_timeFormat = timeFormat;

        m_localeSettings.writeEntry("TimeFormat", timeFormat, KConfigGroup::Notify);
        m_localeConfig->sync();

        QDBusMessage msg =
            QDBusMessage::createSignal(QStringLiteral("/org/kde/kcmshell_clock"), QStringLiteral("org.kde.kcmshell_clock"), QStringLiteral("clockUpdated"));
        QDBusConnection::sessionBus().send(msg);

        qDebug() << "time format is now: " << QLocale().toString(QTime::currentTime(), m_timeFormat);
        emit timeFormatChanged();
        timeout();
    }
}

QString TimeSettings::timeZone()
{
    return m_timezone;
}

void TimeSettings::setTimeZone(const QString &timezone)
{
    if (m_timezone != timezone) {
        m_timezone = timezone;
        qDebug() << "timezone changed to: " << timezone;
        emit timeZoneChanged();
        timeout();
    }
}

TimeZoneFilterProxy *TimeSettings::timeZonesModel()
{
    return m_timeZonesModel;
}

void TimeSettings::setTimeZonesModel(TimeZoneFilterProxy *timezones)
{
    m_timeZonesModel = timezones;
    emit timeZonesModelChanged();
}

bool TimeSettings::twentyFour()
{
    return timeFormat() == QStringLiteral(FORMAT24H);
}

void TimeSettings::setTwentyFour(bool t)
{
    if (twentyFour() != t) {
        if (t) {
            setTimeFormat(FORMAT24H);
        } else {
            setTimeFormat(FORMAT12H);
        }
        qDebug() << "T24 toggled: " << t << m_timeFormat;
        emit twentyFourChanged();
        emit currentTimeChanged();
        timeout();
    }
}

QString TimeSettings::errorString()
{
    return m_errorString;
}

void TimeSettings::notify()
{
    const QDBusMessage msg =
        QDBusMessage::createSignal(QStringLiteral("/org/kde/kcmshell_clock"), QStringLiteral("org.kde.kcmshell_clock"), QStringLiteral("clockUpdated"));
    QDBusConnection::sessionBus().send(msg);
}

#include "timesettings.moc"