From 6805310b65751f52bff954e3d2e78036a6e5c5a9 Mon Sep 17 00:00:00 2001 From: keshavbhatt Date: Mon, 5 Apr 2021 09:51:04 +0530 Subject: added app lock class --- src/WhatsApp.pro | 2 + src/lock.cpp | 172 ++++++++++++++ src/lock.h | 53 +++++ src/lock.ui | 676 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 903 insertions(+) create mode 100644 src/lock.cpp create mode 100644 src/lock.h create mode 100644 src/lock.ui (limited to 'src') diff --git a/src/WhatsApp.pro b/src/WhatsApp.pro index 95e6b33..5dee6e0 100644 --- a/src/WhatsApp.pro +++ b/src/WhatsApp.pro @@ -18,6 +18,8 @@ TEMPLATE = app # deprecated API to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS +LIBS += -L/usr/X11/lib -lX11 + # No debug output in release mode CONFIG(release, debug|release):DEFINES += QT_NO_DEBUG_OUTPUT diff --git a/src/lock.cpp b/src/lock.cpp new file mode 100644 index 0000000..3a43a91 --- /dev/null +++ b/src/lock.cpp @@ -0,0 +1,172 @@ +#include "lock.h" +#include "ui_lock.h" +#include +#include +#ifdef Q_OS_WIN32 +#include +#else +#include // sudo apt install libx11-dev +#endif + +Lock::Lock(QWidget *parent) : + QWidget(parent), + ui(new Ui::Lock) +{ + ui->setupUi(this); + ui->unlock->setEnabled(false); + ui->setPass->setEnabled(false); + ui->wrong->hide(); + + if(settings.value("asdfg").isValid() == false) + { + ui->signup->show(); + ui->login->hide(); + ui->passcode1->setFocus(); + } + else + { + lock_app(); + } + checkCaps(); + QString capsStyle = QString("background-color: rgba(0,170,0,20);" + "padding:4px;" + "border:1px solid rgba(78, 154, 6,50);" + "border-radius: 2px;"); + ui->caps1->setStyleSheet(capsStyle); + ui->caps2->setStyleSheet(capsStyle); + ui->signup_warning->setStyleSheet(capsStyle); + ui->wrong->setStyleSheet(capsStyle); +} + +Lock::~Lock() +{ + delete ui; +} + +void Lock::checkCaps() +{ + if(getCapsLockOn()){ + ui->caps1->show(); + ui->caps2->show(); + }else{ + ui->caps1->hide(); + ui->caps2->hide(); + } +} + +void Lock::keyReleaseEvent(QKeyEvent *event){ + if(event->key() == Qt::Key_CapsLock){ + checkCaps(); + } +} + +bool Lock::event(QEvent* e) +{ + return QWidget::event(e); +} + +void Lock::on_passcode1_textChanged(const QString &arg1) +{ + if(arg1.contains(" ")){ + ui->passcode1->setText(arg1.simplified()); + } + ui->setPass->setEnabled(arg1.length()>4 && arg1== ui->passcode2->text()); +} + +void Lock::on_passcode2_textChanged(const QString &arg1) +{ + if(arg1.contains(" ")){ + ui->passcode2->setText(arg1.simplified()); + } + ui->setPass->setEnabled(arg1.length()>4 && arg1== ui->passcode1->text()); +} + +void Lock::on_setPass_clicked() +{ + QString pass1,pass2; + pass1 = ui->passcode1->text().trimmed(); + pass2 = ui->passcode2->text().trimmed(); + if(pass1==pass2) + { + settings.setValue("asdfg",QByteArray(pass1.toUtf8()).toBase64()); + settings.setValue("lockscreen",true); + ui->passcode1->clear(); + ui->passcode2->clear(); + emit passwordSet(); + if(check_password_set()){ + ui->signup->hide(); + ui->login->show(); + ui->passcodeLogin->setFocus(); + } + }else { + return; + } +} + +bool Lock::check_password_set(){ + return settings.value("asdfg").isValid(); +} + +void Lock::on_unlock_clicked() +{ + QString password = QByteArray::fromBase64(settings.value("asdfg").toByteArray()); + if(ui->passcodeLogin->text() == password && check_password_set()) + { + ui->login->hide(); + ui->signup->hide(); + ui->passcodeLogin->clear(); + isLocked = false; + this->hide(); + emit unLocked(); + }else{ + ui->wrong->show(); + } +} + +void Lock::on_passcodeLogin_textChanged(const QString &arg1) +{ + if(arg1.contains(" ")){ + ui->passcodeLogin->setText(arg1.simplified()); + } + ui->wrong->hide(); + ui->unlock->setEnabled(arg1.length()>4); +} + +void Lock::lock_app() +{ + checkCaps(); + ui->wrong->hide(); + ui->signup->hide(); + ui->login->show(); + ui->passcodeLogin->setFocus(); + isLocked = true; + this->show(); +} + +void Lock::on_passcodeLogin_returnPressed() +{ + on_unlock_clicked(); +} + +bool Lock::getCapsLockOn() +{ +// platform dependent method of determining if CAPS LOCK is on +#ifdef Q_OS_WIN32 // MS Windows version + return GetKeyState(VK_CAPITAL) == 1; +#else // X11 version (Linux/Unix/Mac OS X/etc...) + Display* d = XOpenDisplay((char*)0); + bool caps_state = false; + if (d) { + unsigned n; + XkbGetIndicatorState(d, XkbUseCoreKbd, &n); + caps_state = (n & 0x01) == 1; + } + return caps_state; +#endif +} + +void Lock::on_cancelSetting_clicked() +{ + emit passwordNotSet(); + this->hide(); +} diff --git a/src/lock.h b/src/lock.h new file mode 100644 index 0000000..8120c20 --- /dev/null +++ b/src/lock.h @@ -0,0 +1,53 @@ +#ifndef LOCK_H +#define LOCK_H + +#include +#include + +namespace Ui { +class Lock; +} + +class Lock : public QWidget +{ + Q_OBJECT + +public: + explicit Lock(QWidget *parent = nullptr); + ~Lock(); + bool isLocked = true; + +private slots: + void on_passcode1_textChanged(const QString &arg1); + + void on_passcode2_textChanged(const QString &arg1); + + void on_setPass_clicked(); + bool check_password_set(); + void on_unlock_clicked(); + + void on_passcodeLogin_textChanged(const QString &arg1); + + void on_passcodeLogin_returnPressed(); + + bool getCapsLockOn(); + void checkCaps(); + void on_cancelSetting_clicked(); + +public slots: + void lock_app(); +signals: + void passwordSet(); + void passwordNotSet(); + void unLocked(); + +protected slots: + void keyReleaseEvent(QKeyEvent *event); + + bool event(QEvent *e); +private: + Ui::Lock *ui; + QSettings settings; +}; + +#endif // LOCK_H diff --git a/src/lock.ui b/src/lock.ui new file mode 100644 index 0000000..1746704 --- /dev/null +++ b/src/lock.ui @@ -0,0 +1,676 @@ + + + Lock + + + + 0 + 0 + 810 + 510 + + + + + 0 + 0 + + + + Form + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + QWidget#signup { + background-image:url(:/icons/texture.png) +} + + + + 20 + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + 0 + + + 0 + + + 0 + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + QWidget#widget_2{ +background-color: rgb(46, 52, 54); +border-radius: 5px; +background-image:url(:/icons/texture.png) +} + + + + 10 + + + QLayout::SetMinimumSize + + + 20 + + + 20 + + + 20 + + + 20 + + + + + 0 + + + 5 + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + 128 + 128 + + + + background-color:transparent; + + + + + + :/icons/app/icon-128.png + + + Qt::AlignCenter + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + 0 + 24 + + + + + 14 + + + + background-color:transparent; + + + Set application lock passcode + + + Qt::AlignCenter + + + true + + + + + + + + 0 + 30 + + + + background-color:transparent; + + + QLineEdit::Password + + + Qt::AlignCenter + + + enter passcode + + + + + + + + 0 + 30 + + + + background-color:transparent; + + + QLineEdit::Password + + + Qt::AlignCenter + + + enter passcode again + + + + + + + + 0 + 30 + + + + Set Pass Code + + + + + + + + 0 + 30 + + + + Cancel + + + + + + + Warning: Caps Lock is On + + + Qt::AlignCenter + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + 0 + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + <html><head/><body><p>Note: Passcode must be more then 4 characters and must match in both fields.</p></body></html> + + + Qt::AlignCenter + + + false + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + QWidget#login { + background-image:url(":/icons/texture.png") +} + + + + 20 + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + 0 + + + 0 + + + 0 + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + QWidget#widget{ +background-color: rgb(46, 52, 54); +border-radius: 5px; +background-image:url(:/icons/texture.png) +} + + + + 10 + + + 20 + + + 20 + + + 20 + + + 20 + + + + + 0 + + + 0 + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + 128 + 128 + + + + background-color:transparent; + + + + + + :/icons/app/icon-128.png + + + Qt::AlignCenter + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + 0 + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + 32 + 32 + + + + + 32 + 32 + + + + background-color:transparent; + + + + + + :/icons/lock-2-fill.png + + + true + + + Qt::AlignCenter + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + 0 + 24 + + + + + 14 + + + + background-color:transparent; + + + Enter your passcode to get access to app + + + Qt::AlignCenter + + + true + + + + + + + + 0 + 30 + + + + background-color:transparent; + + + QLineEdit::Password + + + Qt::AlignCenter + + + enter your passcode + + + + + + + + 0 + 30 + + + + Unlock + + + + + + + Warning: Caps Lock is On + + + Qt::AlignCenter + + + + + + + <html><head/><body><p>Wrong Passcode, Please try again.</p></body></html> + + + Qt::AlignCenter + + + true + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + + + -- cgit v1.2.3