From 580b7edc353ce98553b657963ce4fc9543f836b1 Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Wed, 18 Jun 2014 19:14:40 +0100 Subject: [PATCH] refs #276 added async methods to CGenericDBusInterface --- src/blackmisc/genericdbusinterface.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/blackmisc/genericdbusinterface.h b/src/blackmisc/genericdbusinterface.h index 099cfb1d6..11be06c36 100644 --- a/src/blackmisc/genericdbusinterface.h +++ b/src/blackmisc/genericdbusinterface.h @@ -40,6 +40,28 @@ namespace BlackMisc QDBusPendingReply pr = this->asyncCallWithArgumentList(method, argumentList); return pr; } + + //! Call DBus with asynchronous return value + //! Callback can be any callable object taking a single argument of type QDBusPendingCallWatcher*. + template + void callDBusAsync(const QLatin1String &method, Func callback, Args&&... args) + { + QList argumentList { QVariant::fromValue(std::forward(args))... }; + QDBusPendingCall pc = this->asyncCallWithArgumentList(method, argumentList); + auto pcw = new QDBusPendingCallWatcher(pc, this); + connect(pcw, &QDBusPendingCallWatcher::finished, callback); + } + + //! Cancel all asynchronous DBus calls which are currently pending + //! \warning Don't call this from inside an async callback! + void cancelAllPendingAsyncCalls() + { + auto watchers = this->findChildren(QString(), Qt::FindDirectChildrenOnly); + for (auto w : watchers) + { + delete w; + } + } }; }