2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2014-12-01 10:47:38 +00:00
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2015-10-03 13:16:42 +00:00
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2016-02-08 10:56:18 +00:00
|
|
|
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
|
|
|
#include "stdafx.h"
|
2016-04-21 17:57:29 +00:00
|
|
|
#include "intro/introphone.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-04-21 17:57:29 +00:00
|
|
|
#include "lang.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
#include "application.h"
|
2016-03-14 16:59:18 +00:00
|
|
|
#include "intro/introcode.h"
|
2016-10-27 14:10:28 +00:00
|
|
|
#include "styles/style_intro.h"
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
2016-11-16 10:44:06 +00:00
|
|
|
#include "ui/widgets/labels.h"
|
2016-11-24 19:28:23 +00:00
|
|
|
#include "ui/effects/widget_fade_wrap.h"
|
|
|
|
#include "core/click_handler_types.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
namespace Intro {
|
|
|
|
|
|
|
|
PhoneWidget::PhoneWidget(QWidget *parent, Widget::Data *data) : Step(parent, data)
|
2016-11-04 19:50:35 +00:00
|
|
|
, _country(this, st::introCountry)
|
2016-11-15 11:56:49 +00:00
|
|
|
, _code(this, st::introCountryCode)
|
2016-11-24 19:28:23 +00:00
|
|
|
, _phone(this, st::introPhone)
|
2016-11-04 19:50:35 +00:00
|
|
|
, _checkRequest(this) {
|
|
|
|
connect(_phone, SIGNAL(voidBackspace(QKeyEvent*)), _code, SLOT(startErasing(QKeyEvent*)));
|
|
|
|
connect(_country, SIGNAL(codeChanged(const QString &)), _code, SLOT(codeSelected(const QString &)));
|
|
|
|
connect(_code, SIGNAL(codeChanged(const QString &)), _country, SLOT(onChooseCode(const QString &)));
|
|
|
|
connect(_code, SIGNAL(codeChanged(const QString &)), _phone, SLOT(onChooseCode(const QString &)));
|
|
|
|
connect(_country, SIGNAL(codeChanged(const QString &)), _phone, SLOT(onChooseCode(const QString &)));
|
|
|
|
connect(_code, SIGNAL(addedToNumber(const QString &)), _phone, SLOT(addedToNumber(const QString &)));
|
|
|
|
connect(_phone, SIGNAL(changed()), this, SLOT(onInputChange()));
|
|
|
|
connect(_code, SIGNAL(changed()), this, SLOT(onInputChange()));
|
|
|
|
connect(_checkRequest, SIGNAL(timeout()), this, SLOT(onCheckRequest()));
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
setTitleText(lang(lng_phone_title));
|
|
|
|
setDescriptionText(lang(lng_phone_desc));
|
|
|
|
subscribe(getData()->updated, [this] { countryChanged(); });
|
|
|
|
setErrorCentered(true);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
if (!_country->onChooseCountry(getData()->country)) {
|
2016-11-04 19:50:35 +00:00
|
|
|
_country->onChooseCountry(qsl("US"));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-11-04 19:50:35 +00:00
|
|
|
_changed = false;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void PhoneWidget::resizeEvent(QResizeEvent *e) {
|
|
|
|
Step::resizeEvent(e);
|
|
|
|
_country->moveToLeft(contentLeft(), contentTop() + st::introStepFieldTop);
|
|
|
|
auto phoneTop = _country->y() + _country->height() + st::introPhoneTop;
|
|
|
|
_code->moveToLeft(contentLeft(), phoneTop);
|
|
|
|
_phone->moveToLeft(contentLeft() + _country->width() - st::introPhone.width, phoneTop);
|
|
|
|
updateSignupGeometry();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void PhoneWidget::updateSignupGeometry() {
|
|
|
|
if (_signup) {
|
|
|
|
_signup->moveToLeft(contentLeft() + st::buttonRadius, contentTop() + st::introDescriptionTop);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void PhoneWidget::showPhoneError(const QString &text) {
|
|
|
|
_phone->showError();
|
|
|
|
showError(text);
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void PhoneWidget::hidePhoneError() {
|
|
|
|
hideError();
|
|
|
|
if (_signup) {
|
2016-12-13 17:07:56 +00:00
|
|
|
_signup->hideAnimated();
|
2016-11-24 19:28:23 +00:00
|
|
|
showDescription();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void PhoneWidget::showSignup() {
|
|
|
|
showPhoneError(lang(lng_bad_phone_noreg));
|
|
|
|
if (!_signup) {
|
|
|
|
auto signupText = lng_phone_notreg(lt_link_start, textcmdStartLink(1), lt_link_end, textcmdStopLink(), lt_signup_start, textcmdStartLink(2), lt_signup_end, textcmdStopLink());
|
2016-12-13 17:07:56 +00:00
|
|
|
auto inner = object_ptr<Ui::FlatLabel>(this, signupText, Ui::FlatLabel::InitType::Rich, st::introDescription, st::introDescriptionTextStyle);
|
|
|
|
_signup.create(this, std_::move(inner), st::introErrorDuration);
|
2016-11-24 19:28:23 +00:00
|
|
|
_signup->entity()->setLink(1, MakeShared<UrlClickHandler>(qsl("https://telegram.org"), false));
|
|
|
|
_signup->entity()->setLink(2, MakeShared<LambdaClickHandler>([this] {
|
|
|
|
toSignUp();
|
|
|
|
}));
|
|
|
|
_signup->hideFast();
|
|
|
|
updateSignupGeometry();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-12-13 17:07:56 +00:00
|
|
|
_signup->showAnimated();
|
2016-11-24 19:28:23 +00:00
|
|
|
hideDescription();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void PhoneWidget::countryChanged() {
|
2016-11-04 19:50:35 +00:00
|
|
|
if (!_changed) {
|
2016-11-24 19:28:23 +00:00
|
|
|
selectCountry(getData()->country);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void PhoneWidget::onInputChange() {
|
2016-11-04 19:50:35 +00:00
|
|
|
_changed = true;
|
2016-11-24 19:28:23 +00:00
|
|
|
hidePhoneError();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void PhoneWidget::submit() {
|
2016-11-04 19:50:35 +00:00
|
|
|
if (_sentRequest || isHidden()) return;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
if (!App::isValidPhone(fullNumber())) {
|
2016-11-24 19:28:23 +00:00
|
|
|
showPhoneError(lang(lng_bad_phone));
|
2016-11-04 19:50:35 +00:00
|
|
|
_phone->setFocus();
|
2014-05-30 08:53:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
hidePhoneError();
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-04 19:50:35 +00:00
|
|
|
_checkRequest->start(1000);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-04 19:50:35 +00:00
|
|
|
_sentPhone = fullNumber();
|
2016-11-24 19:28:23 +00:00
|
|
|
_sentRequest = MTP::send(MTPauth_CheckPhone(MTP_string(_sentPhone)), rpcDone(&PhoneWidget::phoneCheckDone), rpcFail(&PhoneWidget::phoneSubmitFail));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void PhoneWidget::stopCheck() {
|
2016-11-04 19:50:35 +00:00
|
|
|
_checkRequest->stop();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void PhoneWidget::onCheckRequest() {
|
2016-12-09 18:56:01 +00:00
|
|
|
auto status = MTP::state(_sentRequest);
|
2014-05-30 08:53:19 +00:00
|
|
|
if (status < 0) {
|
2016-12-09 18:56:01 +00:00
|
|
|
auto leftms = -status;
|
2014-05-30 08:53:19 +00:00
|
|
|
if (leftms >= 1000) {
|
2016-11-04 19:50:35 +00:00
|
|
|
MTP::cancel(base::take(_sentRequest));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-04 19:50:35 +00:00
|
|
|
if (!_sentRequest && status == MTP::RequestSent) {
|
2014-05-30 08:53:19 +00:00
|
|
|
stopCheck();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void PhoneWidget::phoneCheckDone(const MTPauth_CheckedPhone &result) {
|
2014-05-30 08:53:19 +00:00
|
|
|
stopCheck();
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
auto &d = result.c_auth_checkedPhone();
|
2015-10-29 00:16:52 +00:00
|
|
|
if (mtpIsTrue(d.vphone_registered)) {
|
2016-11-24 19:28:23 +00:00
|
|
|
hidePhoneError();
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-04 19:50:35 +00:00
|
|
|
_checkRequest->start(1000);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-03-19 16:55:15 +00:00
|
|
|
MTPauth_SendCode::Flags flags = 0;
|
2016-11-24 19:28:23 +00:00
|
|
|
_sentRequest = MTP::send(MTPauth_SendCode(MTP_flags(flags), MTP_string(_sentPhone), MTPBool(), MTP_int(ApiId), MTP_string(ApiHash)), rpcDone(&PhoneWidget::phoneSubmitDone), rpcFail(&PhoneWidget::phoneSubmitFail));
|
2014-05-30 08:53:19 +00:00
|
|
|
} else {
|
2016-11-24 19:28:23 +00:00
|
|
|
showSignup();
|
2016-11-04 19:50:35 +00:00
|
|
|
_sentRequest = 0;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void PhoneWidget::phoneSubmitDone(const MTPauth_SentCode &result) {
|
2014-05-30 08:53:19 +00:00
|
|
|
stopCheck();
|
2016-11-04 19:50:35 +00:00
|
|
|
_sentRequest = 0;
|
2016-03-14 16:59:18 +00:00
|
|
|
|
|
|
|
if (result.type() != mtpc_auth_sentCode) {
|
2016-11-24 19:28:23 +00:00
|
|
|
showPhoneError(lang(lng_server_error));
|
2016-03-14 16:59:18 +00:00
|
|
|
return;
|
2014-10-22 18:39:03 +00:00
|
|
|
}
|
2016-03-14 16:59:18 +00:00
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
auto &d = result.c_auth_sentCode();
|
|
|
|
fillSentCodeData(d.vtype);
|
|
|
|
getData()->phone = _sentPhone;
|
|
|
|
getData()->phoneHash = d.vphone_code_hash.c_string().v.c_str();
|
|
|
|
getData()->phoneIsRegistered = d.is_phone_registered();
|
2016-03-15 19:38:30 +00:00
|
|
|
if (d.has_next_type() && d.vnext_type.type() == mtpc_auth_codeTypeCall) {
|
2016-11-24 19:28:23 +00:00
|
|
|
getData()->callStatus = Widget::Data::CallStatus::Waiting;
|
|
|
|
getData()->callTimeout = d.has_timeout() ? d.vtimeout.v : 60;
|
2016-03-15 19:38:30 +00:00
|
|
|
} else {
|
2016-11-24 19:28:23 +00:00
|
|
|
getData()->callStatus = Widget::Data::CallStatus::Disabled;
|
|
|
|
getData()->callTimeout = 0;
|
2016-03-14 16:59:18 +00:00
|
|
|
}
|
2016-11-24 19:28:23 +00:00
|
|
|
goNext(new Intro::CodeWidget(parentWidget(), getData()));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void PhoneWidget::toSignUp() {
|
|
|
|
hideError(); // Hide error, but leave the signup label visible.
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-04 19:50:35 +00:00
|
|
|
_checkRequest->start(1000);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-03-19 16:55:15 +00:00
|
|
|
MTPauth_SendCode::Flags flags = 0;
|
2016-11-24 19:28:23 +00:00
|
|
|
_sentRequest = MTP::send(MTPauth_SendCode(MTP_flags(flags), MTP_string(_sentPhone), MTPBool(), MTP_int(ApiId), MTP_string(ApiHash)), rpcDone(&PhoneWidget::phoneSubmitDone), rpcFail(&PhoneWidget::phoneSubmitFail));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
bool PhoneWidget::phoneSubmitFail(const RPCError &error) {
|
2016-04-08 10:44:35 +00:00
|
|
|
if (MTP::isFloodError(error)) {
|
|
|
|
stopCheck();
|
2016-11-04 19:50:35 +00:00
|
|
|
_sentRequest = 0;
|
2016-11-24 19:28:23 +00:00
|
|
|
showPhoneError(lang(lng_flood_error));
|
2016-04-08 10:44:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (MTP::isDefaultHandledError(error)) return false;
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
stopCheck();
|
2016-11-04 19:50:35 +00:00
|
|
|
_sentRequest = 0;
|
2016-12-09 18:56:01 +00:00
|
|
|
auto &err = error.type();
|
2016-04-08 10:44:35 +00:00
|
|
|
if (err == qstr("PHONE_NUMBER_INVALID")) { // show error
|
2016-11-24 19:28:23 +00:00
|
|
|
showPhoneError(lang(lng_bad_phone));
|
2014-05-30 08:53:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (cDebug()) { // internal server error
|
2016-11-24 19:28:23 +00:00
|
|
|
showPhoneError(err + ": " + error.description());
|
2014-05-30 08:53:19 +00:00
|
|
|
} else {
|
2016-11-24 19:28:23 +00:00
|
|
|
showPhoneError(lang(lng_server_error));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
QString PhoneWidget::fullNumber() const {
|
|
|
|
return _code->getLastText() + _phone->getLastText();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void PhoneWidget::selectCountry(const QString &c) {
|
2016-11-04 19:50:35 +00:00
|
|
|
_country->onChooseCountry(c);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void PhoneWidget::setInnerFocus() {
|
2016-11-04 19:50:35 +00:00
|
|
|
_phone->setFocus();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void PhoneWidget::activate() {
|
|
|
|
Step::activate();
|
|
|
|
_country->show();
|
|
|
|
_phone->show();
|
|
|
|
_code->show();
|
|
|
|
setInnerFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneWidget::finished() {
|
|
|
|
Step::finished();
|
2016-11-04 19:50:35 +00:00
|
|
|
_checkRequest->stop();
|
2016-03-14 16:59:18 +00:00
|
|
|
rpcClear();
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
cancelled();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void PhoneWidget::cancelled() {
|
|
|
|
MTP::cancel(base::take(_sentRequest));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
} // namespace Intro
|