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