Unity 8
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 <QFile>
23 #include <QStringList>
24 
25 AccountsService::AccountsService(QObject* parent)
26  : QObject(parent),
27  m_service(new AccountsServiceDBusAdaptor(this)),
28  m_user(""),
29  m_demoEdges(false),
30  m_enableLauncherWhileLocked(false),
31  m_enableIndicatorsWhileLocked(false),
32  m_statsWelcomeScreen(false),
33  m_passwordDisplayHint(Keyboard),
34  m_failedLogins(0),
35  m_hereEnabled(false),
36  m_hereLicensePath() // null means not set yet
37 {
38  connect(m_service, SIGNAL(propertiesChanged(const QString &, const QString &, const QStringList &)),
39  this, SLOT(propertiesChanged(const QString &, const QString &, const QStringList &)));
40  connect(m_service, SIGNAL(maybeChanged(const QString &)),
41  this, SLOT(maybeChanged(const QString &)));
42 
43  setUser(qgetenv("USER"));
44 }
45 
46 QString AccountsService::user() const
47 {
48  return m_user;
49 }
50 
51 void AccountsService::setUser(const QString &user)
52 {
53  if (user.isEmpty() || m_user == user)
54  return;
55 
56  m_user = user;
57  Q_EMIT userChanged();
58 
59  updateDemoEdges();
60  updateEnableLauncherWhileLocked();
61  updateEnableIndicatorsWhileLocked();
62  updateBackgroundFile();
63  updateStatsWelcomeScreen();
64  updatePasswordDisplayHint();
65  updateFailedLogins();
66  updateHereEnabled();
67  updateHereLicensePath();
68 }
69 
70 bool AccountsService::demoEdges() const
71 {
72  return m_demoEdges;
73 }
74 
75 void AccountsService::setDemoEdges(bool demoEdges)
76 {
77  m_demoEdges = demoEdges;
78  m_service->setUserProperty(m_user, "com.canonical.unity.AccountsService", "demo-edges", demoEdges);
79 }
80 
81 bool AccountsService::enableLauncherWhileLocked() const
82 {
83  return m_enableLauncherWhileLocked;
84 }
85 
86 bool AccountsService::enableIndicatorsWhileLocked() const
87 {
88  return m_enableIndicatorsWhileLocked;
89 }
90 
91 QString AccountsService::backgroundFile() const
92 {
93  return m_backgroundFile;
94 }
95 
96 bool AccountsService::statsWelcomeScreen() const
97 {
98  return m_statsWelcomeScreen;
99 }
100 
101 AccountsService::PasswordDisplayHint AccountsService::passwordDisplayHint() const
102 {
103  return m_passwordDisplayHint;
104 }
105 
106 bool AccountsService::hereEnabled() const
107 {
108  return m_hereEnabled;
109 }
110 
111 void AccountsService::setHereEnabled(bool enabled)
112 {
113  m_service->setUserProperty(m_user, "com.ubuntu.location.providers.here.AccountsService", "LicenseAccepted", enabled);
114 }
115 
116 QString AccountsService::hereLicensePath() const
117 {
118  return m_hereLicensePath;
119 }
120 
121 bool AccountsService::hereLicensePathValid() const
122 {
123  return !m_hereLicensePath.isNull();
124 }
125 
126 void AccountsService::updateDemoEdges()
127 {
128  auto demoEdges = m_service->getUserProperty(m_user, "com.canonical.unity.AccountsService", "demo-edges").toBool();
129  if (m_demoEdges != demoEdges) {
130  m_demoEdges = demoEdges;
131  Q_EMIT demoEdgesChanged();
132  }
133 }
134 
135 void AccountsService::updateEnableLauncherWhileLocked()
136 {
137  auto enableLauncherWhileLocked = m_service->getUserProperty(m_user, "com.ubuntu.AccountsService.SecurityPrivacy", "EnableLauncherWhileLocked").toBool();
138  if (m_enableLauncherWhileLocked != enableLauncherWhileLocked) {
139  m_enableLauncherWhileLocked = enableLauncherWhileLocked;
140  Q_EMIT enableLauncherWhileLockedChanged();
141  }
142 }
143 
144 void AccountsService::updateEnableIndicatorsWhileLocked()
145 {
146  auto enableIndicatorsWhileLocked = m_service->getUserProperty(m_user, "com.ubuntu.AccountsService.SecurityPrivacy", "EnableIndicatorsWhileLocked").toBool();
147  if (m_enableIndicatorsWhileLocked != enableIndicatorsWhileLocked) {
148  m_enableIndicatorsWhileLocked = enableIndicatorsWhileLocked;
149  Q_EMIT enableIndicatorsWhileLockedChanged();
150  }
151 }
152 
153 void AccountsService::updateBackgroundFile()
154 {
155  auto backgroundFile = m_service->getUserProperty(m_user, "org.freedesktop.Accounts.User", "BackgroundFile").toString();
156  if (m_backgroundFile != backgroundFile) {
157  m_backgroundFile = backgroundFile;
158  Q_EMIT backgroundFileChanged();
159  }
160 }
161 
162 void AccountsService::updateStatsWelcomeScreen()
163 {
164  bool statsWelcomeScreen = m_service->getUserProperty(m_user, "com.ubuntu.touch.AccountsService.SecurityPrivacy", "StatsWelcomeScreen").toBool();
165  if (m_statsWelcomeScreen != statsWelcomeScreen) {
166  m_statsWelcomeScreen = statsWelcomeScreen;
167  Q_EMIT statsWelcomeScreenChanged();
168  }
169 }
170 
171 void AccountsService::updatePasswordDisplayHint()
172 {
173  PasswordDisplayHint passwordDisplayHint = (PasswordDisplayHint)m_service->getUserProperty(m_user, "com.ubuntu.AccountsService.SecurityPrivacy", "PasswordDisplayHint").toInt();
174  if (m_passwordDisplayHint != passwordDisplayHint) {
175  m_passwordDisplayHint = passwordDisplayHint;
176  Q_EMIT passwordDisplayHintChanged();
177  }
178 }
179 
180 void AccountsService::updateFailedLogins()
181 {
182  uint failedLogins = m_service->getUserProperty(m_user, "com.canonical.unity.AccountsService.Private", "FailedLogins").toUInt();
183  if (m_failedLogins != failedLogins) {
184  m_failedLogins = failedLogins;
185  Q_EMIT failedLoginsChanged();
186  }
187 }
188 
189 void AccountsService::updateHereEnabled()
190 {
191  bool hereEnabled = m_service->getUserProperty(m_user, "com.ubuntu.location.providers.here.AccountsService", "LicenseAccepted").toBool();
192  if (m_hereEnabled != hereEnabled) {
193  m_hereEnabled = hereEnabled;
194  Q_EMIT hereEnabledChanged();
195  }
196 }
197 
198 void AccountsService::updateHereLicensePath()
199 {
200  QString hereLicensePath = m_service->getUserProperty(m_user, "com.ubuntu.location.providers.here.AccountsService", "LicenseBasePath").toString();
201 
202  if (hereLicensePath.isEmpty() || !QFile::exists(hereLicensePath))
203  hereLicensePath = "";
204 
205  if (m_hereLicensePath.isNull() || m_hereLicensePath != hereLicensePath) {
206  m_hereLicensePath = hereLicensePath;
207  Q_EMIT hereLicensePathChanged();
208  }
209 }
210 
211 uint AccountsService::failedLogins() const
212 {
213  return m_failedLogins;
214 }
215 
216 void AccountsService::setFailedLogins(uint failedLogins)
217 {
218  m_failedLogins = failedLogins;
219  m_service->setUserProperty(m_user, "com.canonical.unity.AccountsService.Private", "FailedLogins", failedLogins);
220 }
221 
222 void AccountsService::propertiesChanged(const QString &user, const QString &interface, const QStringList &changed)
223 {
224  if (m_user != user) {
225  return;
226  }
227 
228  if (interface == "com.canonical.unity.AccountsService") {
229  if (changed.contains("demo-edges")) {
230  updateDemoEdges();
231  }
232  } else if (interface == "com.canonical.unity.AccountsService.Private") {
233  if (changed.contains("FailedLogins")) {
234  updateFailedLogins();
235  }
236  } else if (interface == "com.ubuntu.touch.AccountsService.SecurityPrivacy") {
237  if (changed.contains("StatsWelcomeScreen")) {
238  updateStatsWelcomeScreen();
239  }
240  } else if (interface == "com.ubuntu.AccountsService.SecurityPrivacy") {
241  if (changed.contains("PasswordDisplayHint")) {
242  updatePasswordDisplayHint();
243  }
244  if (changed.contains("EnableLauncherWhileLocked")) {
245  updateEnableLauncherWhileLocked();
246  }
247  if (changed.contains("EnableIndicatorsWhileLocked")) {
248  updateEnableIndicatorsWhileLocked();
249  }
250  } else if (interface == "com.ubuntu.location.providers.here.AccountsService") {
251  if (changed.contains("LicenseAccepted")) {
252  updateHereEnabled();
253  }
254  if (changed.contains("LicenseBasePath")) {
255  updateHereLicensePath();
256  }
257  }
258 }
259 
260 void AccountsService::maybeChanged(const QString &user)
261 {
262  if (m_user != user) {
263  return;
264  }
265 
266  // Standard properties might have changed
267  updateBackgroundFile();
268 }