| 
									
										
										
										
											2017-03-21 14:42:04 +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. | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +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
 | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | */ | 
					
						
							|  |  |  | #include "boxes/change_phone_box.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-13 11:27:10 +03:00
										 |  |  | #include "lang/lang_keys.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | #include "ui/widgets/labels.h"
 | 
					
						
							|  |  |  | #include "ui/widgets/input_fields.h"
 | 
					
						
							| 
									
										
										
										
											2017-09-30 21:26:45 +03:00
										 |  |  | #include "ui/wrap/fade_wrap.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | #include "ui/toast/toast.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-12 22:11:41 +02:00
										 |  |  | #include "ui/text/text_utilities.h"
 | 
					
						
							| 
									
										
										
										
											2019-09-16 14:14:06 +03:00
										 |  |  | #include "ui/special_fields.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-12 22:11:41 +02:00
										 |  |  | #include "boxes/confirm_phone_box.h"
 | 
					
						
							| 
									
										
										
										
											2017-04-06 17:38:10 +03:00
										 |  |  | #include "boxes/confirm_box.h"
 | 
					
						
							| 
									
										
										
										
											2019-07-24 13:45:24 +02:00
										 |  |  | #include "main/main_session.h"
 | 
					
						
							| 
									
										
										
										
											2019-01-18 16:27:37 +04:00
										 |  |  | #include "data/data_session.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-06 17:40:08 +01:00
										 |  |  | #include "data/data_user.h"
 | 
					
						
							| 
									
										
										
										
											2019-09-13 09:06:02 +03:00
										 |  |  | #include "app.h"
 | 
					
						
							| 
									
										
										
										
											2019-09-18 14:19:05 +03:00
										 |  |  | #include "styles/style_layers.h"
 | 
					
						
							| 
									
										
										
										
											2019-01-18 16:27:37 +04:00
										 |  |  | #include "styles/style_boxes.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-30 21:26:45 +03:00
										 |  |  | void createErrorLabel( | 
					
						
							|  |  |  | 		QWidget *parent, | 
					
						
							|  |  |  | 		object_ptr<Ui::FadeWrap<Ui::FlatLabel>> &label, | 
					
						
							|  |  |  | 		const QString &text, | 
					
						
							|  |  |  | 		int x, | 
					
						
							|  |  |  | 		int y) { | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	if (label) { | 
					
						
							| 
									
										
										
										
											2017-09-30 22:20:40 +03:00
										 |  |  | 		label->hide(anim::type::normal); | 
					
						
							| 
									
										
										
										
											2017-10-01 12:39:07 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		auto saved = label.data(); | 
					
						
							|  |  |  | 		auto destroy = [old = std::move(label)]() mutable { | 
					
						
							|  |  |  | 			old.destroyDelayed(); | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		using namespace rpl::mappers; | 
					
						
							|  |  |  | 		saved->shownValue() | 
					
						
							| 
									
										
										
										
											2017-11-20 16:32:55 +04:00
										 |  |  | 			| rpl::filter(_1 == false) | 
					
						
							| 
									
										
										
										
											2017-10-01 12:39:07 +03:00
										 |  |  | 			| rpl::take(1) | 
					
						
							|  |  |  | 			| rpl::start_with_done( | 
					
						
							|  |  |  | 				std::move(destroy), | 
					
						
							|  |  |  | 				saved->lifetime()); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if (!text.isEmpty()) { | 
					
						
							| 
									
										
										
										
											2017-09-30 21:26:45 +03:00
										 |  |  | 		label.create( | 
					
						
							|  |  |  | 			parent, | 
					
						
							|  |  |  | 			object_ptr<Ui::FlatLabel>( | 
					
						
							|  |  |  | 				parent, | 
					
						
							|  |  |  | 				text, | 
					
						
							|  |  |  | 				st::changePhoneError)); | 
					
						
							| 
									
										
										
										
											2017-09-30 22:20:40 +03:00
										 |  |  | 		label->hide(anim::type::instant); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 		label->moveToLeft(x, y); | 
					
						
							| 
									
										
										
										
											2017-09-30 22:20:40 +03:00
										 |  |  | 		label->show(anim::type::normal); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // namespace
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-18 14:19:05 +03:00
										 |  |  | class ChangePhoneBox::EnterPhone : public Ui::BoxContent { | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2019-07-24 16:00:30 +02:00
										 |  |  | 	EnterPhone(QWidget*, not_null<Main::Session*> session); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	void setInnerFocus() override { | 
					
						
							|  |  |  | 		_phone->setFocusFast(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  | 	void prepare() override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | 	void submit(); | 
					
						
							|  |  |  | 	void sendPhoneDone(const QString &phoneNumber, const MTPauth_SentCode &result); | 
					
						
							|  |  |  | 	bool sendPhoneFail(const QString &phoneNumber, const RPCError &error); | 
					
						
							|  |  |  | 	void showError(const QString &text); | 
					
						
							|  |  |  | 	void hideError() { | 
					
						
							|  |  |  | 		showError(QString()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-24 16:00:30 +02:00
										 |  |  | 	const not_null<Main::Session*> _session; | 
					
						
							| 
									
										
										
										
											2019-08-06 17:40:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	object_ptr<Ui::PhoneInput> _phone = { nullptr }; | 
					
						
							| 
									
										
										
										
											2017-09-30 21:26:45 +03:00
										 |  |  | 	object_ptr<Ui::FadeWrap<Ui::FlatLabel>> _error = { nullptr }; | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	mtpRequestId _requestId = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-18 14:19:05 +03:00
										 |  |  | class ChangePhoneBox::EnterCode : public Ui::BoxContent { | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2019-07-24 16:00:30 +02:00
										 |  |  | 	EnterCode( | 
					
						
							|  |  |  | 		QWidget*, | 
					
						
							|  |  |  | 		not_null<Main::Session*> session, | 
					
						
							|  |  |  | 		const QString &phone, | 
					
						
							|  |  |  | 		const QString &hash, | 
					
						
							|  |  |  | 		int codeLength, | 
					
						
							|  |  |  | 		int callTimeout); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	void setInnerFocus() override { | 
					
						
							|  |  |  | 		_code->setFocusFast(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  | 	void prepare() override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | 	void submit(); | 
					
						
							|  |  |  | 	void sendCall(); | 
					
						
							|  |  |  | 	void updateCall(); | 
					
						
							|  |  |  | 	bool sendCodeFail(const RPCError &error); | 
					
						
							|  |  |  | 	void showError(const QString &text); | 
					
						
							|  |  |  | 	void hideError() { | 
					
						
							|  |  |  | 		showError(QString()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	int countHeight(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-24 16:00:30 +02:00
										 |  |  | 	const not_null<Main::Session*> _session; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	QString _phone; | 
					
						
							|  |  |  | 	QString _hash; | 
					
						
							|  |  |  | 	int _codeLength = 0; | 
					
						
							|  |  |  | 	int _callTimeout = 0; | 
					
						
							|  |  |  | 	object_ptr<SentCodeField> _code = { nullptr }; | 
					
						
							| 
									
										
										
										
											2017-09-30 21:26:45 +03:00
										 |  |  | 	object_ptr<Ui::FadeWrap<Ui::FlatLabel>> _error = { nullptr }; | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	object_ptr<Ui::FlatLabel> _callLabel = { nullptr }; | 
					
						
							|  |  |  | 	mtpRequestId _requestId = 0; | 
					
						
							|  |  |  | 	SentCodeCall _call; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-24 16:00:30 +02:00
										 |  |  | ChangePhoneBox::EnterPhone::EnterPhone( | 
					
						
							|  |  |  | 	QWidget*, | 
					
						
							|  |  |  | 	not_null<Main::Session*> session) | 
					
						
							|  |  |  | : _session(session) { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | void ChangePhoneBox::EnterPhone::prepare() { | 
					
						
							| 
									
										
										
										
											2019-06-18 17:00:55 +02:00
										 |  |  | 	setTitle(tr::lng_change_phone_title()); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	auto phoneValue = QString(); | 
					
						
							| 
									
										
										
										
											2019-08-06 17:40:08 +01:00
										 |  |  | 	_phone.create( | 
					
						
							|  |  |  | 		this, | 
					
						
							|  |  |  | 		st::defaultInputField, | 
					
						
							|  |  |  | 		tr::lng_change_phone_new_title(), | 
					
						
							|  |  |  | 		ExtractPhonePrefix(_session->user()->phone()), | 
					
						
							|  |  |  | 		phoneValue); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	_phone->resize(st::boxWidth - 2 * st::boxPadding.left(), _phone->height()); | 
					
						
							|  |  |  | 	_phone->moveToLeft(st::boxPadding.left(), st::boxLittleSkip); | 
					
						
							| 
									
										
										
										
											2018-05-31 15:20:28 +03:00
										 |  |  | 	connect(_phone, &Ui::PhoneInput::submitted, [=] { submit(); }); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-19 17:09:03 +02:00
										 |  |  | 	auto description = object_ptr<Ui::FlatLabel>(this, tr::lng_change_phone_new_description(tr::now), st::changePhoneLabel); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	auto errorSkip = st::boxLittleSkip + st::changePhoneError.style.font->height; | 
					
						
							|  |  |  | 	description->moveToLeft(st::boxPadding.left(), _phone->y() + _phone->height() + errorSkip + st::boxLittleSkip); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	setDimensions(st::boxWidth, description->bottomNoMargins() + st::boxLittleSkip); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-18 18:53:27 +02:00
										 |  |  | 	addButton(tr::lng_change_phone_new_submit(), [this] { submit(); }); | 
					
						
							|  |  |  | 	addButton(tr::lng_cancel(), [this] { closeBox(); }); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ChangePhoneBox::EnterPhone::submit() { | 
					
						
							|  |  |  | 	if (_requestId) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	hideError(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	auto phoneNumber = _phone->getLastText().trimmed(); | 
					
						
							| 
									
										
										
										
											2019-01-28 12:58:53 +03:00
										 |  |  | 	_requestId = MTP::send( | 
					
						
							|  |  |  | 		MTPaccount_SendChangePhoneCode( | 
					
						
							|  |  |  | 			MTP_string(phoneNumber), | 
					
						
							| 
									
										
										
										
											2019-08-01 10:22:49 +01:00
										 |  |  | 			MTP_codeSettings(MTP_flags(0))), | 
					
						
							| 
									
										
										
										
											2019-01-28 12:58:53 +03:00
										 |  |  | 		rpcDone(crl::guard(this, [=]( | 
					
						
							|  |  |  | 				const MTPauth_SentCode &result) { | 
					
						
							|  |  |  | 			return sendPhoneDone(phoneNumber, result); | 
					
						
							|  |  |  | 		})), rpcFail(crl::guard(this, [=]( | 
					
						
							|  |  |  | 				const RPCError &error) { | 
					
						
							|  |  |  | 			return sendPhoneFail(phoneNumber, error); | 
					
						
							|  |  |  | 		}))); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ChangePhoneBox::EnterPhone::sendPhoneDone(const QString &phoneNumber, const MTPauth_SentCode &result) { | 
					
						
							|  |  |  | 	Expects(result.type() == mtpc_auth_sentCode); | 
					
						
							|  |  |  | 	_requestId = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	auto codeLength = 0; | 
					
						
							|  |  |  | 	auto &data = result.c_auth_sentCode(); | 
					
						
							| 
									
										
										
										
											2019-07-05 15:38:38 +02:00
										 |  |  | 	switch (data.vtype().type()) { | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	case mtpc_auth_sentCodeTypeApp: | 
					
						
							|  |  |  | 		LOG(("Error: should not be in-app code!")); | 
					
						
							| 
									
										
										
										
											2017-09-28 23:11:10 +03:00
										 |  |  | 		showError(Lang::Hard::ServerError()); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2019-07-05 15:38:38 +02:00
										 |  |  | 	case mtpc_auth_sentCodeTypeSms: codeLength = data.vtype().c_auth_sentCodeTypeSms().vlength().v; break; | 
					
						
							|  |  |  | 	case mtpc_auth_sentCodeTypeCall: codeLength = data.vtype().c_auth_sentCodeTypeCall().vlength().v; break; | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	case mtpc_auth_sentCodeTypeFlashCall: | 
					
						
							|  |  |  | 		LOG(("Error: should not be flashcall!")); | 
					
						
							| 
									
										
										
										
											2017-09-28 23:11:10 +03:00
										 |  |  | 		showError(Lang::Hard::ServerError()); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-07-05 15:38:38 +02:00
										 |  |  | 	auto phoneCodeHash = qs(data.vphone_code_hash()); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	auto callTimeout = 0; | 
					
						
							| 
									
										
										
										
											2019-07-05 15:38:38 +02:00
										 |  |  | 	if (const auto nextType = data.vnext_type()) { | 
					
						
							|  |  |  | 		if (nextType->type() == mtpc_auth_codeTypeCall) { | 
					
						
							|  |  |  | 			callTimeout = data.vtimeout().value_or(60); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-09-15 20:34:41 +03:00
										 |  |  | 	Ui::show( | 
					
						
							|  |  |  | 		Box<EnterCode>( | 
					
						
							| 
									
										
										
										
											2019-07-24 16:00:30 +02:00
										 |  |  | 			_session, | 
					
						
							| 
									
										
										
										
											2017-09-15 20:34:41 +03:00
										 |  |  | 			phoneNumber, | 
					
						
							|  |  |  | 			phoneCodeHash, | 
					
						
							|  |  |  | 			codeLength, | 
					
						
							|  |  |  | 			callTimeout), | 
					
						
							| 
									
										
										
										
											2019-09-18 14:19:05 +03:00
										 |  |  | 		Ui::LayerOption::KeepOther); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool ChangePhoneBox::EnterPhone::sendPhoneFail(const QString &phoneNumber, const RPCError &error) { | 
					
						
							| 
									
										
										
										
											2017-09-28 23:11:10 +03:00
										 |  |  | 	auto errorText = Lang::Hard::ServerError(); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	if (MTP::isFloodError(error)) { | 
					
						
							| 
									
										
										
										
											2019-06-19 17:09:03 +02:00
										 |  |  | 		errorText = tr::lng_flood_error(tr::now); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	} else if (MTP::isDefaultHandledError(error)) { | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} else if (error.type() == qstr("PHONE_NUMBER_INVALID")) { | 
					
						
							| 
									
										
										
										
											2019-06-19 17:09:03 +02:00
										 |  |  | 		errorText = tr::lng_bad_phone(tr::now); | 
					
						
							| 
									
										
										
										
											2019-06-05 20:43:21 +03:00
										 |  |  | 	} else if (error.type() == qstr("PHONE_NUMBER_BANNED")) { | 
					
						
							|  |  |  | 		ShowPhoneBannedError(phoneNumber); | 
					
						
							|  |  |  | 		_requestId = 0; | 
					
						
							|  |  |  | 		return true; | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	} else if (error.type() == qstr("PHONE_NUMBER_OCCUPIED")) { | 
					
						
							| 
									
										
										
										
											2019-06-05 20:43:21 +03:00
										 |  |  | 		Ui::show(Box<InformBox>( | 
					
						
							| 
									
										
										
										
											2019-06-19 18:39:25 +02:00
										 |  |  | 			tr::lng_change_phone_occupied( | 
					
						
							|  |  |  | 				tr::now, | 
					
						
							| 
									
										
										
										
											2019-06-05 20:43:21 +03:00
										 |  |  | 				lt_phone, | 
					
						
							|  |  |  | 				App::formatPhone(phoneNumber)), | 
					
						
							| 
									
										
										
										
											2019-06-19 17:09:03 +02:00
										 |  |  | 			tr::lng_box_ok(tr::now))); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 		_requestId = 0; | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	showError(errorText); | 
					
						
							|  |  |  | 	_requestId = 0; | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ChangePhoneBox::EnterPhone::showError(const QString &text) { | 
					
						
							|  |  |  | 	createErrorLabel(this, _error, text, st::boxPadding.left(), _phone->y() + _phone->height() + st::boxLittleSkip); | 
					
						
							|  |  |  | 	if (!text.isEmpty()) { | 
					
						
							|  |  |  | 		_phone->showError(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-24 16:00:30 +02:00
										 |  |  | ChangePhoneBox::EnterCode::EnterCode( | 
					
						
							|  |  |  | 	QWidget*, | 
					
						
							|  |  |  | 	not_null<Main::Session*> session, | 
					
						
							|  |  |  | 	const QString &phone, | 
					
						
							|  |  |  | 	const QString &hash, | 
					
						
							|  |  |  | 	int codeLength, | 
					
						
							|  |  |  | 	int callTimeout) | 
					
						
							|  |  |  | : _session(session) | 
					
						
							|  |  |  | , _phone(phone) | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | , _hash(hash) | 
					
						
							|  |  |  | , _codeLength(codeLength) | 
					
						
							|  |  |  | , _callTimeout(callTimeout) | 
					
						
							| 
									
										
										
										
											2018-04-09 21:56:07 +04:00
										 |  |  | , _call([this] { sendCall(); }, [this] { updateCall(); }) { | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ChangePhoneBox::EnterCode::prepare() { | 
					
						
							| 
									
										
										
										
											2019-06-18 17:00:55 +02:00
										 |  |  | 	setTitle(tr::lng_change_phone_title()); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-18 18:53:27 +02:00
										 |  |  | 	auto descriptionText = tr::lng_change_phone_code_description( | 
					
						
							|  |  |  | 		tr::now, | 
					
						
							| 
									
										
										
										
											2019-06-12 22:11:41 +02:00
										 |  |  | 		lt_phone, | 
					
						
							| 
									
										
										
										
											2019-06-18 18:53:27 +02:00
										 |  |  | 		Ui::Text::Bold(App::formatPhone(_phone)), | 
					
						
							|  |  |  | 		Ui::Text::WithEntities); | 
					
						
							| 
									
										
										
										
											2019-06-12 22:11:41 +02:00
										 |  |  | 	auto description = object_ptr<Ui::FlatLabel>(this, rpl::single(descriptionText), st::changePhoneLabel); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	description->moveToLeft(st::boxPadding.left(), 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	auto phoneValue = QString(); | 
					
						
							| 
									
										
										
										
											2019-06-18 16:07:45 +02:00
										 |  |  | 	_code.create(this, st::defaultInputField, tr::lng_change_phone_code_title(), phoneValue); | 
					
						
							| 
									
										
										
										
											2018-05-31 15:20:28 +03:00
										 |  |  | 	_code->setAutoSubmit(_codeLength, [=] { submit(); }); | 
					
						
							|  |  |  | 	_code->setChangedCallback([=] { hideError(); }); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	_code->resize(st::boxWidth - 2 * st::boxPadding.left(), _code->height()); | 
					
						
							|  |  |  | 	_code->moveToLeft(st::boxPadding.left(), description->bottomNoMargins()); | 
					
						
							| 
									
										
										
										
											2018-05-31 15:20:28 +03:00
										 |  |  | 	connect(_code, &Ui::InputField::submitted, [=] { submit(); }); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	setDimensions(st::boxWidth, countHeight()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (_callTimeout > 0) { | 
					
						
							|  |  |  | 		_call.setStatus({ SentCodeCall::State::Waiting, _callTimeout }); | 
					
						
							|  |  |  | 		updateCall(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-18 18:53:27 +02:00
										 |  |  | 	addButton(tr::lng_change_phone_new_submit(), [=] { submit(); }); | 
					
						
							|  |  |  | 	addButton(tr::lng_cancel(), [=] { closeBox(); }); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int ChangePhoneBox::EnterCode::countHeight() { | 
					
						
							|  |  |  | 	auto errorSkip = st::boxLittleSkip + st::changePhoneError.style.font->height; | 
					
						
							|  |  |  | 	return _code->bottomNoMargins() + errorSkip + 3 * st::boxLittleSkip; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ChangePhoneBox::EnterCode::submit() { | 
					
						
							|  |  |  | 	if (_requestId) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	hideError(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-24 16:00:30 +02:00
										 |  |  | 	const auto session = _session; | 
					
						
							| 
									
										
										
										
											2018-11-19 16:15:59 +04:00
										 |  |  | 	const auto code = _code->getDigitsOnly(); | 
					
						
							| 
									
										
										
										
											2019-09-13 15:22:54 +03:00
										 |  |  | 	const auto weak = Ui::MakeWeak(this); | 
					
						
							| 
									
										
										
										
											2017-11-30 22:04:13 +04:00
										 |  |  | 	_requestId = MTP::send(MTPaccount_ChangePhone( | 
					
						
							|  |  |  | 		MTP_string(_phone), | 
					
						
							|  |  |  | 		MTP_string(_hash), | 
					
						
							|  |  |  | 		MTP_string(code) | 
					
						
							| 
									
										
										
										
											2019-07-24 16:00:30 +02:00
										 |  |  | 	), rpcDone([=](const MTPUser &result) { | 
					
						
							|  |  |  | 		session->data().processUser(result); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 		if (weak) { | 
					
						
							|  |  |  | 			Ui::hideLayer(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-06-19 17:09:03 +02:00
										 |  |  | 		Ui::Toast::Show(tr::lng_change_phone_success(tr::now)); | 
					
						
							| 
									
										
										
										
											2018-06-04 18:35:11 +03:00
										 |  |  | 	}), rpcFail(crl::guard(this, [this](const RPCError &error) { | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 		return sendCodeFail(error); | 
					
						
							|  |  |  | 	}))); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ChangePhoneBox::EnterCode::sendCall() { | 
					
						
							| 
									
										
										
										
											2018-06-04 18:35:11 +03:00
										 |  |  | 	MTP::send(MTPauth_ResendCode(MTP_string(_phone), MTP_string(_hash)), rpcDone(crl::guard(this, [this] { | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 		_call.callDone(); | 
					
						
							|  |  |  | 	}))); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ChangePhoneBox::EnterCode::updateCall() { | 
					
						
							|  |  |  | 	auto text = _call.getText(); | 
					
						
							|  |  |  | 	if (text.isEmpty()) { | 
					
						
							|  |  |  | 		_callLabel.destroy(); | 
					
						
							|  |  |  | 	} else if (!_callLabel) { | 
					
						
							| 
									
										
										
										
											2019-06-12 22:11:41 +02:00
										 |  |  | 		_callLabel.create(this, text, st::changePhoneLabel); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 		_callLabel->moveToLeft(st::boxPadding.left(), countHeight() - _callLabel->height()); | 
					
						
							|  |  |  | 		_callLabel->show(); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		_callLabel->setText(text); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ChangePhoneBox::EnterCode::showError(const QString &text) { | 
					
						
							|  |  |  | 	createErrorLabel(this, _error, text, st::boxPadding.left(), _code->y() + _code->height() + st::boxLittleSkip); | 
					
						
							|  |  |  | 	if (!text.isEmpty()) { | 
					
						
							|  |  |  | 		_code->showError(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool ChangePhoneBox::EnterCode::sendCodeFail(const RPCError &error) { | 
					
						
							| 
									
										
										
										
											2017-09-28 23:11:10 +03:00
										 |  |  | 	auto errorText = Lang::Hard::ServerError(); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	if (MTP::isFloodError(error)) { | 
					
						
							| 
									
										
										
										
											2019-06-19 17:09:03 +02:00
										 |  |  | 		errorText = tr::lng_flood_error(tr::now); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	} else if (MTP::isDefaultHandledError(error)) { | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} else if (error.type() == qstr("PHONE_CODE_EMPTY") || error.type() == qstr("PHONE_CODE_INVALID")) { | 
					
						
							| 
									
										
										
										
											2019-06-19 17:09:03 +02:00
										 |  |  | 		errorText = tr::lng_bad_code(tr::now); | 
					
						
							| 
									
										
										
										
											2019-06-05 20:43:21 +03:00
										 |  |  | 	} else if (error.type() == qstr("PHONE_CODE_EXPIRED") | 
					
						
							|  |  |  | 		|| error.type() == qstr("PHONE_NUMBER_BANNED")) { | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 		closeBox(); // Go back to phone input.
 | 
					
						
							|  |  |  | 		_requestId = 0; | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} else if (error.type() == qstr("PHONE_NUMBER_INVALID")) { | 
					
						
							| 
									
										
										
										
											2019-06-19 17:09:03 +02:00
										 |  |  | 		errorText = tr::lng_bad_phone(tr::now); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	_requestId = 0; | 
					
						
							|  |  |  | 	showError(errorText); | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-24 16:00:30 +02:00
										 |  |  | ChangePhoneBox::ChangePhoneBox(QWidget*, not_null<Main::Session*> session) | 
					
						
							|  |  |  | : _session(session) { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | void ChangePhoneBox::prepare() { | 
					
						
							| 
									
										
										
										
											2019-07-24 16:00:30 +02:00
										 |  |  | 	const auto session = _session; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-18 17:00:55 +02:00
										 |  |  | 	setTitle(tr::lng_change_phone_title()); | 
					
						
							| 
									
										
										
										
											2019-07-24 16:00:30 +02:00
										 |  |  | 	addButton(tr::lng_change_phone_button(), [=] { | 
					
						
							|  |  |  | 		Ui::show(Box<ConfirmBox>(tr::lng_change_phone_warning(tr::now), [=] { | 
					
						
							|  |  |  | 			Ui::show(Box<EnterPhone>(session)); | 
					
						
							| 
									
										
										
										
											2017-03-23 17:58:28 +03:00
										 |  |  | 		})); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2019-06-18 18:53:27 +02:00
										 |  |  | 	addButton(tr::lng_cancel(), [this] { | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 		closeBox(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-12 22:11:41 +02:00
										 |  |  | 	const auto label = Ui::CreateChild<Ui::FlatLabel>( | 
					
						
							|  |  |  | 		this, | 
					
						
							| 
									
										
										
										
											2019-06-18 14:16:43 +02:00
										 |  |  | 		tr::lng_change_phone_about(Ui::Text::RichLangValue), | 
					
						
							| 
									
										
										
										
											2019-06-12 22:11:41 +02:00
										 |  |  | 		st::changePhoneDescription); | 
					
						
							| 
									
										
										
										
											2017-03-21 14:42:04 +03:00
										 |  |  | 	label->moveToLeft((st::boxWideWidth - label->width()) / 2, st::changePhoneDescriptionTop); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	setDimensions(st::boxWideWidth, label->bottomNoMargins() + st::boxLittleSkip); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ChangePhoneBox::paintEvent(QPaintEvent *e) { | 
					
						
							|  |  |  | 	BoxContent::paintEvent(e); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Painter p(this); | 
					
						
							|  |  |  | 	st::changePhoneIcon.paint(p, (width() - st::changePhoneIcon.width()) / 2, st::changePhoneIconTop, width()); | 
					
						
							|  |  |  | } |