From 0868a9f7b9e94e419542cb0d4a5d023b2fc7cb1f Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 6 Jun 2019 14:59:00 +0300 Subject: [PATCH] Use Controller::account in MainWindow. --- Telegram/SourceFiles/calls/calls_panel.cpp | 2 +- Telegram/SourceFiles/core/application.cpp | 18 ++++++---- Telegram/SourceFiles/data/data_peer.cpp | 4 +++ Telegram/SourceFiles/data/data_peer.h | 5 +++ Telegram/SourceFiles/facades.cpp | 2 +- Telegram/SourceFiles/mainwindow.cpp | 27 +++++++------- .../media/view/media_view_overlay_widget.cpp | 2 +- .../platform/linux/main_window_linux.cpp | 2 +- .../platform/mac/main_window_mac.mm | 17 +++++---- Telegram/SourceFiles/window/main_window.cpp | 36 +++++++++++-------- Telegram/SourceFiles/window/main_window.h | 11 ++++-- .../SourceFiles/window/window_controller.cpp | 2 +- .../SourceFiles/window/window_controller.h | 4 +-- .../window/window_session_controller.cpp | 6 ++-- 14 files changed, 84 insertions(+), 54 deletions(-) diff --git a/Telegram/SourceFiles/calls/calls_panel.cpp b/Telegram/SourceFiles/calls/calls_panel.cpp index 7f09be70ca..d2c140d571 100644 --- a/Telegram/SourceFiles/calls/calls_panel.cpp +++ b/Telegram/SourceFiles/calls/calls_panel.cpp @@ -306,7 +306,7 @@ Panel::Panel(not_null call) _cancel->setDuration(st::callPanelDuration); setMouseTracking(true); - setWindowIcon(Window::CreateIcon()); + setWindowIcon(Window::CreateIcon(&_user->account())); initControls(); initLayout(); showAndActivate(); diff --git a/Telegram/SourceFiles/core/application.cpp b/Telegram/SourceFiles/core/application.cpp index e8ee13faed..b7f619c243 100644 --- a/Telegram/SourceFiles/core/application.cpp +++ b/Telegram/SourceFiles/core/application.cpp @@ -232,11 +232,12 @@ bool Application::hideMediaView() { } void Application::showPhoto(not_null link) { - const auto item = Auth().data().message(link->context()); + const auto photo = link->photo(); const auto peer = link->peer(); + const auto item = photo->owner().message(link->context()); return (!item && peer) - ? showPhoto(link->photo(), peer) - : showPhoto(link->photo(), item); + ? showPhoto(photo, peer) + : showPhoto(photo, item); } void Application::showPhoto(not_null photo, HistoryItem *item) { @@ -664,7 +665,8 @@ void Application::startLocalStorage() { if (_mtproto) { _mtproto->requestConfig(); } - Platform::SetApplicationIcon(Window::CreateIcon()); + Platform::SetApplicationIcon( + Window::CreateIcon(&activeAccount())); Shortcuts::ToggleSupportShortcuts(support); }); }, _lifetime); @@ -1114,16 +1116,18 @@ void Application::preventWindowActivation() { void Application::QuitAttempt() { auto prevents = false; - if (AuthSession::Exists() && !Sandbox::Instance().isSavingSession()) { + if (IsAppLaunched() + && App().activeAccount().sessionExists() + && !Sandbox::Instance().isSavingSession()) { if (const auto mainwidget = App::main()) { if (mainwidget->isQuitPrevent()) { prevents = true; } } - if (Auth().api().isQuitPrevent()) { + if (App().activeAccount().session().api().isQuitPrevent()) { prevents = true; } - if (Auth().calls().isQuitPrevent()) { + if (App().activeAccount().session().calls().isQuitPrevent()) { prevents = true; } } diff --git a/Telegram/SourceFiles/data/data_peer.cpp b/Telegram/SourceFiles/data/data_peer.cpp index 8e2d3632bd..0559214d4a 100644 --- a/Telegram/SourceFiles/data/data_peer.cpp +++ b/Telegram/SourceFiles/data/data_peer.cpp @@ -111,6 +111,10 @@ AuthSession &PeerData::session() const { return _owner->session(); } +Main::Account &PeerData::account() const { + return session().account(); +} + void PeerData::updateNameDelayed( const QString &newName, const QString &newNameOrPhone, diff --git a/Telegram/SourceFiles/data/data_peer.h b/Telegram/SourceFiles/data/data_peer.h index 2ffa3c36ef..33ce8f3b3a 100644 --- a/Telegram/SourceFiles/data/data_peer.h +++ b/Telegram/SourceFiles/data/data_peer.h @@ -23,6 +23,10 @@ class UserData; class ChatData; class ChannelData; +namespace Main { +class Account; +} // namespace Main + namespace Data { class Session; @@ -111,6 +115,7 @@ public: [[nodiscard]] Data::Session &owner() const; [[nodiscard]] AuthSession &session() const; + [[nodiscard]] Main::Account &account() const; [[nodiscard]] bool isUser() const { return peerIsUser(id); diff --git a/Telegram/SourceFiles/facades.cpp b/Telegram/SourceFiles/facades.cpp index fdb692166b..e084b70bdb 100644 --- a/Telegram/SourceFiles/facades.cpp +++ b/Telegram/SourceFiles/facades.cpp @@ -115,7 +115,7 @@ void activateBotCommand( Ui::showPeerHistory(history, ShowAtTheEndMsgId); auto options = ApiWrap::SendOptions(history); options.replyTo = msgId; - Auth().api().shareContact(Auth().user(), options); + history->session().api().shareContact(Auth().user(), options); })); } break; diff --git a/Telegram/SourceFiles/mainwindow.cpp b/Telegram/SourceFiles/mainwindow.cpp index 4d3de62084..c4946ad6c9 100644 --- a/Telegram/SourceFiles/mainwindow.cpp +++ b/Telegram/SourceFiles/mainwindow.cpp @@ -82,7 +82,7 @@ MainWindow::MainWindow(not_null controller) setLocale(QLocale(QLocale::English, QLocale::UnitedStates)); - Core::App().activeAccount().sessionValue( + account().sessionValue( ) | rpl::start_with_next([=](AuthSession *session) { updateGlobalMenu(); if (!session) { @@ -201,7 +201,7 @@ void MainWindow::clearPasscodeLock() { Core::App().checkStartUrl(); } else { Core::App().startMtp(); - if (AuthSession::Exists()) { + if (account().sessionExists()) { setupMain(); } else { setupIntro(); @@ -229,7 +229,7 @@ void MainWindow::setupIntro() { } void MainWindow::setupMain() { - Expects(AuthSession::Exists()); + Expects(account().sessionExists()); auto animated = (_intro || _passcodeLock); auto bg = animated ? grabInner() : QPixmap(); @@ -584,7 +584,7 @@ void MainWindow::updateTrayMenu(bool force) { void MainWindow::onShowAddContact() { if (isHidden()) showFromTray(); - if (AuthSession::Exists()) { + if (account().sessionExists()) { Ui::show(Box(), LayerOption::KeepOther); } } @@ -592,7 +592,7 @@ void MainWindow::onShowAddContact() { void MainWindow::onShowNewGroup() { if (isHidden()) showFromTray(); - if (AuthSession::Exists()) { + if (account().sessionExists()) { Ui::show( Box(GroupInfoBox::Type::Group), LayerOption::KeepOther); @@ -618,9 +618,10 @@ void MainWindow::onLogout() { Core::App().logOut(); }; const auto callback = [=] { - if (AuthSession::Exists() && Auth().data().exportInProgress()) { + if (account().sessionExists() + && account().session().data().exportInProgress()) { Ui::hideLayer(); - Auth().data().stopExportWithConfirmation(logout); + account().session().data().stopExportWithConfirmation(logout); } else { logout(); } @@ -708,7 +709,7 @@ void MainWindow::toggleDisplayNotifyFromTray() { Ui::show(Box(lang(lng_passcode_need_unblock))); return; } - if (!AuthSession::Exists()) { + if (!account().sessionExists()) { return; } @@ -730,9 +731,11 @@ void MainWindow::toggleDisplayNotifyFromTray() { } } Local::writeUserSettings(); - Auth().notifications().settingsChanged().notify(Window::Notifications::ChangeType::DesktopEnabled); + account().session().notifications().settingsChanged().notify( + Window::Notifications::ChangeType::DesktopEnabled); 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(); } else { e->ignore(); - if (!AuthSession::Exists() || !hideNoQuit()) { + if (!account().sessionExists() || !hideNoQuit()) { 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))); - if (AuthSession::Exists() && Auth().supportMode()) { + if (account().sessionExists() && account().session().supportMode()) { Window::ConvertIconToBlack(img); } if (!count) return img; diff --git a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp index 285c50fee2..8ccc8a1652 100644 --- a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp +++ b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp @@ -232,7 +232,7 @@ OverlayWidget::OverlayWidget() , _dropdownShowTimer(this) { subscribe(Lang::Current().updated(), [this] { refreshLang(); }); - setWindowIcon(Window::CreateIcon()); + setWindowIcon(Window::CreateIcon(&Core::App().activeAccount())); setWindowTitle(qsl("Media viewer")); TextCustomTagsMap custom; diff --git a/Telegram/SourceFiles/platform/linux/main_window_linux.cpp b/Telegram/SourceFiles/platform/linux/main_window_linux.cpp index 9580364888..f0edc97ff0 100644 --- a/Telegram/SourceFiles/platform/linux/main_window_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/main_window_linux.cpp @@ -265,7 +265,7 @@ void MainWindow::psSetupTrayIcon() { QByteArray path = QFile::encodeName(iconFile.absoluteFilePath()); icon = QIcon(path.constData()); } else { - icon = Window::CreateIcon(); + icon = Window::CreateIcon(&account()); } trayIcon->setIcon(icon); diff --git a/Telegram/SourceFiles/platform/mac/main_window_mac.mm b/Telegram/SourceFiles/platform/mac/main_window_mac.mm index cd2cb69660..6d9096b9c5 100644 --- a/Telegram/SourceFiles/platform/mac/main_window_mac.mm +++ b/Telegram/SourceFiles/platform/mac/main_window_mac.mm @@ -407,7 +407,7 @@ void MainWindow::initTouchBar() { return; } - Core::App().activeAccount().sessionValue( + account().sessionValue( ) | rpl::start_with_next([=](AuthSession *session) { if (session) { // We need only common pinned dialogs. @@ -641,10 +641,13 @@ void MainWindow::createGlobalMenu() { QMenu *window = psMainMenu.addMenu(lang(lng_mac_menu_window)); psContacts = window->addAction(lang(lng_mac_menu_contacts)); - connect(psContacts, &QAction::triggered, psContacts, [] { - if (App::wnd() && App::wnd()->isHidden()) App::wnd()->showFromTray(); - - if (!AuthSession::Exists()) return; + connect(psContacts, &QAction::triggered, psContacts, crl::guard(this, [=] { + if (isHidden()) { + showFromTray(); + } + if (!account().sessionExists()) { + return; + } Ui::show(Box(std::make_unique(), [](not_null box) { box->addButton(langFactory(lng_close), [box] { box->closeBox(); }); box->addLeftButton(langFactory(lng_profile_add_contact), [] { App::wnd()->onShowAddContact(); }); @@ -734,10 +737,10 @@ void MainWindow::updateGlobalMenuHook() { canDelete = list->canDeleteSelected(); } App::wnd()->updateIsActive(0); - const auto logged = AuthSession::Exists(); + const auto logged = account().sessionExists(); const auto locked = Core::App().locked(); const auto inactive = !logged || locked; - const auto support = logged && Auth().supportMode(); + const auto support = logged && account().session().supportMode(); _forceDisabled(psLogout, !logged && !locked); _forceDisabled(psUndo, !canUndo); _forceDisabled(psRedo, !canRedo); diff --git a/Telegram/SourceFiles/window/main_window.cpp b/Telegram/SourceFiles/window/main_window.cpp index 4ce514474e..0f56e04823 100644 --- a/Telegram/SourceFiles/window/main_window.cpp +++ b/Telegram/SourceFiles/window/main_window.cpp @@ -93,16 +93,18 @@ void ConvertIconToBlack(QImage &image) { } } -QIcon CreateOfficialIcon() { +QIcon CreateOfficialIcon(Main::Account *account) { auto image = Core::IsAppLaunched() ? Core::App().logo() : LoadLogo(); - if (AuthSession::Exists() && Auth().supportMode()) { + if (account + && account->sessionExists() + && account->session().supportMode()) { ConvertIconToBlack(image); } return QIcon(App::pixmapFromImageInPlace(std::move(image))); } -QIcon CreateIcon() { - auto result = CreateOfficialIcon(); +QIcon CreateIcon(Main::Account *account) { + auto result = CreateOfficialIcon(account); if (Platform::IsLinux()) { return QIcon::fromTheme("telegram", result); } @@ -114,7 +116,6 @@ MainWindow::MainWindow(not_null controller) , _positionUpdatedTimer([=] { savePosition(); }) , _outdated(CreateOutdatedBar(this)) , _body(this) -, _icon(CreateIcon()) , _titleText(qsl("Telegram")) { subscribe(Theme::Background(), [=]( const Theme::BackgroundUpdate &data) { @@ -150,13 +151,17 @@ MainWindow::MainWindow(not_null controller) _inactivePressTimer.setCallback([this] { setInactivePress(false); }); } +Main::Account &MainWindow::account() const { + return _controller->account(); +} + Window::SessionController *MainWindow::sessionController() const { return _controller->sessionController(); } void MainWindow::checkLockByTerms() { const auto data = Core::App().termsLocked(); - if (!data || !AuthSession::Exists()) { + if (!data || !account().sessionExists()) { if (_termsBox) { _termsBox->closeBox(); } @@ -175,8 +180,8 @@ void MainWindow::checkLockByTerms() { box->agreeClicks( ) | rpl::start_with_next([=] { const auto mention = box ? box->lastClickedMention() : QString(); - if (AuthSession::Exists()) { - Auth().api().acceptTerms(id); + if (account().sessionExists()) { + account().session().api().acceptTerms(id); if (!mention.isEmpty()) { MentionClickHandler(mention).onClick({}); } @@ -224,8 +229,8 @@ void MainWindow::showTermsDecline() { void MainWindow::showTermsDelete() { const auto box = std::make_shared>(); const auto deleteByTerms = [=] { - if (AuthSession::Exists()) { - Auth().termsDeleteNow(); + if (account().sessionExists()) { + account().session().termsDeleteNow(); } else { Ui::hideLayer(); } @@ -278,9 +283,10 @@ bool MainWindow::computeIsActive() const { } void MainWindow::updateWindowIcon() { - const auto supportIcon = AuthSession::Exists() && Auth().supportMode(); - if (supportIcon != _usingSupportIcon) { - _icon = CreateIcon(); + const auto supportIcon = account().sessionExists() + && account().session().supportMode(); + if (supportIcon != _usingSupportIcon || _icon.isNull()) { + _icon = CreateIcon(&account()); _usingSupportIcon = supportIcon; } setWindowIcon(_icon); @@ -485,8 +491,8 @@ void MainWindow::updateControlsGeometry() { void MainWindow::updateUnreadCounter() { if (!Global::started() || App::quitting()) return; - const auto counter = AuthSession::Exists() - ? Auth().data().unreadBadge() + const auto counter = account().sessionExists() + ? account().session().data().unreadBadge() : 0; _titleText = (counter > 0) ? qsl("Telegram (%1)").arg(counter) : qsl("Telegram"); diff --git a/Telegram/SourceFiles/window/main_window.h b/Telegram/SourceFiles/window/main_window.h index db18d996eb..ccf06d31e4 100644 --- a/Telegram/SourceFiles/window/main_window.h +++ b/Telegram/SourceFiles/window/main_window.h @@ -13,6 +13,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL class BoxContent; +namespace Main { +class Account; +} // namespace Main + namespace Window { class Controller; @@ -22,7 +26,7 @@ struct TermsLock; QImage LoadLogo(); QImage LoadLogoNoMargin(); -QIcon CreateIcon(); +QIcon CreateIcon(Main::Account *account = nullptr); void ConvertIconToBlack(QImage &image); class MainWindow : public Ui::RpWidget, protected base::Subscriber { @@ -31,9 +35,10 @@ class MainWindow : public Ui::RpWidget, protected base::Subscriber { public: explicit MainWindow(not_null controller); - not_null controller() const { - return _controller; + Window::Controller &controller() const { + return *_controller; } + Main::Account &account() const; Window::SessionController *sessionController() const; void setInactivePress(bool inactive); bool wasInactivePress() const { diff --git a/Telegram/SourceFiles/window/window_controller.cpp b/Telegram/SourceFiles/window/window_controller.cpp index d69b7a153a..e4b9ea166b 100644 --- a/Telegram/SourceFiles/window/window_controller.cpp +++ b/Telegram/SourceFiles/window/window_controller.cpp @@ -17,7 +17,7 @@ namespace Window { Controller::Controller(not_null account) : _account(account) , _widget(this) { - Core::App().activeAccount().sessionValue( + _account->sessionValue( ) | rpl::start_with_next([=](AuthSession *session) { _sessionController = session ? std::make_unique(session, &_widget) diff --git a/Telegram/SourceFiles/window/window_controller.h b/Telegram/SourceFiles/window/window_controller.h index cb1928cb39..f5fb3a573b 100644 --- a/Telegram/SourceFiles/window/window_controller.h +++ b/Telegram/SourceFiles/window/window_controller.h @@ -23,8 +23,8 @@ public: Controller(const Controller &other) = delete; Controller &operator=(const Controller &other) = delete; - not_null account() const { - return _account; + Main::Account &account() const { + return *_account; } not_null<::MainWindow*> widget() { return &_widget; diff --git a/Telegram/SourceFiles/window/window_session_controller.cpp b/Telegram/SourceFiles/window/window_session_controller.cpp index 0e2dfcf7c2..fe478febcc 100644 --- a/Telegram/SourceFiles/window/window_session_controller.cpp +++ b/Telegram/SourceFiles/window/window_session_controller.cpp @@ -103,7 +103,7 @@ SessionController::SessionController( , _window(window) { init(); - subscribe(Auth().api().fullPeerUpdated(), [=](PeerData *peer) { + subscribe(session->api().fullPeerUpdated(), [=](PeerData *peer) { if (peer == _showEditPeer) { _showEditPeer = nullptr; Ui::show(Box(peer)); @@ -123,7 +123,7 @@ SessionController::SessionController( void SessionController::showEditPeerBox(PeerData *peer) { _showEditPeer = peer; - Auth().api().requestFullPeer(peer); + session().api().requestFullPeer(peer); } void SessionController::init() { @@ -190,7 +190,7 @@ bool SessionController::jumpToChatListEntry(Dialogs::RowDescriptor row) { Ui::showPeerHistory(history, row.fullId.msg); return true; //} 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())); // } else { // showSection(HistoryFeed::Memento(feed));