2014-05-30 12:53:19 +04:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 13:23:14 +03:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2018-01-03 13:23:14 +03:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 12:53:19 +04:00
|
|
|
*/
|
2019-11-27 12:45:23 +03:00
|
|
|
#include "intro/intro_phone.h"
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2017-04-13 11:27:10 +03:00
|
|
|
#include "lang/lang_keys.h"
|
2019-11-27 12:45:23 +03:00
|
|
|
#include "intro/intro_code.h"
|
|
|
|
#include "intro/intro_qr.h"
|
2016-10-27 17:10:28 +03:00
|
|
|
#include "styles/style_intro.h"
|
2016-11-11 16:46:04 +03:00
|
|
|
#include "ui/widgets/buttons.h"
|
2016-11-16 13:44:06 +03:00
|
|
|
#include "ui/widgets/labels.h"
|
2017-09-30 21:26:45 +03:00
|
|
|
#include "ui/wrap/fade_wrap.h"
|
2022-03-28 13:47:48 +03:00
|
|
|
#include "ui/widgets/fields/special_fields.h"
|
2019-07-24 10:46:23 +02:00
|
|
|
#include "main/main_account.h"
|
2020-06-24 13:32:07 +04:00
|
|
|
#include "main/main_domain.h"
|
2019-11-27 12:45:23 +03:00
|
|
|
#include "main/main_app_config.h"
|
2020-06-24 13:32:07 +04:00
|
|
|
#include "main/main_session.h"
|
|
|
|
#include "data/data_user.h"
|
2021-10-19 00:36:55 +03:00
|
|
|
#include "ui/boxes/confirm_box.h"
|
2023-11-15 03:52:41 +03:00
|
|
|
#include "boxes/abstract_box.h"
|
2021-10-18 19:32:50 +03:00
|
|
|
#include "boxes/phone_banned_box.h"
|
2019-01-21 17:42:21 +04:00
|
|
|
#include "core/application.h"
|
2023-05-02 13:33:19 +04:00
|
|
|
#include "window/window_controller.h"
|
2022-06-09 04:50:00 +03:00
|
|
|
#include "window/window_session_controller.h"
|
2021-11-28 11:25:25 +03:00
|
|
|
#include "countries/countries_instance.h" // Countries::Groups
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2016-11-24 22:28:23 +03:00
|
|
|
namespace Intro {
|
2019-11-26 14:10:44 +03:00
|
|
|
namespace details {
|
2017-11-24 21:41:09 +04:00
|
|
|
namespace {
|
|
|
|
|
2024-03-01 03:50:11 +03:00
|
|
|
[[nodiscard]] bool AllowPhoneAttempt(const QString &phone) {
|
2018-10-05 11:14:00 +03:00
|
|
|
const auto digits = ranges::count_if(
|
|
|
|
phone,
|
|
|
|
[](QChar ch) { return ch.isNumber(); });
|
|
|
|
return (digits > 1);
|
|
|
|
}
|
|
|
|
|
2024-03-01 03:50:11 +03:00
|
|
|
[[nodiscard]] QString DigitsOnly(QString value) {
|
|
|
|
static const auto RegExp = QRegularExpression("[^0-9]");
|
|
|
|
return value.replace(RegExp, QString());
|
|
|
|
}
|
|
|
|
|
2017-11-24 21:41:09 +04:00
|
|
|
} // namespace
|
2016-11-24 22:28:23 +03:00
|
|
|
|
2019-07-24 10:46:23 +02:00
|
|
|
PhoneWidget::PhoneWidget(
|
|
|
|
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)
|
2022-06-09 04:50:00 +03:00
|
|
|
, _country(
|
|
|
|
this,
|
2023-05-02 13:33:19 +04:00
|
|
|
getData()->controller->uiShow(),
|
2022-06-09 04:50:00 +03:00
|
|
|
st::introCountry)
|
2016-11-15 14:56:49 +03:00
|
|
|
, _code(this, st::introCountryCode)
|
2021-11-28 11:25:25 +03:00
|
|
|
, _phone(
|
|
|
|
this,
|
|
|
|
st::introPhone,
|
|
|
|
[](const QString &s) { return Countries::Groups(s); })
|
2019-11-27 11:33:18 +03:00
|
|
|
, _checkRequestTimer([=] { checkRequest(); }) {
|
2021-03-29 18:56:26 +04:00
|
|
|
_phone->frontBackspaceEvent(
|
|
|
|
) | rpl::start_with_next([=](not_null<QKeyEvent*> e) {
|
|
|
|
_code->startErasing(e);
|
|
|
|
}, _code->lifetime());
|
|
|
|
|
2021-09-02 21:55:36 +03:00
|
|
|
_country->codeChanged(
|
|
|
|
) | rpl::start_with_next([=](const QString &code) {
|
2021-03-29 18:56:26 +04:00
|
|
|
_code->codeSelected(code);
|
2021-05-03 12:29:21 +04:00
|
|
|
_phone->chooseCode(code);
|
2021-09-02 21:55:36 +03:00
|
|
|
}, _country->lifetime());
|
2021-03-29 18:56:26 +04:00
|
|
|
_code->codeChanged(
|
|
|
|
) | rpl::start_with_next([=](const QString &code) {
|
|
|
|
_country->onChooseCode(code);
|
|
|
|
_phone->chooseCode(code);
|
|
|
|
}, _code->lifetime());
|
|
|
|
_code->addedToNumber(
|
|
|
|
) | rpl::start_with_next([=](const QString &added) {
|
|
|
|
_phone->addedToNumber(added);
|
|
|
|
}, _phone->lifetime());
|
2019-11-27 11:33:18 +03:00
|
|
|
connect(_phone, &Ui::PhonePartInput::changed, [=] { phoneChanged(); });
|
|
|
|
connect(_code, &Ui::CountryCodeInput::changed, [=] { phoneChanged(); });
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2019-06-18 18:53:27 +02:00
|
|
|
setTitleText(tr::lng_phone_title());
|
|
|
|
setDescriptionText(tr::lng_phone_desc());
|
2021-05-25 16:27:23 +03:00
|
|
|
getData()->updated.events(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
countryChanged();
|
|
|
|
}, lifetime());
|
2016-11-24 22:28:23 +03:00
|
|
|
setErrorCentered(true);
|
2019-11-27 12:45:23 +03:00
|
|
|
setupQrLogin();
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2021-03-23 20:06:59 +04:00
|
|
|
if (!_country->chooseCountry(getData()->country)) {
|
2022-11-30 00:46:36 +03:00
|
|
|
_country->chooseCountry(u"US"_q);
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
2016-11-04 22:50:35 +03:00
|
|
|
_changed = false;
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2019-11-27 12:45:23 +03:00
|
|
|
void PhoneWidget::setupQrLogin() {
|
2021-08-30 20:01:39 +03:00
|
|
|
const auto qrLogin = Ui::CreateChild<Ui::LinkButton>(
|
|
|
|
this,
|
|
|
|
tr::lng_phone_to_qr(tr::now));
|
|
|
|
qrLogin->show();
|
2019-11-27 12:45:23 +03:00
|
|
|
|
2021-08-30 20:01:39 +03:00
|
|
|
DEBUG_LOG(("PhoneWidget.qrLogin link created and shown."));
|
2020-02-12 19:36:05 +04:00
|
|
|
|
2021-08-30 20:01:39 +03:00
|
|
|
rpl::combine(
|
|
|
|
sizeValue(),
|
|
|
|
qrLogin->widthValue()
|
|
|
|
) | rpl::start_with_next([=](QSize size, int qrLoginWidth) {
|
|
|
|
qrLogin->moveToLeft(
|
|
|
|
(size.width() - qrLoginWidth) / 2,
|
|
|
|
contentTop() + st::introQrLoginLinkTop);
|
|
|
|
}, qrLogin->lifetime());
|
2019-11-27 12:45:23 +03:00
|
|
|
|
2021-08-30 20:01:39 +03:00
|
|
|
qrLogin->setClickedCallback([=] {
|
|
|
|
goReplace<QrWidget>(Animate::Forward);
|
|
|
|
});
|
2019-11-27 12:45:23 +03:00
|
|
|
}
|
|
|
|
|
2016-11-24 22:28:23 +03:00
|
|
|
void PhoneWidget::resizeEvent(QResizeEvent *e) {
|
|
|
|
Step::resizeEvent(e);
|
|
|
|
_country->moveToLeft(contentLeft(), contentTop() + st::introStepFieldTop);
|
|
|
|
auto phoneTop = _country->y() + _country->height() + st::introPhoneTop;
|
|
|
|
_code->moveToLeft(contentLeft(), phoneTop);
|
|
|
|
_phone->moveToLeft(contentLeft() + _country->width() - st::introPhone.width, phoneTop);
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2019-06-18 18:53:27 +02:00
|
|
|
void PhoneWidget::showPhoneError(rpl::producer<QString> text) {
|
2016-11-24 22:28:23 +03:00
|
|
|
_phone->showError();
|
2019-06-18 18:53:27 +02:00
|
|
|
showError(std::move(text));
|
2016-11-24 22:28:23 +03:00
|
|
|
}
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2016-11-24 22:28:23 +03:00
|
|
|
void PhoneWidget::hidePhoneError() {
|
|
|
|
hideError();
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2016-11-24 22:28:23 +03:00
|
|
|
void PhoneWidget::countryChanged() {
|
2016-11-04 22:50:35 +03:00
|
|
|
if (!_changed) {
|
2016-11-24 22:28:23 +03:00
|
|
|
selectCountry(getData()->country);
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 11:33:18 +03:00
|
|
|
void PhoneWidget::phoneChanged() {
|
2016-11-04 22:50:35 +03:00
|
|
|
_changed = true;
|
2016-11-24 22:28:23 +03:00
|
|
|
hidePhoneError();
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2016-11-24 22:28:23 +03:00
|
|
|
void PhoneWidget::submit() {
|
2020-06-26 15:28:06 +04:00
|
|
|
if (_sentRequest || isHidden()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2023-02-02 22:49:11 +03:00
|
|
|
{
|
|
|
|
const auto hasCodeButWaitingPhone = _code->hasFocus()
|
|
|
|
&& (_code->getLastText().size() > 1)
|
|
|
|
&& _phone->getLastText().isEmpty();
|
|
|
|
if (hasCodeButWaitingPhone) {
|
|
|
|
_phone->hideError();
|
|
|
|
_phone->setFocus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-10-05 11:14:00 +03:00
|
|
|
const auto phone = fullNumber();
|
|
|
|
if (!AllowPhoneAttempt(phone)) {
|
2019-06-18 18:53:27 +02:00
|
|
|
showPhoneError(tr::lng_bad_phone());
|
2016-11-04 22:50:35 +03:00
|
|
|
_phone->setFocus();
|
2014-05-30 12:53:19 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-26 15:28:06 +04:00
|
|
|
cancelNearestDcRequest();
|
|
|
|
|
2020-06-24 13:32:07 +04:00
|
|
|
// Check if such account is authorized already.
|
2024-03-01 03:50:11 +03:00
|
|
|
const auto phoneDigits = DigitsOnly(phone);
|
2020-06-24 13:32:07 +04:00
|
|
|
for (const auto &[index, existing] : Core::App().domain().accounts()) {
|
|
|
|
const auto raw = existing.get();
|
|
|
|
if (const auto session = raw->maybeSession()) {
|
|
|
|
if (raw->mtp().environment() == account().mtp().environment()
|
2024-03-01 03:50:11 +03:00
|
|
|
&& DigitsOnly(session->user()->phone()) == phoneDigits) {
|
2020-06-24 13:32:07 +04:00
|
|
|
crl::on_main(raw, [=] {
|
|
|
|
Core::App().domain().activate(raw);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-01 10:00:18 +03:00
|
|
|
hidePhoneError();
|
2018-05-30 18:08:12 +03:00
|
|
|
|
2019-11-27 11:33:18 +03:00
|
|
|
_checkRequestTimer.callEach(1000);
|
2018-06-01 10:00:18 +03:00
|
|
|
|
2018-10-05 11:14:00 +03:00
|
|
|
_sentPhone = phone;
|
2020-06-17 13:36:25 +04:00
|
|
|
api().instance().setUserPhone(_sentPhone);
|
|
|
|
_sentRequest = api().request(MTPauth_SendCode(
|
2020-06-11 17:07:14 +04:00
|
|
|
MTP_string(_sentPhone),
|
|
|
|
MTP_int(ApiId),
|
|
|
|
MTP_string(ApiHash),
|
2023-01-10 17:10:34 +04:00
|
|
|
MTP_codeSettings(
|
|
|
|
MTP_flags(0),
|
|
|
|
MTPVector<MTPbytes>(),
|
|
|
|
MTPstring(),
|
|
|
|
MTPBool())
|
2020-06-11 17:07:14 +04:00
|
|
|
)).done([=](const MTPauth_SentCode &result) {
|
|
|
|
phoneSubmitDone(result);
|
2021-03-12 16:48:00 +04:00
|
|
|
}).fail([=](const MTP::Error &error) {
|
2020-06-11 17:07:14 +04:00
|
|
|
phoneSubmitFail(error);
|
|
|
|
}).handleFloodErrors().send();
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2016-11-24 22:28:23 +03:00
|
|
|
void PhoneWidget::stopCheck() {
|
2019-11-27 11:33:18 +03:00
|
|
|
_checkRequestTimer.cancel();
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2019-11-27 11:33:18 +03:00
|
|
|
void PhoneWidget::checkRequest() {
|
2020-06-17 13:36:25 +04:00
|
|
|
auto status = api().instance().state(_sentRequest);
|
2014-05-30 12:53:19 +04:00
|
|
|
if (status < 0) {
|
2016-12-09 21:56:01 +03:00
|
|
|
auto leftms = -status;
|
2014-05-30 12:53:19 +04:00
|
|
|
if (leftms >= 1000) {
|
2020-06-17 13:36:25 +04:00
|
|
|
api().request(base::take(_sentRequest)).cancel();
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
}
|
2016-11-04 22:50:35 +03:00
|
|
|
if (!_sentRequest && status == MTP::RequestSent) {
|
2014-05-30 12:53:19 +04:00
|
|
|
stopCheck();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 22:28:23 +03:00
|
|
|
void PhoneWidget::phoneSubmitDone(const MTPauth_SentCode &result) {
|
2014-05-30 12:53:19 +04:00
|
|
|
stopCheck();
|
2016-11-04 22:50:35 +03:00
|
|
|
_sentRequest = 0;
|
2016-03-14 19:59:18 +03:00
|
|
|
|
2023-01-10 17:10:34 +04:00
|
|
|
result.match([&](const MTPDauth_sentCode &data) {
|
|
|
|
fillSentCodeData(data);
|
2024-03-01 03:50:11 +03:00
|
|
|
getData()->phone = DigitsOnly(_sentPhone);
|
2023-01-10 17:10:34 +04:00
|
|
|
getData()->phoneHash = qba(data.vphone_code_hash());
|
|
|
|
const auto next = data.vnext_type();
|
|
|
|
if (next && next->type() == mtpc_auth_codeTypeCall) {
|
|
|
|
getData()->callStatus = CallStatus::Waiting;
|
|
|
|
getData()->callTimeout = data.vtimeout().value_or(60);
|
|
|
|
} else {
|
|
|
|
getData()->callStatus = CallStatus::Disabled;
|
|
|
|
getData()->callTimeout = 0;
|
|
|
|
}
|
|
|
|
goNext<CodeWidget>();
|
|
|
|
}, [&](const MTPDauth_sentCodeSuccess &data) {
|
|
|
|
finish(data.vauthorization());
|
|
|
|
});
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2021-03-12 16:48:00 +04:00
|
|
|
void PhoneWidget::phoneSubmitFail(const MTP::Error &error) {
|
|
|
|
if (MTP::IsFloodError(error)) {
|
2016-04-08 14:44:35 +04:00
|
|
|
stopCheck();
|
2016-11-04 22:50:35 +03:00
|
|
|
_sentRequest = 0;
|
2019-06-18 18:53:27 +02:00
|
|
|
showPhoneError(tr::lng_flood_error());
|
2020-06-11 17:07:14 +04:00
|
|
|
return;
|
2016-04-08 14:44:35 +04:00
|
|
|
}
|
|
|
|
|
2014-05-30 12:53:19 +04:00
|
|
|
stopCheck();
|
2016-11-04 22:50:35 +03:00
|
|
|
_sentRequest = 0;
|
2016-12-09 21:56:01 +03:00
|
|
|
auto &err = error.type();
|
2022-11-27 00:20:17 +03:00
|
|
|
if (err == u"PHONE_NUMBER_FLOOD"_q) {
|
2022-02-27 11:23:20 +03:00
|
|
|
Ui::show(Ui::MakeInformBox(tr::lng_error_phone_flood()));
|
2022-11-27 00:20:17 +03:00
|
|
|
} else if (err == u"PHONE_NUMBER_INVALID"_q) { // show error
|
2019-06-18 18:53:27 +02:00
|
|
|
showPhoneError(tr::lng_bad_phone());
|
2022-11-27 00:20:17 +03:00
|
|
|
} else if (err == u"PHONE_NUMBER_BANNED"_q) {
|
2021-10-18 19:32:50 +03:00
|
|
|
Ui::ShowPhoneBannedError(getData()->controller, _sentPhone);
|
2020-06-11 17:07:14 +04:00
|
|
|
} else if (Logs::DebugEnabled()) { // internal server error
|
2019-06-18 18:53:27 +02:00
|
|
|
showPhoneError(rpl::single(err + ": " + error.description()));
|
2014-05-30 12:53:19 +04:00
|
|
|
} else {
|
2019-06-18 18:53:27 +02:00
|
|
|
showPhoneError(rpl::single(Lang::Hard::ServerError()));
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 22:28:23 +03:00
|
|
|
QString PhoneWidget::fullNumber() const {
|
|
|
|
return _code->getLastText() + _phone->getLastText();
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2019-08-09 11:06:27 +01:00
|
|
|
void PhoneWidget::selectCountry(const QString &country) {
|
2021-03-23 20:06:59 +04:00
|
|
|
_country->chooseCountry(country);
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2016-11-24 22:28:23 +03:00
|
|
|
void PhoneWidget::setInnerFocus() {
|
2016-12-23 16:21:01 +03:00
|
|
|
_phone->setFocusFast();
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2016-11-24 22:28:23 +03:00
|
|
|
void PhoneWidget::activate() {
|
|
|
|
Step::activate();
|
2019-11-27 12:45:23 +03:00
|
|
|
showChildren();
|
2016-11-24 22:28:23 +03:00
|
|
|
setInnerFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneWidget::finished() {
|
|
|
|
Step::finished();
|
2019-11-27 11:33:18 +03:00
|
|
|
_checkRequestTimer.cancel();
|
2020-06-11 17:07:14 +04:00
|
|
|
apiClear();
|
2016-03-14 19:59:18 +03:00
|
|
|
|
2016-11-24 22:28:23 +03:00
|
|
|
cancelled();
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2016-11-24 22:28:23 +03:00
|
|
|
void PhoneWidget::cancelled() {
|
2020-06-17 13:36:25 +04:00
|
|
|
api().request(base::take(_sentRequest)).cancel();
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2019-11-26 14:10:44 +03:00
|
|
|
} // namespace details
|
2016-11-24 22:28:23 +03:00
|
|
|
} // namespace Intro
|