-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathchatmessage.cpp
More file actions
326 lines (272 loc) · 11.8 KB
/
Copy pathchatmessage.cpp
File metadata and controls
326 lines (272 loc) · 11.8 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2014-2019 by The qTox Project Contributors
* Copyright © 2024-2025 The TokTok team.
*/
#include "chatmessage.h"
#include "src/chatlog/chatlinecontentproxy.h"
#include "textformatter.h"
#include "content/broken.h"
#include "content/filetransferwidget.h"
#include "content/image.h"
#include "content/notificationicon.h"
#include "content/spinner.h"
#include "content/text.h"
#include "content/timestamp.h"
#include "src/persistence/history.h"
#include "src/persistence/settings.h"
#include "src/persistence/smileypack.h"
#include "src/widget/style.h"
#include "src/widget/tool/identicon.h"
#include <QCryptographicHash>
#include <QDebug>
#include <memory>
#define NAME_COL_WIDTH 90.0
#define TIME_COL_WIDTH 90.0
ChatMessage::ChatMessage(DocumentCache& documentCache_, Settings& settings_, Style& style_)
: documentCache{documentCache_}
, settings{settings_}
, style{style_}
{
}
ChatMessage::~ChatMessage() = default;
ChatMessage::Ptr ChatMessage::createChatMessage(const QString& sender, const QString& rawMessage,
MessageType type, bool isMe, MessageState state,
const QDateTime& date, DocumentCache& documentCache,
SmileyPack& smileyPack, Settings& settings,
Style& style, bool colorizeName)
{
ChatMessage::Ptr msg = std::make_shared<ChatMessage>(documentCache, settings, style);
QString text = rawMessage.toHtmlEscaped();
QString senderText = sender;
auto textType = Text::NORMAL;
// garbage after \0
if (settings.getHidePostNullSuffix()) {
text = TextFormatter::processPostNullSuffix(text, true);
}
// smileys
if (settings.getUseEmoticons())
text = smileyPack.smileyfied(text);
// quotes (green text)
text = detectQuotes(text, type);
text = TextFormatter::highlightURI(text);
// text styling
const Settings::StyleType styleType = settings.getStylePreference();
if (styleType != Settings::StyleType::NONE) {
text = TextFormatter::applyMarkdown(text, styleType == Settings::StyleType::WITH_CHARS);
}
switch (type) {
case NORMAL:
text = wrapDiv(text, "msg");
break;
case ACTION:
textType = Text::ACTION;
senderText = "*";
text = wrapDiv(QString("%1 %2").arg(sender.toHtmlEscaped(), text), "action");
msg->setAsAction();
break;
case ALERT:
text = wrapDiv(text, "alert");
break;
}
// Note: Eliding cannot be enabled for RichText items. (QTBUG-17207)
const QFont baseFont = settings.getChatMessageFont();
QFont authorFont = baseFont;
if (isMe)
authorFont.setBold(true);
QColor color = style.getColor(Style::ColorPalette::MainText);
if (colorizeName) {
const QByteArray hash = QCryptographicHash::hash(sender.toUtf8(), QCryptographicHash::Sha256);
auto lightness = color.lightnessF();
// Adapt as good as possible to Light/Dark themes
lightness = lightness * 0.5f + 0.3f;
// Magic values
color.setHslF(Identicon::bytesToColor(hash.left(Identicon::IDENTICON_COLOR_BYTES)), 1.0,
lightness);
if (!isMe && textType == Text::NORMAL) {
textType = Text::CUSTOM;
}
}
msg->addColumn(new Text(documentCache, settings, style, color, senderText, authorFont, true,
sender, textType),
ColumnFormat(NAME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
msg->addColumn(new Text(documentCache, settings, style, text, baseFont, false,
((type == ACTION) && isMe) ? QString("%1 %2").arg(sender, rawMessage)
: rawMessage),
ColumnFormat(1.0, ColumnFormat::VariableSize));
switch (state) {
case MessageState::complete:
msg->addColumn(new Timestamp(date, settings.getTimestampFormat(), baseFont, documentCache,
settings, style),
ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
break;
case MessageState::pending:
msg->addColumn(new Spinner(style.getImagePath("chatArea/spinner.svg", settings),
QSize(16, 16), 360.0 / 1.6),
ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
break;
case MessageState::broken:
msg->addColumn(new Broken(style.getImagePath("chatArea/error.svg", settings), QSize(16, 16)),
ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
break;
}
return msg;
}
ChatMessage::Ptr ChatMessage::createChatInfoMessage(const QString& rawMessage,
SystemMessageType type, const QDateTime& date,
DocumentCache& documentCache,
Settings& settings, Style& style)
{
ChatMessage::Ptr msg = std::make_shared<ChatMessage>(documentCache, settings, style);
const QString text = rawMessage.toHtmlEscaped();
QString img;
switch (type) {
case INFO:
img = style.getImagePath("chatArea/info.svg", settings);
break;
case ERROR:
img = style.getImagePath("chatArea/error.svg", settings);
break;
case TYPING:
img = style.getImagePath("chatArea/typing.svg", settings);
break;
}
const QFont baseFont = settings.getChatMessageFont();
msg->addColumn(new Image(QSize(18, 18), img),
ColumnFormat(NAME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
msg->addColumn(new Text(documentCache, settings, style, "<b>" + text + "</b>", baseFont, false, text),
ColumnFormat(1.0, ColumnFormat::VariableSize, ColumnFormat::Left));
msg->addColumn(new Timestamp(date, settings.getTimestampFormat(), baseFont, documentCache,
settings, style),
ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
return msg;
}
ChatMessage::Ptr ChatMessage::createFileTransferMessage(const QString& sender, CoreFile& coreFile,
ToxFile file, bool isMe, const QDateTime& date,
DocumentCache& documentCache,
Settings& settings, Style& style,
IMessageBoxManager& messageBoxManager, ImageLoader& imageLoader)
{
ChatMessage::Ptr msg = std::make_shared<ChatMessage>(documentCache, settings, style);
const QFont baseFont = settings.getChatMessageFont();
QFont authorFont = baseFont;
if (isMe) {
authorFont.setBold(true);
}
msg->addColumn(new Text(documentCache, settings, style, sender, authorFont, true),
ColumnFormat(NAME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
msg->addColumn(new ChatLineContentProxy(new FileTransferWidget(nullptr, coreFile, file, settings,
style, messageBoxManager, imageLoader),
320, 0.6f),
ColumnFormat(1.0, ColumnFormat::VariableSize));
msg->addColumn(new Timestamp(date, settings.getTimestampFormat(), baseFont, documentCache,
settings, style),
ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
return msg;
}
ChatMessage::Ptr ChatMessage::createTypingNotification(DocumentCache& documentCache,
Settings& settings, Style& style)
{
ChatMessage::Ptr msg = std::make_shared<ChatMessage>(documentCache, settings, style);
const QFont baseFont = settings.getChatMessageFont();
// Note: "[user]..." is just a placeholder. The actual text is set in
// ChatForm::setFriendTyping()
//
// FIXME: Due to circumstances, placeholder is being used in a case where
// user received typing notifications constantly since contact came online.
// This causes "[user]..." to be displayed in place of user nick, as long
// as user will keep typing. Issue #1280
msg->addColumn(new NotificationIcon(settings, style, QSize(18, 18)),
ColumnFormat(NAME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
msg->addColumn(new Text(documentCache, settings, style, "[user]...", baseFont, false, ""),
ColumnFormat(1.0, ColumnFormat::VariableSize, ColumnFormat::Left));
return msg;
}
/**
* @brief Create message placeholder while chat form restructures text
*
* It can take a while for chat form to resize large amounts of text, thus
* a message placeholder is needed to inform users about it.
*
* @return created message
*/
ChatMessage::Ptr ChatMessage::createBusyNotification(DocumentCache& documentCache,
Settings& settings, Style& style)
{
ChatMessage::Ptr msg = std::make_shared<ChatMessage>(documentCache, settings, style);
QFont baseFont = settings.getChatMessageFont();
baseFont.setPixelSize(baseFont.pixelSize() + 2);
baseFont.setBold(true);
msg->addColumn(new Text(documentCache, settings, style,
QObject::tr("Reformatting text...",
"Waiting for text to be reformatted"),
baseFont, false, ""),
ColumnFormat(1.0, ColumnFormat::VariableSize, ColumnFormat::Center));
return msg;
}
void ChatMessage::markAsDelivered(const QDateTime& time)
{
const QFont baseFont = settings.getChatMessageFont();
// remove the spinner and replace it by $time
replaceContent(2, new Timestamp(time, settings.getTimestampFormat(), baseFont, documentCache,
settings, style));
}
void ChatMessage::markAsBroken()
{
replaceContent(2, new Broken(style.getImagePath("chatArea/error.svg", settings), QSize(16, 16)));
}
QString ChatMessage::toString() const
{
ChatLineContent* c = getContent(1);
if (c != nullptr)
return c->getText();
return {};
}
bool ChatMessage::isAction() const
{
return action;
}
void ChatMessage::setAsAction()
{
action = true;
}
void ChatMessage::hideSender()
{
ChatLineContent* c = getContent(0);
if (c != nullptr)
c->hide();
}
void ChatMessage::hideDate()
{
ChatLineContent* c = getContent(2);
if (c != nullptr)
c->hide();
}
QString ChatMessage::detectQuotes(const QString& str, MessageType type)
{
// detect text quotes
QStringList messageLines = str.split("\n");
QString quotedText;
for (int i = 0; i < messageLines.size(); ++i) {
// don't quote first line in action message. This makes co-existence of
// quotes and action messages possible, since only first line can cause
// problems in case where there is quote in it used.
if (QRegularExpression(QRegularExpression::anchoredPattern("^(>|>).*"))
.match(messageLines[i])
.hasMatch()) {
if (i > 0 || type != ACTION)
quotedText += "<span class=quote>" + messageLines[i] + " </span>";
else
quotedText += messageLines[i];
} else {
quotedText += messageLines[i];
}
if (i < messageLines.size() - 1) {
quotedText += '\n';
}
}
return quotedText;
}
QString ChatMessage::wrapDiv(const QString& str, const QString& div)
{
return QString("<p class=%1>%2</p>").arg(div, /*QChar(0x200E) + */ QString(str));
}