tdesktop/Telegram/SourceFiles/intro/introcode.cpp

364 lines
10 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 "intro/introcode.h"
2017-04-13 08:27:10 +00:00
#include "lang/lang_keys.h"
#include "application.h"
#include "intro/introsignup.h"
#include "intro/intropwdcheck.h"
#include "ui/widgets/buttons.h"
2016-11-24 19:28:23 +00:00
#include "ui/widgets/labels.h"
#include "styles/style_intro.h"
2016-11-24 19:28:23 +00:00
namespace Intro {
CodeInput::CodeInput(QWidget *parent, const style::InputField &st, Fn<QString()> placeholderFactory) : Ui::MaskedInputField(parent, st, std::move(placeholderFactory)) {
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;
}
}
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);
}
if (strict) emit codeEntered();
}
2016-11-24 19:28:23 +00:00
CodeWidget::CodeWidget(QWidget *parent, Widget::Data *data) : Step(parent, data)
2015-12-08 12:33:37 +00:00
, _noTelegramCode(this, lang(lng_code_no_telegram), st::introLink)
, _code(this, st::introCode, langFactory(lng_code_ph))
, _callTimer(this)
2016-11-24 19:28:23 +00:00
, _callStatus(getData()->callStatus)
, _callTimeout(getData()->callTimeout)
, _callLabel(this, st::introDescription)
, _checkRequest(this) {
subscribe(Lang::Current().updated(), [this] { refreshLang(); });
connect(_code, SIGNAL(changed()), this, SLOT(onInputChange()));
connect(_callTimer, SIGNAL(timeout()), this, SLOT(onSendCall()));
connect(_checkRequest, SIGNAL(timeout()), this, SLOT(onCheckRequest()));
connect(_noTelegramCode, SIGNAL(clicked()), this, SLOT(onNoTelegramCode()));
2016-11-24 19:28:23 +00:00
_code->setDigitsCountMax(getData()->codeLength);
setErrorBelowLink(true);
setTitleText([text = App::formatPhone(getData()->phone)] { return text; });
updateDescText();
}
void CodeWidget::refreshLang() {
if (_noTelegramCode) _noTelegramCode->setText(lang(lng_code_no_telegram));
2016-11-24 19:28:23 +00:00
updateDescText();
updateControlsGeometry();
}
2016-11-24 19:28:23 +00:00
void CodeWidget::updateDescText() {
setDescriptionText(langFactory(getData()->codeByTelegram ? lng_code_telegram : lng_code_desc));
2016-11-24 19:28:23 +00:00
if (getData()->codeByTelegram) {
_noTelegramCode->show();
_callTimer->stop();
} else {
_noTelegramCode->hide();
2016-11-24 19:28:23 +00:00
_callStatus = getData()->callStatus;
_callTimeout = getData()->callTimeout;
if (_callStatus == Widget::Data::CallStatus::Waiting && !_callTimer->isActive()) {
_callTimer->start(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) {
case Widget::Data::CallStatus::Waiting: {
if (_callTimeout >= 3600) {
return lng_code_call(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 {
2016-11-24 19:28:23 +00:00
return lng_code_call(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;
2016-11-24 19:28:23 +00:00
case Widget::Data::CallStatus::Calling: return lang(lng_code_calling);
case Widget::Data::CallStatus::Called: return lang(lng_code_called);
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(Fn<QString()> textFactory) {
if (textFactory) _code->showError();
showError(std::move(textFactory));
}
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();
_checkRequest->stop();
_callTimer->stop();
rpcInvalidate();
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() {
MTP::cancel(base::take(_sentRequest));
MTP::cancel(base::take(_callRequestId));
MTP::send(MTPauth_CancelCode(MTP_string(getData()->phone), MTP_bytes(getData()->phoneHash)));
}
2016-11-24 19:28:23 +00:00
void CodeWidget::stopCheck() {
_checkRequest->stop();
}
2016-11-24 19:28:23 +00:00
void CodeWidget::onCheckRequest() {
auto status = MTP::state(_sentRequest);
if (status < 0) {
auto leftms = -status;
if (leftms >= 1000) {
if (_sentRequest) {
MTP::cancel(base::take(_sentRequest));
_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;
2016-11-24 19:28:23 +00:00
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?
2017-09-28 20:11:10 +00:00
showCodeError(&Lang::Hard::ServerError);
return;
}
2016-11-24 19:28:23 +00:00
cSetLoggedPhoneNumber(getData()->phone);
finish(d.vuser);
}
2016-11-24 19:28:23 +00:00
bool CodeWidget::codeSubmitFail(const RPCError &error) {
if (MTP::isFloodError(error)) {
stopCheck();
_sentRequest = 0;
showCodeError(langFactory(lng_flood_error));
return true;
}
if (MTP::isDefaultHandledError(error)) return false;
stopCheck();
_sentRequest = 0;
auto &err = error.type();
if (err == qstr("PHONE_NUMBER_INVALID") || err == qstr("PHONE_CODE_EXPIRED")) { // show error
2016-11-24 19:28:23 +00:00
goBack();
return true;
} else if (err == qstr("PHONE_CODE_EMPTY") || err == qstr("PHONE_CODE_INVALID")) {
showCodeError(langFactory(lng_bad_code));
return true;
} else if (err == qstr("PHONE_NUMBER_UNOCCUPIED")) { // success, need to signUp
2016-11-24 19:28:23 +00:00
getData()->code = _sentCode;
goReplace(new Intro::SignupWidget(parentWidget(), getData()));
return true;
} else if (err == qstr("SESSION_PASSWORD_NEEDED")) {
2016-11-24 19:28:23 +00:00
getData()->code = _sentCode;
_checkRequest->start(1000);
2016-11-24 19:28:23 +00:00
_sentRequest = MTP::send(MTPaccount_GetPassword(), rpcDone(&CodeWidget::gotPassword), rpcFail(&CodeWidget::codeSubmitFail));
return true;
}
if (Logs::DebugEnabled()) { // internal server error
auto text = err + ": " + error.description();
showCodeError([text] { return text; });
} else {
2017-09-28 20:11:10 +00:00
showCodeError(&Lang::Hard::ServerError);
}
return false;
}
2016-11-24 19:28:23 +00:00
void CodeWidget::onInputChange() {
hideError();
if (_code->getLastText().length() == getData()->codeLength) {
submit();
}
}
2016-11-24 19:28:23 +00:00
void CodeWidget::onSendCall() {
if (_callStatus == Widget::Data::CallStatus::Waiting) {
if (--_callTimeout <= 0) {
_callStatus = Widget::Data::CallStatus::Calling;
_callTimer->stop();
_callRequestId = MTP::send(MTPauth_ResendCode(MTP_string(getData()->phone), MTP_bytes(getData()->phoneHash)), rpcDone(&CodeWidget::callDone));
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);
}
if (_callStatus == Widget::Data::CallStatus::Calling) {
_callStatus = Widget::Data::CallStatus::Called;
getData()->callStatus = _callStatus;
getData()->callTimeout = _callTimeout;
updateCallText();
}
}
2016-11-24 19:28:23 +00:00
void CodeWidget::gotPassword(const MTPaccount_Password &result) {
stopCheck();
_sentRequest = 0;
switch (result.type()) {
2016-11-24 19:28:23 +00:00
case mtpc_account_noPassword: { // should not happen
_code->setFocus();
2016-11-24 19:28:23 +00:00
} break;
case mtpc_account_password: {
2016-11-24 19:28:23 +00:00
auto &d = result.c_account_password();
getData()->pwdSalt = qba(d.vcurrent_salt);
getData()->hasRecovery = d.is_has_recovery();
2016-11-24 19:28:23 +00:00
getData()->pwdHint = qs(d.vhint);
getData()->pwdNotEmptyPassport = d.is_has_secure_values();
2016-11-24 19:28:23 +00:00
goReplace(new Intro::PwdCheckWidget(parentWidget(), getData()));
} break;
}
}
2016-11-24 19:28:23 +00:00
void CodeWidget::submit() {
if (_sentRequest) return;
2016-11-24 19:28:23 +00:00
hideError();
_checkRequest->start(1000);
2016-11-24 19:28:23 +00:00
_sentCode = _code->getLastText();
getData()->pwdSalt = QByteArray();
getData()->hasRecovery = false;
getData()->pwdHint = QString();
getData()->pwdNotEmptyPassport = false;
_sentRequest = MTP::send(MTPauth_SignIn(MTP_string(getData()->phone), MTP_bytes(getData()->phoneHash), MTP_string(_sentCode)), rpcDone(&CodeWidget::codeSubmitDone), rpcFail(&CodeWidget::codeSubmitFail));
}
2016-11-24 19:28:23 +00:00
void CodeWidget::onNoTelegramCode() {
if (_noTelegramCodeRequestId) return;
_noTelegramCodeRequestId = MTP::send(MTPauth_ResendCode(MTP_string(getData()->phone), MTP_bytes(getData()->phoneHash)), rpcDone(&CodeWidget::noTelegramCodeDone), rpcFail(&CodeWidget::noTelegramCodeFail));
}
2016-11-24 19:28:23 +00:00
void CodeWidget::noTelegramCodeDone(const MTPauth_SentCode &result) {
2016-03-15 19:38:30 +00:00
if (result.type() != mtpc_auth_sentCode) {
2017-09-28 20:11:10 +00:00
showCodeError(&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);
2016-03-15 19:38:30 +00:00
if (d.has_next_type() && d.vnext_type.type() == mtpc_auth_codeTypeCall) {
2016-11-24 19:28:23 +00:00
getData()->callStatus = Widget::Data::CallStatus::Waiting;
getData()->callTimeout = d.has_timeout() ? d.vtimeout.v : 60;
2016-03-15 19:38:30 +00:00
} else {
2016-11-24 19:28:23 +00:00
getData()->callStatus = Widget::Data::CallStatus::Disabled;
getData()->callTimeout = 0;
2016-03-15 19:38:30 +00:00
}
2016-11-24 19:28:23 +00:00
getData()->codeByTelegram = false;
updateDescText();
}
2016-11-24 19:28:23 +00:00
bool CodeWidget::noTelegramCodeFail(const RPCError &error) {
if (MTP::isFloodError(error)) {
showCodeError(langFactory(lng_flood_error));
return true;
}
if (MTP::isDefaultHandledError(error)) return false;
if (Logs::DebugEnabled()) { // internal server error
auto text = error.type() + ": " + error.description();
showCodeError([text] { return text; });
} else {
2017-09-28 20:11:10 +00:00
showCodeError(&Lang::Hard::ServerError);
}
return false;
}
2016-11-24 19:28:23 +00:00
} // namespace Intro