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/WhatsApp.pro | 1 + src/autolockeventfilter.h | 48 ++++++++++++++++++++++++++++ src/common.h | 2 +- src/lock.cpp | 1 - src/mainwindow.cpp | 80 +++++++++++++++++++++++++++++++++-------------- src/mainwindow.h | 4 +++ src/settingswidget.cpp | 27 ++++++++++++++-- src/settingswidget.h | 3 ++ src/settingswidget.ui | 16 +++++----- 9 files changed, 146 insertions(+), 36 deletions(-) create mode 100644 src/autolockeventfilter.h (limited to 'src') diff --git a/src/WhatsApp.pro b/src/WhatsApp.pro index b83ec8e..9bff312 100644 --- a/src/WhatsApp.pro +++ b/src/WhatsApp.pro @@ -76,6 +76,7 @@ RESOURCES += \ HEADERS += \ SunClock.hpp \ about.h \ + autolockeventfilter.h \ automatictheme.h \ common.h \ dictionaries.h \ diff --git a/src/autolockeventfilter.h b/src/autolockeventfilter.h new file mode 100644 index 0000000..2f05504 --- /dev/null +++ b/src/autolockeventfilter.h @@ -0,0 +1,48 @@ +#ifndef AUTOLOCKEVENTFILTER_H +#define AUTOLOCKEVENTFILTER_H + +#include +#include +#include + +class AutoLockEventFilter : public QObject { + Q_OBJECT + +public: + explicit AutoLockEventFilter(int timeoutmillis) + : timeoutmillis(timeoutmillis) { + autoLockTimer = new QTimer(this); + connect(autoLockTimer, &QTimer::timeout, this, + QOverload<>::of(&AutoLockEventFilter::lockApp)); + resetTimer(); + } + + ~AutoLockEventFilter() { + autoLockTimer->stop(); + autoLockTimer->deleteLater(); + } + +signals: + void autoLockTimerTimeout(); + +private: + QTimer *autoLockTimer = nullptr; + int timeoutmillis; + +public slots: + void stopTimer() { autoLockTimer->stop(); } + void resetTimer() { autoLockTimer->start(timeoutmillis); } + void lockApp() { emit autoLockTimerTimeout(); } + void setTimeoutmillis(int newTimeoutmillis) { + timeoutmillis = newTimeoutmillis; + } + +protected: + bool eventFilter(QObject *obj, QEvent *ev) { + if (ev->type() == QEvent::KeyPress || ev->type() == QEvent::MouseMove) { + resetTimer(); + } + return QObject::eventFilter(obj, ev); + } +}; +#endif // AUTOLOCKEVENTFILTER_H diff --git a/src/common.h b/src/common.h index 0d57ab8..85b608d 100644 --- a/src/common.h +++ b/src/common.h @@ -6,7 +6,7 @@ QString defaultUserAgentStr = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36"; //appAutoLock -int defaultAppAutoLockDuration = 60; +int defaultAppAutoLockDuration = 30; bool defaultAppAutoLock = false; double defaultZoomFactorMaximized = 1.50; diff --git a/src/lock.cpp b/src/lock.cpp index 6cd0d6c..f9d80ff 100644 --- a/src/lock.cpp +++ b/src/lock.cpp @@ -30,7 +30,6 @@ Lock::Lock(QWidget *parent) : QWidget(parent), ui(new Ui::Lock) { checkCaps(); QString capsStyle = QString("background-color: palette(window);" "padding:4px;" - "border:0px solid palette(highlight);" "border-radius: 2px;" "color:palette(window-text);"); ui->caps1->setStyleSheet(capsStyle); 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 diff --git a/src/mainwindow.h b/src/mainwindow.h index 1f966bd..c3c82a2 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -34,6 +34,7 @@ #include "settingswidget.h" #include "webenginepage.h" #include "webview.h" +#include "autolockeventfilter.h" class MainWindow : public QMainWindow { Q_OBJECT @@ -84,6 +85,7 @@ private: Lock *lockWidget = nullptr; int correctlyLoaderRetries = 4; QStringList m_dictionaries; + AutoLockEventFilter *autoLockEventFilter = nullptr; private slots: @@ -121,6 +123,8 @@ private slots: void forceLogOut(); void tryLogOut(); bool isLoggedIn(); + void initAutoLock(); + void appAutoLockChanged(); }; #endif // MAINWINDOW_H diff --git a/src/settingswidget.cpp b/src/settingswidget.cpp index 2e2b095..75e1075 100644 --- a/src/settingswidget.cpp +++ b/src/settingswidget.cpp @@ -54,8 +54,10 @@ SettingsWidget::SettingsWidget(QWidget *parent, QString engineCachePath, settings.value("useNativeFileDialog", false).toBool()); ui->startMinimized->setChecked( settings.value("startMinimized", false).toBool()); - ui->appAutoLockcheckBox->setChecked( + + appLockSetChecked( settings.value("appAutoLocking", defaultAppAutoLock).toBool()); + ui->autoLockDurationSpinbox->setValue( settings.value("autoLockDuration", defaultAppAutoLockDuration).toInt()); ui->minimizeOnTrayIconClick->setChecked( @@ -394,6 +396,17 @@ void SettingsWidget::on_closeButtonActionComboBox_currentIndexChanged( settings.setValue("closeButtonActionCombo", index); } +void SettingsWidget::autoAppLockSetChecked(bool checked) { + ui->appAutoLockcheckBox->blockSignals(true); + ui->appAutoLockcheckBox->setChecked(checked); + ui->appAutoLockcheckBox->blockSignals(false); +} + +void SettingsWidget::updateAppLockPasswordViewer() { + this->setCurrentPasswordText( + QByteArray::fromBase64(settings.value("asdfg").toString().toUtf8())); +} + void SettingsWidget::appLockSetChecked(bool checked) { ui->applock_checkbox->blockSignals(true); ui->applock_checkbox->setChecked(checked); @@ -540,11 +553,21 @@ void SettingsWidget::on_startMinimized_toggled(bool checked) { } void SettingsWidget::on_appAutoLockcheckBox_toggled(bool checked) { - settings.setValue("appAutoLocking", checked); + if (settings.value("asdfg").isValid()) { + settings.setValue("appAutoLocking", checked); + } else { + showSetApplockPasswordDialog(); + if (settings.value("asdfg").isValid() == false) { + settings.setValue("appAutoLocking", false); + autoAppLockSetChecked(false); + } + } + emit appAutoLockChanged(); } void SettingsWidget::on_autoLockDurationSpinbox_valueChanged(int arg1) { settings.setValue("autoLockDuration", arg1); + emit appAutoLockChanged(); } void SettingsWidget::on_resetAppAutoLockPushButton_clicked() { diff --git a/src/settingswidget.h b/src/settingswidget.h index c9931a2..ccf48ec 100644 --- a/src/settingswidget.h +++ b/src/settingswidget.h @@ -28,6 +28,7 @@ signals: void notify(QString message); void zoomChanged(); void zoomMaximizedChanged(); + void appAutoLockChanged(); public: explicit SettingsWidget(QWidget *parent = nullptr, @@ -43,6 +44,8 @@ public slots: void loadDictionaries(QStringList dictionaries); void clearAllData(); + void autoAppLockSetChecked(bool checked); + void updateAppLockPasswordViewer(); protected slots: bool eventFilter(QObject *obj, QEvent *event); void closeEvent(QCloseEvent *event); diff --git a/src/settingswidget.ui b/src/settingswidget.ui index 44f6b7a..94cdcd7 100644 --- a/src/settingswidget.ui +++ b/src/settingswidget.ui @@ -6,8 +6,8 @@ 0 0 - 637 - 792 + 691 + 860 @@ -98,8 +98,8 @@ background:transparent; 0 0 - 621 - 791 + 675 + 808 @@ -371,7 +371,8 @@ background:transparent; - Minimize/Maximize on right clicking tray Icon + Minimize/Maximize on right clicking tray Icon +(if supported by system tray) @@ -805,14 +806,11 @@ background:transparent; Secs - 10 + 8 14400 - - 60 - -- cgit v1.2.3