loginscreen.cpp
2.55 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
#include "loginscreen.h"
#include "ui_loginscreen.h"
LoginScreen::LoginScreen(Socket* s, QWidget *parent) :
QDialog(parent),
ui(new Ui::LoginScreen)
{
ui->setupUi(this);
ui->nickEdit->setPlaceholderText("Nickname");
ui->serverURLEdit->setPlaceholderText("Server hostname:port");
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()));
}
bool LoginScreen::parseURL(QString& URL)
{
if(validateURL(URL))
{
ui->label->setVisible(false);
QStringList strings = URL.split(':');
host = strings[0];
port = strings[1].toInt();
return true;
}
else
{
ui->label->setVisible(true);
return false;
}
}
bool LoginScreen::validateURL(QString& URL)
{
QRegExp regex("([a-zA-Z.]+|((\\d{1,3}\\.){3}\\d{1,3})):\\d+");
return regex.exactMatch(URL);
}
void LoginScreen::connectToChat()
{
QString nick;
string response;
QString URL = ui->serverURLEdit->text();
if(parseURL(URL))
{
nick = ui->nickEdit->text();
try
{
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);
}
catch(SocketException& e)
{
cout << e.description() << endl;
QString exception = QString::fromStdString(e.description());
ui->label->setText(exception);
ui->label->setVisible(true);
}
}
}
void LoginScreen::cancelLogin()
{
this->reject();
}
LoginScreen::~LoginScreen()
{
delete ui;
}