Lomiri
Loading...
Searching...
No Matches
lomiridbusvirtualobject.cpp
1/*
2 * Copyright (C) 2014 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "lomiridbusvirtualobject.h"
18
19#include <QDebug>
20#include <QDBusMessage>
21#include <QTimer>
22
23LomiriDBusVirtualObject::LomiriDBusVirtualObject(const QString &path, const QString &service, bool async, QObject *parent)
24 : QDBusVirtualObject(parent)
25 , m_connection(QDBusConnection::sessionBus())
26 , m_path(path)
27 , m_service(service)
28{
29 if (async) {
30 // Use a zero-timer to let Qml finish loading before we announce on DBus
31 QTimer::singleShot(0, this, &LomiriDBusVirtualObject::registerObject);
32 } else {
33 registerObject();
34 }
35}
36
37LomiriDBusVirtualObject::~LomiriDBusVirtualObject()
38{
39 // Leave service in place because multiple objects may be registered with
40 // the same service. But we know we own the object path and can unregister it.
41 m_connection.unregisterObject(path());
42}
43
44QDBusConnection LomiriDBusVirtualObject::connection() const
45{
46 return m_connection;
47}
48
49QString LomiriDBusVirtualObject::path() const
50{
51 return m_path;
52}
53
54// Manually emit a PropertiesChanged signal over DBus, because QtDBus
55// doesn't do it for us on Q_PROPERTIES, oddly enough.
56void LomiriDBusVirtualObject::notifyPropertyChanged(const QString& interface, const QString& node, const QString& propertyName, const QVariant &value)
57{
58 QDBusMessage message;
59 QVariantMap changedProps;
60
61 changedProps.insert(propertyName, value);
62
63 message = QDBusMessage::createSignal(path() + "/" + node,
64 QStringLiteral("org.freedesktop.DBus.Properties"),
65 QStringLiteral("PropertiesChanged"));
66 message << interface;
67 message << changedProps;
68 message << QStringList();
69
70 connection().send(message);
71}
72
73void LomiriDBusVirtualObject::registerObject()
74{
75 if (!m_connection.registerVirtualObject(m_path, this, QDBusConnection::VirtualObjectRegisterOption::SubPath)) {
76 qWarning() << "Unable to register DBus object" << m_path;
77 }
78 if (!m_service.isEmpty()) {
79 if (!m_connection.registerService(m_service)) {
80 qWarning() << "Unable to register DBus service" << m_service;
81 }
82 }
83}