KDEGames

kgamechat.cpp
1 /*
2  This file is part of the KDE games library
3  Copyright (C) 2001-2002 Andreas Beckermann ([email protected])
4  Copyright (C) 2001 Martin Heni (kde at heni-online.de)
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "kgamechat.h"
22 
23 
24 #include "kgame.h"
25 #include "kplayer.h"
26 #include "kgameproperty.h"
27 #include "kgamemessage.h"
28 
29 #include <KLocalizedString>
30 
31 #include <QMap>
32 
33 //FIXME:
34 #define FIRST_ID 2 // first id, that is free of use, aka not defined above
35 
36 class KGameChatPrivate
37 {
38 public:
39  KGameChatPrivate()
40  {
41  mFromPlayer = nullptr;
42  mGame = nullptr;
43 
44  mToMyGroup = -1;
45  }
46 
47  KGame* mGame;
48  KPlayer* mFromPlayer;
49  int mMessageId;
50 
51 
52  QMap<int, int> mSendId2PlayerId;
53  int mToMyGroup; // just as the above - but for the group, not for players
54 };
55 
56 KGameChat::KGameChat(KGame* g, int msgid, QWidget* parent, KChatBaseModel* model, KChatBaseItemDelegate* delegate)
57  : KChatBase(parent, model, delegate),
58  d( new KGameChatPrivate )
59 {
60  init(g, msgid);
61 }
62 
63 KGameChat::KGameChat(KGame* g, int msgid, KPlayer* fromPlayer, QWidget* parent, KChatBaseModel* model, KChatBaseItemDelegate* delegate)
64  : KChatBase(parent,model,delegate),
65  d( new KGameChatPrivate )
66 {
67  init(g, msgid);
68  setFromPlayer(fromPlayer);
69 }
70 
72  : KChatBase(parent),
73  d( new KGameChatPrivate )
74 {
75  init(nullptr, -1);
76 }
77 
78 KGameChat::~KGameChat()
79 {
80  qCDebug(GAMES_PRIVATE_KGAME) ;
81  delete d;
82 }
83 
84 void KGameChat::init(KGame* g, int msgId)
85 {
86  qCDebug(GAMES_PRIVATE_KGAME) ;
87  setMessageId(msgId);
88 
89  setKGame(g);
90 }
91 
92 void KGameChat::addMessage(int fromId, const QString& text)
93 {
94  if (!d->mGame) {
95  qCWarning(GAMES_PRIVATE_KGAME) << "no KGame object has been set";
96  addMessage(i18n("Player %1", fromId), text);
97  } else {
98  KPlayer* p = d->mGame->findPlayer(fromId);
99  if (p) {
100  qCDebug(GAMES_PRIVATE_KGAME) << "adding message of player" << p->name() << "id=" << fromId;
101  addMessage(p->name(), text);
102  } else {
103  qCWarning(GAMES_PRIVATE_KGAME) << "Could not find player id" << fromId;
104  addMessage(i18nc("Unknown player", "Unknown"), text);
105  }
106  }
107 }
108 
110 {
111  if (!d->mFromPlayer) {
112  qCWarning(GAMES_PRIVATE_KGAME) << ": You must set a player first!";
113  return;
114  }
115  if (!d->mGame) {
116  qCWarning(GAMES_PRIVATE_KGAME) << ": You must set a game first!";
117  return;
118  }
119 
120  qCDebug(GAMES_PRIVATE_KGAME) << "from:" << d->mFromPlayer->id() << "==" << d->mFromPlayer->name();
121 
122  int id = sendingEntry();
123 
124  if (isToGroupMessage(id)) {
125  // note: there is currently no support for other groups than the players
126  // group! It might be useful to send to other groups, too
127  QString group = d->mFromPlayer->group();
128  qCDebug(GAMES_PRIVATE_KGAME) << "send to group" << group;
129  int sender = d->mFromPlayer->id();
130  d->mGame->sendGroupMessage(text, messageId(), sender, group);
131 
132  //TODO
133  //AB: this message is never received!! we need to connect to
134  //KPlayer::networkData!!!
135  //TODO
136 
137  } else {
138  int toPlayer = 0;
139  if (!isSendToAllMessage(id) && isToPlayerMessage(id)) {
140  toPlayer = playerId(id);
141  if (toPlayer == -1) {
142  qCCritical(GAMES_PRIVATE_KGAME) << ": don't know that player "
143  << "- internal ERROR";
144  }
145  }
146  int receiver = toPlayer;
147  int sender = d->mFromPlayer->id();
148  d->mGame->sendMessage(text, messageId(), receiver, sender);
149  }
150 }
151 
152 void KGameChat::setMessageId(int msgid)
153 { d->mMessageId = msgid; }
154 
156 { return d->mMessageId; }
157 
159 { return (id == KChatBase::SendToAll); }
160 
161 bool KGameChat::isToGroupMessage(int id) const
162 { return (id == d->mToMyGroup); }
163 
165 {
166 return d->mSendId2PlayerId.contains(id); }
167 
169 { return i18n("Send to %1", name); }
170 
171 int KGameChat::playerId(int id) const
172 {
173  if (!isToPlayerMessage(id)) {
174  return -1;
175  }
176 
177  return d->mSendId2PlayerId[id];
178 }
179 
181 {
183  for (it = d->mSendId2PlayerId.begin(); it != d->mSendId2PlayerId.end(); ++it) {
184  if (it.value() == playerId) {
185  return it.key();
186  }
187  }
188  return -1;
189 }
190 
192 { return d->mFromPlayer ? d->mFromPlayer->name() : QString(); }
193 
194 bool KGameChat::hasPlayer(int id) const
195 {
196  return (sendingId(id) != -1);
197 }
198 
200 {
201  if (!p) {
202  qCCritical(GAMES_PRIVATE_KGAME) << ": NULL player";
203  removeSendingEntry(d->mToMyGroup);
204  d->mFromPlayer = nullptr;
205  return;
206  }
207  if (d->mFromPlayer) {
208  changeSendingEntry(p->group(), d->mToMyGroup);
209  } else {
210  if (d->mToMyGroup != -1) {
211  qCWarning(GAMES_PRIVATE_KGAME) << "send to my group exists already - removing";
212  removeSendingEntry(d->mToMyGroup);
213  }
214  d->mToMyGroup = nextId();
215  addSendingEntry(i18n("Send to My Group (\"%1\")", p->group()), d->mToMyGroup);
216  }
217  d->mFromPlayer = p;
218  qCDebug(GAMES_PRIVATE_KGAME) << "player=" << p;
219 }
220 
221 
223 {
224  if (d->mGame) {
225  slotUnsetKGame();
226  }
227  qCDebug(GAMES_PRIVATE_KGAME) << "game=" << g;
228  d->mGame = g;
229 
230  if (d->mGame) {
231  connect(d->mGame, &KGame::signalPlayerJoinedGame, this, &KGameChat::slotAddPlayer);
232  connect(d->mGame, &KGame::signalPlayerLeftGame, this, &KGameChat::slotRemovePlayer);
233  connect(d->mGame, &KGame::signalNetworkData, this, &KGameChat::slotReceiveMessage);
235 
236  QList<KPlayer*> playerList = *d->mGame->playerList();
237  for (int i = 0; i < playerList.count(); i++) {
238  slotAddPlayer(playerList.at(i));
239  }
240  }
241 }
242 
243 KGame* KGameChat::game() const
244 {
245  return d->mGame;
246 }
247 
248 KPlayer* KGameChat::fromPlayer() const
249 {
250  return d->mFromPlayer;
251 }
252 
254 {
255 //TODO: test this method!
256 
257  if (!d->mGame) {
258  return;
259  }
260  disconnect(d->mGame, nullptr, this, nullptr);
261  removeSendingEntry(d->mToMyGroup);
263  for (it = d->mSendId2PlayerId.begin(); it != d->mSendId2PlayerId.end(); ++it) {
265  }
266 }
267 
268 void KGameChat::slotAddPlayer(KPlayer* p)
269 {
270  if (!p) {
271  qCCritical(GAMES_PRIVATE_KGAME) << ": cannot add NULL player";
272  return;
273  }
274  if (hasPlayer(p->id())) {
275  qCCritical(GAMES_PRIVATE_KGAME) << ": player was added before";
276  return;
277  }
278 
279  int sendingId = nextId();
280  addSendingEntry(comboBoxItem(p->name()), sendingId);
281  d->mSendId2PlayerId.insert(sendingId, p->id());
282  connect(p, &KPlayer::signalPropertyChanged, this, &KGameChat::slotPropertyChanged);
284 }
285 
286 void KGameChat::slotRemovePlayer(KPlayer* p)
287 {
288  if (!p) {
289  qCCritical(GAMES_PRIVATE_KGAME) << ": NULL player";
290  return;
291  }
292  if (!hasPlayer(p->id())) {
293  qCCritical(GAMES_PRIVATE_KGAME) << ": cannot remove non-existent player";
294  return;
295  }
296 
297  int id = sendingId(p->id());
298  removeSendingEntry(id);
299  p->disconnect(this);
300  d->mSendId2PlayerId.remove(id);
301 }
302 
303 void KGameChat::slotPropertyChanged(KGamePropertyBase* prop, KPlayer* player)
304 {
305  if (prop->id() == KGamePropertyBase::IdName) {
306 // qCDebug(GAMES_PRIVATE_KGAME) << "new Name";
307  changeSendingEntry(player->name(), sendingId(player->id()));
308 /*
309  mCombo->changeItem(comboBoxItem(player->name()), index);
310  */
311  } else if (prop->id() == KGamePropertyBase::IdGroup) {
312  //TODO
313  }
314 }
315 
316 void KGameChat::slotReceivePrivateMessage(int msgid, const QByteArray& buffer, quint32 sender, KPlayer* me)
317 {
318  if (!me || me != fromPlayer()) {
319  qCDebug(GAMES_PRIVATE_KGAME) << "nope - not for us!";
320  return;
321  }
322  slotReceiveMessage(msgid, buffer, me->id(), sender);
323 }
324 
325 void KGameChat::slotReceiveMessage(int msgid, const QByteArray& buffer, quint32 , quint32 sender)
326 {
327  QDataStream msg(buffer);
328  if (msgid != messageId()) {
329  return;
330  }
331 
332  QString text;
333  msg >> text;
334 
335  addMessage(sender, text);
336 }
337 
virtual QString sendToPlayerEntry(const QString &name) const
Definition: kgamechat.cpp:168
KGameChat(KGame *game, int msgid, KPlayer *fromPlayer, QWidget *parent, KChatBaseModel *model=nullptr, KChatBaseItemDelegate *delegate=nullptr)
Construct a KGame chat widget on game that used msgid for the chat message.
Definition: kgamechat.cpp:63
void signalNetworkData(int msgid, const QByteArray &buffer, quint32 sender, KPlayer *me)
The player object got a message which was targeted at it but has no default method to process it...
quint32 id() const
Returns the id of the player.
Definition: kplayer.cpp:246
void changeSendingEntry(const QString &text, int id)
This changes a combo box entry.
Definition: kchatbase.cpp:208
void setKGame(KGame *g)
Set the KGame object for this chat widget.
Definition: kgamechat.cpp:222
bool isToGroupMessage(int id) const
Used to indicate whether a message shall be sent to a group of players.
Definition: kgamechat.cpp:161
int messageId() const
Definition: kgamechat.cpp:155
bool addSendingEntry(const QString &text, int id)
Adds a new entry in the combo box.
Definition: kchatbase.cpp:150
QObject * sender() const const
void removeSendingEntry(int id)
Removes the entry with the ID id from the combo box.
Definition: kchatbase.cpp:196
bool isToPlayerMessage(int id) const
Used to indicate whether the message shall be sent to a single player only.
Definition: kgamechat.cpp:164
const T & at(int i) const const
The base class for chat widgets.
Definition: kchatbase.h:75
void slotUnsetKGame()
Unsets a KGame object that has been set using setKGame before.
Definition: kgamechat.cpp:253
A delegate (see the Qt Model/View module for details) to paint the lines of the KChatBase list model ...
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
QString fromName() const override
reimplemented from KChatBase
Definition: kgamechat.cpp:191
void signalPropertyChanged(KGamePropertyBase *property, KPlayer *me)
This signal is emitted if a player property changes its value and the property is set to notify this ...
void setFromPlayer(KPlayer *player)
This sets the fromPlayer to player.
Definition: kgamechat.cpp:199
int sendingId(int playerId) const
Definition: kgamechat.cpp:180
void returnPressed(const QString &text) override
This is called whenever the user pushed return ie wants to send a message.
Definition: kgamechat.cpp:109
int count(const T &value) const const
virtual const QString & name() const
Definition: kplayer.cpp:243
QString i18nc(const char *context, const char *text, const TYPE &arg...)
virtual const QString & group() const
Query the group the player belongs to.
Definition: kplayer.cpp:237
void signalPlayerJoinedGame(KPlayer *player)
a player joined the game
int nextId() const
Definition: kchatbase.cpp:232
Base class for a game player.
Definition: kplayer.h:69
void slotReceivePrivateMessage(int msgid, const QByteArray &buffer, quint32 sender, KPlayer *me)
Called when KPlayer::signalNetworkData is emitted.
Definition: kgamechat.cpp:316
Base class of KGameProperty.
Definition: kgameproperty.h:45
void signalNetworkData(int msgid, const QByteArray &buffer, quint32 receiver, quint32 sender)
We got an user defined update message.
void signalPlayerLeftGame(KPlayer *player)
a player left the game because of a broken connection or so!
const Key key(const T &value, const Key &defaultKey) const const
QString i18n(const char *text, const TYPE &arg...)
bool hasPlayer(int id) const
Definition: kgamechat.cpp:194
void setMessageId(int msgid)
Change the message id of the chat widget.
Definition: kgamechat.cpp:152
int playerId(int id) const
Definition: kgamechat.cpp:171
The main KDE game object.
Definition: kgame.h:64
int sendingEntry() const
Definition: kchatbase.cpp:182
bool isSendToAllMessage(int id) const
Definition: kgamechat.cpp:158
virtual QString comboBoxItem(const QString &name) const
Replace to customize the combo box.
Definition: kchatbase.cpp:254
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
void destroyed(QObject *obj)
int id() const
The model used to store messages displayed in the chat dialog messages list.
const T value(const Key &key, const T &defaultValue) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Nov 30 2020 22:37:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.