From d06a4abb4755d0e97e8ef03688bc878117d90b4e Mon Sep 17 00:00:00 2001 From: Keshav Bhatt Date: Wed, 30 Mar 2022 17:30:58 +0530 Subject: feat: app auto locking - settings to allow app auto locking after set time interval --- src/mainwindow.cpp | 80 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 57 insertions(+), 23 deletions(-) (limited to 'src/mainwindow.cpp') diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 74f196c..5a29c60 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -9,6 +9,8 @@ extern QString defaultUserAgentStr; extern double defaultZoomFactorMaximized; +extern int defaultAppAutoLockDuration; +extern bool defaultAppAutoLock; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), notificationsTitleRegExp("^\\([1-9]\\d*\\).*"), @@ -30,6 +32,22 @@ MainWindow::MainWindow(QWidget *parent) initRateWidget(); tryLock(); updateWindowTheme(); + initAutoLock(); +} + +void MainWindow::initAutoLock() { + autoLockEventFilter = new AutoLockEventFilter( + settings.value("autoLockDuration", defaultAppAutoLockDuration).toInt() * + 1000); + connect(autoLockEventFilter, &AutoLockEventFilter::autoLockTimerTimeout, this, + [=]() { + if (settings.value("appAutoLocking", defaultAppAutoLock).toBool()) { + this->lockApp(); + } + }); + if (settings.value("appAutoLocking", defaultAppAutoLock).toBool()) { + qApp->installEventFilter(autoLockEventFilter); + } } void MainWindow::initThemes() { @@ -204,22 +222,22 @@ void MainWindow::forceLogOut() { } } -bool MainWindow::isLoggedIn(){ - static bool loggedIn = false; - if (webEngine && webEngine->page()) { - webEngine->page()->runJavaScript( - "window.localStorage.getItem('WAToken2')", - [=](const QVariant &result) { qDebug() <page()) { + webEngine->page()->runJavaScript( + "window.localStorage.getItem('WAToken2')", [=](const QVariant &result) { + qDebug() << Q_FUNC_INFO << result; + if (result.isValid() && result.toString().isEmpty() == false) { + loggedIn = true; + } }); - qDebug() << "isLoggedIn" <appLockSetChecked(false); - + settingsWidget->autoAppLockSetChecked(false); + settingsWidget->updateAppLockPasswordViewer(); tryLogOut(); - QTimer::singleShot(2000, this, [=]() { - if(isLoggedIn()){ - forceLogOut(); - doAppReload(); - } - init_lock(); + QTimer::singleShot(1500, this, [=]() { + if (isLoggedIn()) { + forceLogOut(); + doAppReload(); + } + appAutoLockChanged(); + init_lock(); }); +} - +void MainWindow::appAutoLockChanged() { + bool enabled = settings.value("appAutoLocking", defaultAppAutoLock).toBool(); + if (enabled) { + qApp->installEventFilter(autoLockEventFilter); + autoLockEventFilter->setTimeoutmillis( + settings.value("autoLockDuration", defaultAppAutoLockDuration).toInt() * + 1000); + autoLockEventFilter->resetTimer(); + } else { + qApp->removeEventFilter(autoLockEventFilter); + autoLockEventFilter->stopTimer(); + } } // check window state and set tray menus -- cgit v1.2.3