summaryrefslogblamecommitdiffstats
path: root/src/core/pvsChatClient.cpp
blob: 6717e348985ee95eaf4c413de47b3f9aaa2b14cf (plain) (tree)






















































































































































                                                                                                          
/*
# Copyright (c) 2009,2010 - OpenSLX Project, Computer Center University of
# Freiburg
#
# This program is free software distributed under the GPL version 2.
# See http://openslx.org/COPYING
#
# If you have any feedback please consult http://openslx.org/feedback and
# send your suggestions, praise, or complaints to feedback@openslx.org
#
# General information about OpenSLX can be found at http://openslx.org/
# -----------------------------------------------------------------------------
# pvsChatClient.cpp
#    - Methods of the chat service.
# -----------------------------------------------------------------------------
*/

#include "pvsChatClient.h"

using namespace std;

// Constructor
PVSChatClient::PVSChatClient()
{
    _chatServerConnection = NULL;
    _source = getFullUsername();
    _registered = true;
}

PVSChatClient::~PVSChatClient()
{

}

// Get Chat login name.
QString PVSChatClient::getSource()
{
    return _source;
}

// Set (from server) assigned username.
void PVSChatClient::setSource(QString name)
{
    _source = name;
    _registered = true;
}

bool PVSChatClient::isRegistered()
{
    return _registered;
}

// Set PVSServerConnection
void PVSChatClient::setServerConnection(PVSServerConnection* sc)
{
    if (sc)
    {
        _chatServerConnection = sc;
        _chatServerConnection->addChatHandler("*", this, &PVSChatClient::receive);
    }
}

// Send a message to Server.
void PVSChatClient::send(QString tar, QString msg)
{
    // Append username to msg.
    QString username = _source;
    username.append(":");
    msg = username.append(msg) ;

    // Create a Msg.
    PVSMsg myMsg(PVSMESSAGE, tar, msg, 0);

    // Send the message.
    if (_chatServerConnection)
        _chatServerConnection->sendMessage(myMsg);
    else
        ConsoleLog writeError("[CHAT] Chat get no server connection"); // no ServerConnection

}

// Handle messages from server.
void PVSChatClient::receive(PVSMsg recv)
{
    PVSChatMsg chatMsg(recv);

    if(chatMsg.isCommand()) // command message for update of the client list.
    {
        _chatCommandDispatcher.fire(chatMsg);
    }
    else // conversation message.
    {
    	if(chatMsg.getMsg().size())
    		_chatMessageDispatcher.fire(chatMsg);
    }
}

// Return the current list of logged Clients.
QStringList PVSChatClient::getClients()
{
    return _clients;
}

// Return the ip of the username.
QString PVSChatClient::getIPFromUsername(QString username)
{
	if(_clientsData.contains(username))
	{
		return _clientsData.value(username);
	}
	else
	{
		ConsoleLog writeError("[CHAT] Wanted key was not found in clients hash table.");
		return QString("ip not found");
	}
}

// Add the name and ip of a new client.
void PVSChatClient::addClient(QString name, QString ip)
{
	if(!_clients.contains(name))
	{
		_clients.append(name);
		_clientsData[name] = ip;
	}
	else
	{
		ConsoleLog writeError("[CHAT] The new client wasn't added, because exists allready.");
	}
}

// Remove the name and ip of a removed client.
void PVSChatClient::removeClient(QString name)
{
	if(_clients.contains(name))
	{
		_clients.removeOne(name);
		_clientsData.remove(name);
	}
	else
	{
		ConsoleLog writeError("[CHAT] The new client can not be removed, because doesn't exist.");
	}
}

// Delete all infomation about client.
void PVSChatClient::clearClients()
{
    _clients.clear();
    _clientsData.clear();
}