diff --git a/src/core/InactivityTimer.cpp b/src/core/InactivityTimer.cpp index 85c58d269..501db50b5 100644 --- a/src/core/InactivityTimer.cpp +++ b/src/core/InactivityTimer.cpp @@ -20,28 +20,28 @@ #include #include +namespace +{ + // Minimum timeout is 10 seconds + constexpr int MIN_TIMEOUT = 10000; +} // namespace + InactivityTimer::InactivityTimer(QObject* parent) : QObject(parent) , m_timer(new QTimer(this)) - , m_active(false) { - m_timer->setSingleShot(true); + m_timer->setSingleShot(false); connect(m_timer, SIGNAL(timeout()), SLOT(timeout())); } -void InactivityTimer::setInactivityTimeout(int inactivityTimeout) -{ - Q_ASSERT(inactivityTimeout > 0); - - m_timer->setInterval(inactivityTimeout); -} - -void InactivityTimer::activate() +void InactivityTimer::activate(int inactivityTimeout) { if (!m_active) { qApp->installEventFilter(this); } m_active = true; + m_resetBlocked = false; + m_timer->setInterval(qMax(MIN_TIMEOUT, inactivityTimeout)); m_timer->start(); } @@ -54,12 +54,15 @@ void InactivityTimer::deactivate() bool InactivityTimer::eventFilter(QObject* watched, QEvent* event) { - const QEvent::Type type = event->type(); + const auto type = event->type(); // clang-format off - if ((type >= QEvent::MouseButtonPress && type <= QEvent::KeyRelease) - || (type >= QEvent::HoverEnter && type <= QEvent::HoverMove) - || (type == QEvent::Wheel)) { + if (!m_resetBlocked && + ((type >= QEvent::MouseButtonPress && type <= QEvent::KeyRelease) || + (type >= QEvent::HoverEnter && type <= QEvent::HoverMove) || + type == QEvent::Wheel)) { m_timer->start(); + m_resetBlocked = true; + QTimer::singleShot(500, this, [this]() { m_resetBlocked = false; }); } // clang-format on @@ -73,7 +76,7 @@ void InactivityTimer::timeout() return; } - if (m_active && !m_timer->isActive()) { + if (m_active) { emit inactivityDetected(); } diff --git a/src/core/InactivityTimer.h b/src/core/InactivityTimer.h index 8bde9751f..c19a4b776 100644 --- a/src/core/InactivityTimer.h +++ b/src/core/InactivityTimer.h @@ -29,8 +29,7 @@ class InactivityTimer : public QObject public: explicit InactivityTimer(QObject* parent = nullptr); - void setInactivityTimeout(int inactivityTimeout); - void activate(); + void activate(int inactivityTimeout); void deactivate(); signals: @@ -44,7 +43,8 @@ private slots: private: QTimer* m_timer; - bool m_active; + bool m_active = false; + bool m_resetBlocked = false; QMutex m_emitMutx; }; diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index c03582073..3abb8f12f 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -273,7 +273,7 @@ MainWindow::MainWindow() m_ui->actionAllowScreenCapture->setVisible(osUtils->canPreventScreenCapture()); m_inactivityTimer = new InactivityTimer(this); - connect(m_inactivityTimer, SIGNAL(inactivityDetected()), this, SLOT(lockDatabasesAfterInactivity())); + connect(m_inactivityTimer, SIGNAL(inactivityDetected()), this, SLOT(lockAllDatabases())); applySettingsChanges(); // Qt 5.10 introduced a new "feature" to hide shortcuts in context menus @@ -1656,14 +1656,9 @@ void MainWindow::showGroupContextMenu(const QPoint& globalPos) void MainWindow::applySettingsChanges() { - int timeout = config()->get(Config::Security_LockDatabaseIdleSeconds).toInt() * 1000; - if (timeout <= 0) { - timeout = 60; - } - - m_inactivityTimer->setInactivityTimeout(timeout); if (config()->get(Config::Security_LockDatabaseIdle).toBool()) { - m_inactivityTimer->activate(); + auto timeout = config()->get(Config::Security_LockDatabaseIdleSeconds).toInt() * 1000; + m_inactivityTimer->activate(timeout); } else { m_inactivityTimer->deactivate(); } @@ -1833,13 +1828,6 @@ void MainWindow::closeModalWindow() } } -void MainWindow::lockDatabasesAfterInactivity() -{ - if (!m_ui->tabWidget->lockDatabases()) { - m_inactivityTimer->activate(); - } -} - bool MainWindow::isTrayIconEnabled() const { return m_trayIcon && m_trayIcon->isVisible(); @@ -1894,7 +1882,7 @@ void MainWindow::bringToFront() void MainWindow::handleScreenLock() { if (config()->get(Config::Security_LockDatabaseScreenLock).toBool()) { - lockDatabasesAfterInactivity(); + lockAllDatabases(); } } @@ -1944,7 +1932,7 @@ void MainWindow::closeAllDatabases() void MainWindow::lockAllDatabases() { - lockDatabasesAfterInactivity(); + m_ui->tabWidget->lockDatabases(); } void MainWindow::displayDesktopNotification(const QString& msg, QString title, int msTimeoutHint) diff --git a/src/gui/MainWindow.h b/src/gui/MainWindow.h index ecd98e03a..229106c52 100644 --- a/src/gui/MainWindow.h +++ b/src/gui/MainWindow.h @@ -140,7 +140,6 @@ private slots: void applySettingsChanges(); void trayIconTriggered(QSystemTrayIcon::ActivationReason reason); void processTrayIconTrigger(); - void lockDatabasesAfterInactivity(); void handleScreenLock(); void showErrorMessage(const QString& message); void selectNextDatabaseTab();