From 872715f4e0ac49a15519f462a226cc79085657a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0tikonas?= Date: Sun, 5 Aug 2018 21:14:43 +0100 Subject: [PATCH] Switch DBus calls to classes generated from XML. --- src/util/CMakeLists.txt | 27 +++- src/util/externalcommand.cpp | 150 ++++++++---------- src/util/externalcommand.h | 4 +- src/util/externalcommandhelper.cpp | 14 +- .../org.kde.kpmcore.applicationinterface.conf | 2 +- 5 files changed, 100 insertions(+), 97 deletions(-) diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index 4d1fd5a..1106ba3 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -1,4 +1,23 @@ +set(application_interface_xml org.kde.kpmcore.applicationinterface.xml) +set(helper_interface_xml org.kde.kpmcore.helperinterface.xml) + +qt5_generate_dbus_interface( + util/externalcommand.h + ${application_interface_xml} + OPTIONS -a +) + +qt5_generate_dbus_interface( + util/externalcommandhelper.h + ${helper_interface_xml} + OPTIONS -a +) + +qt5_add_dbus_interface(ApplicationInterface_SRCS ${CMAKE_CURRENT_BINARY_DIR}/${application_interface_xml} externalcommand_interface) +qt5_add_dbus_interface(HelperInterface_SRCS ${CMAKE_CURRENT_BINARY_DIR}/${helper_interface_xml} externalcommandhelper_interface) + set(UTIL_SRC + ${HelperInterface_SRCS} util/capacity.cpp util/externalcommand.cpp util/globallog.cpp @@ -17,13 +36,11 @@ set(UTIL_LIB_HDRS util/report.h ) -qt5_generate_dbus_interface( - externalcommand_interface.h - org.kde.kpmcore.externalcommand.xml +add_executable(kpmcore_externalcommand + ${ApplicationInterface_SRCS} + util/externalcommandhelper.cpp ) -add_executable(kpmcore_externalcommand util/externalcommandhelper.cpp) - target_link_libraries(kpmcore_externalcommand qca-qt5 Qt5::Core diff --git a/src/util/externalcommand.cpp b/src/util/externalcommand.cpp index 86e10f3..0db7986 100644 --- a/src/util/externalcommand.cpp +++ b/src/util/externalcommand.cpp @@ -26,6 +26,8 @@ #include "util/externalcommand.h" #include "util/report.h" +#include "externalcommandhelper_interface.h" + #include #include #include @@ -132,55 +134,45 @@ bool ExternalCommand::start(int timeout) return false; } - QDBusInterface iface(QStringLiteral("org.kde.kpmcore.helperinterface"), - QStringLiteral("/Helper"), - QStringLiteral("org.kde.kpmcore.externalcommand"), - QDBusConnection::systemBus()); + auto *interface = new org::kde::kpmcore::externalcommand(QStringLiteral("org.kde.kpmcore.externalcommand"), + QStringLiteral("/Helper"), QDBusConnection::systemBus(), this); - iface.setTimeout(10 * 24 * 3600 * 1000); // 10 days + interface->setTimeout(10 * 24 * 3600 * 1000); // 10 days bool rval = false; - if (iface.isValid()) { - QByteArray request; - const quint64 nonce = getNonce(iface); - request.setNum(nonce); - request.append(cmd.toUtf8()); - for (const auto &argument : qAsConst(d->m_Args)) - request.append(argument.toUtf8()); - request.append(d->m_Input); - request.append(d->processChannelMode); + QByteArray request; + const quint64 nonce = interface->getNonce(); + request.setNum(nonce); + request.append(cmd.toUtf8()); + for (const auto &argument : qAsConst(d->m_Args)) + request.append(argument.toUtf8()); + request.append(d->m_Input); + request.append(d->processChannelMode); - QByteArray hash = QCryptographicHash::hash(request, QCryptographicHash::Sha512); + QByteArray hash = QCryptographicHash::hash(request, QCryptographicHash::Sha512); - QDBusPendingCall pcall = iface.asyncCall(QStringLiteral("start"), - privateKey->signMessage(hash, QCA::EMSA3_Raw), - nonce, - cmd, - args(), - d->m_Input, - d->processChannelMode); + QDBusPendingCall pcall = interface->start(privateKey->signMessage(hash, QCA::EMSA3_Raw), + nonce, cmd, args(), d->m_Input, d->processChannelMode); - QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this); + QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this); + QEventLoop loop; - QEventLoop loop; + auto exitLoop = [&] (QDBusPendingCallWatcher *watcher) { + loop.exit(); - auto exitLoop = [&] (QDBusPendingCallWatcher *watcher) { - loop.exit(); + if (watcher->isError()) + qWarning() << watcher->error(); + else { + QDBusPendingReply reply = *watcher; - if (watcher->isError()) - qWarning() << watcher->error(); - else { - QDBusPendingReply reply = *watcher; + d->m_Output = reply.value()[QStringLiteral("output")].toByteArray(); + setExitCode(reply.value()[QStringLiteral("exitCode")].toInt()); + rval = reply.value()[QStringLiteral("success")].toBool(); + } + }; - d->m_Output = reply.value()[QStringLiteral("output")].toByteArray(); - setExitCode(reply.value()[QStringLiteral("exitCode")].toInt()); - rval = reply.value()[QStringLiteral("success")].toBool(); - } - }; - - connect(watcher, &QDBusPendingCallWatcher::finished, exitLoop); - loop.exec(); - } + connect(watcher, &QDBusPendingCallWatcher::finished, exitLoop); + loop.exec(); return rval; } @@ -199,47 +191,42 @@ bool ExternalCommand::copyBlocks(CopySource& source, CopyTarget& target) connect(m_job, SIGNAL(percent(KJob*, unsigned long)), this, SLOT(emitProgress(KJob*, unsigned long))); connect(m_job, &KAuth::ExecuteJob::newData, this, &ExternalCommand::emitReport); - 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()) { - QByteArray request; + auto *interface = new org::kde::kpmcore::externalcommand(QStringLiteral("org.kde.kpmcore.externalcommand"), + QStringLiteral("/Helper"), QDBusConnection::systemBus(), this); + interface->setTimeout(10 * 24 * 3600 * 1000); // 10 days + QByteArray request; - const quint64 nonce = getNonce(iface); - request.setNum(nonce); - request.append(source.path().toUtf8()); - request.append(QByteArray::number(source.firstByte())); - request.append(QByteArray::number(source.length())); - request.append(target.path().toUtf8()); - request.append(QByteArray::number(target.firstByte())); - request.append(QByteArray::number(blockSize)); + const quint64 nonce = interface->getNonce(); + request.setNum(nonce); + request.append(source.path().toUtf8()); + request.append(QByteArray::number(source.firstByte())); + request.append(QByteArray::number(source.length())); + request.append(target.path().toUtf8()); + request.append(QByteArray::number(target.firstByte())); + request.append(QByteArray::number(blockSize)); - QByteArray hash = QCryptographicHash::hash(request, QCryptographicHash::Sha512); + QByteArray hash = QCryptographicHash::hash(request, QCryptographicHash::Sha512); - // Use asynchronous DBus calls, so that we can process reports and progress - QDBusPendingCall pcall = iface.asyncCall(QStringLiteral("copyblocks"), - privateKey->signMessage(hash, QCA::EMSA3_Raw), - nonce, - source.path(), source.firstByte(), source.length(), - target.path(), target.firstByte(), blockSize); + QDBusPendingCall pcall = interface->copyblocks(privateKey->signMessage(hash, QCA::EMSA3_Raw), nonce, + source.path(), source.firstByte(), source.length(), + target.path(), target.firstByte(), blockSize); - QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this); - QEventLoop loop; + 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>(); - } - setExitCode(!rval); - }; + auto exitLoop = [&] (QDBusPendingCallWatcher *watcher) { + loop.exit(); + if (watcher->isError()) + qWarning() << watcher->error(); + else { + QDBusPendingReply reply = *watcher; + rval = reply.argumentAt<0>(); + } + setExitCode(!rval); + }; - connect(watcher, &QDBusPendingCallWatcher::finished, exitLoop); - loop.exec(); - } + connect(watcher, &QDBusPendingCallWatcher::finished, exitLoop); + loop.exec(); return rval; } @@ -385,14 +372,13 @@ bool ExternalCommand::startHelper() void ExternalCommand::stopHelper() { - QDBusInterface iface(QStringLiteral("org.kde.kpmcore.helperinterface"), QStringLiteral("/Helper"), QStringLiteral("org.kde.kpmcore.externalcommand"), QDBusConnection::systemBus()); - if (iface.isValid()) { - QByteArray request; - const quint64 nonce = getNonce(iface); - request.setNum(nonce); - QByteArray hash = QCryptographicHash::hash(request, QCryptographicHash::Sha512); - iface.call(QStringLiteral("exit"), privateKey->signMessage(hash, QCA::EMSA3_Raw), nonce); - } + auto *interface = new org::kde::kpmcore::externalcommand(QStringLiteral("org.kde.kpmcore.externalcommand"), + QStringLiteral("/Helper"), QDBusConnection::systemBus()); + QByteArray request; + const quint64 nonce = interface->getNonce(); + request.setNum(nonce); + QByteArray hash = QCryptographicHash::hash(request, QCryptographicHash::Sha512); + interface->exit(privateKey->signMessage(hash, QCA::EMSA3_Raw), nonce); delete privateKey; delete init; diff --git a/src/util/externalcommand.h b/src/util/externalcommand.h index 14b7c2f..5561ddb 100644 --- a/src/util/externalcommand.h +++ b/src/util/externalcommand.h @@ -43,11 +43,11 @@ struct ExternalCommandPrivate; class DBusThread : public QThread { Q_OBJECT - Q_CLASSINFO("D-Bus Interface", "org.kde.kpmcore.ping") + Q_CLASSINFO("D-Bus Interface", "org.kde.kpmcore.applicationinterface") void run() override; public Q_SLOTS: - Q_SCRIPTABLE void ping() {return;}; + Q_SCRIPTABLE void ping() { return; }; }; /** An external command. diff --git a/src/util/externalcommandhelper.cpp b/src/util/externalcommandhelper.cpp index 2b89c75..a0c1104 100644 --- a/src/util/externalcommandhelper.cpp +++ b/src/util/externalcommandhelper.cpp @@ -16,6 +16,7 @@ *************************************************************************/ #include "externalcommandhelper.h" +#include "externalcommand_interface.h" #include #include @@ -65,13 +66,12 @@ ActionReply ExternalCommandHelper::init(const QVariantMap& args) m_loop = std::make_unique(); HelperSupport::progressStep(QVariantMap()); auto timeout = [this] () { - QDBusInterface iface(QStringLiteral("org.kde.kpmcore.applicationinterface"), - QStringLiteral("/Application"), - QStringLiteral("org.kde.kpmcore.ping"), - QDBusConnection::systemBus()); - iface.setTimeout(2000); // 2 seconds; - auto pcall = iface.asyncCall(QStringLiteral("ping")); - QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this); + auto *interface = new org::kde::kpmcore::applicationinterface(QStringLiteral("org.kde.kpmcore.applicationinterface"), + QStringLiteral("/Application"), QDBusConnection::systemBus(), this); + interface->setTimeout(2000); // 2 seconds; + auto pendingCall = interface->ping(); + + QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall, this); auto exitLoop = [&] (QDBusPendingCallWatcher *watcher) { if (watcher->isError()) { qWarning() << watcher->error(); diff --git a/src/util/org.kde.kpmcore.applicationinterface.conf b/src/util/org.kde.kpmcore.applicationinterface.conf index 59113ff..e64a003 100644 --- a/src/util/org.kde.kpmcore.applicationinterface.conf +++ b/src/util/org.kde.kpmcore.applicationinterface.conf @@ -9,6 +9,6 @@ + send_interface="org.kde.kpmcore.applicationinterface"/>