20 #include "abstractdbusservicemonitor.h"
22 #include <QDBusInterface>
23 #include <QDBusServiceWatcher>
24 #include <QDBusConnection>
25 #include <QDBusConnectionInterface>
28 AbstractDBusServiceMonitor::AbstractDBusServiceMonitor(
const QString &service,
const QString &path,
29 const QString &interface,
const Bus bus,
34 , m_interface(interface)
35 , m_watcher(new QDBusServiceWatcher(service,
36 (bus == SystemBus) ? QDBusConnection::systemBus()
37 : QDBusConnection::sessionBus()))
38 , m_dbusInterface(nullptr)
40 connect(m_watcher, &QDBusServiceWatcher::serviceRegistered,
this, &AbstractDBusServiceMonitor::createInterface);
41 connect(m_watcher, &QDBusServiceWatcher::serviceUnregistered,
this, &AbstractDBusServiceMonitor::destroyInterface);
44 QDBusConnectionInterface* sessionBus = QDBusConnection::sessionBus().interface();
45 QDBusReply<bool> reply = sessionBus->isServiceRegistered(m_service);
46 if (reply.isValid() && reply.value()) {
47 createInterface(m_service);
51 AbstractDBusServiceMonitor::~AbstractDBusServiceMonitor()
54 delete m_dbusInterface;
57 void AbstractDBusServiceMonitor::createInterface(
const QString &)
59 if (m_dbusInterface !=
nullptr) {
60 delete m_dbusInterface;
61 m_dbusInterface =
nullptr;
64 m_dbusInterface =
new QDBusInterface(m_service, m_path, m_interface,
65 QDBusConnection::sessionBus());
66 Q_EMIT serviceAvailableChanged(
true);
69 void AbstractDBusServiceMonitor::destroyInterface(
const QString &)
71 if (m_dbusInterface !=
nullptr) {
72 delete m_dbusInterface;
73 m_dbusInterface =
nullptr;
76 Q_EMIT serviceAvailableChanged(
false);
79 QDBusInterface* AbstractDBusServiceMonitor::dbusInterface()
const
81 return m_dbusInterface;
84 bool AbstractDBusServiceMonitor::serviceAvailable()
const
86 return m_dbusInterface !=
nullptr;