tdesktop/Telegram/SourceFiles/intro/intro_code.cpp

453 lines
12 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
*/
2019-11-27 09:45:23 +00:00
#include "intro/intro_code.h"
2017-04-13 08:27:10 +00:00
#include "lang/lang_keys.h"
2019-11-27 09:45:23 +00:00
#include "intro/intro_signup.h"
#include "intro/intro_password_check.h"
2018-08-03 21:48:00 +00:00
#include "core/update_checker.h"
#include "ui/widgets/buttons.h"
2016-11-24 19:28:23 +00:00
#include "ui/widgets/labels.h"
#include "ui/text/text_utilities.h"
2018-08-03 21:48:00 +00:00
#include "boxes/confirm_box.h"
2020-06-11 13:07:14 +00:00
#include "main/main_account.h"
#include "mtproto/mtp_instance.h"
#include "app.h"
#include "styles/style_intro.h"
2016-11-24 19:28:23 +00:00
namespace Intro {
2019-11-26 11:10:44 +00:00
namespace details {
2016-11-24 19:28:23 +00:00
2018-11-19 12:15:59 +00:00
CodeInput::CodeInput(
QWidget *parent,
const style::InputField &st,
rpl::producer<QString> placeholder)
: Ui::MaskedInputField(parent, st, std::move(placeholder)) {
2016-11-24 19:28:23 +00:00
}
void CodeInput::setDigitsCountMax(int digitsCount) {
_digitsCountMax = digitsCount;
}
2016-11-24 19:28:23 +00:00
void CodeInput::correctValue(const QString &was, int wasCursor, QString &now, int &nowCursor) {
2015-10-06 19:49:23 +00:00
QString newText;
2016-11-24 19:28:23 +00:00
int oldPos(nowCursor), newPos(-1), oldLen(now.length()), digitCount = 0;
for (int i = 0; i < oldLen; ++i) {
2015-10-06 19:49:23 +00:00
if (now[i].isDigit()) {
++digitCount;
}
}
2016-11-24 19:28:23 +00:00
accumulate_min(digitCount, _digitsCountMax);
auto strict = (digitCount == _digitsCountMax);
newText.reserve(oldLen);
for (int i = 0; i < oldLen; ++i) {
2015-10-06 19:49:23 +00:00
QChar ch(now[i]);
if (ch.isDigit()) {
if (!digitCount--) {
break;
}
newText += ch;
if (strict && !digitCount) {
break;
}
2018-11-19 12:15:59 +00:00
} else if (ch == '-') {
newText += ch;
}
if (i == oldPos) {
newPos = newText.length();
}
}
2016-11-24 19:28:23 +00:00
if (newPos < 0 || newPos > newText.size()) {
newPos = newText.size();
}
2015-10-06 19:49:23 +00:00
if (newText != now) {
now = newText;
setText(now);
startPlaceholderAnimation();
2016-11-24 19:28:23 +00:00
}
if (newPos != nowCursor) {
nowCursor = newPos;
setCursorPosition(nowCursor);
}
}
2019-07-24 08:46:23 +00:00
CodeWidget::CodeWidget(
QWidget *parent,
not_null<Main::Account*> account,
2019-11-26 11:10:44 +00:00
not_null<Data*> data)
2019-07-24 08:46:23 +00:00
: Step(parent, account, data)
2019-06-19 15:09:03 +00:00
, _noTelegramCode(this, tr::lng_code_no_telegram(tr::now), st::introLink)
, _code(this, st::introCode, tr::lng_code_ph())
2019-11-27 08:33:18 +00:00
, _callTimer([=] { sendCall(); })
2016-11-24 19:28:23 +00:00
, _callStatus(getData()->callStatus)
, _callTimeout(getData()->callTimeout)
, _callLabel(this, st::introDescription)
2019-11-27 08:33:18 +00:00
, _checkRequestTimer([=] { checkRequest(); }) {
2020-09-30 09:11:44 +00:00
Lang::Updated(
) | rpl::start_with_next([=] {
refreshLang();
}, lifetime());
2019-11-27 08:33:18 +00:00
connect(_code, &CodeInput::changed, [=] { codeChanged(); });
_noTelegramCode->addClickHandler([=] { noTelegramCode(); });
2016-11-24 19:28:23 +00:00
_code->setDigitsCountMax(getData()->codeLength);
setTitleText(rpl::single(App::formatPhone(getData()->phone)));
updateDescText();
}
void CodeWidget::refreshLang() {
2019-11-27 08:33:18 +00:00
if (_noTelegramCode) {
_noTelegramCode->setText(tr::lng_code_no_telegram(tr::now));
}
2016-11-24 19:28:23 +00:00
updateDescText();
updateControlsGeometry();
}
2019-11-26 14:27:09 +00:00
int CodeWidget::errorTop() const {
return contentTop() + st::introErrorBelowLinkTop;
}
2016-11-24 19:28:23 +00:00
void CodeWidget::updateDescText() {
const auto byTelegram = getData()->codeByTelegram;
setDescriptionText(
(byTelegram ? tr::lng_code_from_telegram : tr::lng_code_desc)(
Ui::Text::RichLangValue));
2016-11-24 19:28:23 +00:00
if (getData()->codeByTelegram) {
_noTelegramCode->show();
2019-11-27 08:33:18 +00:00
_callTimer.cancel();
} else {
_noTelegramCode->hide();
2016-11-24 19:28:23 +00:00
_callStatus = getData()->callStatus;
_callTimeout = getData()->callTimeout;
2019-11-27 08:33:18 +00:00
if (_callStatus == CallStatus::Waiting && !_callTimer.isActive()) {
_callTimer.callEach(1000);
}
}
2016-11-24 19:28:23 +00:00
updateCallText();
}
2016-11-24 19:28:23 +00:00
void CodeWidget::updateCallText() {
auto text = ([this]() -> QString {
if (getData()->codeByTelegram) {
return QString();
}
switch (_callStatus) {
2019-11-26 11:10:44 +00:00
case CallStatus::Waiting: {
2016-11-24 19:28:23 +00:00
if (_callTimeout >= 3600) {
return tr::lng_code_call(
tr::now,
lt_minutes,
qsl("%1:%2"
).arg(_callTimeout / 3600
).arg((_callTimeout / 60) % 60, 2, 10, QChar('0')),
lt_seconds,
qsl("%1").arg(_callTimeout % 60, 2, 10, QChar('0')));
2016-03-15 19:38:30 +00:00
} else {
return tr::lng_code_call(
tr::now,
lt_minutes,
QString::number(_callTimeout / 60),
lt_seconds,
qsl("%1").arg(_callTimeout % 60, 2, 10, QChar('0')));
2016-03-15 19:38:30 +00:00
}
} break;
2019-11-26 11:10:44 +00:00
case CallStatus::Calling:
return tr::lng_code_calling(tr::now);
2019-11-26 11:10:44 +00:00
case CallStatus::Called:
return tr::lng_code_called(tr::now);
2016-03-15 19:38:30 +00:00
}
2016-11-24 19:28:23 +00:00
return QString();
})();
_callLabel->setText(text);
_callLabel->setVisible(!text.isEmpty() && !animating());
}
2016-11-24 19:28:23 +00:00
void CodeWidget::resizeEvent(QResizeEvent *e) {
Step::resizeEvent(e);
updateControlsGeometry();
}
void CodeWidget::updateControlsGeometry() {
2016-11-24 19:28:23 +00:00
_code->moveToLeft(contentLeft(), contentTop() + st::introStepFieldTop);
auto linkTop = _code->y() + _code->height() + st::introLinkTop;
_noTelegramCode->moveToLeft(contentLeft() + st::buttonRadius, linkTop);
_callLabel->moveToLeft(contentLeft() + st::buttonRadius, linkTop);
}
void CodeWidget::showCodeError(rpl::producer<QString> text) {
_code->showError();
showError(std::move(text));
}
2016-11-24 19:28:23 +00:00
void CodeWidget::setInnerFocus() {
_code->setFocusFast();
2016-11-24 19:28:23 +00:00
}
2016-11-24 19:28:23 +00:00
void CodeWidget::activate() {
Step::activate();
_code->show();
if (getData()->codeByTelegram) {
_noTelegramCode->show();
} else {
2016-11-24 19:28:23 +00:00
_callLabel->show();
}
2016-11-24 19:28:23 +00:00
setInnerFocus();
}
2016-11-24 19:28:23 +00:00
void CodeWidget::finished() {
Step::finished();
2019-11-27 08:33:18 +00:00
_checkRequestTimer.cancel();
_callTimer.cancel();
2020-06-11 13:07:14 +00:00
apiClear();
2016-11-24 19:28:23 +00:00
cancelled();
_sentCode.clear();
_code->setText(QString());
}
2016-11-24 19:28:23 +00:00
void CodeWidget::cancelled() {
api().request(base::take(_sentRequest)).cancel();
api().request(base::take(_callRequestId)).cancel();
api().request(MTPauth_CancelCode(
2020-06-11 13:07:14 +00:00
MTP_string(getData()->phone),
MTP_bytes(getData()->phoneHash)
)).send();
}
2016-11-24 19:28:23 +00:00
void CodeWidget::stopCheck() {
2019-11-27 08:33:18 +00:00
_checkRequestTimer.cancel();
}
2019-11-27 08:33:18 +00:00
void CodeWidget::checkRequest() {
auto status = api().instance().state(_sentRequest);
if (status < 0) {
auto leftms = -status;
if (leftms >= 1000) {
if (_sentRequest) {
api().request(base::take(_sentRequest)).cancel();
_sentCode.clear();
}
}
}
if (!_sentRequest && status == MTP::RequestSent) {
stopCheck();
}
}
2016-11-24 19:28:23 +00:00
void CodeWidget::codeSubmitDone(const MTPauth_Authorization &result) {
stopCheck();
_sentRequest = 0;
2019-07-15 13:57:49 +00:00
result.match([&](const MTPDauth_authorization &data) {
if (data.vuser().type() != mtpc_user
|| !data.vuser().c_user().is_self()) {
2019-11-22 09:40:52 +00:00
showError(rpl::single(Lang::Hard::ServerError()));
2019-07-15 13:57:49 +00:00
return;
}
finish(data.vuser());
}, [&](const MTPDauth_authorizationSignUpRequired &data) {
if (const auto terms = data.vterms_of_service()) {
terms->match([&](const MTPDhelp_termsOfService &data) {
2020-06-08 08:03:45 +00:00
getData()->termsLock = Window::TermsLock::FromMTP(
nullptr,
data);
2019-07-15 13:57:49 +00:00
});
} else {
getData()->termsLock = Window::TermsLock();
}
goReplace<SignupWidget>(Animate::Forward);
2019-07-15 13:57:49 +00:00
});
}
2020-06-11 13:07:14 +00:00
void CodeWidget::codeSubmitFail(const RPCError &error) {
if (MTP::isFloodError(error)) {
stopCheck();
_sentRequest = 0;
showCodeError(tr::lng_flood_error());
2020-06-11 13:07:14 +00:00
return;
}
stopCheck();
_sentRequest = 0;
auto &err = error.type();
if (err == qstr("PHONE_NUMBER_INVALID")
|| err == qstr("PHONE_CODE_EXPIRED")
|| err == qstr("PHONE_NUMBER_BANNED")) { // show error
2016-11-24 19:28:23 +00:00
goBack();
} else if (err == qstr("PHONE_CODE_EMPTY") || err == qstr("PHONE_CODE_INVALID")) {
showCodeError(tr::lng_bad_code());
} else if (err == qstr("SESSION_PASSWORD_NEEDED")) {
2019-11-27 08:33:18 +00:00
_checkRequestTimer.callEach(1000);
_sentRequest = api().request(MTPaccount_GetPassword(
2020-06-11 13:07:14 +00:00
)).done([=](const MTPaccount_Password &result) {
gotPassword(result);
}).fail([=](const RPCError &error) {
codeSubmitFail(error);
}).handleFloodErrors().send();
} else if (Logs::DebugEnabled()) { // internal server error
showCodeError(rpl::single(err + ": " + error.description()));
} else {
showCodeError(rpl::single(Lang::Hard::ServerError()));
}
}
2019-11-27 08:33:18 +00:00
void CodeWidget::codeChanged() {
2016-11-24 19:28:23 +00:00
hideError();
2018-11-19 12:15:59 +00:00
submit();
}
2019-11-27 08:33:18 +00:00
void CodeWidget::sendCall() {
2019-11-26 11:10:44 +00:00
if (_callStatus == CallStatus::Waiting) {
2016-11-24 19:28:23 +00:00
if (--_callTimeout <= 0) {
2019-11-26 11:10:44 +00:00
_callStatus = CallStatus::Calling;
2019-11-27 08:33:18 +00:00
_callTimer.cancel();
_callRequestId = api().request(MTPauth_ResendCode(
2020-06-11 13:07:14 +00:00
MTP_string(getData()->phone),
MTP_bytes(getData()->phoneHash)
)).done([=](const MTPauth_SentCode &result) {
callDone(result);
}).send();
2016-03-15 19:38:30 +00:00
} else {
2016-11-24 19:28:23 +00:00
getData()->callStatus = _callStatus;
getData()->callTimeout = _callTimeout;
2016-03-15 19:38:30 +00:00
}
2016-11-24 19:28:23 +00:00
updateCallText();
}
}
2016-11-24 19:28:23 +00:00
void CodeWidget::callDone(const MTPauth_SentCode &v) {
if (v.type() == mtpc_auth_sentCode) {
2018-06-01 07:00:18 +00:00
fillSentCodeData(v.c_auth_sentCode());
2016-11-24 19:28:23 +00:00
_code->setDigitsCountMax(getData()->codeLength);
}
2019-11-26 11:10:44 +00:00
if (_callStatus == CallStatus::Calling) {
_callStatus = CallStatus::Called;
2016-11-24 19:28:23 +00:00
getData()->callStatus = _callStatus;
getData()->callTimeout = _callTimeout;
updateCallText();
}
}
2016-11-24 19:28:23 +00:00
void CodeWidget::gotPassword(const MTPaccount_Password &result) {
2018-08-03 21:48:00 +00:00
Expects(result.type() == mtpc_account_password);
stopCheck();
_sentRequest = 0;
2018-08-03 21:48:00 +00:00
const auto &d = result.c_account_password();
2018-08-10 19:19:46 +00:00
getData()->pwdRequest = Core::ParseCloudPasswordCheckRequest(d);
2019-07-05 13:38:38 +00:00
if (!d.vcurrent_algo() || !d.vsrp_id() || !d.vsrp_B()) {
2018-08-03 21:48:00 +00:00
LOG(("API Error: No current password received on login."));
_code->setFocus();
2018-08-03 21:48:00 +00:00
return;
2018-08-10 19:19:46 +00:00
} else if (!getData()->pwdRequest) {
const auto callback = [=](Fn<void()> &&close) {
2018-08-03 21:48:00 +00:00
Core::UpdateApplication();
close();
2018-08-03 21:48:00 +00:00
};
Ui::show(Box<ConfirmBox>(
2019-06-19 15:09:03 +00:00
tr::lng_passport_app_out_of_date(tr::now),
tr::lng_menu_update(tr::now),
2018-08-03 21:48:00 +00:00
callback));
return;
}
2018-08-03 21:48:00 +00:00
getData()->hasRecovery = d.is_has_recovery();
2019-07-05 13:38:38 +00:00
getData()->pwdHint = qs(d.vhint().value_or_empty());
2018-08-03 21:48:00 +00:00
getData()->pwdNotEmptyPassport = d.is_has_secure_values();
goReplace<PasswordCheckWidget>(Animate::Forward);
}
2016-11-24 19:28:23 +00:00
void CodeWidget::submit() {
2018-11-19 12:15:59 +00:00
const auto text = QString(
_code->getLastText()
).remove(
QRegularExpression("[^\\d]")
).mid(0, getData()->codeLength);
if (_sentRequest
|| _sentCode == text
|| text.size() != getData()->codeLength) {
return;
}
2016-11-24 19:28:23 +00:00
hideError();
2019-11-27 08:33:18 +00:00
_checkRequestTimer.callEach(1000);
2018-11-19 12:15:59 +00:00
_sentCode = text;
2018-08-10 19:19:46 +00:00
getData()->pwdRequest = Core::CloudPasswordCheckRequest();
2016-11-24 19:28:23 +00:00
getData()->hasRecovery = false;
getData()->pwdHint = QString();
getData()->pwdNotEmptyPassport = false;
_sentRequest = api().request(MTPauth_SignIn(
2020-06-11 13:07:14 +00:00
MTP_string(getData()->phone),
MTP_bytes(getData()->phoneHash),
MTP_string(_sentCode)
)).done([=](const MTPauth_Authorization &result) {
codeSubmitDone(result);
}).fail([=](const RPCError &error) {
codeSubmitFail(error);
}).handleFloodErrors().send();
}
2019-11-27 08:33:18 +00:00
void CodeWidget::noTelegramCode() {
2019-07-15 13:57:49 +00:00
if (_noTelegramCodeRequestId) {
return;
}
_noTelegramCodeRequestId = api().request(MTPauth_ResendCode(
2020-06-11 13:07:14 +00:00
MTP_string(getData()->phone),
MTP_bytes(getData()->phoneHash)
)).done([=](const MTPauth_SentCode &result) {
noTelegramCodeDone(result);
}).fail([=](const RPCError &error) {
noTelegramCodeFail(error);
}).handleFloodErrors().send();
}
2016-11-24 19:28:23 +00:00
void CodeWidget::noTelegramCodeDone(const MTPauth_SentCode &result) {
_noTelegramCodeRequestId = 0;
2016-03-15 19:38:30 +00:00
if (result.type() != mtpc_auth_sentCode) {
showCodeError(rpl::single(Lang::Hard::ServerError()));
2016-03-15 19:38:30 +00:00
return;
}
2018-06-01 07:00:18 +00:00
const auto &d = result.c_auth_sentCode();
fillSentCodeData(d);
2016-11-24 19:28:23 +00:00
_code->setDigitsCountMax(getData()->codeLength);
2019-07-05 13:38:38 +00:00
const auto next = d.vnext_type();
if (next && next->type() == mtpc_auth_codeTypeCall) {
2019-11-26 11:10:44 +00:00
getData()->callStatus = CallStatus::Waiting;
2019-07-05 13:38:38 +00:00
getData()->callTimeout = d.vtimeout().value_or(60);
2016-03-15 19:38:30 +00:00
} else {
2019-11-26 11:10:44 +00:00
getData()->callStatus = CallStatus::Disabled;
2016-11-24 19:28:23 +00:00
getData()->callTimeout = 0;
2016-03-15 19:38:30 +00:00
}
2016-11-24 19:28:23 +00:00
getData()->codeByTelegram = false;
updateDescText();
}
2020-06-11 13:07:14 +00:00
void CodeWidget::noTelegramCodeFail(const RPCError &error) {
if (MTP::isFloodError(error)) {
_noTelegramCodeRequestId = 0;
showCodeError(tr::lng_flood_error());
2020-06-11 13:07:14 +00:00
return;
}
_noTelegramCodeRequestId = 0;
if (Logs::DebugEnabled()) { // internal server error
showCodeError(rpl::single(error.type() + ": " + error.description()));
} else {
showCodeError(rpl::single(Lang::Hard::ServerError()));
}
}
2019-11-26 11:10:44 +00:00
} // namespace details
2016-11-24 19:28:23 +00:00
} // namespace Intro