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.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2014-12-01 10:47:38 +00:00
|
|
|
Copyright (c) 2014 John Preston, https://desktop.telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "lang.h"
|
|
|
|
#include "style.h"
|
|
|
|
|
|
|
|
#include "application.h"
|
|
|
|
|
|
|
|
#include "intro/introcode.h"
|
|
|
|
#include "intro/intro.h"
|
|
|
|
|
|
|
|
CodeInput::CodeInput(QWidget *parent, const style::flatInput &st, const QString &ph) : FlatInput(parent, st, ph) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeInput::correctValue(QKeyEvent *e, const QString &was) {
|
|
|
|
QString oldText(text()), newText;
|
|
|
|
int oldPos(cursorPosition()), newPos(-1), oldLen(oldText.length()), digitCount = 0;
|
|
|
|
for (int i = 0; i < oldLen; ++i) {
|
|
|
|
if (oldText[i].isDigit()) {
|
|
|
|
++digitCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (digitCount > 5) digitCount = 5;
|
|
|
|
bool strict = (digitCount == 5);
|
|
|
|
|
|
|
|
newText.reserve(oldLen);
|
|
|
|
for (int i = 0; i < oldLen; ++i) {
|
|
|
|
QChar ch(oldText[i]);
|
|
|
|
if (ch.isDigit()) {
|
|
|
|
if (!digitCount--) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
newText += ch;
|
|
|
|
if (strict && !digitCount) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == oldPos) {
|
|
|
|
newPos = newText.length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (newPos < 0) {
|
|
|
|
newPos = newText.length();
|
|
|
|
}
|
|
|
|
if (newText != oldText) {
|
|
|
|
setText(newText);
|
|
|
|
if (newPos != oldPos) {
|
|
|
|
setCursorPosition(newPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strict) emit codeEntered();
|
|
|
|
}
|
|
|
|
|
2014-06-16 09:31:10 +00:00
|
|
|
IntroCode::IntroCode(IntroWidget *parent) : IntroStage(parent), errorAlpha(0),
|
2014-05-30 08:53:19 +00:00
|
|
|
next(this, lang(lng_intro_next), st::btnIntroNext),
|
2014-06-16 09:31:10 +00:00
|
|
|
code(this, st::inpIntroCode, lang(lng_code_ph)), waitTillCall(intro()->getCallTimeout()) {
|
2014-05-30 08:53:19 +00:00
|
|
|
setVisible(false);
|
|
|
|
setGeometry(parent->innerRect());
|
|
|
|
|
|
|
|
connect(&next, SIGNAL(stateChanged(int, ButtonStateChangeSource)), parent, SLOT(onDoneStateChanged(int, ButtonStateChangeSource)));
|
|
|
|
connect(&next, SIGNAL(clicked()), this, SLOT(onSubmitCode()));
|
|
|
|
connect(&code, SIGNAL(changed()), this, SLOT(onInputChange()));
|
|
|
|
connect(&callTimer, SIGNAL(timeout()), this, SLOT(onSendCall()));
|
|
|
|
connect(&checkRequest, SIGNAL(timeout()), this, SLOT(onCheckRequest()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void IntroCode::paintEvent(QPaintEvent *e) {
|
|
|
|
bool trivial = (rect() == e->rect());
|
|
|
|
|
|
|
|
QPainter p(this);
|
|
|
|
if (!trivial) {
|
|
|
|
p.setClipRect(e->rect());
|
|
|
|
}
|
|
|
|
if (trivial || e->rect().intersects(textRect)) {
|
|
|
|
p.setFont(st::introHeaderFont->f);
|
2014-11-26 16:45:52 +00:00
|
|
|
p.drawText(textRect, intro()->getPhone(), style::al_top);
|
2014-05-30 08:53:19 +00:00
|
|
|
p.setFont(st::introFont->f);
|
2014-11-26 16:45:52 +00:00
|
|
|
p.drawText(textRect, lang(lng_code_desc), style::al_bottom);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
QString callText = lang(lng_code_calling);
|
|
|
|
if (waitTillCall >= 3600) {
|
2014-12-18 18:40:49 +00:00
|
|
|
callText = lng_code_call(lt_minutes, qsl("%1:%2").arg(waitTillCall / 3600).arg((waitTillCall / 60) % 60, 2, 10, QChar('0')), lt_seconds, qsl("%1").arg(waitTillCall % 60, 2, 10, QChar('0')));
|
2014-05-30 08:53:19 +00:00
|
|
|
} else if (waitTillCall > 0) {
|
2014-12-18 18:40:49 +00:00
|
|
|
callText = lng_code_call(lt_minutes, QString::number(waitTillCall / 60), lt_seconds, qsl("%1").arg(waitTillCall % 60, 2, 10, QChar('0')));
|
2014-05-30 08:53:19 +00:00
|
|
|
} else if (waitTillCall < 0) {
|
|
|
|
callText = lang(lng_code_called);
|
|
|
|
}
|
2014-11-26 16:45:52 +00:00
|
|
|
p.drawText(QRect(textRect.left(), code.y() + code.height() + st::introCallSkip, st::introTextSize.width(), st::introErrHeight), callText, style::al_center);
|
2014-05-30 08:53:19 +00:00
|
|
|
if (animating() || error.length()) {
|
|
|
|
p.setOpacity(errorAlpha.current());
|
|
|
|
p.setFont(st::introErrFont->f);
|
|
|
|
p.setPen(st::introErrColor->p);
|
2014-12-12 16:27:03 +00:00
|
|
|
p.drawText(QRect(textRect.left(), next.y() + next.height() + st::introErrTop, st::introTextSize.width(), st::introErrHeight), error, style::al_center);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IntroCode::resizeEvent(QResizeEvent *e) {
|
|
|
|
if (e->oldSize().width() != width()) {
|
2014-11-26 16:45:52 +00:00
|
|
|
next.move((width() - next.width()) / 2, st::introBtnTop);
|
|
|
|
code.move((width() - code.width()) / 2, st::introTextTop + st::introTextSize.height() + st::introCountry.top);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2014-11-26 16:45:52 +00:00
|
|
|
textRect = QRect((width() - st::introTextSize.width()) / 2, st::introTextTop, st::introTextSize.width(), st::introTextSize.height());
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IntroCode::showError(const QString &err) {
|
|
|
|
if (!err.isEmpty()) code.notaBene();
|
|
|
|
if (!animating() && err == error) return;
|
|
|
|
|
|
|
|
if (err.length()) {
|
|
|
|
error = err;
|
|
|
|
errorAlpha.start(1);
|
|
|
|
} else {
|
|
|
|
errorAlpha.start(0);
|
|
|
|
}
|
|
|
|
anim::start(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IntroCode::animStep(float64 ms) {
|
|
|
|
float64 dt = ms / st::introErrDuration;
|
|
|
|
|
|
|
|
bool res = true;
|
|
|
|
if (dt >= 1) {
|
|
|
|
res = false;
|
|
|
|
errorAlpha.finish();
|
|
|
|
if (!errorAlpha.current()) {
|
|
|
|
error = "";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errorAlpha.update(dt, st::introErrFunc);
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IntroCode::activate() {
|
|
|
|
waitTillCall = intro()->getCallTimeout();
|
|
|
|
callTimer.start(1000);
|
|
|
|
error = "";
|
|
|
|
errorAlpha = anim::fvalue(0);
|
2015-05-14 16:50:04 +00:00
|
|
|
sentCode = QString();
|
2014-05-30 08:53:19 +00:00
|
|
|
show();
|
|
|
|
code.setDisabled(false);
|
|
|
|
code.setFocus();
|
|
|
|
}
|
|
|
|
|
2015-04-04 20:01:34 +00:00
|
|
|
void IntroCode::prepareShow() {
|
|
|
|
code.setText(QString());
|
|
|
|
if (sentRequest) {
|
|
|
|
MTP::cancel(sentRequest);
|
|
|
|
sentRequest = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void IntroCode::deactivate() {
|
|
|
|
callTimer.stop();
|
|
|
|
hide();
|
|
|
|
code.clearFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IntroCode::stopCheck() {
|
|
|
|
checkRequest.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IntroCode::onCheckRequest() {
|
|
|
|
int32 status = MTP::state(sentRequest);
|
|
|
|
if (status < 0) {
|
|
|
|
int32 leftms = -status;
|
|
|
|
if (leftms >= 1000) {
|
|
|
|
if (sentRequest) {
|
|
|
|
MTP::cancel(sentRequest);
|
|
|
|
sentCode = "";
|
|
|
|
}
|
|
|
|
sentRequest = 0;
|
|
|
|
if (!code.isEnabled()) {
|
|
|
|
code.setDisabled(false);
|
|
|
|
code.setFocus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!sentRequest && status == MTP::RequestSent) {
|
|
|
|
stopCheck();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IntroCode::codeSubmitDone(const MTPauth_Authorization &result) {
|
|
|
|
stopCheck();
|
|
|
|
code.setDisabled(false);
|
|
|
|
const MTPDauth_authorization &d(result.c_auth_authorization());
|
2015-06-10 12:48:26 +00:00
|
|
|
if (d.vuser.type() != mtpc_user || !(d.vuser.c_user().vflags.v & MTPDuser_flag_self)) { // wtf?
|
2014-05-30 08:53:19 +00:00
|
|
|
showError(lang(lng_server_error));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cSetLoggedPhoneNumber(intro()->getPhone());
|
|
|
|
intro()->finish(d.vuser);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IntroCode::codeSubmitFail(const RPCError &error) {
|
|
|
|
stopCheck();
|
|
|
|
code.setDisabled(false);
|
|
|
|
const QString &err = error.type();
|
|
|
|
if (err == "PHONE_NUMBER_INVALID" || err == "PHONE_CODE_EXPIRED") { // show error
|
|
|
|
onBack();
|
|
|
|
return true;
|
|
|
|
} else if (err == "PHONE_CODE_EMPTY" || err == "PHONE_CODE_INVALID") {
|
|
|
|
showError(lang(lng_bad_code));
|
|
|
|
code.setFocus();
|
|
|
|
return true;
|
|
|
|
} else if (err == "PHONE_NUMBER_UNOCCUPIED") { // success, need to signUp
|
|
|
|
intro()->setCode(sentCode);
|
|
|
|
intro()->onIntroNext();
|
|
|
|
return true;
|
2015-04-02 10:33:19 +00:00
|
|
|
} else if (err == "SESSION_PASSWORD_NEEDED") {
|
|
|
|
intro()->setCode(sentCode);
|
|
|
|
code.setDisabled(false);
|
|
|
|
checkRequest.start(1000);
|
|
|
|
sentRequest = MTP::send(MTPaccount_GetPassword(), rpcDone(&IntroCode::gotPassword), rpcFail(&IntroCode::codeSubmitFail));
|
|
|
|
return true;
|
2015-04-04 20:01:34 +00:00
|
|
|
} else if (error.type().startsWith(qsl("FLOOD_WAIT_"))) {
|
2014-05-30 08:53:19 +00:00
|
|
|
showError(lang(lng_flood_error));
|
|
|
|
code.setFocus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (cDebug()) { // internal server error
|
|
|
|
showError(err + ": " + error.description());
|
|
|
|
} else {
|
|
|
|
showError(lang(lng_server_error));
|
|
|
|
}
|
|
|
|
code.setFocus();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IntroCode::onInputChange() {
|
|
|
|
showError("");
|
|
|
|
if (code.text().length() == 5) onSubmitCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IntroCode::onSendCall() {
|
|
|
|
if (!--waitTillCall) {
|
|
|
|
callTimer.stop();
|
|
|
|
MTP::send(MTPauth_SendCall(MTP_string(intro()->getPhone()), MTP_string(intro()->getPhoneHash())), rpcDone(&IntroCode::callDone));
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IntroCode::callDone(const MTPBool &v) {
|
|
|
|
if (!waitTillCall) {
|
|
|
|
waitTillCall = -1;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 10:33:19 +00:00
|
|
|
void IntroCode::gotPassword(const MTPaccount_Password &result) {
|
|
|
|
stopCheck();
|
|
|
|
code.setDisabled(false);
|
|
|
|
switch (result.type()) {
|
|
|
|
case mtpc_account_noPassword: // should not happen
|
|
|
|
code.setFocus();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case mtpc_account_password: {
|
|
|
|
const MTPDaccount_password &d(result.c_account_password());
|
|
|
|
intro()->setPwdSalt(qba(d.vcurrent_salt));
|
|
|
|
intro()->setHasRecovery(d.vhas_recovery.v);
|
|
|
|
intro()->setPwdHint(qs(d.vhint));
|
|
|
|
intro()->onIntroNext();
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void IntroCode::onSubmitCode(bool force) {
|
|
|
|
if (!force && (code.text() == sentCode || !code.isEnabled())) return;
|
|
|
|
|
|
|
|
code.setDisabled(true);
|
|
|
|
setFocus();
|
|
|
|
|
|
|
|
showError("");
|
|
|
|
|
|
|
|
checkRequest.start(1000);
|
|
|
|
|
|
|
|
sentCode = code.text();
|
2015-04-02 10:33:19 +00:00
|
|
|
intro()->setPwdSalt(QByteArray());
|
|
|
|
intro()->setHasRecovery(false);
|
|
|
|
intro()->setPwdHint(QString());
|
2014-05-30 08:53:19 +00:00
|
|
|
sentRequest = MTP::send(MTPauth_SignIn(MTP_string(intro()->getPhone()), MTP_string(intro()->getPhoneHash()), MTP_string(sentCode)), rpcDone(&IntroCode::codeSubmitDone), rpcFail(&IntroCode::codeSubmitFail));
|
|
|
|
}
|
|
|
|
|
|
|
|
void IntroCode::onNext() {
|
|
|
|
onSubmitCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IntroCode::onBack() {
|
|
|
|
intro()->onIntroBack();
|
|
|
|
}
|