summaryrefslogtreecommitdiffstats
path: root/libkdegames/kchat.cpp
blob: d4ffd7ec61fe8a10800163b2dccf12fc35b85aeb (plain)
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
/*
    This file is part of the KDE games library
    Copyright (C) 2001 Andreas Beckermann ([email protected])

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License version 2 as published by the Free Software Foundation.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include <klocale.h>
#include <kdebug.h>

#include "kchat.h"

class KChatPrivate
{
public:
	KChatPrivate()
	{
	}

	bool mAutoAddMessages;

	QMap<int, QString> mPlayerMap;
	int mPlayerId;
	int mFromId;
};

KChat::KChat(QWidget* parent, bool twoPlayerGame) : KChatBase(parent, twoPlayerGame)
{
 init(); 
}

KChat::~KChat()
{
 kdDebug(11000) << "DESTRUCT KChat " << this << endl;
 delete d;
}

void KChat::init()
{
 kdDebug(11001) << "INIT KChat " << this << endl;
 d = new KChatPrivate;
 d->mAutoAddMessages = true;
 d->mPlayerId = 1;
 d->mFromId = 1;
}

void KChat::setFromNickname(const QString& n)
{ d->mFromId = addPlayer(n); }
const QString& KChat::fromName() const
{ return player(fromId()); }
void KChat::setAutoAddMessages(bool add) 
{ d->mAutoAddMessages = add; }
bool KChat::autoAddMessages() const 
{ return d->mAutoAddMessages; }
int KChat::uniqueId()
{ return d->mPlayerId++; }
int KChat::fromId() const
{ return d->mFromId; }
const QString& KChat::player(int id) const
{ return d->mPlayerMap[id]; }

void KChat::returnPressed(const QString& text)
{
 int id = fromId();
 if (id < 0) {
	// don't return - just display "unknown" as name
	kdWarning(11000) << "KChat: no fromNickname has been set!" << endl;
 }
 emit signalSendMessage(id, text);
 if (autoAddMessages()) {
	QString p = player(id);
	if (p.isNull()) {
		p = i18n("Unknown");
	}
	kdDebug(11000) << "auto adding message from player " << p << " ;id=" << id << endl;
	addMessage(p, text);
 }
}

int KChat::addPlayer(const QString& nickname)
{
 int id = uniqueId();
 d->mPlayerMap.insert(id, nickname);
 return id;
}

void KChat::removePlayer(int id)
{
 d->mPlayerMap.remove(id);
}

void KChat::removePlayer(const QString& nickname)
{
 QMap<int, QString>::Iterator it;
 for (it = d->mPlayerMap.begin(); it != d->mPlayerMap.end(); ++it) {
	if (it.data() == nickname) {
		d->mPlayerMap.remove(it);
	}
 }
}


#include "kchat.moc"