2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-09-05 00:46:08 +00:00

Remove MTP::MainInstance() global access point.

This commit is contained in:
John Preston
2020-06-11 20:09:46 +04:00
parent 7f09da9e32
commit 0ad7dcaef9
84 changed files with 823 additions and 1362 deletions

View File

@@ -17,7 +17,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "main/main_session.h"
#include "data/data_session.h"
#include "data/data_user.h"
#include "mtproto/facade.h"
#include "styles/style_layers.h"
#include "styles/style_boxes.h"
@@ -32,6 +31,7 @@ constexpr auto kMinUsernameLength = 5;
UsernameBox::UsernameBox(QWidget*, not_null<Main::Session*> session)
: _session(session)
, _api(_session->mtp())
, _username(
this,
st::defaultInputField,
@@ -115,21 +115,28 @@ void UsernameBox::save() {
if (_saveRequestId) return;
_sentUsername = getName();
_saveRequestId = MTP::send(MTPaccount_UpdateUsername(MTP_string(_sentUsername)), rpcDone(&UsernameBox::onUpdateDone), rpcFail(&UsernameBox::onUpdateFail));
_saveRequestId = _api.request(MTPaccount_UpdateUsername(
MTP_string(_sentUsername)
)).done([=](const MTPUser &result) {
updateDone(result);
}).fail([=](const RPCError &error) {
updateFail(error);
}).send();
}
void UsernameBox::check() {
if (_checkRequestId) {
MTP::cancel(_checkRequestId);
}
_api.request(base::take(_checkRequestId)).cancel();
QString name = getName();
if (name.size() >= kMinUsernameLength) {
_checkUsername = name;
_checkRequestId = MTP::send(
MTPaccount_CheckUsername(
MTP_string(name)),
rpcDone(&UsernameBox::onCheckDone),
rpcFail(&UsernameBox::onCheckFail));
_checkRequestId = _api.request(MTPaccount_CheckUsername(
MTP_string(name)
)).done([=](const MTPBool &result) {
checkDone(result);
}).fail([=](const RPCError &error) {
checkFail(error);
}).send();
}
}
@@ -176,14 +183,12 @@ void UsernameBox::linkClick() {
Ui::Toast::Show(tr::lng_username_copied(tr::now));
}
void UsernameBox::onUpdateDone(const MTPUser &user) {
void UsernameBox::updateDone(const MTPUser &user) {
_session->data().processUser(user);
closeBox();
}
bool UsernameBox::onUpdateFail(const RPCError &error) {
if (MTP::isDefaultHandledError(error)) return false;
void UsernameBox::updateFail(const RPCError &error) {
_saveRequestId = 0;
const auto self = _session->user();
const auto &err = error.type();
@@ -194,25 +199,22 @@ bool UsernameBox::onUpdateFail(const RPCError &error) {
TextUtilities::SingleLine(self->nameOrPhone),
TextUtilities::SingleLine(_sentUsername));
closeBox();
return true;
} else if (err == qstr("USERNAME_INVALID")) {
_username->setFocus();
_username->showError();
_errorText = tr::lng_username_invalid(tr::now);
update();
return true;
} else if (err == qstr("USERNAME_OCCUPIED") || err == qstr("USERNAMES_UNAVAILABLE")) {
_username->setFocus();
_username->showError();
_errorText = tr::lng_username_occupied(tr::now);
update();
return true;
} else {
_username->setFocus();
}
_username->setFocus();
return true;
}
void UsernameBox::onCheckDone(const MTPBool &result) {
void UsernameBox::checkDone(const MTPBool &result) {
_checkRequestId = 0;
const auto newError = (mtpIsTrue(result)
|| _checkUsername == _session->user()->username)
@@ -228,23 +230,19 @@ void UsernameBox::onCheckDone(const MTPBool &result) {
}
}
bool UsernameBox::onCheckFail(const RPCError &error) {
if (MTP::isDefaultHandledError(error)) return false;
void UsernameBox::checkFail(const RPCError &error) {
_checkRequestId = 0;
QString err(error.type());
if (err == qstr("USERNAME_INVALID")) {
_errorText = tr::lng_username_invalid(tr::now);
update();
return true;
} else if (err == qstr("USERNAME_OCCUPIED") && _checkUsername != _session->user()->username) {
_errorText = tr::lng_username_occupied(tr::now);
update();
return true;
} else {
_goodText = QString();
_username->setFocus();
}
_goodText = QString();
_username->setFocus();
return true;
}
QString UsernameBox::getName() const {