|
1
2
3
|
#include "loginscreen.h"
#include "ui_loginscreen.h"
|
|
4
|
LoginScreen::LoginScreen(Socket* s, QWidget *parent) :
|
|
5
6
7
8
|
QDialog(parent),
ui(new Ui::LoginScreen)
{
ui->setupUi(this);
|
|
9
10
|
ui->nickEdit->setPlaceholderText("Nickname");
ui->serverURLEdit->setPlaceholderText("Server hostname:port");
|
|
11
12
13
14
15
|
ui->label->setStyleSheet("QLabel { color : red}");
ui->label->setVisible(false);
this->s = s;
connect(ui->connectButton,SIGNAL(clicked()),this,SLOT(connectToChat()));
connect(ui->cancelButton,SIGNAL(clicked()),this,SLOT(cancelLogin()));
|
|
16
17
|
}
|
|
18
19
20
21
22
23
|
bool LoginScreen::validateURL(QString& URL)
{
QRegExp regex("([a-zA-Z.]+|((\\d{1,3}\\.){3}\\d{1,3})):\\d+");
return regex.exactMatch(URL);
}
|
|
24
|
bool LoginScreen::parseURL(QString& URL)
|
|
25
|
{
|
|
26
27
|
if(validateURL(URL))
{
|
|
28
|
ui->label->setVisible(false);
|
|
29
|
QStringList strings = URL.split(':');
|
|
30
|
host = strings[0];
|
|
31
|
port = strings[1].toInt();
|
|
32
33
34
35
36
37
|
return true;
}
else
{
ui->label->setVisible(true);
return false;
|
|
38
|
}
|
|
39
40
|
}
|
|
41
|
void LoginScreen::connectToChat()
|
|
42
|
{
|
|
43
|
QString nick;
|
|
44
|
string response;
|
|
45
46
|
QString URL = ui->serverURLEdit->text();
if(parseURL(URL))
|
|
47
|
{
|
|
48
49
|
nick = ui->nickEdit->text();
try
|
|
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
|
s->Create();
s->Connect(host.toStdString(),port);
(*s) << nick.toStdString();
(*s) >> response;
if(response == "CHATNICKINVALID")
{
ui->label->setText("Spaces not allowed in nicknames, please enter another nickname");
}
else if(response == "CHATNICKEXIST")
{
ui->label->setText("Nickname in use, please enter another nickname");
}
else if(response == "CHATFULL")
{
ui->label->setText("Chatroom is full, please wait...");
}
else if(response != "CHATOK")
{
ui->label->setText("Error: " + QString::fromStdString(response));
}
else
{
((Chatroom*)this->parent())->setNickname(nick);
this->accept();
return;
}
ui->label->setVisible(true);
|
|
78
|
}
|
|
79
|
catch(SocketException& e)
|
|
80
|
{
|
|
81
82
83
84
|
cout << e.description() << endl;
QString exception = QString::fromStdString(e.description());
ui->label->setText(exception);
ui->label->setVisible(true);
|
|
85
86
|
}
}
|
|
87
88
|
}
|
|
89
90
91
92
93
|
void LoginScreen::cancelLogin()
{
this->reject();
}
|
|
94
95
96
97
|
LoginScreen::~LoginScreen()
{
delete ui;
}
|