2014-10-22 18:39:03 +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.
|
2014-10-22 18:39:03 +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
|
2014-10-22 18:39:03 +00:00
|
|
|
*/
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/username_box.h"
|
2014-10-22 18:39:03 +00:00
|
|
|
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2014-10-22 18:39:03 +00:00
|
|
|
#include "mainwidget.h"
|
2016-04-12 21:31:28 +00:00
|
|
|
#include "mainwindow.h"
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
2019-09-16 11:14:06 +00:00
|
|
|
#include "ui/special_fields.h"
|
2016-12-20 13:03:51 +00:00
|
|
|
#include "ui/toast/toast.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/application.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2019-01-18 12:27:37 +00:00
|
|
|
#include "data/data_session.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_user.h"
|
2020-05-28 10:00:51 +00:00
|
|
|
#include "mtproto/facade.h"
|
2019-09-18 11:19:05 +00:00
|
|
|
#include "styles/style_layers.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "styles/style_boxes.h"
|
2014-10-22 18:39:03 +00:00
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtGui/QGuiApplication>
|
|
|
|
#include <QtGui/QClipboard>
|
|
|
|
|
2018-01-22 10:58:11 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kMinUsernameLength = 5;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-08-06 16:40:08 +00:00
|
|
|
UsernameBox::UsernameBox(QWidget*, not_null<Main::Session*> session)
|
|
|
|
: _session(session)
|
|
|
|
, _username(
|
2018-09-11 12:50:40 +00:00
|
|
|
this,
|
|
|
|
st::defaultInputField,
|
2019-06-18 14:07:45 +00:00
|
|
|
rpl::single(qsl("@username")),
|
2019-08-06 16:40:08 +00:00
|
|
|
session->user()->username,
|
2018-09-11 12:50:40 +00:00
|
|
|
false)
|
2016-12-09 18:56:01 +00:00
|
|
|
, _link(this, QString(), st::boxLinkButton)
|
2016-12-13 17:07:56 +00:00
|
|
|
, _about(st::boxWidth - st::usernamePadding.left())
|
|
|
|
, _checkTimer(this) {
|
|
|
|
}
|
2015-10-11 08:37:24 +00:00
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
void UsernameBox::prepare() {
|
2019-08-06 16:40:08 +00:00
|
|
|
_goodText = _session->user()->username.isEmpty()
|
2018-09-11 12:50:40 +00:00
|
|
|
? QString()
|
2019-06-19 15:09:03 +00:00
|
|
|
: tr::lng_username_available(tr::now);
|
2014-10-22 18:39:03 +00:00
|
|
|
|
2019-06-18 15:00:55 +00:00
|
|
|
setTitle(tr::lng_username_title());
|
2016-12-13 17:07:56 +00:00
|
|
|
|
2019-06-18 16:53:27 +00:00
|
|
|
addButton(tr::lng_settings_save(), [=] { save(); });
|
|
|
|
addButton(tr::lng_cancel(), [=] { closeBox(); });
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2018-05-31 12:20:28 +00:00
|
|
|
connect(_username, &Ui::MaskedInputField::changed, [=] { changed(); });
|
|
|
|
connect(_username, &Ui::MaskedInputField::submitted, [=] { save(); });
|
|
|
|
_link->addClickHandler([=] { linkClick(); });
|
2014-10-22 18:39:03 +00:00
|
|
|
|
2019-06-19 15:09:03 +00:00
|
|
|
_about.setText(st::usernameTextStyle, tr::lng_username_about(tr::now));
|
2016-12-13 17:07:56 +00:00
|
|
|
setDimensions(st::boxWidth, st::usernamePadding.top() + _username->height() + st::usernameSkip + _about.countHeight(st::boxWidth - st::usernamePadding.left()) + 3 * st::usernameTextStyle.lineHeight + st::usernamePadding.bottom());
|
|
|
|
|
|
|
|
_checkTimer->setSingleShot(true);
|
2018-05-31 12:20:28 +00:00
|
|
|
connect(_checkTimer, &QTimer::timeout, [=] { check(); });
|
2014-10-22 18:39:03 +00:00
|
|
|
|
2015-10-06 19:49:23 +00:00
|
|
|
updateLinkText();
|
2014-10-22 18:39:03 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
void UsernameBox::setInnerFocus() {
|
2016-12-20 13:03:51 +00:00
|
|
|
_username->setFocusFast();
|
2014-10-22 18:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UsernameBox::paintEvent(QPaintEvent *e) {
|
2016-12-13 17:07:56 +00:00
|
|
|
BoxContent::paintEvent(e);
|
2015-04-02 10:33:19 +00:00
|
|
|
|
2016-11-19 14:47:28 +00:00
|
|
|
Painter p(this);
|
2015-04-02 10:33:19 +00:00
|
|
|
|
2016-07-28 17:01:08 +00:00
|
|
|
p.setFont(st::boxTextFont);
|
2016-12-20 13:03:51 +00:00
|
|
|
if (!_errorText.isEmpty()) {
|
2016-10-31 12:29:26 +00:00
|
|
|
p.setPen(st::boxTextFgError);
|
2016-11-11 13:46:04 +00:00
|
|
|
p.drawTextLeft(st::usernamePadding.left(), _username->y() + _username->height() + ((st::usernameSkip - st::boxTextFont->height) / 2), width(), _errorText);
|
2015-04-02 10:33:19 +00:00
|
|
|
} else if (!_goodText.isEmpty()) {
|
2016-10-31 12:29:26 +00:00
|
|
|
p.setPen(st::boxTextFgGood);
|
2016-11-11 13:46:04 +00:00
|
|
|
p.drawTextLeft(st::usernamePadding.left(), _username->y() + _username->height() + ((st::usernameSkip - st::boxTextFont->height) / 2), width(), _goodText);
|
2015-10-06 19:49:23 +00:00
|
|
|
} else {
|
|
|
|
p.setPen(st::usernameDefaultFg);
|
2019-06-19 15:09:03 +00:00
|
|
|
p.drawTextLeft(st::usernamePadding.left(), _username->y() + _username->height() + ((st::usernameSkip - st::boxTextFont->height) / 2), width(), tr::lng_username_choose(tr::now));
|
2015-10-06 19:49:23 +00:00
|
|
|
}
|
2016-10-31 12:29:26 +00:00
|
|
|
p.setPen(st::boxTextFg);
|
2015-10-14 21:49:50 +00:00
|
|
|
int32 availw = st::boxWidth - st::usernamePadding.left(), h = _about.countHeight(availw);
|
2016-11-11 13:46:04 +00:00
|
|
|
_about.drawLeft(p, st::usernamePadding.left(), _username->y() + _username->height() + st::usernameSkip, availw, width());
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
int32 linky = _username->y() + _username->height() + st::usernameSkip + h + st::usernameTextStyle.lineHeight + ((st::usernameTextStyle.lineHeight - st::boxTextFont->height) / 2);
|
|
|
|
if (_link->isHidden()) {
|
2019-06-19 15:09:03 +00:00
|
|
|
p.drawTextLeft(st::usernamePadding.left(), linky, width(), tr::lng_username_link_willbe(tr::now));
|
2015-10-06 19:49:23 +00:00
|
|
|
p.setPen(st::usernameDefaultFg);
|
2019-01-21 13:42:21 +00:00
|
|
|
p.drawTextLeft(st::usernamePadding.left(), linky + st::usernameTextStyle.lineHeight + ((st::usernameTextStyle.lineHeight - st::boxTextFont->height) / 2), width(), Core::App().createInternalLinkFull(qsl("username")));
|
2015-10-06 19:49:23 +00:00
|
|
|
} else {
|
2019-06-19 15:09:03 +00:00
|
|
|
p.drawTextLeft(st::usernamePadding.left(), linky, width(), tr::lng_username_link(tr::now));
|
2014-10-22 18:39:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 10:33:19 +00:00
|
|
|
void UsernameBox::resizeEvent(QResizeEvent *e) {
|
2016-12-13 17:07:56 +00:00
|
|
|
BoxContent::resizeEvent(e);
|
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
_username->resize(width() - st::usernamePadding.left() - st::usernamePadding.right(), _username->height());
|
2016-12-13 17:07:56 +00:00
|
|
|
_username->moveToLeft(st::usernamePadding.left(), st::usernamePadding.top());
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2015-10-14 21:49:50 +00:00
|
|
|
int32 availw = st::boxWidth - st::usernamePadding.left(), h = _about.countHeight(availw);
|
2016-11-11 13:46:04 +00:00
|
|
|
int32 linky = _username->y() + _username->height() + st::usernameSkip + h + st::usernameTextStyle.lineHeight + ((st::usernameTextStyle.lineHeight - st::boxTextFont->height) / 2);
|
|
|
|
_link->moveToLeft(st::usernamePadding.left(), linky + st::usernameTextStyle.lineHeight + ((st::usernameTextStyle.lineHeight - st::boxTextFont->height) / 2));
|
2014-10-22 18:39:03 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 12:20:28 +00:00
|
|
|
void UsernameBox::save() {
|
2015-10-06 19:49:23 +00:00
|
|
|
if (_saveRequestId) return;
|
2014-10-22 18:39:03 +00:00
|
|
|
|
|
|
|
_sentUsername = getName();
|
2015-10-06 19:49:23 +00:00
|
|
|
_saveRequestId = MTP::send(MTPaccount_UpdateUsername(MTP_string(_sentUsername)), rpcDone(&UsernameBox::onUpdateDone), rpcFail(&UsernameBox::onUpdateFail));
|
2014-10-22 18:39:03 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 12:20:28 +00:00
|
|
|
void UsernameBox::check() {
|
2015-10-06 19:49:23 +00:00
|
|
|
if (_checkRequestId) {
|
|
|
|
MTP::cancel(_checkRequestId);
|
2014-10-22 18:39:03 +00:00
|
|
|
}
|
|
|
|
QString name = getName();
|
2018-01-22 10:58:11 +00:00
|
|
|
if (name.size() >= kMinUsernameLength) {
|
2015-09-16 13:04:08 +00:00
|
|
|
_checkUsername = name;
|
2018-01-22 10:58:11 +00:00
|
|
|
_checkRequestId = MTP::send(
|
|
|
|
MTPaccount_CheckUsername(
|
|
|
|
MTP_string(name)),
|
|
|
|
rpcDone(&UsernameBox::onCheckDone),
|
|
|
|
rpcFail(&UsernameBox::onCheckFail));
|
2014-10-22 18:39:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 12:20:28 +00:00
|
|
|
void UsernameBox::changed() {
|
2015-10-06 19:49:23 +00:00
|
|
|
updateLinkText();
|
2014-10-22 18:39:03 +00:00
|
|
|
QString name = getName();
|
|
|
|
if (name.isEmpty()) {
|
2014-11-25 12:15:29 +00:00
|
|
|
if (!_errorText.isEmpty() || !_goodText.isEmpty()) {
|
2016-12-20 13:03:51 +00:00
|
|
|
_errorText = _goodText = QString();
|
2014-10-22 18:39:03 +00:00
|
|
|
update();
|
|
|
|
}
|
2016-12-13 17:07:56 +00:00
|
|
|
_checkTimer->stop();
|
2014-10-22 18:39:03 +00:00
|
|
|
} else {
|
2016-03-20 08:16:35 +00:00
|
|
|
int32 len = name.size();
|
2014-11-25 12:15:29 +00:00
|
|
|
for (int32 i = 0; i < len; ++i) {
|
|
|
|
QChar ch = name.at(i);
|
|
|
|
if ((ch < 'A' || ch > 'Z') && (ch < 'a' || ch > 'z') && (ch < '0' || ch > '9') && ch != '_' && (ch != '@' || i > 0)) {
|
2019-06-19 15:09:03 +00:00
|
|
|
if (_errorText != tr::lng_username_bad_symbols(tr::now)) {
|
|
|
|
_errorText = tr::lng_username_bad_symbols(tr::now);
|
2014-11-25 12:15:29 +00:00
|
|
|
update();
|
|
|
|
}
|
2016-12-13 17:07:56 +00:00
|
|
|
_checkTimer->stop();
|
2014-11-25 12:15:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-01-22 10:58:11 +00:00
|
|
|
if (name.size() < kMinUsernameLength) {
|
2019-06-19 15:09:03 +00:00
|
|
|
if (_errorText != tr::lng_username_too_short(tr::now)) {
|
|
|
|
_errorText = tr::lng_username_too_short(tr::now);
|
2014-11-25 12:15:29 +00:00
|
|
|
update();
|
|
|
|
}
|
2016-12-13 17:07:56 +00:00
|
|
|
_checkTimer->stop();
|
2014-11-25 12:15:29 +00:00
|
|
|
} else {
|
2016-12-20 13:03:51 +00:00
|
|
|
if (!_errorText.isEmpty() || !_goodText.isEmpty()) {
|
|
|
|
_errorText = _goodText = QString();
|
2014-11-25 12:15:29 +00:00
|
|
|
update();
|
|
|
|
}
|
2016-12-13 17:07:56 +00:00
|
|
|
_checkTimer->start(UsernameCheckTimeout);
|
2014-10-22 18:39:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 12:20:28 +00:00
|
|
|
void UsernameBox::linkClick() {
|
2019-09-04 07:19:15 +00:00
|
|
|
QGuiApplication::clipboard()->setText(Core::App().createInternalLinkFull(getName()));
|
2019-06-19 15:09:03 +00:00
|
|
|
Ui::Toast::Show(tr::lng_username_copied(tr::now));
|
2015-10-06 19:49:23 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 18:39:03 +00:00
|
|
|
void UsernameBox::onUpdateDone(const MTPUser &user) {
|
2019-08-06 16:40:08 +00:00
|
|
|
_session->data().processUser(user);
|
2016-12-13 17:07:56 +00:00
|
|
|
closeBox();
|
2014-10-22 18:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UsernameBox::onUpdateFail(const RPCError &error) {
|
2016-04-08 10:44:35 +00:00
|
|
|
if (MTP::isDefaultHandledError(error)) return false;
|
2015-04-04 20:01:34 +00:00
|
|
|
|
2015-10-06 19:49:23 +00:00
|
|
|
_saveRequestId = 0;
|
2019-08-06 16:40:08 +00:00
|
|
|
const auto self = _session->user();
|
2019-01-18 11:26:43 +00:00
|
|
|
const auto &err = error.type();
|
2018-09-11 12:50:40 +00:00
|
|
|
if (err == qstr("USERNAME_NOT_MODIFIED") || _sentUsername == self->username) {
|
|
|
|
self->setName(
|
|
|
|
TextUtilities::SingleLine(self->firstName),
|
|
|
|
TextUtilities::SingleLine(self->lastName),
|
|
|
|
TextUtilities::SingleLine(self->nameOrPhone),
|
|
|
|
TextUtilities::SingleLine(_sentUsername));
|
2016-12-13 17:07:56 +00:00
|
|
|
closeBox();
|
2014-10-22 18:39:03 +00:00
|
|
|
return true;
|
2016-04-08 10:44:35 +00:00
|
|
|
} else if (err == qstr("USERNAME_INVALID")) {
|
2016-11-11 13:46:04 +00:00
|
|
|
_username->setFocus();
|
|
|
|
_username->showError();
|
2019-06-19 15:09:03 +00:00
|
|
|
_errorText = tr::lng_username_invalid(tr::now);
|
2015-10-06 19:49:23 +00:00
|
|
|
update();
|
2014-10-22 18:39:03 +00:00
|
|
|
return true;
|
2016-04-08 10:44:35 +00:00
|
|
|
} else if (err == qstr("USERNAME_OCCUPIED") || err == qstr("USERNAMES_UNAVAILABLE")) {
|
2016-11-11 13:46:04 +00:00
|
|
|
_username->setFocus();
|
|
|
|
_username->showError();
|
2019-06-19 15:09:03 +00:00
|
|
|
_errorText = tr::lng_username_occupied(tr::now);
|
2015-10-06 19:49:23 +00:00
|
|
|
update();
|
2014-10-22 18:39:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-11-11 13:46:04 +00:00
|
|
|
_username->setFocus();
|
2014-10-22 18:39:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UsernameBox::onCheckDone(const MTPBool &result) {
|
2015-10-06 19:49:23 +00:00
|
|
|
_checkRequestId = 0;
|
2018-09-11 12:50:40 +00:00
|
|
|
const auto newError = (mtpIsTrue(result)
|
2019-08-06 16:40:08 +00:00
|
|
|
|| _checkUsername == _session->user()->username)
|
2018-09-11 12:50:40 +00:00
|
|
|
? QString()
|
2019-06-19 15:09:03 +00:00
|
|
|
: tr::lng_username_occupied(tr::now);
|
2018-09-11 12:50:40 +00:00
|
|
|
const auto newGood = newError.isEmpty()
|
2019-06-19 15:09:03 +00:00
|
|
|
? tr::lng_username_available(tr::now)
|
2018-09-11 12:50:40 +00:00
|
|
|
: QString();
|
2016-12-20 13:03:51 +00:00
|
|
|
if (_errorText != newError || _goodText != newGood) {
|
2014-10-22 18:39:03 +00:00
|
|
|
_errorText = newError;
|
2014-11-25 12:15:29 +00:00
|
|
|
_goodText = newGood;
|
2014-10-22 18:39:03 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UsernameBox::onCheckFail(const RPCError &error) {
|
2016-04-08 10:44:35 +00:00
|
|
|
if (MTP::isDefaultHandledError(error)) return false;
|
2015-04-04 20:01:34 +00:00
|
|
|
|
2015-10-06 19:49:23 +00:00
|
|
|
_checkRequestId = 0;
|
2014-10-22 18:39:03 +00:00
|
|
|
QString err(error.type());
|
2016-04-08 10:44:35 +00:00
|
|
|
if (err == qstr("USERNAME_INVALID")) {
|
2019-06-19 15:09:03 +00:00
|
|
|
_errorText = tr::lng_username_invalid(tr::now);
|
2014-10-22 18:39:03 +00:00
|
|
|
update();
|
|
|
|
return true;
|
2019-08-06 16:40:08 +00:00
|
|
|
} else if (err == qstr("USERNAME_OCCUPIED") && _checkUsername != _session->user()->username) {
|
2019-06-19 15:09:03 +00:00
|
|
|
_errorText = tr::lng_username_occupied(tr::now);
|
2014-10-22 18:39:03 +00:00
|
|
|
update();
|
|
|
|
return true;
|
|
|
|
}
|
2014-11-25 12:15:29 +00:00
|
|
|
_goodText = QString();
|
2016-11-11 13:46:04 +00:00
|
|
|
_username->setFocus();
|
2014-10-22 18:39:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString UsernameBox::getName() const {
|
2016-11-11 13:46:04 +00:00
|
|
|
return _username->text().replace('@', QString()).trimmed();
|
2014-10-22 18:39:03 +00:00
|
|
|
}
|
2015-10-06 19:49:23 +00:00
|
|
|
|
|
|
|
void UsernameBox::updateLinkText() {
|
|
|
|
QString uname = getName();
|
2019-01-21 13:42:21 +00:00
|
|
|
_link->setText(st::boxTextFont->elided(Core::App().createInternalLinkFull(uname), st::boxWidth - st::usernamePadding.left() - st::usernamePadding.right()));
|
2015-10-06 19:49:23 +00:00
|
|
|
if (uname.isEmpty()) {
|
2016-11-11 13:46:04 +00:00
|
|
|
if (!_link->isHidden()) {
|
|
|
|
_link->hide();
|
2015-10-06 19:49:23 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
} else {
|
2016-11-11 13:46:04 +00:00
|
|
|
if (_link->isHidden()) {
|
|
|
|
_link->show();
|
2015-10-06 19:49:23 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|