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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
|
// SPDX-FileCopyrightText: 2017 Konstantinos Sideris <siderisk@auth.gr>
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include <limits>
#include <set>
#include <QObject>
#include <QPainter>
#include <QScroller>
#include <QStyle>
#include <QStyleOption>
#include <QTimer>
#include "Logging.h"
#include "MainWindow.h"
#include "RoomInfoListItem.h"
#include "RoomList.h"
#include "UserSettingsPage.h"
#include "Utils.h"
#include "ui/OverlayModal.h"
RoomList::RoomList(QSharedPointer<UserSettings> userSettings, QWidget *parent)
: QWidget(parent)
{
topLayout_ = new QVBoxLayout(this);
topLayout_->setSpacing(0);
topLayout_->setMargin(0);
scrollArea_ = new QScrollArea(this);
scrollArea_->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scrollArea_->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
scrollArea_->setWidgetResizable(true);
scrollArea_->setAlignment(Qt::AlignLeading | Qt::AlignTop | Qt::AlignVCenter);
scrollArea_->setAttribute(Qt::WA_AcceptTouchEvents);
QScroller::grabGesture(scrollArea_, QScroller::TouchGesture);
QScroller::grabGesture(scrollArea_, QScroller::LeftMouseButtonGesture);
// The scrollbar on macOS will hide itself when not active so it won't interfere
// with the content.
#if not defined(Q_OS_MAC)
scrollArea_->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
#endif
scrollAreaContents_ = new QWidget(this);
scrollAreaContents_->setObjectName("roomlist_area");
contentsLayout_ = new QVBoxLayout(scrollAreaContents_);
contentsLayout_->setAlignment(Qt::AlignTop);
contentsLayout_->setSpacing(0);
contentsLayout_->setMargin(0);
scrollArea_->setWidget(scrollAreaContents_);
topLayout_->addWidget(scrollArea_);
connect(this, &RoomList::updateRoomAvatarCb, this, &RoomList::updateRoomAvatar);
connect(userSettings.data(),
&UserSettings::roomSortingChanged,
this,
&RoomList::sortRoomsByLastMessage);
}
void
RoomList::addRoom(const QString &room_id, const RoomInfo &info)
{
auto room_item = new RoomInfoListItem(room_id, info, scrollArea_);
room_item->setRoomName(QString::fromStdString(std::move(info.name)));
connect(room_item, &RoomInfoListItem::clicked, this, &RoomList::highlightSelectedRoom);
connect(room_item, &RoomInfoListItem::leaveRoom, this, [](const QString &room_id) {
MainWindow::instance()->openLeaveRoomDialog(room_id);
});
QSharedPointer<RoomInfoListItem> roomWidget(room_item, &QObject::deleteLater);
rooms_.emplace(room_id, roomWidget);
rooms_sort_cache_.push_back(roomWidget);
if (!info.avatar_url.empty())
updateAvatar(room_id, QString::fromStdString(info.avatar_url));
int pos = contentsLayout_->count() - 1;
contentsLayout_->insertWidget(pos, room_item);
}
void
RoomList::updateAvatar(const QString &room_id, const QString &url)
{
emit updateRoomAvatarCb(room_id, url);
}
void
RoomList::removeRoom(const QString &room_id, bool reset)
{
auto roomIt = rooms_.find(room_id);
if (roomIt == rooms_.end()) {
return;
}
for (auto roomSortIt = rooms_sort_cache_.begin(); roomSortIt != rooms_sort_cache_.end();
++roomSortIt) {
if (roomIt->second == *roomSortIt) {
rooms_sort_cache_.erase(roomSortIt);
break;
}
}
rooms_.erase(room_id);
if (rooms_.empty() || !reset)
return;
auto room = firstRoom();
if (room.second.isNull())
return;
room.second->setPressedState(true);
emit roomChanged(room.first);
}
void
RoomList::updateUnreadMessageCount(const QString &roomid, int count, int highlightedCount)
{
if (!roomExists(roomid)) {
nhlog::ui()->warn("updateUnreadMessageCount: unknown room_id {}",
roomid.toStdString());
return;
}
rooms_[roomid]->updateUnreadMessageCount(count, highlightedCount);
calculateUnreadMessageCount();
sortRoomsByLastMessage();
}
void
RoomList::calculateUnreadMessageCount()
{
int total_unread_msgs = 0;
for (const auto &room : rooms_) {
if (!room.second.isNull())
total_unread_msgs += room.second->unreadMessageCount();
}
emit totalUnreadMessageCountUpdated(total_unread_msgs);
}
void
RoomList::initialize(const QMap<QString, RoomInfo> &info)
{
nhlog::ui()->info("initialize room list");
rooms_.clear();
// prevent flickering and save time sorting over and over again
setUpdatesEnabled(false);
for (auto it = info.begin(); it != info.end(); it++) {
if (it.value().is_invite)
addInvitedRoom(it.key(), it.value());
else
addRoom(it.key(), it.value());
}
for (auto it = info.begin(); it != info.end(); it++)
updateRoomDescription(it.key(), it.value().msgInfo);
setUpdatesEnabled(true);
if (rooms_.empty())
return;
sortRoomsByLastMessage();
auto room = firstRoom();
if (room.second.isNull())
return;
room.second->setPressedState(true);
emit roomChanged(room.first);
}
void
RoomList::cleanupInvites(const std::map<QString, bool> &invites)
{
if (invites.size() == 0)
return;
utils::erase_if(rooms_, [invites](auto &room) {
auto room_id = room.first;
auto item = room.second;
if (!item)
return false;
return item->isInvite() && (invites.find(room_id) == invites.end());
});
}
void
RoomList::sync(const std::map<QString, RoomInfo> &info)
{
for (const auto &room : info)
updateRoom(room.first, room.second);
if (!info.empty())
sortRoomsByLastMessage();
}
void
RoomList::highlightSelectedRoom(const QString &room_id)
{
emit roomChanged(room_id);
if (!roomExists(room_id)) {
nhlog::ui()->warn("roomlist: clicked unknown room_id");
return;
}
for (auto const &room : rooms_) {
if (room.second.isNull())
continue;
if (room.first != room_id) {
room.second->setPressedState(false);
} else {
room.second->setPressedState(true);
scrollArea_->ensureWidgetVisible(room.second.data());
}
}
selectedRoom_ = room_id;
}
void
RoomList::nextRoom()
{
for (int ii = 0; ii < contentsLayout_->count() - 1; ++ii) {
auto room = qobject_cast<RoomInfoListItem *>(contentsLayout_->itemAt(ii)->widget());
if (!room)
continue;
if (room->roomId() == selectedRoom_) {
auto nextRoom = qobject_cast<RoomInfoListItem *>(
contentsLayout_->itemAt(ii + 1)->widget());
// Not a room message.
if (!nextRoom || nextRoom->isInvite())
return;
emit roomChanged(nextRoom->roomId());
if (!roomExists(nextRoom->roomId())) {
nhlog::ui()->warn("roomlist: clicked unknown room_id");
return;
}
room->setPressedState(false);
nextRoom->setPressedState(true);
scrollArea_->ensureWidgetVisible(nextRoom);
selectedRoom_ = nextRoom->roomId();
return;
}
}
}
void
RoomList::previousRoom()
{
for (int ii = 1; ii < contentsLayout_->count(); ++ii) {
auto room = qobject_cast<RoomInfoListItem *>(contentsLayout_->itemAt(ii)->widget());
if (!room)
continue;
if (room->roomId() == selectedRoom_) {
auto nextRoom = qobject_cast<RoomInfoListItem *>(
contentsLayout_->itemAt(ii - 1)->widget());
// Not a room message.
if (!nextRoom || nextRoom->isInvite())
return;
emit roomChanged(nextRoom->roomId());
if (!roomExists(nextRoom->roomId())) {
nhlog::ui()->warn("roomlist: clicked unknown room_id");
return;
}
room->setPressedState(false);
nextRoom->setPressedState(true);
scrollArea_->ensureWidgetVisible(nextRoom);
selectedRoom_ = nextRoom->roomId();
return;
}
}
}
void
RoomList::updateRoomAvatar(const QString &roomid, const QString &img)
{
if (!roomExists(roomid)) {
nhlog::ui()->warn("avatar update on non-existent room_id: {}",
roomid.toStdString());
return;
}
rooms_[roomid]->setAvatar(img);
// Used to inform other widgets for the new image data.
emit roomAvatarChanged(roomid, img);
}
void
RoomList::updateRoomDescription(const QString &roomid, const DescInfo &info)
{
if (!roomExists(roomid)) {
nhlog::ui()->warn("description update on non-existent room_id: {}, {}",
roomid.toStdString(),
info.body.toStdString());
return;
}
rooms_[roomid]->setDescriptionMessage(info);
if (underMouse()) {
// When the user hover out of the roomlist a sort will be triggered.
isSortPending_ = true;
return;
}
isSortPending_ = false;
emit sortRoomsByLastMessage();
}
struct room_sort
{
bool operator()(const QSharedPointer<RoomInfoListItem> &a,
const QSharedPointer<RoomInfoListItem> &b) const
{
// Sort by "importance" (i.e. invites before mentions before
// notifs before new events before old events), then secondly
// by recency.
// Checking importance first
const auto a_importance = a->calculateImportance();
const auto b_importance = b->calculateImportance();
if (a_importance != b_importance) {
return a_importance > b_importance;
}
// Now sort by recency
// Zero if empty, otherwise the time that the event occured
const uint64_t a_recency =
a->lastMsgInfo_.userid.isEmpty() ? 0 : a->lastMsgInfo_.timestamp;
const uint64_t b_recency =
b->lastMsgInfo_.userid.isEmpty() ? 0 : b->lastMsgInfo_.timestamp;
return a_recency > b_recency;
}
};
void
RoomList::sortRoomsByLastMessage()
{
isSortPending_ = false;
std::stable_sort(begin(rooms_sort_cache_), end(rooms_sort_cache_), room_sort{});
int newIndex = 0;
for (const auto &roomWidget : rooms_sort_cache_) {
const auto currentIndex = contentsLayout_->indexOf(roomWidget.data());
if (currentIndex != newIndex) {
contentsLayout_->removeWidget(roomWidget.data());
contentsLayout_->insertWidget(newIndex, roomWidget.data());
}
newIndex++;
}
}
void
RoomList::leaveEvent(QEvent *event)
{
if (isSortPending_)
QTimer::singleShot(700, this, &RoomList::sortRoomsByLastMessage);
QWidget::leaveEvent(event);
}
void
RoomList::closeJoinRoomDialog(bool isJoining, QString roomAlias)
{
joinRoomModal_->hide();
if (isJoining)
emit joinRoom(roomAlias);
}
void
RoomList::removeFilter(const std::set<QString> &roomsToHide)
{
setUpdatesEnabled(false);
for (int i = 0; i < contentsLayout_->count(); i++) {
auto widget =
qobject_cast<RoomInfoListItem *>(contentsLayout_->itemAt(i)->widget());
if (widget) {
if (roomsToHide.find(widget->roomId()) == roomsToHide.end())
widget->show();
else
widget->hide();
}
}
setUpdatesEnabled(true);
}
void
RoomList::applyFilter(const std::set<QString> &filter)
{
// Disabling paint updates will resolve issues with screen flickering on big room lists.
setUpdatesEnabled(false);
for (int i = 0; i < contentsLayout_->count(); i++) {
// If filter contains the room for the current RoomInfoListItem,
// show the list item, otherwise hide it
auto listitem =
qobject_cast<RoomInfoListItem *>(contentsLayout_->itemAt(i)->widget());
if (!listitem)
continue;
if (filter.find(listitem->roomId()) != filter.end())
listitem->show();
else
listitem->hide();
}
setUpdatesEnabled(true);
// If the already selected room is part of the group, make sure it's visible.
if (!selectedRoom_.isEmpty() && (filter.find(selectedRoom_) != filter.end()))
return;
selectFirstVisibleRoom();
}
void
RoomList::selectFirstVisibleRoom()
{
for (int i = 0; i < contentsLayout_->count(); i++) {
auto item = qobject_cast<RoomInfoListItem *>(contentsLayout_->itemAt(i)->widget());
if (item && item->isVisible()) {
highlightSelectedRoom(item->roomId());
break;
}
}
}
void
RoomList::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
void
RoomList::updateRoom(const QString &room_id, const RoomInfo &info)
{
if (!roomExists(room_id)) {
if (info.is_invite)
addInvitedRoom(room_id, info);
else
addRoom(room_id, info);
return;
}
auto room = rooms_[room_id];
updateAvatar(room_id, QString::fromStdString(info.avatar_url));
room->setRoomName(QString::fromStdString(info.name));
room->setRoomType(info.is_invite);
room->update();
}
void
RoomList::addInvitedRoom(const QString &room_id, const RoomInfo &info)
{
auto room_item = new RoomInfoListItem(room_id, info, scrollArea_);
connect(room_item, &RoomInfoListItem::acceptInvite, this, &RoomList::acceptInvite);
connect(room_item, &RoomInfoListItem::declineInvite, this, &RoomList::declineInvite);
QSharedPointer<RoomInfoListItem> roomWidget(room_item);
rooms_.emplace(room_id, roomWidget);
rooms_sort_cache_.push_back(roomWidget);
updateAvatar(room_id, QString::fromStdString(info.avatar_url));
int pos = contentsLayout_->count() - 1;
contentsLayout_->insertWidget(pos, room_item);
}
std::pair<QString, QSharedPointer<RoomInfoListItem>>
RoomList::firstRoom() const
{
for (int i = 0; i < contentsLayout_->count(); i++) {
auto item = qobject_cast<RoomInfoListItem *>(contentsLayout_->itemAt(i)->widget());
if (item) {
auto topRoom = rooms_.find(item->roomId());
if (topRoom != rooms_.end()) {
return std::pair<QString, QSharedPointer<RoomInfoListItem>>(
item->roomId(), topRoom->second);
}
}
}
return {};
}
void
RoomList::updateReadStatus(const std::map<QString, bool> &status)
{
for (const auto &room : status) {
if (roomExists(room.first)) {
auto item = rooms_.at(room.first);
if (item)
item->setReadState(room.second);
}
}
}
|