aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Keshav Bhatt <keshavnrj@gmail.com>2022-03-30 17:30:58 +0530
committerLibravatar Keshav Bhatt <keshavnrj@gmail.com>2022-03-30 17:30:58 +0530
commitd06a4abb4755d0e97e8ef03688bc878117d90b4e (patch)
treeace02b248a88ccd08dabee8b9ddc751fb40bb776
parent5be4cae996d0411f4ab50af66d95452bf3a6022e (diff)
downloadwhatsie-d06a4abb4755d0e97e8ef03688bc878117d90b4e.tar.gz
whatsie-d06a4abb4755d0e97e8ef03688bc878117d90b4e.zip
feat: app auto locking
- settings to allow app auto locking after set time interval
-rw-r--r--src/WhatsApp.pro1
-rw-r--r--src/autolockeventfilter.h48
-rw-r--r--src/common.h2
-rw-r--r--src/lock.cpp1
-rw-r--r--src/mainwindow.cpp80
-rw-r--r--src/mainwindow.h4
-rw-r--r--src/settingswidget.cpp27
-rw-r--r--src/settingswidget.h3
-rw-r--r--src/settingswidget.ui16
9 files changed, 146 insertions, 36 deletions
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 <QDebug>
+#include <QEvent>
+#include <QTimer>
+
+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() <<Q_FUNC_INFO << result;
- if(result.isValid() && result.toString().isEmpty() == false){
- loggedIn = true;
- }
+bool MainWindow::isLoggedIn() {
+ static bool loggedIn = false;
+ if (webEngine && webEngine->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" <<loggedIn;
- return loggedIn;
- }else{
- qDebug() << "isLoggedIn" <<loggedIn;
- return loggedIn;
- }
+ qDebug() << "isLoggedIn" << loggedIn;
+ return loggedIn;
+ } else {
+ qDebug() << "isLoggedIn" << loggedIn;
+ return loggedIn;
+ }
}
void MainWindow::tryLogOut() {
@@ -244,6 +262,8 @@ void MainWindow::init_settingWidget() {
connect(settingsWidget, SIGNAL(init_lock()), this, SLOT(init_lock()));
connect(settingsWidget, SIGNAL(change_lock_password()), this,
SLOT(change_lock_password()));
+ connect(settingsWidget, SIGNAL(appAutoLockChanged()), this,
+ SLOT(appAutoLockChanged()));
connect(settingsWidget, SIGNAL(updateWindowTheme()), this,
SLOT(updateWindowTheme()));
@@ -630,17 +650,31 @@ void MainWindow::init_lock() {
void MainWindow::change_lock_password() {
settings.remove("asdfg");
settingsWidget->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 @@
<rect>
<x>0</x>
<y>0</y>
- <width>637</width>
- <height>792</height>
+ <width>691</width>
+ <height>860</height>
</rect>
</property>
<property name="windowTitle">
@@ -98,8 +98,8 @@ background:transparent;
<rect>
<x>0</x>
<y>0</y>
- <width>621</width>
- <height>791</height>
+ <width>675</width>
+ <height>808</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_5">
@@ -371,7 +371,8 @@ background:transparent;
<item row="4" column="0">
<widget class="QCheckBox" name="minimizeOnTrayIconClick">
<property name="text">
- <string>Minimize/Maximize on right clicking tray Icon</string>
+ <string>Minimize/Maximize on right clicking tray Icon
+(if supported by system tray)</string>
</property>
</widget>
</item>
@@ -805,14 +806,11 @@ background:transparent;
<string> Secs</string>
</property>
<property name="minimum">
- <number>10</number>
+ <number>8</number>
</property>
<property name="maximum">
<number>14400</number>
</property>
- <property name="value">
- <number>60</number>
- </property>
</widget>
</item>
<item row="1" column="2">