tdesktop/Telegram/SourceFiles/intro/intro_signup.cpp

215 lines
5.2 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
*/
2019-11-27 09:45:23 +00:00
#include "intro/intro_signup.h"
#include "boxes/abstract_box.h"
2019-11-27 09:45:23 +00:00
#include "intro/intro_widget.h"
#include "core/file_utilities.h"
2021-10-18 21:36:55 +00:00
#include "ui/boxes/confirm_box.h"
2017-04-13 08:27:10 +00:00
#include "lang/lang_keys.h"
2022-12-19 11:48:24 +00:00
#include "ui/controls/userpic_button.h"
#include "ui/widgets/buttons.h"
#include "ui/widgets/fields/input_field.h"
2016-11-24 19:28:23 +00:00
#include "ui/widgets/labels.h"
2019-11-26 11:10:44 +00:00
#include "styles/style_intro.h"
#include "styles/style_boxes.h"
2016-11-24 19:28:23 +00:00
namespace Intro {
2019-11-26 11:10:44 +00:00
namespace details {
2016-11-24 19:28:23 +00:00
2019-07-24 08:46:23 +00:00
SignupWidget::SignupWidget(
QWidget *parent,
not_null<Main::Account*> account,
2019-11-26 11:10:44 +00:00
not_null<Data*> data)
2019-11-27 08:33:18 +00:00
: Step(parent, account, data)
, _photo(
this,
data->controller,
Ui::UserpicButton::Role::ChoosePhoto,
2019-11-27 08:33:18 +00:00
st::defaultUserpicButton)
, _first(this, st::introName, tr::lng_signup_firstname())
, _last(this, st::introName, tr::lng_signup_lastname())
, _invertOrder(langFirstNameGoesSecond()) {
_photo->showCustomOnChosen();
2020-09-30 09:11:44 +00:00
Lang::Updated(
) | rpl::start_with_next([=] {
refreshLang();
}, lifetime());
if (_invertOrder) {
setTabOrder(_last, _first);
} else {
setTabOrder(_first, _last);
}
2016-11-24 19:28:23 +00:00
setErrorCentered(true);
setTitleText(tr::lng_signup_title());
setDescriptionText(tr::lng_signup_desc());
setMouseTracking(true);
}
2018-06-01 07:00:18 +00:00
void SignupWidget::finishInit() {
showTerms();
}
void SignupWidget::refreshLang() {
_invertOrder = langFirstNameGoesSecond();
if (_invertOrder) {
setTabOrder(_last, _first);
} else {
setTabOrder(_first, _last);
}
updateControlsGeometry();
}
2016-11-24 19:28:23 +00:00
void SignupWidget::resizeEvent(QResizeEvent *e) {
Step::resizeEvent(e);
updateControlsGeometry();
}
2015-12-08 12:33:37 +00:00
void SignupWidget::updateControlsGeometry() {
2016-11-24 19:28:23 +00:00
auto photoRight = contentLeft() + st::introNextButton.width;
auto photoTop = contentTop() + st::introPhotoTop;
_photo->moveToLeft(photoRight - _photo->width(), photoTop);
2015-12-08 12:33:37 +00:00
2016-11-24 19:28:23 +00:00
auto firstTop = contentTop() + st::introStepFieldTop;
auto secondTop = firstTop + st::introName.heightMin + st::introPhoneTop;
2016-11-24 19:28:23 +00:00
if (_invertOrder) {
_last->moveToLeft(contentLeft(), firstTop);
_first->moveToLeft(contentLeft(), secondTop);
2015-12-08 12:33:37 +00:00
} else {
2016-11-24 19:28:23 +00:00
_first->moveToLeft(contentLeft(), firstTop);
_last->moveToLeft(contentLeft(), secondTop);
2015-12-08 12:33:37 +00:00
}
}
2016-11-24 19:28:23 +00:00
void SignupWidget::setInnerFocus() {
if (_invertOrder || _last->hasFocus()) {
_last->setFocusFast();
} else {
_first->setFocusFast();
}
}
2016-11-24 19:28:23 +00:00
void SignupWidget::activate() {
Step::activate();
_first->show();
_last->show();
_photo->show();
setInnerFocus();
}
2016-11-24 19:28:23 +00:00
void SignupWidget::cancelled() {
api().request(base::take(_sentRequest)).cancel();
2016-11-24 19:28:23 +00:00
}
void SignupWidget::nameSubmitDone(const MTPauth_Authorization &result) {
2023-01-10 13:10:34 +00:00
finish(result);
}
2021-03-12 12:48:00 +00:00
void SignupWidget::nameSubmitFail(const MTP::Error &error) {
if (MTP::IsFloodError(error)) {
showError(tr::lng_flood_error());
if (_invertOrder) {
_first->setFocus();
} else {
_last->setFocus();
}
2020-06-11 13:07:14 +00:00
return;
}
auto &err = error.type();
2022-11-26 21:20:17 +00:00
if (err == u"PHONE_NUMBER_FLOOD"_q) {
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
|| err == u"PHONE_NUMBER_BANNED"_q
|| err == u"PHONE_CODE_EXPIRED"_q
|| err == u"PHONE_CODE_EMPTY"_q
|| err == u"PHONE_CODE_INVALID"_q
|| err == u"PHONE_NUMBER_OCCUPIED"_q) {
2016-11-24 19:28:23 +00:00
goBack();
} else if (err == "FIRSTNAME_INVALID") {
showError(tr::lng_bad_name());
_first->setFocus();
} else if (err == "LASTNAME_INVALID") {
showError(tr::lng_bad_name());
_last->setFocus();
} else {
2020-06-11 13:07:14 +00:00
if (Logs::DebugEnabled()) { // internal server error
showError(rpl::single(err + ": " + error.description()));
} else {
showError(rpl::single(Lang::Hard::ServerError()));
}
if (_invertOrder) {
_last->setFocus();
} else {
_first->setFocus();
}
}
}
2016-11-24 19:28:23 +00:00
void SignupWidget::submit() {
2018-06-01 07:00:18 +00:00
if (_sentRequest) {
return;
}
if (_invertOrder) {
2016-11-24 19:28:23 +00:00
if ((_last->hasFocus() || _last->getLastText().trimmed().length()) && !_first->getLastText().trimmed().length()) {
_first->setFocus();
return;
2016-11-24 19:28:23 +00:00
} else if (!_last->getLastText().trimmed().length()) {
_last->setFocus();
return;
}
} else {
2016-11-24 19:28:23 +00:00
if ((_first->hasFocus() || _first->getLastText().trimmed().length()) && !_last->getLastText().trimmed().length()) {
_last->setFocus();
return;
2016-11-24 19:28:23 +00:00
} else if (!_first->getLastText().trimmed().length()) {
_first->setFocus();
return;
}
}
2018-06-01 07:00:18 +00:00
const auto send = [&] {
hideError();
_firstName = _first->getLastText().trimmed();
_lastName = _last->getLastText().trimmed();
_sentRequest = api().request(MTPauth_SignUp(
2024-02-01 08:47:46 +00:00
MTP_flags(0),
2020-06-11 13:07:14 +00:00
MTP_string(getData()->phone),
MTP_bytes(getData()->phoneHash),
MTP_string(_firstName),
MTP_string(_lastName)
)).done([=](const MTPauth_Authorization &result) {
nameSubmitDone(result);
2021-03-12 12:48:00 +00:00
}).fail([=](const MTP::Error &error) {
2020-06-11 13:07:14 +00:00
nameSubmitFail(error);
}).handleFloodErrors().send();
2018-06-01 07:00:18 +00:00
};
if (_termsAccepted
|| getData()->termsLock.text.text.isEmpty()
|| !getData()->termsLock.popup) {
2018-06-01 07:00:18 +00:00
send();
} else {
acceptTerms(crl::guard(this, [=] {
2018-06-01 07:00:18 +00:00
_termsAccepted = true;
send();
}));
}
}
rpl::producer<QString> SignupWidget::nextButtonText() const {
return tr::lng_intro_finish();
}
2016-11-24 19:28:23 +00:00
2019-11-26 11:10:44 +00:00
} // namespace details
2016-11-24 19:28:23 +00:00
} // namespace Intro