| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | /*
 | 
					
						
							|  |  |  | This file is part of Telegram Desktop, | 
					
						
							| 
									
										
										
										
											2018-01-03 13:23:14 +03:00
										 |  |  | the official desktop application for the Telegram messaging service. | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-03 13:23:14 +03:00
										 |  |  | For license and copyright information please follow this link: | 
					
						
							|  |  |  | https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-04-06 17:38:10 +03:00
										 |  |  | #include "boxes/confirm_phone_box.h"
 | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "styles/style_boxes.h"
 | 
					
						
							| 
									
										
										
										
											2017-04-06 17:38:10 +03:00
										 |  |  | #include "boxes/confirm_box.h"
 | 
					
						
							| 
									
										
										
										
											2016-11-11 16:46:04 +03:00
										 |  |  | #include "ui/widgets/buttons.h"
 | 
					
						
							| 
									
										
										
										
											2016-11-15 14:56:49 +03:00
										 |  |  | #include "ui/widgets/input_fields.h"
 | 
					
						
							| 
									
										
										
										
											2016-11-16 13:44:06 +03:00
										 |  |  | #include "ui/widgets/labels.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-23 15:40:59 +02:00
										 |  |  | #include "ui/text/text_utilities.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-05 20:43:21 +03:00
										 |  |  | #include "core/click_handler_types.h" // UrlClickHandler
 | 
					
						
							|  |  |  | #include "base/qthelp_url.h" // qthelp::url_encode
 | 
					
						
							|  |  |  | #include "platform/platform_info.h" // Platform::SystemVersionPretty
 | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | #include "mainwidget.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-06 17:40:08 +01:00
										 |  |  | #include "numbers.h"
 | 
					
						
							| 
									
										
										
										
											2017-04-13 11:27:10 +03:00
										 |  |  | #include "lang/lang_keys.h"
 | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-13 20:07:56 +03:00
										 |  |  | object_ptr<ConfirmPhoneBox> CurrentConfirmPhoneBox = { nullptr }; | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-05 20:43:21 +03:00
										 |  |  | void SendToBannedHelp(const QString &phone) { | 
					
						
							|  |  |  | 	const auto version = QString::fromLatin1(AppVersionStr) | 
					
						
							|  |  |  | 		+ (cAlphaVersion() | 
					
						
							|  |  |  | 			? qsl(" alpha %1").arg(cAlphaVersion()) | 
					
						
							|  |  |  | 			: (AppBetaVersion ? " beta" : "")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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: ") + Platform::SystemVersionPretty() + 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::Open(url); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | } // namespace
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-05 20:43:21 +03:00
										 |  |  | void ShowPhoneBannedError(const QString &phone) { | 
					
						
							|  |  |  | 	const auto box = std::make_shared<QPointer<BoxContent>>(); | 
					
						
							|  |  |  | 	const auto close = [=] { | 
					
						
							|  |  |  | 		if (*box) { | 
					
						
							|  |  |  | 			(*box)->closeBox(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 	*box = Ui::show(Box<ConfirmBox>( | 
					
						
							| 
									
										
										
										
											2019-06-19 17:09:03 +02:00
										 |  |  | 		tr::lng_signin_banned_text(tr::now), | 
					
						
							|  |  |  | 		tr::lng_box_ok(tr::now), | 
					
						
							|  |  |  | 		tr::lng_signin_banned_help(tr::now), | 
					
						
							| 
									
										
										
										
											2019-06-05 20:43:21 +03:00
										 |  |  | 		close, | 
					
						
							|  |  |  | 		[=] { SendToBannedHelp(phone); close(); })); | 
					
						
							| 
									
										
										
										
											2019-08-06 17:40:08 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-06-05 20:43:21 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-06 17:40:08 +01:00
										 |  |  | QString ExtractPhonePrefix(const QString &phone) { | 
					
						
							|  |  |  | 	const auto pattern = phoneNumberParse(phone); | 
					
						
							|  |  |  | 	if (!pattern.isEmpty()) { | 
					
						
							|  |  |  | 		return phone.mid(0, pattern[0]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return QString(); | 
					
						
							| 
									
										
										
										
											2019-06-05 20:43:21 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-19 16:15:59 +04:00
										 |  |  | SentCodeField::SentCodeField( | 
					
						
							|  |  |  | 	QWidget *parent, | 
					
						
							|  |  |  | 	const style::InputField &st, | 
					
						
							| 
									
										
										
										
											2019-06-18 16:07:45 +02:00
										 |  |  | 	rpl::producer<QString> placeholder, | 
					
						
							| 
									
										
										
										
											2018-11-19 16:15:59 +04:00
										 |  |  | 	const QString &val) | 
					
						
							| 
									
										
										
										
											2019-06-18 16:07:45 +02:00
										 |  |  | : Ui::InputField(parent, st, std::move(placeholder), val) { | 
					
						
							| 
									
										
										
										
											2018-11-19 16:15:59 +04:00
										 |  |  | 	connect(this, &Ui::InputField::changed, [this] { fix(); }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SentCodeField::setAutoSubmit(int length, Fn<void()> submitCallback) { | 
					
						
							|  |  |  | 	_autoSubmitLength = length; | 
					
						
							|  |  |  | 	_submitCallback = std::move(submitCallback); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SentCodeField::setChangedCallback(Fn<void()> changedCallback) { | 
					
						
							|  |  |  | 	_changedCallback = std::move(changedCallback); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QString SentCodeField::getDigitsOnly() const { | 
					
						
							|  |  |  | 	return QString( | 
					
						
							|  |  |  | 		getLastText() | 
					
						
							|  |  |  | 	).remove( | 
					
						
							|  |  |  | 		QRegularExpression("[^\\d]") | 
					
						
							|  |  |  | 	); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | void SentCodeField::fix() { | 
					
						
							|  |  |  | 	if (_fixing) return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_fixing = true; | 
					
						
							|  |  |  | 	auto newText = QString(); | 
					
						
							| 
									
										
										
										
											2018-11-19 16:15:59 +04:00
										 |  |  | 	const auto now = getLastText(); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	auto oldPos = textCursor().position(); | 
					
						
							|  |  |  | 	auto newPos = -1; | 
					
						
							|  |  |  | 	auto oldLen = now.size(); | 
					
						
							|  |  |  | 	auto digitCount = 0; | 
					
						
							| 
									
										
										
										
											2018-11-19 16:15:59 +04:00
										 |  |  | 	for (const auto ch : now) { | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 		if (ch.isDigit()) { | 
					
						
							|  |  |  | 			++digitCount; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (_autoSubmitLength > 0 && digitCount > _autoSubmitLength) { | 
					
						
							|  |  |  | 		digitCount = _autoSubmitLength; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-19 16:15:59 +04:00
										 |  |  | 	auto strict = (_autoSubmitLength > 0) | 
					
						
							|  |  |  | 		&& (digitCount == _autoSubmitLength); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	newText.reserve(oldLen); | 
					
						
							|  |  |  | 	int i = 0; | 
					
						
							| 
									
										
										
										
											2018-11-19 16:15:59 +04:00
										 |  |  | 	for (const auto ch : now) { | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 		if (i++ == oldPos) { | 
					
						
							|  |  |  | 			newPos = newText.length(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (ch.isDigit()) { | 
					
						
							|  |  |  | 			if (!digitCount--) { | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			newText += ch; | 
					
						
							|  |  |  | 			if (strict && !digitCount) { | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-11-19 16:15:59 +04:00
										 |  |  | 		} else if (ch == '-') { | 
					
						
							|  |  |  | 			newText += ch; | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (newPos < 0) { | 
					
						
							|  |  |  | 		newPos = newText.length(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (newText != now) { | 
					
						
							| 
									
										
										
										
											2018-11-19 16:15:59 +04:00
										 |  |  | 		setText(newText); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 		setCursorPosition(newPos); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	_fixing = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (_changedCallback) { | 
					
						
							|  |  |  | 		_changedCallback(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (strict && _submitCallback) { | 
					
						
							|  |  |  | 		_submitCallback(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-19 16:15:59 +04:00
										 |  |  | SentCodeCall::SentCodeCall( | 
					
						
							|  |  |  | 	FnMut<void()> callCallback, | 
					
						
							|  |  |  | 	Fn<void()> updateCallback) | 
					
						
							| 
									
										
										
										
											2018-04-09 21:56:07 +04:00
										 |  |  | : _call(std::move(callCallback)) | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | , _update(std::move(updateCallback)) { | 
					
						
							| 
									
										
										
										
											2018-04-09 21:56:07 +04:00
										 |  |  | 	_timer.setCallback([=] { | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 		if (_status.state == State::Waiting) { | 
					
						
							|  |  |  | 			if (--_status.timeout <= 0) { | 
					
						
							|  |  |  | 				_status.state = State::Calling; | 
					
						
							| 
									
										
										
										
											2018-04-09 21:56:07 +04:00
										 |  |  | 				_timer.cancel(); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 				if (_call) { | 
					
						
							|  |  |  | 					_call(); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (_update) { | 
					
						
							|  |  |  | 			_update(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SentCodeCall::setStatus(const Status &status) { | 
					
						
							|  |  |  | 	_status = status; | 
					
						
							|  |  |  | 	if (_status.state == State::Waiting) { | 
					
						
							| 
									
										
										
										
											2018-04-09 21:56:07 +04:00
										 |  |  | 		_timer.callEach(1000); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QString SentCodeCall::getText() const { | 
					
						
							|  |  |  | 	switch (_status.state) { | 
					
						
							|  |  |  | 	case State::Waiting: { | 
					
						
							|  |  |  | 		if (_status.timeout >= 3600) { | 
					
						
							| 
									
										
										
										
											2019-06-19 18:39:25 +02:00
										 |  |  | 			return tr::lng_code_call(tr::now, lt_minutes, qsl("%1:%2").arg(_status.timeout / 3600).arg((_status.timeout / 60) % 60, 2, 10, QChar('0')), lt_seconds, qsl("%1").arg(_status.timeout % 60, 2, 10, QChar('0'))); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-06-19 18:39:25 +02:00
										 |  |  | 		return tr::lng_code_call(tr::now, lt_minutes, QString::number(_status.timeout / 60), lt_seconds, qsl("%1").arg(_status.timeout % 60, 2, 10, QChar('0'))); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	} break; | 
					
						
							| 
									
										
										
										
											2019-06-19 17:09:03 +02:00
										 |  |  | 	case State::Calling: return tr::lng_code_calling(tr::now); | 
					
						
							|  |  |  | 	case State::Called: return tr::lng_code_called(tr::now); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return QString(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | void ConfirmPhoneBox::start(const QString &phone, const QString &hash) { | 
					
						
							| 
									
										
										
										
											2016-12-13 20:07:56 +03:00
										 |  |  | 	if (CurrentConfirmPhoneBox && CurrentConfirmPhoneBox->getPhone() != phone) { | 
					
						
							|  |  |  | 		CurrentConfirmPhoneBox.destroyDelayed(); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-13 20:07:56 +03:00
										 |  |  | 	if (!CurrentConfirmPhoneBox) { | 
					
						
							|  |  |  | 		CurrentConfirmPhoneBox = Box<ConfirmPhoneBox>(phone, hash); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-13 20:07:56 +03:00
										 |  |  | 	CurrentConfirmPhoneBox->checkPhoneAndHash(); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-13 20:07:56 +03:00
										 |  |  | ConfirmPhoneBox::ConfirmPhoneBox(QWidget*, const QString &phone, const QString &hash) | 
					
						
							|  |  |  | : _phone(phone) | 
					
						
							|  |  |  | , _hash(hash) | 
					
						
							| 
									
										
										
										
											2018-04-09 21:56:07 +04:00
										 |  |  | , _call([this] { sendCall(); }, [this] { update(); }) { | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ConfirmPhoneBox::sendCall() { | 
					
						
							|  |  |  | 	MTP::send(MTPauth_ResendCode(MTP_string(_phone), MTP_string(_phoneHash)), rpcDone(&ConfirmPhoneBox::callDone)); | 
					
						
							| 
									
										
										
										
											2016-12-13 20:07:56 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ConfirmPhoneBox::checkPhoneAndHash() { | 
					
						
							|  |  |  | 	if (_sendCodeRequestId) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-01-28 12:58:53 +03:00
										 |  |  | 	_sendCodeRequestId = MTP::send( | 
					
						
							|  |  |  | 		MTPaccount_SendConfirmPhoneCode( | 
					
						
							|  |  |  | 			MTP_string(_hash), | 
					
						
							| 
									
										
										
										
											2019-08-01 10:22:49 +01:00
										 |  |  | 			MTP_codeSettings(MTP_flags(0))), | 
					
						
							| 
									
										
										
										
											2019-01-28 12:58:53 +03:00
										 |  |  | 		rpcDone(&ConfirmPhoneBox::sendCodeDone), | 
					
						
							|  |  |  | 		rpcFail(&ConfirmPhoneBox::sendCodeFail)); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ConfirmPhoneBox::sendCodeDone(const MTPauth_SentCode &result) { | 
					
						
							| 
									
										
										
										
											2019-07-05 15:38:38 +02:00
										 |  |  | 	result.match([&](const MTPDauth_sentCode &data) { | 
					
						
							|  |  |  | 		_sendCodeRequestId = 0; | 
					
						
							|  |  |  | 		_sentCodeLength = data.vtype().match([&](const MTPDauth_sentCodeTypeApp &data) { | 
					
						
							|  |  |  | 			LOG(("Error: should not be in-app code!")); | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 		}, [&](const MTPDauth_sentCodeTypeSms &data) { | 
					
						
							|  |  |  | 			return data.vlength().v; | 
					
						
							|  |  |  | 		}, [&](const MTPDauth_sentCodeTypeCall &data) { | 
					
						
							|  |  |  | 			return data.vlength().v; | 
					
						
							|  |  |  | 		}, [&](const MTPDauth_sentCodeTypeFlashCall &data) { | 
					
						
							|  |  |  | 			LOG(("Error: should not be flashcall!")); | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		_phoneHash = qs(data.vphone_code_hash()); | 
					
						
							|  |  |  | 		if (const auto nextType = data.vnext_type()) { | 
					
						
							|  |  |  | 			if (nextType->type() == mtpc_auth_codeTypeCall) { | 
					
						
							|  |  |  | 				_call.setStatus({ SentCodeCall::State::Waiting, data.vtimeout().value_or(60) }); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		launch(); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool ConfirmPhoneBox::sendCodeFail(const RPCError &error) { | 
					
						
							| 
									
										
										
										
											2017-09-28 23:11:10 +03:00
										 |  |  | 	auto errorText = Lang::Hard::ServerError(); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 	if (MTP::isFloodError(error)) { | 
					
						
							| 
									
										
										
										
											2019-06-19 17:09:03 +02:00
										 |  |  | 		errorText = tr::lng_flood_error(tr::now); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 	} else if (MTP::isDefaultHandledError(error)) { | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} else if (error.code() == 400) { | 
					
						
							| 
									
										
										
										
											2019-06-19 17:09:03 +02:00
										 |  |  | 		errorText = tr::lng_confirm_phone_link_invalid(tr::now); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	_sendCodeRequestId = 0; | 
					
						
							| 
									
										
										
										
											2016-12-13 20:07:56 +03:00
										 |  |  | 	Ui::show(Box<InformBox>(errorText)); | 
					
						
							|  |  |  | 	if (this == CurrentConfirmPhoneBox) { | 
					
						
							|  |  |  | 		CurrentConfirmPhoneBox.destroyDelayed(); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		deleteLater(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ConfirmPhoneBox::launch() { | 
					
						
							| 
									
										
										
										
											2016-12-13 20:07:56 +03:00
										 |  |  | 	if (!CurrentConfirmPhoneBox) return; | 
					
						
							| 
									
										
										
										
											2017-02-21 16:45:56 +03:00
										 |  |  | 	Ui::show(std::move(CurrentConfirmPhoneBox)); | 
					
						
							| 
									
										
										
										
											2016-12-13 20:07:56 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ConfirmPhoneBox::prepare() { | 
					
						
							| 
									
										
										
										
											2019-06-23 15:40:59 +02:00
										 |  |  | 	_about.create( | 
					
						
							|  |  |  | 		this, | 
					
						
							|  |  |  | 		tr::lng_confirm_phone_about( | 
					
						
							|  |  |  | 			lt_phone, | 
					
						
							|  |  |  | 			rpl::single(Ui::Text::Bold(App::formatPhone(_phone))), | 
					
						
							|  |  |  | 			Ui::Text::WithEntities), | 
					
						
							|  |  |  | 		st::confirmPhoneAboutLabel); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-18 16:07:45 +02:00
										 |  |  | 	_code.create(this, st::confirmPhoneCodeField, tr::lng_code_ph()); | 
					
						
							| 
									
										
										
										
											2018-05-31 15:20:28 +03:00
										 |  |  | 	_code->setAutoSubmit(_sentCodeLength, [=] { sendCode(); }); | 
					
						
							|  |  |  | 	_code->setChangedCallback([=] { showError(QString()); }); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-18 17:00:55 +02:00
										 |  |  | 	setTitle(tr::lng_confirm_phone_title()); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-18 18:53:27 +02:00
										 |  |  | 	addButton(tr::lng_confirm_phone_send(), [=] { sendCode(); }); | 
					
						
							|  |  |  | 	addButton(tr::lng_cancel(), [=] { closeBox(); }); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-13 20:07:56 +03:00
										 |  |  | 	setDimensions(st::boxWidth, st::usernamePadding.top() + _code->height() + st::usernameSkip + _about->height() + st::usernameSkip); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-31 15:20:28 +03:00
										 |  |  | 	connect(_code, &Ui::InputField::submitted, [=] { sendCode(); }); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 16:34:58 +03:00
										 |  |  | 	showChildren(); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ConfirmPhoneBox::callDone(const MTPauth_SentCode &result) { | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	_call.callDone(); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-31 15:20:28 +03:00
										 |  |  | void ConfirmPhoneBox::sendCode() { | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 	if (_sendCodeRequestId) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-19 16:15:59 +04:00
										 |  |  | 	const auto code = _code->getDigitsOnly(); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 	if (code.isEmpty()) { | 
					
						
							|  |  |  | 		_code->showError(); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_code->setDisabled(true); | 
					
						
							|  |  |  | 	setFocus(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	showError(QString()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-19 16:15:59 +04:00
										 |  |  | 	_sendCodeRequestId = MTP::send( | 
					
						
							|  |  |  | 		MTPaccount_ConfirmPhone(MTP_string(_phoneHash), MTP_string(code)), | 
					
						
							|  |  |  | 		rpcDone(&ConfirmPhoneBox::confirmDone), | 
					
						
							|  |  |  | 		rpcFail(&ConfirmPhoneBox::confirmFail)); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ConfirmPhoneBox::confirmDone(const MTPBool &result) { | 
					
						
							|  |  |  | 	_sendCodeRequestId = 0; | 
					
						
							| 
									
										
										
										
											2019-06-19 18:39:25 +02:00
										 |  |  | 	Ui::show(Box<InformBox>(tr::lng_confirm_phone_success(tr::now, lt_phone, App::formatPhone(_phone)))); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool ConfirmPhoneBox::confirmFail(const RPCError &error) { | 
					
						
							| 
									
										
										
										
											2017-09-28 23:11:10 +03:00
										 |  |  | 	auto errorText = Lang::Hard::ServerError(); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 	if (MTP::isFloodError(error)) { | 
					
						
							| 
									
										
										
										
											2019-06-19 17:09:03 +02:00
										 |  |  | 		errorText = tr::lng_flood_error(tr::now); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 	} else if (MTP::isDefaultHandledError(error)) { | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		auto &errorType = error.type(); | 
					
						
							|  |  |  | 		if (errorType == qstr("PHONE_CODE_EMPTY") || errorType == qstr("PHONE_CODE_INVALID")) { | 
					
						
							| 
									
										
										
										
											2019-06-19 17:09:03 +02:00
										 |  |  | 			errorText = tr::lng_bad_code(tr::now); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	_sendCodeRequestId = 0; | 
					
						
							|  |  |  | 	_code->setDisabled(false); | 
					
						
							|  |  |  | 	_code->setFocus(); | 
					
						
							|  |  |  | 	showError(errorText); | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ConfirmPhoneBox::showError(const QString &error) { | 
					
						
							|  |  |  | 	_error = error; | 
					
						
							|  |  |  | 	if (!_error.isEmpty()) { | 
					
						
							|  |  |  | 		_code->showError(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ConfirmPhoneBox::paintEvent(QPaintEvent *e) { | 
					
						
							| 
									
										
										
										
											2016-12-13 20:07:56 +03:00
										 |  |  | 	BoxContent::paintEvent(e); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-19 17:47:28 +03:00
										 |  |  | 	Painter p(this); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	p.setFont(st::boxTextFont); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	auto callText = _call.getText(); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 	if (!callText.isEmpty()) { | 
					
						
							|  |  |  | 		p.setPen(st::usernameDefaultFg); | 
					
						
							|  |  |  | 		auto callTextRectLeft = st::usernamePadding.left(); | 
					
						
							|  |  |  | 		auto callTextRectTop = _about->y() + _about->height(); | 
					
						
							|  |  |  | 		auto callTextRectWidth = width() - 2 * st::usernamePadding.left(); | 
					
						
							|  |  |  | 		auto callTextRect = QRect(callTextRectLeft, callTextRectTop, callTextRectWidth, st::usernameSkip); | 
					
						
							|  |  |  | 		p.drawText(callTextRect, callText, style::al_left); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	auto errorText = _error; | 
					
						
							|  |  |  | 	if (errorText.isEmpty()) { | 
					
						
							|  |  |  | 		p.setPen(st::usernameDefaultFg); | 
					
						
							| 
									
										
										
										
											2019-06-19 17:09:03 +02:00
										 |  |  | 		errorText = tr::lng_confirm_phone_enter_code(tr::now); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2016-10-31 15:29:26 +03:00
										 |  |  | 		p.setPen(st::boxTextFgError); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	auto errorTextRectLeft = st::usernamePadding.left(); | 
					
						
							|  |  |  | 	auto errorTextRectTop = _code->y() + _code->height(); | 
					
						
							|  |  |  | 	auto errorTextRectWidth = width() - 2 * st::usernamePadding.left(); | 
					
						
							|  |  |  | 	auto errorTextRect = QRect(errorTextRectLeft, errorTextRectTop, errorTextRectWidth, st::usernameSkip); | 
					
						
							|  |  |  | 	p.drawText(errorTextRect, errorText, style::al_left); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ConfirmPhoneBox::resizeEvent(QResizeEvent *e) { | 
					
						
							| 
									
										
										
										
											2016-12-13 20:07:56 +03:00
										 |  |  | 	BoxContent::resizeEvent(e); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 	_code->resize(width() - st::usernamePadding.left() - st::usernamePadding.right(), _code->height()); | 
					
						
							| 
									
										
										
										
											2016-12-13 20:07:56 +03:00
										 |  |  | 	_code->moveToLeft(st::usernamePadding.left(), st::usernamePadding.top()); | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	_about->moveToLeft(st::usernamePadding.left(), _code->y() + _code->height() + st::usernameSkip); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-13 20:07:56 +03:00
										 |  |  | void ConfirmPhoneBox::setInnerFocus() { | 
					
						
							| 
									
										
										
										
											2016-12-20 16:03:51 +03:00
										 |  |  | 	_code->setFocusFast(); | 
					
						
							| 
									
										
										
										
											2016-11-15 14:56:49 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-28 20:01:08 +03:00
										 |  |  | ConfirmPhoneBox::~ConfirmPhoneBox() { | 
					
						
							|  |  |  | 	if (_sendCodeRequestId) { | 
					
						
							|  |  |  | 		MTP::cancel(_sendCodeRequestId); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |