From dac3372a78c9a36acd058554f0111341362ed404 Mon Sep 17 00:00:00 2001 From: Caio Carvalho Date: Thu, 22 Mar 2018 02:32:59 -0300 Subject: [PATCH] - Including ExternalCommandHelper asynchronous DBus call in ExternalCommand::execute - Stop helper before starting a new in CoreBackendManager::startExternalCommandHelper - Unregister org.kde.kpmcore.helperinterface service in ExternalCommandHelper::exit --- src/backend/corebackendmanager.cpp | 2 ++ src/util/externalcommand.cpp | 52 ++++++++++++++++++++++-------- src/util/externalcommandhelper.cpp | 5 +++ 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/backend/corebackendmanager.cpp b/src/backend/corebackendmanager.cpp index 195bbf6..3f9b817 100644 --- a/src/backend/corebackendmanager.cpp +++ b/src/backend/corebackendmanager.cpp @@ -65,6 +65,8 @@ QVector CoreBackendManager::list() const void CoreBackendManager::startExternalCommandHelper() { + stopExternalCommandHelper(); + KAuth::Action action = KAuth::Action(QStringLiteral("org.kde.kpmcore.externalcommand.init")); action.setHelperId(QStringLiteral("org.kde.kpmcore.externalcommand")); action.setTimeout(10 * 24 * 3600 * 1000); // 10 days diff --git a/src/util/externalcommand.cpp b/src/util/externalcommand.cpp index 1400cfb..c77ad36 100644 --- a/src/util/externalcommand.cpp +++ b/src/util/externalcommand.cpp @@ -152,6 +152,7 @@ bool ExternalCommand::start(int timeout) // connect(this, &ExternalCommand::finished, &loop, &QEventLoop::quit); // loop.exec(); // return true; + Q_UNUSED(timeout) execute(); return true; } @@ -173,20 +174,43 @@ void ExternalCommand::execute() return; } - 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("start"), CoreBackendManager::self()->Uuid(), cmd, args(), m_Input, QStringList()); - if (reply.isValid()) { - m_Output = reply.value()[QStringLiteral("output")].toByteArray(); - setExitCode(reply.value()[QStringLiteral("exitCode")].toInt()); - } - else { - qWarning() << reply.error().message(); - } - } + QDBusInterface iface(QStringLiteral("org.kde.kpmcore.helperinterface"), + QStringLiteral("/Helper"), + QStringLiteral("org.kde.kpmcore.externalcommand"), + QDBusConnection::systemBus()); - emit finished(); + iface.setTimeout(10 * 24 * 3600 * 1000); // 10 days + + if (iface.isValid()) { + QDBusPendingCall pcall = iface.asyncCall(QStringLiteral("start"), + CoreBackendManager::self()->Uuid(), + cmd, + args(), + m_Input, + QStringList()); + + QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this); + + QEventLoop loop; + + auto exitLoop = [&] (QDBusPendingCallWatcher *watcher) { + loop.exit(); + + if (watcher->isError()) + qWarning() << watcher->error(); + else { + QDBusPendingReply reply = *watcher; + + m_Output = reply.value()[QStringLiteral("output")].toByteArray(); + setExitCode(reply.value()[QStringLiteral("exitCode")].toInt()); + } + + emit finished(); + }; + + connect(watcher, &QDBusPendingCallWatcher::finished, exitLoop); + loop.exec(); + } } bool ExternalCommand::write(const QByteArray& input) @@ -210,7 +234,7 @@ bool ExternalCommand::waitFor(int timeout) }*/ // onReadOutput(); - + Q_UNUSED(timeout) return true; } diff --git a/src/util/externalcommandhelper.cpp b/src/util/externalcommandhelper.cpp index 159af88..a4d9012 100644 --- a/src/util/externalcommandhelper.cpp +++ b/src/util/externalcommandhelper.cpp @@ -225,6 +225,11 @@ void ExternalCommandHelper::exit(const QString& Uuid) { isCallerAuthorized(Uuid); m_loop.exit(); + + if (QDBusConnection::systemBus().unregisterService(QStringLiteral("org.kde.kpmcore.helperinterface"))) + qDebug() << "org.kde.kpmcore.helperinterface unregistered"; + + QDBusConnection::systemBus().unregisterObject(QStringLiteral("/Helper")); } void ExternalCommandHelper::onReadOutput()