@@ -17,110 +17,88 @@
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)),
27+ m_user(qgetenv("USER"))
2928{
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);
29+ connect(m_service, SIGNAL(propertiesChanged(const QString &, const QString &, const QStringList &)),
30+ this, SLOT(propertiesChanged(const QString &, const QString &, const QStringList &)));
31+ connect(m_service, SIGNAL(maybeChanged(const QString &)),
32+ this, SLOT(maybeChanged(const QString &)));
3733}
3834
39−QVariant AccountsService::getUserProperty(const QString &user, const QString &interface, const QString &property)
35+QString AccountsService::getUser()
4036{
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();
37+ return m_user;
4938}
5039
51−void AccountsService::setUserProperty(const QString &user, const QString &interface, const QString &property, const QVariant &value)
40+void AccountsService::setUser(const QString &user)
5241{
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− }
42+ m_user = user;
43+ Q_EMIT userChanged();
44+
45+ updateDemoEdges();
46+ updateBackgroundFile();
5847}
5948
60−void AccountsService::propertiesChangedSlot(const QString &interface, const QVariantMap &changed, const QStringList &invalid)
49+bool AccountsService::getDemoEdges()
6150{
62− // Merge changed and invalidated together
63− QStringList combined;
64− combined << invalid;
65− combined << changed.keys();
66− combined.removeDuplicates();
51+ return m_demoEdges;
52+}
6753
68− Q_EMIT propertiesChanged(getUserForPath(message().path()), interface, combined);
54+void AccountsService::setDemoEdges(bool demoEdges)
55+{
56+ m_demoEdges = demoEdges;
57+ m_service->setUserProperty(m_user, "com.canonical.unity.AccountsService", "demo-edges", demoEdges);
6958}
7059
71−void AccountsService::maybeChangedSlot()
60+QString AccountsService::getBackgroundFile()
7261{
73− Q_EMIT maybeChanged(getUserForPath(message().path()));
62+ return m_backgroundFile;
7463}
7564
76−QString AccountsService::getUserForPath(const QString &path)
65+void AccountsService::updateDemoEdges()
7766{
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− }
67+ auto demoEdges = m_service->getUserProperty(m_user, "com.canonical.unity.AccountsService", "demo-edges").toBool();
68+ if (m_demoEdges != demoEdges) {
69+ m_demoEdges = demoEdges;
70+ Q_EMIT demoEdgesChanged();
8371 }
84− return QString();
8572}
8673
87−QDBusInterface *AccountsService::getUserInterface(const QString &user)
74+void AccountsService::updateBackgroundFile()
8875{
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()));
76+ auto backgroundFile = m_service->getUserProperty(m_user, "org.freedesktop.Accounts.User", "BackgroundFile").toString();
77+ if (m_backgroundFile != backgroundFile) {
78+ m_backgroundFile = backgroundFile;
79+ Q_EMIT backgroundFileChanged();
80+ }
81+}
11182
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)));
83+void AccountsService::propertiesChanged(const QString &user, const QString &interface, const QStringList &changed)
84+{
85+ if (m_user != user) {
86+ return;
87+ }
12188
122− users.insert(user, iface);
89+ if (interface == "com.canonical.unity.AccountsService") {
90+ if (changed.contains("demo-edges")) {
91+ updateDemoEdges();
12392 }
12493 }
125− return iface;
94+}
95+
96+void AccountsService::maybeChanged(const QString &user)
97+{
98+ if (m_user != user) {
99+ return;
100+ }
101+
102+ // Standard properties might have changed
103+ updateBackgroundFile();
126104}