@@ -0,0 +1,70 @@
1+/*
2+ * Copyright (C) 2013 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ *
16+ * Author: Michael Terry <michael.terry@canonical.com>
17+ */
18+
19+#include "AccountsService.h"
20+#include <QtCore/QEvent>
21+#include <QtDBus/QDBusPendingReply>
22+
23+AccountsService::AccountsService(QObject* parent)
24+ : QObject(parent),
25+ accounts_manager(NULL),
26+ users()
27+{
28+ accounts_manager = new QDBusInterface("org.freedesktop.Accounts",
29+ "/org/freedesktop/Accounts",
30+ "org.freedesktop.Accounts",
31+ QDBusConnection::SM_BUSNAME(), this);
32+}
33+
34+QVariant AccountsService::getUserProperty(const QString &user, const QString &property)
35+{
36+ auto iface = getUserInterface(user);
37+ if (iface != nullptr && iface->isValid()) {
38+ auto answer = iface->call("Get", "com.canonical.unity.AccountsService", property);
39+ if (answer.type() == QDBusMessage::ReplyMessage) {
40+ return answer.arguments()[0].value<QDBusVariant>().variant();
41+ }
42+ }
43+ return QVariant();
44+}
45+
46+void AccountsService::setUserProperty(const QString &user, const QString &property, const QVariant &value)
47+{
48+ auto iface = getUserInterface(user);
49+ if (iface != nullptr && iface->isValid()) {
50+ // The value needs to be carefully wrapped thrice
51+ iface->call("Set", "com.canonical.unity.AccountsService", property, QVariant::fromValue(QDBusVariant(value)));
52+ }
53+}
54+
55+QDBusInterface *AccountsService::getUserInterface(const QString &user)
56+{
57+ auto iface = users.value(user);
58+ if (iface == nullptr && accounts_manager->isValid()) {
59+ auto answer = accounts_manager->call("FindUserByName", user);
60+ if (answer.type() == QDBusMessage::ReplyMessage) {
61+ iface = new QDBusInterface("org.freedesktop.Accounts",
62+ answer.arguments()[0].value<QDBusObjectPath>().path(),
63+ "org.freedesktop.DBus.Properties",
64+ accounts_manager->connection(), this);
65+ users.insert(user, iface);
66+ }
67+ }
68+ return iface;
69+}
70+