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) find_package(QCoro6 REQUIRED COMPONENTS DBus)
qcoro_enable_coroutines() qcoro_enable_coroutines()
kde_enable_exceptions()
pkg_check_modules(GOBJECT gobject-2.0 REQUIRED IMPORTED_TARGET) pkg_check_modules(GOBJECT gobject-2.0 REQUIRED IMPORTED_TARGET)
pkg_check_modules(GIO gio-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::KCMUtilsQuick
KF6::ConfigCore KF6::ConfigCore
KF6::I18n KF6::I18n
QCoro6::Core
QCoro6::DBus
) )

View file

@ -26,6 +26,9 @@
#include <KSharedConfig> #include <KSharedConfig>
#include <utility> #include <utility>
#include <QCoroDBus>
#include <QCoroTask>
#include "timedated_interface.h" #include "timedated_interface.h"
#define FORMAT24H "HH:mm:ss" #define FORMAT24H "HH:mm:ss"
@ -125,25 +128,24 @@ void TimeSettings::setUseNtp(bool ntp)
} }
} }
bool TimeSettings::saveTime() void TimeSettings::saveTime()
{ {
OrgFreedesktopTimedate1Interface timedateIface(QStringLiteral("org.freedesktop.timedate1"), auto timedateIface = std::make_shared<OrgFreedesktopTimedate1Interface>(QStringLiteral("org.freedesktop.timedate1"),
QStringLiteral("/org/freedesktop/timedate1"), QStringLiteral("/org/freedesktop/timedate1"),
QDBusConnection::systemBus()); 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 // 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 // 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 // timedated processes these in parallel and will return an error otherwise
auto reply = timedateIface.SetNTP(m_useNtp, true); auto reply = timedateIface->SetNTP(m_useNtp, true);
reply.waitForFinished(); auto r = reply;
if (reply.isError()) { QCoro::connect(std::move(reply), this, [=, this]() {
if (r.isError()) {
m_errorString = i18n("Unable to change NTP settings"); m_errorString = i18n("Unable to change NTP settings");
emit errorStringChanged(); emit errorStringChanged();
qWarning() << "Failed to enable NTP" << reply.error().name() << reply.error().message(); qWarning() << "Failed to enable NTP" << r.error().name() << r.error().message();
rc = false;
} }
if (!useNtp()) { if (!useNtp()) {
@ -152,19 +154,20 @@ bool TimeSettings::saveTime()
userTime.setDate(currentDate()); userTime.setDate(currentDate());
qDebug() << "Setting userTime: " << userTime; qDebug() << "Setting userTime: " << userTime;
qint64 timeDiff = userTime.toMSecsSinceEpoch() - QDateTime::currentMSecsSinceEpoch(); qint64 timeDiff = userTime.toMSecsSinceEpoch() - QDateTime::currentMSecsSinceEpoch();
//*1000 for milliseconds -> microseconds //*1000 for milliseconds -> microseconds
auto reply = timedateIface.SetTime(timeDiff * 1000, true, true); auto reply = timedateIface->SetTime(timeDiff * 1000, true, true);
reply.waitForFinished(); auto r = reply;
if (reply.isError()) { QCoro::connect(std::move(reply), this, [=, this]() {
if (r.isError()) {
m_errorString = i18n("Unable to set current time"); m_errorString = i18n("Unable to set current time");
emit errorStringChanged(); emit errorStringChanged();
qWarning() << "Failed to set current time" << reply.error().name() << reply.error().message(); qWarning() << "Failed to set current time" << r.error().name() << r.error().message();
rc = false;
} }
});
} }
saveTimeZone(m_timezone); saveTimeZone(m_timezone);
});
return rc;
} }
void TimeSettings::saveTimeZone(const QString &newtimezone) void TimeSettings::saveTimeZone(const QString &newtimezone)
@ -177,18 +180,20 @@ void TimeSettings::saveTimeZone(const QString &newtimezone)
if (!newtimezone.isEmpty()) { if (!newtimezone.isEmpty()) {
qDebug() << "Setting timezone: " << newtimezone; qDebug() << "Setting timezone: " << newtimezone;
auto reply = timedateIface.SetTimezone(newtimezone, true); auto reply = timedateIface.SetTimezone(newtimezone, true);
reply.waitForFinished(); auto r = reply;
if (reply.isError()) { QCoro::connect(std::move(reply), this, [=, this]() {
if (r.isError()) {
m_errorString = i18n("Unable to set timezone"); m_errorString = i18n("Unable to set timezone");
emit errorStringChanged(); emit errorStringChanged();
qWarning() << "Failed to set timezone" << reply.error().name() << reply.error().message(); qWarning() << "Failed to set timezone" << r.error().name() << r.error().message();
} } else {
}
setTimeZone(newtimezone); setTimeZone(newtimezone);
emit timeZoneChanged(); emit timeZoneChanged();
notify(); notify();
} }
});
}
}
QString TimeSettings::timeFormat() QString TimeSettings::timeFormat()
{ {

View file

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