diff options
author | 2021-04-05 09:51:04 +0530 | |
---|---|---|
committer | 2021-04-05 09:51:04 +0530 | |
commit | 6805310b65751f52bff954e3d2e78036a6e5c5a9 (patch) | |
tree | 1c46b0f4f9a4ebccbc644738eaadfbc690217dc0 /src | |
parent | 03576d577f9aa691df207a559e840c6f27d86e88 (diff) | |
download | whatsie-6805310b65751f52bff954e3d2e78036a6e5c5a9.tar.gz whatsie-6805310b65751f52bff954e3d2e78036a6e5c5a9.zip |
added app lock class
Diffstat (limited to 'src')
-rw-r--r-- | src/WhatsApp.pro | 2 | ||||
-rw-r--r-- | src/lock.cpp | 172 | ||||
-rw-r--r-- | src/lock.h | 53 | ||||
-rw-r--r-- | src/lock.ui | 676 |
4 files changed, 903 insertions, 0 deletions
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 <QDebug> +#include <QKeyEvent> +#ifdef Q_OS_WIN32 +#include <Windows.h> +#else +#include <X11/XKBlib.h> // 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 <QWidget> +#include <QSettings> + +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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>Lock</class> + <widget class="QWidget" name="Lock"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>810</width> + <height>510</height> + </rect> + </property> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Expanding"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="windowTitle"> + <string>Form</string> + </property> + <layout class="QHBoxLayout" name="horizontalLayout"> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QWidget" name="signup" native="true"> + <property name="styleSheet"> + <string notr="true">QWidget#signup { + background-image:url(:/icons/texture.png) +}</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <property name="spacing"> + <number>20</number> + </property> + <item> + <spacer name="verticalSpacer"> + <property name="orientation"> + <enum>Qt::Vertical</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>20</width> + <height>40</height> + </size> + </property> + </spacer> + </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout_5"> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <spacer name="horizontalSpacer_7"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QWidget" name="widget_2" native="true"> + <property name="styleSheet"> + <string notr="true">QWidget#widget_2{ +background-color: rgb(46, 52, 54); +border-radius: 5px; +background-image:url(:/icons/texture.png) +}</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout_4"> + <property name="spacing"> + <number>10</number> + </property> + <property name="sizeConstraint"> + <enum>QLayout::SetMinimumSize</enum> + </property> + <property name="leftMargin"> + <number>20</number> + </property> + <property name="topMargin"> + <number>20</number> + </property> + <property name="rightMargin"> + <number>20</number> + </property> + <property name="bottomMargin"> + <number>20</number> + </property> + <item> + <layout class="QHBoxLayout" name="horizontalLayout_2"> + <property name="topMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>5</number> + </property> + <item> + <spacer name="horizontalSpacer"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QLabel" name="label"> + <property name="minimumSize"> + <size> + <width>128</width> + <height>128</height> + </size> + </property> + <property name="styleSheet"> + <string notr="true">background-color:transparent;</string> + </property> + <property name="text"> + <string/> + </property> + <property name="pixmap"> + <pixmap resource="icons.qrc">:/icons/app/icon-128.png</pixmap> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + <item> + <spacer name="horizontalSpacer_2"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + </layout> + </item> + <item> + <widget class="QLabel" name="label_3"> + <property name="minimumSize"> + <size> + <width>0</width> + <height>24</height> + </size> + </property> + <property name="font"> + <font> + <pointsize>14</pointsize> + </font> + </property> + <property name="styleSheet"> + <string notr="true">background-color:transparent;</string> + </property> + <property name="text"> + <string>Set application lock passcode</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + <property name="wordWrap"> + <bool>true</bool> + </property> + </widget> + </item> + <item> + <widget class="QLineEdit" name="passcode1"> + <property name="minimumSize"> + <size> + <width>0</width> + <height>30</height> + </size> + </property> + <property name="styleSheet"> + <string notr="true">background-color:transparent;</string> + </property> + <property name="echoMode"> + <enum>QLineEdit::Password</enum> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + <property name="placeholderText"> + <string>enter passcode</string> + </property> + </widget> + </item> + <item> + <widget class="QLineEdit" name="passcode2"> + <property name="minimumSize"> + <size> + <width>0</width> + <height>30</height> + </size> + </property> + <property name="styleSheet"> + <string notr="true">background-color:transparent;</string> + </property> + <property name="echoMode"> + <enum>QLineEdit::Password</enum> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + <property name="placeholderText"> + <string>enter passcode again</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="setPass"> + <property name="minimumSize"> + <size> + <width>0</width> + <height>30</height> + </size> + </property> + <property name="text"> + <string>Set Pass Code</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="cancelSetting"> + <property name="minimumSize"> + <size> + <width>0</width> + <height>30</height> + </size> + </property> + <property name="text"> + <string>Cancel</string> + </property> + </widget> + </item> + <item> + <widget class="QLabel" name="caps2"> + <property name="text"> + <string>Warning: Caps Lock is On</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <spacer name="horizontalSpacer_8"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + </layout> + </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout_7"> + <property name="topMargin"> + <number>0</number> + </property> + <item> + <spacer name="horizontalSpacer_11"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QLabel" name="signup_warning"> + <property name="text"> + <string><html><head/><body><p>Note: Passcode must be more then 4 characters and must match in both fields.</p></body></html></string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + <property name="wordWrap"> + <bool>false</bool> + </property> + </widget> + </item> + <item> + <spacer name="horizontalSpacer_12"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + </layout> + </item> + <item> + <spacer name="verticalSpacer_2"> + <property name="orientation"> + <enum>Qt::Vertical</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>20</width> + <height>40</height> + </size> + </property> + </spacer> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QWidget" name="login" native="true"> + <property name="styleSheet"> + <string notr="true">QWidget#login { + background-image:url(":/icons/texture.png") +}</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout_2"> + <property name="spacing"> + <number>20</number> + </property> + <item> + <spacer name="verticalSpacer_4"> + <property name="orientation"> + <enum>Qt::Vertical</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>20</width> + <height>40</height> + </size> + </property> + </spacer> + </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout_4"> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <spacer name="horizontalSpacer_6"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QWidget" name="widget" native="true"> + <property name="styleSheet"> + <string notr="true">QWidget#widget{ +background-color: rgb(46, 52, 54); +border-radius: 5px; +background-image:url(:/icons/texture.png) +}</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout_3"> + <property name="spacing"> + <number>10</number> + </property> + <property name="leftMargin"> + <number>20</number> + </property> + <property name="topMargin"> + <number>20</number> + </property> + <property name="rightMargin"> + <number>20</number> + </property> + <property name="bottomMargin"> + <number>20</number> + </property> + <item> + <layout class="QHBoxLayout" name="horizontalLayout_3"> + <property name="topMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <spacer name="horizontalSpacer_3"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QLabel" name="label_2"> + <property name="minimumSize"> + <size> + <width>128</width> + <height>128</height> + </size> + </property> + <property name="styleSheet"> + <string notr="true">background-color:transparent;</string> + </property> + <property name="text"> + <string/> + </property> + <property name="pixmap"> + <pixmap resource="icons.qrc">:/icons/app/icon-128.png</pixmap> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + <item> + <spacer name="horizontalSpacer_4"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + </layout> + </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout_6"> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <spacer name="horizontalSpacer_9"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QLabel" name="label_5"> + <property name="minimumSize"> + <size> + <width>32</width> + <height>32</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>32</width> + <height>32</height> + </size> + </property> + <property name="styleSheet"> + <string notr="true">background-color:transparent;</string> + </property> + <property name="text"> + <string/> + </property> + <property name="pixmap"> + <pixmap resource="icons.qrc">:/icons/lock-2-fill.png</pixmap> + </property> + <property name="scaledContents"> + <bool>true</bool> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + <item> + <spacer name="horizontalSpacer_10"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + </layout> + </item> + <item> + <widget class="QLabel" name="label_4"> + <property name="minimumSize"> + <size> + <width>0</width> + <height>24</height> + </size> + </property> + <property name="font"> + <font> + <pointsize>14</pointsize> + </font> + </property> + <property name="styleSheet"> + <string notr="true">background-color:transparent;</string> + </property> + <property name="text"> + <string>Enter your passcode to get access to app</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + <property name="wordWrap"> + <bool>true</bool> + </property> + </widget> + </item> + <item> + <widget class="QLineEdit" name="passcodeLogin"> + <property name="minimumSize"> + <size> + <width>0</width> + <height>30</height> + </size> + </property> + <property name="styleSheet"> + <string notr="true">background-color:transparent;</string> + </property> + <property name="echoMode"> + <enum>QLineEdit::Password</enum> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + <property name="placeholderText"> + <string>enter your passcode</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="unlock"> + <property name="minimumSize"> + <size> + <width>0</width> + <height>30</height> + </size> + </property> + <property name="text"> + <string>Unlock</string> + </property> + </widget> + </item> + <item> + <widget class="QLabel" name="caps1"> + <property name="text"> + <string>Warning: Caps Lock is On</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + <item> + <widget class="QLabel" name="wrong"> + <property name="text"> + <string><html><head/><body><p>Wrong Passcode, Please try again.</p></body></html></string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + <property name="wordWrap"> + <bool>true</bool> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <spacer name="horizontalSpacer_5"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + </layout> + </item> + <item> + <spacer name="verticalSpacer_3"> + <property name="orientation"> + <enum>Qt::Vertical</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>20</width> + <height>40</height> + </size> + </property> + </spacer> + </item> + </layout> + </widget> + </item> + </layout> + </widget> + <resources> + <include location="icons.qrc"/> + </resources> + <connections/> +</ui> |