From 35659536c530d6d384bfd968500c64cbc4d30c47 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 7 Jun 2018 10:44:38 +0300 Subject: [PATCH] Fix first passcode unlock. Fixes #4811. --- Telegram/SourceFiles/base/flat_map.h | 3 +- Telegram/SourceFiles/base/flat_set.h | 3 +- Telegram/SourceFiles/mainwindow.cpp | 22 ++++++----- Telegram/SourceFiles/messenger.cpp | 18 ++++----- Telegram/SourceFiles/messenger.h | 1 + .../window/window_lock_widgets.cpp | 39 ++++++------------- 6 files changed, 38 insertions(+), 48 deletions(-) diff --git a/Telegram/SourceFiles/base/flat_map.h b/Telegram/SourceFiles/base/flat_map.h index 75770e4f97..2c6a914be1 100644 --- a/Telegram/SourceFiles/base/flat_map.h +++ b/Telegram/SourceFiles/base/flat_map.h @@ -417,8 +417,9 @@ public: if (range.first == range.second) { return 0; } + const auto result = (range.second - range.first); impl().erase(range.first, range.second); - return (range.second - range.first); + return result; } iterator erase(const_iterator where) { diff --git a/Telegram/SourceFiles/base/flat_set.h b/Telegram/SourceFiles/base/flat_set.h index 368bd104d3..2edaa9d3cf 100644 --- a/Telegram/SourceFiles/base/flat_set.h +++ b/Telegram/SourceFiles/base/flat_set.h @@ -297,8 +297,9 @@ public: if (range.first == range.second) { return 0; } + const auto result = (range.second - range.first); impl().erase(range.first, range.second); - return (range.second - range.first); + return result; } iterator erase(const_iterator where) { diff --git a/Telegram/SourceFiles/mainwindow.cpp b/Telegram/SourceFiles/mainwindow.cpp index 57cb367aa9..814de4d710 100644 --- a/Telegram/SourceFiles/mainwindow.cpp +++ b/Telegram/SourceFiles/mainwindow.cpp @@ -128,9 +128,11 @@ void MainWindow::firstShow() { } void MainWindow::clearWidgetsHook() { + Expects(_passcodeLock == nullptr || !Global::LocalPasscode()); + auto wasMain = (_main != nullptr); - _passcodeLock.destroy(); _main.destroy(); + _passcodeLock.destroy(); _intro.destroy(); if (wasMain) { App::clearHistories(); @@ -177,18 +179,20 @@ void MainWindow::clearPasscodeLock() { _passcodeLock.destroy(); if (_intro) { _intro->showAnimated(bg, true); - } else { - Assert(_main != nullptr); + } else if (_main) { _main->showAnimated(bg, true); Messenger::Instance().checkStartUrl(); + } else { + Messenger::Instance().startMtp(); + if (AuthSession::Exists()) { + setupMain(); + } else { + setupIntro(); + } } } void MainWindow::setupIntro() { - if (_intro && !_intro->isHidden() && !_main) { - return; - } - Ui::hideSettingsAndLayer(anim::type::instant); auto animated = (_main || _passcodeLock); @@ -273,13 +277,13 @@ void MainWindow::sendServiceHistoryRequest() { } void MainWindow::setupMain(const MTPUser *self) { + Expects(AuthSession::Exists()); + auto animated = (_intro || _passcodeLock); auto bg = animated ? grabInner() : QPixmap(); clearWidgets(); - Assert(AuthSession::Exists()); - _main.create(bodyWidget(), controller()); _main->show(); updateControlsGeometry(); diff --git a/Telegram/SourceFiles/messenger.cpp b/Telegram/SourceFiles/messenger.cpp index 13b084a19a..ba27d43234 100644 --- a/Telegram/SourceFiles/messenger.cpp +++ b/Telegram/SourceFiles/messenger.cpp @@ -151,24 +151,19 @@ Messenger::Messenger(not_null launcher) if (state == Local::ReadMapPassNeeded) { Global::SetLocalPasscode(true); Global::RefLocalPasscodeChanged().notify(); + lockByPasscode(); DEBUG_LOG(("Application Info: passcode needed...")); } else { DEBUG_LOG(("Application Info: local map read...")); startMtp(); - } - - DEBUG_LOG(("Application Info: MTP started...")); - - DEBUG_LOG(("Application Info: showing.")); - if (state == Local::ReadMapPassNeeded) { - lockByPasscode(); - } else { + DEBUG_LOG(("Application Info: MTP started...")); if (AuthSession::Exists()) { _window->setupMain(); } else { _window->setupIntro(); } } + DEBUG_LOG(("Application Info: showing.")); _window->firstShow(); if (cStartToSettings()) { @@ -1037,8 +1032,12 @@ void Messenger::lockByPasscode() { } void Messenger::unlockPasscode() { - cSetPasscodeBadTries(0); + clearPasscodeLock(); _window->clearPasscodeLock(); +} + +void Messenger::clearPasscodeLock() { + cSetPasscodeBadTries(0); _passcodeLock = false; } @@ -1202,6 +1201,7 @@ void Messenger::loggedOut() { Global::SetLocalPasscode(false); Global::RefLocalPasscodeChanged().notify(); } + clearPasscodeLock(); Media::Player::mixer()->stopAndClear(); if (const auto w = getActiveWindow()) { w->tempDirDelete(Local::ClearManagerAll); diff --git a/Telegram/SourceFiles/messenger.h b/Telegram/SourceFiles/messenger.h index 6acab14f5c..b32441c2e8 100644 --- a/Telegram/SourceFiles/messenger.h +++ b/Telegram/SourceFiles/messenger.h @@ -233,6 +233,7 @@ private: void photoUpdated(const FullMsgId &msgId, const MTPInputFile &file); void resetAuthorizationKeys(); void authSessionDestroy(); + void clearPasscodeLock(); void loggedOut(); not_null _launcher; diff --git a/Telegram/SourceFiles/window/window_lock_widgets.cpp b/Telegram/SourceFiles/window/window_lock_widgets.cpp index 8222c784e5..cf12b8f4f0 100644 --- a/Telegram/SourceFiles/window/window_lock_widgets.cpp +++ b/Telegram/SourceFiles/window/window_lock_widgets.cpp @@ -132,35 +132,18 @@ void PasscodeLockWidget::submit() { return; } - if (App::main()) { - if (Local::checkPasscode(_passcode->text().toUtf8())) { - Messenger::Instance().unlockPasscode(); // Destroys this widget. - return; - } else { - cSetPasscodeBadTries(cPasscodeBadTries() + 1); - cSetPasscodeLastTry(getms(true)); - error(); - return; - } - } else { - if (Local::readMap(_passcode->text().toUtf8()) != Local::ReadMapPassNeeded) { - cSetPasscodeBadTries(0); - - Messenger::Instance().startMtp(); - - // Destroys this widget. - if (AuthSession::Exists()) { - App::wnd()->setupMain(); - } else { - App::wnd()->setupIntro(); - } - } else { - cSetPasscodeBadTries(cPasscodeBadTries() + 1); - cSetPasscodeLastTry(getms(true)); - error(); - return; - } + const auto passcode = _passcode->text().toUtf8(); + const auto correct = App::main() + ? Local::checkPasscode(passcode) + : (Local::readMap(passcode) != Local::ReadMapPassNeeded); + if (!correct) { + cSetPasscodeBadTries(cPasscodeBadTries() + 1); + cSetPasscodeLastTry(getms(true)); + error(); + return; } + + Messenger::Instance().unlockPasscode(); // Destroys this widget. } void PasscodeLockWidget::error() {