tdesktop/Telegram/SourceFiles/passcodewidget.cpp

210 lines
5.8 KiB
C++
Raw Normal View History

/*
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.
2015-10-03 13:16:42 +00:00
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
2016-02-08 10:56:18 +00:00
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
*/
#include "stdafx.h"
#include "passcodewidget.h"
#include "lang.h"
#include "localstorage.h"
#include "mainwindow.h"
#include "application.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"
2015-10-17 14:52:26 +00:00
PasscodeWidget::PasscodeWidget(QWidget *parent) : TWidget(parent)
2015-12-08 12:33:37 +00:00
, _a_show(animation(this, &PasscodeWidget::step_show))
2015-10-17 14:52:26 +00:00
, _passcode(this, st::passcodeInput)
, _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();
_passcode->setFocus();
}
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())) {
2015-04-04 20:01:34 +00:00
cSetPasscodeBadTries(0);
2016-11-24 19:28:23 +00:00
App::wnd()->clearPasscode(); // Destroys this widget.
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);
MTP::start();
if (MTP::authedId()) {
App::wnd()->setupMain();
} else {
App::wnd()->setupIntro();
}
App::app()->checkMapVersion();
} 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::animShow(const QPixmap &bgAnimCache, bool back) {
2015-10-15 11:51:10 +00:00
if (App::app()) App::app()->mtpPause();
2015-10-17 14:52:26 +00:00
(back ? _cacheOver : _cacheUnder) = bgAnimCache;
_a_show.stop();
showAll();
2015-10-17 14:52:26 +00:00
(back ? _cacheUnder : _cacheOver) = myGrab(this);
hideAll();
a_coordUnder = back ? anim::ivalue(-st::slideShift, 0) : anim::ivalue(0, -st::slideShift);
2015-10-17 14:52:26 +00:00
a_coordOver = back ? anim::ivalue(0, width()) : anim::ivalue(width(), 0);
a_shadow = back ? anim::fvalue(1, 0) : anim::fvalue(0, 1);
_a_show.start();
show();
}
2015-12-08 12:33:37 +00:00
void PasscodeWidget::step_show(float64 ms, bool timer) {
2015-10-17 14:52:26 +00:00
float64 dt = ms / st::slideDuration;
if (dt >= 1) {
_a_show.stop();
a_coordUnder.finish();
a_coordOver.finish();
a_shadow.finish();
2015-10-17 14:52:26 +00:00
_cacheUnder = _cacheOver = QPixmap();
showAll();
2015-09-16 13:04:08 +00:00
if (App::wnd()) App::wnd()->setInnerFocus();
if (App::app()) App::app()->mtpUnpause();
Ui::showChatsList();
} else {
a_coordUnder.update(dt, Window::SlideAnimation::transition());
a_coordOver.update(dt, Window::SlideAnimation::transition());
a_shadow.update(dt, Window::SlideAnimation::transition());
}
2015-12-08 12:33:37 +00:00
if (timer) update();
}
2015-12-08 12:33:37 +00:00
void PasscodeWidget::stop_show() {
2015-10-17 14:52:26 +00:00
_a_show.stop();
}
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());
}
2015-10-17 14:52:26 +00:00
if (_a_show.animating()) {
if (a_coordOver.current() > 0) {
p.drawPixmap(QRect(0, 0, a_coordOver.current(), height()), _cacheUnder, QRect(-a_coordUnder.current() * cRetinaFactor(), 0, a_coordOver.current() * cRetinaFactor(), height() * cRetinaFactor()));
2016-10-31 12:29:26 +00:00
p.setOpacity(a_shadow.current());
p.fillRect(0, 0, a_coordOver.current(), height(), st::slideFadeOutBg);
2015-10-17 14:52:26 +00:00
p.setOpacity(1);
}
p.drawPixmap(a_coordOver.current(), 0, _cacheOver);
p.setOpacity(a_shadow.current());
st::slideShadow.fill(p, QRect(a_coordOver.current() - 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() {
_passcode->setFocus();
}