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
|
2017-01-11 18:31:31 +00:00
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
2016-04-21 17:57:29 +00:00
|
|
|
#include "intro/introsignup.h"
|
|
|
|
|
2016-10-28 12:44:28 +00:00
|
|
|
#include "styles/style_intro.h"
|
2016-10-31 12:29:26 +00:00
|
|
|
#include "styles/style_boxes.h"
|
2017-02-28 14:05:30 +00:00
|
|
|
#include "core/file_utilities.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
#include "boxes/photocropbox.h"
|
2016-12-30 13:53:51 +00:00
|
|
|
#include "boxes/confirmbox.h"
|
2016-04-21 17:57:29 +00:00
|
|
|
#include "lang.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
#include "application.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-24 19:28:23 +00:00
|
|
|
#include "ui/widgets/labels.h"
|
2017-03-07 09:17:37 +00:00
|
|
|
#include "ui/special_buttons.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
namespace Intro {
|
|
|
|
|
|
|
|
SignupWidget::SignupWidget(QWidget *parent, Widget::Data *data) : Step(parent, data)
|
|
|
|
, _photo(this, st::introPhotoSize, st::introPhotoIconPosition)
|
2016-11-15 11:56:49 +00:00
|
|
|
, _first(this, st::introName, lang(lng_signup_firstname))
|
|
|
|
, _last(this, st::introName, lang(lng_signup_lastname))
|
2016-11-04 19:50:35 +00:00
|
|
|
, _invertOrder(langFirstNameGoesSecond())
|
|
|
|
, _checkRequest(this) {
|
|
|
|
connect(_checkRequest, SIGNAL(timeout()), this, SLOT(onCheckRequest()));
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-02-28 15:43:03 +00:00
|
|
|
setupPhotoButton();
|
2016-11-24 19:28:23 +00:00
|
|
|
|
2015-09-23 17:43:08 +00:00
|
|
|
if (_invertOrder) {
|
2016-11-04 19:50:35 +00:00
|
|
|
setTabOrder(_last, _first);
|
2015-09-23 17:43:08 +00:00
|
|
|
}
|
2016-11-24 19:28:23 +00:00
|
|
|
setErrorCentered(true);
|
2015-09-23 17:43:08 +00:00
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
setTitleText(lang(lng_signup_title));
|
|
|
|
setDescriptionText(lang(lng_signup_desc));
|
2014-05-30 08:53:19 +00:00
|
|
|
setMouseTracking(true);
|
|
|
|
}
|
|
|
|
|
2017-02-28 15:43:03 +00:00
|
|
|
void SignupWidget::setupPhotoButton() {
|
|
|
|
_photo->setClickedCallback(App::LambdaDelayed(st::defaultActiveButton.ripple.hideDuration, this, [this] {
|
|
|
|
auto imgExtensions = cImgExtensions();
|
|
|
|
auto filter = qsl("Image files (*") + imgExtensions.join(qsl(" *")) + qsl(");;") + FileDialog::AllFilesFilter();
|
|
|
|
FileDialog::GetOpenPath(lang(lng_choose_image), filter, base::lambda_guarded(this, [this](const FileDialog::OpenResult &result) {
|
|
|
|
if (result.remoteContent.isEmpty() && result.paths.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage img;
|
|
|
|
if (!result.remoteContent.isEmpty()) {
|
|
|
|
img = App::readImage(result.remoteContent);
|
|
|
|
} else {
|
|
|
|
img = App::readImage(result.paths.front());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (img.isNull() || img.width() > 10 * img.height() || img.height() > 10 * img.width()) {
|
|
|
|
showError(lang(lng_bad_photo));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto box = Ui::show(Box<PhotoCropBox>(img, PeerId(0)));
|
|
|
|
connect(box, SIGNAL(ready(const QImage&)), this, SLOT(onPhotoReady(const QImage&)));
|
|
|
|
}));
|
|
|
|
}));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void SignupWidget::resizeEvent(QResizeEvent *e) {
|
|
|
|
Step::resizeEvent(e);
|
2015-12-08 12:33:37 +00:00
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
auto photoRight = contentLeft() + st::introNextButton.width;
|
|
|
|
auto photoTop = contentTop() + st::introPhotoTop;
|
|
|
|
_photo->moveToLeft(photoRight - _photo->width(), photoTop);
|
2015-12-08 12:33:37 +00:00
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
auto firstTop = contentTop() + st::introStepFieldTop;
|
2016-12-09 18:56:01 +00:00
|
|
|
auto secondTop = firstTop + st::introName.heightMin + st::introPhoneTop;
|
2016-11-24 19:28:23 +00:00
|
|
|
if (_invertOrder) {
|
|
|
|
_last->moveToLeft(contentLeft(), firstTop);
|
|
|
|
_first->moveToLeft(contentLeft(), secondTop);
|
2015-12-08 12:33:37 +00:00
|
|
|
} else {
|
2016-11-24 19:28:23 +00:00
|
|
|
_first->moveToLeft(contentLeft(), firstTop);
|
|
|
|
_last->moveToLeft(contentLeft(), secondTop);
|
2015-12-08 12:33:37 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void SignupWidget::setInnerFocus() {
|
|
|
|
if (_invertOrder || _last->hasFocus()) {
|
2016-12-23 13:21:01 +00:00
|
|
|
_last->setFocusFast();
|
2015-09-23 17:43:08 +00:00
|
|
|
} else {
|
2016-12-23 13:21:01 +00:00
|
|
|
_first->setFocusFast();
|
2015-09-23 17:43:08 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void SignupWidget::activate() {
|
|
|
|
Step::activate();
|
|
|
|
_first->show();
|
|
|
|
_last->show();
|
|
|
|
_photo->show();
|
|
|
|
setInnerFocus();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void SignupWidget::cancelled() {
|
|
|
|
MTP::cancel(base::take(_sentRequest));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SignupWidget::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 SignupWidget::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 SignupWidget::onPhotoReady(const QImage &img) {
|
|
|
|
_photoImage = img;
|
|
|
|
_photo->setImage(_photoImage);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void SignupWidget::nameSubmitDone(const MTPauth_Authorization &result) {
|
2014-05-30 08:53:19 +00:00
|
|
|
stopCheck();
|
2016-12-09 18:56:01 +00:00
|
|
|
auto &d = result.c_auth_authorization();
|
2015-10-29 00:16:52 +00:00
|
|
|
if (d.vuser.type() != mtpc_user || !d.vuser.c_user().is_self()) { // wtf?
|
2014-05-30 08:53:19 +00:00
|
|
|
showError(lang(lng_server_error));
|
|
|
|
return;
|
|
|
|
}
|
2016-11-24 19:28:23 +00:00
|
|
|
finish(d.vuser, _photoImage);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
bool SignupWidget::nameSubmitFail(const RPCError &error) {
|
2016-04-08 10:44:35 +00:00
|
|
|
if (MTP::isFloodError(error)) {
|
|
|
|
stopCheck();
|
|
|
|
showError(lang(lng_flood_error));
|
|
|
|
if (_invertOrder) {
|
2016-11-04 19:50:35 +00:00
|
|
|
_first->setFocus();
|
2016-04-08 10:44:35 +00:00
|
|
|
} else {
|
2016-11-04 19:50:35 +00:00
|
|
|
_last->setFocus();
|
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-12-09 18:56:01 +00:00
|
|
|
auto &err = error.type();
|
2016-12-30 13:53:51 +00:00
|
|
|
if (err == qstr("PHONE_NUMBER_FLOOD")) {
|
|
|
|
Ui::show(Box<InformBox>(lang(lng_error_phone_flood)));
|
|
|
|
return true;
|
|
|
|
} else if (err == qstr("PHONE_NUMBER_INVALID") || err == qstr("PHONE_CODE_EXPIRED") ||
|
2016-04-08 10:44:35 +00:00
|
|
|
err == qstr("PHONE_CODE_EMPTY") || err == qstr("PHONE_CODE_INVALID") ||
|
|
|
|
err == qstr("PHONE_NUMBER_OCCUPIED")) {
|
2016-11-24 19:28:23 +00:00
|
|
|
goBack();
|
2014-05-30 08:53:19 +00:00
|
|
|
return true;
|
|
|
|
} else if (err == "FIRSTNAME_INVALID") {
|
|
|
|
showError(lang(lng_bad_name));
|
2016-11-04 19:50:35 +00:00
|
|
|
_first->setFocus();
|
2014-05-30 08:53:19 +00:00
|
|
|
return true;
|
|
|
|
} else if (err == "LASTNAME_INVALID") {
|
|
|
|
showError(lang(lng_bad_name));
|
2016-11-04 19:50:35 +00:00
|
|
|
_last->setFocus();
|
2014-05-30 08:53:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (cDebug()) { // internal server error
|
|
|
|
showError(err + ": " + error.description());
|
|
|
|
} else {
|
|
|
|
showError(lang(lng_server_error));
|
|
|
|
}
|
2015-09-23 17:43:08 +00:00
|
|
|
if (_invertOrder) {
|
2016-11-04 19:50:35 +00:00
|
|
|
_last->setFocus();
|
2015-09-23 17:43:08 +00:00
|
|
|
} else {
|
2016-11-04 19:50:35 +00:00
|
|
|
_first->setFocus();
|
2015-09-23 17:43:08 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void SignupWidget::onInputChange() {
|
2016-03-14 16:59:18 +00:00
|
|
|
showError(QString());
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
void SignupWidget::submit() {
|
2016-12-09 18:56:01 +00:00
|
|
|
if (_sentRequest) return;
|
2015-09-23 17:43:08 +00:00
|
|
|
if (_invertOrder) {
|
2016-11-24 19:28:23 +00:00
|
|
|
if ((_last->hasFocus() || _last->getLastText().trimmed().length()) && !_first->getLastText().trimmed().length()) {
|
2016-11-04 19:50:35 +00:00
|
|
|
_first->setFocus();
|
2015-09-23 17:43:08 +00:00
|
|
|
return;
|
2016-11-24 19:28:23 +00:00
|
|
|
} else if (!_last->getLastText().trimmed().length()) {
|
2016-11-04 19:50:35 +00:00
|
|
|
_last->setFocus();
|
2015-09-23 17:43:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2016-11-24 19:28:23 +00:00
|
|
|
if ((_first->hasFocus() || _first->getLastText().trimmed().length()) && !_last->getLastText().trimmed().length()) {
|
2016-11-04 19:50:35 +00:00
|
|
|
_last->setFocus();
|
2015-09-23 17:43:08 +00:00
|
|
|
return;
|
2016-11-24 19:28:23 +00:00
|
|
|
} else if (!_first->getLastText().trimmed().length()) {
|
2016-11-04 19:50:35 +00:00
|
|
|
_first->setFocus();
|
2015-09-23 17:43:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 16:59:18 +00:00
|
|
|
showError(QString());
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
_firstName = _first->getLastText().trimmed();
|
|
|
|
_lastName = _last->getLastText().trimmed();
|
2017-03-10 19:46:28 +00:00
|
|
|
_sentRequest = MTP::send(MTPauth_SignUp(MTP_string(getData()->phone), MTP_bytes(getData()->phoneHash), MTP_string(getData()->code), MTP_string(_firstName), MTP_string(_lastName)), rpcDone(&SignupWidget::nameSubmitDone), rpcFail(&SignupWidget::nameSubmitFail));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
QString SignupWidget::nextButtonText() const {
|
|
|
|
return lang(lng_intro_finish);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-11-24 19:28:23 +00:00
|
|
|
|
|
|
|
} // namespace Intro
|