kchat.cpp
00001 /* 00002 This file is part of the KDE games library 00003 Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de) 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License version 2 as published by the Free Software Foundation. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include <klocale.h> 00021 #include <kdebug.h> 00022 00023 #include "kchat.h" 00024 00025 class KChatPrivate 00026 { 00027 public: 00028 KChatPrivate() 00029 { 00030 } 00031 00032 bool mAutoAddMessages; 00033 00034 TQMap<int, TQString> mPlayerMap; 00035 int mPlayerId; 00036 int mFromId; 00037 }; 00038 00039 KChat::KChat(TQWidget* parent, bool twoPlayerGame) : KChatBase(parent, twoPlayerGame) 00040 { 00041 init(); 00042 } 00043 00044 KChat::~KChat() 00045 { 00046 kdDebug(11000) << "DESTRUCT KChat " << this << endl; 00047 delete d; 00048 } 00049 00050 void KChat::init() 00051 { 00052 kdDebug(11001) << "INIT KChat " << this << endl; 00053 d = new KChatPrivate; 00054 d->mAutoAddMessages = true; 00055 d->mPlayerId = 1; 00056 d->mFromId = 1; 00057 } 00058 00059 void KChat::setFromNickname(const TQString& n) 00060 { d->mFromId = addPlayer(n); } 00061 const TQString& KChat::fromName() const 00062 { return player(fromId()); } 00063 void KChat::setAutoAddMessages(bool add) 00064 { d->mAutoAddMessages = add; } 00065 bool KChat::autoAddMessages() const 00066 { return d->mAutoAddMessages; } 00067 int KChat::uniqueId() 00068 { return d->mPlayerId++; } 00069 int KChat::fromId() const 00070 { return d->mFromId; } 00071 const TQString& KChat::player(int id) const 00072 { return d->mPlayerMap[id]; } 00073 00074 void KChat::returnPressed(const TQString& text) 00075 { 00076 int id = fromId(); 00077 if (id < 0) { 00078 // don't return - just display "unknown" as name 00079 kdWarning(11000) << "KChat: no fromNickname has been set!" << endl; 00080 } 00081 emit signalSendMessage(id, text); 00082 if (autoAddMessages()) { 00083 TQString p = player(id); 00084 if (p.isNull()) { 00085 p = i18n("Unknown"); 00086 } 00087 kdDebug(11000) << "auto adding message from player " << p << " ;id=" << id << endl; 00088 addMessage(p, text); 00089 } 00090 } 00091 00092 int KChat::addPlayer(const TQString& nickname) 00093 { 00094 int id = uniqueId(); 00095 d->mPlayerMap.insert(id, nickname); 00096 return id; 00097 } 00098 00099 void KChat::removePlayer(int id) 00100 { 00101 d->mPlayerMap.remove(id); 00102 } 00103 00104 void KChat::removePlayer(const TQString& nickname) 00105 { 00106 TQMap<int, TQString>::Iterator it; 00107 for (it = d->mPlayerMap.begin(); it != d->mPlayerMap.end(); ++it) { 00108 if (it.data() == nickname) { 00109 d->mPlayerMap.remove(it); 00110 } 00111 } 00112 } 00113 00114 00115 #include "kchat.moc"