tdesktop/Telegram/SourceFiles/boxes/usernamebox.cpp

282 lines
10 KiB
C++
Raw Normal View History

2014-10-22 18:39:03 +00:00
/*
This file is part of Telegram Desktop,
the official desktop version of Telegram messaging app, see https://telegram.org
2014-10-22 18:39:03 +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-10-22 18:39:03 +00:00
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
2016-02-08 10:56:18 +00:00
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
2014-10-22 18:39:03 +00:00
*/
#include "stdafx.h"
#include "lang.h"
#include "application.h"
#include "usernamebox.h"
#include "mainwidget.h"
#include "window.h"
2015-10-06 19:49:23 +00:00
UsernameBox::UsernameBox() : AbstractBox(st::boxWidth),
_save(this, lang(lng_settings_save), st::defaultBoxButton),
2015-10-11 08:37:24 +00:00
_cancel(this, lang(lng_cancel), st::cancelBoxButton),
2015-10-12 21:02:10 +00:00
_username(this, st::defaultInputField, qsl("@username"), App::self()->username, false),
2015-10-06 19:49:23 +00:00
_link(this, QString(), st::defaultBoxLinkButton),
_saveRequestId(0), _checkRequestId(0),
_about(st::boxWidth - st::usernamePadding.left()) {
2015-10-11 08:37:24 +00:00
setBlueTitle(true);
_goodText = App::self()->username.isEmpty() ? QString() : lang(lng_username_available);
2014-10-22 18:39:03 +00:00
2015-10-06 19:49:23 +00:00
textstyleSet(&st::usernameTextStyle);
_about.setRichText(st::boxTextFont, lang(lng_username_about));
resizeMaxHeight(st::boxWidth, st::boxTitleHeight + st::usernamePadding.top() + _username.height() + st::usernameSkip + _about.countHeight(st::boxWidth - st::usernamePadding.left()) + 3 * st::usernameTextStyle.lineHeight + st::usernamePadding.bottom() + st::boxButtonPadding.top() + _save.height() + st::boxButtonPadding.bottom());
2015-10-06 19:49:23 +00:00
textstyleRestore();
connect(&_save, SIGNAL(clicked()), this, SLOT(onSave()));
connect(&_cancel, SIGNAL(clicked()), this, SLOT(onClose()));
connect(&_username, SIGNAL(changed()), this, SLOT(onChanged()));
connect(&_username, SIGNAL(submitted(bool)), this, SLOT(onSave()));
2014-10-22 18:39:03 +00:00
2015-10-06 19:49:23 +00:00
connect(&_link, SIGNAL(clicked()), this, SLOT(onLinkClick()));
2014-10-22 18:39:03 +00:00
_checkTimer.setSingleShot(true);
connect(&_checkTimer, SIGNAL(timeout()), this, SLOT(onCheck()));
prepare();
2014-10-22 18:39:03 +00:00
}
void UsernameBox::hideAll() {
2015-10-06 19:49:23 +00:00
_username.hide();
_save.hide();
_cancel.hide();
_link.hide();
2015-10-11 08:37:24 +00:00
AbstractBox::hideAll();
2014-10-22 18:39:03 +00:00
}
void UsernameBox::showAll() {
2015-10-06 19:49:23 +00:00
_username.show();
_save.show();
_cancel.show();
updateLinkText();
2015-10-11 08:37:24 +00:00
AbstractBox::showAll();
2014-10-22 18:39:03 +00:00
}
void UsernameBox::showDone() {
2015-10-06 19:49:23 +00:00
_username.setFocus();
2014-10-22 18:39:03 +00:00
}
void UsernameBox::paintEvent(QPaintEvent *e) {
2015-04-04 20:01:34 +00:00
Painter p(this);
if (paint(p)) return;
2015-10-11 08:37:24 +00:00
paintTitle(p, lang(lng_username_title));
2015-10-06 19:49:23 +00:00
if (!_copiedTextLink.isEmpty()) {
p.setPen(st::usernameDefaultFg);
p.setFont(st::boxTextFont);
p.drawTextLeft(st::usernamePadding.left(), _username.y() + _username.height() + ((st::usernameSkip - st::boxTextFont->height) / 2), width(), _copiedTextLink);
} else if (!_errorText.isEmpty()) {
p.setPen(st::setErrColor);
p.setFont(st::boxTextFont);
p.drawTextLeft(st::usernamePadding.left(), _username.y() + _username.height() + ((st::usernameSkip - st::boxTextFont->height) / 2), width(), _errorText);
} else if (!_goodText.isEmpty()) {
2015-10-06 19:49:23 +00:00
p.setPen(st::setGoodColor);
p.setFont(st::boxTextFont);
p.drawTextLeft(st::usernamePadding.left(), _username.y() + _username.height() + ((st::usernameSkip - st::boxTextFont->height) / 2), width(), _goodText);
} else {
p.setPen(st::usernameDefaultFg);
p.setFont(st::boxTextFont);
p.drawTextLeft(st::usernamePadding.left(), _username.y() + _username.height() + ((st::usernameSkip - st::boxTextFont->height) / 2), width(), lang(lng_username_choose));
}
p.setPen(st::black);
textstyleSet(&st::usernameTextStyle);
int32 availw = st::boxWidth - st::usernamePadding.left(), h = _about.countHeight(availw);
2015-10-14 11:51:37 +00:00
_about.drawLeft(p, st::usernamePadding.left(), _username.y() + _username.height() + st::usernameSkip, availw, width());
2015-10-06 19:49:23 +00:00
textstyleRestore();
int32 linky = _username.y() + _username.height() + st::usernameSkip + h + st::usernameTextStyle.lineHeight + ((st::usernameTextStyle.lineHeight - st::boxTextFont->height) / 2);
if (_link.isHidden()) {
p.drawTextLeft(st::usernamePadding.left(), linky, width(), lang(lng_username_link_willbe));
p.setPen(st::usernameDefaultFg);
p.drawTextLeft(st::usernamePadding.left(), linky + st::usernameTextStyle.lineHeight + ((st::usernameTextStyle.lineHeight - st::boxTextFont->height) / 2), width(), qsl("https://telegram.me/username"));
} else {
p.drawTextLeft(st::usernamePadding.left(), linky, width(), lang(lng_username_link));
2014-10-22 18:39:03 +00:00
}
}
void UsernameBox::resizeEvent(QResizeEvent *e) {
2015-10-06 19:49:23 +00:00
_username.resize(width() - st::usernamePadding.left() - st::usernamePadding.right(), _username.height());
2015-10-11 08:37:24 +00:00
_username.moveToLeft(st::usernamePadding.left(), st::boxTitleHeight + st::usernamePadding.top());
2015-10-06 19:49:23 +00:00
textstyleSet(&st::usernameTextStyle);
int32 availw = st::boxWidth - st::usernamePadding.left(), h = _about.countHeight(availw);
2015-10-06 19:49:23 +00:00
textstyleRestore();
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));
2015-10-06 19:49:23 +00:00
_save.moveToRight(st::boxButtonPadding.right(), height() - st::boxButtonPadding.bottom() - _save.height());
_cancel.moveToRight(st::boxButtonPadding.right() + _save.width() + st::boxButtonPadding.left(), _save.y());
2015-10-11 08:37:24 +00:00
AbstractBox::resizeEvent(e);
2014-10-22 18:39:03 +00:00
}
void UsernameBox::onSave() {
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
}
void UsernameBox::onCheck() {
2015-10-06 19:49:23 +00:00
if (_checkRequestId) {
MTP::cancel(_checkRequestId);
2014-10-22 18:39:03 +00:00
}
QString name = getName();
if (name.size() >= MinUsernameLength) {
2015-09-16 13:04:08 +00:00
_checkUsername = name;
2015-10-06 19:49:23 +00:00
_checkRequestId = MTP::send(MTPaccount_CheckUsername(MTP_string(name)), rpcDone(&UsernameBox::onCheckDone), rpcFail(&UsernameBox::onCheckFail));
2014-10-22 18:39:03 +00:00
}
}
void UsernameBox::onChanged() {
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()) {
2015-10-06 19:49:23 +00:00
_copiedTextLink = _errorText = _goodText = QString();
2014-10-22 18:39:03 +00:00
update();
}
_checkTimer.stop();
} else {
int32 i, 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)) {
2015-10-06 19:49:23 +00:00
if (_errorText != lang(lng_username_bad_symbols) || !_copiedTextLink.isEmpty()) {
_copiedTextLink = QString();
_errorText = lang(lng_username_bad_symbols);
update();
}
_checkTimer.stop();
return;
}
}
if (name.size() < MinUsernameLength) {
2015-10-06 19:49:23 +00:00
if (_errorText != lang(lng_username_too_short) || !_copiedTextLink.isEmpty()) {
_copiedTextLink = QString();
_errorText = lang(lng_username_too_short);
update();
}
_checkTimer.stop();
} else {
2015-10-06 19:49:23 +00:00
if (!_errorText.isEmpty() || !_goodText.isEmpty() || !_copiedTextLink.isEmpty()) {
_copiedTextLink = _errorText = _goodText = QString();
update();
}
_checkTimer.start(UsernameCheckTimeout);
2014-10-22 18:39:03 +00:00
}
}
}
2015-10-06 19:49:23 +00:00
void UsernameBox::onLinkClick() {
2016-01-30 18:24:18 +00:00
Application::clipboard()->setText(qsl("https://telegram.me/") + getName());
2015-10-06 19:49:23 +00:00
_copiedTextLink = lang(lng_username_copied);
update();
}
2014-10-22 18:39:03 +00:00
void UsernameBox::onUpdateDone(const MTPUser &user) {
App::feedUsers(MTP_vector<MTPUser>(1, user));
2014-10-22 18:39:03 +00:00
emit closed();
}
bool UsernameBox::onUpdateFail(const RPCError &error) {
2015-10-06 19:49:23 +00:00
if (mtpIsFlood(error)) return false;
2015-04-04 20:01:34 +00:00
2015-10-06 19:49:23 +00:00
_saveRequestId = 0;
2015-09-16 13:04:08 +00:00
QString err(error.type());
2014-10-22 20:27:49 +00:00
if (err == "USERNAME_NOT_MODIFIED" || _sentUsername == App::self()->username) {
2015-09-16 13:04:08 +00:00
App::self()->setName(textOneLine(App::self()->firstName), textOneLine(App::self()->lastName), textOneLine(App::self()->nameOrPhone), textOneLine(_sentUsername));
2014-10-22 18:39:03 +00:00
emit closed();
return true;
} else if (err == "USERNAME_INVALID") {
2015-10-06 19:49:23 +00:00
_username.setFocus();
_username.showError();
_copiedTextLink = QString();
2014-10-22 18:39:03 +00:00
_errorText = lang(lng_username_invalid);
2015-10-06 19:49:23 +00:00
update();
2014-10-22 18:39:03 +00:00
return true;
2014-10-22 20:27:49 +00:00
} else if (err == "USERNAME_OCCUPIED" || err == "USERNAMES_UNAVAILABLE") {
2015-10-06 19:49:23 +00:00
_username.setFocus();
_username.showError();
_copiedTextLink = QString();
2014-10-22 18:39:03 +00:00
_errorText = lang(lng_username_occupied);
2015-10-06 19:49:23 +00:00
update();
2014-10-22 18:39:03 +00:00
return true;
}
2015-10-06 19:49:23 +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;
2015-10-29 00:16:52 +00:00
QString newError = (mtpIsTrue(result) || _checkUsername == App::self()->username) ? QString() : lang(lng_username_occupied);
QString newGood = newError.isEmpty() ? lang(lng_username_available) : QString();
2015-10-06 19:49:23 +00:00
if (_errorText != newError || _goodText != newGood || !_copiedTextLink.isEmpty()) {
2014-10-22 18:39:03 +00:00
_errorText = newError;
_goodText = newGood;
2015-10-06 19:49:23 +00:00
_copiedTextLink = QString();
2014-10-22 18:39:03 +00:00
update();
}
}
bool UsernameBox::onCheckFail(const RPCError &error) {
2015-10-06 19:49:23 +00:00
if (mtpIsFlood(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());
if (err == "USERNAME_INVALID") {
_errorText = lang(lng_username_invalid);
update();
return true;
2014-10-22 20:27:49 +00:00
} else if (err == "USERNAME_OCCUPIED" && _checkUsername != App::self()->username) {
2014-10-22 18:39:03 +00:00
_errorText = lang(lng_username_occupied);
update();
return true;
}
_goodText = QString();
2015-10-06 19:49:23 +00:00
_copiedTextLink = QString();
_username.setFocus();
2014-10-22 18:39:03 +00:00
return true;
}
QString UsernameBox::getName() const {
2015-10-06 19:49:23 +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();
_link.setText(st::boxTextFont->elided(qsl("https://telegram.me/") + uname, st::boxWidth - st::usernamePadding.left() - st::usernamePadding.right()));
if (uname.isEmpty()) {
if (!_link.isHidden()) {
_link.hide();
update();
}
} else {
if (_link.isHidden()) {
_link.show();
update();
}
}
}