-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathauth_face.cpp
More file actions
250 lines (232 loc) · 7.06 KB
/
auth_face.cpp
File metadata and controls
250 lines (232 loc) · 7.06 KB
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
// SPDX-FileCopyrightText: 2021 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "auth_face.h"
#include <QKeyEvent>
#include <QTimer>
#include <QVBoxLayout>
AuthFace::AuthFace(QWidget *parent)
: AuthModule(AuthCommon::AT_Face, parent)
, m_aniIndex(-1)
, m_textLabel(new DLabel(this))
, m_filterTimer(new QTimer(this))
{
setObjectName(QStringLiteral("AuthFace"));
setAccessibleName(QStringLiteral("AuthFace"));
initUI();
initConnections();
}
/**
* @brief 初始化界面
*/
void AuthFace::initUI()
{
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->setContentsMargins(27, 0, 10, 0);
mainLayout->setSpacing(0);
/* 文案提示 */
m_textLabel->setText(tr("Face ID"));
m_textLabel->setWordWrap(true);
m_textLabel->setAlignment(Qt::AlignmentFlag::AlignCenter);
mainLayout->addWidget(m_textLabel, 1);
/* 认证状态 */
m_authStateLabel = new DLabel(this);
m_authStateLabel->installEventFilter(this);
setAuthStateStyle(LOGIN_WAIT);
mainLayout->addWidget(m_authStateLabel, 0, Qt::AlignRight | Qt::AlignVCenter);
// 多数摄像头模组前2s内会有由暗变亮的过程,特别是重新上电的第一次,例如待机/休眠/重启等场景,此时获取的人脸图像会报错,此处增加一个定时器,过滤前2s的错误
m_filterTimer->setInterval(2000);
m_filterTimer->setSingleShot(true);
}
/**
* @brief 初始化信号连接
*/
void AuthFace::initConnections()
{
AuthModule::initConnections();
}
/**
* @brief AuthFace::reset
*/
void AuthFace::reset()
{
m_state = AuthCommon::AS_Ended;
m_textLabel->setText(tr("Face ID"));
if (m_authStateLabel) {
setAuthStateStyle(isMFA() ? LOGIN_WAIT : AUTH_LOCK);
m_authStateLabel->show();
}
}
/**
* @brief 设置认证状态
*
* @param state
* @param result
*/
void AuthFace::setAuthState(const AuthCommon::AuthState state, const QString &result)
{
static bool startFilterTimer = false;
m_state = state;
switch (state) {
case AuthCommon::AS_Success:
if (isMFA())
setAuthStateStyle(LOGIN_CHECK);
else
setAnimationState(true);
m_textLabel->setText(tr("Verification successful"));
m_showPrompt = true;
emit authFinished(state);
emit retryButtonVisibleChanged(false);
startFilterTimer = false;
break;
case AuthCommon::AS_Failure: {
setAnimationState(false);
setAuthStateStyle(isMFA() ? LOGIN_RETRY : AUTH_LOCK);
m_showPrompt = false;
const int leftTimes = static_cast<int>(m_limitsInfo->maxTries - m_limitsInfo->numFailures);
if (leftTimes > 1) {
m_textLabel->setText(tr("Verification failed, %n chances left", "", leftTimes));
} else if (leftTimes == 1) {
m_textLabel->setText(tr("Verification failed, only one chance left"));
}
emit retryButtonVisibleChanged(true);
emit authFinished(state);
startFilterTimer = false;
break;
}
case AuthCommon::AS_Cancel:
setAnimationState(false);
m_showPrompt = true;
break;
case AuthCommon::AS_Timeout:
case AuthCommon::AS_Error:
setAnimationState(false);
setAuthStateStyle(isMFA() ? LOGIN_WAIT : AUTH_LOCK);
m_textLabel->setText(result);
m_showPrompt = true;
break;
case AuthCommon::AS_Verify:
if (!startFilterTimer) {
m_filterTimer->start();
startFilterTimer = true;
}
setAnimationState(false);
setAuthStateStyle(isMFA() ? LOGIN_SPINNER : AUTH_LOCK);
if (!m_filterTimer->isActive()) {
m_textLabel->setText(result);
}
break;
case AuthCommon::AS_Exception:
setAnimationState(false);
setAuthStateStyle(isMFA() ? LOGIN_WAIT : AUTH_LOCK);
m_textLabel->setText(tr("Device unavailable!"));
m_showPrompt = true;
break;
case AuthCommon::AS_Prompt:
setAnimationState(false);
setAuthStateStyle(isMFA() ? LOGIN_WAIT : AUTH_LOCK);
break;
case AuthCommon::AS_Started:
startFilterTimer = false;
m_textLabel->setText(tr("Verify your Face ID"));
break;
case AuthCommon::AS_Ended:
break;
case AuthCommon::AS_Locked:
setAnimationState(false);
setAuthStateStyle(isMFA() ? LOGIN_LOCK : AUTH_LOCK);
m_textLabel->setText(tr("Face ID locked, use password please"));
m_showPrompt = true;
if (DDESESSIONCC::SingleAuthFactor == m_authFactorType)
emit retryButtonVisibleChanged(false);
break;
case AuthCommon::AS_Recover:
setAnimationState(false);
setAuthStateStyle(isMFA() ? LOGIN_WAIT : AUTH_LOCK);
m_showPrompt = true;
break;
case AuthCommon::AS_Unlocked:
setAnimationState(false);
setAuthStateStyle(isMFA() ? LOGIN_WAIT : AUTH_LOCK);
m_showPrompt = true;
emit activeAuth(AuthCommon::AT_Face);
break;
default:
setAnimationState(false);
setAuthStateStyle(isMFA() ? LOGIN_WAIT : AUTH_LOCK);
m_textLabel->setText(result);
qCWarning(DDE_SHELL) << "The state of face auth is wrong, state: " << state << ", result: " << result;
break;
}
update();
}
/**
* @brief 设置认证动画状态
*
* @param start
*/
void AuthFace::setAnimationState(const bool start)
{
m_aniIndex = 1;
if (start) {
m_aniTimer->start(20);
} else {
m_aniTimer->stop();
}
}
/**
* @brief 更新认证受限信息
*
* @param info
*/
void AuthFace::setLimitsInfo(const LimitsInfo &info)
{
AuthModule::setLimitsInfo(info);
}
/**
* @brief 更新认证锁定时的文案
*/
void AuthFace::updateUnlockPrompt()
{
AuthModule::updateUnlockPrompt();
if (m_limitsInfo->locked) {
m_textLabel->setText(tr("Face ID locked, use password please"));
} else {
QTimer::singleShot(1000, this, [this] {
emit activeAuth(m_type);
});
qCInfo(DDE_SHELL) << "Waiting authentication service...";
}
update();
}
/**
* @brief 执行动画(认证成功,认证失败,认证中...)
*/
void AuthFace::doAnimation()
{
if (m_state == AuthCommon::AS_Success) {
if (m_aniIndex > 10) {
m_aniTimer->stop();
emit authFinished(AuthCommon::AS_Success);
} else {
setAuthStateStyle(QStringLiteral(":/misc/images/unlock/unlock_%1.svg").arg(m_aniIndex++));
}
}
}
bool AuthFace::eventFilter(QObject *watched, QEvent *event)
{
if (watched == m_authStateLabel
&& QEvent::MouseButtonRelease == event->type()
&& !m_isAuthing
&& AuthCommon::AS_Locked != m_state
&& DDESESSIONCC::MultiAuthFactor == m_authFactorType) {
emit activeAuth(m_type);
}
return false;
}
void AuthFace::setAuthFactorType(AuthFactorType authFactorType)
{
if (DDESESSIONCC::SingleAuthFactor == authFactorType)
layout()->setContentsMargins(10, 0, 10, 0);
AuthModule::setAuthFactorType(authFactorType);
}