19 #include "AccountsServiceDBusAdaptor.h"
20 #include <QDBusConnection>
21 #include <QDBusConnectionInterface>
22 #include <QDBusMessage>
23 #include <QDBusVariant>
25 AccountsServiceDBusAdaptor::AccountsServiceDBusAdaptor(QObject* parent)
27 m_accountsManager(nullptr),
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",
40 QVariant 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();
52 void 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()) {
57 iface->call(
"Set", interface, property, QVariant::fromValue(QDBusVariant(value)));
61 void 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()) {
66 iface->asyncCall(
"Set", interface, property, QVariant::fromValue(QDBusVariant(value)));
70 void AccountsServiceDBusAdaptor::propertiesChangedSlot(
const QString &interface,
const QVariantMap &changed,
const QStringList &invalid)
75 combined << changed.keys();
76 combined.removeDuplicates();
78 Q_EMIT propertiesChanged(getUserForPath(message().path()), interface, combined);
83 m_ignoreNextChanged =
true;
86 void AccountsServiceDBusAdaptor::maybeChangedSlot()
88 if (!m_ignoreNextChanged) {
89 Q_EMIT maybeChanged(getUserForPath(message().path()));
91 m_ignoreNextChanged =
false;
94 QString 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) {
105 QDBusInterface *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",
115 "org.freedesktop.DBus.Properties",
116 m_accountsManager->connection(),
this);
122 iface->connection().connect(
125 "org.freedesktop.Accounts.User",
128 SLOT(maybeChangedSlot()));
132 iface->connection().connect(
135 "org.freedesktop.DBus.Properties",
138 SLOT(propertiesChangedSlot(QString, QVariantMap, QStringList)));
140 m_users.insert(user, iface);