@@ -0,0 +1,84 @@
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+ */
19+
20+#include "abstractdbusservicemonitor.h"
21+
22+#include <QDBusConnection>
23+#include <QDBusConnectionInterface>
24+#include <QDBusReply>
25+
26+AbstractDBusServiceMonitor::AbstractDBusServiceMonitor(QString service, QString path,
27+ QString interface, QObject *parent)
28+ : QObject(parent)
29+ , m_service(service)
30+ , m_path(path)
31+ , m_interface(interface)
32+ , m_watcher(new QDBusServiceWatcher(service, QDBusConnection::sessionBus()))
33+ , m_dbusInterface(0)
34+{
35+ connect(m_watcher, SIGNAL(serviceRegistered(QString)), SLOT(createInterface()));
36+ connect(m_watcher, SIGNAL(serviceUnregistered(QString)), SLOT(destroyInterface()));
37+
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()) {
42+ createInterface();
43+ }
44+}
45+
46+AbstractDBusServiceMonitor::~AbstractDBusServiceMonitor()
47+{
48+ delete m_watcher;
49+ if (m_dbusInterface) {
50+ delete m_dbusInterface;
51+ }
52+}
53+
54+void AbstractDBusServiceMonitor::createInterface()
55+{
56+ if (m_dbusInterface != 0) {
57+ delete m_dbusInterface;
58+ m_dbusInterface = 0;
59+ }
60+
61+ m_dbusInterface = new QDBusInterface(m_service, m_path, m_interface,
62+ QDBusConnection::sessionBus());
63+ Q_EMIT serviceAvailableChanged(true);
64+}
65+
66+void AbstractDBusServiceMonitor::destroyInterface()
67+{
68+ if (m_dbusInterface != 0) {
69+ delete m_dbusInterface;
70+ m_dbusInterface = 0;
71+ }
72+
73+ Q_EMIT serviceAvailableChanged(false);
74+}
75+
76+QDBusInterface* AbstractDBusServiceMonitor::dbusInterface() const
77+{
78+ return m_dbusInterface;
79+}
80+
81+bool AbstractDBusServiceMonitor::serviceAvailable() const
82+{
83+ return m_dbusInterface != 0;
84+}