diff --git a/CMakeLists.txt b/CMakeLists.txt index d1e58638..13a6412b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/kcms/time/CMakeLists.txt b/kcms/time/CMakeLists.txt index 3a6d6ec7..a53e878f 100644 --- a/kcms/time/CMakeLists.txt +++ b/kcms/time/CMakeLists.txt @@ -19,5 +19,7 @@ target_link_libraries(kcm_mobile_time PRIVATE KF6::KCMUtilsQuick KF6::ConfigCore KF6::I18n + QCoro6::Core + QCoro6::DBus ) diff --git a/kcms/time/timesettings.cpp b/kcms/time/timesettings.cpp index 2c2cea18..33317a34 100644 --- a/kcms/time/timesettings.cpp +++ b/kcms/time/timesettings.cpp @@ -26,6 +26,9 @@ #include #include +#include +#include + #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(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() diff --git a/kcms/time/timesettings.h b/kcms/time/timesettings.h index c3cc8248..07312fdc 100644 --- a/kcms/time/timesettings.h +++ b/kcms/time/timesettings.h @@ -20,6 +20,9 @@ #include +#include +#include + #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();