Unity 8
 All Classes Functions Properties
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(NULL),
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::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 = m_users.constBegin(); i != m_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)
88 {
89  QDBusInterface *iface = m_users.value(user);
90  if (iface == nullptr && m_accountsManager->isValid()) {
91  QDBusMessage answer = m_accountsManager->call("FindUserByName", user);
92  if (answer.type() == QDBusMessage::ReplyMessage) {
93  QString path = answer.arguments()[0].value<QDBusObjectPath>().path();
94 
95  iface = new QDBusInterface("org.freedesktop.Accounts",
96  path,
97  "org.freedesktop.DBus.Properties",
98  m_accountsManager->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 
122  m_users.insert(user, iface);
123  }
124  }
125  return iface;
126 }