on 48005cf help sign in
unity8/plugins/AccountsService/AccountsService.cpp
538 lines 18 kB history raw
1/*
2 * Copyright (C) 2013, 2015 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 */
17#include "AccountsService.h"
18#include "AccountsServiceDBusAdaptor.h"
20#include <QFile>
21#include <QStringList>
22#include <QDebug>
24AccountsService::AccountsService(QObject* parent, const QString &user)
25 : QObject(parent),
26 m_service(new AccountsServiceDBusAdaptor(this)),
27 m_demoEdges(false),
28 m_enableLauncherWhileLocked(false),
29 m_enableIndicatorsWhileLocked(false),
30 m_statsWelcomeScreen(false),
31 m_passwordDisplayHint(Keyboard),
32 m_failedLogins(0),
33 m_hereEnabled(false),
34 m_hereLicensePath() // null means not set yet
36 connect(m_service, &AccountsServiceDBusAdaptor::propertiesChanged, this, &AccountsService::onPropertiesChanged);
37 connect(m_service, &AccountsServiceDBusAdaptor::maybeChanged, this, &AccountsService::onMaybeChanged);
39 setUser(!user.isEmpty() ? user : QString::fromUtf8(qgetenv("USER")));
42QString AccountsService::user() const
44 return m_user;
47void AccountsService::setUser(const QString &user)
49 if (user.isEmpty() || m_user == user)
50 return;
52 m_user = user;
53 Q_EMIT userChanged();
55 updateDemoEdges(false);
56 updateEnableLauncherWhileLocked(false);
57 updateEnableIndicatorsWhileLocked(false);
58 updateBackgroundFile(false);
59 updateStatsWelcomeScreen(false);
60 updatePasswordDisplayHint(false);
61 updateFailedLogins(false);
62 updateHereEnabled(false);
63 updateHereLicensePath(false);
66bool AccountsService::demoEdges() const
68 return m_demoEdges;
71void AccountsService::setDemoEdges(bool demoEdges)
73 if (m_demoEdges != demoEdges) {
74 m_demoEdges = demoEdges;
75 m_service->setUserProperty(m_user, "com.canonical.unity.AccountsService", "demo-edges", demoEdges);
77 Q_EMIT demoEdgesChanged();
78 }
81bool AccountsService::enableLauncherWhileLocked() const
83 return m_enableLauncherWhileLocked;
86bool AccountsService::enableIndicatorsWhileLocked() const
88 return m_enableIndicatorsWhileLocked;
91QString AccountsService::backgroundFile() const
93 return m_backgroundFile;
96bool AccountsService::statsWelcomeScreen() const
98 return m_statsWelcomeScreen;
101AccountsService::PasswordDisplayHint AccountsService::passwordDisplayHint() const
103 return m_passwordDisplayHint;
106bool AccountsService::hereEnabled() const
108 return m_hereEnabled;
111void AccountsService::setHereEnabled(bool enabled)
113 if (m_hereEnabled != enabled) {
114 m_hereEnabled = enabled;
115 m_service->setUserProperty(m_user, "com.ubuntu.location.providers.here.AccountsService", "LicenseAccepted", enabled);
117 Q_EMIT hereEnabledChanged();
118 }
121QString AccountsService::hereLicensePath() const
123 return m_hereLicensePath;
126bool AccountsService::hereLicensePathValid() const
128 return !m_hereLicensePath.isNull();
131QString AccountsService::realName() const
133 return m_realName;
136void AccountsService::setRealName(const QString &realName)
138 if (realName != m_realName) {
139 m_realName = realName;
140 m_service->setUserProperty(m_user, "org.freedesktop.Accounts.User", "RealName", m_realName);
141 Q_EMIT realNameChanged();
142 }
145QString AccountsService::email() const
147 return m_email;
150void AccountsService::setEmail(const QString &email)
152 if (email != m_email) {
153 m_email = email;
154 m_service->setUserProperty(m_user, "org.freedesktop.Accounts.User", "Email", m_email);
155 Q_EMIT emailChanged();
156 }
159void AccountsService::updateDemoEdges(bool async)
161 QDBusPendingCall pendingReply = m_service->getUserPropertyAsync(m_user,
162 "com.canonical.unity.AccountsService",
163 "demo-edges");
164 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingReply, this);
166 connect(watcher, &QDBusPendingCallWatcher::finished,
167 this, [this](QDBusPendingCallWatcher* watcher) {
169 QDBusPendingReply<QDBusVariant> reply = *watcher;
170 watcher->deleteLater();
171 if (reply.isError()) {
172 qWarning() << "Failed to get 'demo-edges' property - " << reply.error().message();
173 return;
174 }
176 auto demoEdges = reply.value().variant().toBool();
177 if (m_demoEdges != demoEdges) {
178 m_demoEdges = demoEdges;
179 Q_EMIT demoEdgesChanged();
180 }
181 });
182 if (!async) {
183 watcher->waitForFinished();
184 delete watcher;
185 }
188void AccountsService::updateEnableLauncherWhileLocked(bool async)
190 QDBusPendingCall pendingReply = m_service->getUserPropertyAsync(m_user,
191 "com.ubuntu.AccountsService.SecurityPrivacy",
192 "EnableLauncherWhileLocked");
193 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingReply, this);
195 connect(watcher, &QDBusPendingCallWatcher::finished,
196 this, [this](QDBusPendingCallWatcher* watcher) {
198 QDBusPendingReply<QVariant> reply = *watcher;
199 watcher->deleteLater();
200 if (reply.isError()) {
201 qWarning() << "Failed to get 'EnableLauncherWhileLocked' property - " << reply.error().message();
202 return;
203 }
205 const bool enableLauncherWhileLocked = reply.value().toBool();
206 if (m_enableLauncherWhileLocked != enableLauncherWhileLocked) {
207 m_enableLauncherWhileLocked = enableLauncherWhileLocked;
208 Q_EMIT enableLauncherWhileLockedChanged();
209 }
210 });
211 if (!async) {
212 watcher->waitForFinished();
213 delete watcher;
214 }
217void AccountsService::updateEnableIndicatorsWhileLocked(bool async)
219 QDBusPendingCall pendingReply = m_service->getUserPropertyAsync(m_user,
220 "com.ubuntu.AccountsService.SecurityPrivacy",
221 "EnableIndicatorsWhileLocked");
222 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingReply, this);
224 connect(watcher, &QDBusPendingCallWatcher::finished,
225 this, [this](QDBusPendingCallWatcher* watcher) {
227 QDBusPendingReply<QVariant> reply = *watcher;
228 watcher->deleteLater();
229 if (reply.isError()) {
230 qWarning() << "Failed to get 'EnableIndicatorsWhileLocked' property - " << reply.error().message();
231 return;
232 }
234 const bool enableIndicatorsWhileLocked = reply.value().toBool();
235 if (m_enableIndicatorsWhileLocked != enableIndicatorsWhileLocked) {
236 m_enableIndicatorsWhileLocked = enableIndicatorsWhileLocked;
237 Q_EMIT enableIndicatorsWhileLockedChanged();
238 }
239 });
240 if (!async) {
241 watcher->waitForFinished();
242 delete watcher;
243 }
246void AccountsService::updateBackgroundFile(bool async)
248 QDBusPendingCall pendingReply = m_service->getUserPropertyAsync(m_user,
249 "org.freedesktop.Accounts.User",
250 "BackgroundFile");
251 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingReply, this);
253 connect(watcher, &QDBusPendingCallWatcher::finished,
254 this, [this](QDBusPendingCallWatcher* watcher) {
256 QDBusPendingReply<QVariant> reply = *watcher;
257 watcher->deleteLater();
258 if (reply.isError()) {
259 qWarning() << "Failed to get 'BackgroundFile' property - " << reply.error().message();
260 return;
261 }
263 const QString backgroundFile = reply.value().toString();
264 if (m_backgroundFile != backgroundFile) {
265 m_backgroundFile = backgroundFile;
266 Q_EMIT backgroundFileChanged();
267 }
268 });
269 if (!async) {
270 watcher->waitForFinished();
271 delete watcher;
272 }
275void AccountsService::updateStatsWelcomeScreen(bool async)
277 QDBusPendingCall pendingReply = m_service->getUserPropertyAsync(m_user,
278 "com.ubuntu.touch.AccountsService.SecurityPrivacy",
279 "StatsWelcomeScreen");
280 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingReply, this);
282 connect(watcher, &QDBusPendingCallWatcher::finished,
283 this, [this](QDBusPendingCallWatcher* watcher) {
285 QDBusPendingReply<QVariant> reply = *watcher;
286 watcher->deleteLater();
287 if (reply.isError()) {
288 qWarning() << "Failed to get 'StatsWelcomeScreen' property - " << reply.error().message();
289 return;
290 }
292 const bool statsWelcomeScreen = reply.value().toBool();
293 if (m_statsWelcomeScreen != statsWelcomeScreen) {
294 m_statsWelcomeScreen = statsWelcomeScreen;
295 Q_EMIT statsWelcomeScreenChanged();
296 }
297 });
298 if (!async) {
299 watcher->waitForFinished();
300 delete watcher;
301 }
304void AccountsService::updatePasswordDisplayHint(bool async)
306 QDBusPendingCall pendingReply = m_service->getUserPropertyAsync(m_user,
307 "com.ubuntu.AccountsService.SecurityPrivacy",
308 "PasswordDisplayHint");
309 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingReply, this);
311 connect(watcher, &QDBusPendingCallWatcher::finished,
312 this, [this](QDBusPendingCallWatcher* watcher) {
314 QDBusPendingReply<QVariant> reply = *watcher;
315 watcher->deleteLater();
316 if (reply.isError()) {
317 qWarning() << "Failed to get 'PasswordDisplayHint' property - " << reply.error().message();
318 return;
319 }
321 const PasswordDisplayHint passwordDisplayHint = (PasswordDisplayHint)reply.value().toInt();
322 if (m_passwordDisplayHint != passwordDisplayHint) {
323 m_passwordDisplayHint = passwordDisplayHint;
324 Q_EMIT passwordDisplayHintChanged();
325 }
326 });
327 if (!async) {
328 watcher->waitForFinished();
329 delete watcher;
330 }
333void AccountsService::updateFailedLogins(bool async)
335 QDBusPendingCall pendingReply = m_service->getUserPropertyAsync(m_user,
336 "com.canonical.unity.AccountsService.Private",
337 "FailedLogins");
338 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingReply, this);
340 connect(watcher, &QDBusPendingCallWatcher::finished,
341 this, [this](QDBusPendingCallWatcher* watcher) {
343 QDBusPendingReply<QVariant> reply = *watcher;
344 watcher->deleteLater();
345 if (reply.isError()) {
346 qWarning() << "Failed to get 'FailedLogins' property - " << reply.error().message();
347 return;
348 }
350 const uint failedLogins = reply.value().toUInt();
351 if (m_failedLogins != failedLogins) {
352 m_failedLogins = failedLogins;
353 Q_EMIT failedLoginsChanged();
354 }
355 });
356 if (!async) {
357 watcher->waitForFinished();
358 delete watcher;
359 }
362void AccountsService::updateHereEnabled(bool async)
364 QDBusPendingCall pendingReply = m_service->getUserPropertyAsync(m_user,
365 "com.ubuntu.location.providers.here.AccountsService",
366 "LicenseAccepted");
367 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingReply, this);
369 connect(watcher, &QDBusPendingCallWatcher::finished,
370 this, [this](QDBusPendingCallWatcher* watcher) {
372 QDBusPendingReply<QVariant> reply = *watcher;
373 watcher->deleteLater();
374 if (reply.isError()) {
375 qWarning() << "Failed to get 'LicenseAccepted' property - " << reply.error().message();
376 return;
377 }
379 const bool hereEnabled = reply.value().toBool();
380 if (m_hereEnabled != hereEnabled) {
381 m_hereEnabled = hereEnabled;
382 Q_EMIT hereEnabledChanged();
383 }
384 });
385 if (!async) {
386 watcher->waitForFinished();
387 delete watcher;
388 }
391void AccountsService::updateHereLicensePath(bool async)
393 QDBusPendingCall pendingReply = m_service->getUserPropertyAsync(m_user,
394 "com.ubuntu.location.providers.here.AccountsService",
395 "LicenseBasePath");
396 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingReply, this);
398 connect(watcher, &QDBusPendingCallWatcher::finished,
399 this, [this](QDBusPendingCallWatcher* watcher) {
401 QDBusPendingReply<QVariant> reply = *watcher;
402 watcher->deleteLater();
403 if (reply.isError()) {
404 qWarning() << "Failed to get 'LicenseBasePath' property - " << reply.error().message();
405 return;
406 }
408 QString hereLicensePath = reply.value().toString();
409 if (hereLicensePath.isEmpty() || !QFile::exists(hereLicensePath))
410 hereLicensePath = "";
412 if (m_hereLicensePath.isNull() || m_hereLicensePath != hereLicensePath) {
413 m_hereLicensePath = hereLicensePath;
414 Q_EMIT hereLicensePathChanged();
415 }
416 });
417 if (!async) {
418 watcher->waitForFinished();
419 delete watcher;
420 }
423void AccountsService::updateRealName(bool async)
425 QDBusPendingCall pendingReply = m_service->getUserPropertyAsync(m_user, "org.freedesktop.Accounts.User", "RealName");
426 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingReply, this);
428 connect(watcher, &QDBusPendingCallWatcher::finished,
429 this, [this](QDBusPendingCallWatcher* watcher) {
430 QDBusPendingReply<QVariant> reply = *watcher;
431 watcher->deleteLater();
432 if (reply.isError()) {
433 qWarning() << "Failed to get 'RealName' property - " << reply.error().message();
434 return;
435 }
437 const QString realName = reply.value().toString();
438 if (m_realName != realName) {
439 m_realName = realName;
440 Q_EMIT realNameChanged();
441 }
442 });
443 if (!async) {
444 watcher->waitForFinished();
445 delete watcher;
446 }
449void AccountsService::updateEmail(bool async)
451 QDBusPendingCall pendingReply = m_service->getUserPropertyAsync(m_user, "org.freedesktop.Accounts.User", "Email");
452 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingReply, this);
454 connect(watcher, &QDBusPendingCallWatcher::finished,
455 this, [this](QDBusPendingCallWatcher* watcher) {
456 QDBusPendingReply<QVariant> reply = *watcher;
457 watcher->deleteLater();
458 if (reply.isError()) {
459 qWarning() << "Failed to get 'Email' property - " << reply.error().message();
460 return;
461 }
463 const QString email = reply.value().toString();
464 if (m_email != email) {
465 m_email = email;
466 Q_EMIT emailChanged();
467 }
468 });
469 if (!async) {
470 watcher->waitForFinished();
471 delete watcher;
472 }
475uint AccountsService::failedLogins() const
477 return m_failedLogins;
480void AccountsService::setFailedLogins(uint failedLogins)
482 if (m_failedLogins != failedLogins) {
483 m_failedLogins = failedLogins;
484 m_service->setUserProperty(m_user, "com.canonical.unity.AccountsService.Private", "FailedLogins", failedLogins);
486 Q_EMIT failedLoginsChanged();
487 }
490void AccountsService::onPropertiesChanged(const QString &user, const QString &interface, const QStringList &changed)
492 if (m_user != user) {
493 return;
494 }
496 if (interface == "com.canonical.unity.AccountsService") {
497 if (changed.contains("demo-edges")) {
498 updateDemoEdges();
499 }
500 } else if (interface == "com.canonical.unity.AccountsService.Private") {
501 if (changed.contains("FailedLogins")) {
502 updateFailedLogins();
503 }
504 } else if (interface == "com.ubuntu.touch.AccountsService.SecurityPrivacy") {
505 if (changed.contains("StatsWelcomeScreen")) {
506 updateStatsWelcomeScreen();
507 }
508 } else if (interface == "com.ubuntu.AccountsService.SecurityPrivacy") {
509 if (changed.contains("PasswordDisplayHint")) {
510 updatePasswordDisplayHint();
511 }
512 if (changed.contains("EnableLauncherWhileLocked")) {
513 updateEnableLauncherWhileLocked();
514 }
515 if (changed.contains("EnableIndicatorsWhileLocked")) {
516 updateEnableIndicatorsWhileLocked();
517 }
518 } else if (interface == "com.ubuntu.location.providers.here.AccountsService") {
519 if (changed.contains("LicenseAccepted")) {
520 updateHereEnabled();
521 }
522 if (changed.contains("LicenseBasePath")) {
523 updateHereLicensePath();
524 }
525 }
528void AccountsService::onMaybeChanged(const QString &user)
530 if (m_user != user) {
531 return;
532 }
534 // Standard properties might have changed
535 updateBackgroundFile();
536 updateRealName();
537 updateEmail();