tdesktop/Telegram/SourceFiles/intro/introphone.cpp

236 lines
6.1 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/introphone.h"
2017-04-13 08:27:10 +00:00
#include "lang/lang_keys.h"
#include "intro/introcode.h"
2016-10-27 14:10:28 +00:00
#include "styles/style_intro.h"
#include "ui/widgets/buttons.h"
#include "ui/widgets/labels.h"
#include "ui/wrap/fade_wrap.h"
2019-07-24 08:46:23 +00:00
#include "main/main_account.h"
#include "boxes/confirm_phone_box.h"
2017-04-06 14:38:10 +00:00
#include "boxes/confirm_box.h"
#include "core/application.h"
2016-11-24 19:28:23 +00:00
namespace Intro {
namespace {
bool AllowPhoneAttempt(const QString &phone) {
const auto digits = ranges::count_if(
phone,
[](QChar ch) { return ch.isNumber(); });
return (digits > 1);
}
} // 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,
not_null<Widget::Data*> data)
: Step(parent, account, data)
, _country(this, st::introCountry)
, _code(this, st::introCountryCode)
2016-11-24 19:28:23 +00:00
, _phone(this, st::introPhone)
, _checkRequest(this) {
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 &)));
connect(_phone, SIGNAL(changed()), this, SLOT(onInputChange()));
connect(_code, SIGNAL(changed()), this, SLOT(onInputChange()));
connect(_checkRequest, SIGNAL(timeout()), this, SLOT(onCheckRequest()));
setTitleText(tr::lng_phone_title());
setDescriptionText(tr::lng_phone_desc());
2016-11-24 19:28:23 +00:00
subscribe(getData()->updated, [this] { countryChanged(); });
setErrorCentered(true);
2016-11-24 19:28:23 +00:00
if (!_country->onChooseCountry(getData()->country)) {
_country->onChooseCountry(qsl("US"));
}
_changed = false;
2019-07-24 08:46:23 +00:00
account->destroyStaleAuthorizationKeys();
}
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);
updateSignupGeometry();
}
2016-11-24 19:28:23 +00:00
void PhoneWidget::updateSignupGeometry() {
if (_signup) {
_signup->moveToLeft(contentLeft() + st::buttonRadius, contentTop() + st::introDescriptionTop);
}
}
void PhoneWidget::showPhoneError(rpl::producer<QString> text) {
2016-11-24 19:28:23 +00:00
_phone->showError();
showError(std::move(text));
2016-11-24 19:28:23 +00:00
}
2016-11-24 19:28:23 +00:00
void PhoneWidget::hidePhoneError() {
hideError();
if (_signup) {
_signup->hide(anim::type::instant);
2016-11-24 19:28:23 +00:00
showDescription();
}
}
2016-11-24 19:28:23 +00:00
void PhoneWidget::countryChanged() {
if (!_changed) {
2016-11-24 19:28:23 +00:00
selectCountry(getData()->country);
}
}
2016-11-24 19:28:23 +00:00
void PhoneWidget::onInputChange() {
_changed = true;
2016-11-24 19:28:23 +00:00
hidePhoneError();
}
2016-11-24 19:28:23 +00:00
void PhoneWidget::submit() {
if (_sentRequest || isHidden()) return;
const auto phone = fullNumber();
if (!AllowPhoneAttempt(phone)) {
showPhoneError(tr::lng_bad_phone());
_phone->setFocus();
return;
}
2018-06-01 07:00:18 +00:00
hidePhoneError();
2018-05-30 15:08:12 +00:00
2018-06-01 07:00:18 +00:00
_checkRequest->start(1000);
_sentPhone = phone;
2019-07-24 08:46:23 +00:00
account().mtp()->setUserPhone(_sentPhone);
2018-06-01 07:00:18 +00:00
_sentRequest = MTP::send(
MTPauth_SendCode(
MTP_string(_sentPhone),
MTP_int(ApiId),
2019-01-28 09:58:53 +00:00
MTP_string(ApiHash),
MTP_codeSettings(
MTP_flags(0),
MTPstring())),
2018-06-01 07:00:18 +00:00
rpcDone(&PhoneWidget::phoneSubmitDone),
rpcFail(&PhoneWidget::phoneSubmitFail));
}
2016-11-24 19:28:23 +00:00
void PhoneWidget::stopCheck() {
_checkRequest->stop();
}
2016-11-24 19:28:23 +00:00
void PhoneWidget::onCheckRequest() {
auto status = MTP::state(_sentRequest);
if (status < 0) {
auto leftms = -status;
if (leftms >= 1000) {
MTP::cancel(base::take(_sentRequest));
}
}
if (!_sentRequest && status == MTP::RequestSent) {
stopCheck();
}
}
2016-11-24 19:28:23 +00:00
void PhoneWidget::phoneSubmitDone(const MTPauth_SentCode &result) {
stopCheck();
_sentRequest = 0;
if (result.type() != mtpc_auth_sentCode) {
showPhoneError(rpl::single(Lang::Hard::ServerError()));
return;
2014-10-22 18:39:03 +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) {
2016-11-24 19:28:23 +00:00
getData()->callStatus = Widget::Data::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 {
2016-11-24 19:28:23 +00:00
getData()->callStatus = Widget::Data::CallStatus::Disabled;
getData()->callTimeout = 0;
}
2019-07-24 08:46:23 +00:00
goNext<CodeWidget>();
}
2016-11-24 19:28:23 +00:00
bool PhoneWidget::phoneSubmitFail(const RPCError &error) {
if (MTP::isFloodError(error)) {
stopCheck();
_sentRequest = 0;
showPhoneError(tr::lng_flood_error());
return true;
}
if (MTP::isDefaultHandledError(error)) return false;
stopCheck();
_sentRequest = 0;
auto &err = error.type();
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)));
return true;
} else if (err == qstr("PHONE_NUMBER_INVALID")) { // show error
showPhoneError(tr::lng_bad_phone());
return true;
} else if (err == qstr("PHONE_NUMBER_BANNED")) {
ShowPhoneBannedError(_sentPhone);
return true;
}
if (Logs::DebugEnabled()) { // internal server error
showPhoneError(rpl::single(err + ": " + error.description()));
} else {
showPhoneError(rpl::single(Lang::Hard::ServerError()));
}
return false;
}
2016-11-24 19:28:23 +00:00
QString PhoneWidget::fullNumber() const {
return _code->getLastText() + _phone->getLastText();
}
2016-11-24 19:28:23 +00:00
void PhoneWidget::selectCountry(const QString &c) {
_country->onChooseCountry(c);
}
2016-11-24 19:28:23 +00:00
void PhoneWidget::setInnerFocus() {
_phone->setFocusFast();
}
2016-11-24 19:28:23 +00:00
void PhoneWidget::activate() {
Step::activate();
_country->show();
_phone->show();
_code->show();
setInnerFocus();
}
void PhoneWidget::finished() {
Step::finished();
_checkRequest->stop();
rpcInvalidate();
2016-11-24 19:28:23 +00:00
cancelled();
}
2016-11-24 19:28:23 +00:00
void PhoneWidget::cancelled() {
MTP::cancel(base::take(_sentRequest));
}
2016-11-24 19:28:23 +00:00
} // namespace Intro