2016-07-28 17:01:08 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-07-28 17:01:08 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-07-28 17:01:08 +00:00
|
|
|
*/
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/confirm_phone_box.h"
|
2016-07-28 17:01:08 +00:00
|
|
|
|
|
|
|
#include "styles/style_boxes.h"
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/confirm_box.h"
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
2016-11-15 11:56:49 +00:00
|
|
|
#include "ui/widgets/input_fields.h"
|
2016-11-16 10:44:06 +00:00
|
|
|
#include "ui/widgets/labels.h"
|
2016-07-28 17:01:08 +00:00
|
|
|
#include "mainwidget.h"
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2016-07-28 17:01:08 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
object_ptr<ConfirmPhoneBox> CurrentConfirmPhoneBox = { nullptr };
|
2016-07-28 17:01:08 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-11-19 12:15:59 +00:00
|
|
|
SentCodeField::SentCodeField(
|
|
|
|
QWidget *parent,
|
|
|
|
const style::InputField &st,
|
|
|
|
Fn<QString()> placeholderFactory,
|
|
|
|
const QString &val)
|
|
|
|
: Ui::InputField(parent, st, std::move(placeholderFactory), val) {
|
|
|
|
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 11:42:04 +00:00
|
|
|
void SentCodeField::fix() {
|
|
|
|
if (_fixing) return;
|
|
|
|
|
|
|
|
_fixing = true;
|
|
|
|
auto newText = QString();
|
2018-11-19 12:15:59 +00:00
|
|
|
const auto now = getLastText();
|
2017-03-21 11:42:04 +00:00
|
|
|
auto oldPos = textCursor().position();
|
|
|
|
auto newPos = -1;
|
|
|
|
auto oldLen = now.size();
|
|
|
|
auto digitCount = 0;
|
2018-11-19 12:15:59 +00:00
|
|
|
for (const auto ch : now) {
|
2017-03-21 11:42:04 +00:00
|
|
|
if (ch.isDigit()) {
|
|
|
|
++digitCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_autoSubmitLength > 0 && digitCount > _autoSubmitLength) {
|
|
|
|
digitCount = _autoSubmitLength;
|
|
|
|
}
|
2018-11-19 12:15:59 +00:00
|
|
|
auto strict = (_autoSubmitLength > 0)
|
|
|
|
&& (digitCount == _autoSubmitLength);
|
2017-03-21 11:42:04 +00:00
|
|
|
|
|
|
|
newText.reserve(oldLen);
|
|
|
|
int i = 0;
|
2018-11-19 12:15:59 +00:00
|
|
|
for (const auto ch : now) {
|
2017-03-21 11:42:04 +00:00
|
|
|
if (i++ == oldPos) {
|
|
|
|
newPos = newText.length();
|
|
|
|
}
|
|
|
|
if (ch.isDigit()) {
|
|
|
|
if (!digitCount--) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
newText += ch;
|
|
|
|
if (strict && !digitCount) {
|
|
|
|
break;
|
|
|
|
}
|
2018-11-19 12:15:59 +00:00
|
|
|
} else if (ch == '-') {
|
|
|
|
newText += ch;
|
2017-03-21 11:42:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (newPos < 0) {
|
|
|
|
newPos = newText.length();
|
|
|
|
}
|
|
|
|
if (newText != now) {
|
2018-11-19 12:15:59 +00:00
|
|
|
setText(newText);
|
2017-03-21 11:42:04 +00:00
|
|
|
setCursorPosition(newPos);
|
|
|
|
}
|
|
|
|
_fixing = false;
|
|
|
|
|
|
|
|
if (_changedCallback) {
|
|
|
|
_changedCallback();
|
|
|
|
}
|
|
|
|
if (strict && _submitCallback) {
|
|
|
|
_submitCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-19 12:15:59 +00:00
|
|
|
SentCodeCall::SentCodeCall(
|
|
|
|
FnMut<void()> callCallback,
|
|
|
|
Fn<void()> updateCallback)
|
2018-04-09 17:56:07 +00:00
|
|
|
: _call(std::move(callCallback))
|
2017-03-21 11:42:04 +00:00
|
|
|
, _update(std::move(updateCallback)) {
|
2018-04-09 17:56:07 +00:00
|
|
|
_timer.setCallback([=] {
|
2017-03-21 11:42:04 +00:00
|
|
|
if (_status.state == State::Waiting) {
|
|
|
|
if (--_status.timeout <= 0) {
|
|
|
|
_status.state = State::Calling;
|
2018-04-09 17:56:07 +00:00
|
|
|
_timer.cancel();
|
2017-03-21 11:42:04 +00:00
|
|
|
if (_call) {
|
|
|
|
_call();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_update) {
|
|
|
|
_update();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void SentCodeCall::setStatus(const Status &status) {
|
|
|
|
_status = status;
|
|
|
|
if (_status.state == State::Waiting) {
|
2018-04-09 17:56:07 +00:00
|
|
|
_timer.callEach(1000);
|
2017-03-21 11:42:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SentCodeCall::getText() const {
|
|
|
|
switch (_status.state) {
|
|
|
|
case State::Waiting: {
|
|
|
|
if (_status.timeout >= 3600) {
|
|
|
|
return lng_code_call(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')));
|
|
|
|
}
|
|
|
|
return lng_code_call(lt_minutes, QString::number(_status.timeout / 60), lt_seconds, qsl("%1").arg(_status.timeout % 60, 2, 10, QChar('0')));
|
|
|
|
} break;
|
|
|
|
case State::Calling: return lang(lng_code_calling);
|
|
|
|
case State::Called: return lang(lng_code_called);
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2016-07-28 17:01:08 +00:00
|
|
|
void ConfirmPhoneBox::start(const QString &phone, const QString &hash) {
|
2016-12-13 17:07:56 +00:00
|
|
|
if (CurrentConfirmPhoneBox && CurrentConfirmPhoneBox->getPhone() != phone) {
|
|
|
|
CurrentConfirmPhoneBox.destroyDelayed();
|
2016-07-28 17:01:08 +00:00
|
|
|
}
|
2016-12-13 17:07:56 +00:00
|
|
|
if (!CurrentConfirmPhoneBox) {
|
|
|
|
CurrentConfirmPhoneBox = Box<ConfirmPhoneBox>(phone, hash);
|
2016-07-28 17:01:08 +00:00
|
|
|
}
|
2016-12-13 17:07:56 +00:00
|
|
|
CurrentConfirmPhoneBox->checkPhoneAndHash();
|
2016-07-28 17:01:08 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
ConfirmPhoneBox::ConfirmPhoneBox(QWidget*, const QString &phone, const QString &hash)
|
|
|
|
: _phone(phone)
|
|
|
|
, _hash(hash)
|
2018-04-09 17:56:07 +00:00
|
|
|
, _call([this] { sendCall(); }, [this] { update(); }) {
|
2017-03-21 11:42:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfirmPhoneBox::sendCall() {
|
|
|
|
MTP::send(MTPauth_ResendCode(MTP_string(_phone), MTP_string(_phoneHash)), rpcDone(&ConfirmPhoneBox::callDone));
|
2016-12-13 17:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfirmPhoneBox::checkPhoneAndHash() {
|
|
|
|
if (_sendCodeRequestId) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-25 15:42:01 +00:00
|
|
|
_sendCodeRequestId = MTP::send(MTPaccount_SendConfirmPhoneCode(MTP_flags(0), MTP_string(_hash), MTPBool()), rpcDone(&ConfirmPhoneBox::sendCodeDone), rpcFail(&ConfirmPhoneBox::sendCodeFail));
|
2016-07-28 17:01:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfirmPhoneBox::sendCodeDone(const MTPauth_SentCode &result) {
|
2017-03-21 11:42:04 +00:00
|
|
|
Expects(result.type() == mtpc_auth_sentCode);
|
2016-07-28 17:01:08 +00:00
|
|
|
_sendCodeRequestId = 0;
|
|
|
|
|
|
|
|
auto &resultInner = result.c_auth_sentCode();
|
|
|
|
switch (resultInner.vtype.type()) {
|
|
|
|
case mtpc_auth_sentCodeTypeApp: LOG(("Error: should not be in-app code!")); break;
|
|
|
|
case mtpc_auth_sentCodeTypeSms: _sentCodeLength = resultInner.vtype.c_auth_sentCodeTypeSms().vlength.v; break;
|
|
|
|
case mtpc_auth_sentCodeTypeCall: _sentCodeLength = resultInner.vtype.c_auth_sentCodeTypeCall().vlength.v; break;
|
|
|
|
case mtpc_auth_sentCodeTypeFlashCall: LOG(("Error: should not be flashcall!")); break;
|
|
|
|
}
|
2016-07-29 10:05:00 +00:00
|
|
|
_phoneHash = qs(resultInner.vphone_code_hash);
|
2016-07-28 17:01:08 +00:00
|
|
|
if (resultInner.has_next_type() && resultInner.vnext_type.type() == mtpc_auth_codeTypeCall) {
|
2017-03-21 11:42:04 +00:00
|
|
|
_call.setStatus({ SentCodeCall::State::Waiting, resultInner.has_timeout() ? resultInner.vtimeout.v : 60 });
|
2016-07-28 17:01:08 +00:00
|
|
|
}
|
|
|
|
launch();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConfirmPhoneBox::sendCodeFail(const RPCError &error) {
|
2017-09-28 20:11:10 +00:00
|
|
|
auto errorText = Lang::Hard::ServerError();
|
2016-07-28 17:01:08 +00:00
|
|
|
if (MTP::isFloodError(error)) {
|
|
|
|
errorText = lang(lng_flood_error);
|
|
|
|
} else if (MTP::isDefaultHandledError(error)) {
|
|
|
|
return false;
|
|
|
|
} else if (error.code() == 400) {
|
|
|
|
errorText = lang(lng_confirm_phone_link_invalid);
|
|
|
|
}
|
|
|
|
_sendCodeRequestId = 0;
|
2016-12-13 17:07:56 +00:00
|
|
|
Ui::show(Box<InformBox>(errorText));
|
|
|
|
if (this == CurrentConfirmPhoneBox) {
|
|
|
|
CurrentConfirmPhoneBox.destroyDelayed();
|
|
|
|
} else {
|
|
|
|
deleteLater();
|
|
|
|
}
|
2016-07-28 17:01:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfirmPhoneBox::launch() {
|
2016-12-13 17:07:56 +00:00
|
|
|
if (!CurrentConfirmPhoneBox) return;
|
2017-02-21 13:45:56 +00:00
|
|
|
Ui::show(std::move(CurrentConfirmPhoneBox));
|
2016-12-13 17:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfirmPhoneBox::prepare() {
|
2016-11-16 10:44:06 +00:00
|
|
|
_about.create(this, st::confirmPhoneAboutLabel);
|
2016-07-28 17:01:08 +00:00
|
|
|
TextWithEntities aboutText;
|
|
|
|
auto formattedPhone = App::formatPhone(_phone);
|
|
|
|
aboutText.text = lng_confirm_phone_about(lt_phone, formattedPhone);
|
|
|
|
auto phonePosition = aboutText.text.indexOf(formattedPhone);
|
|
|
|
if (phonePosition >= 0) {
|
|
|
|
aboutText.entities.push_back(EntityInText(EntityInTextBold, phonePosition, formattedPhone.size()));
|
|
|
|
}
|
|
|
|
_about->setMarkedText(aboutText);
|
|
|
|
|
2017-05-30 15:21:05 +00:00
|
|
|
_code.create(this, st::confirmPhoneCodeField, langFactory(lng_code_ph));
|
2018-05-31 12:20:28 +00:00
|
|
|
_code->setAutoSubmit(_sentCodeLength, [=] { sendCode(); });
|
|
|
|
_code->setChangedCallback([=] { showError(QString()); });
|
2016-07-28 17:01:08 +00:00
|
|
|
|
2017-05-30 15:21:05 +00:00
|
|
|
setTitle(langFactory(lng_confirm_phone_title));
|
2016-07-28 17:01:08 +00:00
|
|
|
|
2018-05-31 12:20:28 +00:00
|
|
|
addButton(langFactory(lng_confirm_phone_send), [=] { sendCode(); });
|
|
|
|
addButton(langFactory(lng_cancel), [=] { closeBox(); });
|
2016-07-28 17:01:08 +00:00
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
setDimensions(st::boxWidth, st::usernamePadding.top() + _code->height() + st::usernameSkip + _about->height() + st::usernameSkip);
|
2016-07-28 17:01:08 +00:00
|
|
|
|
2018-05-31 12:20:28 +00:00
|
|
|
connect(_code, &Ui::InputField::submitted, [=] { sendCode(); });
|
2016-07-28 17:01:08 +00:00
|
|
|
|
2016-11-18 13:34:58 +00:00
|
|
|
showChildren();
|
2016-07-28 17:01:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfirmPhoneBox::callDone(const MTPauth_SentCode &result) {
|
2017-03-21 11:42:04 +00:00
|
|
|
_call.callDone();
|
2016-07-28 17:01:08 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 12:20:28 +00:00
|
|
|
void ConfirmPhoneBox::sendCode() {
|
2016-07-28 17:01:08 +00:00
|
|
|
if (_sendCodeRequestId) {
|
|
|
|
return;
|
|
|
|
}
|
2018-11-19 12:15:59 +00:00
|
|
|
const auto code = _code->getDigitsOnly();
|
2016-07-28 17:01:08 +00:00
|
|
|
if (code.isEmpty()) {
|
|
|
|
_code->showError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_code->setDisabled(true);
|
|
|
|
setFocus();
|
|
|
|
|
|
|
|
showError(QString());
|
|
|
|
|
2018-11-19 12:15:59 +00:00
|
|
|
_sendCodeRequestId = MTP::send(
|
|
|
|
MTPaccount_ConfirmPhone(MTP_string(_phoneHash), MTP_string(code)),
|
|
|
|
rpcDone(&ConfirmPhoneBox::confirmDone),
|
|
|
|
rpcFail(&ConfirmPhoneBox::confirmFail));
|
2016-07-28 17:01:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfirmPhoneBox::confirmDone(const MTPBool &result) {
|
|
|
|
_sendCodeRequestId = 0;
|
2016-12-13 17:07:56 +00:00
|
|
|
Ui::show(Box<InformBox>(lng_confirm_phone_success(lt_phone, App::formatPhone(_phone))));
|
2016-07-28 17:01:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ConfirmPhoneBox::confirmFail(const RPCError &error) {
|
2017-09-28 20:11:10 +00:00
|
|
|
auto errorText = Lang::Hard::ServerError();
|
2016-07-28 17:01:08 +00:00
|
|
|
if (MTP::isFloodError(error)) {
|
|
|
|
errorText = lang(lng_flood_error);
|
|
|
|
} else if (MTP::isDefaultHandledError(error)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
auto &errorType = error.type();
|
|
|
|
if (errorType == qstr("PHONE_CODE_EMPTY") || errorType == qstr("PHONE_CODE_INVALID")) {
|
|
|
|
errorText = lang(lng_bad_code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_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 17:07:56 +00:00
|
|
|
BoxContent::paintEvent(e);
|
2016-07-28 17:01:08 +00:00
|
|
|
|
2016-11-19 14:47:28 +00:00
|
|
|
Painter p(this);
|
2016-07-28 17:01:08 +00:00
|
|
|
|
|
|
|
p.setFont(st::boxTextFont);
|
2017-03-21 11:42:04 +00:00
|
|
|
auto callText = _call.getText();
|
2016-07-28 17:01:08 +00: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);
|
|
|
|
errorText = lang(lng_confirm_phone_enter_code);
|
|
|
|
} else {
|
2016-10-31 12:29:26 +00:00
|
|
|
p.setPen(st::boxTextFgError);
|
2016-07-28 17:01:08 +00: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 17:07:56 +00:00
|
|
|
BoxContent::resizeEvent(e);
|
|
|
|
|
2016-07-28 17:01:08 +00:00
|
|
|
_code->resize(width() - st::usernamePadding.left() - st::usernamePadding.right(), _code->height());
|
2016-12-13 17:07:56 +00:00
|
|
|
_code->moveToLeft(st::usernamePadding.left(), st::usernamePadding.top());
|
2016-07-28 17:01:08 +00:00
|
|
|
|
|
|
|
_about->moveToLeft(st::usernamePadding.left(), _code->y() + _code->height() + st::usernameSkip);
|
|
|
|
}
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
void ConfirmPhoneBox::setInnerFocus() {
|
2016-12-20 13:03:51 +00:00
|
|
|
_code->setFocusFast();
|
2016-11-15 11:56:49 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 17:01:08 +00:00
|
|
|
ConfirmPhoneBox::~ConfirmPhoneBox() {
|
|
|
|
if (_sendCodeRequestId) {
|
|
|
|
MTP::cancel(_sendCodeRequestId);
|
|
|
|
}
|
|
|
|
}
|