tdesktop/Telegram/SourceFiles/intro/introphone.cpp

299 lines
8.7 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 "application.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"
2016-11-24 19:28:23 +00:00
#include "core/click_handler_types.h"
2017-04-06 14:38:10 +00:00
#include "boxes/confirm_box.h"
#include "base/qthelp_url.h"
#include "platform/platform_specific.h"
#include "messenger.h"
2016-11-24 19:28:23 +00:00
namespace Intro {
namespace {
void SendToBannedHelp(const QString &phone) {
const auto version = QString::fromLatin1(AppVersionStr.c_str())
+ (cAlphaVersion() ? " alpha" : "")
+ (cBetaVersion() ? qsl(" beta %1").arg(cBetaVersion()) : QString());
const auto subject = qsl("Banned phone number: ") + phone;
const auto body = qsl("\
I'm trying to use my mobile phone number: ") + phone + qsl("\n\
But Telegram says it's banned. Please help.\n\
\n\
App version: ") + version + qsl("\n\
OS version: ") + cPlatformString() + qsl("\n\
Locale: ") + Platform::SystemLanguage();
const auto url = "mailto:?to="
+ qthelp::url_encode("login@stel.com")
+ "&subject="
+ qthelp::url_encode(subject)
+ "&body="
+ qthelp::url_encode(body);
UrlClickHandler::doOpen(url);
}
} // namespace
2016-11-24 19:28:23 +00:00
PhoneWidget::PhoneWidget(QWidget *parent, Widget::Data *data) : Step(parent, 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(langFactory(lng_phone_title));
setDescriptionText(langFactory(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;
Messenger::Instance().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(Fn<QString()> textFactory) {
2016-11-24 19:28:23 +00:00
_phone->showError();
showError(std::move(textFactory));
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();
}
}
//void PhoneWidget::showSignup() {
// showPhoneError(langFactory(lng_bad_phone_noreg));
// if (!_signup) {
// auto signupText = lng_phone_notreg(lt_link_start, textcmdStartLink(1), lt_link_end, textcmdStopLink(), lt_signup_start, textcmdStartLink(2), lt_signup_end, textcmdStopLink());
// auto inner = object_ptr<Ui::FlatLabel>(this, signupText, Ui::FlatLabel::InitType::Rich, st::introDescription);
// _signup.create(this, std::move(inner));
// _signup->entity()->setLink(1, std::make_shared<UrlClickHandler>(qsl("https://telegram.org"), false));
// _signup->entity()->setLink(2, std::make_shared<LambdaClickHandler>([this] {
// toSignUp();
// }));
// _signup->hide(anim::type::instant);
// updateSignupGeometry();
// }
// _signup->show(anim::type::normal);
// hideDescription();
//}
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;
if (!App::isValidPhone(fullNumber())) {
showPhoneError(langFactory(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 = fullNumber();
Messenger::Instance().mtp()->setUserPhone(_sentPhone);
//_sentRequest = MTP::send(MTPauth_CheckPhone(MTP_string(_sentPhone)), rpcDone(&PhoneWidget::phoneCheckDone), rpcFail(&PhoneWidget::phoneSubmitFail));
_sentRequest = MTP::send(
MTPauth_SendCode(
MTP_flags(0),
MTP_string(_sentPhone),
MTPBool(),
MTP_int(ApiId),
MTP_string(ApiHash)),
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();
}
}
//
//void PhoneWidget::phoneCheckDone(const MTPauth_CheckedPhone &result) {
// stopCheck();
//
// auto &d = result.c_auth_checkedPhone();
// if (mtpIsTrue(d.vphone_registered)) {
// hidePhoneError();
//
// _checkRequest->start(1000);
//
// _sentRequest = MTP::send(MTPauth_SendCode(MTP_flags(0), MTP_string(_sentPhone), MTPBool(), MTP_int(ApiId), MTP_string(ApiHash)), rpcDone(&PhoneWidget::phoneSubmitDone), rpcFail(&PhoneWidget::phoneSubmitFail));
// } else {
// showSignup();
// _sentRequest = 0;
// }
//}
2016-11-24 19:28:23 +00:00
void PhoneWidget::phoneSubmitDone(const MTPauth_SentCode &result) {
stopCheck();
_sentRequest = 0;
if (result.type() != mtpc_auth_sentCode) {
2017-09-28 20:11:10 +00:00
showPhoneError(&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;
getData()->phoneHash = qba(d.vphone_code_hash);
2016-11-24 19:28:23 +00:00
getData()->phoneIsRegistered = d.is_phone_registered();
2016-03-15 19:38:30 +00:00
if (d.has_next_type() && d.vnext_type.type() == mtpc_auth_codeTypeCall) {
2016-11-24 19:28:23 +00:00
getData()->callStatus = Widget::Data::CallStatus::Waiting;
getData()->callTimeout = d.has_timeout() ? d.vtimeout.v : 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;
}
2016-11-24 19:28:23 +00:00
goNext(new Intro::CodeWidget(parentWidget(), getData()));
}
//void PhoneWidget::toSignUp() {
// hideError(); // Hide error, but leave the signup label visible.
//
// _checkRequest->start(1000);
//
// _sentRequest = MTP::send(MTPauth_SendCode(MTP_flags(0), MTP_string(_sentPhone), MTPBool(), MTP_int(ApiId), MTP_string(ApiHash)), rpcDone(&PhoneWidget::phoneSubmitDone), rpcFail(&PhoneWidget::phoneSubmitFail));
//}
2016-11-24 19:28:23 +00:00
bool PhoneWidget::phoneSubmitFail(const RPCError &error) {
if (MTP::isFloodError(error)) {
stopCheck();
_sentRequest = 0;
showPhoneError(langFactory(lng_flood_error));
return true;
}
if (MTP::isDefaultHandledError(error)) return false;
stopCheck();
_sentRequest = 0;
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")) { // show error
showPhoneError(langFactory(lng_bad_phone));
return true;
} else if (err == qstr("PHONE_NUMBER_BANNED")) {
const auto phone = _sentPhone;
Ui::show(Box<ConfirmBox>(
lang(lng_signin_banned_text),
lang(lng_box_ok),
lang(lng_signin_banned_help),
[] { Ui::hideLayer(); },
[phone] { SendToBannedHelp(phone); Ui::hideLayer(); }));
return true;
}
if (Logs::DebugEnabled()) { // internal server error
auto text = err + ": " + error.description();
showPhoneError([text] { return text; });
} else {
2017-09-28 20:11:10 +00:00
showPhoneError(&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