/
devicelock.cpp
279 lines (223 loc) · 7.71 KB
/
devicelock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Copyright (C) 2016 Jolla Ltd
* Contact: Andrew den Exter <andrew.den.exter@jolla.com>
*
* You may use this file under the terms of the BSD license as follows:
*
* "Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Nemo Mobile nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
*/
#include "devicelock.h"
#include "settingswatcher.h"
namespace NemoDeviceLock
{
/*!
\class NemoDeviceLock::DeviceLock
\brief The DeviceLock class provides an interface to the security daemons device lock state.
Through this interface the current state of the device lock can be queried, and a request to
release the device lock may be made. Unlocking requires user authentication which the
security daemon will independently request through the currently registered device lock
AuthenticationInput provider.
*/
/*!
\enum NemoDeviceLock::DeviceLock::Notice
Broadcast notifications decribing events of importance.
\value SecurityCodeDueToExpire The user's security code is due to due to expire in the near
future. The expiration date is provided as an ISO 8601 formatted string in the
\c expirationDate member of the notice data.
\value SecurityCodeChanged The user's security code has been changed.
*/
/*!
Constructs a device lock interface instance which is a child of \a parent.
*/
DeviceLock::DeviceLock(QObject *parent)
: QObject(parent)
, ConnectionClient(
this, QStringLiteral("/devicelock/lock"), QStringLiteral("org.nemomobile.devicelock.DeviceLock"))
, m_settings(SettingsWatcher::instance())
, m_state(Undefined)
, m_enabled(true)
, m_unlocking(false)
{
connect(m_settings.data(), &SettingsWatcher::automaticLockingChanged,
this, &DeviceLock::automaticLockingChanged);
connect(this, &DeviceLock::enabledChanged,
this, &DeviceLock::automaticLockingChanged);
connect(m_settings.data(), &SettingsWatcher::showNotificationsChanged,
this, &DeviceLock::showNotificationsChanged);
connect(this, &DeviceLock::stateChanged,
this, &DeviceLock::showNotificationsChanged);
m_connection->onConnected(this, [this] {
connected();
});
m_connection->onDisconnected(this, [this] {
const bool wasUnlocked = m_state == Unlocked;
m_state = Undefined;
if (m_unlocking) {
m_unlocking = false;
emit unlockingChanged();
}
emit stateChanged();
if (wasUnlocked) {
emit locked();
}
});
if (m_connection->isConnected()) {
connected();
}
}
/*!
Destroys a device lock interface instance.
*/
DeviceLock::~DeviceLock()
{
}
/*!
\property NemoDeviceLock::DeviceLock::automaticLocking
This property holds the number of minutes of inactivity required before the device will
automatically lock.
*/
int DeviceLock::automaticLocking() const
{
return isEnabled() ? m_settings->automaticLocking : -1;
}
/*!
\property NemoDeviceLock::DeviceLock::showNotifications
This property holds whether the notifications should be shown over the lockscreen.
*/
bool DeviceLock::showNotifications() const
{
switch (m_state) {
case Unlocked:
return true;
case Locked:
return m_settings->showNotifications > 0;
default:
return false;
}
}
/*!
\property NemoDeviceLock::DeviceLock::enabled
This property holds whether device lock is enabled.
*/
bool DeviceLock::isEnabled() const
{
return m_enabled;
}
/*!
\property NemoDeviceLock::DeviceLock::unlocking
This property holds whether the user is currently being prompted for authentication to unlock
the device.
*/
bool DeviceLock::isUnlocking() const
{
return m_unlocking;
}
/*!
\property NemoDeviceLock::DeviceLock::state
This property holds the current state of the device lock.
*/
DeviceLock::LockState DeviceLock::state() const
{
return m_state;
}
/*!
Requests user authentication to unlock the device.
*/
void DeviceLock::unlock()
{
if (!m_unlocking && m_state >= Locked && m_state < Undefined) {
m_unlocking = true;
const auto response = call(QStringLiteral("Unlock"));
response->onError([this](const QDBusError &) {
m_unlocking = false;
emit unlockingChanged();
});
emit unlockingChanged();
}
}
/*!
Cancels a request for user authentication to unlock the device.
*/
void DeviceLock::cancel()
{
if (m_unlocking) {
m_unlocking = false;
call(QStringLiteral("Cancel"));
emit unlockingChanged();
}
}
/*!
\signal NemoDeviceLock::DeviceLock::locked()
Signals that the device has been locked.
*/
/*!
\signal NemoDeviceLock::DeviceLock::unlocked()
Signals that the device has been unlocked.
*/
/*!
\signal NemoDeviceLock::DeviceLock::unlockError()
Signals that there was an error requesting authentication to unlock the device.
*/
/*!
\signal NemoDeviceLock::DeviceLock::notice(Notice notice, const QVariantMap &map)
Emits a broadcast \a notice from the device lock. Some noteices will also
include \a data that should be incorporated into the message, the members of data
accompanying a notice will be described in the documentation for that notice.
*/
void DeviceLock::handleNotice(uint notice, const QVariantMap &data)
{
emit DeviceLock::notice(DeviceLock::Notice(notice), data);
}
void DeviceLock::connected()
{
registerObject();
connectToSignal(QStringLiteral("Notice"), SLOT(handleNotice(uint,QVariantMap)));
subscribeToProperty<bool>(QStringLiteral("Enabled"), [this](bool enabled) {
if (m_enabled != enabled) {
m_enabled = enabled;
emit enabledChanged();
}
});
subscribeToProperty<bool>(QStringLiteral("Unlocking"), [this](bool unlocking) {
if (m_unlocking != unlocking) {
m_unlocking = unlocking;
emit unlockingChanged();
}
});
subscribeToProperty<uint>(QStringLiteral("State"), [this](uint state) {
if (m_state != state) {
bool wasLocked = m_state >= Locked;
m_state = LockState(state);
emit stateChanged();
if (!wasLocked && m_state >= Locked && m_state < Undefined) {
emit locked();
} else if (wasLocked && m_state == Unlocked) {
emit unlocked();
}
}
});
}
}