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