#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; }