chattexteditpart.h 6.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
    chattexteditpart.h - Chat Text Edit Part

    Copyright (c) 2004      by Richard Smith         <kde@metafoo.co.uk>

    Kopete    (c) 2002-2004 by the Kopete developers <kopete-devel@kde.org>

    *************************************************************************
    *                                                                       *
    * This program is free software; you can redistribute it and/or modify  *
    * it under the terms of the GNU General Public License as published by  *
    * the Free Software Foundation; either version 2 of the License, or     *
    * (at your option) any later version.                                   *
    *                                                                       *
    *************************************************************************
*/

#ifndef CHATTEXTEDITPART_H
#define CHATTEXTEDITPART_H
20

21 22
#include <kparts/part.h>
#include <krichtextedit.h>
23
#include <K4AboutData>
24
#include "kopeterichtextwidget.h"
25

R. Harish Navnit's avatar
R. Harish Navnit committed
26 27 28 29
#include <QFont>
#include <QColor>
#include <QFlags>
#include <QStringList>
30

31
// TODO: Use kdelibs export
32 33
#include <kopete_export.h>

34 35
class KTextEdit;
class KConfigGroup;
36
class KCompletion;
37
class QTimer;
38 39 40 41

namespace Sonnet {
	class Highlighter;
}
42 43 44 45 46 47 48

namespace Kopete
{
class Message;
class Contact;
class OnlineStatus;
class ChatSession;
49
class Protocol;
Olivier Goffart's avatar
Olivier Goffart committed
50
class PropertyContainer;
51 52 53 54 55 56 57 58 59 60 61
}

/**
 * @brief An instant message composition part
 * 
 * This class provides an input part suitable for the composition of instant messages.
 * It provides command history, nickname completion and typing notifications. It is
 * also able to determine whether the send button should be enabled.
 * 
 * @author Richard Smith
 */
62
class CHATTEXTEDITPART_EXPORT ChatTextEditPart : public KParts::ReadOnlyPart
63 64 65
{
	Q_OBJECT
public:
Olivier Goffart's avatar
compile  
Olivier Goffart committed
66
	ChatTextEditPart( Kopete::ChatSession *session, QWidget *parent);
67
	ChatTextEditPart(QWidget *parent, QObject*, const QStringList&);
68 69
	~ChatTextEditPart();
	
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
	/**
	 * @brief Get the text in the editor in the given format.
	 * By default if return the text using the most appropriate format.
	 *
	 * @param format A value in Qt::TextFormat enum.
	 *
	 * @return text using the given format
	 */
	QString text( Qt::TextFormat format = Qt::AutoText ) const;
	
	/**
	 * Enable or Disable the automatic spell checking
	 * @param enabled the state that auto spell checking should beee
	 */
	void setCheckSpellingEnabled( bool enabled );
	
	/**
	 * Get the state of auto spell checking
	 * @return true if auto spell checking is turned on, false otherwise
	 */
	bool checkSpellingEnabled() const;
	
92
	static K4AboutData *createAboutData();
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	
	/**
	 * @brief Disable file open, because it's not used by this part.
	 */
	virtual bool openFile() {
	    return false;
	}
	
	/**
	 * @brief Get the inside KTextEdit
	 * @return instance of KTextEdit
	 */
	KopeteRichTextWidget *textEdit();
	
	/**
	* @brief Is rich text is currently enabled
	*/
	bool isRichTextEnabled() const;
	
	
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
	/**
	 * Returns the message currently in the edit area
	 * @return The @ref Kopete::Message object for the message
	 */
	Kopete::Message contents();
	
	/**
	 * Sets the message in the edit field
	 * @param message The message to display
	 */
	void setContents( const Kopete::Message &message );

	/**
	 * Adds text into the edit area. Used when an emoticon is selected.
	 * @param text The text to be inserted
	 */
	void addText( const QString &text );
	
	/**
	 * Can we send messages now?
	 */
	bool canSend();

	/**
	 * Is the user typing right now?
	 */
	bool isTyping();
140 141 142 143 144
	
	void readConfig( KConfigGroup& config );
	void resetConfig( KConfigGroup& config );
	void writeConfig( KConfigGroup& config );

145 146 147
public slots:
	/**
	 * Go up an entry in the message history.
148
	 */	
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	void historyUp();
	
	/**
	 * Go down an entry in the message history.
	 */
	void historyDown();
	
	/**
	 * Try to complete the word under the cursor.
	 */
	void complete();
	
	/**
	 * Sends the text currently entered into the edit area.
	 */
	void sendMessage();
165 166
	
	void checkToolbarEnabled();
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

signals:
	/**
	 * Emitted when a message is sent.
	 * @param message The message sent
	 */
	void messageSent( Kopete::Message &message );

	/**
	 * Emitted every 4 seconds while the user is typing.
	 * @param typing @c true if the user is typing, @c false otherwise
	 */
	void typing( bool typing );

	/**
	 * Our send-button-enabled flag might have changed
	 * @param canSend The return value of @ref canSend().
	 */
	void canSendChanged( bool canSend );
186 187 188 189
	
	void toolbarToggled(bool enabled);
	void richTextChanged();

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
private slots:
	/**
	 * Called when a contact is added to the chat session.
	 * Adds this contact to the nickname completion list.
	 * @param c The contact that joined the chat
	 */
	void slotContactAdded( const Kopete::Contact *c );

	/**
	 * Called when a contact is removed from the chat session.
	 * Removes this contact from the nickname completion list.
	 * @param c The contact left the chat
	 */
	void slotContactRemoved( const Kopete::Contact *c );

	/**
	 * Called when a contact changes status, may emit @ref canSendChanged.
	 * @param contact The contact who changed status
	 * @param status The new status of the contact
	 */
	void slotContactStatusChanged( Kopete::Contact *contact, const Kopete::OnlineStatus &status, const Kopete::OnlineStatus &oldstatus );

	/**
	 * Called when text is changed in the edit area
	 */
	void slotTextChanged();

	/**
	 * User is typing, so emit a @ref typing( @c true ) signal every 4 seconds.
	 * This is stupid. Why not just emit it once?
	 */
	void slotRepeatTypingTimer();
	
	/**
	 * Emits a @ref typing( @c false ) signal 4.5 seconds after the user stops typing.
	 */
	void slotStoppedTypingTimer();
	
	/**
	 * Update completion to follow changes in users' nicknames
	 */
231
	void slotDisplayNameChanged( const QString &oldName, const QString &newName );
232

233 234 235 236 237
	/**
	 * Some appearance settings has changed.
	 */
	void slotAppearanceChanged();

238
	void slotRichTextSupportChanged();
239

240
private:
241 242
	void init( Kopete::ChatSession *session, QWidget *parent);

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	Kopete::ChatSession *m_session;
	
	/**
	 * The history buffer conceptually works like this:
	 * We have a list of messages (historyList), with indices from -1 to n.
	 * historyPos is our current position in this list; historyList[historyPos]
	 * is conceptually the message we are editing. The exception to this is that
	 * index -1 is treated specially; when it is modified, changes are saved to
	 * a new message placed at index 0.
	 */
	QStringList historyList;
	int historyPos;
	
	KCompletion *mComplete;
	QString m_lastMatch;
	
	QTimer *m_typingRepeatTimer;
	QTimer *m_typingStopTimer;
261 262
	
	KopeteRichTextWidget *editor;
263 264 265 266 267
};

#endif

// vim: set noet ts=4 sts=4 sw=4: