/
mainwindow.cpp
425 lines (380 loc) · 15.2 KB
/
mainwindow.cpp
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/**
This file is part of Slacken.
Slacken 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 3 of the License, or
(at your option) any later version.
Slacken is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Slacken. If not, see <http://www.gnu.org/licenses/>.
**/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "slackclient.h"
#include <QSystemTrayIcon>
#include <QRegularExpression>
#include <QMessageBox>
#include <QDesktopServices>
#include <QTreeWidgetItem>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
currentChannel = nullptr;
QSettings settings;
settings.beginGroup("layout");
if (settings.contains("splitter"))
ui->splitter->restoreState(settings.value("splitter").toByteArray());
tray = new QSystemTrayIcon(this);
tray->setIcon(this->windowIcon());
tray->show();
connect(tray, &QSystemTrayIcon::activated, [this](QSystemTrayIcon::ActivationReason) {
this->show();
});
client = new SlackClient(this);
clients.append(new QTreeWidgetItem(ui->channelTreeWidget));
clients[0]->setText(0, "Pending...");
connect(client, &SlackClient::hasBasicData, [this]() {
clients[0]->setText(0, client->teamName());
});
connect(client, &SlackClient::authenticated, [this]() {
QSettings settings;
settings.beginGroup("auth");
settings.setValue("token", client->currentToken());
settings.sync();
client->start();
});
connect(client, &SlackClient::channelAdded, [this](SlackChannel *chan) {
if (chan->is_member) {
this->showChannelInTree(chan);
}
});
connect(client, &SlackClient::channelJoined, [this](SlackChannel *chan) {
this->showChannelInTree(chan);
});
connect(client, &SlackClient::channelHistory, this, &MainWindow::channelHistoryAvailable);
connect(client, &SlackClient::newMessage, this, &MainWindow::newMessageArrived);
connect(client, &SlackClient::desktopNotification, this, &MainWindow::desktopNotificationArrived);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::showChannelInTree(SlackChannel *chan)
{
QString display = chan->name;
if (chan->is_channel)
display = QString("#%1").arg(chan->name);
else if (chan->is_group)
display = QString("&%1").arg(chan->name);
auto item = new QTreeWidgetItem(clients[0]);
item->setText(0, display);
item->setData(0, Qt::UserRole + 42, QVariant::fromValue(chan));
if (chan->has_unread) {
auto font = item->font(0);
font.setWeight(QFont::Bold);
item->setFont(0, font);
}
connect(chan, &SlackChannel::unreadChanged, [item, chan]() {
if (chan->has_unread) {
auto font = item->font(0);
font.setWeight(QFont::Bold);
item->setFont(0, font);
} else {
auto font = item->font(0);
font.setWeight(QFont::Normal);
item->setFont(0, font);
}
});
clients[0]->addChild(item);
clients[0]->sortChildren(0, Qt::AscendingOrder);
}
void MainWindow::on_channelTreeWidget_itemClicked(QTreeWidgetItem *item)
{
currentChannel = item->data(0, Qt::UserRole + 42).value<SlackChannel*>();
if (currentChannel == nullptr)
return;
qDebug() << "Switching to " << currentChannel->id;
client->requestHistory(currentChannel->id);
item->setTextColor(0, QPalette().windowText().color());
ui->topicViewer->clear();
auto cursor = ui->topicViewer->textCursor();
renderText(cursor, currentChannel->topic);
ui->newMessage->setFocus();
}
void MainWindow::renderText(QTextCursor &cursor, const QString &text)
{
QString output = text;
QRegularExpression formatDelimiter("(?:\\W|^)(<([^>]+)>)(?:\\W|$)");
int currentIdx = 0;
int pos;
QRegularExpressionMatch match;
while ((pos = output.indexOf(formatDelimiter, currentIdx, &match)) != -1) {
pos = match.capturedStart(1);
cursor.insertText(text.mid(currentIdx, pos - currentIdx));
QStringRef capturedPart = match.capturedRef(2);
if (capturedPart.startsWith('@')) {
QString userId = capturedPart.mid(1).toString();
if (client->hasUser(userId)) {
auto charFormat = cursor.charFormat();
charFormat.setFontWeight(QFont::Bold);
charFormat.setAnchorHref(QString("slack://im/%1").arg(userId));
cursor.setCharFormat(charFormat);
auto &user = client->user(userId);
cursor.insertText("@");
cursor.insertText(user.name);
cursor.setCharFormat(QTextCharFormat());
} else {
cursor.insertText("@");
cursor.insertText(userId);
}
} else if (capturedPart.startsWith("http")) {
// This is a link !
auto charFormat = cursor.charFormat();
charFormat.setForeground(QBrush(QColor(0, 0, 255)));
charFormat.setAnchorHref(capturedPart.mid(0, capturedPart.indexOf('|')).toString());
cursor.setCharFormat(charFormat);
if (capturedPart.contains('|')) {
cursor.insertText(capturedPart.mid(capturedPart.indexOf('|') + 1).toString());
} else {
cursor.insertText(capturedPart.toString());
}
cursor.setCharFormat(QTextCharFormat());
} else if (capturedPart.startsWith('#')) {
QString channel = capturedPart.mid(1, capturedPart.indexOf('|') - 1).toString();
// This is a channel
auto charFormat = cursor.charFormat();
charFormat.setFontWeight(QFont::Bold);
charFormat.setAnchorHref(QString("slack://channel/%1").arg(channel));
cursor.setCharFormat(charFormat);
cursor.insertText("#");
if (capturedPart.contains('|')) {
cursor.insertText(capturedPart.mid(capturedPart.indexOf('|') + 1).toString());
} else {
cursor.insertText(capturedPart.toString());
}
cursor.setCharFormat(QTextCharFormat());
} else {
// We don't know
cursor.insertText(capturedPart.toString());
}
currentIdx = pos + 1 + match.capturedLength(1) - 1;
}
cursor.insertText(text.mid(currentIdx));
cursor.setCharFormat(QTextCharFormat());
}
void MainWindow::renderMessage(QTextCursor &cursor, const SlackMessage &message)
{
// Display time
cursor.insertText(message.when.time().toString());
// Display user
QString userDisplay = "\t<%1>\t";
if (!message.user.isEmpty()) {
if (client->hasUser(message.user)) {
auto user = client->user(message.user);
userDisplay = userDisplay.arg(user.name);
QTextCharFormat fmt;
fmt.setAnchorHref(QString("slack://im/%1").arg(message.user));
cursor.mergeCharFormat(fmt);
}
} else if (!message.username.isEmpty()) {
userDisplay = userDisplay.arg(message.username);
}
cursor.insertText(userDisplay);
cursor.setCharFormat(QTextCharFormat());
// Display text itself
renderText(cursor, message.text);
for (auto &attachment: message.attachments) {
cursor.insertBlock();
QTextCharFormat fmt = cursor.charFormat();
QBrush foreground = fmt.foreground();
if (attachment.color.isValid()) {
fmt.setForeground(attachment.color);
cursor.setCharFormat(fmt);
}
if (!attachment.title.isEmpty()) {
qDebug() << "TITLE";
cursor.insertText(attachment.title);
if (!attachment.text.isEmpty()) {
cursor.insertBlock();
}
cursor.insertText("\t\t");
}
if (attachment.text.isEmpty() && !attachment.fallback.isEmpty())
cursor.insertText(attachment.fallback);
else
cursor.insertText(attachment.text);
if (attachment.color.isValid()) {
fmt.setForeground(foreground);
cursor.setCharFormat(fmt);
}
}
}
void MainWindow::channelHistoryAvailable(const QList<SlackMessage> &messages)
{
qDebug() << "Received !";
ui->historyView->clear();
QList<QTextOption::Tab> tabs;
tabs << QTextOption::Tab(300, QTextOption::LeftTab);
tabs << QTextOption::Tab(500, QTextOption::LeftTab);
ui->historyView->document()->defaultTextOption().setTabs(tabs);
//ui->historyView->setTabStopDistance(200);
auto cursor = ui->historyView->textCursor();
cursor.movePosition(QTextCursor::End);
QList<SlackMessage> sorted_messages = messages;
std::sort(sorted_messages.begin(), sorted_messages.end(), [] (const SlackMessage &msgA, const SlackMessage &msgB) {
return msgA.when < msgB.when;
});
bool crossedMark = !currentChannel->last_read.isValid();
QTextBlockFormat baseFmt;
QString lastReadMarker("last-read-marker");
QTextCursor lastReadPosition;
QDate currentDate;
QTextCursor previousCursor = cursor;
for (const SlackMessage &message: sorted_messages) {
if (!crossedMark && (message.when > currentChannel->last_read)) {
qDebug() << "INSERTING HTML " << crossedMark << message.when << currentChannel->last_read;
//baseFmt = cursor.blockFormat();
QTextCursor previousBlock(cursor);
previousBlock.movePosition(QTextCursor::PreviousBlock);
QTextBlockFormat newFmt;
newFmt.setBottomMargin(1);
//newFmt.setTopMargin(1);
newFmt.setProperty(QTextFormat::BlockTrailingHorizontalRulerWidth, QTextLength(QTextLength::PercentageLength, 100));
previousBlock.mergeBlockFormat(newFmt);
QTextCharFormat newCharFmt;
newCharFmt.setProperty(QTextFormat::AnchorName, lastReadMarker);
newCharFmt.setProperty(QTextFormat::IsAnchor, true);
previousBlock.mergeCharFormat(newCharFmt);
lastReadPosition = previousBlock;
//cursor.setBlockFormat(baseFmt);
crossedMark = true;
}
if (message.when.date() != currentDate) {
currentDate = message.when.date();
cursor.insertBlock();
cursor.insertText("It's a brand new date - ");
cursor.insertText(currentDate.toString());
}
cursor.insertBlock();
cursor.movePosition(QTextCursor::NextBlock);
renderMessage(cursor, message);
}
if (!crossedMark) {
// Always set the last-read-marker.
QTextCharFormat newFmt;
newFmt.setProperty(QTextFormat::AnchorName, lastReadMarker);
newFmt.setProperty(QTextFormat::IsAnchor, true);
lastReadPosition = cursor;
cursor.mergeCharFormat(newFmt);
}
currentChannel->markRead(sorted_messages.last());
QTextCursor endOfDoc(ui->historyView->document());
endOfDoc.movePosition(QTextCursor::End);
// Go to the end
ui->historyView->setTextCursor(endOfDoc);
ui->historyView->ensureCursorVisible();
// Now force a scroll back
ui->historyView->setTextCursor(lastReadPosition);
ui->historyView->ensureCursorVisible();
}
void MainWindow::on_newMessage_returnPressed()
{
qDebug() << "Sending " << ui->newMessage->text() << " to " << currentChannel;
QString msg = ui->newMessage->text();
// Trying to match @XXX to users
QRegularExpression userRegex("(?:\\W|^)(@\\w+)(?:\\W|$)");
QRegularExpressionMatch match;
int offset = 0;
while ((match = userRegex.match(msg, offset)).hasMatch()) {
//qDebug() << match.capturedTexts();
QString id = client->userId(match.captured(1).mid(1));
if (!id.isNull()) {
msg = QString("%1<@%2>%3")
.arg(msg.mid(0, match.capturedStart(1)))
.arg(id)
.arg(msg.mid(match.capturedEnd(1)));
offset = match.capturedStart() + id.length();
} else {
offset = match.capturedEnd();
}
}
//qDebug() << msg;
client->sendMessage(currentChannel->id, msg);
ui->newMessage->clear();
}
void MainWindow::desktopNotificationArrived(const QString &title, const QString &subtitle, const QString &msg)
{
tray->showMessage(title + " / " + subtitle, msg);
}
void MainWindow::newMessageArrived(const QString &channel, const SlackMessage &message)
{
if (!currentChannel)
return;
qDebug() << "Received message on " << channel << " while on " << currentChannel->id;
if (channel == currentChannel->id) {
auto cursor = ui->historyView->textCursor();
cursor.movePosition(QTextCursor::End);
cursor.setCharFormat(QTextCharFormat());
cursor.setBlockCharFormat(QTextCharFormat());
cursor.insertBlock();
renderMessage(cursor, message);
cursor.movePosition(QTextCursor::NextBlock);
ui->historyView->setTextCursor(cursor);
// TODO : some focus jumble mumble : if window is not focused, don't mark read, wait until it is really seen/acted upon
// You must *not* mark your own messages as read, it makes slack all fuzzy...
if (message.user != client->currentUserId())
currentChannel->markRead(message);
}
}
void MainWindow::on_actionQuit_triggered()
{
qApp->exit();
}
void MainWindow::on_actionLogin_triggered()
{
QSettings settings;
settings.beginGroup("auth");
if (settings.contains("token")) {
client->login(settings.value("token").toString());
} else {
QMessageBox::information(this, "OAuth authentication", "A new webbrowser window will open for you to login. It will contact Slacken back on the loopback for authentication purpose.\n"
"Make sure extensions like NoScript won't block that as a XSS attack.");
client->login(QString());
}
}
void MainWindow::on_historyView_anchorClicked(const QUrl &url)
{
if (url.scheme() == "slack") {
// XXX TODO : have a way to get a callback to the proper view.
if (url.host() == "im") {
qDebug() << "Open IM with " << url.path().mid(1);
client->openConversation(QStringList(url.path().mid(1)));
} else if (url.host() == "channel") {
qDebug() << "Open channel " << url.path().mid(1);
client->openConversation(url.path().mid(1));
} else {
qDebug() << "Unknown slack action " << url.host();
}
} else {
QDesktopServices::openUrl(url);
}
}
void MainWindow::on_splitter_splitterMoved(int, int)
{
QSettings settings;
settings.beginGroup("layout");
if (!settings.contains("splitter") || settings.value("splitter").toByteArray() != ui->splitter->saveState()) {
settings.setValue("splitter", ui->splitter->saveState());
// That's ugly
settings.sync();
}
}
void MainWindow::on_historyView_highlighted(const QString &arg1)
{
ui->statusBar->showMessage(arg1, 1000);
}