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/>.
|
|
|
|
*/
|
|
|
|
|
2017-12-22 01:00:48 +03:00
|
|
|
#include <QBuffer>
|
2017-04-06 02:06:42 +03:00
|
|
|
#include <QDebug>
|
2017-10-28 21:24:42 +03:00
|
|
|
#include <QObject>
|
2017-12-30 18:29:57 +03:00
|
|
|
#include <QTimer>
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-12-22 01:00:48 +03:00
|
|
|
#include "Cache.h"
|
2017-10-01 19:49:36 +03:00
|
|
|
#include "MainWindow.h"
|
2017-10-28 15:46:39 +03:00
|
|
|
#include "MatrixClient.h"
|
|
|
|
#include "OverlayModal.h"
|
2017-04-06 02:06:42 +03:00
|
|
|
#include "RoomInfoListItem.h"
|
|
|
|
#include "RoomList.h"
|
2017-10-28 15:46:39 +03:00
|
|
|
#include "RoomSettings.h"
|
|
|
|
#include "RoomState.h"
|
2017-12-30 18:29:57 +03:00
|
|
|
#include "UserSettingsPage.h"
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-12-30 18:29:57 +03:00
|
|
|
RoomList::RoomList(QSharedPointer<MatrixClient> client,
|
|
|
|
QSharedPointer<UserSettings> userSettings,
|
|
|
|
QWidget *parent)
|
2017-08-20 13:47:22 +03:00
|
|
|
: QWidget(parent)
|
|
|
|
, client_(client)
|
2017-12-30 18:29:57 +03:00
|
|
|
, userSettings_{userSettings}
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-11-16 17:33:52 +03:00
|
|
|
setStyleSheet("border: none;");
|
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_->setVerticalScrollBarPolicy(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);
|
2017-09-10 12:59:21 +03:00
|
|
|
|
2017-11-09 00:09:15 +03:00
|
|
|
scrollAreaContents_ = new QWidget(this);
|
2017-09-10 12:59:21 +03: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-22 01:00:48 +03:00
|
|
|
&MatrixClient::roomAvatarRetrieved,
|
2017-09-10 12:59:21 +03:00
|
|
|
this,
|
2017-12-22 01:00:48 +03:00
|
|
|
[=](const QString &room_id,
|
|
|
|
const QPixmap &img,
|
|
|
|
const QString &url,
|
|
|
|
const QByteArray &data) {
|
|
|
|
if (!cache_.isNull())
|
|
|
|
cache_->saveImage(url, data);
|
|
|
|
|
|
|
|
updateRoomAvatar(room_id, img);
|
|
|
|
});
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-10-01 12:11:33 +03:00
|
|
|
RoomList::~RoomList() {}
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
RoomList::clear()
|
2017-04-09 02:17:04 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
rooms_.clear();
|
2017-04-09 02:17:04 +03:00
|
|
|
}
|
|
|
|
|
2017-10-01 19:49:36 +03:00
|
|
|
void
|
2018-01-24 21:46:37 +03:00
|
|
|
RoomList::addRoom(const QSharedPointer<RoomSettings> &settings,
|
2018-01-13 18:15:47 +03:00
|
|
|
const QSharedPointer<RoomState> &state,
|
2017-10-01 19:49:36 +03:00
|
|
|
const QString &room_id)
|
|
|
|
{
|
2018-01-24 21:46:37 +03:00
|
|
|
auto room_item = new RoomInfoListItem(settings, state, room_id, scrollArea_);
|
2017-10-01 19:49:36 +03:00
|
|
|
connect(room_item, &RoomInfoListItem::clicked, this, &RoomList::highlightSelectedRoom);
|
|
|
|
connect(room_item, &RoomInfoListItem::leaveRoom, this, &RoomList::openLeaveRoomDialog);
|
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
rooms_.emplace(room_id, QSharedPointer<RoomInfoListItem>(room_item));
|
2017-10-01 19:49:36 +03:00
|
|
|
|
2018-01-13 18:15:47 +03:00
|
|
|
if (!state->getAvatar().toString().isEmpty())
|
|
|
|
updateAvatar(room_id, state->getAvatar().toString());
|
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)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-01 19:49:36 +03:00
|
|
|
void
|
|
|
|
RoomList::removeRoom(const QString &room_id, bool reset)
|
|
|
|
{
|
2018-01-24 21:46:37 +03:00
|
|
|
rooms_.erase(rooms_.find(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
|
|
|
|
RoomList::updateUnreadMessageCount(const QString &roomid, int count)
|
2017-04-15 02:56:04 +03:00
|
|
|
{
|
2018-01-24 21:46:37 +03:00
|
|
|
if (!roomExists(roomid)) {
|
2017-09-10 12:59:21 +03:00
|
|
|
qWarning() << "UpdateUnreadMessageCount: Unknown roomid";
|
|
|
|
return;
|
|
|
|
}
|
2017-04-15 02:56:04 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
rooms_[roomid]->updateUnreadMessageCount(count);
|
2017-04-15 19:04:02 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
calculateUnreadMessageCount();
|
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-01-24 21:46:37 +03:00
|
|
|
RoomList::setInitialRooms(const std::map<QString, QSharedPointer<RoomSettings>> &settings,
|
|
|
|
const std::map<QString, QSharedPointer<RoomState>> &states)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
rooms_.clear();
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
if (settings.size() != states.size()) {
|
|
|
|
qWarning() << "Initializing room list";
|
|
|
|
qWarning() << "Different number of room states and room settings";
|
|
|
|
return;
|
|
|
|
}
|
2017-05-31 19:42:07 +03:00
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
for (auto const &state : states)
|
|
|
|
addRoom(settings.at(state.first), state.second, state.first);
|
2017-04-06 02:06:42 +03:00
|
|
|
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-10-01 19:49:36 +03:00
|
|
|
void
|
|
|
|
RoomList::openLeaveRoomDialog(const QString &room_id)
|
|
|
|
{
|
2017-10-07 20:09:34 +03:00
|
|
|
if (leaveRoomDialog_.isNull()) {
|
2017-11-30 21:02:46 +03:00
|
|
|
leaveRoomDialog_ = QSharedPointer<dialogs::LeaveRoom>(new dialogs::LeaveRoom(this));
|
2017-10-01 19:49:36 +03:00
|
|
|
|
2017-10-07 20:09:34 +03:00
|
|
|
connect(leaveRoomDialog_.data(),
|
2017-11-30 21:02:46 +03:00
|
|
|
&dialogs::LeaveRoom::closing,
|
2017-10-07 20:09:34 +03:00
|
|
|
this,
|
|
|
|
[=](bool leaving) { closeLeaveRoomDialog(leaving, room_id); });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (leaveRoomModal_.isNull()) {
|
|
|
|
leaveRoomModal_ = QSharedPointer<OverlayModal>(
|
|
|
|
new OverlayModal(MainWindow::instance(), leaveRoomDialog_.data()));
|
|
|
|
leaveRoomModal_->setDuration(0);
|
|
|
|
leaveRoomModal_->setColor(QColor(30, 30, 30, 170));
|
|
|
|
}
|
2017-10-01 19:49:36 +03:00
|
|
|
|
2017-10-07 20:09:34 +03:00
|
|
|
leaveRoomModal_->fadeIn();
|
2017-10-01 19:49:36 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2018-01-24 21:46:37 +03:00
|
|
|
RoomList::sync(const std::map<QString, QSharedPointer<RoomState>> &states,
|
|
|
|
const std::map<QString, QSharedPointer<RoomSettings>> &settings)
|
2017-11-21 18:34:32 +03:00
|
|
|
|
2017-05-07 17:15:38 +03:00
|
|
|
{
|
2018-01-24 21:46:37 +03:00
|
|
|
for (auto const &state : states) {
|
|
|
|
if (!roomExists(state.first))
|
|
|
|
addRoom(settings.at(state.first), state.second, state.first);
|
2017-05-07 17:15:38 +03:00
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
auto room = rooms_[state.first];
|
2017-05-07 17:15:38 +03:00
|
|
|
|
2018-01-13 18:15:47 +03:00
|
|
|
auto current_avatar = room->state()->getAvatar();
|
2018-01-24 21:46:37 +03:00
|
|
|
auto new_avatar = state.second->getAvatar();
|
2017-05-07 17:15:38 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
if (current_avatar != new_avatar && !new_avatar.toString().isEmpty())
|
2018-01-24 21:46:37 +03:00
|
|
|
updateAvatar(state.first, new_avatar.toString());
|
2017-05-07 17:15:38 +03:00
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
room->setState(state.second);
|
2017-09-10 12:59:21 +03:00
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-11-24 01:10:58 +03:00
|
|
|
void
|
|
|
|
RoomList::clearRoomMessageCount(const QString &room_id)
|
|
|
|
{
|
2018-01-24 21:46:37 +03:00
|
|
|
if (!roomExists(room_id))
|
2017-11-24 01:10:58 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
auto room = rooms_[room_id];
|
|
|
|
room->clearUnreadMessageCount();
|
|
|
|
|
|
|
|
calculateUnreadMessageCount();
|
|
|
|
}
|
|
|
|
|
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)) {
|
2017-09-10 12:59:21 +03:00
|
|
|
qDebug() << "RoomList: clicked unknown roomid";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-24 01:10:58 +03:00
|
|
|
clearRoomMessageCount(room_id);
|
2017-09-10 12:59:21 +03:00
|
|
|
|
|
|
|
calculateUnreadMessageCount();
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
RoomList::updateRoomAvatar(const QString &roomid, const QPixmap &img)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2018-01-24 21:46:37 +03:00
|
|
|
if (!roomExists(roomid)) {
|
2017-09-10 12:59:21 +03:00
|
|
|
qWarning() << "Avatar update on non existent room" << roomid;
|
|
|
|
return;
|
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
rooms_[roomid]->setAvatar(img.toImage());
|
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)) {
|
2017-09-10 12:59:21 +03:00
|
|
|
qWarning() << "Description update on non existent room" << roomid << info.body;
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2017-12-30 18:44:47 +03:00
|
|
|
times.emplace(room->lastMessageInfo().datetime.toMSecsSinceEpoch(), room);
|
2017-12-30 18:29:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 02:06:42 +03:00
|
|
|
}
|
2017-10-01 19:49:36 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
RoomList::closeJoinRoomDialog(bool isJoining, QString roomAlias)
|
|
|
|
{
|
|
|
|
joinRoomModal_->fadeOut();
|
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
if (isJoining)
|
2017-10-01 19:49:36 +03:00
|
|
|
client_->joinRoom(roomAlias);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RoomList::closeLeaveRoomDialog(bool leaving, const QString &room_id)
|
|
|
|
{
|
2017-10-07 20:09:34 +03:00
|
|
|
leaveRoomModal_->fadeOut();
|
2017-10-01 19:49:36 +03:00
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
if (leaving)
|
2017-10-01 19:49:36 +03:00
|
|
|
client_->leaveRoom(room_id);
|
|
|
|
}
|
2017-11-25 23:20:34 +03:00
|
|
|
|
2018-01-09 16:07:32 +03:00
|
|
|
void
|
2018-01-24 21:46:37 +03:00
|
|
|
RoomList::setFilterRooms(bool isFilteringEnabled)
|
2018-01-09 16:07:32 +03: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
|
2018-01-24 21:46:37 +03:00
|
|
|
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 16:07:32 +03:00
|
|
|
}
|
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
if (isFilteringEnabled && !filterItemExists(selectedRoom_)) {
|
2018-01-09 16:07:32 +03:00
|
|
|
RoomInfoListItem *firstVisibleRoom = nullptr;
|
2018-01-24 21:46:37 +03:00
|
|
|
|
2018-01-09 16:07:32 +03:00
|
|
|
for (int i = 0; i < contentsLayout_->count(); i++) {
|
|
|
|
QWidget *item = contentsLayout_->itemAt(i)->widget();
|
2018-01-24 21:46:37 +03:00
|
|
|
|
2018-01-09 16:07:32 +03:00
|
|
|
if (item != nullptr && item->isVisible()) {
|
2018-01-24 21:46:37 +03:00
|
|
|
firstVisibleRoom = qobject_cast<RoomInfoListItem *>(item);
|
2018-01-09 16:07:32 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-01-24 21:46:37 +03:00
|
|
|
|
|
|
|
if (firstVisibleRoom != nullptr)
|
2018-01-09 16:07:32 +03:00
|
|
|
highlightSelectedRoom(firstVisibleRoom->roomId());
|
|
|
|
} else {
|
2018-01-24 21:46:37 +03:00
|
|
|
scrollArea_->ensureWidgetVisible(rooms_[selectedRoom_].data());
|
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
|
|
|
|
RoomList::syncInvites(const std::map<std::string, mtx::responses::InvitedRoom> &rooms)
|
|
|
|
{
|
|
|
|
for (auto it = rooms.cbegin(); it != rooms.cend(); ++it) {
|
|
|
|
const auto room_id = QString::fromStdString(it->first);
|
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
if (!roomExists(room_id))
|
2017-12-19 23:36:12 +03:00
|
|
|
addInvitedRoom(room_id, it->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:07:32 +03:00
|
|
|
void
|
2018-01-24 21:46:37 +03:00
|
|
|
RoomList::setRoomFilter(std::vector<QString> room_ids)
|
2018-01-09 16:07:32 +03:00
|
|
|
{
|
|
|
|
roomFilter_ = room_ids;
|
|
|
|
setFilterRooms(true);
|
|
|
|
}
|
|
|
|
|
2017-12-19 23:36:12 +03:00
|
|
|
void
|
|
|
|
RoomList::addInvitedRoom(const QString &room_id, const mtx::responses::InvitedRoom &room)
|
|
|
|
{
|
|
|
|
auto room_item = new RoomInfoListItem(room_id, room, scrollArea_);
|
|
|
|
connect(room_item, &RoomInfoListItem::acceptInvite, this, &RoomList::acceptInvite);
|
|
|
|
connect(room_item, &RoomInfoListItem::declineInvite, this, &RoomList::declineInvite);
|
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
rooms_.emplace(room_id, QSharedPointer<RoomInfoListItem>(room_item));
|
2017-12-19 23:36:12 +03:00
|
|
|
|
2017-12-22 01:00:48 +03:00
|
|
|
updateAvatar(room_id, QString::fromStdString(room.avatar()));
|
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
|
|
|
|
{
|
|
|
|
auto firstRoom = rooms_.begin();
|
|
|
|
|
|
|
|
while (firstRoom->second.isNull() && firstRoom != rooms_.end())
|
|
|
|
firstRoom++;
|
|
|
|
|
|
|
|
return std::pair<QString, QSharedPointer<RoomInfoListItem>>(firstRoom->first,
|
|
|
|
firstRoom->second);
|
|
|
|
}
|