1 /*
2     This file is part of the KDE games library
3     SPDX-FileCopyrightText: 2001-2002 Andreas Beckermann <b_mann@gmx.de>
4     SPDX-FileCopyrightText: 2001 Martin Heni <kde at heni-online.de>
5 
6     SPDX-License-Identifier: LGPL-2.0-only
7 */
8 
9 #ifndef __KGAMECHAT_H__
10 #define __KGAMECHAT_H__
11 
12 // own
13 #include "../kchatbase.h"
14 #include "libkdegamesprivate_export.h"
15 // Qt
16 #include <QString>
17 
18 class KPlayer;
19 class KGame;
20 class KGamePropertyBase;
21 
22 class KGameChatPrivate;
23 
24 /**
25  * \class KGameChat kgamechat.h <KGame/KGameChat>
26  *
27  * @short A Chat widget for KGame-based games
28  *
29  * Call @ref setFromPlayer() first - this will be used as the "from" part of
30  * every message you will send. Otherwise it won't work! You can also use the
31  * fromPlayer parameter in the constructor though...
32  *
33  * @author Andreas Beckermann <b_mann@gmx.de>
34  **/
35 class KDEGAMESPRIVATE_EXPORT KGameChat : public KChatBase
36 {
37 	Q_OBJECT
38 public:
39 	/**
40 	 * Construct a @ref KGame chat widget on @p game that used @p msgid for
41 	 * the chat message. The @p fromPlayer is the local player (see @ref
42 	 * setFromPlayer).
43 	 **/
44 	KGameChat(KGame* game, int msgid, KPlayer* fromPlayer, QWidget * parent, KChatBaseModel* model=nullptr, KChatBaseItemDelegate* delegate=nullptr);
45 
46 	/**
47 	 * @overload
48 	 * To make use of this widget you need to call @ref setFromPlayer
49 	 * manually.
50 	 **/
51 	KGameChat(KGame* game, int msgId, QWidget* parent, KChatBaseModel* model=nullptr, KChatBaseItemDelegate* delegate=nullptr);
52 
53 	/**
54 	 * @overload
55 	 * This constructs a widget that is not usable. You must call at least
56 	 * setGame, setFromPlayer and setMessageId manually.
57 	 **/
58 	explicit KGameChat(QWidget* parent);
59 
60 	~KGameChat() override;
61 
62 	enum SendingIds {
63 		SendToGroup = 1
64 	};
65 
66 	/**
67 	 * This sets the fromPlayer to @p player. The fromPlayer is the
68 	 * player that will appear as "from" when you send messages through this
69 	 * widget.
70 	 * @param player The player of this widget
71 	 **/
72 	void setFromPlayer(KPlayer* player);
73 
74 	KPlayer* fromPlayer() const;
75 
76 	/**
77 	 * Set the @ref KGame object for this chat widget. All messages will be
78 	 * sent through this object. You don't have to implement any send
79 	 * functions, just call this function, call @ref setFromPlayer and be
80 	 * done :-)
81 	 * @param g The @ref KGame object the messages will be sent through
82 	 **/
83 	void setKGame(KGame* g);
84 
85 	KGame* game() const;
86 
87 	/**
88 	 * @return The id of the messages produced by KGameChat. The id will be
89 	 * used in @ref KGame as parameter msgid in the method @ref KGame::sendMessage
90 	 **/
91 	int messageId() const;
92 
93 	/**
94 	 * Change the message id of the chat widget. It is recommended that you
95 	 * don't use this but prefer the constructor instead, but in certain
96 	 * situations (such as using this widget in Qt designer) it may be
97 	 * useful to change the message id.
98 	 *
99 	 * See also @ref messageId
100 	 **/
101 	void setMessageId(int msgid);
102 
103 	/**
104 	 * reimplemented from @ref KChatBase
105 	 * @return @ref KPlayer::name() for the player set by @ref setFromPlayer
106 	 **/
107 	QString fromName() const override;
108 
109 
110 public Q_SLOTS:
addMessage(const QString & fromName,const QString & text)111 	void addMessage(const QString& fromName, const QString& text) override { KChatBase::addMessage(fromName, text);}
112 	virtual void addMessage(int fromId, const QString& text);
113 
114 	void slotReceiveMessage(int, const QByteArray&, quint32 receiver, quint32 sender);
115 
116 protected:
117 	/**
118 	 * @param id The ID of the sending entry, as returned by @ref KChatBase sendingEntry
119 	 * @return True if the entry "send to all" was selected, otherwise false
120 	 **/
121 	bool isSendToAllMessage(int id) const;
122 
123 	/**
124 	 * Used to indicate whether a message shall be sent to a group of
125 	 * players. Note that this was not yet implemented when this doc was
126 	 * written so this description might be wrong. (FIXME)
127 	 * @param id The ID of the sending entry, as returned by @ref KChatBase sendingEntry
128 	 * @return True if the message is meant to be sent to a group (see @ref KPlayer::group
129          * ), e.g. if "send to my group" was selected.
130 	 **/
131 	bool isToGroupMessage(int id) const;
132 
133 
134 	/**
135 	 * Used to indicate whether the message shall be sent to a single player
136 	 * only. Note that you can also call @ref isSendToAllMessage and @ref
137 	 * isToGroupMessage - if both return false it must be a player message.
138 	 * This behaviour might be changed later - so don't depend on it.
139 	 *
140 	 * See also toPlayerId
141 	 * @param id The ID of the sending entry, as returned by
142 	 * @ref KChatBase sendingEntry
143 	 * @return True if the message shall be sent to a special player,
144 	 * otherwise false.
145 	 **/
146 	bool isToPlayerMessage(int id) const;
147 
148 	/**
149 	 * @param id The ID of the sending entry, as returned by
150 	 * @ref KChatBase sendingEntry
151 	 * @return The ID of the player (see KPlayer::id) the sending entry
152 	 * belongs to. Note that the parameter id is an id as returned by
153 	 * @ref KChatBase sendingEntry and the id this method returns is a
154 	 * KPlayer ID. If @ref isToPlayerMessage returns false this method
155 	 * returns -1
156 	 **/
157 	int playerId(int id) const;
158 
159 	/**
160 	 * @param playerId The ID of the KPlayer object
161 	 * @return The ID of the sending entry (see KChatBase) or -1 if
162 	 * the player id was not found.
163 	 **/
164 	int sendingId(int playerId) const;
165 
166 	/**
167 	 * @return True if the player with this ID was added before (see
168 	 * slotAddPlayer)
169 	 **/
170 	bool hasPlayer(int id) const;
171 
172 	/**
173 	 * @param name The name of the added player
174 	 * @return A string that will be added as sending entry in @ref
175 	 * KChatBase. By default this is "send to name" where name is the name
176 	 * that you specify. See also KChatBase::addSendingEntry
177 	 **/
178 	virtual QString sendToPlayerEntry(const QString& name) const;
179 
180 
181 protected Q_SLOTS:
182 	/**
183 	 * Unsets a KGame object that has been set using setKGame
184 	 * before. You don't have to call this - this is usually done
185 	 * automatically.
186 	 **/
187 	void slotUnsetKGame();
188 
189 
190 	void slotPropertyChanged(KGamePropertyBase*, KPlayer*);
191 	void slotAddPlayer(KPlayer*);
192 	void slotRemovePlayer(KPlayer*);
193 
194 	/**
195 	 * Called when KPlayer::signalNetworkData is emitted. The message
196 	 * gets forwarded to slotReceiveMessage if @p me equals
197 	 * fromPlayer.
198 	 **/
199 	void slotReceivePrivateMessage(int msgid, const QByteArray& buffer, quint32 sender, KPlayer* me);
200 
201 protected:
202 	void returnPressed(const QString& text) override;
203 
204 private:
205 	void init(KGame* g, int msgid);
206 
207 private:
208 	Q_DECLARE_PRIVATE_D(KChatBase::d, KGameChat)
209 #if KDEGAMESPRIVATE_BUILD_DEPRECATED_SINCE(7, 3)
210 	// Unused, kept for ABI compatibility
211 	const void * __kdegames_d_do_not_use;
212 #endif
213 };
214 
215 #endif
216