on 577fe5b help sign in
unity8/plugins/AccountsService/AccountsService.cpp
70 lines 2.7 kB history raw
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 */
19#include "AccountsService.h"
20#include <QtDBus/QDBusConnection>
21#include <QtDBus/QDBusMessage>
22#include <QtDBus/QDBusVariant>
24AccountsService::AccountsService(QObject* parent)
25 : QObject(parent),
26 accounts_manager(NULL),
27 users()
29 accounts_manager = new QDBusInterface("org.freedesktop.Accounts",
30 "/org/freedesktop/Accounts",
31 "org.freedesktop.Accounts",
32 QDBusConnection::SM_BUSNAME(), this);
35QVariant AccountsService::getUserProperty(const QString &user, const QString &property)
37 auto iface = getUserInterface(user);
38 if (iface != nullptr && iface->isValid()) {
39 auto answer = iface->call("Get", "com.canonical.unity.AccountsService", property);
40 if (answer.type() == QDBusMessage::ReplyMessage) {
41 return answer.arguments()[0].value<QDBusVariant>().variant();
42 }
43 }
44 return QVariant();
47void AccountsService::setUserProperty(const QString &user, const QString &property, const QVariant &value)
49 auto iface = getUserInterface(user);
50 if (iface != nullptr && iface->isValid()) {
51 // The value needs to be carefully wrapped
52 iface->call("Set", "com.canonical.unity.AccountsService", property, QVariant::fromValue(QDBusVariant(value)));
53 }
56QDBusInterface *AccountsService::getUserInterface(const QString &user)
58 auto iface = users.value(user);
59 if (iface == nullptr && accounts_manager->isValid()) {
60 auto answer = accounts_manager->call("FindUserByName", user);
61 if (answer.type() == QDBusMessage::ReplyMessage) {
62 iface = new QDBusInterface("org.freedesktop.Accounts",
63 answer.arguments()[0].value<QDBusObjectPath>().path(),
64 "org.freedesktop.DBus.Properties",
65 accounts_manager->connection(), this);
66 users.insert(user, iface);
67 }
68 }
69 return iface;