tdesktop/Telegram/SourceFiles/boxes/username_box.cpp

268 lines
8.5 KiB
C++
Raw Normal View History

2014-10-22 18:39:03 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
2014-10-22 18:39:03 +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"
#include "mainwindow.h"
#include "ui/widgets/buttons.h"
2019-09-16 11:14:06 +00:00
#include "ui/special_fields.h"
#include "ui/toast/toast.h"
#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"
#include "data/data_user.h"
2019-09-18 11:19:05 +00:00
#include "styles/style_layers.h"
#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>
namespace {
constexpr auto kMinUsernameLength = 5;
} // namespace
2019-08-06 16:40:08 +00:00
UsernameBox::UsernameBox(QWidget*, not_null<Main::Session*> session)
: _session(session)
, _api(&_session->mtp())
2019-08-06 16:40:08 +00:00
, _username(
this,
st::defaultInputField,
rpl::single(qsl("@username")),
2019-08-06 16:40:08 +00:00
session->user()->username,
QString())
, _link(this, QString(), st::boxLinkButton)
, _about(st::boxWidth - st::usernamePadding.left())
, _checkTimer(this) {
}
2015-10-11 08:37:24 +00:00
void UsernameBox::prepare() {
2019-08-06 16:40:08 +00:00
_goodText = _session->user()->username.isEmpty()
? QString()
2019-06-19 15:09:03 +00:00
: tr::lng_username_available(tr::now);
2014-10-22 18:39:03 +00:00
setTitle(tr::lng_username_title());
addButton(tr::lng_settings_save(), [=] { save(); });
addButton(tr::lng_cancel(), [=] { closeBox(); });
2015-10-06 19:49:23 +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));
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);
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
}
void UsernameBox::setInnerFocus() {
_username->setFocusFast();
2014-10-22 18:39:03 +00:00
}
void UsernameBox::paintEvent(QPaintEvent *e) {
BoxContent::paintEvent(e);
Painter p(this);
p.setFont(st::boxTextFont);
if (!_errorText.isEmpty()) {
2016-10-31 12:29:26 +00:00
p.setPen(st::boxTextFgError);
p.drawTextLeft(st::usernamePadding.left(), _username->y() + _username->height() + ((st::usernameSkip - st::boxTextFont->height) / 2), width(), _errorText);
} else if (!_goodText.isEmpty()) {
2016-10-31 12:29:26 +00:00
p.setPen(st::boxTextFgGood);
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);
int32 availw = st::boxWidth - st::usernamePadding.left(), h = _about.countHeight(availw);
_about.drawLeft(p, st::usernamePadding.left(), _username->y() + _username->height() + st::usernameSkip, availw, width());
2015-10-06 19:49:23 +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);
const auto link = _session->createInternalLinkFull(qsl("username"));
p.drawTextLeft(st::usernamePadding.left(), linky + st::usernameTextStyle.lineHeight + ((st::usernameTextStyle.lineHeight - st::boxTextFont->height) / 2), width(), link);
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
}
}
void UsernameBox::resizeEvent(QResizeEvent *e) {
BoxContent::resizeEvent(e);
_username->resize(width() - st::usernamePadding.left() - st::usernamePadding.right(), _username->height());
_username->moveToLeft(st::usernamePadding.left(), st::usernamePadding.top());
2015-10-06 19:49:23 +00:00
int32 availw = st::boxWidth - st::usernamePadding.left(), h = _about.countHeight(availw);
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
}
void UsernameBox::save() {
2015-10-06 19:49:23 +00:00
if (_saveRequestId) return;
2014-10-22 18:39:03 +00:00
_sentUsername = getName();
_saveRequestId = _api.request(MTPaccount_UpdateUsername(
MTP_string(_sentUsername)
)).done([=](const MTPUser &result) {
updateDone(result);
}).fail([=](const RPCError &error) {
updateFail(error);
}).send();
2014-10-22 18:39:03 +00:00
}
void UsernameBox::check() {
_api.request(base::take(_checkRequestId)).cancel();
2014-10-22 18:39:03 +00:00
QString name = getName();
if (name.size() >= kMinUsernameLength) {
2015-09-16 13:04:08 +00:00
_checkUsername = name;
_checkRequestId = _api.request(MTPaccount_CheckUsername(
MTP_string(name)
)).done([=](const MTPBool &result) {
checkDone(result);
}).fail([=](const RPCError &error) {
checkFail(error);
}).send();
2014-10-22 18:39:03 +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()) {
if (!_errorText.isEmpty() || !_goodText.isEmpty()) {
_errorText = _goodText = QString();
2014-10-22 18:39:03 +00:00
update();
}
_checkTimer->stop();
2014-10-22 18:39:03 +00:00
} else {
int32 len = name.size();
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);
update();
}
_checkTimer->stop();
return;
}
}
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);
update();
}
_checkTimer->stop();
} else {
if (!_errorText.isEmpty() || !_goodText.isEmpty()) {
_errorText = _goodText = QString();
update();
}
_checkTimer->start(UsernameCheckTimeout);
2014-10-22 18:39:03 +00:00
}
}
}
void UsernameBox::linkClick() {
QGuiApplication::clipboard()->setText(
_session->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
}
void UsernameBox::updateDone(const MTPUser &user) {
2019-08-06 16:40:08 +00:00
_session->data().processUser(user);
closeBox();
2014-10-22 18:39:03 +00:00
}
void UsernameBox::updateFail(const RPCError &error) {
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();
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));
closeBox();
} else if (err == qstr("USERNAME_INVALID")) {
_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();
} else if (err == qstr("USERNAME_OCCUPIED") || err == qstr("USERNAMES_UNAVAILABLE")) {
_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();
} else {
_username->setFocus();
2014-10-22 18:39:03 +00:00
}
}
void UsernameBox::checkDone(const MTPBool &result) {
2015-10-06 19:49:23 +00:00
_checkRequestId = 0;
const auto newError = (mtpIsTrue(result)
2019-08-06 16:40:08 +00:00
|| _checkUsername == _session->user()->username)
? QString()
2019-06-19 15:09:03 +00:00
: tr::lng_username_occupied(tr::now);
const auto newGood = newError.isEmpty()
2019-06-19 15:09:03 +00:00
? tr::lng_username_available(tr::now)
: QString();
if (_errorText != newError || _goodText != newGood) {
2014-10-22 18:39:03 +00:00
_errorText = newError;
_goodText = newGood;
2014-10-22 18:39:03 +00:00
update();
}
}
void UsernameBox::checkFail(const RPCError &error) {
2015-10-06 19:49:23 +00:00
_checkRequestId = 0;
2014-10-22 18:39:03 +00:00
QString err(error.type());
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();
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();
} else {
_goodText = QString();
_username->setFocus();
2014-10-22 18:39:03 +00:00
}
}
QString UsernameBox::getName() const {
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();
_link->setText(st::boxTextFont->elided(_session->createInternalLinkFull(uname), st::boxWidth - st::usernamePadding.left() - st::usernamePadding.right()));
2015-10-06 19:49:23 +00:00
if (uname.isEmpty()) {
if (!_link->isHidden()) {
_link->hide();
2015-10-06 19:49:23 +00:00
update();
}
} else {
if (_link->isHidden()) {
_link->show();
2015-10-06 19:49:23 +00:00
update();
}
}
}