tdesktop/Telegram/SourceFiles/intro/intropwdcheck.cpp

387 lines
11 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 "intro/intropwdcheck.h"
#include "lang.h"
#include "ui/style.h"
#include "ui/filedialog.h"
#include "boxes/confirmbox.h"
#include "application.h"
#include "intro/introsignup.h"
IntroPwdCheck::IntroPwdCheck(IntroWidget *parent) : IntroStep(parent)
2015-12-08 12:33:37 +00:00
, a_errorAlpha(0)
, _a_error(animation(this, &IntroPwdCheck::step_error))
, _next(this, lang(lng_intro_submit), st::btnIntroNext)
, _salt(parent->getPwdSalt())
, _hasRecovery(parent->getHasRecovery())
, _hint(parent->getPwdHint())
, _pwdField(this, st::inpIntroPassword, lang(lng_signin_password))
, _codeField(this, st::inpIntroPassword, lang(lng_signin_code))
, _toRecover(this, lang(lng_signin_recover))
, _toPassword(this, lang(lng_signin_try_password))
, _reset(this, lang(lng_signin_reset_account), st::btnRedLink)
, sentRequest(0) {
setVisible(false);
setGeometry(parent->innerRect());
connect(&_next, SIGNAL(clicked()), this, SLOT(onSubmitPwd()));
connect(&checkRequest, SIGNAL(timeout()), this, SLOT(onCheckRequest()));
connect(&_toRecover, SIGNAL(clicked()), this, SLOT(onToRecover()));
connect(&_toPassword, SIGNAL(clicked()), this, SLOT(onToPassword()));
connect(&_pwdField, SIGNAL(changed()), this, SLOT(onInputChange()));
connect(&_codeField, SIGNAL(changed()), this, SLOT(onInputChange()));
2015-04-04 20:01:34 +00:00
connect(&_reset, SIGNAL(clicked()), this, SLOT(onReset()));
_pwdField.setEchoMode(QLineEdit::Password);
if (!_hint.isEmpty()) {
_hintText.setText(st::introFont, lng_signin_hint(lt_password_hint, _hint));
}
_codeField.hide();
_toPassword.hide();
2015-04-04 20:01:34 +00:00
_toRecover.show();
_reset.hide();
setMouseTracking(true);
}
void IntroPwdCheck::paintEvent(QPaintEvent *e) {
bool trivial = (rect() == e->rect());
QPainter p(this);
if (!trivial) {
p.setClipRect(e->rect());
}
if (trivial || e->rect().intersects(textRect)) {
p.setFont(st::introHeaderFont->f);
p.drawText(textRect, lang(lng_signin_title), style::al_top);
p.setFont(st::introFont->f);
p.drawText(textRect, lang(_pwdField.isHidden() ? lng_signin_recover_desc : lng_signin_desc), style::al_bottom);
}
if (_pwdField.isHidden()) {
if (!_emailPattern.isEmpty()) {
p.drawText(QRect(textRect.x(), _pwdField.y() + _pwdField.height() + st::introFinishSkip, textRect.width(), st::introFont->height), _emailPattern, style::al_top);
}
} else if (!_hint.isEmpty()) {
_hintText.drawElided(p, _pwdField.x(), _pwdField.y() + _pwdField.height() + st::introFinishSkip, _pwdField.width(), 1, style::al_top);
}
2015-12-08 12:33:37 +00:00
if (_a_error.animating() || error.length()) {
p.setOpacity(a_errorAlpha.current());
QRect errRect((width() - st::introErrWidth) / 2, (_pwdField.y() + _pwdField.height() + st::introFinishSkip + st::introFont->height + _next.y() - st::introErrHeight) / 2, st::introErrWidth, st::introErrHeight);
p.setFont(st::introErrFont->f);
p.setPen(st::introErrColor->p);
p.drawText(errRect, error, QTextOption(style::al_center));
p.setOpacity(1);
}
}
void IntroPwdCheck::resizeEvent(QResizeEvent *e) {
if (e->oldSize().width() != width()) {
_next.move((width() - _next.width()) / 2, st::introBtnTop);
_pwdField.move((width() - _pwdField.width()) / 2, st::introTextTop + st::introTextSize.height() + st::introCountry.top);
_codeField.move((width() - _codeField.width()) / 2, st::introTextTop + st::introTextSize.height() + st::introCountry.top);
_toRecover.move(_next.x() + (_pwdField.width() - _toRecover.width()) / 2, _next.y() + _next.height() + st::introFinishSkip);
_toPassword.move(_next.x() + (_pwdField.width() - _toPassword.width()) / 2, _next.y() + _next.height() + st::introFinishSkip);
2015-04-04 20:01:34 +00:00
_reset.move((width() - _reset.width()) / 2, _toRecover.y() + _toRecover.height() + st::introFinishSkip);
}
textRect = QRect((width() - st::introTextSize.width()) / 2, st::introTextTop, st::introTextSize.width(), st::introTextSize.height());
}
void IntroPwdCheck::showError(const QString &err) {
2015-12-08 12:33:37 +00:00
if (!_a_error.animating() && err == error) return;
if (err.length()) {
error = err;
2015-12-08 12:33:37 +00:00
a_errorAlpha.start(1);
} else {
2015-12-08 12:33:37 +00:00
a_errorAlpha.start(0);
}
2015-12-08 12:33:37 +00:00
_a_error.start();
}
2015-12-08 12:33:37 +00:00
void IntroPwdCheck::step_error(float64 ms, bool timer) {
float64 dt = ms / st::introErrDuration;
if (dt >= 1) {
2015-12-08 12:33:37 +00:00
_a_error.stop();
a_errorAlpha.finish();
if (!a_errorAlpha.current()) {
error.clear();
}
} else {
2015-12-08 12:33:37 +00:00
a_errorAlpha.update(dt, st::introErrFunc);
}
2015-12-08 12:33:37 +00:00
if (timer) update();
}
void IntroPwdCheck::activate() {
IntroStep::activate();
if (_pwdField.isHidden()) {
_codeField.setFocus();
} else {
_pwdField.setFocus();
}
}
void IntroPwdCheck::cancelled() {
if (sentRequest) {
MTP::cancel(sentRequest);
sentRequest = 0;
}
}
void IntroPwdCheck::stopCheck() {
checkRequest.stop();
}
void IntroPwdCheck::onCheckRequest() {
int32 status = MTP::state(sentRequest);
if (status < 0) {
int32 leftms = -status;
if (leftms >= 1000) {
MTP::cancel(sentRequest);
sentRequest = 0;
if (!_pwdField.isEnabled()) {
_pwdField.setDisabled(false);
_codeField.setDisabled(false);
activate();
}
}
}
if (!sentRequest && status == MTP::RequestSent) {
stopCheck();
}
}
void IntroPwdCheck::pwdSubmitDone(bool recover, const MTPauth_Authorization &result) {
2015-04-04 20:01:34 +00:00
sentRequest = 0;
stopCheck();
if (recover) {
cSetPasswordRecovered(true);
}
_pwdField.setDisabled(false);
_codeField.setDisabled(false);
const auto &d(result.c_auth_authorization());
2015-10-29 00:16:52 +00:00
if (d.vuser.type() != mtpc_user || !d.vuser.c_user().is_self()) { // wtf?
showError(lang(lng_server_error));
return;
}
intro()->finish(d.vuser);
}
bool IntroPwdCheck::pwdSubmitFail(const RPCError &error) {
if (MTP::isFloodError(error)) {
sentRequest = 0;
stopCheck();
_codeField.setDisabled(false);
showError(lang(lng_flood_error));
_pwdField.setDisabled(false);
_pwdField.notaBene();
return true;
}
if (MTP::isDefaultHandledError(error)) return false;
2015-04-04 20:01:34 +00:00
sentRequest = 0;
stopCheck();
_pwdField.setDisabled(false);
_codeField.setDisabled(false);
const QString &err = error.type();
if (err == qstr("PASSWORD_HASH_INVALID")) {
showError(lang(lng_signin_bad_password));
_pwdField.selectAll();
_pwdField.notaBene();
return true;
} else if (err == qstr("PASSWORD_EMPTY")) {
intro()->onBack();
}
if (cDebug()) { // internal server error
showError(err + ": " + error.description());
} else {
showError(lang(lng_server_error));
}
_pwdField.setFocus();
return false;
}
bool IntroPwdCheck::codeSubmitFail(const RPCError &error) {
if (MTP::isFloodError(error)) {
showError(lang(lng_flood_error));
_codeField.notaBene();
return true;
}
if (MTP::isDefaultHandledError(error)) return false;
2015-04-04 20:01:34 +00:00
sentRequest = 0;
stopCheck();
_pwdField.setDisabled(false);
_codeField.setDisabled(false);
const QString &err = error.type();
if (err == qstr("PASSWORD_EMPTY")) {
intro()->onBack();
return true;
} else if (err == qstr("PASSWORD_RECOVERY_NA")) {
recoverStartFail(error);
return true;
} else if (err == qstr("PASSWORD_RECOVERY_EXPIRED")) {
_emailPattern = QString();
onToPassword();
return true;
} else if (err == qstr("CODE_INVALID")) {
showError(lang(lng_signin_wrong_code));
_codeField.selectAll();
2015-04-04 20:01:34 +00:00
_codeField.notaBene();
return true;
}
if (cDebug()) { // internal server error
showError(err + ": " + error.description());
} else {
showError(lang(lng_server_error));
}
_codeField.setFocus();
return false;
}
void IntroPwdCheck::recoverStarted(const MTPauth_PasswordRecovery &result) {
2015-10-03 10:09:09 +00:00
_emailPattern = st::introFont->elided(lng_signin_recover_hint(lt_recover_email, qs(result.c_auth_passwordRecovery().vemail_pattern)), textRect.width());
update();
}
bool IntroPwdCheck::recoverStartFail(const RPCError &error) {
stopCheck();
_pwdField.setDisabled(false);
_codeField.setDisabled(false);
_pwdField.show();
_codeField.hide();
_pwdField.setFocus();
update();
showError(QString());
return true;
}
void IntroPwdCheck::onToRecover() {
2015-04-04 20:01:34 +00:00
if (_hasRecovery) {
if (sentRequest) {
MTP::cancel(sentRequest);
sentRequest = 0;
}
showError(QString());
2015-04-04 20:01:34 +00:00
_toRecover.hide();
_toPassword.show();
_pwdField.hide();
_pwdField.setText(QString());
_codeField.show();
_codeField.setFocus();
if (_emailPattern.isEmpty()) {
MTP::send(MTPauth_RequestPasswordRecovery(), rpcDone(&IntroPwdCheck::recoverStarted), rpcFail(&IntroPwdCheck::recoverStartFail));
}
update();
} else {
2015-10-03 10:09:09 +00:00
ConfirmBox *box = new InformBox(lang(lng_signin_no_email_forgot));
2015-12-07 18:09:05 +00:00
Ui::showLayer(box);
2015-04-04 20:01:34 +00:00
connect(box, SIGNAL(destroyed(QObject*)), this, SLOT(onToReset()));
}
}
void IntroPwdCheck::onToPassword() {
2015-10-03 10:09:09 +00:00
ConfirmBox *box = new InformBox(lang(lng_signin_cant_email_forgot));
2015-12-07 18:09:05 +00:00
Ui::showLayer(box);
2015-04-04 20:01:34 +00:00
connect(box, SIGNAL(destroyed(QObject*)), this, SLOT(onToReset()));
}
void IntroPwdCheck::onToReset() {
if (sentRequest) {
MTP::cancel(sentRequest);
sentRequest = 0;
}
_toRecover.show();
_toPassword.hide();
_pwdField.show();
_codeField.hide();
_codeField.setText(QString());
_pwdField.setFocus();
2015-04-04 20:01:34 +00:00
_reset.show();
update();
}
2015-04-04 20:01:34 +00:00
void IntroPwdCheck::onReset() {
if (sentRequest) return;
2015-10-03 10:09:09 +00:00
ConfirmBox *box = new ConfirmBox(lang(lng_signin_sure_reset), lang(lng_signin_reset), st::attentionBoxButton);
2015-04-04 20:01:34 +00:00
connect(box, SIGNAL(confirmed()), this, SLOT(onResetSure()));
2015-12-07 18:09:05 +00:00
Ui::showLayer(box);
2015-04-04 20:01:34 +00:00
}
void IntroPwdCheck::onResetSure() {
if (sentRequest) return;
sentRequest = MTP::send(MTPaccount_DeleteAccount(MTP_string("Forgot password")), rpcDone(&IntroPwdCheck::deleteDone), rpcFail(&IntroPwdCheck::deleteFail));
}
bool IntroPwdCheck::deleteFail(const RPCError &error) {
if (MTP::isDefaultHandledError(error)) return false;
2015-04-04 20:01:34 +00:00
sentRequest = 0;
showError(lang(lng_server_error));
return true;
}
void IntroPwdCheck::deleteDone(const MTPBool &v) {
2015-12-07 18:09:05 +00:00
Ui::hideLayer();
intro()->replaceStep(new IntroSignup(intro()));
2015-04-04 20:01:34 +00:00
}
void IntroPwdCheck::onInputChange() {
showError(QString());
}
void IntroPwdCheck::onSubmitPwd(bool force) {
2015-04-04 20:01:34 +00:00
if (sentRequest) return;
if (_pwdField.isHidden()) {
if (!force && !_codeField.isEnabled()) return;
QString code = _codeField.text().trimmed();
if (code.isEmpty()) {
_codeField.notaBene();
return;
}
sentRequest = MTP::send(MTPauth_RecoverPassword(MTP_string(code)), rpcDone(&IntroPwdCheck::pwdSubmitDone, true), rpcFail(&IntroPwdCheck::codeSubmitFail));
} else {
if (!force && !_pwdField.isEnabled()) return;
_pwdField.setDisabled(true);
setFocus();
showError(QString());
QByteArray pwdData = _salt + _pwdField.text().toUtf8() + _salt, pwdHash(32, Qt::Uninitialized);
hashSha256(pwdData.constData(), pwdData.size(), pwdHash.data());
sentRequest = MTP::send(MTPauth_CheckPassword(MTP_bytes(pwdHash)), rpcDone(&IntroPwdCheck::pwdSubmitDone, false), rpcFail(&IntroPwdCheck::pwdSubmitFail));
}
}
void IntroPwdCheck::onSubmit() {
onSubmitPwd();
}