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
${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

View File

@ -26,6 +26,8 @@
#include "util/externalcommand.h"
#include "util/report.h"
#include "externalcommandhelper_interface.h"
#include <QCryptographicHash>
#include <QDBusConnection>
#include <QDBusInterface>
@ -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<QVariantMap> reply = *watcher;
if (watcher->isError())
qWarning() << watcher->error();
else {
QDBusPendingReply<QVariantMap> 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<bool> reply = *watcher;
rval = reply.argumentAt<0>();
}
setExitCode(!rval);
};
auto exitLoop = [&] (QDBusPendingCallWatcher *watcher) {
loop.exit();
if (watcher->isError())
qWarning() << watcher->error();
else {
QDBusPendingReply<bool> 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;

View File

@ -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.

View File

@ -16,6 +16,7 @@
*************************************************************************/
#include "externalcommandhelper.h"
#include "externalcommand_interface.h"
#include <QtDBus>
#include <QDebug>
@ -65,13 +66,12 @@ ActionReply ExternalCommandHelper::init(const QVariantMap& args)
m_loop = std::make_unique<QEventLoop>();
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();

View File

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