Guest User

Untitled

a guest
Apr 24th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QtWidgets>
  4. #include <QDateTime>
  5. #include <QTimer>
  6.  
  7. #define Path_to_DB "/home/ghostly/Desktop/projects/Business Development /HMS/APPLICATIONS/Database/HMSSecureD"
  8.  
  9. MainWindow::MainWindow(QWidget *parent) :
  10. QMainWindow(parent),
  11. ui(new Ui::MainWindow)
  12. {
  13. ui->setupUi(this);
  14.  
  15.  
  16. //set up for the date and time labels
  17. QTimer *timer = new QTimer(this);
  18. connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
  19. timer->start(1000);
  20. showTime();
  21.  
  22. QDateTime dateTime = dateTime.currentDateTime();
  23. QString dateTimeString = dateTime.toString("dddd dd/MM/yyyy");
  24. QString errorDateTimeString = dateTime.toString("dd/MM/yy hh:mm:ss");
  25. ui->showDateLabel->setText(dateTimeString);
  26.  
  27. //set up for the errorLogs
  28. QString errorLogsName = "Logs.txt";
  29. QFile errorLogs(errorLogsName);
  30. if (QFileInfo::exists("Logs.txt"))
  31. {
  32. ui->statusBar->showMessage("[+] Logging activated",5);
  33. errorLogs.open(QIODevice::WriteOnly | QIODevice::Append);
  34. QTextStream errorStream(&errorLogs);
  35. errorStream << errorDateTimeString + ":New Session: 0101 Logging activated for new session \n"; //later ill add computer name etc
  36. errorLogs.flush();
  37. errorLogs.close();
  38. }
  39. else
  40. {
  41. ui->statusBar->showMessage("Error logs do not exist, creating Logs");
  42. errorLogs.open(QIODevice::ReadWrite | QIODevice::Text);
  43. errorLogs.write("Logs.txt \n");
  44. ui->statusBar->showMessage("Logging file created",2);
  45.  
  46. QTextStream errorStream(&errorLogs);
  47. errorStream << errorDateTimeString + ":Log Created: 0102 Logging activated for new session \n"; //later ill add computer name etc
  48. errorLogs.flush();
  49. errorLogs.close();
  50. ui->statusBar->showMessage("Ready to connect to the database!");
  51. }
  52.  
  53. //connect to the database. this will later be encrypted with 256bit AES encryption
  54.  
  55. ui->statusBar->showMessage("Connecting to database now!");
  56. myDB = QSqlDatabase::addDatabase("QSQLITE");
  57. myDB.setDatabaseName(Path_to_DB);
  58. QFileInfo checkFile(Path_to_DB);
  59.  
  60. if(checkFile.isFile())
  61. {
  62. if(myDB.open())
  63. {
  64. ui->statusBar->showMessage("[+] Connected to the database.");
  65. errorLogs.open(QIODevice::WriteOnly | QIODevice::Append);
  66. QTextStream errorStream(&errorLogs);
  67. errorStream << errorDateTimeString + ":LOG: 0103 Connected \n"; //later ill add computer name etc
  68. errorLogs.flush();
  69. errorLogs.close();
  70. }
  71. }else{
  72. ui->statusBar->showMessage("[+] Unable to connect to the database, database missing!");
  73. // Write Error to errorLogs {time + "Unable to connect to the database, database missing!" }
  74. errorLogs.open(QIODevice::WriteOnly | QIODevice::Append);
  75. QTextStream errorStream(&errorLogs);
  76. errorStream << errorDateTimeString + ":ERROR: 0105 Unable to connect to the database, database missing! \n"; //later ill add computer name etc
  77. errorLogs.flush();
  78. errorLogs.close();
  79. }
  80. }
  81.  
  82. MainWindow::~MainWindow()
  83. {
  84. delete ui;
  85.  
  86. // errorLog
  87. QDateTime dateTime = dateTime.currentDateTime();
  88. QString errorDateTimeString = dateTime.toString("dd/MM/yy hh:mm:ss");
  89. QString errorLogsName = "Logs.txt";
  90. QFile errorLogs(errorLogsName);
  91. errorLogs.open(QIODevice::WriteOnly | QIODevice::Append);
  92. QTextStream errorStream(&errorLogs);
  93. errorStream << errorDateTimeString + ":EXIT: 0109 Application exited \n";
  94. errorLogs.flush();
  95. errorLogs.close();
  96.  
  97. //close database connection
  98. myDB.close();
  99. }
  100.  
  101. void MainWindow::showTime()
  102. {
  103. QTime time = QTime::currentTime();
  104. QString text = time.toString("hh:mm");
  105. if ((time.second() % 2) == 0)
  106. text[2] = ' ';
  107. ui->showTimeLabel->setText(text);
  108. }
  109.  
  110.  
  111.  
  112. void MainWindow::on_loginBTN_clicked()
  113. {
  114. qDebug() << "Login clicked";
  115. QDateTime dateTime = dateTime.currentDateTime();
  116. QString errorDateTimeString = dateTime.toString("dd/MM/yy hh:mm:ss");
  117.  
  118. QString Username, Password;
  119. Username = ui->usernameIN->text();
  120. Password = ui->passwordIN->text();
  121.  
  122. if(!myDB.isOpen())
  123. {
  124. qDebug() << "err";
  125. ui->statusBar->showMessage("[+] You must be connected to the database to login!");
  126. // errorLog
  127. QString errorLogsName = "Logs.txt";
  128. QFile errorLogs(errorLogsName);
  129. errorLogs.open(QIODevice::WriteOnly | QIODevice::Append);
  130. QTextStream errorStream(&errorLogs);
  131. errorStream << errorDateTimeString + ":ERROR: 0106 Tried to login without connecting to the database \n"; //later ill add computer name etc
  132. errorLogs.flush();
  133. errorLogs.close();
  134. return;
  135. }
  136. qDebug() << "qry";
  137. QSqlQuery qry;
  138. if(qry.exec("SELECT Username, Password, Role FROM StaffUsers WHERE Username=\'"
  139. + Username + "\' AND Password=\'" + Password + "\'"))
  140. {
  141. qDebug() << "qry next";
  142. if(qry.next())
  143. {
  144. ui->statusBar->showMessage("[+] Valid Login");
  145. QString msg = "Username = " + qry.value(0).toString() + " \n" +
  146. "Password = " + qry.value(1).toString() + " \n" +
  147. "Role = " +qry.value(2).toString();
  148. QMessageBox::warning(this, "Login was successsful", msg); // later add what type of user
  149.  
  150. return;
  151.  
  152.  
  153. }else{
  154. qDebug() << "INVALID";
  155. ui->statusBar->showMessage("[-] Invalid User details!");
  156.  
  157.  
  158. }
  159. }
  160. }
Add Comment
Please, Sign In to add comment