Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "logindialog.h"
- #include "ui_logindialog.h"
- #include <QKeyEvent>
- #include "networkrequestsender.h"
- #include <QJsonDocument>
- #include <QJsonObject>
- #include <QCryptographicHash>
- #include <QMessageBox>
- #include "network.h"
- LoginDialog::LoginDialog(QWidget *parent) :
- QDialog(parent),
- ui(new Ui::LoginDialog),
- m_logged (false),
- m_user_id (-1),
- m_user_session ("")
- {
- ui->setupUi(this);
- move(parentWidget()->window()->frameGeometry().topLeft() +
- parentWidget()->window()->rect().center() -
- rect().center());
- ui->lAccountNotExists->setStyleSheet("color: rgba(255, 0, 0, 0);");
- ui->lPassIncorrect->setStyleSheet("color: rgba(255, 0, 0, 0);");
- ui->lRegAccountExists->setStyleSheet("color: rgba(255, 0, 0, 0);");
- ui->lRegPassIncorrect->setStyleSheet("color: rgba(255, 0, 0, 0);");
- ui->pbReg->setEnabled(false);
- }
- LoginDialog::~LoginDialog()
- {
- delete ui;
- }
- void LoginDialog::keyPressEvent(QKeyEvent *e)
- {
- if(e->key() != Qt::Key_Escape)
- QDialog::keyPressEvent(e);
- }
- void LoginDialog::on_pbCancel_clicked()
- {
- m_logged = false;
- emit finished(QDialog::Rejected);
- }
- void LoginDialog::on_pbLogin_clicked()
- {
- QString login = ui->leLogin->text();
- QString pass = ui->lePass->text();
- QCryptographicHash md5(QCryptographicHash::Md5);
- md5.addData(login.toLatin1());
- md5.addData(pass.toLatin1());
- md5.addData(login.toLatin1());
- md5.addData(pass.toLatin1());
- QString md5_str = md5.result().toHex();
- QCryptographicHash sha256(QCryptographicHash::Sha256);
- md5.addData(login.toLatin1());
- md5.addData(pass.toLatin1());
- md5.addData(login.toLatin1());
- md5.addData(pass.toLatin1());
- QString sha256_str = sha256.result().toHex();
- NetworkRequest req(NW_LOGIN_URL);
- req.addParam("req", "login");
- req.addParam("login", login);
- req.addParam("pass", md5_str + ":" + sha256_str);
- NetworkRequestSender* sender = new NetworkRequestSender();
- QByteArray data = sender->get(req);
- QJsonDocument doc = QJsonDocument::fromJson(data);
- QJsonObject obj = doc.object();
- QString reply = obj["ret"].toString();
- if (reply.compare("OK") == 0)
- {
- m_user_id = obj["user_id"].toInt();
- m_user_login = login;
- m_user_session = obj["session"].toString();
- emit finished(QDialog::Accepted);
- }
- else
- {
- QMessageBox::critical(this, tr("Ошибка"), tr("Ошибка!\nНет такого пользователя или неверный пароль!"),
- QMessageBox::Ok, QMessageBox::NoButton);
- }
- delete sender;
- }
- void LoginDialog::on_leRegLogin_editingFinished()
- {
- //проверка наличия такого логина
- NetworkRequest req(NW_LOGIN_URL);
- req.addParam("req", "login_check");
- req.addParam("login", ui->leRegLogin->text());
- NetworkRequestSender* sender = new NetworkRequestSender();
- QByteArray data = sender->get(req);
- QJsonDocument doc = QJsonDocument::fromJson(data);
- QString reply = doc.object()["ret"].toString();
- if (reply.compare("USER_EXIST") == 0)
- {
- ui->lRegAccountExists->setStyleSheet("color: rgba(255, 0, 0, 255);");
- ui->pbReg->setEnabled(false);
- }
- else
- {
- ui->lRegAccountExists->setStyleSheet("color: rgba(255, 0, 0, 0);");
- ui->pbReg->setEnabled(true);
- }
- delete sender;
- }
- void LoginDialog::on_leRegPass_textChanged(const QString &arg1)
- {
- if (arg1.compare(ui->leRegPassCheck->text()) == 0)
- ui->lRegPassIncorrect->setStyleSheet("color: rgba(255, 0, 0, 0);");
- else
- ui->lRegPassIncorrect->setStyleSheet("color: rgba(255, 0, 0, 255);");
- }
- void LoginDialog::on_leRegPassCheck_textChanged(const QString &arg1)
- {
- if (arg1.compare(ui->leRegPass->text()) == 0)
- ui->lRegPassIncorrect->setStyleSheet("color: rgba(255, 0, 0, 0);");
- else
- ui->lRegPassIncorrect->setStyleSheet("color: rgba(255, 0, 0, 255);");
- }
- void LoginDialog::on_pbReg_clicked()
- {
- QString login = ui->leRegLogin->text();
- QString pass = ui->leRegPass->text();
- QCryptographicHash md5(QCryptographicHash::Md5);
- md5.addData(login.toLatin1());
- md5.addData(pass.toLatin1());
- md5.addData(login.toLatin1());
- md5.addData(pass.toLatin1());
- QString md5_str = md5.result().toHex();
- QCryptographicHash sha256(QCryptographicHash::Sha256);
- md5.addData(login.toLatin1());
- md5.addData(pass.toLatin1());
- md5.addData(login.toLatin1());
- md5.addData(pass.toLatin1());
- QString sha256_str = sha256.result().toHex();
- NetworkRequest req(NW_LOGIN_URL);
- req.addParam("req", "reg");
- req.addParam("login", ui->leRegLogin->text());
- req.addParam("pass", md5_str + ":" + sha256_str);
- NetworkRequestSender* sender = new NetworkRequestSender();
- QByteArray data = sender->get(req);
- QJsonDocument doc = QJsonDocument::fromJson(data);
- QString reply = doc.object()["ret"].toString();
- if (reply.compare("OK") == 0)
- {
- QMessageBox::information(this, tr("Регистрация"),
- tr("Новый пользователь был зарегестрирован.\nТеперь можно выполнить вход на предыдущей вкладке."),
- QMessageBox::Ok, QMessageBox::NoButton);
- }
- else if (reply.compare("USER_EXIST") == 0)
- {
- QMessageBox::critical(this, tr("Ошибка"), tr("Пользователь с таким именем уже существует."),
- QMessageBox::Ok, QMessageBox::NoButton);
- }
- else
- {
- QMessageBox::critical(this, tr("Ошибка"), tr("Ошибка при добавлении пользователя, попробуйте позже."),
- QMessageBox::Ok, QMessageBox::NoButton);
- }
- delete sender;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement