116 lines
3.7 KiB
C++
116 lines
3.7 KiB
C++
#include "dialoglogin.h"
|
|
#include "ui_dialoglogin.h"
|
|
#include "mainwindow.h"
|
|
#include "Modules/Logger.h"
|
|
#include "Modules/socketactions.h"
|
|
#include "Modules/methods.h"
|
|
#include <winsock2.h>
|
|
#include <ws2tcpip.h>
|
|
#include <Windows.h>
|
|
#include <QPushButton>
|
|
#include <QCheckBox>
|
|
#include <QTextEdit>
|
|
#include <QLabel>
|
|
#include <QString>
|
|
#include <QApplication>
|
|
#include <QIntValidator>
|
|
#pragma comment(lib, "ws2_32.lib")
|
|
#pragma comment(lib, "user32.lib")
|
|
|
|
using std::to_string;
|
|
using std::string;
|
|
|
|
structCheckBox checkBox;
|
|
structTextEdit textEdit;
|
|
structPushButton pushButton;
|
|
structLabel label;
|
|
structSession session;
|
|
|
|
extern string configPath;
|
|
extern structUserInfo userInfo;
|
|
|
|
DialogLogin::DialogLogin(QWidget *parent)
|
|
: QDialog(parent)
|
|
, ui(new Ui::DialogLogin){
|
|
|
|
ui->setupUi(this);
|
|
/*
|
|
* Place all elements handles here
|
|
*/
|
|
checkBox.rememberMe = findChild<QCheckBox*>("checkBox_rememberMe");
|
|
textEdit.username = findChild<QTextEdit*>("textEdit_username");
|
|
textEdit.passwd = findChild<QTextEdit*>("textEdit_passwd");
|
|
pushButton.ok = findChild<QPushButton*>("pushButton_ok");
|
|
pushButton.cancel = findChild<QPushButton*>("pushButton_cancel");
|
|
label.connStatus = findChild<QLabel*>("label_connStatus");
|
|
}
|
|
|
|
DialogLogin::~DialogLogin()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void DialogLogin::enableAll() {
|
|
checkBox.rememberMe->setEnabled(true);
|
|
textEdit.username->setEnabled(true);
|
|
textEdit.passwd->setEnabled(true);
|
|
pushButton.ok->setEnabled(true);
|
|
pushButton.cancel->setEnabled(true);
|
|
label.connStatus->setText("Connected");
|
|
label.connStatus->setStyleSheet("QLabel { color: green; }");
|
|
if(userInfo.userName != "" && userInfo.passwd != ""){
|
|
tryLogin(userInfo.userName, userInfo.passwd);
|
|
}
|
|
}
|
|
|
|
void DialogLogin::on_pushButton_ok_clicked()
|
|
{
|
|
QString username = textEdit.username->toPlainText();
|
|
QString passwd = textEdit.passwd->toPlainText();
|
|
|
|
if(username.isEmpty() && passwd.isEmpty()){
|
|
::MessageBoxA(NULL, "Username or Password can't be empty.", "Error", MB_OK|MB_ICONERROR);
|
|
}
|
|
if(!tryLogin(username.toStdString(), passwd.toStdString())){
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
void DialogLogin::on_pushButton_cancel_clicked()
|
|
{
|
|
QApplication::quit();
|
|
}
|
|
|
|
bool DialogLogin::tryLogin(string userName, string passWd){
|
|
QString username = QString::fromStdString(userName);
|
|
QString passwd = QString::fromStdString(passWd);
|
|
|
|
if (username.isEmpty() || passwd.isEmpty()){
|
|
return false;
|
|
} else {
|
|
string resp = sockAct::tcpSend("[newUserEntry] <userName>" + username.toStdString() + "</userName> <userPasswd>" + passwd.toStdString() + "</userPasswd> <timeStamp>" + to_string(getUnixTimestamp()) + "</timeStamp>");
|
|
if(getTextMiddle(resp, "[", "]") == "Login_Accepted"){
|
|
session.userName = getTextMiddle(resp, "<userName>", "</userName>");
|
|
session.token = getTextMiddle(resp, "<token>", "</token>");
|
|
session.userID = getTextMiddle(resp, "<userID>", "</userID>");
|
|
is.loggedIn = true;
|
|
|
|
if(checkBox.rememberMe->isChecked()){
|
|
setKeyText(configPath, "userInfo", "userName", username.toStdString());
|
|
setKeyText(configPath, "userInfo", "passwd", passwd.toStdString());
|
|
}
|
|
userInfo.userName = username.toStdString();
|
|
userInfo.passwd = passwd.toStdString();
|
|
emit loadMainWindow();
|
|
this->close();
|
|
return true;
|
|
} else if (getTextMiddle(resp, "[", "]") == "Login_Rejected"){
|
|
::MessageBoxA(NULL, ("Login failed due to: " + resp.substr(resp.find_first_of(' '))).c_str(), "Login Failed", MB_OK|MB_ICONERROR);
|
|
postLog("[userLogin] Login rejected due to: " + resp, 3);
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
}
|