Skip to content

Instantly share code, notes, and snippets.

@scope2229
Created April 16, 2018 18:59
Show Gist options
  • Save scope2229/d8b677c8a987745fdf7441635b21bdb2 to your computer and use it in GitHub Desktop.
Save scope2229/d8b677c8a987745fdf7441635b21bdb2 to your computer and use it in GitHub Desktop.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWidgets>
#include <QDateTime>
#include <QTimer>
#define Path_to_DB "/home/ghostly/Desktop/projects/Business Development /HMS/APPLICATIONS/Database/HMSSecureD"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//set up for the date and time labels
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
timer->start(1000);
showTime();
QDateTime dateTime = dateTime.currentDateTime();
QString dateTimeString = dateTime.toString("dddd dd/MM/yyyy");
QString errorDateTimeString = dateTime.toString("dd/MM/yy hh:mm:ss");
ui->showDateLabel->setText(dateTimeString);
//set up for the errorLogs
QString errorLogsName = "Logs.txt";
QFile errorLogs(errorLogsName);
if (QFileInfo::exists("Logs.txt"))
{
ui->statusBar->showMessage("[+] Logging activated",5);
errorLogs.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream errorStream(&errorLogs);
errorStream << errorDateTimeString + ":New Session: 0101 Logging activated for new session \n"; //later ill add computer name etc
errorLogs.flush();
errorLogs.close();
}
else
{
ui->statusBar->showMessage("Error logs do not exist, creating Logs");
errorLogs.open(QIODevice::ReadWrite | QIODevice::Text);
errorLogs.write("Logs.txt \n");
ui->statusBar->showMessage("Logging file created",2);
QTextStream errorStream(&errorLogs);
errorStream << errorDateTimeString + ":Log Created: 0102 Logging activated for new session \n"; //later ill add computer name etc
errorLogs.flush();
errorLogs.close();
ui->statusBar->showMessage("Ready to connect to the database!");
}
//connect to the database. this will later be encrypted with 256bit AES encryption
ui->statusBar->showMessage("Connecting to database now!");
myDB = QSqlDatabase::addDatabase("QSQLITE");
myDB.setDatabaseName(Path_to_DB);
QFileInfo checkFile(Path_to_DB);
if(checkFile.isFile())
{
if(myDB.open())
{
ui->statusBar->showMessage("[+] Connected to the database.");
errorLogs.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream errorStream(&errorLogs);
errorStream << errorDateTimeString + ":LOG: 0103 Connected \n"; //later ill add computer name etc
errorLogs.flush();
errorLogs.close();
}
}else{
ui->statusBar->showMessage("[+] Unable to connect to the database, database missing!");
// Write Error to errorLogs {time + "Unable to connect to the database, database missing!" }
errorLogs.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream errorStream(&errorLogs);
errorStream << errorDateTimeString + ":ERROR: 0105 Unable to connect to the database, database missing! \n"; //later ill add computer name etc
errorLogs.flush();
errorLogs.close();
}
}
MainWindow::~MainWindow()
{
delete ui;
// errorLog
QDateTime dateTime = dateTime.currentDateTime();
QString errorDateTimeString = dateTime.toString("dd/MM/yy hh:mm:ss");
QString errorLogsName = "Logs.txt";
QFile errorLogs(errorLogsName);
errorLogs.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream errorStream(&errorLogs);
errorStream << errorDateTimeString + ":EXIT: 0109 Application exited \n";
errorLogs.flush();
errorLogs.close();
//close database connection
myDB.close();
}
void MainWindow::showTime()
{
QTime time = QTime::currentTime();
QString text = time.toString("hh:mm");
if ((time.second() % 2) == 0)
text[2] = ' ';
ui->showTimeLabel->setText(text);
}
void MainWindow::on_loginBTN_clicked()
{
qDebug() << "Login clicked";
QDateTime dateTime = dateTime.currentDateTime();
QString errorDateTimeString = dateTime.toString("dd/MM/yy hh:mm:ss");
QString Username, Password;
Username = ui->usernameIN->text();
Password = ui->passwordIN->text();
if(!myDB.isOpen())
{
qDebug() << "err";
ui->statusBar->showMessage("[+] You must be connected to the database to login!");
// errorLog
QString errorLogsName = "Logs.txt";
QFile errorLogs(errorLogsName);
errorLogs.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream errorStream(&errorLogs);
errorStream << errorDateTimeString + ":ERROR: 0106 Tried to login without connecting to the database \n"; //later ill add computer name etc
errorLogs.flush();
errorLogs.close();
return;
}
qDebug() << "qry";
QSqlQuery qry;
if(qry.exec("SELECT Username, Password, Role FROM StaffUsers WHERE Username=\'"
+ Username + "\' AND Password=\'" + Password + "\'"))
{
qDebug() << "qry next";
if(qry.next())
{
ui->statusBar->showMessage("[+] Valid Login");
QString msg = "Username = " + qry.value(0).toString() + " \n" +
"Password = " + qry.value(1).toString() + " \n" +
"Role = " +qry.value(2).toString();
QMessageBox::warning(this, "Login was successsful", msg); // later add what type of user
return;
}else{
qDebug() << "INVALID";
ui->statusBar->showMessage("[-] Invalid User details!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment