Use Controller::account in MainWindow.

This commit is contained in:
John Preston 2019-06-06 14:59:00 +03:00
parent 1e861f79f0
commit 0868a9f7b9
14 changed files with 84 additions and 54 deletions

View File

@ -306,7 +306,7 @@ Panel::Panel(not_null<Call*> call)
_cancel->setDuration(st::callPanelDuration); _cancel->setDuration(st::callPanelDuration);
setMouseTracking(true); setMouseTracking(true);
setWindowIcon(Window::CreateIcon()); setWindowIcon(Window::CreateIcon(&_user->account()));
initControls(); initControls();
initLayout(); initLayout();
showAndActivate(); showAndActivate();

View File

@ -232,11 +232,12 @@ bool Application::hideMediaView() {
} }
void Application::showPhoto(not_null<const PhotoOpenClickHandler*> link) { void Application::showPhoto(not_null<const PhotoOpenClickHandler*> link) {
const auto item = Auth().data().message(link->context()); const auto photo = link->photo();
const auto peer = link->peer(); const auto peer = link->peer();
const auto item = photo->owner().message(link->context());
return (!item && peer) return (!item && peer)
? showPhoto(link->photo(), peer) ? showPhoto(photo, peer)
: showPhoto(link->photo(), item); : showPhoto(photo, item);
} }
void Application::showPhoto(not_null<PhotoData*> photo, HistoryItem *item) { void Application::showPhoto(not_null<PhotoData*> photo, HistoryItem *item) {
@ -664,7 +665,8 @@ void Application::startLocalStorage() {
if (_mtproto) { if (_mtproto) {
_mtproto->requestConfig(); _mtproto->requestConfig();
} }
Platform::SetApplicationIcon(Window::CreateIcon()); Platform::SetApplicationIcon(
Window::CreateIcon(&activeAccount()));
Shortcuts::ToggleSupportShortcuts(support); Shortcuts::ToggleSupportShortcuts(support);
}); });
}, _lifetime); }, _lifetime);
@ -1114,16 +1116,18 @@ void Application::preventWindowActivation() {
void Application::QuitAttempt() { void Application::QuitAttempt() {
auto prevents = false; auto prevents = false;
if (AuthSession::Exists() && !Sandbox::Instance().isSavingSession()) { if (IsAppLaunched()
&& App().activeAccount().sessionExists()
&& !Sandbox::Instance().isSavingSession()) {
if (const auto mainwidget = App::main()) { if (const auto mainwidget = App::main()) {
if (mainwidget->isQuitPrevent()) { if (mainwidget->isQuitPrevent()) {
prevents = true; prevents = true;
} }
} }
if (Auth().api().isQuitPrevent()) { if (App().activeAccount().session().api().isQuitPrevent()) {
prevents = true; prevents = true;
} }
if (Auth().calls().isQuitPrevent()) { if (App().activeAccount().session().calls().isQuitPrevent()) {
prevents = true; prevents = true;
} }
} }

View File

@ -111,6 +111,10 @@ AuthSession &PeerData::session() const {
return _owner->session(); return _owner->session();
} }
Main::Account &PeerData::account() const {
return session().account();
}
void PeerData::updateNameDelayed( void PeerData::updateNameDelayed(
const QString &newName, const QString &newName,
const QString &newNameOrPhone, const QString &newNameOrPhone,

View File

@ -23,6 +23,10 @@ class UserData;
class ChatData; class ChatData;
class ChannelData; class ChannelData;
namespace Main {
class Account;
} // namespace Main
namespace Data { namespace Data {
class Session; class Session;
@ -111,6 +115,7 @@ public:
[[nodiscard]] Data::Session &owner() const; [[nodiscard]] Data::Session &owner() const;
[[nodiscard]] AuthSession &session() const; [[nodiscard]] AuthSession &session() const;
[[nodiscard]] Main::Account &account() const;
[[nodiscard]] bool isUser() const { [[nodiscard]] bool isUser() const {
return peerIsUser(id); return peerIsUser(id);

View File

@ -115,7 +115,7 @@ void activateBotCommand(
Ui::showPeerHistory(history, ShowAtTheEndMsgId); Ui::showPeerHistory(history, ShowAtTheEndMsgId);
auto options = ApiWrap::SendOptions(history); auto options = ApiWrap::SendOptions(history);
options.replyTo = msgId; options.replyTo = msgId;
Auth().api().shareContact(Auth().user(), options); history->session().api().shareContact(Auth().user(), options);
})); }));
} break; } break;

View File

@ -82,7 +82,7 @@ MainWindow::MainWindow(not_null<Window::Controller*> controller)
setLocale(QLocale(QLocale::English, QLocale::UnitedStates)); setLocale(QLocale(QLocale::English, QLocale::UnitedStates));
Core::App().activeAccount().sessionValue( account().sessionValue(
) | rpl::start_with_next([=](AuthSession *session) { ) | rpl::start_with_next([=](AuthSession *session) {
updateGlobalMenu(); updateGlobalMenu();
if (!session) { if (!session) {
@ -201,7 +201,7 @@ void MainWindow::clearPasscodeLock() {
Core::App().checkStartUrl(); Core::App().checkStartUrl();
} else { } else {
Core::App().startMtp(); Core::App().startMtp();
if (AuthSession::Exists()) { if (account().sessionExists()) {
setupMain(); setupMain();
} else { } else {
setupIntro(); setupIntro();
@ -229,7 +229,7 @@ void MainWindow::setupIntro() {
} }
void MainWindow::setupMain() { void MainWindow::setupMain() {
Expects(AuthSession::Exists()); Expects(account().sessionExists());
auto animated = (_intro || _passcodeLock); auto animated = (_intro || _passcodeLock);
auto bg = animated ? grabInner() : QPixmap(); auto bg = animated ? grabInner() : QPixmap();
@ -584,7 +584,7 @@ void MainWindow::updateTrayMenu(bool force) {
void MainWindow::onShowAddContact() { void MainWindow::onShowAddContact() {
if (isHidden()) showFromTray(); if (isHidden()) showFromTray();
if (AuthSession::Exists()) { if (account().sessionExists()) {
Ui::show(Box<AddContactBox>(), LayerOption::KeepOther); Ui::show(Box<AddContactBox>(), LayerOption::KeepOther);
} }
} }
@ -592,7 +592,7 @@ void MainWindow::onShowAddContact() {
void MainWindow::onShowNewGroup() { void MainWindow::onShowNewGroup() {
if (isHidden()) showFromTray(); if (isHidden()) showFromTray();
if (AuthSession::Exists()) { if (account().sessionExists()) {
Ui::show( Ui::show(
Box<GroupInfoBox>(GroupInfoBox::Type::Group), Box<GroupInfoBox>(GroupInfoBox::Type::Group),
LayerOption::KeepOther); LayerOption::KeepOther);
@ -618,9 +618,10 @@ void MainWindow::onLogout() {
Core::App().logOut(); Core::App().logOut();
}; };
const auto callback = [=] { const auto callback = [=] {
if (AuthSession::Exists() && Auth().data().exportInProgress()) { if (account().sessionExists()
&& account().session().data().exportInProgress()) {
Ui::hideLayer(); Ui::hideLayer();
Auth().data().stopExportWithConfirmation(logout); account().session().data().stopExportWithConfirmation(logout);
} else { } else {
logout(); logout();
} }
@ -708,7 +709,7 @@ void MainWindow::toggleDisplayNotifyFromTray() {
Ui::show(Box<InformBox>(lang(lng_passcode_need_unblock))); Ui::show(Box<InformBox>(lang(lng_passcode_need_unblock)));
return; return;
} }
if (!AuthSession::Exists()) { if (!account().sessionExists()) {
return; return;
} }
@ -730,9 +731,11 @@ void MainWindow::toggleDisplayNotifyFromTray() {
} }
} }
Local::writeUserSettings(); Local::writeUserSettings();
Auth().notifications().settingsChanged().notify(Window::Notifications::ChangeType::DesktopEnabled); account().session().notifications().settingsChanged().notify(
Window::Notifications::ChangeType::DesktopEnabled);
if (soundNotifyChanged) { if (soundNotifyChanged) {
Auth().notifications().settingsChanged().notify(Window::Notifications::ChangeType::SoundEnabled); account().session().notifications().settingsChanged().notify(
Window::Notifications::ChangeType::SoundEnabled);
} }
} }
@ -742,7 +745,7 @@ void MainWindow::closeEvent(QCloseEvent *e) {
App::quit(); App::quit();
} else { } else {
e->ignore(); e->ignore();
if (!AuthSession::Exists() || !hideNoQuit()) { if (!account().sessionExists() || !hideNoQuit()) {
App::quit(); App::quit();
} }
} }
@ -904,7 +907,7 @@ QImage MainWindow::iconWithCounter(int size, int count, style::color bg, style::
} }
QImage img(smallIcon ? ((size == 16) ? iconbig16 : (size == 32 ? iconbig32 : iconbig64)) : ((size == 16) ? icon16 : (size == 32 ? icon32 : icon64))); QImage img(smallIcon ? ((size == 16) ? iconbig16 : (size == 32 ? iconbig32 : iconbig64)) : ((size == 16) ? icon16 : (size == 32 ? icon32 : icon64)));
if (AuthSession::Exists() && Auth().supportMode()) { if (account().sessionExists() && account().session().supportMode()) {
Window::ConvertIconToBlack(img); Window::ConvertIconToBlack(img);
} }
if (!count) return img; if (!count) return img;

View File

@ -232,7 +232,7 @@ OverlayWidget::OverlayWidget()
, _dropdownShowTimer(this) { , _dropdownShowTimer(this) {
subscribe(Lang::Current().updated(), [this] { refreshLang(); }); subscribe(Lang::Current().updated(), [this] { refreshLang(); });
setWindowIcon(Window::CreateIcon()); setWindowIcon(Window::CreateIcon(&Core::App().activeAccount()));
setWindowTitle(qsl("Media viewer")); setWindowTitle(qsl("Media viewer"));
TextCustomTagsMap custom; TextCustomTagsMap custom;

View File

@ -265,7 +265,7 @@ void MainWindow::psSetupTrayIcon() {
QByteArray path = QFile::encodeName(iconFile.absoluteFilePath()); QByteArray path = QFile::encodeName(iconFile.absoluteFilePath());
icon = QIcon(path.constData()); icon = QIcon(path.constData());
} else { } else {
icon = Window::CreateIcon(); icon = Window::CreateIcon(&account());
} }
trayIcon->setIcon(icon); trayIcon->setIcon(icon);

View File

@ -407,7 +407,7 @@ void MainWindow::initTouchBar() {
return; return;
} }
Core::App().activeAccount().sessionValue( account().sessionValue(
) | rpl::start_with_next([=](AuthSession *session) { ) | rpl::start_with_next([=](AuthSession *session) {
if (session) { if (session) {
// We need only common pinned dialogs. // We need only common pinned dialogs.
@ -641,10 +641,13 @@ void MainWindow::createGlobalMenu() {
QMenu *window = psMainMenu.addMenu(lang(lng_mac_menu_window)); QMenu *window = psMainMenu.addMenu(lang(lng_mac_menu_window));
psContacts = window->addAction(lang(lng_mac_menu_contacts)); psContacts = window->addAction(lang(lng_mac_menu_contacts));
connect(psContacts, &QAction::triggered, psContacts, [] { connect(psContacts, &QAction::triggered, psContacts, crl::guard(this, [=] {
if (App::wnd() && App::wnd()->isHidden()) App::wnd()->showFromTray(); if (isHidden()) {
showFromTray();
if (!AuthSession::Exists()) return; }
if (!account().sessionExists()) {
return;
}
Ui::show(Box<PeerListBox>(std::make_unique<ContactsBoxController>(), [](not_null<PeerListBox*> box) { Ui::show(Box<PeerListBox>(std::make_unique<ContactsBoxController>(), [](not_null<PeerListBox*> box) {
box->addButton(langFactory(lng_close), [box] { box->closeBox(); }); box->addButton(langFactory(lng_close), [box] { box->closeBox(); });
box->addLeftButton(langFactory(lng_profile_add_contact), [] { App::wnd()->onShowAddContact(); }); box->addLeftButton(langFactory(lng_profile_add_contact), [] { App::wnd()->onShowAddContact(); });
@ -734,10 +737,10 @@ void MainWindow::updateGlobalMenuHook() {
canDelete = list->canDeleteSelected(); canDelete = list->canDeleteSelected();
} }
App::wnd()->updateIsActive(0); App::wnd()->updateIsActive(0);
const auto logged = AuthSession::Exists(); const auto logged = account().sessionExists();
const auto locked = Core::App().locked(); const auto locked = Core::App().locked();
const auto inactive = !logged || locked; const auto inactive = !logged || locked;
const auto support = logged && Auth().supportMode(); const auto support = logged && account().session().supportMode();
_forceDisabled(psLogout, !logged && !locked); _forceDisabled(psLogout, !logged && !locked);
_forceDisabled(psUndo, !canUndo); _forceDisabled(psUndo, !canUndo);
_forceDisabled(psRedo, !canRedo); _forceDisabled(psRedo, !canRedo);

View File

@ -93,16 +93,18 @@ void ConvertIconToBlack(QImage &image) {
} }
} }
QIcon CreateOfficialIcon() { QIcon CreateOfficialIcon(Main::Account *account) {
auto image = Core::IsAppLaunched() ? Core::App().logo() : LoadLogo(); auto image = Core::IsAppLaunched() ? Core::App().logo() : LoadLogo();
if (AuthSession::Exists() && Auth().supportMode()) { if (account
&& account->sessionExists()
&& account->session().supportMode()) {
ConvertIconToBlack(image); ConvertIconToBlack(image);
} }
return QIcon(App::pixmapFromImageInPlace(std::move(image))); return QIcon(App::pixmapFromImageInPlace(std::move(image)));
} }
QIcon CreateIcon() { QIcon CreateIcon(Main::Account *account) {
auto result = CreateOfficialIcon(); auto result = CreateOfficialIcon(account);
if (Platform::IsLinux()) { if (Platform::IsLinux()) {
return QIcon::fromTheme("telegram", result); return QIcon::fromTheme("telegram", result);
} }
@ -114,7 +116,6 @@ MainWindow::MainWindow(not_null<Controller*> controller)
, _positionUpdatedTimer([=] { savePosition(); }) , _positionUpdatedTimer([=] { savePosition(); })
, _outdated(CreateOutdatedBar(this)) , _outdated(CreateOutdatedBar(this))
, _body(this) , _body(this)
, _icon(CreateIcon())
, _titleText(qsl("Telegram")) { , _titleText(qsl("Telegram")) {
subscribe(Theme::Background(), [=]( subscribe(Theme::Background(), [=](
const Theme::BackgroundUpdate &data) { const Theme::BackgroundUpdate &data) {
@ -150,13 +151,17 @@ MainWindow::MainWindow(not_null<Controller*> controller)
_inactivePressTimer.setCallback([this] { setInactivePress(false); }); _inactivePressTimer.setCallback([this] { setInactivePress(false); });
} }
Main::Account &MainWindow::account() const {
return _controller->account();
}
Window::SessionController *MainWindow::sessionController() const { Window::SessionController *MainWindow::sessionController() const {
return _controller->sessionController(); return _controller->sessionController();
} }
void MainWindow::checkLockByTerms() { void MainWindow::checkLockByTerms() {
const auto data = Core::App().termsLocked(); const auto data = Core::App().termsLocked();
if (!data || !AuthSession::Exists()) { if (!data || !account().sessionExists()) {
if (_termsBox) { if (_termsBox) {
_termsBox->closeBox(); _termsBox->closeBox();
} }
@ -175,8 +180,8 @@ void MainWindow::checkLockByTerms() {
box->agreeClicks( box->agreeClicks(
) | rpl::start_with_next([=] { ) | rpl::start_with_next([=] {
const auto mention = box ? box->lastClickedMention() : QString(); const auto mention = box ? box->lastClickedMention() : QString();
if (AuthSession::Exists()) { if (account().sessionExists()) {
Auth().api().acceptTerms(id); account().session().api().acceptTerms(id);
if (!mention.isEmpty()) { if (!mention.isEmpty()) {
MentionClickHandler(mention).onClick({}); MentionClickHandler(mention).onClick({});
} }
@ -224,8 +229,8 @@ void MainWindow::showTermsDecline() {
void MainWindow::showTermsDelete() { void MainWindow::showTermsDelete() {
const auto box = std::make_shared<QPointer<BoxContent>>(); const auto box = std::make_shared<QPointer<BoxContent>>();
const auto deleteByTerms = [=] { const auto deleteByTerms = [=] {
if (AuthSession::Exists()) { if (account().sessionExists()) {
Auth().termsDeleteNow(); account().session().termsDeleteNow();
} else { } else {
Ui::hideLayer(); Ui::hideLayer();
} }
@ -278,9 +283,10 @@ bool MainWindow::computeIsActive() const {
} }
void MainWindow::updateWindowIcon() { void MainWindow::updateWindowIcon() {
const auto supportIcon = AuthSession::Exists() && Auth().supportMode(); const auto supportIcon = account().sessionExists()
if (supportIcon != _usingSupportIcon) { && account().session().supportMode();
_icon = CreateIcon(); if (supportIcon != _usingSupportIcon || _icon.isNull()) {
_icon = CreateIcon(&account());
_usingSupportIcon = supportIcon; _usingSupportIcon = supportIcon;
} }
setWindowIcon(_icon); setWindowIcon(_icon);
@ -485,8 +491,8 @@ void MainWindow::updateControlsGeometry() {
void MainWindow::updateUnreadCounter() { void MainWindow::updateUnreadCounter() {
if (!Global::started() || App::quitting()) return; if (!Global::started() || App::quitting()) return;
const auto counter = AuthSession::Exists() const auto counter = account().sessionExists()
? Auth().data().unreadBadge() ? account().session().data().unreadBadge()
: 0; : 0;
_titleText = (counter > 0) ? qsl("Telegram (%1)").arg(counter) : qsl("Telegram"); _titleText = (counter > 0) ? qsl("Telegram (%1)").arg(counter) : qsl("Telegram");

View File

@ -13,6 +13,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
class BoxContent; class BoxContent;
namespace Main {
class Account;
} // namespace Main
namespace Window { namespace Window {
class Controller; class Controller;
@ -22,7 +26,7 @@ struct TermsLock;
QImage LoadLogo(); QImage LoadLogo();
QImage LoadLogoNoMargin(); QImage LoadLogoNoMargin();
QIcon CreateIcon(); QIcon CreateIcon(Main::Account *account = nullptr);
void ConvertIconToBlack(QImage &image); void ConvertIconToBlack(QImage &image);
class MainWindow : public Ui::RpWidget, protected base::Subscriber { class MainWindow : public Ui::RpWidget, protected base::Subscriber {
@ -31,9 +35,10 @@ class MainWindow : public Ui::RpWidget, protected base::Subscriber {
public: public:
explicit MainWindow(not_null<Controller*> controller); explicit MainWindow(not_null<Controller*> controller);
not_null<Window::Controller*> controller() const { Window::Controller &controller() const {
return _controller; return *_controller;
} }
Main::Account &account() const;
Window::SessionController *sessionController() const; Window::SessionController *sessionController() const;
void setInactivePress(bool inactive); void setInactivePress(bool inactive);
bool wasInactivePress() const { bool wasInactivePress() const {

View File

@ -17,7 +17,7 @@ namespace Window {
Controller::Controller(not_null<Main::Account*> account) Controller::Controller(not_null<Main::Account*> account)
: _account(account) : _account(account)
, _widget(this) { , _widget(this) {
Core::App().activeAccount().sessionValue( _account->sessionValue(
) | rpl::start_with_next([=](AuthSession *session) { ) | rpl::start_with_next([=](AuthSession *session) {
_sessionController = session _sessionController = session
? std::make_unique<SessionController>(session, &_widget) ? std::make_unique<SessionController>(session, &_widget)

View File

@ -23,8 +23,8 @@ public:
Controller(const Controller &other) = delete; Controller(const Controller &other) = delete;
Controller &operator=(const Controller &other) = delete; Controller &operator=(const Controller &other) = delete;
not_null<Main::Account*> account() const { Main::Account &account() const {
return _account; return *_account;
} }
not_null<::MainWindow*> widget() { not_null<::MainWindow*> widget() {
return &_widget; return &_widget;

View File

@ -103,7 +103,7 @@ SessionController::SessionController(
, _window(window) { , _window(window) {
init(); init();
subscribe(Auth().api().fullPeerUpdated(), [=](PeerData *peer) { subscribe(session->api().fullPeerUpdated(), [=](PeerData *peer) {
if (peer == _showEditPeer) { if (peer == _showEditPeer) {
_showEditPeer = nullptr; _showEditPeer = nullptr;
Ui::show(Box<EditPeerInfoBox>(peer)); Ui::show(Box<EditPeerInfoBox>(peer));
@ -123,7 +123,7 @@ SessionController::SessionController(
void SessionController::showEditPeerBox(PeerData *peer) { void SessionController::showEditPeerBox(PeerData *peer) {
_showEditPeer = peer; _showEditPeer = peer;
Auth().api().requestFullPeer(peer); session().api().requestFullPeer(peer);
} }
void SessionController::init() { void SessionController::init() {
@ -190,7 +190,7 @@ bool SessionController::jumpToChatListEntry(Dialogs::RowDescriptor row) {
Ui::showPeerHistory(history, row.fullId.msg); Ui::showPeerHistory(history, row.fullId.msg);
return true; return true;
//} else if (const auto feed = row.key.feed()) { // #feed //} else if (const auto feed = row.key.feed()) { // #feed
// if (const auto item = Auth().data().message(row.fullId)) { // if (const auto item = session().data().message(row.fullId)) {
// showSection(HistoryFeed::Memento(feed, item->position())); // showSection(HistoryFeed::Memento(feed, item->position()));
// } else { // } else {
// showSection(HistoryFeed::Memento(feed)); // showSection(HistoryFeed::Memento(feed));