@@ -17,110 +17,109 @@
1717 */
1818
1919#include "AccountsService.h"
20−#include <QDBusConnection>
21−#include <QDBusConnectionInterface>
22−#include <QDBusMessage>
23−#include <QDBusVariant>
20+#include "AccountsServiceDBusAdaptor.h"
21+
22+#include <QStringList>
2423
2524AccountsService::AccountsService(QObject* parent)
2625 : QObject(parent),
27− accounts_manager(NULL),
28− users()
26+ m_service(new AccountsServiceDBusAdaptor(this))
2927{
30− auto connection = QDBusConnection::SM_BUSNAME();
31− auto interface = connection.interface();
32− interface->startService("org.freedesktop.Accounts");
33− accounts_manager = new QDBusInterface("org.freedesktop.Accounts",
34− "/org/freedesktop/Accounts",
35− "org.freedesktop.Accounts",
36− connection, this);
28+ connect(m_service, SIGNAL(propertiesChanged(const QString &, const QString &, const QStringList &)),
29+ this, SLOT(propertiesChanged(const QString &, const QString &, const QStringList &)));
30+ connect(m_service, SIGNAL(maybeChanged(const QString &)),
31+ this, SLOT(maybeChanged(const QString &)));
32+
33+ setUser(qgetenv("USER"));
34+ updateDemoEdgesForCurrentUser();
3735}
3836
39−QVariant AccountsService::getUserProperty(const QString &user, const QString &interface, const QString &property)
37+QString AccountsService::getUser()
4038{
41− auto iface = getUserInterface(user);
42− if (iface != nullptr && iface->isValid()) {
43− auto 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();
39+ return m_user;
4940}
5041
51−void AccountsService::setUserProperty(const QString &user, const QString &interface, const QString &property, const QVariant &value)
42+void AccountsService::setUser(const QString &user)
5243{
53− auto 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− }
44+ m_user = user;
45+ Q_EMIT userChanged();
46+
47+ updateDemoEdges();
48+ updateBackgroundFile();
49+}
50+
51+bool AccountsService::getDemoEdges()
52+{
53+ return m_demoEdges;
5854}
5955
60−void AccountsService::propertiesChangedSlot(const QString &interface, const QVariantMap &changed, const QStringList &invalid)
56+void AccountsService::setDemoEdges(bool demoEdges)
6157{
62− // Merge changed and invalidated together
63− QStringList combined;
64− combined << invalid;
65− combined << changed.keys();
66− combined.removeDuplicates();
58+ m_demoEdges = demoEdges;
59+ m_service->setUserProperty(m_user, "com.canonical.unity.AccountsService", "demo-edges", demoEdges);
60+}
6761
68− Q_EMIT propertiesChanged(getUserForPath(message().path()), interface, combined);
62+bool AccountsService::getDemoEdgesForCurrentUser()
63+{
64+ return m_demoEdgesForCurrentUser;
6965}
7066
71−void AccountsService::maybeChangedSlot()
67+void AccountsService::setDemoEdgesForCurrentUser(bool demoEdgesForCurrentUser)
7268{
73− Q_EMIT maybeChanged(getUserForPath(message().path()));
69+ m_demoEdgesForCurrentUser = demoEdgesForCurrentUser;
70+ m_service->setUserProperty(qgetenv("USER"), "com.canonical.unity.AccountsService", "demo-edges", demoEdgesForCurrentUser);
7471}
7572
76−QString AccountsService::getUserForPath(const QString &path)
73+QString AccountsService::getBackgroundFile()
7774{
78− QMap<QString, QDBusInterface *>::const_iterator i;
79− for (i = users.constBegin(); i != users.constEnd(); ++i) {
80− if (i.value()->path() == path) {
81− return i.key();
82− }
75+ return m_backgroundFile;
76+}
77+
78+void AccountsService::updateDemoEdges()
79+{
80+ auto demoEdges = m_service->getUserProperty(m_user, "com.canonical.unity.AccountsService", "demo-edges").toBool();
81+ if (m_demoEdges != demoEdges) {
82+ m_demoEdges = demoEdges;
83+ Q_EMIT demoEdgesChanged();
8384 }
84− return QString();
8585}
8686
87−QDBusInterface *AccountsService::getUserInterface(const QString &user)
87+void AccountsService::updateDemoEdgesForCurrentUser()
8888{
89− auto iface = users.value(user);
90− if (iface == nullptr && accounts_manager->isValid()) {
91− auto answer = accounts_manager->call("FindUserByName", user);
92− if (answer.type() == QDBusMessage::ReplyMessage) {
93− auto path = answer.arguments()[0].value<QDBusObjectPath>().path();
94−
95− iface = new QDBusInterface("org.freedesktop.Accounts",
96− path,
97− "org.freedesktop.DBus.Properties",
98− accounts_manager->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− users.insert(user, iface);
89+ auto demoEdgesForCurrentUser = m_service->getUserProperty(qgetenv("USER"), "com.canonical.unity.AccountsService", "demo-edges").toBool();
90+ if (m_demoEdgesForCurrentUser != demoEdgesForCurrentUser) {
91+ m_demoEdgesForCurrentUser = demoEdgesForCurrentUser;
92+ Q_EMIT demoEdgesForCurrentUserChanged();
93+ }
94+}
95+
96+void AccountsService::updateBackgroundFile()
97+{
98+ auto backgroundFile = m_service->getUserProperty(m_user, "org.freedesktop.Accounts.User", "BackgroundFile").toString();
99+ if (m_backgroundFile != backgroundFile) {
100+ m_backgroundFile = backgroundFile;
101+ Q_EMIT backgroundFileChanged();
102+ }
103+}
104+
105+void AccountsService::propertiesChanged(const QString &user, const QString &interface, const QStringList &changed)
106+{
107+ if (interface == "com.canonical.unity.AccountsService") {
108+ if (changed.contains("demo-edges")) {
109+ if (qgetenv("USER") == user) {
110+ updateDemoEdgesForCurrentUser();
111+ }
112+ if (m_user != user) {
113+ updateDemoEdges();
114+ }
123115 }
124116 }
125− return iface;
117+}
118+
119+void AccountsService::maybeChanged(const QString &user)
120+{
121+ if (m_user == user) {
122+ // Standard properties might have changed
123+ updateBackgroundFile();
124+ }
126125}