forked from markopoloparadox/ProjectPerun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoginWindow.cpp
195 lines (173 loc) · 8.24 KB
/
LoginWindow.cpp
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
#include "LoginWindow.h"
#include "ui_LoginWindow.h"
#include <QMessageBox>
LoginWindow::LoginWindow(bool adminMode, const char* const serverAddress, const short serverPort, QWidget *parent) :
QMainWindow(parent),
socket(new QTcpSocket()),
adminMode(adminMode), //value of adminMode as a parameter (that was sent from main() function) is set to adminMode attribute of LoginWindow (that's required because it will be forwarded next to instance of MainWindow class)
serverAddress(serverAddress),
serverPort(serverPort),
ui(new Ui::LoginWindow)
{
this->ui->setupUi(this);
this->setWindowFlags(Qt::Dialog);
this->setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
this->ui->passwordLineEdit->setEchoMode(QLineEdit::Password); //hides entered characters while typing
connect(this->ui->registerButton, SIGNAL(clicked()), this, SLOT(registerAnAccount()));
connect(this->ui->loginButton, SIGNAL(clicked()), this, SLOT(login()));
connect(this->ui->passwordLineEdit, SIGNAL(returnPressed()), this, SLOT(login()));
connect(this->ui->emailLineEdit, SIGNAL(returnPressed()), this, SLOT(login()));
if (!adminMode) {
QMessageBox msgBox;
msgBox.setText("To use all features that application contains, please run this application with administative privileges.");
msgBox.exec();
}
}
LoginWindow::~LoginWindow()
{
delete this->ui;
}
void LoginWindow::registerAnAccount() {
/*
* Qstring za razliku od std::string nam dopusta rad s unicodom, ima korisnije metode te
* daje bolje performanse. Također, brojne funkcije koje kao ulaz traže znakovni niz, zapravo
* traže QString tip podataka. Vrijednosti koji se nalaze u našim tekstualnim okvirima kopiramo
* u pomoćne varijable koristeći metodu text();
*
*/
QString email, password;
email = this->ui->emailLineEdit->text();
password = this->ui->passwordLineEdit->text();
//Treba upozoriti korisnika ako je jedan teksutalni okvir prazan
if(email.isEmpty() || password.isEmpty()) {
this->ui->statusLabel->setText("Please enter all the information!");
this->ui->statusLabel->setStyleSheet("QLabel { background-color : white; color : blue; }");
return;
}
/*
* Metoda connectToHost(Qstring, quint16,...) kao prvi argument prima ip adresu koja može
* biti u dva oblika ("192.168.0.1") ili ("mywebpage.com") a kao drugi broj porta.
* Sljedeći parametri su neobavezni pa ih neću niti napomenuti.
* Kako bi se program izvodio sinkronizirano (jer se u suprotnom može dogoditi da šaljemo
* podatke a još se nismo spojiti) koristimo metodu waitForConnected(ms) koja nakon što je
* primljen signal da smo spojeni vraća true odnosno vraća false nakon što istekne određeno
* vrijeme(timeout je određen brojem u zagradi).
*
*/
this->socket->connectToHost(this->serverAddress, this->serverPort);
if(this->socket->waitForConnected(3000))
qDebug() << "Connection with server is successfully established!";
else {
qDebug() << "Connection with server could not be established!";
this->ui->statusLabel->setText("Server is offline!");
this->ui->statusLabel->setStyleSheet("QLabel { background-color : white; color : red; }");
return;
}
/*
* QJsonObject je objekt koji se sastoji od više QJsonValue. QJsonObject možemo gledati kao
* klasu koje se može sastojati od različitih tipova podataka, te mi možemo proizvoljno
* dodavati elemente koje želimo. Za dodavanje elementa u QJsonObject korisimo ovu sintaxu
* QJsonObject["key"] = example.
*
* QJsonDocument je srce i pokazatelj moći JSON strukture. QJsonDocument kao argumenat prima
* QJsonObject ili QJsonArray(isti efekt možemo dobiti ako koristimo metode setObject() ili
* setArray()) te taj objekt(ondosno array) postavlja kao primarni dokument. Koristeći metodu
* toJson(format), vraća svoj dokumenat u obliku QByteArray-a a metodom fromJson(QByteArray)
* pretvara QByteArray u QJsonDocument.
*
* QByteArray je laički gledano isto kao i QString. Koristimo ga ovjde isključivo zato što
* funkcije traže takav tip podatakaali moglii smo također i QString koristiti.
*
* Postoji dva tipa prikaza JSON datoteka. Compact i Indented. Dovoljno je znati da Compact
* je kompaktan i "ljepši" je prikaz za računala. Za ljude je bolji Indented prikaz.
* http://doc.qt.io/qt-5/qjsondocument.html#JsonFormat-enum
*
*/
QJsonObject object;
QJsonDocument document;
QByteArray packet;
object["connection"] = "0001";
object["email"] = email;
object["password"] = password;
document.setObject(object);
packet = document.toJson(QJsonDocument::Compact);
/*
* Write(QByteArray) metoda šalje sadržaj dok waitForBytesWritten(timeout) čeka dok svi
* podaci ne budu poslani (ili do poajve signala). WaitForReadyRead(timeout) čeka dok se ne
* pojavi signal readyRead() signal. Obje wait metode također blokiraju rad programa
* odnosno vrši se sinkronizacija.
*
*/
this->socket->write(packet);
this->socket->waitForBytesWritten(1000);
this->socket->waitForReadyRead(3000);
/*
* readAll() metoda čita bajtovni niz i sprema ga. Ostalo bi trebalo biti sve jasno
*
*/
packet = this->socket->readAll();
document = QJsonDocument::fromJson(packet.constData());
object = document.object();
if(object["connection"] == "0002") {
this->ui->statusLabel->setText("User with that Email adress already exists!");
this->ui->statusLabel->setStyleSheet("QLabel { background-color : white; color : blue; }");
} else if (object["connection"] == "0003") {
this->ui->statusLabel->setText("User has been added!");
this->ui->statusLabel->setStyleSheet("QLabel { background-color : white; color : green; }");
}
/*
* Odspajamo se i čekamo potvrdu da smo se odspojili. Metoda close() zatvara socket i resetira
* ga.
*
*/
this->socket->disconnectFromHost();
if(this->socket->state() == QAbstractSocket::UnconnectedState || this->socket->waitForDisconnected(1000)) {
qDebug() << "Disconnected!";
}
this->socket->close();
}
void LoginWindow::login() {
QString email, password;
email = this->ui->emailLineEdit->text();
password = this->ui->passwordLineEdit->text();
if(email.isEmpty() || password.isEmpty()) {
this->ui->statusLabel->setText("Please enter all the information!");
this->ui->statusLabel->setStyleSheet("QLabel { background-color : white; color : blue; }");
return;
}
this->socket->connectToHost(this->serverAddress, this->serverPort);
if(this->socket->waitForConnected(3000))
qDebug() << "Connection with server is successfully established!";
else {
qDebug() << "Connection with server could not be established!";
this->ui->statusLabel->setText("Server is offline!");
this->ui->statusLabel->setStyleSheet("QLabel { background-color : white; color : red; }");
return;
}
QJsonObject object;
QJsonDocument document;
QByteArray packet;
object["connection"] = "0004";
object["email"] = email;
object["password"] = password;
document.setObject(object);
packet = document.toJson(QJsonDocument::Compact);
this->socket->write(packet);
this->socket->waitForBytesWritten(1000);
this->socket->waitForReadyRead(3000);
packet = this->socket->readAll();
document = QJsonDocument::fromJson(packet.constData());
object = document.object();
if(object["connection"] == "0005") {
this->ui->statusLabel->setText("Sorry, that user does not seem to exist in our database!");
this->ui->statusLabel->setStyleSheet("QLabel { background-color : white; color : red; }");
} else if (object["connection"] == "0006") {
this->ui->statusLabel->setText("Wrong password!");
this->ui->statusLabel->setStyleSheet("QLabel { background-color : white; color : red; }");
} else if(object["connection"] == "0007") {
MainWindow* mainWin = new MainWindow(this->socket, this->adminMode, email);
//mainWin->setAttribute(Qt::WA_DeleteOnClose);
mainWin->show();
this->close();
}
}