2
* Copyright (C) 2011-2015 Canonical, Ltd.
4
* This program is free software: you can redistribute it and/or modify it under
5
* the terms of the GNU Lesser General Public License version 3, as published by
6
* the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but WITHOUT
9
* ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10
* SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
* Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
#include "abstractdbusservicemonitor.h"
19
#include <QDBusInterface>
20
#include <QDBusServiceWatcher>
21
#include <QDBusConnection>
22
#include <QDBusConnectionInterface>
25
// On construction QDBusInterface synchronously introspects the service, which will block the GUI
26
// thread if the service is busy. QDBusAbstractInterface does not perform this introspection, so
27
// let's subclass that and avoid the blocking scenario.
28
class AsyncDBusInterface : public QDBusAbstractInterface
32
AsyncDBusInterface(const QString &service, const QString &path,
33
const QString &interface, const QDBusConnection &connection,
35
: QDBusAbstractInterface(service, path, interface.toLatin1().data(), connection, parent)
37
~AsyncDBusInterface() = default;
40
AbstractDBusServiceMonitor::AbstractDBusServiceMonitor(const QString &service, const QString &path,
41
const QString &interface, const QDBusConnection &connection,
46
, m_interface(interface)
47
, m_busConnection(connection)
48
, m_watcher(new QDBusServiceWatcher(service, m_busConnection))
49
, m_dbusInterface(nullptr)
51
connect(m_watcher, &QDBusServiceWatcher::serviceRegistered, this, &AbstractDBusServiceMonitor::createInterface);
52
connect(m_watcher, &QDBusServiceWatcher::serviceUnregistered, this, &AbstractDBusServiceMonitor::destroyInterface);
54
// Connect to the service if it's up already
55
QDBusConnectionInterface* sessionBus = m_busConnection.interface();
56
QDBusReply<bool> reply = sessionBus->isServiceRegistered(m_service);
57
if (reply.isValid() && reply.value()) {
58
createInterface(m_service);
62
AbstractDBusServiceMonitor::~AbstractDBusServiceMonitor()
65
delete m_dbusInterface;
68
void AbstractDBusServiceMonitor::createInterface(const QString &)
70
if (m_dbusInterface != nullptr) {
71
delete m_dbusInterface;
72
m_dbusInterface = nullptr;
75
m_dbusInterface = new AsyncDBusInterface(m_service, m_path, m_interface, m_busConnection);
76
Q_EMIT serviceAvailableChanged(true);
79
void AbstractDBusServiceMonitor::destroyInterface(const QString &)
81
if (m_dbusInterface != nullptr) {
82
delete m_dbusInterface;
83
m_dbusInterface = nullptr;
86
Q_EMIT serviceAvailableChanged(false);
89
QDBusAbstractInterface* AbstractDBusServiceMonitor::dbusInterface() const
91
return m_dbusInterface;
94
bool AbstractDBusServiceMonitor::serviceAvailable() const
96
return m_dbusInterface != nullptr;
99
#include "abstractdbusservicemonitor.moc"