forked from giovannic96/Real-time-collaborative-text-editor
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstartwindow.cpp
More file actions
252 lines (216 loc) · 8.65 KB
/
Copy pathstartwindow.cpp
File metadata and controls
252 lines (216 loc) · 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include "startwindow.h"
#include "ui_startwindow.h"
#include "menuwindow.h"
#include <QMessageBox>
#include <iostream>
#include <thread>
#include <QInputDialog>
#include "userprofile.h"
#include "jsonUtility.h"
#include "editorwindow.h"
#include "message.h"
//#include <QDesktopWidget>
using json = nlohmann::json;
using boost::asio::ip::tcp;
typedef std::deque<message> message_queue;
//CONSTRUCTOR
StartWindow::StartWindow(QWidget *parent): QMainWindow(parent, Qt::FramelessWindowHint | Qt::WindowSystemMenuHint),
ui(new Ui::StartWindow), _client(new myClient)
{
//QRect screenGeometry = QApplication::desktop()->screenGeometry();
QRect screenGeometry = QGuiApplication::primaryScreen()->availableGeometry();
double width = screenGeometry.width();
int minWidth = 1920;
double scale = width / minWidth;
std::string scaleAsString = std::to_string(scale);
QByteArray scaleAsQByteArray(scaleAsString.c_str(), scaleAsString.length());
qputenv("QT_SCALE_FACTOR", scaleAsQByteArray);
ui->setupUi(this);
ui->version->setText(qstr);
ui->LoginUsernameForm->setFocus();
ui->usernameERR->hide();
ui->passERR->hide();
ui->mailERR->hide();
setStatus(_client->getStatus());
connect(_client, &myClient::statusChanged, this, &StartWindow::setStatus);
connect(_client, &myClient::formResultSuccess, this, &StartWindow::showPopupSuccess);
connect(_client, &myClient::formResultFailure, this, &StartWindow::showPopupFailure);
connect(_client, &myClient::jsonMsgFailure, this, &StartWindow::showJsonPopupFailure);
setFixedSize(size()); //IS AN HALF HELP WITH THE DPI-Related-BUG - DON'T DELETE ME FOR NOW
}
//DESTRUCTOR
StartWindow::~StartWindow() {
delete ui;
delete _client;
}
void StartWindow::mousePressEvent(QMouseEvent *evt){
oldPos = evt->globalPos();
}
void StartWindow::mouseMoveEvent(QMouseEvent *evt){
const QPoint delta = evt->globalPos() - oldPos;
move(x()+delta.x(), y()+delta.y());
oldPos = evt->globalPos();
}
void StartWindow::on_LoginButton_clicked(){
if(_client->getStatus()==false){
_client ->do_connect();
sleep(1000);
qDebug () << "IL SERVER é connesso?--> " <<_client->getStatus();
if(_client->getStatus()==false){
//secondo controllo se non sono riuscito a ricollegarmi al server
QMessageBox::warning(nullptr, "Attenzione", "Non sono riuscito a contattare il server!\n" "Riprova più tardi");
}else{
LoginProcedure();
}
} else {
LoginProcedure();
}
}
void StartWindow::LoginProcedure(){
QString user = ui->LoginUsernameForm->text();
QByteArray ba_user = user.toLocal8Bit();
const char *c_user = ba_user.data();
QString pass = ui->LoginPasswordForm->text();
QByteArray ba_pass = pass.toLocal8Bit();
const char *c_pass = ba_pass.data();
//update client data
_client->setUsername(user);
//Serialize data
json j;
jsonUtility::to_json(j, "LOGIN_REQUEST", c_user, c_pass);
const std::string req = j.dump();
//Send data (header and body)
_client->sendRequestMsg(req);
}
void StartWindow::on_SignUpButton_clicked() {
if(_client->getStatus()==false) {
QMessageBox::warning(nullptr, "Attenzione", "Non sono riuscito a contattare il server!\n"
"Riprova più tardi");
} else {
if (ui->RegUsernameForm->text().isEmpty()) {
ui->usernameERR->show();
} else {
ui->usernameERR->hide();
if (ui->RegPasswordForm->text().length() < 6) {
ui->passERR->show();
} else {
ui->passERR->hide();
QRegularExpression mailREX("^[0-9a-zA-Z]+([0-9a-zA-Z]*[-._+])*[0-9a-zA-Z]+@[0-9a-zA-Z]+([-.][0-9a-zA-Z]+)*([0-9a-zA-Z]*[.])[a-zA-Z]{2,6}$");
regMat = mailREX.match(ui->RegMailForm->text()).hasMatch();
if (!regMat) {
ui->mailERR->show();
} else {
ui->mailERR->hide();
//Get data from the form
QString user = ui->RegUsernameForm->text();
QByteArray ba_user = user.toLocal8Bit();
const char *c_user = ba_user.data();
QString pass = ui->RegPasswordForm->text();
QByteArray ba_pass = pass.toLocal8Bit();
const char *c_pass = ba_pass.data();
QString email = ui->RegMailForm->text();
QByteArray ba_email = email.toLocal8Bit();
const char *c_email = ba_email.data();
//Serialize data
json j;
jsonUtility::to_json(j, "SIGNUP_REQUEST", c_user, c_pass, c_email);
const std::string req = j.dump();
//Send data (header and body)
_client->sendRequestMsg(req);
}
}
}
}
}
void StartWindow::on_RegisterButton_clicked() {
ui->stackedWidget->setCurrentIndex(1);
}
void StartWindow::on_AccediButton_clicked() {
ui->stackedWidget->setCurrentIndex(0);
}
void StartWindow::on_exitButton_clicked() {
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Uscita", "Vuoi uscire?",
QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes) {
QApplication::exit();
}
}
void StartWindow::setStatus(bool newStatus) {
if(newStatus)
ui->label_status->setText(tr("<font color=\"green\">CONNECTED</font>"));
else
ui->label_status->setText(tr("<font color=\"red\">DISCONNECTED</font>"));
}
void StartWindow::showPopupSuccess(QString result) {
if(result == "LOGIN_SUCCESS") {
MenuWindow *m = new MenuWindow(_client);
this->close(); //this startWindow will be then created (new) when user press Logout button on menuWindow
m->show();
} else if(result == "SIGNUP_SUCCESS") {
QMessageBox::information(this,"Complimenti", "La registrazione è avvenuta correttamente!");
ui->stackedWidget->setCurrentIndex(0);
}
}
void StartWindow::showPopupFailure(QString result) {
if(result == "LOGIN_FAILURE") {
QMessageBox::critical(this,"Errore", "Il login non è stato completato correttamente! Riprova!"); //Stay in the same window
} else if(result == "SIGNUP_FAILURE") {
QMessageBox::critical(this,"Errore", "La registrazione non è avvenuta correttamente! Riprova!"); //Stay in the same window
} else {
QMessageBox::information(nullptr, "Attenzione", "Qualcosa è andato storto! Riprova!");
}
}
void StartWindow::showJsonPopupFailure(QString windowName,QString msg) {
if(windowName == "StartWindow") {
QMessageBox::critical(this, "Errore", msg);
QApplication::exit();
}
}
//LOGIN PAGE
void StartWindow::on_LoginPasswordForm_returnPressed() {
on_LoginButton_clicked();
}
void StartWindow::on_LoginUsernameForm_returnPressed() {
on_LoginButton_clicked();
}
//REG PAGE
void StartWindow::on_RegUsernameForm_returnPressed() {
on_SignUpButton_clicked();
}
void StartWindow::on_RegPasswordForm_returnPressed() {
on_SignUpButton_clicked();
}
void StartWindow::on_RegMailForm_returnPressed() {
on_SignUpButton_clicked();
}
void StartWindow::on_RegPasswordForm_editingFinished() {
if (ui->RegPasswordForm->text().length() < 6)
ui->passERR->show();
else
ui->passERR->hide();
}
void StartWindow::on_RegUsernameForm_editingFinished() {
if (ui->RegUsernameForm->text().isEmpty())
ui->usernameERR->show();
else
ui->usernameERR->hide();
}
void StartWindow::on_RegMailForm_editingFinished() {
QRegularExpression mailREX("^[0-9a-zA-Z]+([0-9a-zA-Z]*[-._+])*[0-9a-zA-Z]+@[0-9a-zA-Z]+([-.][0-9a-zA-Z]+)*([0-9a-zA-Z]*[.])[a-zA-Z]{2,6}$");
regMat = mailREX.match(ui->RegMailForm->text()).hasMatch();
if (!regMat)
ui->mailERR->show();
else
ui->mailERR->hide();
}
void StartWindow::on_eyeButton_clicked() {
if(ui->eyeButton->isChecked()){
ui->LoginPasswordForm->setEchoMode(QLineEdit::Normal);
ui->eyeButton->setStyleSheet("border-image: url(:image/hidePassword.png);");
}
else{
ui->LoginPasswordForm->setEchoMode(QLineEdit::Password);
ui->eyeButton->setStyleSheet("border-image: url(:image/showPassword.png);");
}
}