kchat.cpp

Go to the documentation of this file.
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 "kchat.h"
00021 
00022 #include <QtCore/QMap>
00023 
00024 #include <klocale.h>
00025 #include <kdebug.h>
00026 
00027 #include "kchatbasemodel.h"
00028 #include "kchatbaseitemdelegate.h"
00029 
00030 class KChatPrivate
00031 {
00032 public:
00033         KChatPrivate()
00034         {
00035         }
00036 
00037         bool mAutoAddMessages;
00038 
00039         QMap<int, QString> mPlayerMap;
00040         int mPlayerId;
00041         int mFromId;
00042 };
00043 
00044 KChat::KChat(QWidget* parent, bool twoPlayerGame) 
00045     : KChatBase(parent, 
00046                 new KChatBaseModel(parent),
00047                 new KChatBaseItemDelegate(parent),twoPlayerGame),
00048       d( new KChatPrivate )
00049 {
00050  init(); 
00051 }
00052 
00053 KChat::~KChat()
00054 {
00055  kDebug(11000) << "DESTRUCT KChat" << this;
00056  delete d;
00057 }
00058 
00059 void KChat::init()
00060 {
00061  kDebug(11001) << "INIT KChat" << this;
00062  d->mAutoAddMessages = true;
00063  d->mPlayerId = 1;
00064  d->mFromId = 1;
00065 }
00066 
00067 void KChat::setFromNickname(const QString& n)
00068 { d->mFromId = addPlayer(n); }
00069 QString KChat::fromName() const
00070 { return player(fromId()); }
00071 void KChat::setAutoAddMessages(bool add) 
00072 { d->mAutoAddMessages = add; }
00073 bool KChat::autoAddMessages() const 
00074 { return d->mAutoAddMessages; }
00075 int KChat::uniqueId()
00076 { return d->mPlayerId++; }
00077 int KChat::fromId() const
00078 { return d->mFromId; }
00079 QString KChat::player(int id) const
00080 { return d->mPlayerMap[id]; }
00081 
00082 void KChat::returnPressed(const QString& text)
00083 {
00084  int id = fromId();
00085  if (id < 0) {
00086         // don't return - just display "unknown" as name
00087         kWarning(11000) << "KChat: no fromNickname has been set!";
00088  }
00089  emit signalSendMessage(id, text);
00090  if (autoAddMessages()) {
00091         QString p = player(id);
00092         if (p.isNull()) {
00093                 p = i18nc("Unknown player", "Unknown");
00094         }
00095         kDebug(11000) << "auto adding message from player" << p << " ;id=" << id;
00096         addMessage(p, text);
00097  }
00098 }
00099 
00100 int KChat::addPlayer(const QString& nickname)
00101 {
00102  int id = uniqueId();
00103  d->mPlayerMap.insert(id, nickname);
00104  return id;
00105 }
00106 
00107 void KChat::removePlayer(int id)
00108 {
00109  d->mPlayerMap.remove(id);
00110 }
00111 
00112 void KChat::removePlayer(const QString& nickname)
00113 {
00114  QMap<int, QString>::Iterator it;
00115  for (it = d->mPlayerMap.begin(); it != d->mPlayerMap.end(); ++it) {
00116         if (it.value() == nickname) {
00117                 d->mPlayerMap.erase(it);
00118         }
00119  }
00120 }
00121 
00122 
00123 #include "kchat.moc"

Generated on Sun Mar 16 08:02:52 2008 for Libkdegames by  doxygen 1.5.3