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"
|
2019-09-16 11:14:06 +00:00
|
|
|
#include "ui/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"
|
2019-06-05 17:43:21 +00:00
|
|
|
#include "boxes/confirm_phone_box.h"
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/confirm_box.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/application.h"
|
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)
|
2016-11-04 19:50:35 +00:00
|
|
|
, _country(this, st::introCountry)
|
2016-11-15 11:56:49 +00:00
|
|
|
, _code(this, st::introCountryCode)
|
2016-11-24 19:28:23 +00:00
|
|
|
, _phone(this, st::introPhone)
|
2019-11-27 08:33:18 +00:00
|
|
|
, _checkRequestTimer([=] { checkRequest(); }) {
|
2016-11-04 19:50:35 +00:00
|
|
|
connect(_phone, SIGNAL(voidBackspace(QKeyEvent*)), _code, SLOT(startErasing(QKeyEvent*)));
|
|
|
|
connect(_country, SIGNAL(codeChanged(const QString &)), _code, SLOT(codeSelected(const QString &)));
|
|
|
|
connect(_code, SIGNAL(codeChanged(const QString &)), _country, SLOT(onChooseCode(const QString &)));
|
|
|
|
connect(_code, SIGNAL(codeChanged(const QString &)), _phone, SLOT(onChooseCode(const QString &)));
|
|
|
|
connect(_country, SIGNAL(codeChanged(const QString &)), _phone, SLOT(onChooseCode(const QString &)));
|
|
|
|
connect(_code, SIGNAL(addedToNumber(const QString &)), _phone, SLOT(addedToNumber(const QString &)));
|
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());
|
2019-11-26 11:10:44 +00:00
|
|
|
subscribe(getData()->updated, [=] { countryChanged(); });
|
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
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
if (!_country->onChooseCountry(getData()->country)) {
|
2016-11-04 19:50:35 +00:00
|
|
|
_country->onChooseCountry(qsl("US"));
|
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() {
|
|
|
|
rpl::single(
|
|
|
|
rpl::empty_value()
|
|
|
|
) | rpl::then(
|
|
|
|
account().appConfig().refreshed()
|
|
|
|
) | rpl::map([=] {
|
2020-02-12 15:36:05 +00:00
|
|
|
const auto result = account().appConfig().get<QString>(
|
2019-11-27 09:45:23 +00:00
|
|
|
"qr_login_code",
|
2020-02-12 15:42:59 +00:00
|
|
|
"[not-set]");
|
2020-02-12 15:36:05 +00:00
|
|
|
DEBUG_LOG(("PhoneWidget.qr_login_code: %1").arg(result));
|
|
|
|
return result;
|
2019-11-27 09:45:23 +00:00
|
|
|
}) | rpl::filter([](const QString &value) {
|
|
|
|
return (value != "disabled");
|
|
|
|
}) | rpl::take(1) | rpl::start_with_next([=] {
|
|
|
|
const auto qrLogin = Ui::CreateChild<Ui::LinkButton>(
|
|
|
|
this,
|
|
|
|
tr::lng_phone_to_qr(tr::now));
|
|
|
|
qrLogin->show();
|
|
|
|
|
2020-02-12 15:36:05 +00:00
|
|
|
DEBUG_LOG(("PhoneWidget.qrLogin link created and shown."));
|
|
|
|
|
2019-11-27 09:45:23 +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());
|
|
|
|
|
2020-06-23 14:55:08 +00:00
|
|
|
qrLogin->setClickedCallback([=] {
|
|
|
|
goReplace<QrWidget>(Animate::Forward);
|
|
|
|
});
|
2019-11-27 09:45:23 +00:00
|
|
|
}, lifetime());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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),
|
|
|
|
MTP_codeSettings(MTP_flags(0))
|
|
|
|
)).done([=](const MTPauth_SentCode &result) {
|
|
|
|
phoneSubmitDone(result);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
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
|
|
|
|
|
|
|
if (result.type() != mtpc_auth_sentCode) {
|
2019-06-18 16:53:27 +00:00
|
|
|
showPhoneError(rpl::single(Lang::Hard::ServerError()));
|
2016-03-14 16:59:18 +00:00
|
|
|
return;
|
2014-10-22 18:39:03 +00:00
|
|
|
}
|
2016-03-14 16:59:18 +00:00
|
|
|
|
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
|
|
|
getData()->phone = _sentPhone;
|
2019-07-05 13:38:38 +00:00
|
|
|
getData()->phoneHash = qba(d.vphone_code_hash());
|
|
|
|
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-14 16:59:18 +00:00
|
|
|
}
|
2019-07-24 08:46:23 +00:00
|
|
|
goNext<CodeWidget>();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 13:07:14 +00:00
|
|
|
void PhoneWidget::phoneSubmitFail(const RPCError &error) {
|
2016-04-08 10:44:35 +00:00
|
|
|
if (MTP::isFloodError(error)) {
|
|
|
|
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();
|
2016-12-30 13:53:51 +00:00
|
|
|
if (err == qstr("PHONE_NUMBER_FLOOD")) {
|
2019-06-19 15:09:03 +00:00
|
|
|
Ui::show(Box<InformBox>(tr::lng_error_phone_flood(tr::now)));
|
2016-12-30 13:53:51 +00:00
|
|
|
} else if (err == qstr("PHONE_NUMBER_INVALID")) { // show error
|
2019-06-18 16:53:27 +00:00
|
|
|
showPhoneError(tr::lng_bad_phone());
|
2017-11-24 17:41:09 +00:00
|
|
|
} else if (err == qstr("PHONE_NUMBER_BANNED")) {
|
2019-06-05 17:43:21 +00:00
|
|
|
ShowPhoneBannedError(_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) {
|
|
|
|
_country->onChooseCountry(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
|