2015-03-02 12:34:16 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2015-03-02 12:34:16 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2015-03-02 12:34:16 +00:00
|
|
|
*/
|
2016-04-21 17:57:29 +00:00
|
|
|
#include "passcodewidget.h"
|
2015-03-02 12:34:16 +00:00
|
|
|
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/localstorage.h"
|
2016-04-12 21:31:28 +00:00
|
|
|
#include "mainwindow.h"
|
2017-02-23 10:59:19 +00:00
|
|
|
#include "messenger.h"
|
2016-04-14 11:00:23 +00:00
|
|
|
#include "ui/text/text.h"
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
2016-11-15 11:56:49 +00:00
|
|
|
#include "ui/widgets/input_fields.h"
|
2016-10-31 12:29:26 +00:00
|
|
|
#include "styles/style_boxes.h"
|
2016-11-24 19:28:23 +00:00
|
|
|
#include "window/window_slide_animation.h"
|
2017-04-09 18:06:06 +00:00
|
|
|
#include "window/window_controller.h"
|
2017-02-23 09:32:28 +00:00
|
|
|
#include "auth_session.h"
|
2015-03-02 12:34:16 +00:00
|
|
|
|
2015-10-17 14:52:26 +00:00
|
|
|
PasscodeWidget::PasscodeWidget(QWidget *parent) : TWidget(parent)
|
2017-05-30 15:21:05 +00:00
|
|
|
, _passcode(this, st::passcodeInput, langFactory(lng_passcode_ph))
|
|
|
|
, _submit(this, langFactory(lng_passcode_submit), st::passcodeSubmit)
|
2015-10-17 14:52:26 +00:00
|
|
|
, _logout(this, lang(lng_passcode_logout)) {
|
2016-11-04 19:50:35 +00:00
|
|
|
connect(_passcode, SIGNAL(changed()), this, SLOT(onChanged()));
|
|
|
|
connect(_passcode, SIGNAL(submitted(bool)), this, SLOT(onSubmit()));
|
2015-03-02 12:34:16 +00:00
|
|
|
|
2016-11-04 19:50:35 +00:00
|
|
|
_submit->setClickedCallback([this] { onSubmit(); });
|
|
|
|
_logout->setClickedCallback([] { App::wnd()->onLogout(); });
|
2015-03-12 12:23:08 +00:00
|
|
|
|
2015-03-02 12:34:16 +00:00
|
|
|
show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PasscodeWidget::onSubmit() {
|
2016-11-04 19:50:35 +00:00
|
|
|
if (_passcode->text().isEmpty()) {
|
2016-11-24 19:28:23 +00:00
|
|
|
_passcode->showError();
|
2015-03-02 12:34:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-04-04 20:01:34 +00:00
|
|
|
if (!passcodeCanTry()) {
|
|
|
|
_error = lang(lng_flood_error);
|
2016-11-24 19:28:23 +00:00
|
|
|
_passcode->showError();
|
2015-04-04 20:01:34 +00:00
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
2015-03-02 12:34:16 +00:00
|
|
|
|
|
|
|
if (App::main()) {
|
2016-11-04 19:50:35 +00:00
|
|
|
if (Local::checkPasscode(_passcode->text().toUtf8())) {
|
2017-04-15 18:48:54 +00:00
|
|
|
Messenger::Instance().clearPasscode(); // Destroys this widget.
|
2016-11-24 19:28:23 +00:00
|
|
|
return;
|
2015-03-02 12:34:16 +00:00
|
|
|
} else {
|
2015-04-04 20:01:34 +00:00
|
|
|
cSetPasscodeBadTries(cPasscodeBadTries() + 1);
|
|
|
|
cSetPasscodeLastTry(getms(true));
|
|
|
|
onError();
|
2015-03-02 12:34:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2016-11-04 19:50:35 +00:00
|
|
|
if (Local::readMap(_passcode->text().toUtf8()) != Local::ReadMapPassNeeded) {
|
2015-04-04 20:01:34 +00:00
|
|
|
cSetPasscodeBadTries(0);
|
2015-03-02 12:34:16 +00:00
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
Messenger::Instance().startMtp();
|
2017-03-04 19:36:59 +00:00
|
|
|
if (AuthSession::Exists()) {
|
2016-11-04 19:50:35 +00:00
|
|
|
App::wnd()->setupMain();
|
2015-03-02 12:34:16 +00:00
|
|
|
} else {
|
2016-11-04 19:50:35 +00:00
|
|
|
App::wnd()->setupIntro();
|
2015-03-02 12:34:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-04-04 20:01:34 +00:00
|
|
|
cSetPasscodeBadTries(cPasscodeBadTries() + 1);
|
|
|
|
cSetPasscodeLastTry(getms(true));
|
|
|
|
onError();
|
2015-03-02 12:34:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PasscodeWidget::onError() {
|
|
|
|
_error = lang(lng_passcode_wrong);
|
2016-11-04 19:50:35 +00:00
|
|
|
_passcode->selectAll();
|
2016-11-24 19:28:23 +00:00
|
|
|
_passcode->showError();
|
2015-03-02 12:34:16 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PasscodeWidget::onChanged() {
|
|
|
|
if (!_error.isEmpty()) {
|
|
|
|
_error = QString();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
void PasscodeWidget::showAnimated(const QPixmap &bgAnimCache, bool back) {
|
|
|
|
_showBack = back;
|
|
|
|
(_showBack ? _cacheOver : _cacheUnder) = bgAnimCache;
|
2015-10-15 10:18:24 +00:00
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
_a_show.finish();
|
2015-03-02 12:34:16 +00:00
|
|
|
|
|
|
|
showAll();
|
2016-12-21 15:05:58 +00:00
|
|
|
setInnerFocus();
|
2017-09-30 19:20:40 +00:00
|
|
|
_passcode->finishAnimating();
|
2017-12-26 12:41:48 +00:00
|
|
|
(_showBack ? _cacheUnder : _cacheOver) = Ui::GrabWidget(this);
|
2015-10-17 14:52:26 +00:00
|
|
|
hideAll();
|
2015-03-02 12:34:16 +00:00
|
|
|
|
2017-12-26 12:41:48 +00:00
|
|
|
_a_show.start(
|
|
|
|
[this] { animationCallback(); },
|
|
|
|
0.,
|
|
|
|
1.,
|
|
|
|
st::slideDuration,
|
|
|
|
Window::SlideAnimation::transition());
|
2015-03-02 12:34:16 +00:00
|
|
|
show();
|
|
|
|
}
|
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
void PasscodeWidget::animationCallback() {
|
|
|
|
update();
|
|
|
|
if (!_a_show.animating()) {
|
2015-03-02 12:34:16 +00:00
|
|
|
showAll();
|
2015-09-16 13:04:08 +00:00
|
|
|
if (App::wnd()) App::wnd()->setInnerFocus();
|
2015-10-15 10:18:24 +00:00
|
|
|
|
2016-10-17 10:55:17 +00:00
|
|
|
Ui::showChatsList();
|
2015-03-02 12:34:16 +00:00
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
_cacheUnder = _cacheOver = QPixmap();
|
|
|
|
}
|
2015-10-17 14:52:26 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 12:34:16 +00:00
|
|
|
void PasscodeWidget::showAll() {
|
2016-11-04 19:50:35 +00:00
|
|
|
_passcode->show();
|
|
|
|
_submit->show();
|
|
|
|
_logout->show();
|
2015-03-02 12:34:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PasscodeWidget::hideAll() {
|
2016-11-04 19:50:35 +00:00
|
|
|
_passcode->hide();
|
|
|
|
_submit->hide();
|
|
|
|
_logout->hide();
|
2015-03-02 12:34:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PasscodeWidget::paintEvent(QPaintEvent *e) {
|
|
|
|
bool trivial = (rect() == e->rect());
|
|
|
|
setMouseTracking(true);
|
|
|
|
|
2016-04-21 17:57:29 +00:00
|
|
|
Painter p(this);
|
2015-03-02 12:34:16 +00:00
|
|
|
if (!trivial) {
|
|
|
|
p.setClipRect(e->rect());
|
|
|
|
}
|
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
auto progress = _a_show.current(getms(), 1.);
|
2015-10-17 14:52:26 +00:00
|
|
|
if (_a_show.animating()) {
|
2016-12-05 11:01:08 +00:00
|
|
|
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);
|
2015-10-17 14:52:26 +00:00
|
|
|
p.setOpacity(1);
|
|
|
|
}
|
2016-12-05 11:01:08 +00:00
|
|
|
p.drawPixmap(coordOver, 0, _cacheOver);
|
|
|
|
p.setOpacity(shadow);
|
|
|
|
st::slideShadow.fill(p, QRect(coordOver - st::slideShadow.width(), 0, st::slideShadow.width(), height()));
|
2015-03-02 12:34:16 +00:00
|
|
|
} else {
|
2016-09-29 11:37:16 +00:00
|
|
|
p.fillRect(rect(), st::windowBg);
|
2015-03-02 12:34:16 +00:00
|
|
|
|
2016-10-31 12:29:26 +00:00
|
|
|
p.setFont(st::passcodeHeaderFont);
|
2016-11-16 10:44:06 +00:00
|
|
|
p.setPen(st::windowFg);
|
2016-11-04 19:50:35 +00:00
|
|
|
p.drawText(QRect(0, _passcode->y() - st::passcodeHeaderHeight, width(), st::passcodeHeaderHeight), lang(lng_passcode_enter), style::al_center);
|
2015-03-02 12:34:16 +00:00
|
|
|
|
|
|
|
if (!_error.isEmpty()) {
|
2016-10-31 12:29:26 +00:00
|
|
|
p.setFont(st::boxTextFont);
|
|
|
|
p.setPen(st::boxTextFgError);
|
2016-11-04 19:50:35 +00:00
|
|
|
p.drawText(QRect(0, _passcode->y() + _passcode->height(), width(), st::passcodeSubmitSkip), _error, style::al_center);
|
2015-03-02 12:34:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PasscodeWidget::resizeEvent(QResizeEvent *e) {
|
2016-11-04 19:50:35 +00:00
|
|
|
_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);
|
2015-03-02 12:34:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PasscodeWidget::setInnerFocus() {
|
2017-04-09 18:06:06 +00:00
|
|
|
if (auto controller = App::wnd()->controller()) {
|
|
|
|
controller->dialogsListFocused().set(false, true);
|
|
|
|
}
|
2016-12-21 15:05:58 +00:00
|
|
|
_passcode->setFocusFast();
|
2015-03-02 12:34:16 +00:00
|
|
|
}
|