19 #include "AccountsServiceDBusAdaptor.h"
20 #include <QDBusConnection>
21 #include <QDBusConnectionInterface>
22 #include <QDBusMessage>
23 #include <QDBusVariant>
26 AccountsServiceDBusAdaptor::AccountsServiceDBusAdaptor(QObject* parent)
28 m_accountsManager(nullptr),
29 m_ignoreNextChanged(false)
31 QDBusConnection connection = QDBusConnection::SM_BUSNAME();
32 QDBusConnectionInterface *
interface = connection.interface();
33 interface->startService(QStringLiteral(
"org.freedesktop.Accounts"));
34 m_accountsManager =
new QDBusInterface(QStringLiteral(
"org.freedesktop.Accounts"),
35 QStringLiteral(
"/org/freedesktop/Accounts"),
36 QStringLiteral(
"org.freedesktop.Accounts"),
40 QDBusPendingReply<QVariant> AccountsServiceDBusAdaptor::getUserPropertyAsync(
const QString &user,
const QString &interface,
const QString &property)
42 QDBusInterface *iface = getUserInterface(user);
43 if (iface !=
nullptr && iface->isValid()) {
44 return iface->asyncCall(QStringLiteral(
"Get"), interface, property);
46 return QDBusPendingReply<QVariant>(QDBusMessage::createError(QDBusError::Other, QStringLiteral(
"Invalid Interface")));
49 QDBusPendingCall AccountsServiceDBusAdaptor::setUserPropertyAsync(
const QString &user,
const QString &interface,
const QString &property,
const QVariant &value)
51 QDBusInterface *iface = getUserInterface(user);
52 if (iface !=
nullptr && iface->isValid()) {
54 return iface->asyncCall(QStringLiteral(
"Set"), interface, property, QVariant::fromValue(QDBusVariant(value)));
56 return QDBusPendingCall::fromCompletedCall(QDBusMessage::createError(QDBusError::Other, QStringLiteral(
"Invalid Interface")));
59 void AccountsServiceDBusAdaptor::propertiesChangedSlot(
const QString &interface,
const QVariantMap &changed,
const QStringList &invalid)
64 combined << changed.keys();
65 combined.removeDuplicates();
67 Q_EMIT propertiesChanged(getUserForPath(message().path()), interface, combined);
72 m_ignoreNextChanged =
true;
75 void AccountsServiceDBusAdaptor::maybeChangedSlot()
77 if (!m_ignoreNextChanged) {
78 Q_EMIT maybeChanged(getUserForPath(message().path()));
80 m_ignoreNextChanged =
false;
83 QString AccountsServiceDBusAdaptor::getUserForPath(
const QString &path)
85 QMap<QString, QDBusInterface *>::const_iterator i;
86 for (i = m_users.constBegin(); i != m_users.constEnd(); ++i) {
87 if (i.value()->path() == path) {
94 QDBusInterface *AccountsServiceDBusAdaptor::getUserInterface(
const QString &user)
96 QDBusInterface *iface = m_users.value(user);
97 if (iface ==
nullptr && m_accountsManager->isValid()) {
98 QDBusReply<QDBusObjectPath> answer = m_accountsManager->call(QStringLiteral(
"FindUserByName"), user);
99 if (answer.isValid()) {
100 const QString path = answer.value().path();
102 iface =
new QDBusInterface(QStringLiteral(
"org.freedesktop.Accounts"),
104 QStringLiteral(
"org.freedesktop.DBus.Properties"),
105 m_accountsManager->connection(),
this);
111 iface->connection().connect(
114 QStringLiteral(
"org.freedesktop.Accounts.User"),
115 QStringLiteral(
"Changed"),
117 SLOT(maybeChangedSlot()));
121 iface->connection().connect(
124 QStringLiteral(
"org.freedesktop.DBus.Properties"),
125 QStringLiteral(
"PropertiesChanged"),
127 SLOT(propertiesChangedSlot(QString, QVariantMap, QStringList)));
129 m_users.insert(user, iface);
131 qWarning() <<
"Couldn't get user interface" << answer.error().name() << answer.error().message();