summaryrefslogtreecommitdiffstats
blob: 45585c82ec4333fae657283b3ca5a009409dc1fd (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
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

/*
 * Copyright (c) 2012 Thilo Cestonaro
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#ifndef SWIFT_QtWebKitChatView_H
#define SWIFT_QtWebKitChatView_H

#include <QString>
#include <QWidget>
#include <QList>
#include <QWebElement>
#include <QInputDialog>
#include <QFileDialog>

#include <boost/shared_ptr.hpp>

#include "MessageSnippet.h"

#include "QtChatView.h"
#include <Swift/Controllers/UIInterfaces/ChatWindow.h>
#include "Swift/Controllers/Settings/SettingsProvider.h"

#include <SwifTools/LastLineTracker.h>

class QWebPage;
class QUrl;

namespace Swift {
	class QtWebView;
	class QtChatTheme;
	class UIEventStream;
	class QtChatWindowJSBridge;

	class QtWebKitChatView : public QtChatView {
			Q_OBJECT

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

		public:
			QtWebKitChatView(QtChatTheme* theme, QWidget* parent, UIEventStream* eventStream, SettingsProvider* settings, QMap<QString, QString> emoticons);
			virtual ~QtWebKitChatView();
			void addMessage(boost::shared_ptr<ChatSnippet> snippet);
			void addLastSeenLine();
			void replaceWithAction(const std::string& message, const std::string& id, const boost::posix_time::ptime& time);
			void replaceLastMessage(const QString& newMessage);
			void replaceLastMessage(const QString& newMessage, const QString& note);
			void replaceMessage(const std::string& newMessage, const QString& id, const QDateTime& time, const QString& style);
			void replaceMessage(const QString& message, const QString& id, const QDateTime& time, const QString& style);
			void rememberScrolledToBottom();
			void setAckXML(const QString& id, const QString& xml);
			void setReceiptXML(const QString& id, const QString& xml);
			void displayReceiptInfo(const QString& id, bool showIt);
			QString linkimoticonify(const QString& message) const;

			QString getLastSentMessage();
			void addToJSEnvironment(const QString&, QObject*);
			void setFileTransferStatus(QString id, const ChatWindow::FileTransferState state, const QString& msg);
			void setFileTransferProgress(QString id, const int percentageDone);
			void setMUCInvitationJoined(QString id);
			void showEmoticons(bool show);
			void setAckState(std::string const& id, ChatWindow::AckState);
			void setMessageReceiptState(const std::string& id, ChatWindow::ReceiptState state);
			std::string addMessage(const std::string &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);
			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 setChatWindowHasFocus(bool focus);
			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);
			std::string addFileTransfer(const std::string& senderName, bool senderIsSelf, const std::string& filename, const boost::uintmax_t sizeInBytes);
			void addErrorMessage(const std::string& errorMessage);
			void addSystemMessage(const std::string& message);
			void addPresenceMessage(const std::string& message);
			void addMUCInvitation(const std::string& senderName, const JID& jid, const std::string& reason, const std::string& password, bool direct);

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

		public slots:
			void copySelectionToClipboard();
			void scrollToBottom();
			void handleLinkClicked(const QUrl&);
			void handleKeyPressEvent(QKeyEvent* event);
			void resetView();
			void increaseFontSize(int numSteps = 1);
			void decreaseFontSize();
			void resizeFont(int fontSizeSteps);

		private slots:
			void handleViewLoadFinished(bool);
			void handleFrameSizeChanged();
			void handleClearRequested();
			void handleHTMLButtonClicked(QString id, QString arg1, QString arg2, QString arg3);

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

			void headerEncode();
			void messageEncode();
			void addToDOM(boost::shared_ptr<ChatSnippet> snippet);
			QWebElement snippetToDOM(boost::shared_ptr<ChatSnippet> snippet);
			bool appendToPreviousCheck(PreviousMessageKind messageKind, const std::string& senderName, bool senderIsSelf) const;
			void handleSettingChanged(const std::string& setting);

			bool viewReady_;
			bool isAtBottom_;
			QtWebView* webView_;
			QWebPage* webPage_;
			int fontSizeSteps_;
			QtChatTheme* theme_;
			QWebElement newInsertPoint_;
			QWebElement lineSeparator_;
			QWebElement lastElement_;
			QWebElement document_;
			bool previousMessageWasSelf_;
			QString previousSenderName_;
			PreviousMessageKind previousMessageKind_;
			int idCounter_;
			LastLineTracker lastLineTracker_;
			UIEventStream* eventStream_;
			std::map<QString, QString> descriptions_;
			QtChatWindowJSBridge* jsBridge_;
			SettingsProvider* settings_;
			QMap<QString, QString> emoticons_;
			bool showEmoticons_;
	};
}

#endif