on 4bf0fdb help sign in
unity8/src/libunity8-private/unitydbusvirtualobject.cpp
79 lines 2.6 kB history raw
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 */
17#include "unitydbusvirtualobject.h"
19#include <QDebug>
20#include <QDBusMessage>
21#include <QTimer>
23UnityDBusVirtualObject::UnityDBusVirtualObject(const QString &path, const QString &service, QObject *parent)
24 : QDBusVirtualObject(parent)
25 , m_connection(QDBusConnection::sessionBus())
26 , m_path(path)
27 , m_service(service)
29 // Use a zero-timer to let Qml finish loading before we announce on DBus
30 QTimer::singleShot(0, this, SLOT(registerObject()));
33UnityDBusVirtualObject::~UnityDBusVirtualObject()
35 // Leave service in place because multiple objects may be registered with
36 // the same service. But we know we own the object path and can unregister it.
37 m_connection.unregisterObject(path());
40QDBusConnection UnityDBusVirtualObject::connection() const
42 return m_connection;
45QString UnityDBusVirtualObject::path() const
47 return m_path;
50// Manually emit a PropertiesChanged signal over DBus, because QtDBus
51// doesn't do it for us on Q_PROPERTIES, oddly enough.
52void UnityDBusVirtualObject::notifyPropertyChanged(const QString& interface, const QString& node, const QString& propertyName, const QVariant &value)
54 QDBusMessage message;
55 QVariantMap changedProps;
57 changedProps.insert(propertyName, value);
59 message = QDBusMessage::createSignal(path() + "/" + node,
60 "org.freedesktop.DBus.Properties",
61 "PropertiesChanged");
62 message << interface;
63 message << changedProps;
64 message << QStringList();
66 connection().send(message);
69void UnityDBusVirtualObject::registerObject()
71 if (!m_connection.registerVirtualObject(m_path, this, QDBusConnection::VirtualObjectRegisterOption::SubPath)) {
72 qWarning() << "Unable to register DBus object" << m_path;
73 }
74 if (!m_service.isEmpty()) {
75 if (!m_connection.registerService(m_service)) {
76 qWarning() << "Unable to register DBus service" << m_service;
77 }
78 }