summaryrefslogtreecommitdiffstats
path: root/src/core/pvsChatClient.cpp
blob: 6717e348985ee95eaf4c413de47b3f9aaa2b14cf (plain) (blame)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
# 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();
}