Unity 8
 All Classes Functions
AccountsService.cpp
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 "AccountsServiceDBusAdaptor.h"
21 
22 #include <QStringList>
23 
24 AccountsService::AccountsService(QObject* parent)
25  : QObject(parent),
26  m_service(new AccountsServiceDBusAdaptor(this)),
27  m_user(qgetenv("USER")),
28  m_demoEdges(false),
29  m_enableLauncherWhileLocked(false),
30  m_enableIndicatorsWhileLocked(false),
31  m_statsWelcomeScreen(false),
32  m_passwordDisplayHint(Keyboard),
33  m_failedLogins(0)
34 {
35  connect(m_service, SIGNAL(propertiesChanged(const QString &, const QString &, const QStringList &)),
36  this, SLOT(propertiesChanged(const QString &, const QString &, const QStringList &)));
37  connect(m_service, SIGNAL(maybeChanged(const QString &)),
38  this, SLOT(maybeChanged(const QString &)));
39 }
40 
41 QString AccountsService::user() const
42 {
43  return m_user;
44 }
45 
46 void AccountsService::setUser(const QString &user)
47 {
48  m_user = user;
49  Q_EMIT userChanged();
50 
51  updateDemoEdges();
52  updateEnableLauncherWhileLocked();
53  updateEnableIndicatorsWhileLocked();
54  updateBackgroundFile();
55  updateStatsWelcomeScreen();
56  updatePasswordDisplayHint();
57  updateFailedLogins();
58 }
59 
60 bool AccountsService::demoEdges() const
61 {
62  return m_demoEdges;
63 }
64 
65 void AccountsService::setDemoEdges(bool demoEdges)
66 {
67  m_demoEdges = demoEdges;
68  m_service->setUserProperty(m_user, "com.canonical.unity.AccountsService", "demo-edges", demoEdges);
69 }
70 
71 bool AccountsService::enableLauncherWhileLocked() const
72 {
73  return m_enableLauncherWhileLocked;
74 }
75 
76 bool AccountsService::enableIndicatorsWhileLocked() const
77 {
78  return m_enableIndicatorsWhileLocked;
79 }
80 
81 QString AccountsService::backgroundFile() const
82 {
83  return m_backgroundFile;
84 }
85 
86 bool AccountsService::statsWelcomeScreen() const
87 {
88  return m_statsWelcomeScreen;
89 }
90 
91 AccountsService::PasswordDisplayHint AccountsService::passwordDisplayHint() const
92 {
93  return m_passwordDisplayHint;
94 }
95 
96 void AccountsService::updateDemoEdges()
97 {
98  auto demoEdges = m_service->getUserProperty(m_user, "com.canonical.unity.AccountsService", "demo-edges").toBool();
99  if (m_demoEdges != demoEdges) {
100  m_demoEdges = demoEdges;
101  Q_EMIT demoEdgesChanged();
102  }
103 }
104 
105 void AccountsService::updateEnableLauncherWhileLocked()
106 {
107  auto enableLauncherWhileLocked = m_service->getUserProperty(m_user, "com.ubuntu.AccountsService.SecurityPrivacy", "EnableLauncherWhileLocked").toBool();
108  if (m_enableLauncherWhileLocked != enableLauncherWhileLocked) {
109  m_enableLauncherWhileLocked = enableLauncherWhileLocked;
110  Q_EMIT enableLauncherWhileLockedChanged();
111  }
112 }
113 
114 void AccountsService::updateEnableIndicatorsWhileLocked()
115 {
116  auto enableIndicatorsWhileLocked = m_service->getUserProperty(m_user, "com.ubuntu.AccountsService.SecurityPrivacy", "EnableIndicatorsWhileLocked").toBool();
117  if (m_enableIndicatorsWhileLocked != enableIndicatorsWhileLocked) {
118  m_enableIndicatorsWhileLocked = enableIndicatorsWhileLocked;
119  Q_EMIT enableIndicatorsWhileLockedChanged();
120  }
121 }
122 
123 void AccountsService::updateBackgroundFile()
124 {
125  auto backgroundFile = m_service->getUserProperty(m_user, "org.freedesktop.Accounts.User", "BackgroundFile").toString();
126  if (m_backgroundFile != backgroundFile) {
127  m_backgroundFile = backgroundFile;
128  Q_EMIT backgroundFileChanged();
129  }
130 }
131 
132 void AccountsService::updateStatsWelcomeScreen()
133 {
134  bool statsWelcomeScreen = m_service->getUserProperty(m_user, "com.ubuntu.touch.AccountsService.SecurityPrivacy", "StatsWelcomeScreen").toBool();
135  if (m_statsWelcomeScreen != statsWelcomeScreen) {
136  m_statsWelcomeScreen = statsWelcomeScreen;
137  Q_EMIT statsWelcomeScreenChanged();
138  }
139 }
140 
141 void AccountsService::updatePasswordDisplayHint()
142 {
143  PasswordDisplayHint passwordDisplayHint = (PasswordDisplayHint)m_service->getUserProperty(m_user, "com.ubuntu.AccountsService.SecurityPrivacy", "PasswordDisplayHint").toInt();
144  if (m_passwordDisplayHint != passwordDisplayHint) {
145  m_passwordDisplayHint = passwordDisplayHint;
146  Q_EMIT passwordDisplayHintChanged();
147  }
148 }
149 
150 void AccountsService::updateFailedLogins()
151 {
152  uint failedLogins = m_service->getUserProperty(m_user, "com.canonical.unity.AccountsService.Private", "FailedLogins").toUInt();
153  if (m_failedLogins != failedLogins) {
154  m_failedLogins = failedLogins;
155  Q_EMIT failedLoginsChanged();
156  }
157 }
158 
159 uint AccountsService::failedLogins() const
160 {
161  return m_failedLogins;
162 }
163 
164 void AccountsService::setFailedLogins(uint failedLogins)
165 {
166  m_failedLogins = failedLogins;
167  m_service->setUserProperty(m_user, "com.canonical.unity.AccountsService.Private", "FailedLogins", failedLogins);
168 }
169 
170 void AccountsService::propertiesChanged(const QString &user, const QString &interface, const QStringList &changed)
171 {
172  if (m_user != user) {
173  return;
174  }
175 
176  if (interface == "com.canonical.unity.AccountsService") {
177  if (changed.contains("demo-edges")) {
178  updateDemoEdges();
179  }
180  } else if (interface == "com.canonical.unity.AccountsService.Private") {
181  if (changed.contains("FailedLogins")) {
182  updateFailedLogins();
183  }
184  } else if (interface == "com.ubuntu.touch.AccountsService.SecurityPrivacy") {
185  if (changed.contains("StatsWelcomeScreen")) {
186  updateStatsWelcomeScreen();
187  }
188  } else if (interface == "com.ubuntu.AccountsService.SecurityPrivacy") {
189  if (changed.contains("PasswordDisplayHint")) {
190  updatePasswordDisplayHint();
191  }
192  if (changed.contains("EnableLauncherWhileLocked")) {
193  updateEnableLauncherWhileLocked();
194  }
195  if (changed.contains("EnableIndicatorsWhileLocked")) {
196  updateEnableIndicatorsWhileLocked();
197  }
198  }
199 }
200 
201 void AccountsService::maybeChanged(const QString &user)
202 {
203  if (m_user != user) {
204  return;
205  }
206 
207  // Standard properties might have changed
208  updateBackgroundFile();
209 }