tdesktop/Telegram/SourceFiles/passcodewidget.cpp

185 lines
5.0 KiB
C++
Raw Normal View History

/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "passcodewidget.h"
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"
#include "mainwindow.h"
#include "messenger.h"
#include "ui/text/text.h"
#include "ui/widgets/buttons.h"
#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"
#include "window/window_controller.h"
#include "auth_session.h"
2015-10-17 14:52:26 +00:00
PasscodeWidget::PasscodeWidget(QWidget *parent) : TWidget(parent)
, _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)) {
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()) {
2016-11-24 19:28:23 +00:00
_passcode->showError();
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;
}
if (App::main()) {
if (Local::checkPasscode(_passcode->text().toUtf8())) {
Messenger::Instance().clearPasscode(); // Destroys this widget.
2016-11-24 19:28:23 +00:00
return;
} else {
2015-04-04 20:01:34 +00:00
cSetPasscodeBadTries(cPasscodeBadTries() + 1);
cSetPasscodeLastTry(getms(true));
onError();
return;
}
} else {
if (Local::readMap(_passcode->text().toUtf8()) != Local::ReadMapPassNeeded) {
2015-04-04 20:01:34 +00:00
cSetPasscodeBadTries(0);
Messenger::Instance().startMtp();
if (AuthSession::Exists()) {
App::wnd()->setupMain();
} else {
App::wnd()->setupIntro();
}
} else {
2015-04-04 20:01:34 +00:00
cSetPasscodeBadTries(cPasscodeBadTries() + 1);
cSetPasscodeLastTry(getms(true));
onError();
return;
}
}
}
void PasscodeWidget::onError() {
_error = lang(lng_passcode_wrong);
_passcode->selectAll();
2016-11-24 19:28:23 +00:00
_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->finishAnimating();
(_showBack ? _cacheUnder : _cacheOver) = Ui::GrabWidget(this);
2015-10-17 14:52:26 +00:00
hideAll();
_a_show.start(
[this] { animationCallback(); },
0.,
1.,
st::slideDuration,
Window::SlideAnimation::transition());
show();
}
void PasscodeWidget::animationCallback() {
update();
if (!_a_show.animating()) {
showAll();
2015-09-16 13:04:08 +00:00
if (App::wnd()) App::wnd()->setInnerFocus();
Ui::showChatsList();
_cacheUnder = _cacheOver = QPixmap();
}
2015-10-17 14:52:26 +00:00
}
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.);
2015-10-17 14:52:26 +00:00
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);
2015-10-17 14:52:26 +00:00
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);
2016-10-31 12:29:26 +00:00
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()) {
2016-10-31 12:29:26 +00:00
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();
}