on xenial help sign in
Add libunity8-utils library and import AbstractDBusServiceMonitor from unity2d
dfee783Gerry Boland
src/CMakeLists.txt+1
@@ -36,3 +36,4 @@ install(TARGETS ${SHELL_APP}
36
36
37add_subdirectory(Panel)
37add_subdirectory(Panel)
38add_subdirectory(Dash)
38add_subdirectory(Dash)
39+add_subdirectory(libunity8-utils)
src/libunity8-utils/CMakeLists.txtnew file+13
@@ -0,0 +1,13 @@
1+set(UTILS_SRC
2+ abstractdbusservicemonitor.cpp
3+)
4+
5+add_library(unity8-utils SHARED
6+ ${UTILS_SRC}
7+)
8+qt5_use_modules(unity8-utils DBus)
9+
10+# install library
11+install(TARGETS unity8-utils
12+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
13+)
src/libunity8-utils/abstractdbusservicemonitor.cppnew file+84
@@ -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+}
src/libunity8-utils/abstractdbusservicemonitor.hnew file+59
@@ -0,0 +1,59 @@
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+#ifndef ABSTRACTDBUSSERVICEMONITOR_H
21+#define ABSTRACTDBUSSERVICEMONITOR_H
22+
23+#include <QObject>
24+
25+#include <QDBusInterface>
26+#include <QDBusServiceWatcher>
27+#include <QObject>
28+#include <QString>
29+
30+class AbstractDBusServiceMonitor : public QObject
31+{
32+ Q_OBJECT
33+ Q_PROPERTY(bool serviceAvailable READ serviceAvailable NOTIFY serviceAvailableChanged)
34+
35+public:
36+ explicit AbstractDBusServiceMonitor(QString service, QString path, QString interface,
37+ QObject *parent = 0);
38+ ~AbstractDBusServiceMonitor();
39+
40+ QDBusInterface* dbusInterface() const;
41+
42+ bool serviceAvailable() const;
43+
44+Q_SIGNALS:
45+ void serviceAvailableChanged(bool available);
46+
47+private Q_SLOTS:
48+ void createInterface();
49+ void destroyInterface();
50+
51+protected:
52+ QString m_service;
53+ QString m_path;
54+ QString m_interface;
55+ QDBusServiceWatcher* m_watcher;
56+ QDBusInterface* m_dbusInterface;
57+};
58+
59+#endif // ABSTRACTDBUSSERVICEMONITOR_H