#include "dialoglogin.h" #include "ui_dialoglogin.h" #include "mainwindow.h" #include "Modules/Logger.h" #include "Modules/socketactions.h" #include "Modules/methods.h" #include #include #include #include #include #include #include #include #include #include #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("checkBox_rememberMe"); textEdit.username = findChild("textEdit_username"); textEdit.passwd = findChild("textEdit_passwd"); pushButton.ok = findChild("pushButton_ok"); pushButton.cancel = findChild("pushButton_cancel"); label.connStatus = findChild("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.toStdString() + " " + passwd.toStdString() + " " + to_string(getUnixTimestamp()) + ""); if(getTextMiddle(resp, "[", "]") == "Login_Accepted"){ session.userName = getTextMiddle(resp, "", ""); session.token = getTextMiddle(resp, "", ""); session.userID = getTextMiddle(resp, "", ""); 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; } }