tdesktop/Telegram/SourceFiles/intro/introsignup.cpp

235 lines
5.8 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/introsignup.h"
#include "styles/style_intro.h"
2016-10-31 12:29:26 +00:00
#include "styles/style_boxes.h"
#include "core/file_utilities.h"
2017-04-06 14:38:10 +00:00
#include "boxes/photo_crop_box.h"
#include "boxes/confirm_box.h"
2017-04-13 08:27:10 +00:00
#include "lang/lang_keys.h"
#include "ui/widgets/buttons.h"
#include "ui/widgets/input_fields.h"
2016-11-24 19:28:23 +00:00
#include "ui/widgets/labels.h"
#include "ui/special_buttons.h"
2016-11-24 19:28:23 +00:00
namespace Intro {
SignupWidget::SignupWidget(QWidget *parent, Widget::Data *data) : Step(parent, data)
, _photo(
this,
2018-12-23 17:23:36 +00:00
lang(lng_settings_crop_profile),
Ui::UserpicButton::Role::ChangePhoto,
st::defaultUserpicButton)
, _first(this, st::introName, langFactory(lng_signup_firstname))
, _last(this, st::introName, langFactory(lng_signup_lastname))
, _invertOrder(langFirstNameGoesSecond())
, _checkRequest(this) {
subscribe(Lang::Current().updated(), [this] { refreshLang(); });
if (_invertOrder) {
setTabOrder(_last, _first);
} else {
setTabOrder(_first, _last);
}
connect(_checkRequest, SIGNAL(timeout()), this, SLOT(onCheckRequest()));
2016-11-24 19:28:23 +00:00
setErrorCentered(true);
setTitleText(langFactory(lng_signup_title));
setDescriptionText(langFactory(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() {
MTP::cancel(base::take(_sentRequest));
}
void SignupWidget::stopCheck() {
_checkRequest->stop();
}
2016-11-24 19:28:23 +00:00
void SignupWidget::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 SignupWidget::nameSubmitDone(const MTPauth_Authorization &result) {
stopCheck();
auto &d = result.c_auth_authorization();
2015-10-29 00:16:52 +00:00
if (d.vuser.type() != mtpc_user || !d.vuser.c_user().is_self()) { // wtf?
2017-09-28 20:11:10 +00:00
showError(&Lang::Hard::ServerError);
return;
}
finish(d.vuser, _photo->takeResultImage());
}
2016-11-24 19:28:23 +00:00
bool SignupWidget::nameSubmitFail(const RPCError &error) {
if (MTP::isFloodError(error)) {
stopCheck();
showError(langFactory(lng_flood_error));
if (_invertOrder) {
_first->setFocus();
} else {
_last->setFocus();
}
return true;
}
if (MTP::isDefaultHandledError(error)) return false;
stopCheck();
auto &err = error.type();
if (err == qstr("PHONE_NUMBER_FLOOD")) {
Ui::show(Box<InformBox>(lang(lng_error_phone_flood)));
return true;
} else if (err == qstr("PHONE_NUMBER_INVALID") || err == qstr("PHONE_CODE_EXPIRED") ||
err == qstr("PHONE_CODE_EMPTY") || err == qstr("PHONE_CODE_INVALID") ||
err == qstr("PHONE_NUMBER_OCCUPIED")) {
2016-11-24 19:28:23 +00:00
goBack();
return true;
} else if (err == "FIRSTNAME_INVALID") {
showError(langFactory(lng_bad_name));
_first->setFocus();
return true;
} else if (err == "LASTNAME_INVALID") {
showError(langFactory(lng_bad_name));
_last->setFocus();
return true;
}
if (Logs::DebugEnabled()) { // internal server error
auto text = err + ": " + error.description();
showError([text] { return text; });
} else {
2017-09-28 20:11:10 +00:00
showError(&Lang::Hard::ServerError);
}
if (_invertOrder) {
_last->setFocus();
} else {
_first->setFocus();
}
return false;
}
2016-11-24 19:28:23 +00:00
void SignupWidget::onInputChange() {
hideError();
}
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 = MTP::send(
MTPauth_SignUp(
MTP_string(getData()->phone),
MTP_bytes(getData()->phoneHash),
MTP_string(getData()->code),
MTP_string(_firstName),
MTP_string(_lastName)),
rpcDone(&SignupWidget::nameSubmitDone),
rpcFail(&SignupWidget::nameSubmitFail));
};
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();
}));
}
}
2016-11-24 19:28:23 +00:00
QString SignupWidget::nextButtonText() const {
return lang(lng_intro_finish);
}
2016-11-24 19:28:23 +00:00
} // namespace Intro