#ifndef BLACKMISC_GENERICDBUSINTERFACE_H #define BLACKMISC_GENERICDBUSINTERFACE_H #include #include #include #include namespace BlackMisc { /*! * Used for hand written interface based on virtual methods. * Allows to relay a message to DBus in a single code line */ class CGenericDBusInterface : public QDBusAbstractInterface { public: //! Constructor CGenericDBusInterface(const QString &serviceName, const QString &path, const QString &interfaceName, const QDBusConnection &connection, QObject *parent = 0) : QDBusAbstractInterface(serviceName, path, interfaceName.toUtf8().constData(), connection, parent) { } //! Call DBus, no return value template void callDBus(const QLatin1String &method, Args&&... args) { QList argumentList { QVariant::fromValue(std::forward(args))... }; this->asyncCallWithArgumentList(method, argumentList); } //! Call DBus with synchronous return value template Ret callDBusRet(const QLatin1String &method, Args&&... args) { QList argumentList { QVariant::fromValue(std::forward(args))... }; 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; } } }; } #endif // guard