tdesktop/Telegram/SourceFiles/boxes/change_phone_box.cpp

576 lines
15 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 "boxes/change_phone_box.h"
#include "core/file_utilities.h"
2017-04-13 08:27:10 +00:00
#include "lang/lang_keys.h"
#include "ui/widgets/labels.h"
#include "ui/widgets/sent_code_field.h"
#include "ui/widgets/buttons.h"
#include "ui/wrap/fade_wrap.h"
#include "ui/toast/toast.h"
2021-07-26 06:32:16 +00:00
#include "ui/text/format_values.h" // Ui::FormatPhone
#include "ui/text/text_utilities.h"
#include "ui/widgets/fields/special_fields.h"
2021-10-18 21:36:55 +00:00
#include "ui/boxes/confirm_box.h"
#include "boxes/phone_banned_box.h"
#include "countries/countries_instance.h" // Countries::ExtractPhoneCode.
2022-12-28 23:33:57 +00:00
#include "main/main_account.h"
2019-07-24 11:45:24 +00:00
#include "main/main_session.h"
2019-01-18 12:27:37 +00:00
#include "data/data_session.h"
2019-08-06 16:40:08 +00:00
#include "data/data_user.h"
#include "info/profile/info_profile_values.h"
#include "lottie/lottie_icon.h"
#include "mtproto/sender.h"
#include "apiwrap.h"
#include "window/window_session_controller.h"
2019-09-18 11:19:05 +00:00
#include "styles/style_layers.h"
2019-01-18 12:27:37 +00:00
#include "styles/style_boxes.h"
namespace {
2021-10-20 00:54:41 +00:00
void CreateErrorLabel(
QWidget *parent,
object_ptr<Ui::FadeWrap<Ui::FlatLabel>> &label,
const QString &text,
int x,
int y) {
if (label) {
label->hide(anim::type::normal);
2017-10-01 09:39:07 +00:00
auto saved = label.data();
auto destroy = [old = std::move(label)]() mutable {
old.destroyDelayed();
};
using namespace rpl::mappers;
saved->shownValue()
2017-11-20 12:32:55 +00:00
| rpl::filter(_1 == false)
2017-10-01 09:39:07 +00:00
| rpl::take(1)
| rpl::start_with_done(
std::move(destroy),
saved->lifetime());
}
if (!text.isEmpty()) {
label.create(
parent,
object_ptr<Ui::FlatLabel>(
parent,
text,
st::changePhoneError));
label->hide(anim::type::instant);
label->moveToLeft(x, y);
label->show(anim::type::normal);
}
}
[[nodiscard]] int ErrorSkip() {
return st::boxLittleSkip + st::changePhoneError.style.font->height;
}
} // namespace
namespace Settings {
class ChangePhone::EnterPhone : public Ui::BoxContent {
public:
EnterPhone(QWidget*, not_null<Window::SessionController*> controller);
void setInnerFocus() override {
_phone->setFocusFast();
}
protected:
void prepare() override;
private:
void submit();
2021-10-20 00:54:41 +00:00
void sendPhoneDone(
const MTPauth_SentCode &result,
const QString &phoneNumber);
2021-03-12 12:48:00 +00:00
void sendPhoneFail(const MTP::Error &error, const QString &phoneNumber);
void showError(const QString &text);
void hideError() {
showError(QString());
}
const not_null<Window::SessionController*> _controller;
MTP::Sender _api;
2019-08-06 16:40:08 +00:00
object_ptr<Ui::PhoneInput> _phone = { nullptr };
object_ptr<Ui::FadeWrap<Ui::FlatLabel>> _error = { nullptr };
mtpRequestId _requestId = 0;
};
class ChangePhone::EnterCode : public Ui::BoxContent {
public:
2019-07-24 14:00:30 +00:00
EnterCode(
QWidget*,
not_null<Window::SessionController*> controller,
2019-07-24 14:00:30 +00:00
const QString &phone,
const QString &hash,
const QString &openUrl,
2019-07-24 14:00:30 +00:00
int codeLength,
int callTimeout);
void setInnerFocus() override {
_code->setFocusFast();
}
protected:
void prepare() override;
private:
2022-12-28 23:33:57 +00:00
void submit(const QString &code);
void sendCall();
void updateCall();
2021-03-12 12:48:00 +00:00
void sendCodeFail(const MTP::Error &error);
void showError(const QString &text);
void hideError() {
showError(QString());
}
[[nodiscard]] int countHeight() const;
const not_null<Window::SessionController*> _controller;
MTP::Sender _api;
2019-07-24 14:00:30 +00:00
QString _phone;
QString _hash;
QString _openUrl;
int _codeLength = 0;
int _callTimeout = 0;
2021-10-18 15:48:46 +00:00
object_ptr<Ui::SentCodeField> _code = { nullptr };
object_ptr<Ui::FadeWrap<Ui::FlatLabel>> _error = { nullptr };
object_ptr<Ui::FlatLabel> _callLabel = { nullptr };
object_ptr<Ui::RoundButton> _fragment = { nullptr };
mtpRequestId _requestId = 0;
2021-10-18 15:48:46 +00:00
Ui::SentCodeCall _call;
};
ChangePhone::EnterPhone::EnterPhone(
2019-07-24 14:00:30 +00:00
QWidget*,
not_null<Window::SessionController*> controller)
: _controller(controller)
, _api(&controller->session().mtp()) {
2019-07-24 14:00:30 +00:00
}
void ChangePhone::EnterPhone::prepare() {
setTitle(tr::lng_change_phone_title());
2021-10-20 00:54:41 +00:00
const auto phoneValue = QString();
2019-08-06 16:40:08 +00:00
_phone.create(
this,
st::defaultInputField,
tr::lng_change_phone_new_title(),
Countries::ExtractPhoneCode(_controller->session().user()->phone()),
phoneValue,
[](const QString &s) { return Countries::Groups(s); });
2021-10-20 00:54:41 +00:00
_phone->resize(
st::boxWidth - 2 * st::boxPadding.left(),
_phone->height());
_phone->moveToLeft(st::boxPadding.left(), st::boxLittleSkip);
connect(_phone, &Ui::PhoneInput::submitted, [=] { submit(); });
2021-10-20 00:54:41 +00:00
const auto description = object_ptr<Ui::FlatLabel>(
this,
tr::lng_change_phone_new_description(tr::now),
st::changePhoneLabel);
description->moveToLeft(
st::boxPadding.left(),
_phone->y() + _phone->height() + ErrorSkip() + st::boxLittleSkip);
2021-10-20 00:54:41 +00:00
setDimensions(
st::boxWidth,
description->bottomNoMargins() + st::boxLittleSkip);
addButton(tr::lng_change_phone_new_submit(), [this] { submit(); });
addButton(tr::lng_cancel(), [this] { closeBox(); });
}
void ChangePhone::EnterPhone::submit() {
if (_requestId) {
return;
}
hideError();
2021-10-20 00:54:41 +00:00
const auto phoneNumber = _phone->getLastText().trimmed();
_requestId = _api.request(MTPaccount_SendChangePhoneCode(
MTP_string(phoneNumber),
2023-01-10 13:10:34 +00:00
MTP_codeSettings(
MTP_flags(0),
MTPVector<MTPbytes>(),
MTPstring(),
MTPBool())
)).done([=](const MTPauth_SentCode &result) {
2021-10-20 00:54:41 +00:00
_requestId = 0;
sendPhoneDone(result, phoneNumber);
2021-03-12 12:48:00 +00:00
}).fail([=](const MTP::Error &error) {
2021-10-20 00:54:41 +00:00
_requestId = 0;
sendPhoneFail(error, phoneNumber);
}).handleFloodErrors().send();
}
void ChangePhone::EnterPhone::sendPhoneDone(
const MTPauth_SentCode &result,
const QString &phoneNumber) {
2023-01-10 13:10:34 +00:00
const auto data = result.match([](const MTPDauth_sentCode &data) {
return &data;
}, [](const MTPDauth_sentCodeSuccess &) -> const MTPDauth_sentCode* {
LOG(("API Error: Unexpected auth.sentCodeSuccess "
"(ChangePhone::EnterPhone)."));
return nullptr;
2021-10-20 00:54:41 +00:00
});
2023-01-10 13:10:34 +00:00
if (!data) {
showError(Lang::Hard::ServerError());
return;
}
2022-08-12 19:08:59 +00:00
const auto bad = [&](const char *type) {
LOG(("API Error: Should not be '%1'.").arg(type));
showError(Lang::Hard::ServerError());
return false;
};
auto codeLength = 0;
auto codeByFragmentUrl = QString();
2023-01-10 13:10:34 +00:00
const auto hasLength = data->vtype().match([&](
2021-10-20 00:54:41 +00:00
const MTPDauth_sentCodeTypeApp &typeData) {
LOG(("Error: should not be in-app code!"));
2017-09-28 20:11:10 +00:00
showError(Lang::Hard::ServerError());
2021-10-20 00:54:41 +00:00
return false;
}, [&](const MTPDauth_sentCodeTypeSms &typeData) {
codeLength = typeData.vlength().v;
return true;
2022-11-25 15:57:09 +00:00
}, [&](const MTPDauth_sentCodeTypeFragmentSms &typeData) {
codeLength = typeData.vlength().v;
codeByFragmentUrl = qs(typeData.vurl());
2022-11-25 15:57:09 +00:00
return true;
2021-10-20 00:54:41 +00:00
}, [&](const MTPDauth_sentCodeTypeCall &typeData) {
codeLength = typeData.vlength().v;
return true;
2022-08-12 19:08:59 +00:00
}, [&](const MTPDauth_sentCodeTypeFlashCall &) {
return bad("FlashCall");
}, [&](const MTPDauth_sentCodeTypeMissedCall &) {
return bad("MissedCall");
2023-01-10 13:10:34 +00:00
}, [&](const MTPDauth_sentCodeTypeFirebaseSms &) {
return bad("FirebaseSms");
2022-08-12 19:08:59 +00:00
}, [&](const MTPDauth_sentCodeTypeEmailCode &) {
return bad("EmailCode");
}, [&](const MTPDauth_sentCodeTypeSetUpEmailRequired &) {
return bad("SetUpEmailRequired");
2021-10-20 00:54:41 +00:00
});
if (!hasLength) {
return;
}
2023-01-10 13:10:34 +00:00
const auto phoneCodeHash = qs(data->vphone_code_hash());
2021-10-20 00:54:41 +00:00
const auto callTimeout = [&] {
2023-01-10 13:10:34 +00:00
if (const auto nextType = data->vnext_type()) {
2021-10-20 00:54:41 +00:00
return nextType->match([&](const MTPDauth_sentCodeTypeCall &) {
2023-01-10 13:10:34 +00:00
return data->vtimeout().value_or(60);
2021-10-20 00:54:41 +00:00
}, [](const auto &) {
return 0;
});
2019-07-05 13:38:38 +00:00
}
2021-10-20 00:54:41 +00:00
return 0;
}();
_controller->show(
Box<EnterCode>(
_controller,
phoneNumber,
phoneCodeHash,
codeByFragmentUrl,
codeLength,
callTimeout),
2019-09-18 11:19:05 +00:00
Ui::LayerOption::KeepOther);
}
void ChangePhone::EnterPhone::sendPhoneFail(
2021-10-20 00:54:41 +00:00
const MTP::Error &error,
const QString &phoneNumber) {
2021-03-12 12:48:00 +00:00
if (MTP::IsFloodError(error)) {
showError(tr::lng_flood_error(tr::now));
2022-11-26 21:20:17 +00:00
} else if (error.type() == u"PHONE_NUMBER_INVALID"_q) {
showError(tr::lng_bad_phone(tr::now));
2022-11-26 21:20:17 +00:00
} else if (error.type() == u"PHONE_NUMBER_BANNED"_q) {
Ui::ShowPhoneBannedError(&_controller->window(), phoneNumber);
2022-11-26 21:20:17 +00:00
} else if (error.type() == u"PHONE_NUMBER_OCCUPIED"_q) {
_controller->show(
Ui::MakeInformBox(
tr::lng_change_phone_occupied(
tr::now,
lt_phone,
Ui::FormatPhone(phoneNumber))),
Ui::LayerOption::CloseOther);
} else {
showError(Lang::Hard::ServerError());
}
}
void ChangePhone::EnterPhone::showError(const QString &text) {
2021-10-20 00:54:41 +00:00
CreateErrorLabel(
this,
_error,
text,
st::boxPadding.left(),
_phone->y() + _phone->height() + st::boxLittleSkip);
if (!text.isEmpty()) {
_phone->showError();
}
}
ChangePhone::EnterCode::EnterCode(
2019-07-24 14:00:30 +00:00
QWidget*,
not_null<Window::SessionController*> controller,
2019-07-24 14:00:30 +00:00
const QString &phone,
const QString &hash,
const QString &openUrl,
2019-07-24 14:00:30 +00:00
int codeLength,
int callTimeout)
: _controller(controller)
, _api(&controller->session().mtp())
2019-07-24 14:00:30 +00:00
, _phone(phone)
, _hash(hash)
, _openUrl(openUrl)
, _codeLength(codeLength)
, _callTimeout(callTimeout)
, _call([this] { sendCall(); }, [this] { updateCall(); }) {
}
void ChangePhone::EnterCode::prepare() {
const auto width = st::boxWidth;
setTitle(tr::lng_change_phone_title());
2021-10-20 00:54:41 +00:00
const auto descriptionText = tr::lng_change_phone_code_description(
tr::now,
lt_phone,
2021-07-26 06:32:16 +00:00
Ui::Text::Bold(Ui::FormatPhone(_phone)),
Ui::Text::WithEntities);
2021-10-20 00:54:41 +00:00
const auto description = object_ptr<Ui::FlatLabel>(
this,
rpl::single(descriptionText),
st::changePhoneLabel);
description->moveToLeft(st::boxPadding.left(), 0);
2022-12-28 23:33:57 +00:00
const auto submitInput = [=] { submit(_code->getDigitsOnly()); };
2021-10-20 00:54:41 +00:00
const auto phoneValue = QString();
_code.create(
this,
st::defaultInputField,
tr::lng_change_phone_code_title(),
phoneValue);
2022-12-28 23:33:57 +00:00
_code->setAutoSubmit(_codeLength, submitInput);
_code->setChangedCallback([=] { hideError(); });
_code->resize(width - 2 * st::boxPadding.left(), _code->height());
_code->moveToLeft(st::boxPadding.left(), description->bottomNoMargins());
2022-12-28 23:33:57 +00:00
connect(_code, &Ui::InputField::submitted, submitInput);
if (!_openUrl.isEmpty()) {
_fragment.create(
this,
tr::lng_intro_fragment_button(),
st::fragmentBoxButton);
_fragment->setClickedCallback([=] { File::OpenUrl(_openUrl); });
_fragment->setTextTransform(
Ui::RoundButton::TextTransform::NoTransform);
const auto codeBottom = _code->y() + _code->height();
_fragment->setFullWidth(_code->width());
_fragment->moveToLeft(
(width - _fragment->width()) / 2,
codeBottom + ErrorSkip() + st::boxLittleSkip);
}
2022-12-28 23:33:57 +00:00
_controller->session().account().setHandleLoginCode([=](QString code) {
submit(code);
});
boxClosing(
) | rpl::start_with_next([controller = _controller] {
controller->session().account().setHandleLoginCode(nullptr);
}, lifetime());
setDimensions(width, countHeight());
if (_callTimeout > 0) {
2021-10-18 15:48:46 +00:00
_call.setStatus({ Ui::SentCodeCall::State::Waiting, _callTimeout });
updateCall();
}
2022-12-28 23:33:57 +00:00
addButton(tr::lng_change_phone_new_submit(), submitInput);
addButton(tr::lng_cancel(), [=] { closeBox(); });
}
int ChangePhone::EnterCode::countHeight() const {
return _code->bottomNoMargins()
+ ErrorSkip()
+ 3 * st::boxLittleSkip
+ (_fragment ? _fragment->height() : 0);
}
2022-12-28 23:33:57 +00:00
void ChangePhone::EnterCode::submit(const QString &code) {
if (_requestId) {
return;
}
hideError();
const auto session = &_controller->session();
2019-09-13 12:22:54 +00:00
const auto weak = Ui::MakeWeak(this);
_requestId = session->api().request(MTPaccount_ChangePhone(
MTP_string(_phone),
MTP_string(_hash),
MTP_string(code)
)).done([=, show = Window::Show(_controller)](const MTPUser &result) {
2021-10-20 00:54:41 +00:00
_requestId = 0;
2019-07-24 14:00:30 +00:00
session->data().processUser(result);
if (show.valid()) {
if (weak) {
show.hideLayer();
}
Ui::Toast::Show(
show.toastParent(),
tr::lng_change_phone_success(tr::now));
}
2021-03-12 12:48:00 +00:00
}).fail(crl::guard(this, [=](const MTP::Error &error) {
2021-10-20 00:54:41 +00:00
_requestId = 0;
sendCodeFail(error);
})).handleFloodErrors().send();
}
void ChangePhone::EnterCode::sendCall() {
_api.request(MTPauth_ResendCode(
MTP_string(_phone),
MTP_string(_hash)
2023-01-10 13:10:34 +00:00
)).done([=] {
_call.callDone();
}).send();
}
void ChangePhone::EnterCode::updateCall() {
2021-10-20 00:54:41 +00:00
const auto text = _call.getText();
if (text.isEmpty()) {
_callLabel.destroy();
} else if (!_callLabel) {
_callLabel.create(this, text, st::changePhoneLabel);
2021-10-20 00:54:41 +00:00
_callLabel->moveToLeft(
st::boxPadding.left(),
countHeight() - _callLabel->height());
_callLabel->show();
} else {
_callLabel->setText(text);
}
}
void ChangePhone::EnterCode::showError(const QString &text) {
2021-10-20 00:54:41 +00:00
CreateErrorLabel(
this,
_error,
text,
st::boxPadding.left(),
_code->y() + _code->height() + st::boxLittleSkip);
if (!text.isEmpty()) {
_code->showError();
}
}
void ChangePhone::EnterCode::sendCodeFail(const MTP::Error &error) {
2021-03-12 12:48:00 +00:00
if (MTP::IsFloodError(error)) {
showError(tr::lng_flood_error(tr::now));
2022-11-26 21:20:17 +00:00
} else if (error.type() == u"PHONE_CODE_EMPTY"_q
|| error.type() == u"PHONE_CODE_INVALID"_q) {
showError(tr::lng_bad_code(tr::now));
2022-11-26 21:20:17 +00:00
} else if (error.type() == u"PHONE_CODE_EXPIRED"_q
|| error.type() == u"PHONE_NUMBER_BANNED"_q) {
closeBox(); // Go back to phone input.
2022-11-26 21:20:17 +00:00
} else if (error.type() == u"PHONE_NUMBER_INVALID"_q) {
showError(tr::lng_bad_phone(tr::now));
} else {
showError(Lang::Hard::ServerError());
}
}
ChangePhone::ChangePhone(
QWidget *parent,
not_null<Window::SessionController*> controller)
: Section(parent)
, _controller(controller) {
setupContent();
}
rpl::producer<QString> ChangePhone::title() {
return Info::Profile::PhoneValue(
_controller->session().user()
) | rpl::map([](const TextWithEntities &text) {
return text.text;
});
2019-07-24 14:00:30 +00:00
}
void ChangePhone::setupContent() {
const auto content = Ui::CreateChild<Ui::VerticalLayout>(this);
auto icon = CreateLottieIcon(content, {
.name = u"change_number"_q,
.sizeOverride = {
st::changePhoneIconSize,
st::changePhoneIconSize,
},
}, st::changePhoneIconPadding);
content->add(std::move(icon.widget));
_animate = std::move(icon.animate);
content->add(
object_ptr<Ui::CenterWrap<>>(
content,
object_ptr<Ui::FlatLabel>(
content,
tr::lng_change_phone_button(),
st::changePhoneTitle)),
st::changePhoneTitlePadding);
content->add(
object_ptr<Ui::CenterWrap<>>(
content,
object_ptr<Ui::FlatLabel>(
content,
tr::lng_change_phone_about(Ui::Text::RichLangValue),
st::changePhoneDescription)),
st::changePhoneDescriptionPadding);
const auto button = content->add(
object_ptr<Ui::CenterWrap<Ui::RoundButton>>(
content,
object_ptr<Ui::RoundButton>(
content,
tr::lng_change_phone_button(),
st::changePhoneButton)),
st::changePhoneButtonPadding)->entity();
button->setTextTransform(Ui::RoundButton::TextTransform::NoTransform);
button->setClickedCallback([=] {
auto callback = [=] {
_controller->show(
Box<EnterPhone>(_controller),
Ui::LayerOption::CloseOther);
};
_controller->show(
Ui::MakeConfirmBox({
.text = tr::lng_change_phone_warning(),
.confirmed = std::move(callback),
}),
Ui::LayerOption::CloseOther);
});
Ui::ResizeFitChild(this, content);
}
void ChangePhone::showFinished() {
_animate(anim::repeat::loop);
}
} // namespace Settings