tdesktop/Telegram/SourceFiles/boxes/username_box.cpp

306 lines
7.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
2022-10-11 18:10:15 +00:00
#include "base/timer.h"
#include "boxes/peers/edit_peer_common.h"
2022-10-11 18:10:15 +00:00
#include "data/data_session.h"
#include "data/data_user.h"
2017-04-13 08:27:10 +00:00
#include "lang/lang_keys.h"
2022-10-11 18:10:15 +00:00
#include "main/main_session.h"
#include "mtproto/sender.h"
#include "settings/settings_common.h"
#include "ui/layers/generic_box.h"
#include "ui/painter.h"
#include "ui/text/text_utilities.h"
#include "ui/toast/toast.h"
#include "ui/widgets/buttons.h"
#include "ui/widgets/fields/special_fields.h"
2022-10-11 18:10:15 +00:00
#include "ui/widgets/labels.h"
2019-09-18 11:19:05 +00:00
#include "styles/style_layers.h"
#include "styles/style_boxes.h"
2022-10-11 18:10:15 +00:00
#include "styles/style_settings.h"
namespace {
class UsernameEditor final : public Ui::RpWidget {
public:
UsernameEditor(not_null<Ui::RpWidget*>, not_null<Main::Session*> session);
2014-10-22 18:39:03 +00:00
2022-10-11 18:10:15 +00:00
void setInnerFocus();
void save();
2019-09-04 07:19:15 +00:00
2022-10-11 18:10:15 +00:00
[[nodiscard]] rpl::producer<> closeRequests() const;
protected:
void paintEvent(QPaintEvent *e) override;
void resizeEvent(QResizeEvent *e) override;
private:
void updateFail(const QString &error);
void checkFail(const QString &error);
void check();
void changed();
QString getName() const;
const not_null<Main::Session*> _session;
const style::font &_font;
const style::margins &_padding;
MTP::Sender _api;
object_ptr<Ui::UsernameInput> _username;
mtpRequestId _saveRequestId = 0;
mtpRequestId _checkRequestId = 0;
QString _sentUsername, _checkUsername, _errorText, _goodText;
base::Timer _checkTimer;
rpl::event_stream<> _closeRequests;
};
UsernameEditor::UsernameEditor(
not_null<Ui::RpWidget*>,
not_null<Main::Session*> session)
2019-08-06 16:40:08 +00:00
: _session(session)
, _font(st::normalFont)
2021-10-17 21:30:25 +00:00
, _padding(st::usernamePadding)
, _api(&_session->mtp())
2019-08-06 16:40:08 +00:00
, _username(
this,
st::defaultInputField,
rpl::single(qsl("@username")),
session->user()->username(),
QString())
2021-10-17 21:30:25 +00:00
, _checkTimer([=] { check(); }) {
_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
connect(_username, &Ui::MaskedInputField::changed, [=] { changed(); });
connect(_username, &Ui::MaskedInputField::submitted, [=] { save(); });
2022-10-11 18:10:15 +00:00
resize(
width(),
(_padding.top()
+ _username->height()
+ st::usernameSkip));
2014-10-22 18:39:03 +00:00
}
2022-10-11 18:10:15 +00:00
rpl::producer<> UsernameEditor::closeRequests() const {
return _closeRequests.events();
2014-10-22 18:39:03 +00:00
}
2022-10-11 18:10:15 +00:00
void UsernameEditor::setInnerFocus() {
_username->setFocusFast();
}
2022-10-11 18:10:15 +00:00
void UsernameEditor::paintEvent(QPaintEvent *e) {
Painter p(this);
2021-10-17 21:30:25 +00:00
const auto textTop = _username->y()
+ _username->height()
+ ((st::usernameSkip - _font->height) / 2);
p.setFont(_font);
if (!_errorText.isEmpty()) {
2016-10-31 12:29:26 +00:00
p.setPen(st::boxTextFgError);
2021-10-17 21:30:25 +00:00
p.drawTextLeft(
_padding.left(),
textTop,
width(),
_errorText);
} else if (!_goodText.isEmpty()) {
2016-10-31 12:29:26 +00:00
p.setPen(st::boxTextFgGood);
2021-10-17 21:30:25 +00:00
p.drawTextLeft(
_padding.left(),
textTop,
width(),
_goodText);
2015-10-06 19:49:23 +00:00
} else {
p.setPen(st::usernameDefaultFg);
2021-10-17 21:30:25 +00:00
p.drawTextLeft(
_padding.left(),
textTop,
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);
2014-10-22 18:39:03 +00:00
}
2022-10-11 18:10:15 +00:00
void UsernameEditor::resizeEvent(QResizeEvent *e) {
2021-10-17 21:30:25 +00:00
_username->resize(
width() - _padding.left() - _padding.right(),
_username->height());
_username->moveToLeft(_padding.left(), _padding.top());
2014-10-22 18:39:03 +00:00
}
2022-10-11 18:10:15 +00:00
void UsernameEditor::save() {
2021-10-17 21:30:25 +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) {
2021-10-17 21:30:25 +00:00
_saveRequestId = 0;
_session->data().processUser(result);
2022-10-11 18:10:15 +00:00
_closeRequests.fire({});
2021-03-12 12:48:00 +00:00
}).fail([=](const MTP::Error &error) {
2021-10-17 21:30:25 +00:00
_saveRequestId = 0;
updateFail(error.type());
}).send();
2014-10-22 18:39:03 +00:00
}
2022-10-11 18:10:15 +00:00
void UsernameEditor::check() {
_api.request(base::take(_checkRequestId)).cancel();
2021-10-17 21:30:25 +00:00
const auto name = getName();
if (name.size() < Ui::EditPeer::kMinUsernameLength) {
2021-10-17 21:30:25 +00:00
return;
2014-10-22 18:39:03 +00:00
}
2021-10-17 21:30:25 +00:00
_checkUsername = name;
_checkRequestId = _api.request(MTPaccount_CheckUsername(
MTP_string(name)
)).done([=](const MTPBool &result) {
_checkRequestId = 0;
_errorText = (mtpIsTrue(result)
|| _checkUsername == _session->user()->username())
2021-10-17 21:30:25 +00:00
? QString()
: tr::lng_username_occupied(tr::now);
_goodText = _errorText.isEmpty()
? tr::lng_username_available(tr::now)
: QString();
update();
}).fail([=](const MTP::Error &error) {
_checkRequestId = 0;
checkFail(error.type());
}).send();
2014-10-22 18:39:03 +00:00
}
2022-10-11 18:10:15 +00:00
void UsernameEditor::changed() {
2021-10-17 21:30:25 +00:00
const auto name = getName();
2014-10-22 18:39:03 +00:00
if (name.isEmpty()) {
if (!_errorText.isEmpty() || !_goodText.isEmpty()) {
_errorText = _goodText = QString();
2014-10-22 18:39:03 +00:00
update();
}
2021-10-17 21:30:25 +00:00
_checkTimer.cancel();
2014-10-22 18:39:03 +00:00
} else {
2021-10-17 21:30:25 +00:00
const auto len = int(name.size());
for (auto i = 0; i < len; ++i) {
const auto 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();
}
2021-10-17 21:30:25 +00:00
_checkTimer.cancel();
return;
}
}
if (name.size() < Ui::EditPeer::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();
}
2021-10-17 21:30:25 +00:00
_checkTimer.cancel();
} else {
if (!_errorText.isEmpty() || !_goodText.isEmpty()) {
_errorText = _goodText = QString();
update();
}
_checkTimer.callOnce(Ui::EditPeer::kUsernameCheckTimeout);
2014-10-22 18:39:03 +00:00
}
}
}
2022-10-11 18:10:15 +00:00
void UsernameEditor::updateFail(const QString &error) {
2019-08-06 16:40:08 +00:00
const auto self = _session->user();
2021-10-17 21:30:25 +00:00
if ((error == qstr("USERNAME_NOT_MODIFIED"))
|| (_sentUsername == self->username())) {
self->setName(
TextUtilities::SingleLine(self->firstName),
TextUtilities::SingleLine(self->lastName),
TextUtilities::SingleLine(self->nameOrPhone),
TextUtilities::SingleLine(_sentUsername));
2022-10-11 18:10:15 +00:00
_closeRequests.fire({});
2021-10-17 21:30:25 +00:00
} else if (error == 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();
2021-10-17 21:30:25 +00:00
} else if ((error == qstr("USERNAME_OCCUPIED"))
|| (error == 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
}
}
2022-10-11 18:10:15 +00:00
void UsernameEditor::checkFail(const QString &error) {
2021-10-17 21:30:25 +00:00
if (error == 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();
2021-10-17 21:30:25 +00:00
} else if ((error == 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
}
}
2022-10-11 18:10:15 +00:00
QString UsernameEditor::getName() const {
return _username->text().replace('@', QString()).trimmed();
2014-10-22 18:39:03 +00:00
}
2015-10-06 19:49:23 +00:00
2022-10-11 18:10:15 +00:00
} // namespace
void UsernamesBox(
not_null<Ui::GenericBox*> box,
not_null<Main::Session*> session) {
box->setTitle(tr::lng_username_title());
const auto container = box->verticalLayout();
const auto editor = box->addRow(
object_ptr<UsernameEditor>(box, session),
{});
editor->closeRequests(
) | rpl::start_with_next([=] {
box->closeBox();
}, editor->lifetime());
container->add(object_ptr<Ui::DividerLabel>(
container,
object_ptr<Ui::FlatLabel>(
container,
tr::lng_username_description(Ui::Text::RichLangValue),
st::boxDividerLabel),
st::settingsDividerLabelPadding));
box->addButton(tr::lng_settings_save(), [=] { editor->save(); });
box->addButton(tr::lng_cancel(), [=] { box->closeBox(); });
2021-10-17 21:30:25 +00:00
}