forked from telegramdesktop/tdesktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passcodewidget.cpp
194 lines (164 loc) · 5.65 KB
/
passcodewidget.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
/*
This file is part of Telegram Desktop,
the official desktop version of Telegram messaging app, see https://telegram.org
Telegram Desktop is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
It is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
In addition, as a special exception, the copyright holders give permission
to link the code of portions of this program with the OpenSSL library.
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
*/
#include "passcodewidget.h"
#include "lang.h"
#include "storage/localstorage.h"
#include "mainwindow.h"
#include "messenger.h"
#include "ui/text/text.h"
#include "ui/widgets/buttons.h"
#include "ui/widgets/input_fields.h"
#include "styles/style_boxes.h"
#include "window/window_slide_animation.h"
#include "window/window_controller.h"
#include "auth_session.h"
PasscodeWidget::PasscodeWidget(QWidget *parent) : TWidget(parent)
, _passcode(this, st::passcodeInput, lang(lng_passcode_ph))
, _submit(this, lang(lng_passcode_submit), st::passcodeSubmit)
, _logout(this, lang(lng_passcode_logout)) {
connect(_passcode, SIGNAL(changed()), this, SLOT(onChanged()));
connect(_passcode, SIGNAL(submitted(bool)), this, SLOT(onSubmit()));
_submit->setClickedCallback([this] { onSubmit(); });
_logout->setClickedCallback([] { App::wnd()->onLogout(); });
show();
}
void PasscodeWidget::onSubmit() {
if (_passcode->text().isEmpty()) {
_passcode->showError();
return;
}
if (!passcodeCanTry()) {
_error = lang(lng_flood_error);
_passcode->showError();
update();
return;
}
if (App::main()) {
if (Local::checkPasscode(_passcode->text().toUtf8())) {
Messenger::Instance().clearPasscode(); // Destroys this widget.
return;
} else {
cSetPasscodeBadTries(cPasscodeBadTries() + 1);
cSetPasscodeLastTry(getms(true));
onError();
return;
}
} else {
if (Local::readMap(_passcode->text().toUtf8()) != Local::ReadMapPassNeeded) {
cSetPasscodeBadTries(0);
Messenger::Instance().startMtp();
if (AuthSession::Exists()) {
App::wnd()->setupMain();
} else {
App::wnd()->setupIntro();
}
App::app()->checkMapVersion();
} else {
cSetPasscodeBadTries(cPasscodeBadTries() + 1);
cSetPasscodeLastTry(getms(true));
onError();
return;
}
}
}
void PasscodeWidget::onError() {
_error = lang(lng_passcode_wrong);
_passcode->selectAll();
_passcode->showError();
update();
}
void PasscodeWidget::onChanged() {
if (!_error.isEmpty()) {
_error = QString();
update();
}
}
void PasscodeWidget::showAnimated(const QPixmap &bgAnimCache, bool back) {
_showBack = back;
(_showBack ? _cacheOver : _cacheUnder) = bgAnimCache;
_a_show.finish();
showAll();
setInnerFocus();
_passcode->finishAnimations();
(_showBack ? _cacheUnder : _cacheOver) = myGrab(this);
hideAll();
_a_show.start([this] { animationCallback(); }, 0., 1., st::slideDuration, Window::SlideAnimation::transition());
show();
}
void PasscodeWidget::animationCallback() {
update();
if (!_a_show.animating()) {
showAll();
if (App::wnd()) App::wnd()->setInnerFocus();
Ui::showChatsList();
_cacheUnder = _cacheOver = QPixmap();
}
}
void PasscodeWidget::showAll() {
_passcode->show();
_submit->show();
_logout->show();
}
void PasscodeWidget::hideAll() {
_passcode->hide();
_submit->hide();
_logout->hide();
}
void PasscodeWidget::paintEvent(QPaintEvent *e) {
bool trivial = (rect() == e->rect());
setMouseTracking(true);
Painter p(this);
if (!trivial) {
p.setClipRect(e->rect());
}
auto progress = _a_show.current(getms(), 1.);
if (_a_show.animating()) {
auto coordUnder = _showBack ? anim::interpolate(-st::slideShift, 0, progress) : anim::interpolate(0, -st::slideShift, progress);
auto coordOver = _showBack ? anim::interpolate(0, width(), progress) : anim::interpolate(width(), 0, progress);
auto shadow = _showBack ? (1. - progress) : progress;
if (coordOver > 0) {
p.drawPixmap(QRect(0, 0, coordOver, height()), _cacheUnder, QRect(-coordUnder * cRetinaFactor(), 0, coordOver * cRetinaFactor(), height() * cRetinaFactor()));
p.setOpacity(shadow);
p.fillRect(0, 0, coordOver, height(), st::slideFadeOutBg);
p.setOpacity(1);
}
p.drawPixmap(coordOver, 0, _cacheOver);
p.setOpacity(shadow);
st::slideShadow.fill(p, QRect(coordOver - st::slideShadow.width(), 0, st::slideShadow.width(), height()));
} else {
p.fillRect(rect(), st::windowBg);
p.setFont(st::passcodeHeaderFont);
p.setPen(st::windowFg);
p.drawText(QRect(0, _passcode->y() - st::passcodeHeaderHeight, width(), st::passcodeHeaderHeight), lang(lng_passcode_enter), style::al_center);
if (!_error.isEmpty()) {
p.setFont(st::boxTextFont);
p.setPen(st::boxTextFgError);
p.drawText(QRect(0, _passcode->y() + _passcode->height(), width(), st::passcodeSubmitSkip), _error, style::al_center);
}
}
}
void PasscodeWidget::resizeEvent(QResizeEvent *e) {
_passcode->move((width() - _passcode->width()) / 2, (height() / 3));
_submit->move(_passcode->x(), _passcode->y() + _passcode->height() + st::passcodeSubmitSkip);
_logout->move(_passcode->x() + (_passcode->width() - _logout->width()) / 2, _submit->y() + _submit->height() + st::linkFont->ascent);
}
void PasscodeWidget::setInnerFocus() {
if (auto controller = App::wnd()->controller()) {
controller->dialogsListFocused().set(false, true);
}
_passcode->setFocusFast();
}