~mterry/+junk/u8.2

« back to all changes in this revision

Viewing changes to src/libunity8-private/unitydbusobject.cpp

  • Committer: Michael Terry
  • Date: 2014-11-17 14:56:04 UTC
  • mfrom: (1317.1.118 unity8)
  • Revision ID: michael.terry@canonical.com-20141117145604-96dn9p5nwkifq2f4
MergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "unitydbusobject.h"
 
18
 
 
19
#include <QDebug>
 
20
#include <QDBusMessage>
 
21
#include <QMetaClassInfo>
 
22
#include <QTimer>
 
23
 
 
24
UnityDBusObject::UnityDBusObject(const QString &path, const QString &service, bool async, QObject *parent)
 
25
    : QObject(parent)
 
26
    , m_connection(QDBusConnection::sessionBus())
 
27
    , m_path(path)
 
28
    , m_service(service)
 
29
{
 
30
    if (async) {
 
31
        // Use a zero-timer to let Qml finish loading before we announce on DBus
 
32
        QTimer::singleShot(0, this, SLOT(registerObject()));
 
33
    } else {
 
34
        registerObject();
 
35
    }
 
36
}
 
37
 
 
38
UnityDBusObject::~UnityDBusObject()
 
39
{
 
40
    // Leave service in place because multiple objects may be registered with
 
41
    // the same service.  But we know we own the object path and can unregister it.
 
42
    m_connection.unregisterObject(path());
 
43
}
 
44
 
 
45
QDBusConnection UnityDBusObject::connection() const
 
46
{
 
47
    return m_connection;
 
48
}
 
49
 
 
50
QString UnityDBusObject::path() const
 
51
{
 
52
    return m_path;
 
53
}
 
54
 
 
55
// Manually emit a PropertiesChanged signal over DBus, because QtDBus
 
56
// doesn't do it for us on Q_PROPERTIES, oddly enough.
 
57
void UnityDBusObject::notifyPropertyChanged(const QString& propertyName, const QVariant &value)
 
58
{
 
59
    QDBusMessage message;
 
60
    QString interface;
 
61
    QVariantMap changedProps;
 
62
 
 
63
    interface = metaObject()->classInfo(metaObject()->indexOfClassInfo("D-Bus Interface")).value();
 
64
    changedProps.insert(propertyName, value);
 
65
 
 
66
    message = QDBusMessage::createSignal(path(),
 
67
                                         "org.freedesktop.DBus.Properties",
 
68
                                         "PropertiesChanged");
 
69
    message << interface;
 
70
    message << changedProps;
 
71
    message << QStringList();
 
72
 
 
73
    connection().send(message);
 
74
}
 
75
 
 
76
void UnityDBusObject::registerObject()
 
77
{
 
78
    if (!m_connection.registerObject(m_path, this, QDBusConnection::ExportScriptableContents)) {
 
79
        qWarning() << "Unable to register DBus object" << m_path;
 
80
    }
 
81
    if (!m_service.isEmpty()) {
 
82
        if (!m_connection.registerService(m_service)) {
 
83
            qWarning() << "Unable to register DBus service" << m_service;
 
84
        }
 
85
    }
 
86
}