Switch DBus calls to classes generated from XML.

This commit is contained in:
Andrius Štikonas 2018-08-05 21:14:43 +01:00
parent c9258f74c0
commit 872715f4e0
5 changed files with 100 additions and 97 deletions

View File

@ -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 set(UTIL_SRC
${HelperInterface_SRCS}
util/capacity.cpp util/capacity.cpp
util/externalcommand.cpp util/externalcommand.cpp
util/globallog.cpp util/globallog.cpp
@ -17,13 +36,11 @@ set(UTIL_LIB_HDRS
util/report.h util/report.h
) )
qt5_generate_dbus_interface( add_executable(kpmcore_externalcommand
externalcommand_interface.h ${ApplicationInterface_SRCS}
org.kde.kpmcore.externalcommand.xml util/externalcommandhelper.cpp
) )
add_executable(kpmcore_externalcommand util/externalcommandhelper.cpp)
target_link_libraries(kpmcore_externalcommand target_link_libraries(kpmcore_externalcommand
qca-qt5 qca-qt5
Qt5::Core Qt5::Core

View File

@ -26,6 +26,8 @@
#include "util/externalcommand.h" #include "util/externalcommand.h"
#include "util/report.h" #include "util/report.h"
#include "externalcommandhelper_interface.h"
#include <QCryptographicHash> #include <QCryptographicHash>
#include <QDBusConnection> #include <QDBusConnection>
#include <QDBusInterface> #include <QDBusInterface>
@ -132,55 +134,45 @@ bool ExternalCommand::start(int timeout)
return false; return false;
} }
QDBusInterface iface(QStringLiteral("org.kde.kpmcore.helperinterface"), auto *interface = new org::kde::kpmcore::externalcommand(QStringLiteral("org.kde.kpmcore.externalcommand"),
QStringLiteral("/Helper"), QStringLiteral("/Helper"), QDBusConnection::systemBus(), this);
QStringLiteral("org.kde.kpmcore.externalcommand"),
QDBusConnection::systemBus());
iface.setTimeout(10 * 24 * 3600 * 1000); // 10 days interface->setTimeout(10 * 24 * 3600 * 1000); // 10 days
bool rval = false; bool rval = false;
if (iface.isValid()) { QByteArray request;
QByteArray request; const quint64 nonce = interface->getNonce();
const quint64 nonce = getNonce(iface); request.setNum(nonce);
request.setNum(nonce); request.append(cmd.toUtf8());
request.append(cmd.toUtf8()); for (const auto &argument : qAsConst(d->m_Args))
for (const auto &argument : qAsConst(d->m_Args)) request.append(argument.toUtf8());
request.append(argument.toUtf8()); request.append(d->m_Input);
request.append(d->m_Input); request.append(d->processChannelMode);
request.append(d->processChannelMode);
QByteArray hash = QCryptographicHash::hash(request, QCryptographicHash::Sha512); QByteArray hash = QCryptographicHash::hash(request, QCryptographicHash::Sha512);
QDBusPendingCall pcall = iface.asyncCall(QStringLiteral("start"), QDBusPendingCall pcall = interface->start(privateKey->signMessage(hash, QCA::EMSA3_Raw),
privateKey->signMessage(hash, QCA::EMSA3_Raw), nonce, cmd, args(), d->m_Input, d->processChannelMode);
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) { if (watcher->isError())
loop.exit(); qWarning() << watcher->error();
else {
QDBusPendingReply<QVariantMap> reply = *watcher;
if (watcher->isError()) d->m_Output = reply.value()[QStringLiteral("output")].toByteArray();
qWarning() << watcher->error(); setExitCode(reply.value()[QStringLiteral("exitCode")].toInt());
else { rval = reply.value()[QStringLiteral("success")].toBool();
QDBusPendingReply<QVariantMap> reply = *watcher; }
};
d->m_Output = reply.value()[QStringLiteral("output")].toByteArray(); connect(watcher, &QDBusPendingCallWatcher::finished, exitLoop);
setExitCode(reply.value()[QStringLiteral("exitCode")].toInt()); loop.exec();
rval = reply.value()[QStringLiteral("success")].toBool();
}
};
connect(watcher, &QDBusPendingCallWatcher::finished, exitLoop);
loop.exec();
}
return rval; 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, SIGNAL(percent(KJob*, unsigned long)), this, SLOT(emitProgress(KJob*, unsigned long)));
connect(m_job, &KAuth::ExecuteJob::newData, this, &ExternalCommand::emitReport); 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()); auto *interface = new org::kde::kpmcore::externalcommand(QStringLiteral("org.kde.kpmcore.externalcommand"),
iface.setTimeout(10 * 24 * 3600 * 1000); // 10 days QStringLiteral("/Helper"), QDBusConnection::systemBus(), this);
if (iface.isValid()) { interface->setTimeout(10 * 24 * 3600 * 1000); // 10 days
QByteArray request; QByteArray request;
const quint64 nonce = getNonce(iface); const quint64 nonce = interface->getNonce();
request.setNum(nonce); request.setNum(nonce);
request.append(source.path().toUtf8()); request.append(source.path().toUtf8());
request.append(QByteArray::number(source.firstByte())); request.append(QByteArray::number(source.firstByte()));
request.append(QByteArray::number(source.length())); request.append(QByteArray::number(source.length()));
request.append(target.path().toUtf8()); request.append(target.path().toUtf8());
request.append(QByteArray::number(target.firstByte())); request.append(QByteArray::number(target.firstByte()));
request.append(QByteArray::number(blockSize)); 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 = interface->copyblocks(privateKey->signMessage(hash, QCA::EMSA3_Raw), nonce,
QDBusPendingCall pcall = iface.asyncCall(QStringLiteral("copyblocks"), source.path(), source.firstByte(), source.length(),
privateKey->signMessage(hash, QCA::EMSA3_Raw), target.path(), target.firstByte(), blockSize);
nonce,
source.path(), source.firstByte(), source.length(),
target.path(), target.firstByte(), blockSize);
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this); QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this);
QEventLoop loop; QEventLoop loop;
auto exitLoop = [&] (QDBusPendingCallWatcher *watcher) { auto exitLoop = [&] (QDBusPendingCallWatcher *watcher) {
loop.exit(); loop.exit();
if (watcher->isError()) { if (watcher->isError())
qWarning() << watcher->error(); qWarning() << watcher->error();
} else {
else { QDBusPendingReply<bool> reply = *watcher;
QDBusPendingReply<bool> reply = *watcher; rval = reply.argumentAt<0>();
rval = reply.argumentAt<0>(); }
} setExitCode(!rval);
setExitCode(!rval); };
};
connect(watcher, &QDBusPendingCallWatcher::finished, exitLoop); connect(watcher, &QDBusPendingCallWatcher::finished, exitLoop);
loop.exec(); loop.exec();
}
return rval; return rval;
} }
@ -385,14 +372,13 @@ bool ExternalCommand::startHelper()
void ExternalCommand::stopHelper() void ExternalCommand::stopHelper()
{ {
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"),
if (iface.isValid()) { QStringLiteral("/Helper"), QDBusConnection::systemBus());
QByteArray request; QByteArray request;
const quint64 nonce = getNonce(iface); const quint64 nonce = interface->getNonce();
request.setNum(nonce); request.setNum(nonce);
QByteArray hash = QCryptographicHash::hash(request, QCryptographicHash::Sha512); QByteArray hash = QCryptographicHash::hash(request, QCryptographicHash::Sha512);
iface.call(QStringLiteral("exit"), privateKey->signMessage(hash, QCA::EMSA3_Raw), nonce); interface->exit(privateKey->signMessage(hash, QCA::EMSA3_Raw), nonce);
}
delete privateKey; delete privateKey;
delete init; delete init;

View File

@ -43,11 +43,11 @@ struct ExternalCommandPrivate;
class DBusThread : public QThread class DBusThread : public QThread
{ {
Q_OBJECT Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.kde.kpmcore.ping") Q_CLASSINFO("D-Bus Interface", "org.kde.kpmcore.applicationinterface")
void run() override; void run() override;
public Q_SLOTS: public Q_SLOTS:
Q_SCRIPTABLE void ping() {return;}; Q_SCRIPTABLE void ping() { return; };
}; };
/** An external command. /** An external command.

View File

@ -16,6 +16,7 @@
*************************************************************************/ *************************************************************************/
#include "externalcommandhelper.h" #include "externalcommandhelper.h"
#include "externalcommand_interface.h"
#include <QtDBus> #include <QtDBus>
#include <QDebug> #include <QDebug>
@ -65,13 +66,12 @@ ActionReply ExternalCommandHelper::init(const QVariantMap& args)
m_loop = std::make_unique<QEventLoop>(); m_loop = std::make_unique<QEventLoop>();
HelperSupport::progressStep(QVariantMap()); HelperSupport::progressStep(QVariantMap());
auto timeout = [this] () { auto timeout = [this] () {
QDBusInterface iface(QStringLiteral("org.kde.kpmcore.applicationinterface"), auto *interface = new org::kde::kpmcore::applicationinterface(QStringLiteral("org.kde.kpmcore.applicationinterface"),
QStringLiteral("/Application"), QStringLiteral("/Application"), QDBusConnection::systemBus(), this);
QStringLiteral("org.kde.kpmcore.ping"), interface->setTimeout(2000); // 2 seconds;
QDBusConnection::systemBus()); auto pendingCall = interface->ping();
iface.setTimeout(2000); // 2 seconds;
auto pcall = iface.asyncCall(QStringLiteral("ping")); QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall, this);
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this);
auto exitLoop = [&] (QDBusPendingCallWatcher *watcher) { auto exitLoop = [&] (QDBusPendingCallWatcher *watcher) {
if (watcher->isError()) { if (watcher->isError()) {
qWarning() << watcher->error(); qWarning() << watcher->error();

View File

@ -9,6 +9,6 @@
<policy user="root"> <policy user="root">
<allow send_destination="org.kde.kpmcore.applicationinterface" <allow send_destination="org.kde.kpmcore.applicationinterface"
send_interface="org.kde.kpmcore.ping"/> send_interface="org.kde.kpmcore.applicationinterface"/>
</policy> </policy>
</busconfig> </busconfig>