SVN_SILENT made messages (.desktop file)
[kdegames.git] / libkdegames / kchat.cpp
blob6c2a625f685725bcd55062b6d4514b642cfbd483
1 /*
2 This file is part of the KDE games library
3 Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de)
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "kchat.h"
22 #include <QtCore/QMap>
24 #include <klocale.h>
25 #include <kdebug.h>
27 #include "kchatbasemodel.h"
28 #include "kchatbaseitemdelegate.h"
30 class KChatPrivate
32 public:
33 KChatPrivate()
37 bool mAutoAddMessages;
39 QMap<int, QString> mPlayerMap;
40 int mPlayerId;
41 int mFromId;
44 KChat::KChat(QWidget* parent, bool twoPlayerGame)
45 : KChatBase(parent,
46 new KChatBaseModel(parent),
47 new KChatBaseItemDelegate(parent),twoPlayerGame),
48 d( new KChatPrivate )
50 init();
53 KChat::KChat(QWidget* parent, KChatBaseModel* model, KChatBaseItemDelegate* delegate, bool noComboBox)
54 : KChatBase(parent, model, delegate, noComboBox),
55 d( new KChatPrivate )
57 init();
60 KChat::~KChat()
62 kDebug(11000) << "DESTRUCT KChat" << this;
63 delete d;
66 void KChat::init()
68 kDebug(11001) << "INIT KChat" << this;
69 d->mAutoAddMessages = true;
70 d->mPlayerId = 1;
71 d->mFromId = 1;
74 void KChat::setFromNickname(const QString& n)
75 { d->mFromId = addPlayer(n); }
76 QString KChat::fromName() const
77 { return player(fromId()); }
78 void KChat::setAutoAddMessages(bool add)
79 { d->mAutoAddMessages = add; }
80 bool KChat::autoAddMessages() const
81 { return d->mAutoAddMessages; }
82 int KChat::uniqueId()
83 { return d->mPlayerId++; }
84 int KChat::fromId() const
85 { return d->mFromId; }
86 QString KChat::player(int id) const
87 { return d->mPlayerMap[id]; }
89 void KChat::returnPressed(const QString& text)
91 int id = fromId();
92 if (id < 0) {
93 // don't return - just display "unknown" as name
94 kWarning(11000) << "KChat: no fromNickname has been set!";
96 emit signalSendMessage(id, text);
97 if (autoAddMessages()) {
98 QString p = player(id);
99 if (p.isNull()) {
100 p = i18nc("Unknown player", "Unknown");
102 kDebug(11000) << "auto adding message from player" << p << " ;id=" << id;
103 addMessage(p, text);
107 int KChat::addPlayer(const QString& nickname)
109 int id = uniqueId();
110 d->mPlayerMap.insert(id, nickname);
111 return id;
114 void KChat::removePlayer(int id)
116 d->mPlayerMap.remove(id);
119 void KChat::removePlayer(const QString& nickname)
121 QMap<int, QString>::Iterator it;
122 for (it = d->mPlayerMap.begin(); it != d->mPlayerMap.end(); ++it) {
123 if (it.value() == nickname) {
124 d->mPlayerMap.erase(it);
130 #include "kchat.moc"