~nick-dedekind/unity8/indicator.submenu-reinit

« back to all changes in this revision

Viewing changes to plugins/AccountsService/AccountsServiceDBusAdaptor.cpp

  • Committer: Nick Dedekind
  • Date: 2013-09-16 07:37:30 UTC
  • mfrom: (229.1.91 trunk)
  • Revision ID: nicholas.dedekind@gmail.com-20130916073730-3o9iv6i9h0d2c7rl
merged with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 * Author: Michael Terry <michael.terry@canonical.com>
17
17
 */
18
18
 
19
 
#include "AccountsService.h"
 
19
#include "AccountsServiceDBusAdaptor.h"
20
20
#include <QDBusConnection>
21
21
#include <QDBusConnectionInterface>
22
22
#include <QDBusMessage>
23
23
#include <QDBusVariant>
24
24
 
25
 
AccountsService::AccountsService(QObject* parent)
 
25
AccountsServiceDBusAdaptor::AccountsServiceDBusAdaptor(QObject* parent)
26
26
  : QObject(parent),
27
27
    accounts_manager(NULL),
28
28
    users()
36
36
                                          connection, this);
37
37
}
38
38
 
39
 
QVariant AccountsService::getUserProperty(const QString &user, const QString &property)
 
39
QVariant AccountsServiceDBusAdaptor::getUserProperty(const QString &user, const QString &interface, const QString &property)
40
40
{
41
41
    auto iface = getUserInterface(user);
42
42
    if (iface != nullptr && iface->isValid()) {
43
 
        auto answer = iface->call("Get", "com.canonical.unity.AccountsService", property);
 
43
        auto answer = iface->call("Get", interface, property);
44
44
        if (answer.type() == QDBusMessage::ReplyMessage) {
45
45
            return answer.arguments()[0].value<QDBusVariant>().variant();
46
46
        }
48
48
    return QVariant();
49
49
}
50
50
 
51
 
void AccountsService::setUserProperty(const QString &user, const QString &property, const QVariant &value)
 
51
void AccountsServiceDBusAdaptor::setUserProperty(const QString &user, const QString &interface, const QString &property, const QVariant &value)
52
52
{
53
53
    auto iface = getUserInterface(user);
54
54
    if (iface != nullptr && iface->isValid()) {
55
55
        // The value needs to be carefully wrapped
56
 
        iface->call("Set", "com.canonical.unity.AccountsService", property, QVariant::fromValue(QDBusVariant(value)));
57
 
    }
58
 
}
59
 
 
60
 
QDBusInterface *AccountsService::getUserInterface(const QString &user)
 
56
        iface->call("Set", interface, property, QVariant::fromValue(QDBusVariant(value)));
 
57
    }
 
58
}
 
59
 
 
60
void AccountsServiceDBusAdaptor::propertiesChangedSlot(const QString &interface, const QVariantMap &changed, const QStringList &invalid)
 
61
{
 
62
    // Merge changed and invalidated together
 
63
    QStringList combined;
 
64
    combined << invalid;
 
65
    combined << changed.keys();
 
66
    combined.removeDuplicates();
 
67
 
 
68
    Q_EMIT propertiesChanged(getUserForPath(message().path()), interface, combined);
 
69
}
 
70
 
 
71
void AccountsServiceDBusAdaptor::maybeChangedSlot()
 
72
{
 
73
    Q_EMIT maybeChanged(getUserForPath(message().path()));
 
74
}
 
75
 
 
76
QString AccountsServiceDBusAdaptor::getUserForPath(const QString &path)
 
77
{
 
78
    QMap<QString, QDBusInterface *>::const_iterator i;
 
79
    for (i = users.constBegin(); i != users.constEnd(); ++i) {
 
80
        if (i.value()->path() == path) {
 
81
            return i.key();
 
82
        }
 
83
    }
 
84
    return QString();
 
85
}
 
86
 
 
87
QDBusInterface *AccountsServiceDBusAdaptor::getUserInterface(const QString &user)
61
88
{
62
89
    auto iface = users.value(user);
63
90
    if (iface == nullptr && accounts_manager->isValid()) {
64
91
        auto answer = accounts_manager->call("FindUserByName", user);
65
92
        if (answer.type() == QDBusMessage::ReplyMessage) {
 
93
            auto path = answer.arguments()[0].value<QDBusObjectPath>().path();
 
94
 
66
95
            iface = new QDBusInterface("org.freedesktop.Accounts",
67
 
                                       answer.arguments()[0].value<QDBusObjectPath>().path(),
 
96
                                       path,
68
97
                                       "org.freedesktop.DBus.Properties",
69
98
                                       accounts_manager->connection(), this);
 
99
 
 
100
            // With its own pre-defined properties, AccountsService is oddly
 
101
            // close-lipped.  It won't send out proper DBus.Properties notices,
 
102
            // but it does have one catch-all Changed() signal.  So let's
 
103
            // listen to that.
 
104
            iface->connection().connect(
 
105
                iface->service(),
 
106
                path,
 
107
                "org.freedesktop.Accounts.User",
 
108
                "Changed",
 
109
                this,
 
110
                SLOT(maybeChangedSlot()));
 
111
 
 
112
            // But custom properties do send out the right notifications, so
 
113
            // let's still listen there.
 
114
            iface->connection().connect(
 
115
                iface->service(),
 
116
                path,
 
117
                "org.freedesktop.DBus.Properties",
 
118
                "PropertiesChanged",
 
119
                this,
 
120
                SLOT(propertiesChangedSlot(QString, QVariantMap, QStringList)));
 
121
 
70
122
            users.insert(user, iface);
71
123
        }
72
124
    }