mirror of
				https://gitlab.com/comunic/comunicwatcher
				synced 2025-10-31 02:04:45 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			99 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			99 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| #include "apirequest.h"
 | |
| #include "loginwindow.h"
 | |
| #include "ui_loginwindow.h"
 | |
| 
 | |
| #include <QMessageBox>
 | |
| 
 | |
| LoginWindow::LoginWindow(QWidget *parent)
 | |
|     : QDialog(parent)
 | |
|     , ui(new Ui::LoginWindow)
 | |
| {
 | |
|     ui->setupUi(this);
 | |
|     setWindowFlag(Qt::FramelessWindowHint);
 | |
|     setLoading(false);
 | |
| }
 | |
| 
 | |
| LoginWindow::~LoginWindow()
 | |
| {
 | |
|     delete ui;
 | |
| }
 | |
| 
 | |
| 
 | |
| void LoginWindow::on_closeButton_clicked()
 | |
| {
 | |
|     this->close();
 | |
| }
 | |
| 
 | |
| 
 | |
| void LoginWindow::mousePressEvent(QMouseEvent *evt)
 | |
| {
 | |
|     mOldPos = evt->globalPos();
 | |
| }
 | |
| 
 | |
| void LoginWindow::mouseMoveEvent(QMouseEvent *evt)
 | |
| {
 | |
|     const QPoint delta = evt->globalPos() - mOldPos;
 | |
|     move(x()+delta.x(), y()+delta.y());
 | |
|     mOldPos = evt->globalPos();
 | |
| }
 | |
| 
 | |
| void LoginWindow::submitForm()
 | |
| {
 | |
|     if(ui->emailEdit->text().isEmpty()) {
 | |
|         QMessageBox::warning(this, tr("Error"), tr("Please specify an email address!"));
 | |
|         return;
 | |
|     }
 | |
| 
 | |
|     if(ui->passwordEdit->text().isEmpty()) {
 | |
|         QMessageBox::warning(this, tr("Error"), tr("Please specify your password!"));
 | |
|         return;
 | |
|     }
 | |
| 
 | |
|     setLoading(true);
 | |
| 
 | |
|     auto req = new APIRequest("account/login");
 | |
|     req->addString("userMail", ui->emailEdit->text());
 | |
|     req->addString("userPassword", ui->passwordEdit->text());
 | |
|     req->exec();
 | |
|     connect(req, &APIRequest::done, this, &LoginWindow::onResponse);
 | |
| }
 | |
| 
 | |
| void LoginWindow::onResponse(APIResponse res)
 | |
| {
 | |
|     if(res.isError()) {
 | |
|         QString msg;
 | |
| 
 | |
|         switch(res.getCode()) {
 | |
|         case 401:
 | |
|             msg = tr("Invalid credentials!");
 | |
|             break;
 | |
| 
 | |
|         case 429:
 | |
|             msg = tr("Too many login attempt, please try again later!");
 | |
|             break;
 | |
| 
 | |
|         default:
 | |
|             msg = tr("An error occured while trying to sign you in!");
 | |
|             break;
 | |
|         }
 | |
| 
 | |
|         setLoading(false);
 | |
|         QMessageBox::warning(this, tr("Login failed"), msg);
 | |
| 
 | |
|         return;
 | |
|     }
 | |
| 
 | |
|     QMessageBox::information(this, "ok", "success");
 | |
| }
 | |
| 
 | |
| void LoginWindow::on_submitButton_clicked()
 | |
| {
 | |
|     submitForm();
 | |
| }
 | |
| 
 | |
| void LoginWindow::setLoading(bool loading)
 | |
| {
 | |
|     ui->loginProgress->setVisible(loading);
 | |
|     ui->loginFormContainer->setVisible(!loading);
 | |
| }
 |