From 4b91f2c07eced1fbd7bbc3cf3b0e0afb38959de7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0tikonas?= Date: Wed, 21 Mar 2018 21:51:30 +0100 Subject: [PATCH] Convert copyblocks dbus call to asynchronous call. --- src/util/externalcommand.cpp | 28 +++++++++++++++++++--------- src/util/externalcommandhelper.cpp | 7 ++----- src/util/externalcommandhelper.h | 2 +- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/util/externalcommand.cpp b/src/util/externalcommand.cpp index 100ad72..1400cfb 100644 --- a/src/util/externalcommand.cpp +++ b/src/util/externalcommand.cpp @@ -77,17 +77,27 @@ bool ExternalCommand::copyBlocks() QDBusInterface iface(QStringLiteral("org.kde.kpmcore.helperinterface"), QStringLiteral("/Helper"), QStringLiteral("org.kde.kpmcore.externalcommand"), QDBusConnection::systemBus()); iface.setTimeout(10 * 24 * 3600 * 1000); // 10 days if (iface.isValid()) { - QDBusReply reply = iface.call(QStringLiteral("copyblocks"), CoreBackendManager::self()->Uuid(), m_Source->path(), m_Source->firstByte(), m_Source->length(), m_Target->path(), m_Target->firstByte(), blockSize); - if (reply.isValid()) { - rval = reply.value()[QStringLiteral("success")].toInt(); - } - else { - qWarning() << reply.error().message(); - } + QDBusPendingCall pcall= iface.asyncCall(QStringLiteral("copyblocks"), CoreBackendManager::self()->Uuid(), m_Source->path(), m_Source->firstByte(), m_Source->length(), m_Target->path(), m_Target->firstByte(), blockSize); + QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this); + QEventLoop loop; + + auto exitLoop = [&] (QDBusPendingCallWatcher *watcher) { + loop.exit(); + if (watcher->isError()) { + qWarning() << watcher->error(); + } + else { + QDBusPendingReply reply = *watcher; + rval = reply.argumentAt<0>(); + } + emit finished(); + setExitCode(!rval); + }; + + connect(watcher, &QDBusPendingCallWatcher::finished, exitLoop); + loop.exec(); } - emit finished(); - setExitCode(!rval); return rval; } diff --git a/src/util/externalcommandhelper.cpp b/src/util/externalcommandhelper.cpp index 76cf992..159af88 100644 --- a/src/util/externalcommandhelper.cpp +++ b/src/util/externalcommandhelper.cpp @@ -109,10 +109,9 @@ bool ExternalCommandHelper::writeData(const QString &targetDevice, const QByteAr return true; } -QVariantMap ExternalCommandHelper::copyblocks(const QString& Uuid, const QString& sourceDevice, const qint64 sourceFirstByte, const qint64 sourceLength, const QString& targetDevice, const qint64 targetFirstByte, const qint64 blockSize) +bool ExternalCommandHelper::copyblocks(const QString& Uuid, const QString& sourceDevice, const qint64 sourceFirstByte, const qint64 sourceLength, const QString& targetDevice, const qint64 targetFirstByte, const qint64 blockSize) { isCallerAuthorized(Uuid); - QVariantMap reply; const qint64 blocksToCopy = sourceLength / blockSize; qint64 readOffset = sourceFirstByte; @@ -190,9 +189,7 @@ QVariantMap ExternalCommandHelper::copyblocks(const QString& Uuid, const QString report[QStringLiteral("report")] = xi18ncp("@info:progress argument 2 is a string such as 7 bytes (localized accordingly)", "Copying 1 block (%2) finished.", "Copying %1 blocks (%2) finished.", blocksCopied, i18np("1 byte", "%1 bytes", bytesWritten)); HelperSupport::progressStep(report); - reply[QStringLiteral("success")] = rval; - - return reply; + return rval; } QVariantMap ExternalCommandHelper::start(const QString& Uuid, const QString& command, const QStringList& arguments, const QByteArray& input, const QStringList& environment) diff --git a/src/util/externalcommandhelper.h b/src/util/externalcommandhelper.h index 7714de3..caf0935 100644 --- a/src/util/externalcommandhelper.h +++ b/src/util/externalcommandhelper.h @@ -42,7 +42,7 @@ public: public Q_SLOTS: ActionReply init(const QVariantMap& args); Q_SCRIPTABLE QVariantMap start(const QString& Uuid, const QString& command, const QStringList& arguments, const QByteArray& input, const QStringList& environment); - Q_SCRIPTABLE QVariantMap copyblocks(const QString& Uuid, const QString& sourceDevice, const qint64 sourceFirstByte, const qint64 sourceLength, const QString& targetDevice, const qint64 targetFirstByte, const qint64 blockSize); + Q_SCRIPTABLE bool copyblocks(const QString& Uuid, const QString& sourceDevice, const qint64 sourceFirstByte, const qint64 sourceLength, const QString& targetDevice, const qint64 targetFirstByte, const qint64 blockSize); Q_SCRIPTABLE void exit(const QString& Uuid); private: