2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-23 18:57:12 +00:00

434 lines
11 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 12:45:23 +03:00
#include "intro/intro_code.h"
2017-04-13 11:27:10 +03:00
#include "lang/lang_keys.h"
#include "intro/intro_code_input.h"
2019-11-27 12:45:23 +03:00
#include "intro/intro_signup.h"
#include "intro/intro_password_check.h"
#include "boxes/abstract_box.h"
#include "core/file_utilities.h"
2018-08-04 00:48:00 +03:00
#include "core/update_checker.h"
#include "ui/widgets/buttons.h"
2016-11-24 22:28:23 +03:00
#include "ui/widgets/labels.h"
#include "ui/widgets/fields/masked_input_field.h"
2021-07-26 09:32:16 +03:00
#include "ui/text/format_values.h" // Ui::FormatPhone
#include "ui/text/text_utilities.h"
2021-10-19 00:36:55 +03:00
#include "ui/boxes/confirm_box.h"
2020-06-11 17:07:14 +04:00
#include "main/main_account.h"
#include "mtproto/mtp_instance.h"
#include "styles/style_intro.h"
2016-11-24 22:28:23 +03:00
namespace Intro {
2019-11-26 14:10:44 +03:00
namespace details {
2016-11-24 22:28:23 +03:00
2019-07-24 10:46:23 +02:00
CodeWidget::CodeWidget(
QWidget *parent,
not_null<Main::Account*> account,
2019-11-26 14:10:44 +03:00
not_null<Data*> data)
2019-07-24 10:46:23 +02:00
: Step(parent, account, data)
2019-06-19 17:09:03 +02:00
, _noTelegramCode(this, tr::lng_code_no_telegram(tr::now), st::introLink)
, _code(this)
2019-11-27 11:33:18 +03:00
, _callTimer([=] { sendCall(); })
2016-11-24 22:28:23 +03:00
, _callStatus(getData()->callStatus)
, _callTimeout(getData()->callTimeout)
, _callLabel(this, st::introDescription)
2019-11-27 11:33:18 +03:00
, _checkRequestTimer([=] { checkRequest(); }) {
2020-09-30 12:11:44 +03:00
Lang::Updated(
) | rpl::start_with_next([=] {
refreshLang();
}, lifetime());
2019-11-27 11:33:18 +03:00
_noTelegramCode->addClickHandler([=] { noTelegramCode(); });
2016-11-24 22:28:23 +03:00
_code->setDigitsCountMax(getData()->codeLength);
updateDescText();
setTitleText(_isFragment.value(
) | rpl::map([=](bool isFragment) {
return !isFragment
? rpl::single(Ui::FormatPhone(getData()->phone))
: tr::lng_intro_fragment_title();
}) | rpl::flatten_latest());
2022-12-29 02:33:57 +03:00
account->setHandleLoginCode([=](const QString &code) {
_code->setCode(code);
_code->requestCode();
2022-12-29 02:33:57 +03:00
});
_code->codeCollected(
) | rpl::start_with_next([=](const QString &code) {
hideError();
submitCode(code);
}, lifetime());
}
void CodeWidget::refreshLang() {
2019-11-27 11:33:18 +03:00
if (_noTelegramCode) {
_noTelegramCode->setText(tr::lng_code_no_telegram(tr::now));
}
2016-11-24 22:28:23 +03:00
updateDescText();
updateControlsGeometry();
}
2019-11-26 17:27:09 +03:00
int CodeWidget::errorTop() const {
return contentTop() + st::introErrorBelowLinkTop;
}
2016-11-24 22:28:23 +03:00
void CodeWidget::updateDescText() {
const auto byTelegram = getData()->codeByTelegram;
const auto isFragment = !getData()->codeByFragmentUrl.isEmpty();
_isFragment = isFragment;
setDescriptionText(
isFragment
? tr::lng_intro_fragment_about(
lt_phone_number,
rpl::single(TextWithEntities{
.text = Ui::FormatPhone(getData()->phone)
}),
Ui::Text::RichLangValue)
: (byTelegram ? tr::lng_code_from_telegram : tr::lng_code_desc)(
Ui::Text::RichLangValue));
if (getData()->codeByTelegram) {
_noTelegramCode->show();
2019-11-27 11:33:18 +03:00
_callTimer.cancel();
} else {
_noTelegramCode->hide();
2016-11-24 22:28:23 +03:00
_callStatus = getData()->callStatus;
_callTimeout = getData()->callTimeout;
2019-11-27 11:33:18 +03:00
if (_callStatus == CallStatus::Waiting && !_callTimer.isActive()) {
_callTimer.callEach(1000);
}
}
2016-11-24 22:28:23 +03:00
updateCallText();
}
2016-11-24 22:28:23 +03:00
void CodeWidget::updateCallText() {
auto text = ([this]() -> QString {
if (getData()->codeByTelegram) {
return QString();
}
switch (_callStatus) {
2019-11-26 14:10:44 +03:00
case CallStatus::Waiting: {
2016-11-24 22:28:23 +03:00
if (_callTimeout >= 3600) {
return tr::lng_code_call(
tr::now,
lt_minutes,
2022-11-30 00:46:36 +03:00
(u"%1:%2"_q
).arg(_callTimeout / 3600
).arg((_callTimeout / 60) % 60, 2, 10, QChar('0')),
lt_seconds,
2022-11-30 00:46:36 +03:00
u"%1"_q.arg(_callTimeout % 60, 2, 10, QChar('0')));
2016-03-15 22:38:30 +03:00
} else {
return tr::lng_code_call(
tr::now,
lt_minutes,
QString::number(_callTimeout / 60),
lt_seconds,
2022-11-30 00:46:36 +03:00
u"%1"_q.arg(_callTimeout % 60, 2, 10, QChar('0')));
2016-03-15 22:38:30 +03:00
}
} break;
2019-11-26 14:10:44 +03:00
case CallStatus::Calling:
return tr::lng_code_calling(tr::now);
2019-11-26 14:10:44 +03:00
case CallStatus::Called:
return tr::lng_code_called(tr::now);
2016-03-15 22:38:30 +03:00
}
2016-11-24 22:28:23 +03:00
return QString();
})();
_callLabel->setText(text);
_callLabel->setVisible(!text.isEmpty() && !animating());
}
2016-11-24 22:28:23 +03:00
void CodeWidget::resizeEvent(QResizeEvent *e) {
Step::resizeEvent(e);
updateControlsGeometry();
}
void CodeWidget::updateControlsGeometry() {
2016-11-24 22:28:23 +03: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 22:28:23 +03:00
void CodeWidget::setInnerFocus() {
_code->setFocus();
2016-11-24 22:28:23 +03:00
}
2016-11-24 22:28:23 +03:00
void CodeWidget::activate() {
Step::activate();
_code->show();
if (getData()->codeByTelegram) {
_noTelegramCode->show();
} else {
2016-11-24 22:28:23 +03:00
_callLabel->show();
}
2016-11-24 22:28:23 +03:00
setInnerFocus();
}
2016-11-24 22:28:23 +03:00
void CodeWidget::finished() {
Step::finished();
2022-12-29 02:33:57 +03:00
account().setHandleLoginCode(nullptr);
2019-11-27 11:33:18 +03:00
_checkRequestTimer.cancel();
_callTimer.cancel();
2020-06-11 17:07:14 +04:00
apiClear();
2016-11-24 22:28:23 +03:00
cancelled();
_sentCode.clear();
_code->clear();
}
2016-11-24 22:28:23 +03:00
void CodeWidget::cancelled() {
api().request(base::take(_sentRequest)).cancel();
api().request(base::take(_callRequestId)).cancel();
api().request(MTPauth_CancelCode(
2020-06-11 17:07:14 +04:00
MTP_string(getData()->phone),
MTP_bytes(getData()->phoneHash)
)).send();
}
2016-11-24 22:28:23 +03:00
void CodeWidget::stopCheck() {
2019-11-27 11:33:18 +03:00
_checkRequestTimer.cancel();
}
2019-11-27 11:33:18 +03: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 22:28:23 +03:00
void CodeWidget::codeSubmitDone(const MTPauth_Authorization &result) {
stopCheck();
_code->setEnabled(true);
_sentRequest = 0;
2023-01-10 17:10:34 +04:00
finish(result);
}
2021-03-12 16:48:00 +04:00
void CodeWidget::codeSubmitFail(const MTP::Error &error) {
if (MTP::IsFloodError(error)) {
stopCheck();
_code->setEnabled(true);
_code->setFocus();
_sentRequest = 0;
showCodeError(tr::lng_flood_error());
2020-06-11 17:07:14 +04:00
return;
}
stopCheck();
_code->setEnabled(true);
_code->setFocus();
_sentRequest = 0;
auto &err = error.type();
2022-11-27 00:20:17 +03:00
if (err == u"PHONE_NUMBER_INVALID"_q
|| err == u"PHONE_CODE_EXPIRED"_q
|| err == u"PHONE_NUMBER_BANNED"_q) { // show error
2016-11-24 22:28:23 +03:00
goBack();
2022-11-27 00:20:17 +03:00
} else if (err == u"PHONE_CODE_EMPTY"_q || err == u"PHONE_CODE_INVALID"_q) {
showCodeError(tr::lng_bad_code());
2022-11-27 00:20:17 +03:00
} else if (err == u"SESSION_PASSWORD_NEEDED"_q) {
2019-11-27 11:33:18 +03:00
_checkRequestTimer.callEach(1000);
_sentRequest = api().request(MTPaccount_GetPassword(
2020-06-11 17:07:14 +04:00
)).done([=](const MTPaccount_Password &result) {
gotPassword(result);
2021-03-12 16:48:00 +04:00
}).fail([=](const MTP::Error &error) {
2020-06-11 17:07:14 +04:00
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 11:33:18 +03:00
void CodeWidget::sendCall() {
2019-11-26 14:10:44 +03:00
if (_callStatus == CallStatus::Waiting) {
2016-11-24 22:28:23 +03:00
if (--_callTimeout <= 0) {
2019-11-26 14:10:44 +03:00
_callStatus = CallStatus::Calling;
2019-11-27 11:33:18 +03:00
_callTimer.cancel();
_callRequestId = api().request(MTPauth_ResendCode(
2024-05-16 12:54:39 +04:00
MTP_flags(0),
2020-06-11 17:07:14 +04:00
MTP_string(getData()->phone),
2024-05-16 12:54:39 +04:00
MTP_bytes(getData()->phoneHash),
MTPstring() // reason
2020-06-11 17:07:14 +04:00
)).done([=](const MTPauth_SentCode &result) {
callDone(result);
}).send();
2016-03-15 22:38:30 +03:00
} else {
2016-11-24 22:28:23 +03:00
getData()->callStatus = _callStatus;
getData()->callTimeout = _callTimeout;
2016-03-15 22:38:30 +03:00
}
2016-11-24 22:28:23 +03:00
updateCallText();
}
}
2023-01-10 17:10:34 +04:00
void CodeWidget::callDone(const MTPauth_SentCode &result) {
result.match([&](const MTPDauth_sentCode &data) {
fillSentCodeData(data);
2016-11-24 22:28:23 +03:00
_code->setDigitsCountMax(getData()->codeLength);
2023-01-10 17:10:34 +04:00
if (_callStatus == CallStatus::Calling) {
_callStatus = CallStatus::Called;
getData()->callStatus = _callStatus;
getData()->callTimeout = _callTimeout;
updateCallText();
}
}, [&](const MTPDauth_sentCodeSuccess &data) {
finish(data.vauthorization());
});
}
2016-11-24 22:28:23 +03:00
void CodeWidget::gotPassword(const MTPaccount_Password &result) {
2018-08-04 00:48:00 +03:00
Expects(result.type() == mtpc_account_password);
stopCheck();
_sentRequest = 0;
2018-08-04 00:48:00 +03:00
const auto &d = result.c_account_password();
getData()->pwdState = Core::ParseCloudPasswordState(d);
2019-07-05 15:38:38 +02:00
if (!d.vcurrent_algo() || !d.vsrp_id() || !d.vsrp_B()) {
2018-08-04 00:48:00 +03:00
LOG(("API Error: No current password received on login."));
_code->setFocus();
2018-08-04 00:48:00 +03:00
return;
} else if (!getData()->pwdState.hasPassword) {
const auto callback = [=](Fn<void()> &&close) {
2018-08-04 00:48:00 +03:00
Core::UpdateApplication();
close();
2018-08-04 00:48:00 +03:00
};
Ui::show(Ui::MakeConfirmBox({
.text = tr::lng_passport_app_out_of_date(),
.confirmed = callback,
.confirmText = tr::lng_menu_update(),
}));
2018-08-04 00:48:00 +03:00
return;
}
goReplace<PasswordCheckWidget>(Animate::Forward);
}
2016-11-24 22:28:23 +03:00
void CodeWidget::submit() {
if (getData()->codeByFragmentUrl.isEmpty()) {
_code->requestCode();
} else {
File::OpenUrl(getData()->codeByFragmentUrl);
}
}
void CodeWidget::submitCode(const QString &text) {
2018-11-19 16:15:59 +04:00
if (_sentRequest
|| _sentCode == text
|| text.size() != getData()->codeLength) {
return;
}
2016-11-24 22:28:23 +03:00
hideError();
2019-11-27 11:33:18 +03:00
_checkRequestTimer.callEach(1000);
2018-11-19 16:15:59 +04:00
_sentCode = text;
_code->setEnabled(false);
getData()->pwdState = Core::CloudPasswordState();
_sentRequest = api().request(MTPauth_SignIn(
2022-08-12 22:08:59 +03:00
MTP_flags(MTPauth_SignIn::Flag::f_phone_code),
2020-06-11 17:07:14 +04:00
MTP_string(getData()->phone),
MTP_bytes(getData()->phoneHash),
2022-08-12 22:08:59 +03:00
MTP_string(_sentCode),
MTPEmailVerification()
2020-06-11 17:07:14 +04:00
)).done([=](const MTPauth_Authorization &result) {
codeSubmitDone(result);
2021-03-12 16:48:00 +04:00
}).fail([=](const MTP::Error &error) {
2020-06-11 17:07:14 +04:00
codeSubmitFail(error);
}).handleFloodErrors().send();
}
rpl::producer<QString> CodeWidget::nextButtonText() const {
return _isFragment.value(
) | rpl::map([=](bool isFragment) {
return isFragment
? tr::lng_intro_fragment_button()
: Step::nextButtonText();
}) | rpl::flatten_latest();
}
rpl::producer<const style::RoundButton*> CodeWidget::nextButtonStyle() const {
return _isFragment.value(
) | rpl::map([](bool isFragment) {
return isFragment ? &st::introFragmentButton : nullptr;
});
}
2019-11-27 11:33:18 +03:00
void CodeWidget::noTelegramCode() {
if (_noTelegramCodeRequestId) {
return;
}
_noTelegramCodeRequestId = api().request(MTPauth_ResendCode(
2024-05-16 12:54:39 +04:00
MTP_flags(0),
MTP_string(getData()->phone),
2024-05-16 12:54:39 +04:00
MTP_bytes(getData()->phoneHash),
MTPstring() // reason
)).done([=](const MTPauth_SentCode &result) {
noTelegramCodeDone(result);
}).fail([=](const MTP::Error &error) {
noTelegramCodeFail(error);
}).handleFloodErrors().send();
}
2016-11-24 22:28:23 +03:00
void CodeWidget::noTelegramCodeDone(const MTPauth_SentCode &result) {
_noTelegramCodeRequestId = 0;
2023-01-10 17:10:34 +04:00
result.match([&](const MTPDauth_sentCode &data) {
const auto &d = result.c_auth_sentCode();
fillSentCodeData(data);
_code->setDigitsCountMax(getData()->codeLength);
const auto next = data.vnext_type();
if (next && next->type() == mtpc_auth_codeTypeCall) {
getData()->callStatus = CallStatus::Waiting;
getData()->callTimeout = d.vtimeout().value_or(60);
} else {
getData()->callStatus = CallStatus::Disabled;
getData()->callTimeout = 0;
}
getData()->codeByTelegram = false;
updateDescText();
}, [&](const MTPDauth_sentCodeSuccess &data) {
finish(data.vauthorization());
});
}
2021-03-12 16:48:00 +04:00
void CodeWidget::noTelegramCodeFail(const MTP::Error &error) {
if (MTP::IsFloodError(error)) {
_noTelegramCodeRequestId = 0;
showCodeError(tr::lng_flood_error());
2020-06-11 17:07:14 +04:00
return;
} else if (error.type() == u"SEND_CODE_UNAVAILABLE"_q) {
_noTelegramCodeRequestId = 0;
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 14:10:44 +03:00
} // namespace details
2016-11-24 22:28:23 +03:00
} // namespace Intro