2017-04-06 02:06:42 +03:00
|
|
|
/*
|
|
|
|
* nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr>
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-12-13 22:49:27 +03:00
|
|
|
#include <limits>
|
2020-03-14 02:30:50 +03:00
|
|
|
#include <set>
|
2019-12-13 22:49:27 +03:00
|
|
|
|
2017-10-28 21:24:42 +03:00
|
|
|
#include <QObject>
|
2020-01-31 08:12:02 +03:00
|
|
|
#include <QPainter>
|
2020-02-15 05:47:58 +03:00
|
|
|
#include <QScroller>
|
2020-05-25 14:03:49 +03:00
|
|
|
#include <QStyle>
|
|
|
|
#include <QStyleOption>
|
2017-12-30 18:29:57 +03:00
|
|
|
#include <QTimer>
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2018-07-17 16:37:25 +03:00
|
|
|
#include "Logging.h"
|
2017-10-01 19:49:36 +03:00
|
|
|
#include "MainWindow.h"
|
2017-04-06 02:06:42 +03:00
|
|
|
#include "RoomInfoListItem.h"
|
|
|
|
#include "RoomList.h"
|
2020-03-15 18:19:22 +03:00
|
|
|
#include "UserSettingsPage.h"
|
2018-04-29 15:42:40 +03:00
|
|
|
#include "Utils.h"
|
2018-07-17 16:37:25 +03:00
|
|
|
#include "ui/OverlayModal.h"
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2020-03-15 18:19:22 +03:00
|
|
|
RoomList::RoomList(QSharedPointer<UserSettings> userSettings, QWidget *parent)
|
2017-08-20 13:47:22 +03:00
|
|
|
: QWidget(parent)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
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);
|
2018-01-09 16:07:32 +03:00
|
|
|
scrollArea_->setAlignment(Qt::AlignLeading | Qt::AlignTop | Qt::AlignVCenter);
|
2020-12-15 02:17:44 +03:00
|
|
|
scrollArea_->setAttribute(Qt::WA_AcceptTouchEvents);
|
2017-09-10 12:59:21 +03:00
|
|
|
|
2020-02-15 05:47:58 +03:00
|
|
|
QScroller::grabGesture(scrollArea_, QScroller::TouchGesture);
|
2020-12-15 02:17:44 +03:00
|
|
|
QScroller::grabGesture(scrollArea_, QScroller::LeftMouseButtonGesture);
|
2020-02-15 05:47:58 +03:00
|
|
|
|
2021-01-23 02:30:45 +03:00
|
|
|
// The scrollbar on macOS will hide itself when not active so it won't interfere
|
|
|
|
// with the content.
|
2018-07-25 18:59:33 +03:00
|
|
|
#if not defined(Q_OS_MAC)
|
|
|
|
scrollArea_->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
#endif
|
|
|
|
|
2017-11-09 00:09:15 +03:00
|
|
|
scrollAreaContents_ = new QWidget(this);
|
2018-07-26 12:12:00 +03:00
|
|
|
scrollAreaContents_->setObjectName("roomlist_area");
|
2017-09-10 12:59:21 +03:00
|
|
|
|
|
|
|
contentsLayout_ = new QVBoxLayout(scrollAreaContents_);
|
2018-10-07 14:18:44 +03:00
|
|
|
contentsLayout_->setAlignment(Qt::AlignTop);
|
2017-09-10 12:59:21 +03:00
|
|
|
contentsLayout_->setSpacing(0);
|
|
|
|
contentsLayout_->setMargin(0);
|
|
|
|
|
|
|
|
scrollArea_->setWidget(scrollAreaContents_);
|
|
|
|
topLayout_->addWidget(scrollArea_);
|
|
|
|
|
2018-06-09 16:03:14 +03:00
|
|
|
connect(this, &RoomList::updateRoomAvatarCb, this, &RoomList::updateRoomAvatar);
|
2020-03-15 22:27:30 +03:00
|
|
|
connect(userSettings.data(),
|
2020-03-15 21:30:21 +03:00
|
|
|
&UserSettings::roomSortingChanged,
|
|
|
|
this,
|
|
|
|
&RoomList::sortRoomsByLastMessage);
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2018-04-21 16:34:50 +03:00
|
|
|
RoomList::addRoom(const QString &room_id, const RoomInfo &info)
|
2017-04-09 02:17:04 +03:00
|
|
|
{
|
2020-10-17 01:57:29 +03:00
|
|
|
auto room_item = new RoomInfoListItem(room_id, info, scrollArea_);
|
2018-04-21 16:34:50 +03:00
|
|
|
room_item->setRoomName(QString::fromStdString(std::move(info.name)));
|
2017-04-09 02:17:04 +03:00
|
|
|
|
2017-10-01 19:49:36 +03:00
|
|
|
connect(room_item, &RoomInfoListItem::clicked, this, &RoomList::highlightSelectedRoom);
|
2018-02-20 18:09:11 +03:00
|
|
|
connect(room_item, &RoomInfoListItem::leaveRoom, this, [](const QString &room_id) {
|
2018-02-10 17:05:31 +03:00
|
|
|
MainWindow::instance()->openLeaveRoomDialog(room_id);
|
|
|
|
});
|
2017-10-01 19:49:36 +03:00
|
|
|
|
2020-10-17 01:57:29 +03:00
|
|
|
QSharedPointer<RoomInfoListItem> roomWidget(room_item, &QObject::deleteLater);
|
2020-05-01 00:59:17 +03:00
|
|
|
rooms_.emplace(room_id, roomWidget);
|
|
|
|
rooms_sort_cache_.push_back(roomWidget);
|
2017-10-01 19:49:36 +03:00
|
|
|
|
2018-04-21 16:34:50 +03:00
|
|
|
if (!info.avatar_url.empty())
|
|
|
|
updateAvatar(room_id, QString::fromStdString(info.avatar_url));
|
2017-10-01 19:49:36 +03:00
|
|
|
|
2017-11-21 18:34:32 +03:00
|
|
|
int pos = contentsLayout_->count() - 1;
|
|
|
|
contentsLayout_->insertWidget(pos, room_item);
|
2017-10-01 19:49:36 +03:00
|
|
|
}
|
|
|
|
|
2017-12-22 01:00:48 +03:00
|
|
|
void
|
|
|
|
RoomList::updateAvatar(const QString &room_id, const QString &url)
|
|
|
|
{
|
2019-08-26 02:24:56 +03:00
|
|
|
emit updateRoomAvatarCb(room_id, url);
|
2017-12-22 01:00:48 +03:00
|
|
|
}
|
|
|
|
|
2017-10-01 19:49:36 +03:00
|
|
|
void
|
|
|
|
RoomList::removeRoom(const QString &room_id, bool reset)
|
|
|
|
{
|
2020-05-01 00:59:17 +03:00
|
|
|
auto roomIt = rooms_.find(room_id);
|
|
|
|
for (auto roomSortIt = rooms_sort_cache_.begin(); roomSortIt != rooms_sort_cache_.end();
|
|
|
|
++roomSortIt) {
|
|
|
|
if (roomIt->second == *roomSortIt) {
|
|
|
|
rooms_sort_cache_.erase(roomSortIt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-01-25 15:34:15 +03:00
|
|
|
rooms_.erase(room_id);
|
2017-10-01 19:49:36 +03:00
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
if (rooms_.empty() || !reset)
|
2017-10-01 19:49:36 +03:00
|
|
|
return;
|
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
auto room = firstRoom();
|
2017-10-01 19:49:36 +03:00
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
if (room.second.isNull())
|
|
|
|
return;
|
|
|
|
|
|
|
|
room.second->setPressedState(true);
|
|
|
|
emit roomChanged(room.first);
|
2017-10-01 19:49:36 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2019-01-25 05:43:54 +03:00
|
|
|
RoomList::updateUnreadMessageCount(const QString &roomid, int count, int highlightedCount)
|
2017-04-15 02:56:04 +03:00
|
|
|
{
|
2018-01-24 21:46:37 +03:00
|
|
|
if (!roomExists(roomid)) {
|
2018-06-14 02:28:35 +03:00
|
|
|
nhlog::ui()->warn("updateUnreadMessageCount: unknown room_id {}",
|
2018-06-09 16:03:14 +03:00
|
|
|
roomid.toStdString());
|
2017-09-10 12:59:21 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-04-15 02:56:04 +03:00
|
|
|
|
2019-01-25 05:43:54 +03:00
|
|
|
rooms_[roomid]->updateUnreadMessageCount(count, highlightedCount);
|
2017-04-15 19:04:02 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
calculateUnreadMessageCount();
|
2020-03-14 03:52:42 +03:00
|
|
|
|
|
|
|
sortRoomsByLastMessage();
|
2017-04-15 19:04:02 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
RoomList::calculateUnreadMessageCount()
|
2017-04-15 19:04:02 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
int total_unread_msgs = 0;
|
2017-04-15 19:04:02 +03:00
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
for (const auto &room : rooms_) {
|
|
|
|
if (!room.second.isNull())
|
|
|
|
total_unread_msgs += room.second->unreadMessageCount();
|
|
|
|
}
|
2017-04-15 19:04:02 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
emit totalUnreadMessageCountUpdated(total_unread_msgs);
|
2017-04-15 02:56:04 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2018-04-21 16:34:50 +03:00
|
|
|
RoomList::initialize(const QMap<QString, RoomInfo> &info)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2018-06-14 02:28:35 +03:00
|
|
|
nhlog::ui()->info("initialize room list");
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
rooms_.clear();
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2020-05-03 23:30:51 +03:00
|
|
|
// prevent flickering and save time sorting over and over again
|
2018-06-28 16:16:43 +03:00
|
|
|
setUpdatesEnabled(false);
|
2018-04-21 16:34:50 +03:00
|
|
|
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());
|
2017-09-10 12:59:21 +03:00
|
|
|
}
|
2017-05-31 19:42:07 +03:00
|
|
|
|
2018-06-28 16:16:43 +03:00
|
|
|
for (auto it = info.begin(); it != info.end(); it++)
|
|
|
|
updateRoomDescription(it.key(), it.value().msgInfo);
|
|
|
|
|
|
|
|
setUpdatesEnabled(true);
|
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
if (rooms_.empty())
|
2017-09-10 12:59:21 +03:00
|
|
|
return;
|
2017-04-24 20:05:24 +03:00
|
|
|
|
2020-02-04 23:16:04 +03:00
|
|
|
sortRoomsByLastMessage();
|
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
auto room = firstRoom();
|
|
|
|
if (room.second.isNull())
|
|
|
|
return;
|
2017-05-07 17:15:38 +03:00
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
room.second->setPressedState(true);
|
|
|
|
emit roomChanged(room.first);
|
2017-05-07 17:15:38 +03:00
|
|
|
}
|
|
|
|
|
2018-04-22 14:19:05 +03:00
|
|
|
void
|
|
|
|
RoomList::cleanupInvites(const std::map<QString, bool> &invites)
|
|
|
|
{
|
2018-04-27 18:19:43 +03:00
|
|
|
if (invites.size() == 0)
|
2018-04-22 14:19:05 +03:00
|
|
|
return;
|
|
|
|
|
2018-04-27 18:19:43 +03:00
|
|
|
utils::erase_if(rooms_, [invites](auto &room) {
|
2018-04-27 22:15:44 +03:00
|
|
|
auto room_id = room.first;
|
|
|
|
auto item = room.second;
|
|
|
|
|
|
|
|
if (!item)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return item->isInvite() && (invites.find(room_id) == invites.end());
|
2018-04-27 18:19:43 +03:00
|
|
|
});
|
2018-04-22 14:19:05 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2018-04-21 16:34:50 +03:00
|
|
|
RoomList::sync(const std::map<QString, RoomInfo> &info)
|
2017-11-21 18:34:32 +03:00
|
|
|
|
2017-05-07 17:15:38 +03:00
|
|
|
{
|
2018-04-21 16:34:50 +03:00
|
|
|
for (const auto &room : info)
|
|
|
|
updateRoom(room.first, room.second);
|
2019-12-13 22:49:27 +03:00
|
|
|
|
|
|
|
if (!info.empty())
|
|
|
|
sortRoomsByLastMessage();
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
RoomList::highlightSelectedRoom(const QString &room_id)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
emit roomChanged(room_id);
|
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
if (!roomExists(room_id)) {
|
2018-06-14 02:28:35 +03:00
|
|
|
nhlog::ui()->warn("roomlist: clicked unknown room_id");
|
2017-09-10 12:59:21 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
for (auto const &room : rooms_) {
|
|
|
|
if (room.second.isNull())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (room.first != room_id) {
|
|
|
|
room.second->setPressedState(false);
|
2017-09-10 12:59:21 +03:00
|
|
|
} else {
|
2018-01-24 21:46:37 +03:00
|
|
|
room.second->setPressedState(true);
|
|
|
|
scrollArea_->ensureWidgetVisible(room.second.data());
|
2017-09-10 12:59:21 +03:00
|
|
|
}
|
|
|
|
}
|
2018-01-09 16:07:32 +03:00
|
|
|
|
|
|
|
selectedRoom_ = room_id;
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2020-01-31 03:39:51 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2019-08-26 02:24:56 +03:00
|
|
|
RoomList::updateRoomAvatar(const QString &roomid, const QString &img)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2018-01-24 21:46:37 +03:00
|
|
|
if (!roomExists(roomid)) {
|
2018-06-14 02:28:35 +03:00
|
|
|
nhlog::ui()->warn("avatar update on non-existent room_id: {}",
|
2018-06-09 16:03:14 +03:00
|
|
|
roomid.toStdString());
|
2017-09-10 12:59:21 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2019-08-26 02:24:56 +03:00
|
|
|
rooms_[roomid]->setAvatar(img);
|
2017-12-22 01:00:48 +03:00
|
|
|
|
|
|
|
// Used to inform other widgets for the new image data.
|
|
|
|
emit roomAvatarChanged(roomid, img);
|
2017-08-06 18:53:31 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
RoomList::updateRoomDescription(const QString &roomid, const DescInfo &info)
|
2017-08-06 18:53:31 +03:00
|
|
|
{
|
2018-01-24 21:46:37 +03:00
|
|
|
if (!roomExists(roomid)) {
|
2018-06-14 02:28:35 +03:00
|
|
|
nhlog::ui()->warn("description update on non-existent room_id: {}, {}",
|
2018-06-09 16:03:14 +03:00
|
|
|
roomid.toStdString(),
|
|
|
|
info.body.toStdString());
|
2017-09-10 12:59:21 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-08-06 18:53:31 +03:00
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
rooms_[roomid]->setDescriptionMessage(info);
|
2017-12-30 18:29:57 +03:00
|
|
|
|
|
|
|
if (underMouse()) {
|
|
|
|
// When the user hover out of the roomlist a sort will be triggered.
|
|
|
|
isSortPending_ = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isSortPending_ = false;
|
|
|
|
|
|
|
|
emit sortRoomsByLastMessage();
|
|
|
|
}
|
|
|
|
|
2020-03-14 16:16:08 +03:00
|
|
|
struct room_sort
|
|
|
|
{
|
2020-05-01 00:59:17 +03:00
|
|
|
bool operator()(const QSharedPointer<RoomInfoListItem> a,
|
|
|
|
const QSharedPointer<RoomInfoListItem> b) const
|
2020-03-14 16:16:08 +03:00
|
|
|
{
|
2020-03-14 02:30:50 +03:00
|
|
|
// 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();
|
2020-03-14 16:16:08 +03:00
|
|
|
if (a_importance != b_importance) {
|
2020-03-14 02:30:50 +03:00
|
|
|
return a_importance > b_importance;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now sort by recency
|
|
|
|
// Zero if empty, otherwise the time that the event occured
|
2020-05-01 00:59:17 +03:00
|
|
|
const uint64_t a_recency =
|
|
|
|
a->lastMessageInfo().userid.isEmpty() ? 0 : a->lastMessageInfo().timestamp;
|
|
|
|
const uint64_t b_recency =
|
|
|
|
b->lastMessageInfo().userid.isEmpty() ? 0 : b->lastMessageInfo().timestamp;
|
2020-03-14 02:30:50 +03:00
|
|
|
return a_recency > b_recency;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-30 18:29:57 +03:00
|
|
|
void
|
|
|
|
RoomList::sortRoomsByLastMessage()
|
|
|
|
{
|
|
|
|
isSortPending_ = false;
|
|
|
|
|
2020-05-04 19:55:08 +03:00
|
|
|
std::stable_sort(begin(rooms_sort_cache_), end(rooms_sort_cache_), room_sort{});
|
2017-12-30 18:29:57 +03:00
|
|
|
|
2020-05-01 00:59:17 +03:00
|
|
|
int newIndex = 0;
|
|
|
|
for (const auto &roomWidget : rooms_sort_cache_) {
|
2020-05-01 01:38:07 +03:00
|
|
|
const auto currentIndex = contentsLayout_->indexOf(roomWidget.data());
|
2017-12-30 18:29:57 +03:00
|
|
|
|
2020-05-01 00:59:17 +03:00
|
|
|
if (currentIndex != newIndex) {
|
2020-05-01 01:38:07 +03:00
|
|
|
contentsLayout_->removeWidget(roomWidget.data());
|
|
|
|
contentsLayout_->insertWidget(newIndex, roomWidget.data());
|
2020-05-01 00:59:17 +03:00
|
|
|
}
|
|
|
|
newIndex++;
|
2017-12-30 18:29:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RoomList::leaveEvent(QEvent *event)
|
|
|
|
{
|
|
|
|
if (isSortPending_)
|
|
|
|
QTimer::singleShot(700, this, &RoomList::sortRoomsByLastMessage);
|
|
|
|
|
|
|
|
QWidget::leaveEvent(event);
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
2017-10-01 19:49:36 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
RoomList::closeJoinRoomDialog(bool isJoining, QString roomAlias)
|
|
|
|
{
|
2018-02-17 19:43:40 +03:00
|
|
|
joinRoomModal_->hide();
|
2017-10-01 19:49:36 +03:00
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
if (isJoining)
|
2018-06-09 16:03:14 +03:00
|
|
|
emit joinRoom(roomAlias);
|
2017-10-01 19:49:36 +03:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:07:32 +03:00
|
|
|
void
|
2021-01-23 02:30:45 +03:00
|
|
|
RoomList::removeFilter(const std::set<QString> &roomsToHide)
|
2018-07-21 13:35:36 +03:00
|
|
|
{
|
2018-07-24 21:43:51 +03:00
|
|
|
setUpdatesEnabled(false);
|
2018-07-21 13:35:36 +03:00
|
|
|
for (int i = 0; i < contentsLayout_->count(); i++) {
|
|
|
|
auto widget =
|
|
|
|
qobject_cast<RoomInfoListItem *>(contentsLayout_->itemAt(i)->widget());
|
2021-01-23 02:30:45 +03:00
|
|
|
if (widget) {
|
|
|
|
if (roomsToHide.find(widget->roomId()) == roomsToHide.end())
|
|
|
|
widget->show();
|
|
|
|
else
|
|
|
|
widget->hide();
|
|
|
|
}
|
2018-07-21 13:35:36 +03:00
|
|
|
}
|
2018-07-24 21:43:51 +03:00
|
|
|
setUpdatesEnabled(true);
|
2018-07-21 13:35:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-01-23 02:30:45 +03:00
|
|
|
RoomList::applyFilter(const std::set<QString> &filter)
|
2018-01-09 16:07:32 +03:00
|
|
|
{
|
2018-07-21 14:57:56 +03:00
|
|
|
// Disabling paint updates will resolve issues with screen flickering on big room lists.
|
|
|
|
setUpdatesEnabled(false);
|
|
|
|
|
2018-01-09 16:07:32 +03:00
|
|
|
for (int i = 0; i < contentsLayout_->count(); i++) {
|
2018-07-21 13:35:36 +03:00
|
|
|
// If filter contains the room for the current RoomInfoListItem,
|
2018-01-09 16:07:32 +03:00
|
|
|
// show the list item, otherwise hide it
|
2018-01-24 21:46:37 +03:00
|
|
|
auto listitem =
|
|
|
|
qobject_cast<RoomInfoListItem *>(contentsLayout_->itemAt(i)->widget());
|
|
|
|
|
|
|
|
if (!listitem)
|
|
|
|
continue;
|
|
|
|
|
2018-07-21 13:35:36 +03:00
|
|
|
if (filter.find(listitem->roomId()) != filter.end())
|
2018-01-24 21:46:37 +03:00
|
|
|
listitem->show();
|
|
|
|
else
|
|
|
|
listitem->hide();
|
2018-01-09 16:07:32 +03:00
|
|
|
}
|
|
|
|
|
2018-07-21 14:57:56 +03:00
|
|
|
setUpdatesEnabled(true);
|
|
|
|
|
2018-07-21 13:35:36 +03:00
|
|
|
// 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();
|
|
|
|
}
|
2018-01-24 21:46:37 +03:00
|
|
|
|
2018-07-21 13:35:36 +03:00
|
|
|
void
|
|
|
|
RoomList::selectFirstVisibleRoom()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < contentsLayout_->count(); i++) {
|
|
|
|
auto item = qobject_cast<RoomInfoListItem *>(contentsLayout_->itemAt(i)->widget());
|
2018-01-24 21:46:37 +03:00
|
|
|
|
2018-07-21 13:35:36 +03:00
|
|
|
if (item && item->isVisible()) {
|
|
|
|
highlightSelectedRoom(item->roomId());
|
|
|
|
break;
|
2018-01-09 16:07:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 23:20:34 +03:00
|
|
|
void
|
|
|
|
RoomList::paintEvent(QPaintEvent *)
|
|
|
|
{
|
|
|
|
QStyleOption opt;
|
|
|
|
opt.init(this);
|
|
|
|
QPainter p(this);
|
|
|
|
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
|
|
|
|
}
|
2017-12-19 23:36:12 +03:00
|
|
|
|
|
|
|
void
|
2018-04-21 16:34:50 +03:00
|
|
|
RoomList::updateRoom(const QString &room_id, const RoomInfo &info)
|
2017-12-19 23:36:12 +03:00
|
|
|
{
|
2018-04-21 16:34:50 +03:00
|
|
|
if (!roomExists(room_id)) {
|
|
|
|
if (info.is_invite)
|
|
|
|
addInvitedRoom(room_id, info);
|
|
|
|
else
|
|
|
|
addRoom(room_id, info);
|
|
|
|
|
|
|
|
return;
|
2017-12-19 23:36:12 +03:00
|
|
|
}
|
2018-04-21 16:34:50 +03:00
|
|
|
|
|
|
|
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();
|
2017-12-19 23:36:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-04-21 16:34:50 +03:00
|
|
|
RoomList::addInvitedRoom(const QString &room_id, const RoomInfo &info)
|
2017-12-19 23:36:12 +03:00
|
|
|
{
|
2020-10-17 01:57:29 +03:00
|
|
|
auto room_item = new RoomInfoListItem(room_id, info, scrollArea_);
|
2018-04-21 16:34:50 +03:00
|
|
|
|
2017-12-19 23:36:12 +03:00
|
|
|
connect(room_item, &RoomInfoListItem::acceptInvite, this, &RoomList::acceptInvite);
|
|
|
|
connect(room_item, &RoomInfoListItem::declineInvite, this, &RoomList::declineInvite);
|
|
|
|
|
2020-05-01 00:59:17 +03:00
|
|
|
QSharedPointer<RoomInfoListItem> roomWidget(room_item);
|
|
|
|
rooms_.emplace(room_id, roomWidget);
|
|
|
|
rooms_sort_cache_.push_back(roomWidget);
|
2017-12-19 23:36:12 +03:00
|
|
|
|
2018-04-21 16:34:50 +03:00
|
|
|
updateAvatar(room_id, QString::fromStdString(info.avatar_url));
|
2017-12-19 23:36:12 +03:00
|
|
|
|
|
|
|
int pos = contentsLayout_->count() - 1;
|
|
|
|
contentsLayout_->insertWidget(pos, room_item);
|
|
|
|
}
|
2018-01-24 21:46:37 +03:00
|
|
|
|
|
|
|
std::pair<QString, QSharedPointer<RoomInfoListItem>>
|
|
|
|
RoomList::firstRoom() const
|
|
|
|
{
|
2020-02-04 23:16:04 +03:00
|
|
|
for (int i = 0; i < contentsLayout_->count(); i++) {
|
|
|
|
auto item = qobject_cast<RoomInfoListItem *>(contentsLayout_->itemAt(i)->widget());
|
2018-01-24 21:46:37 +03:00
|
|
|
|
2020-02-04 23:16:04 +03:00
|
|
|
if (item) {
|
|
|
|
return std::pair<QString, QSharedPointer<RoomInfoListItem>>(
|
|
|
|
item->roomId(), rooms_.at(item->roomId()));
|
|
|
|
}
|
|
|
|
}
|
2018-01-24 21:46:37 +03:00
|
|
|
|
2020-02-04 23:16:04 +03:00
|
|
|
return {};
|
2018-01-24 21:46:37 +03:00
|
|
|
}
|
2018-09-13 19:15:58 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|