loginscreen.cpp 1.62 KB
#include "loginscreen.h"
#include "ui_loginscreen.h"

LoginScreen::LoginScreen(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginScreen)
{
    ui->setupUi(this);
    ui->nickEdit->setPlaceholderText("Nickname");
    ui->serverURLEdit->setPlaceholderText("Server hostname:port");
}

void LoginScreen::parseURL(QString host, int port, QString URL)
{
    if(validateURL(URL))
    {
        QStringList strings = URL.split(':');
        host = strings[0].toStdString();
        port = strings[1].toInt();
    }
}

bool LoginScreen::validateURL(QString& url)
{

}

bool LoginScreen::connect(Socket& s)
{
    QString host, nick;
    string response;
    int port;
    parseURL(host,port,ui->serverURLEdit->text());
    nick = ui->nickEdit->text();
    try
    {
        s.Create();
        s.Connect(host.toStdString(),port);
        s << nick.toStdString();
        s >> response;
        if(response == "CHATNICKINVALID")
        {
            cout << "Spaces not allowed in nicknames, please enter another nickname: ";
        }
        else if(response == "CHATNICKEXIST")
        {
            cout << "Nickname in use, please enter another nickname: ";
        }
        else if(response == "CHATFULL")
        {
            cout << "Chatroom is full, please wait..." << endl;
        }
        else if(response != "CHATOK")
        {
            cout << "Error: " << response << endl;
        }
        else
        {
            return true;
        }
        s.Close();
        return false;
    }
    catch(SocketException& e)
    {
        cout << e.description() << endl;
        exit(-1);
    }
}

LoginScreen::~LoginScreen()
{
    delete ui;
}