kcms: time: Avoid calling waitForFinished

This commit is contained in:
Jonah Brüchert 2023-12-29 13:19:44 +01:00 committed by Devin Lin
parent 48dd3ce33e
commit d919403366
4 changed files with 53 additions and 42 deletions

View file

@ -79,6 +79,7 @@ find_package(PkgConfig REQUIRED)
find_package(QCoro6 REQUIRED COMPONENTS DBus)
qcoro_enable_coroutines()
kde_enable_exceptions()
pkg_check_modules(GOBJECT gobject-2.0 REQUIRED IMPORTED_TARGET)
pkg_check_modules(GIO gio-2.0 REQUIRED IMPORTED_TARGET)

View file

@ -19,5 +19,7 @@ target_link_libraries(kcm_mobile_time PRIVATE
KF6::KCMUtilsQuick
KF6::ConfigCore
KF6::I18n
QCoro6::Core
QCoro6::DBus
)

View file

@ -26,6 +26,9 @@
#include <KSharedConfig>
#include <utility>
#include <QCoroDBus>
#include <QCoroTask>
#include "timedated_interface.h"
#define FORMAT24H "HH:mm:ss"
@ -125,46 +128,46 @@ void TimeSettings::setUseNtp(bool ntp)
}
}
bool TimeSettings::saveTime()
void TimeSettings::saveTime()
{
OrgFreedesktopTimedate1Interface timedateIface(QStringLiteral("org.freedesktop.timedate1"),
QStringLiteral("/org/freedesktop/timedate1"),
QDBusConnection::systemBus());
auto timedateIface = std::make_shared<OrgFreedesktopTimedate1Interface>(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");
auto reply = timedateIface->SetNTP(m_useNtp, true);
auto r = reply;
QCoro::connect(std::move(reply), this, [=, this]() {
if (r.isError()) {
m_errorString = i18n("Unable to change NTP settings");
emit errorStringChanged();
qWarning() << "Failed to set current time" << reply.error().name() << reply.error().message();
rc = false;
qWarning() << "Failed to enable NTP" << r.error().name() << r.error().message();
}
}
saveTimeZone(m_timezone);
return rc;
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);
auto r = reply;
QCoro::connect(std::move(reply), this, [=, this]() {
if (r.isError()) {
m_errorString = i18n("Unable to set current time");
emit errorStringChanged();
qWarning() << "Failed to set current time" << r.error().name() << r.error().message();
}
});
}
saveTimeZone(m_timezone);
});
}
void TimeSettings::saveTimeZone(const QString &newtimezone)
@ -177,17 +180,19 @@ void TimeSettings::saveTimeZone(const QString &newtimezone)
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();
}
auto r = reply;
QCoro::connect(std::move(reply), this, [=, this]() {
if (r.isError()) {
m_errorString = i18n("Unable to set timezone");
emit errorStringChanged();
qWarning() << "Failed to set timezone" << r.error().name() << r.error().message();
} else {
setTimeZone(newtimezone);
emit timeZoneChanged();
notify();
}
});
}
setTimeZone(newtimezone);
emit timeZoneChanged();
notify();
}
QString TimeSettings::timeFormat()

View file

@ -20,6 +20,9 @@
#include <KQuickConfigModule>
#include <QCoroQmlTask>
#include <QCoroTask>
#include "timezonemodel.h"
// #include "settingsmodule.h"
@ -73,9 +76,9 @@ public Q_SLOTS:
void setTimeFormat(const QString &timeFormat);
void setTwentyFour(bool t);
void timeout();
bool saveTime();
void saveTime();
void notify();
Q_INVOKABLE void saveTimeZone(const QString &newtimezone);
void saveTimeZone(const QString &newtimezone);
Q_SIGNALS:
void currentTimeTextChanged();