2 * Copyright (C) 2013, 2015 Canonical, Ltd.
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.
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.
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/>.
17#include "AccountsService.h"
18#include "AccountsServiceDBusAdaptor.h"
20#include <QDBusInterface>
25#define IFACE_ACCOUNTS_USER QStringLiteral("org.freedesktop.Accounts.User")
26#define IFACE_LOCATION_HERE QStringLiteral("com.ubuntu.location.providers.here.AccountsService")
27#define IFACE_UBUNTU_INPUT QStringLiteral("com.ubuntu.AccountsService.Input")
28#define IFACE_UBUNTU_SECURITY QStringLiteral("com.ubuntu.AccountsService.SecurityPrivacy")
29#define IFACE_UBUNTU_SECURITY_OLD QStringLiteral("com.ubuntu.touch.AccountsService.SecurityPrivacy")
30#define IFACE_UNITY QStringLiteral("com.canonical.unity.AccountsService")
31#define IFACE_UNITY_PRIVATE QStringLiteral("com.canonical.unity.AccountsService.Private")
33#define PROP_BACKGROUND_FILE QStringLiteral("BackgroundFile")
34#define PROP_DEMO_EDGES QStringLiteral("demo-edges")
35#define PROP_EMAIL QStringLiteral("Email")
36#define PROP_ENABLE_INDICATORS_WHILE_LOCKED QStringLiteral("EnableIndicatorsWhileLocked")
37#define PROP_ENABLE_LAUNCHER_WHILE_LOCKED QStringLiteral("EnableLauncherWhileLocked")
38#define PROP_FAILED_LOGINS QStringLiteral("FailedLogins")
39#define PROP_INPUT_SOURCES QStringLiteral("InputSources")
40#define PROP_LICENSE_ACCEPTED QStringLiteral("LicenseAccepted")
41#define PROP_LICENSE_BASE_PATH QStringLiteral("LicenseBasePath")
42#define PROP_MOUSE_CURSOR_SPEED QStringLiteral("MouseCursorSpeed")
43#define PROP_MOUSE_DOUBLE_CLICK_SPEED QStringLiteral("MouseDoubleClickSpeed")
44#define PROP_MOUSE_PRIMARY_BUTTON QStringLiteral("MousePrimaryButton")
45#define PROP_MOUSE_SCROLL_SPEED QStringLiteral("MouseScrollSpeed")
46#define PROP_PASSWORD_DISPLAY_HINT QStringLiteral("PasswordDisplayHint")
47#define PROP_REAL_NAME QStringLiteral("RealName")
48#define PROP_STATS_WELCOME_SCREEN QStringLiteral("StatsWelcomeScreen")
49#define PROP_TOUCHPAD_CURSOR_SPEED QStringLiteral("TouchpadCursorSpeed")
50#define PROP_TOUCHPAD_DISABLE_WHILE_TYPING QStringLiteral("TouchpadDisableWhileTyping")
51#define PROP_TOUCHPAD_DISABLE_WITH_MOUSE QStringLiteral("TouchpadDisableWithMouse")
52#define PROP_TOUCHPAD_DOUBLE_CLICK_SPEED QStringLiteral("TouchpadDoubleClickSpeed")
53#define PROP_TOUCHPAD_PRIMARY_BUTTON QStringLiteral("TouchpadPrimaryButton")
54#define PROP_TOUCHPAD_SCROLL_SPEED QStringLiteral("TouchpadScrollSpeed")
55#define PROP_TOUCHPAD_TAP_TO_CLICK QStringLiteral("TouchpadTapToClick")
56#define PROP_TOUCHPAD_TWO_FINGER_SCROLL QStringLiteral("TouchpadTwoFingerScroll")
58using StringMap = QMap<QString,QString>;
59using StringMapList = QList<StringMap>;
60Q_DECLARE_METATYPE(StringMapList)
63QVariant primaryButtonConverter(const QVariant &value)
65 QString stringValue = value.toString();
66 if (stringValue == "left") {
67 return QVariant::fromValue(0);
68 } else if (stringValue == "right") {
69 return QVariant::fromValue(1); // Mir is less clear on this -- any non-zero value is the same
71 return QVariant::fromValue(0); // default to left
75AccountsService::AccountsService(QObject* parent, const QString &user)
77 , m_service(new AccountsServiceDBusAdaptor(this))
79 m_unityInput = new QDBusInterface(QStringLiteral("com.canonical.Unity.Input"),
80 QStringLiteral("/com/canonical/Unity/Input"),
81 QStringLiteral("com.canonical.Unity.Input"),
82 QDBusConnection::SM_BUSNAME(), this);
84 connect(m_service, &AccountsServiceDBusAdaptor::propertiesChanged, this, &AccountsService::onPropertiesChanged);
85 connect(m_service, &AccountsServiceDBusAdaptor::maybeChanged, this, &AccountsService::onMaybeChanged);
87 registerProperty(IFACE_ACCOUNTS_USER, PROP_BACKGROUND_FILE, QStringLiteral("backgroundFileChanged"));
88 registerProperty(IFACE_ACCOUNTS_USER, PROP_EMAIL, QStringLiteral("emailChanged"));
89 registerProperty(IFACE_ACCOUNTS_USER, PROP_REAL_NAME, QStringLiteral("realNameChanged"));
90 registerProperty(IFACE_ACCOUNTS_USER, PROP_INPUT_SOURCES, QStringLiteral("keymapsChanged"));
91 registerProperty(IFACE_LOCATION_HERE, PROP_LICENSE_ACCEPTED, QStringLiteral("hereEnabledChanged"));
92 registerProperty(IFACE_LOCATION_HERE, PROP_LICENSE_BASE_PATH, QStringLiteral("hereLicensePathChanged"));
93 registerProperty(IFACE_UBUNTU_SECURITY, PROP_ENABLE_LAUNCHER_WHILE_LOCKED, QStringLiteral("enableLauncherWhileLockedChanged"));
94 registerProperty(IFACE_UBUNTU_SECURITY, PROP_ENABLE_INDICATORS_WHILE_LOCKED, QStringLiteral("enableIndicatorsWhileLockedChanged"));
95 registerProperty(IFACE_UBUNTU_SECURITY, PROP_PASSWORD_DISPLAY_HINT, QStringLiteral("passwordDisplayHintChanged"));
96 registerProperty(IFACE_UBUNTU_SECURITY_OLD, PROP_STATS_WELCOME_SCREEN, QStringLiteral("statsWelcomeScreenChanged"));
97 registerProperty(IFACE_UNITY, PROP_DEMO_EDGES, QStringLiteral("demoEdgesChanged"));
98 registerProperty(IFACE_UNITY_PRIVATE, PROP_FAILED_LOGINS, QStringLiteral("failedLoginsChanged"));
100 registerProxy(IFACE_UBUNTU_INPUT, PROP_MOUSE_CURSOR_SPEED,
101 m_unityInput, QStringLiteral("setMouseCursorSpeed"));
102 registerProxy(IFACE_UBUNTU_INPUT, PROP_MOUSE_DOUBLE_CLICK_SPEED,
103 m_unityInput, QStringLiteral("setMouseDoubleClickSpeed"));
104 registerProxy(IFACE_UBUNTU_INPUT, PROP_MOUSE_PRIMARY_BUTTON,
105 m_unityInput, QStringLiteral("setMousePrimaryButton"),
106 primaryButtonConverter);
107 registerProxy(IFACE_UBUNTU_INPUT, PROP_MOUSE_SCROLL_SPEED,
108 m_unityInput, QStringLiteral("setMouseScrollSpeed"));
109 registerProxy(IFACE_UBUNTU_INPUT, PROP_TOUCHPAD_CURSOR_SPEED,
110 m_unityInput, QStringLiteral("setTouchpadCursorSpeed"));
111 registerProxy(IFACE_UBUNTU_INPUT, PROP_TOUCHPAD_SCROLL_SPEED,
112 m_unityInput, QStringLiteral("setTouchpadScrollSpeed"));
113 registerProxy(IFACE_UBUNTU_INPUT, PROP_TOUCHPAD_DISABLE_WHILE_TYPING,
114 m_unityInput, QStringLiteral("setTouchpadDisableWhileTyping"));
115 registerProxy(IFACE_UBUNTU_INPUT, PROP_TOUCHPAD_DISABLE_WITH_MOUSE,
116 m_unityInput, QStringLiteral("setTouchpadDisableWithMouse"));
117 registerProxy(IFACE_UBUNTU_INPUT, PROP_TOUCHPAD_DOUBLE_CLICK_SPEED,
118 m_unityInput, QStringLiteral("setTouchpadDoubleClickSpeed"));
119 registerProxy(IFACE_UBUNTU_INPUT, PROP_TOUCHPAD_PRIMARY_BUTTON,
120 m_unityInput, QStringLiteral("setTouchpadPrimaryButton"),
121 primaryButtonConverter);
122 registerProxy(IFACE_UBUNTU_INPUT, PROP_TOUCHPAD_TAP_TO_CLICK,
123 m_unityInput, QStringLiteral("setTouchpadTapToClick"));
124 registerProxy(IFACE_UBUNTU_INPUT, PROP_TOUCHPAD_TWO_FINGER_SCROLL,
125 m_unityInput, QStringLiteral("setTouchpadTwoFingerScroll"));
127 setUser(!user.isEmpty() ? user : QString::fromUtf8(qgetenv("USER")));
130QString AccountsService::user() const
135void AccountsService::setUser(const QString &user)
137 if (user.isEmpty() || m_user == user)
140 bool wasEmpty = m_user.isEmpty();
143 Q_EMIT userChanged();
145 // Do the first update synchronously, as a cheap way to block rendering
146 // until we have the right values on bootup.
150bool AccountsService::demoEdges() const
152 auto value = getProperty(IFACE_UNITY, PROP_DEMO_EDGES);
153 return value.toBool();
156void AccountsService::setDemoEdges(bool demoEdges)
158 setProperty(IFACE_UNITY, PROP_DEMO_EDGES, demoEdges);
161bool AccountsService::enableLauncherWhileLocked() const
163 auto value = getProperty(IFACE_UBUNTU_SECURITY, PROP_ENABLE_LAUNCHER_WHILE_LOCKED);
164 return value.toBool();
167bool AccountsService::enableIndicatorsWhileLocked() const
169 auto value = getProperty(IFACE_UBUNTU_SECURITY, PROP_ENABLE_INDICATORS_WHILE_LOCKED);
170 return value.toBool();
173QString AccountsService::backgroundFile() const
175 auto value = getProperty(IFACE_ACCOUNTS_USER, PROP_BACKGROUND_FILE);
176 return value.toString();
179bool AccountsService::statsWelcomeScreen() const
181 auto value = getProperty(IFACE_UBUNTU_SECURITY_OLD, PROP_STATS_WELCOME_SCREEN);
182 return value.toBool();
185AccountsService::PasswordDisplayHint AccountsService::passwordDisplayHint() const
187 auto value = getProperty(IFACE_UBUNTU_SECURITY, PROP_PASSWORD_DISPLAY_HINT);
188 return (PasswordDisplayHint)value.toInt();
191bool AccountsService::hereEnabled() const
193 auto value = getProperty(IFACE_LOCATION_HERE, PROP_LICENSE_ACCEPTED);
194 return value.toBool();
197void AccountsService::setHereEnabled(bool enabled)
199 setProperty(IFACE_LOCATION_HERE, PROP_LICENSE_ACCEPTED, enabled);
202QString AccountsService::hereLicensePath() const
204 auto value = getProperty(IFACE_LOCATION_HERE, PROP_LICENSE_BASE_PATH);
205 QString hereLicensePath = value.toString();
206 if (hereLicensePath.isEmpty() || !QFile::exists(hereLicensePath))
207 hereLicensePath = QStringLiteral("");
208 return hereLicensePath;
211bool AccountsService::hereLicensePathValid() const
213 auto value = getProperty(IFACE_LOCATION_HERE, PROP_LICENSE_BASE_PATH);
214 return !value.toString().isNull();
217QString AccountsService::realName() const
219 auto value = getProperty(IFACE_ACCOUNTS_USER, PROP_REAL_NAME);
220 return value.toString();
223void AccountsService::setRealName(const QString &realName)
225 setProperty(IFACE_ACCOUNTS_USER, PROP_REAL_NAME, realName);
228QString AccountsService::email() const
230 auto value = getProperty(IFACE_ACCOUNTS_USER, PROP_EMAIL);
231 return value.toString();
234void AccountsService::setEmail(const QString &email)
236 setProperty(IFACE_ACCOUNTS_USER, PROP_EMAIL, email);
239QStringList AccountsService::keymaps() const
241 auto value = getProperty(IFACE_ACCOUNTS_USER, PROP_INPUT_SOURCES);
242 QDBusArgument arg = value.value<QDBusArgument>();
243 StringMapList maps = qdbus_cast<StringMapList>(arg);
244 QStringList simplifiedMaps;
246 Q_FOREACH(const StringMap &map, maps) {
247 Q_FOREACH(const QString &entry, map) {
248 simplifiedMaps.append(entry);
252 if (!simplifiedMaps.isEmpty()) {
253 return simplifiedMaps;
256 return {QStringLiteral("us")};
259uint AccountsService::failedLogins() const
261 return getProperty(IFACE_UNITY_PRIVATE, PROP_FAILED_LOGINS).toUInt();
264void AccountsService::setFailedLogins(uint failedLogins)
266 setProperty(IFACE_UNITY_PRIVATE, PROP_FAILED_LOGINS, failedLogins);
269// ====================================================
270// Everything below this line is generic helper methods
271// ====================================================
273void AccountsService::emitChangedForProperty(const QString &interface, const QString &property)
275 QString signalName = m_properties[interface][property].signal;
276 QMetaObject::invokeMethod(this, signalName.toUtf8().data());
279QVariant AccountsService::getProperty(const QString &interface, const QString &property) const
281 return m_properties[interface][property].value;
284void AccountsService::setProperty(const QString &interface, const QString &property, const QVariant &value)
286 if (m_properties[interface][property].value != value) {
287 m_properties[interface][property].value = value;
288 m_service->setUserPropertyAsync(m_user, interface, property, value);
289 emitChangedForProperty(interface, property);
293void AccountsService::updateCache(const QString &interface, const QString &property, const QVariant &value)
295 PropertyInfo &info = m_properties[interface][property];
297 if (info.proxyInterface) {
299 if (info.proxyConverter) {
300 finalValue = info.proxyConverter(value);
304 info.proxyInterface->asyncCall(info.proxyMethod, finalValue);
305 return; // don't bother saving a copy
308 if (info.value != value) {
310 emitChangedForProperty(interface, property);
314void AccountsService::updateProperty(const QString &interface, const QString &property)
316 QDBusPendingCall pendingReply = m_service->getUserPropertyAsync(m_user,
319 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingReply, this);
321 connect(watcher, &QDBusPendingCallWatcher::finished,
322 this, [this, interface, property](QDBusPendingCallWatcher* watcher) {
324 QDBusPendingReply<QVariant> reply = *watcher;
325 watcher->deleteLater();
326 if (reply.isError()) {
327 qWarning() << "Failed to get '" << property << "' property:" << reply.error().message();
331 updateCache(interface, property, reply.value());
335void AccountsService::updateAllProperties(const QString &interface, bool async)
337 QDBusPendingCall pendingReply = m_service->getAllPropertiesAsync(m_user,
339 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingReply, this);
341 connect(watcher, &QDBusPendingCallWatcher::finished,
342 this, [this, interface](QDBusPendingCallWatcher* watcher) {
344 QDBusPendingReply< QHash<QString, QVariant> > reply = *watcher;
345 watcher->deleteLater();
346 if (reply.isError()) {
347 qWarning() << "Failed to get all properties for" << interface << ":" << reply.error().message();
351 auto valueHash = reply.value();
352 auto i = valueHash.constBegin();
353 while (i != valueHash.constEnd()) {
354 updateCache(interface, i.key(), i.value());
359 watcher->waitForFinished();
363void AccountsService::registerProxy(const QString &interface, const QString &property, QDBusInterface *iface, const QString &method, ProxyConverter converter)
365 registerProperty(interface, property, nullptr);
367 m_properties[interface][property].proxyInterface = iface;
368 m_properties[interface][property].proxyMethod = method;
369 m_properties[interface][property].proxyConverter = converter;
372void AccountsService::registerProperty(const QString &interface, const QString &property, const QString &signal)
374 m_properties[interface][property] = PropertyInfo();
375 m_properties[interface][property].signal = signal;
378void AccountsService::onPropertiesChanged(const QString &user, const QString &interface, const QStringList &changed)
380 if (m_user != user) {
384 auto propHash = m_properties.value(interface);
385 auto i = propHash.constBegin();
386 while (i != propHash.constEnd()) {
387 if (changed.contains(i.key())) {
388 updateProperty(interface, i.key());
394void AccountsService::onMaybeChanged(const QString &user)
396 if (m_user != user) {
400 // Any of the standard properties might have changed!
401 auto propHash = m_properties.value(IFACE_ACCOUNTS_USER);
402 auto i = propHash.constBegin();
403 while (i != propHash.constEnd()) {
404 updateProperty(IFACE_ACCOUNTS_USER, i.key());
409void AccountsService::refresh(bool async)
411 auto i = m_properties.constBegin();
412 while (i != m_properties.constEnd()) {
413 updateAllProperties(i.key(), async);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list