nheko/src/RoomList.cc

422 lines
12 KiB
C++
Raw Normal View History

2017-04-06 01:06:42 +02: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/>.
*/
2017-12-21 23:00:48 +01:00
#include <QBuffer>
2017-04-06 01:06:42 +02:00
#include <QDebug>
2017-10-28 20:24:42 +02:00
#include <QObject>
#include <QTimer>
2017-04-06 01:06:42 +02:00
2017-12-21 23:00:48 +01:00
#include "Cache.h"
#include "MainWindow.h"
2017-10-28 14:46:39 +02:00
#include "MatrixClient.h"
#include "OverlayModal.h"
2017-04-06 01:06:42 +02:00
#include "RoomInfoListItem.h"
#include "RoomList.h"
#include "UserSettingsPage.h"
2018-04-29 14:42:40 +02:00
#include "Utils.h"
2017-04-06 01:06:42 +02:00
RoomList::RoomList(QSharedPointer<MatrixClient> client,
QSharedPointer<UserSettings> userSettings,
QWidget *parent)
2017-08-20 12:47:22 +02:00
: QWidget(parent)
, client_(client)
, userSettings_{userSettings}
2017-04-06 01:06:42 +02:00
{
setStyleSheet("border: none;");
2017-09-10 11:59:21 +02:00
topLayout_ = new QVBoxLayout(this);
topLayout_->setSpacing(0);
topLayout_->setMargin(0);
scrollArea_ = new QScrollArea(this);
scrollArea_->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scrollArea_->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scrollArea_->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
scrollArea_->setWidgetResizable(true);
2018-01-09 14:07:32 +01:00
scrollArea_->setAlignment(Qt::AlignLeading | Qt::AlignTop | Qt::AlignVCenter);
2017-09-10 11:59:21 +02:00
2017-11-08 22:09:15 +01:00
scrollAreaContents_ = new QWidget(this);
2017-09-10 11:59:21 +02:00
contentsLayout_ = new QVBoxLayout(scrollAreaContents_);
contentsLayout_->setSpacing(0);
contentsLayout_->setMargin(0);
contentsLayout_->addStretch(1);
scrollArea_->setWidget(scrollAreaContents_);
topLayout_->addWidget(scrollArea_);
connect(client_.data(),
2017-12-21 23:00:48 +01:00
&MatrixClient::roomAvatarRetrieved,
2017-09-10 11:59:21 +02:00
this,
[this](const QString &room_id,
const QPixmap &img,
const QString &url,
const QByteArray &data) {
2017-12-21 23:00:48 +01:00
if (!cache_.isNull())
cache_->saveImage(url, data);
updateRoomAvatar(room_id, img);
});
2017-04-06 01:06:42 +02:00
}
2017-08-20 12:47:22 +02:00
void
2018-04-21 15:34:50 +02:00
RoomList::addRoom(const QString &room_id, const RoomInfo &info)
{
2018-04-21 15:34:50 +02:00
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);
});
rooms_.emplace(room_id, QSharedPointer<RoomInfoListItem>(room_item));
2018-04-21 15:34:50 +02:00
if (!info.avatar_url.empty())
updateAvatar(room_id, QString::fromStdString(info.avatar_url));
int pos = contentsLayout_->count() - 1;
contentsLayout_->insertWidget(pos, room_item);
}
2017-12-21 23:00:48 +01:00
void
RoomList::updateAvatar(const QString &room_id, const QString &url)
{
if (url.isEmpty())
return;
QByteArray savedImgData;
if (!cache_.isNull())
savedImgData = cache_->image(url);
if (savedImgData.isEmpty()) {
client_->fetchRoomAvatar(room_id, url);
} else {
QPixmap img;
img.loadFromData(savedImgData);
updateRoomAvatar(room_id, img);
}
}
void
RoomList::removeRoom(const QString &room_id, bool reset)
{
2018-01-25 13:34:15 +01:00
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);
}
2017-08-20 12:47:22 +02:00
void
RoomList::updateUnreadMessageCount(const QString &roomid, int count)
{
if (!roomExists(roomid)) {
2017-09-10 11:59:21 +02:00
qWarning() << "UpdateUnreadMessageCount: Unknown roomid";
return;
}
2017-09-10 11:59:21 +02:00
rooms_[roomid]->updateUnreadMessageCount(count);
2017-09-10 11:59:21 +02:00
calculateUnreadMessageCount();
}
2017-08-20 12:47:22 +02:00
void
RoomList::calculateUnreadMessageCount()
{
2017-09-10 11:59:21 +02:00
int total_unread_msgs = 0;
for (const auto &room : rooms_) {
if (!room.second.isNull())
total_unread_msgs += room.second->unreadMessageCount();
}
2017-09-10 11:59:21 +02:00
emit totalUnreadMessageCountUpdated(total_unread_msgs);
}
2017-08-20 12:47:22 +02:00
void
2018-04-21 15:34:50 +02:00
RoomList::initialize(const QMap<QString, RoomInfo> &info)
2017-04-06 01:06:42 +02:00
{
2018-04-21 15:34:50 +02:00
qDebug() << "initialize room list";
2017-09-10 11:59:21 +02:00
rooms_.clear();
2017-04-06 01:06:42 +02:00
2018-04-21 15:34:50 +02: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 11:59:21 +02:00
}
2017-05-31 18:42:07 +02:00
if (rooms_.empty())
2017-09-10 11:59:21 +02:00
return;
auto room = firstRoom();
if (room.second.isNull())
return;
room.second->setPressedState(true);
emit roomChanged(room.first);
}
2018-04-22 13:19:05 +02:00
void
RoomList::cleanupInvites(const std::map<QString, bool> &invites)
{
2018-04-27 17:19:43 +02:00
if (invites.size() == 0)
2018-04-22 13:19:05 +02:00
return;
2018-04-27 17:19:43 +02:00
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());
2018-04-27 17:19:43 +02:00
});
2018-04-22 13:19:05 +02:00
}
2017-08-20 12:47:22 +02:00
void
2018-04-21 15:34:50 +02:00
RoomList::sync(const std::map<QString, RoomInfo> &info)
{
2018-04-21 15:34:50 +02:00
for (const auto &room : info)
updateRoom(room.first, room.second);
2017-04-06 01:06:42 +02:00
}
2017-08-20 12:47:22 +02:00
void
RoomList::highlightSelectedRoom(const QString &room_id)
2017-04-06 01:06:42 +02:00
{
2017-09-10 11:59:21 +02:00
emit roomChanged(room_id);
if (!roomExists(room_id)) {
2017-09-10 11:59:21 +02:00
qDebug() << "RoomList: clicked unknown roomid";
return;
}
for (auto const &room : rooms_) {
if (room.second.isNull())
continue;
if (room.first != room_id) {
room.second->setPressedState(false);
2017-09-10 11:59:21 +02:00
} else {
room.second->setPressedState(true);
scrollArea_->ensureWidgetVisible(room.second.data());
2017-09-10 11:59:21 +02:00
}
}
2018-01-09 14:07:32 +01:00
selectedRoom_ = room_id;
2017-04-06 01:06:42 +02:00
}
2017-08-20 12:47:22 +02:00
void
RoomList::updateRoomAvatar(const QString &roomid, const QPixmap &img)
2017-04-06 01:06:42 +02:00
{
if (!roomExists(roomid)) {
2017-09-10 11:59:21 +02:00
qWarning() << "Avatar update on non existent room" << roomid;
return;
}
2017-04-06 01:06:42 +02:00
rooms_[roomid]->setAvatar(img.toImage());
2017-12-21 23:00:48 +01:00
// Used to inform other widgets for the new image data.
emit roomAvatarChanged(roomid, img);
}
2017-08-20 12:47:22 +02:00
void
RoomList::updateRoomDescription(const QString &roomid, const DescInfo &info)
{
if (!roomExists(roomid)) {
2017-09-10 11:59:21 +02:00
qWarning() << "Description update on non existent room" << roomid << info.body;
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();
}
void
RoomList::sortRoomsByLastMessage()
{
if (!userSettings_->isOrderingEnabled())
return;
isSortPending_ = false;
std::multimap<uint64_t, RoomInfoListItem *, std::greater<uint64_t>> times;
for (int ii = 0; ii < contentsLayout_->count(); ++ii) {
auto room = qobject_cast<RoomInfoListItem *>(contentsLayout_->itemAt(ii)->widget());
if (!room)
continue;
// Not a room message.
if (room->lastMessageInfo().userid.isEmpty())
times.emplace(0, room);
else
times.emplace(room->lastMessageInfo().datetime.toMSecsSinceEpoch(), room);
}
for (auto it = times.cbegin(); it != times.cend(); ++it) {
const auto roomWidget = it->second;
const auto currentIndex = contentsLayout_->indexOf(roomWidget);
const auto newIndex = std::distance(times.cbegin(), it);
if (currentIndex == newIndex)
continue;
contentsLayout_->removeWidget(roomWidget);
contentsLayout_->insertWidget(newIndex, roomWidget);
}
}
void
RoomList::leaveEvent(QEvent *event)
{
if (isSortPending_)
QTimer::singleShot(700, this, &RoomList::sortRoomsByLastMessage);
QWidget::leaveEvent(event);
2017-04-06 01:06:42 +02:00
}
void
RoomList::closeJoinRoomDialog(bool isJoining, QString roomAlias)
{
joinRoomModal_->hide();
if (isJoining)
client_->joinRoom(roomAlias);
}
2018-01-09 14:07:32 +01:00
void
RoomList::setFilterRooms(bool isFilteringEnabled)
2018-01-09 14:07:32 +01:00
{
for (int i = 0; i < contentsLayout_->count(); i++) {
// If roomFilter_ 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 (!isFilteringEnabled || filterItemExists(listitem->roomId()))
listitem->show();
else
listitem->hide();
2018-01-09 14:07:32 +01:00
}
if (isFilteringEnabled && !filterItemExists(selectedRoom_)) {
2018-01-09 14:07:32 +01:00
RoomInfoListItem *firstVisibleRoom = nullptr;
2018-01-09 14:07:32 +01:00
for (int i = 0; i < contentsLayout_->count(); i++) {
QWidget *item = contentsLayout_->itemAt(i)->widget();
2018-01-09 14:07:32 +01:00
if (item != nullptr && item->isVisible()) {
firstVisibleRoom = qobject_cast<RoomInfoListItem *>(item);
2018-01-09 14:07:32 +01:00
break;
}
}
if (firstVisibleRoom != nullptr)
2018-01-09 14:07:32 +01:00
highlightSelectedRoom(firstVisibleRoom->roomId());
} else {
scrollArea_->ensureWidgetVisible(rooms_[selectedRoom_].data());
2018-01-09 14:07:32 +01:00
}
}
2017-11-25 21:20:34 +01:00
void
RoomList::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
2017-12-19 21:36:12 +01:00
void
2018-04-21 15:34:50 +02:00
RoomList::updateRoom(const QString &room_id, const RoomInfo &info)
2017-12-19 21:36:12 +01:00
{
2018-04-21 15:34:50 +02:00
if (!roomExists(room_id)) {
if (info.is_invite)
addInvitedRoom(room_id, info);
else
addRoom(room_id, info);
return;
2017-12-19 21:36:12 +01:00
}
2018-04-21 15:34:50 +02: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 21:36:12 +01:00
}
2018-01-09 14:07:32 +01:00
void
RoomList::setRoomFilter(std::vector<QString> room_ids)
2018-01-09 14:07:32 +01:00
{
roomFilter_ = room_ids;
setFilterRooms(true);
}
2017-12-19 21:36:12 +01:00
void
2018-04-21 15:34:50 +02:00
RoomList::addInvitedRoom(const QString &room_id, const RoomInfo &info)
2017-12-19 21:36:12 +01:00
{
2018-04-21 15:34:50 +02:00
auto room_item = new RoomInfoListItem(room_id, info, scrollArea_);
2017-12-19 21:36:12 +01:00
connect(room_item, &RoomInfoListItem::acceptInvite, this, &RoomList::acceptInvite);
connect(room_item, &RoomInfoListItem::declineInvite, this, &RoomList::declineInvite);
rooms_.emplace(room_id, QSharedPointer<RoomInfoListItem>(room_item));
2017-12-19 21:36:12 +01:00
2018-04-21 15:34:50 +02:00
updateAvatar(room_id, QString::fromStdString(info.avatar_url));
2017-12-19 21:36:12 +01:00
int pos = contentsLayout_->count() - 1;
contentsLayout_->insertWidget(pos, room_item);
}
std::pair<QString, QSharedPointer<RoomInfoListItem>>
RoomList::firstRoom() const
{
auto firstRoom = rooms_.begin();
while (firstRoom->second.isNull() && firstRoom != rooms_.end())
firstRoom++;
return std::pair<QString, QSharedPointer<RoomInfoListItem>>(firstRoom->first,
firstRoom->second);
}