~lukas-kde/miral/shellchrome-windowinfo

« back to all changes in this revision

Viewing changes to miral-qt/src/common/abstractdbusservicemonitor.cpp

  • Committer: Larry Price
  • Date: 2016-09-13 16:19:29 UTC
  • mto: (330.4.1 miral)
  • mto: This revision was merged to the branch mainline in revision 352.
  • Revision ID: larry.price@canonical.com-20160913161929-vs9ka1capmljq1es
Removing miral-qt from release branch, updating copyright file, and adding GPL3 license to root dir

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2011-2015 Canonical, Ltd.
3
 
 *
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.
7
 
 *
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.
12
 
 *
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/>.
15
 
 */
16
 
 
17
 
#include "abstractdbusservicemonitor.h"
18
 
 
19
 
#include <QDBusInterface>
20
 
#include <QDBusServiceWatcher>
21
 
#include <QDBusConnection>
22
 
#include <QDBusConnectionInterface>
23
 
#include <QDBusReply>
24
 
 
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
29
 
{
30
 
    Q_OBJECT
31
 
public:
32
 
    AsyncDBusInterface(const QString &service, const QString &path,
33
 
                       const QString &interface, const QDBusConnection &connection,
34
 
                       QObject *parent = 0)
35
 
    : QDBusAbstractInterface(service, path, interface.toLatin1().data(), connection, parent)
36
 
    {}
37
 
    ~AsyncDBusInterface() = default;
38
 
};
39
 
 
40
 
AbstractDBusServiceMonitor::AbstractDBusServiceMonitor(const QString &service, const QString &path,
41
 
                                                       const QString &interface, const QDBusConnection &connection,
42
 
                                                       QObject *parent)
43
 
    : QObject(parent)
44
 
    , m_service(service)
45
 
    , m_path(path)
46
 
    , m_interface(interface)
47
 
    , m_busConnection(connection)
48
 
    , m_watcher(new QDBusServiceWatcher(service, m_busConnection))
49
 
    , m_dbusInterface(nullptr)
50
 
{
51
 
    connect(m_watcher, &QDBusServiceWatcher::serviceRegistered, this, &AbstractDBusServiceMonitor::createInterface);
52
 
    connect(m_watcher, &QDBusServiceWatcher::serviceUnregistered, this, &AbstractDBusServiceMonitor::destroyInterface);
53
 
 
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);
59
 
    }
60
 
}
61
 
 
62
 
AbstractDBusServiceMonitor::~AbstractDBusServiceMonitor()
63
 
{
64
 
    delete m_watcher;
65
 
    delete m_dbusInterface;
66
 
}
67
 
 
68
 
void AbstractDBusServiceMonitor::createInterface(const QString &)
69
 
{
70
 
    if (m_dbusInterface != nullptr) {
71
 
        delete m_dbusInterface;
72
 
        m_dbusInterface = nullptr;
73
 
    }
74
 
 
75
 
    m_dbusInterface = new AsyncDBusInterface(m_service, m_path, m_interface, m_busConnection);
76
 
    Q_EMIT serviceAvailableChanged(true);
77
 
}
78
 
 
79
 
void AbstractDBusServiceMonitor::destroyInterface(const QString &)
80
 
{
81
 
    if (m_dbusInterface != nullptr) {
82
 
        delete m_dbusInterface;
83
 
        m_dbusInterface = nullptr;
84
 
    }
85
 
 
86
 
    Q_EMIT serviceAvailableChanged(false);
87
 
}
88
 
 
89
 
QDBusAbstractInterface* AbstractDBusServiceMonitor::dbusInterface() const
90
 
{
91
 
    return m_dbusInterface;
92
 
}
93
 
 
94
 
bool AbstractDBusServiceMonitor::serviceAvailable() const
95
 
{
96
 
    return m_dbusInterface != nullptr;
97
 
}
98
 
 
99
 
#include "abstractdbusservicemonitor.moc"