@@ -0,0 +1,84 @@
2+ * Copyright (C) 2011-2014 Canonical, Ltd.
5+ * Ugo Riboni <ugo.riboni@canonical.com>
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.
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.
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/>.
20+#include "abstractdbusservicemonitor.h"
22+#include <QDBusConnection>
23+#include <QDBusConnectionInterface>
26+AbstractDBusServiceMonitor::AbstractDBusServiceMonitor(QString service, QString path,
27+ QString interface, QObject *parent)
31+ , m_interface(interface)
32+ , m_watcher(new QDBusServiceWatcher(service, QDBusConnection::sessionBus()))
35+ connect(m_watcher, SIGNAL(serviceRegistered(QString)), SLOT(createInterface()));
36+ connect(m_watcher, SIGNAL(serviceUnregistered(QString)), SLOT(destroyInterface()));
38+ // Connect to the service if it's up already
39+ QDBusConnectionInterface* sessionBus = QDBusConnection::sessionBus().interface();
40+ QDBusReply<bool> reply = sessionBus->isServiceRegistered(m_service);
41+ if (reply.isValid() && reply.value()) {
46+AbstractDBusServiceMonitor::~AbstractDBusServiceMonitor()
49+ if (m_dbusInterface) {
50+ delete m_dbusInterface;
54+void AbstractDBusServiceMonitor::createInterface()
56+ if (m_dbusInterface != 0) {
57+ delete m_dbusInterface;
61+ m_dbusInterface = new QDBusInterface(m_service, m_path, m_interface,
62+ QDBusConnection::sessionBus());
63+ Q_EMIT serviceAvailableChanged(true);
66+void AbstractDBusServiceMonitor::destroyInterface()
68+ if (m_dbusInterface != 0) {
69+ delete m_dbusInterface;
73+ Q_EMIT serviceAvailableChanged(false);
76+QDBusInterface* AbstractDBusServiceMonitor::dbusInterface() const
78+ return m_dbusInterface;
81+bool AbstractDBusServiceMonitor::serviceAvailable() const
83+ return m_dbusInterface != 0;