17#include "AccountsServiceDBusAdaptor.h"
18#include <QDBusConnection>
19#include <QDBusConnectionInterface>
20#include <QDBusMessage>
21#include <QDBusVariant>
24AccountsServiceDBusAdaptor::AccountsServiceDBusAdaptor(QObject* parent)
26 m_accountsManager(nullptr),
27 m_ignoreNextChanged(false)
29 QDBusConnection connection = QDBusConnection::SM_BUSNAME();
30 QDBusConnectionInterface *
interface = connection.interface();
31 interface->startService(QStringLiteral(
"org.freedesktop.Accounts"));
32 m_accountsManager =
new QDBusInterface(QStringLiteral(
"org.freedesktop.Accounts"),
33 QStringLiteral(
"/org/freedesktop/Accounts"),
34 QStringLiteral(
"org.freedesktop.Accounts"),
38QDBusPendingReply<QVariantMap> AccountsServiceDBusAdaptor::getAllPropertiesAsync(
const QString &user,
const QString &interface)
40 QDBusInterface *iface = getUserInterface(user);
41 if (iface !=
nullptr && iface->isValid()) {
42 return iface->asyncCall(QStringLiteral(
"GetAll"), interface);
44 return QDBusPendingReply<QVariantMap>(QDBusMessage::createError(QDBusError::Other, QStringLiteral(
"Invalid Interface")));
47QDBusPendingReply<QVariant> AccountsServiceDBusAdaptor::getUserPropertyAsync(
const QString &user,
const QString &interface,
const QString &property)
49 QDBusInterface *iface = getUserInterface(user);
50 if (iface !=
nullptr && iface->isValid()) {
51 return iface->asyncCall(QStringLiteral(
"Get"), interface, property);
53 return QDBusPendingReply<QVariant>(QDBusMessage::createError(QDBusError::Other, QStringLiteral(
"Invalid Interface")));
56QDBusPendingCall AccountsServiceDBusAdaptor::setUserPropertyAsync(
const QString &user,
const QString &interface,
const QString &property,
const QVariant &value)
58 QDBusInterface *iface = getUserInterface(user);
59 if (iface !=
nullptr && iface->isValid()) {
60 if (interface == QStringLiteral(
"org.freedesktop.Accounts.User")) {
63 QDBusInterface accountsIface(iface->service(),
67 return accountsIface.asyncCall(QStringLiteral(
"Set") + property, value);
70 return iface->asyncCall(QStringLiteral(
"Set"), interface, property, QVariant::fromValue(QDBusVariant(value)));
73 return QDBusPendingCall::fromCompletedCall(QDBusMessage::createError(QDBusError::Other, QStringLiteral(
"Invalid Interface")));
76void AccountsServiceDBusAdaptor::propertiesChangedSlot(
const QString &interface,
const QVariantMap &changed,
const QStringList &invalid)
81 combined << changed.keys();
82 combined.removeDuplicates();
84 Q_EMIT propertiesChanged(getUserForPath(message().path()), interface, combined);
89 m_ignoreNextChanged =
true;
92void AccountsServiceDBusAdaptor::maybeChangedSlot()
94 if (!m_ignoreNextChanged) {
95 Q_EMIT maybeChanged(getUserForPath(message().path()));
97 m_ignoreNextChanged =
false;
100QString AccountsServiceDBusAdaptor::getUserForPath(
const QString &path)
const
102 QMap<QString, QDBusInterface *>::const_iterator i;
103 for (i = m_users.constBegin(); i != m_users.constEnd(); ++i) {
104 if (i.value()->path() == path) {
111QDBusInterface *AccountsServiceDBusAdaptor::getUserInterface(
const QString &user)
113 QDBusInterface *iface = m_users.value(user);
114 if (iface ==
nullptr && m_accountsManager->isValid()) {
115 QDBusReply<QDBusObjectPath> answer = m_accountsManager->call(QStringLiteral(
"FindUserByName"), user);
116 if (answer.isValid()) {
117 const QString path = answer.value().path();
119 iface =
new QDBusInterface(QStringLiteral(
"org.freedesktop.Accounts"),
121 QStringLiteral(
"org.freedesktop.DBus.Properties"),
122 m_accountsManager->connection(),
this);
128 iface->connection().connect(
131 QStringLiteral(
"org.freedesktop.Accounts.User"),
132 QStringLiteral(
"Changed"),
134 SLOT(maybeChangedSlot()));
138 iface->connection().connect(
141 QStringLiteral(
"org.freedesktop.DBus.Properties"),
142 QStringLiteral(
"PropertiesChanged"),
144 SLOT(propertiesChangedSlot(QString, QVariantMap, QStringList)));
146 m_users.insert(user, iface);
148 qWarning() <<
"Couldn't get user interface" << answer.error().name() << answer.error().message();