on df92548 help sign in
unity8/plugins/AccountsService/AccountsServiceDBusAdaptor.cpp
144 lines 5.6 kB history raw
1/*
2 * Copyright (C) 2013 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 * Author: Michael Terry <michael.terry@canonical.com>
17 */
19#include "AccountsServiceDBusAdaptor.h"
20#include <QDBusConnection>
21#include <QDBusConnectionInterface>
22#include <QDBusMessage>
23#include <QDBusVariant>
25AccountsServiceDBusAdaptor::AccountsServiceDBusAdaptor(QObject* parent)
26 : QObject(parent),
27 m_accountsManager(nullptr),
28 m_users(),
29 m_ignoreNextChanged(false)
31 QDBusConnection connection = QDBusConnection::SM_BUSNAME();
32 QDBusConnectionInterface *interface = connection.interface();
33 interface->startService("org.freedesktop.Accounts");
34 m_accountsManager = new QDBusInterface("org.freedesktop.Accounts",
35 "/org/freedesktop/Accounts",
36 "org.freedesktop.Accounts",
37 connection, this);
40QVariant AccountsServiceDBusAdaptor::getUserProperty(const QString &user, const QString &interface, const QString &property)
42 QDBusInterface *iface = getUserInterface(user);
43 if (iface != nullptr && iface->isValid()) {
44 QDBusMessage answer = iface->call("Get", interface, property);
45 if (answer.type() == QDBusMessage::ReplyMessage) {
46 return answer.arguments()[0].value<QDBusVariant>().variant();
47 }
48 }
49 return QVariant();
52void AccountsServiceDBusAdaptor::setUserProperty(const QString &user, const QString &interface, const QString &property, const QVariant &value)
54 QDBusInterface *iface = getUserInterface(user);
55 if (iface != nullptr && iface->isValid()) {
56 // The value needs to be carefully wrapped
57 iface->call("Set", interface, property, QVariant::fromValue(QDBusVariant(value)));
58 }
61void AccountsServiceDBusAdaptor::setUserPropertyAsync(const QString &user, const QString &interface, const QString &property, const QVariant &value)
63 QDBusInterface *iface = getUserInterface(user);
64 if (iface != nullptr && iface->isValid()) {
65 // The value needs to be carefully wrapped
66 iface->asyncCall("Set", interface, property, QVariant::fromValue(QDBusVariant(value)));
67 }
70void AccountsServiceDBusAdaptor::propertiesChangedSlot(const QString &interface, const QVariantMap &changed, const QStringList &invalid)
72 // Merge changed and invalidated together
73 QStringList combined;
74 combined << invalid;
75 combined << changed.keys();
76 combined.removeDuplicates();
78 Q_EMIT propertiesChanged(getUserForPath(message().path()), interface, combined);
80 // In case a non-builtin property changes, we're getting propertiesChanged *and* changed
81 // As the generic changed requires asking back over DBus, it's quite slow to process.
82 // We don't want to trigger that when we know it's not a built-in property change.
83 m_ignoreNextChanged = true;
86void AccountsServiceDBusAdaptor::maybeChangedSlot()
88 if (!m_ignoreNextChanged) {
89 Q_EMIT maybeChanged(getUserForPath(message().path()));
90 }
91 m_ignoreNextChanged = false;
94QString AccountsServiceDBusAdaptor::getUserForPath(const QString &path)
96 QMap<QString, QDBusInterface *>::const_iterator i;
97 for (i = m_users.constBegin(); i != m_users.constEnd(); ++i) {
98 if (i.value()->path() == path) {
99 return i.key();
100 }
101 }
102 return QString();
105QDBusInterface *AccountsServiceDBusAdaptor::getUserInterface(const QString &user)
107 QDBusInterface *iface = m_users.value(user);
108 if (iface == nullptr && m_accountsManager->isValid()) {
109 QDBusMessage answer = m_accountsManager->call("FindUserByName", user);
110 if (answer.type() == QDBusMessage::ReplyMessage) {
111 QString path = answer.arguments()[0].value<QDBusObjectPath>().path();
113 iface = new QDBusInterface("org.freedesktop.Accounts",
114 path,
115 "org.freedesktop.DBus.Properties",
116 m_accountsManager->connection(), this);
118 // With its own pre-defined properties, AccountsService is oddly
119 // close-lipped. It won't send out proper DBus.Properties notices,
120 // but it does have one catch-all Changed() signal. So let's
121 // listen to that.
122 iface->connection().connect(
123 iface->service(),
124 path,
125 "org.freedesktop.Accounts.User",
126 "Changed",
127 this,
128 SLOT(maybeChangedSlot()));
130 // But custom properties do send out the right notifications, so
131 // let's still listen there.
132 iface->connection().connect(
133 iface->service(),
134 path,
135 "org.freedesktop.DBus.Properties",
136 "PropertiesChanged",
137 this,
138 SLOT(propertiesChangedSlot(QString, QVariantMap, QStringList)));
140 m_users.insert(user, iface);
141 }
142 }
143 return iface;