on cfb1731 help sign in
unity8/src/libunity8-utils/abstractdbusservicemonitor.cpp
86 lines 2.8 kB history raw
1/*
2 * Copyright (C) 2011-2014 Canonical, Ltd.
3 *
4 * Authors:
5 * Ugo Riboni <ugo.riboni@canonical.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 3.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
20#include "abstractdbusservicemonitor.h"
22#include <QDBusInterface>
23#include <QDBusServiceWatcher>
24#include <QDBusConnection>
25#include <QDBusConnectionInterface>
26#include <QDBusReply>
28AbstractDBusServiceMonitor::AbstractDBusServiceMonitor(const QString &service, const QString &path,
29 const QString &interface, QObject *parent)
30 : QObject(parent)
31 , m_service(service)
32 , m_path(path)
33 , m_interface(interface)
34 , m_watcher(new QDBusServiceWatcher(service, QDBusConnection::sessionBus()))
35 , m_dbusInterface(nullptr)
37 connect(m_watcher, &QDBusServiceWatcher::serviceRegistered, this, &AbstractDBusServiceMonitor::createInterface);
38 connect(m_watcher, &QDBusServiceWatcher::serviceUnregistered, this, &AbstractDBusServiceMonitor::destroyInterface);
40 // Connect to the service if it's up already
41 QDBusConnectionInterface* sessionBus = QDBusConnection::sessionBus().interface();
42 QDBusReply<bool> reply = sessionBus->isServiceRegistered(m_service);
43 if (reply.isValid() && reply.value()) {
44 createInterface(m_service);
45 }
48AbstractDBusServiceMonitor::~AbstractDBusServiceMonitor()
50 delete m_watcher;
51 if (m_dbusInterface) {
52 delete m_dbusInterface;
53 }
56void AbstractDBusServiceMonitor::createInterface(const QString &)
58 if (m_dbusInterface != nullptr) {
59 delete m_dbusInterface;
60 m_dbusInterface = nullptr;
61 }
63 m_dbusInterface = new QDBusInterface(m_service, m_path, m_interface,
64 QDBusConnection::sessionBus());
65 Q_EMIT serviceAvailableChanged(true);
68void AbstractDBusServiceMonitor::destroyInterface(const QString &)
70 if (m_dbusInterface != nullptr) {
71 delete m_dbusInterface;
72 m_dbusInterface = nullptr;
73 }
75 Q_EMIT serviceAvailableChanged(false);
78QDBusInterface* AbstractDBusServiceMonitor::dbusInterface() const
80 return m_dbusInterface;
83bool AbstractDBusServiceMonitor::serviceAvailable() const
85 return m_dbusInterface != nullptr;