mirror of
https://invent.kde.org/marcoa/shift-shell.git
synced 2026-07-26 14:24:45 +00:00
kcms: time: Avoid calling waitForFinished
This commit is contained in:
parent
48dd3ce33e
commit
d919403366
4 changed files with 53 additions and 42 deletions
|
|
@ -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)
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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,46 +128,46 @@ 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]() {
|
||||||
m_errorString = i18n("Unable to change NTP settings");
|
if (r.isError()) {
|
||||||
emit errorStringChanged();
|
m_errorString = i18n("Unable to change NTP settings");
|
||||||
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();
|
emit errorStringChanged();
|
||||||
qWarning() << "Failed to set current time" << reply.error().name() << reply.error().message();
|
qWarning() << "Failed to enable NTP" << r.error().name() << r.error().message();
|
||||||
rc = false;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
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)
|
void TimeSettings::saveTimeZone(const QString &newtimezone)
|
||||||
|
|
@ -177,17 +180,19 @@ 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]() {
|
||||||
m_errorString = i18n("Unable to set timezone");
|
if (r.isError()) {
|
||||||
emit errorStringChanged();
|
m_errorString = i18n("Unable to set timezone");
|
||||||
qWarning() << "Failed to set timezone" << reply.error().name() << reply.error().message();
|
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()
|
QString TimeSettings::timeFormat()
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue