summaryrefslogtreecommitdiffstats
blob: a70381803df126f7af31f757b1ad8d144ad8d5c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
231
232
233
234
/*
 * Copyright (c) 2010-2012 Kevin Smith
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#pragma once

#include <Swift/Controllers/UIInterfaces/ChatWindow.h>
#include <Swift/QtUI/QtMUCConfigurationWindow.h>
#include <Swift/QtUI/QtAffiliationEditor.h>
#include <Swift/QtUI/QtSwiftUtil.h>

#include <QtTabbable.h>

#include <SwifTools/LastLineTracker.h>

#include <map>
#include <QPointer>
#include <QTextCursor>
#include <QMap>

class QTextEdit;
class QLineEdit;
class QComboBox;
class QLabel;
class QSplitter;
class QPushButton;

namespace Swift {
	class QtChatView;
	class QtOccupantListWidget;
	class QtChatTheme;
	class TreeWidget;
	class QtTextEdit;
	class UIEventStream;
	class QtChatWindowJSBridge;
	class SettingsProvider;

	class LabelModel : public QAbstractListModel {
		Q_OBJECT
		public:
			LabelModel(QObject* parent = NULL) : QAbstractListModel(parent) {}

			virtual int rowCount(const QModelIndex& /*index*/) const {
				return static_cast<int>(availableLabels_.size());
			}

			virtual QVariant data(const QModelIndex& index, int role) const {
				if (!index.isValid()) {
					return QVariant();
				}
				SecurityLabel::ref label = availableLabels_[index.row()].getLabel();
				if (label && role == Qt::TextColorRole) {
					return P2QSTRING(label->getForegroundColor());
				}
				if (label && role == Qt::TextColorRole) {
					return P2QSTRING(label->getBackgroundColor());
				}
				if (role == Qt::DisplayRole) {
					std::string selector = availableLabels_[index.row()].getSelector();
					std::string displayMarking = label ? label->getDisplayMarking() : "";
					QString labelName = selector.empty() ? displayMarking.c_str() : selector.c_str();
					return labelName;
				}
				return QVariant();
			}

			std::vector<SecurityLabelsCatalog::Item> availableLabels_;
	};

	class QtChatWindow : public QtTabbable, public ChatWindow {
		Q_OBJECT

		public:
			static const QString ButtonFileTransferCancel;
			static const QString ButtonFileTransferSetDescription;
			static const QString ButtonFileTransferSendRequest;
			static const QString ButtonFileTransferAcceptRequest;
			static const QString ButtonMUCInvite;

		public:
			QtChatWindow(const QString &contact, QtChatTheme* theme, UIEventStream* eventStream, SettingsProvider* settings, QMap<QString, QString> emoticons);
			~QtChatWindow();
			std::string addMessage(const std::string &message, const std::string &senderName, bool senderIsSelf, boost::shared_ptr<SecurityLabel> label, const std::string& avatarPath, const boost::posix_time::ptime& time);
			std::string addAction(const std::string &message, const std::string &senderName, bool senderIsSelf, boost::shared_ptr<SecurityLabel> label, const std::string& avatarPath, const boost::posix_time::ptime& time);
			void addSystemMessage(const std::string& message);
			void addPresenceMessage(const std::string& message);
			void addErrorMessage(const std::string& errorMessage);
			void replaceMessage(const std::string& message, const std::string& id, const boost::posix_time::ptime& time);
			void replaceWithAction(const std::string& message, const std::string& id, const boost::posix_time::ptime& time);
			// File transfer related stuff
			std::string addFileTransfer(const std::string& senderName, bool senderIsSelf, const std::string& filename, const boost::uintmax_t sizeInBytes);
			void setFileTransferProgress(std::string id, const int percentageDone);
			void setFileTransferStatus(std::string id, const FileTransferState state, const std::string& msg);
			
			void show();
			void activate();
			void setUnreadMessageCount(int count);
			void convertToMUC();
//			TreeWidget *getTreeWidget();
			void setAvailableSecurityLabels(const std::vector<SecurityLabelsCatalog::Item>& labels);
			void setSecurityLabelsEnabled(bool enabled);
			void setSecurityLabelsError();
			SecurityLabelsCatalog::Item getSelectedSecurityLabel();
			void setName(const std::string& name);
			void setInputEnabled(bool enabled);
			QtTabbable::AlertType getWidgetAlertState();
			void setContactChatState(ChatState::ChatStateType state);
			void setRosterModel(Roster* roster);
			void setTabComplete(TabComplete* completer);
			int getCount();
			void replaceLastMessage(const std::string& message);
			void setAckState(const std::string& id, AckState state);

			// message receipts
			void setMessageReceiptState(const std::string& id, ChatWindow::ReceiptState state);

			void flash();
			QByteArray getSplitterState();
			virtual void setAvailableOccupantActions(const std::vector<OccupantAction>& actions);
			void setSubject(const std::string& subject);
			void showRoomConfigurationForm(Form::ref);
			void addMUCInvitation(const std::string& senderName, const JID& jid, const std::string& reason, const std::string& password, bool direct = true);
			void setAffiliations(MUCOccupant::Affiliation, const std::vector<JID>&);
			void setAvailableRoomActions(const std::vector<RoomAction> &actions);

			InviteToChatWindow* createInviteToChatWindow();

			static QString buildChatWindowButton(const QString& name, const QString& id, const QString& arg1 = QString(), const QString& arg2 = QString(), const QString& arg3 = QString());

		public slots:
			void handleChangeSplitterState(QByteArray state);
			void handleFontResized(int fontSizeSteps);
			void setAlert(const std::string& alertText, const std::string& buttonText = "");
			void cancelAlert();
			void setCorrectionEnabled(Tristate enabled);

		signals:
			void geometryChanged();
			void splitterMoved();
			void fontResized(int);

		protected slots:
			void qAppFocusChanged(QWidget* old, QWidget* now);
			void closeEvent(QCloseEvent* event);
			void resizeEvent(QResizeEvent* event);
			void moveEvent(QMoveEvent* event);

			void dragEnterEvent(QDragEnterEvent *event);
			void dropEvent(QDropEvent *event);

		protected:
			void showEvent(QShowEvent* event);

		private slots:
			void handleLogCleared();
			void returnPressed();
			void handleInputChanged();
			void handleCursorPositionChanged();
			void handleKeyPressEvent(QKeyEvent* event);
			void handleSplitterMoved(int pos, int index);
			void handleAlertButtonClicked();
			void handleActionButtonClicked();

			void handleHTMLButtonClicked(QString id, QString arg1, QString arg2, QString arg3);
			void handleAffiliationEditorAccepted();
			void handleCurrentLabelChanged(int);

		private:
			enum PreviousMessageKind {
				PreviosuMessageWasNone,
				PreviousMessageWasMessage,
				PreviousMessageWasSystem,
				PreviousMessageWasPresence,
				PreviousMessageWasFileTransfer,
				PreviousMessageWasMUCInvite
			};

		private:
			void updateTitleWithUnreadCount();
			void tabComplete();
			void beginCorrection();
			void cancelCorrection();
			void handleSettingChanged(const std::string& setting);
			std::string addMessage(const QString& message, const std::string& senderName, bool senderIsSelf, boost::shared_ptr<SecurityLabel> label, const std::string& avatarPath, const QString& style, const boost::posix_time::ptime& time);
			void replaceMessage(const QString& message, const std::string& id, const boost::posix_time::ptime& time, const QString& style);
			void handleOccupantSelectionChanged(RosterItem* item);
			bool appendToPreviousCheck(PreviousMessageKind messageKind, const std::string& senderName, bool senderIsSelf) const;
			QString linkimoticonify(const QString& message) const;

			int unreadCount_;
			bool contactIsTyping_;
			LastLineTracker lastLineTracker_;
			QString contact_;
			QString lastSentMessage_;
			QTextCursor tabCompleteCursor_;
			QtChatView* messageLog_;
			QtChatTheme* theme_;
			QtTextEdit* input_;
			QWidget* midBar_; 
			QComboBox* labelsWidget_;
			QtOccupantListWidget* treeWidget_;
			QLabel* correctingLabel_;
			QLabel* alertLabel_;
			QWidget* alertWidget_;
			QPushButton* alertButton_;
			TabComplete* completer_;
			QLineEdit* subject_;
			QPushButton* actionButton_;
			bool isCorrection_;
			bool previousMessageWasSelf_;
			PreviousMessageKind previousMessageKind_;
			QString previousSenderName_;
			bool inputClearing_;
			bool tabCompletion_;
			UIEventStream* eventStream_;
			bool inputEnabled_;
			QSplitter *logRosterSplitter_;
			Tristate correctionEnabled_;
			QString alertStyleSheet_;
			std::map<QString, QString> descriptions;
			QtChatWindowJSBridge* jsBridge;
			QPointer<QtMUCConfigurationWindow> mucConfigurationWindow_;
			QPointer<QtAffiliationEditor> affiliationEditor_;
			int idCounter_;
			SettingsProvider* settings_;
			std::vector<ChatWindow::RoomAction> availableRoomActions_;
			QMap<QString, QString> emoticons_;
			bool showEmoticons_;
			QPalette defaultLabelsPalette_;
			LabelModel* labelModel_;
	};
}