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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QDebug>
|
2017-10-28 21:24:42 +03:00
|
|
|
#include <QObject>
|
2017-04-06 02:06:42 +03:00
|
|
|
|
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-04-06 02:06:42 +03:00
|
|
|
|
2017-04-11 17:45:47 +03:00
|
|
|
RoomList::RoomList(QSharedPointer<MatrixClient> client, QWidget *parent)
|
2017-08-20 13:47:22 +03:00
|
|
|
: QWidget(parent)
|
|
|
|
, client_(client)
|
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);
|
|
|
|
scrollArea_->setAlignment(Qt::AlignLeading | Qt::AlignLeft | Qt::AlignVCenter);
|
|
|
|
|
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(),
|
|
|
|
SIGNAL(roomAvatarRetrieved(const QString &, const QPixmap &)),
|
|
|
|
this,
|
|
|
|
SLOT(updateRoomAvatar(const QString &, const QPixmap &)));
|
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
|
2017-11-21 18:34:32 +03:00
|
|
|
RoomList::addRoom(const QMap<QString, QSharedPointer<RoomSettings>> &settings,
|
2017-10-01 19:49:36 +03:00
|
|
|
const RoomState &state,
|
|
|
|
const QString &room_id)
|
|
|
|
{
|
2017-11-21 18:34:32 +03:00
|
|
|
RoomInfoListItem *room_item =
|
|
|
|
new RoomInfoListItem(settings[room_id], 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);
|
|
|
|
|
|
|
|
rooms_.insert(room_id, QSharedPointer<RoomInfoListItem>(room_item));
|
|
|
|
|
2017-11-21 18:34:32 +03:00
|
|
|
if (!state.getAvatar().toString().isEmpty())
|
|
|
|
client_->fetchRoomAvatar(room_id, state.getAvatar());
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RoomList::removeRoom(const QString &room_id, bool reset)
|
|
|
|
{
|
|
|
|
rooms_.remove(room_id);
|
|
|
|
|
|
|
|
if (rooms_.isEmpty() || !reset)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto first_room = rooms_.first();
|
|
|
|
first_room->setPressedState(true);
|
|
|
|
|
|
|
|
emit roomChanged(rooms_.firstKey());
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
RoomList::updateUnreadMessageCount(const QString &roomid, int count)
|
2017-04-15 02:56:04 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
if (!rooms_.contains(roomid)) {
|
|
|
|
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
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
for (const auto &room : rooms_)
|
|
|
|
total_unread_msgs += room->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
|
|
|
|
RoomList::setInitialRooms(const QMap<QString, QSharedPointer<RoomSettings>> &settings,
|
2017-09-10 12:59:21 +03:00
|
|
|
const QMap<QString, 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
|
|
|
|
2017-10-28 20:46:34 +03:00
|
|
|
for (auto it = states.constBegin(); it != states.constEnd(); ++it) {
|
2017-11-21 18:34:32 +03:00
|
|
|
const auto room_id = it.key();
|
|
|
|
const auto state = it.value();
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-11-21 18:34:32 +03:00
|
|
|
addRoom(settings, state, room_id);
|
2017-09-10 12:59:21 +03:00
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
if (rooms_.isEmpty())
|
|
|
|
return;
|
2017-04-24 20:05:24 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
auto first_room = rooms_.first();
|
|
|
|
first_room->setPressedState(true);
|
2017-05-07 17:15:38 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
emit roomChanged(rooms_.firstKey());
|
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
|
2017-11-21 18:34:32 +03:00
|
|
|
RoomList::sync(const QMap<QString, RoomState> &states,
|
|
|
|
QMap<QString, QSharedPointer<RoomSettings>> &settings)
|
|
|
|
|
2017-05-07 17:15:38 +03:00
|
|
|
{
|
2017-10-28 20:46:34 +03:00
|
|
|
for (auto it = states.constBegin(); it != states.constEnd(); ++it) {
|
2017-09-10 12:59:21 +03:00
|
|
|
auto room_id = it.key();
|
|
|
|
auto state = it.value();
|
2017-05-07 17:15:38 +03:00
|
|
|
|
2017-10-01 19:49:36 +03:00
|
|
|
if (!rooms_.contains(room_id)) {
|
2017-11-21 18:34:32 +03:00
|
|
|
settings.insert(room_id,
|
|
|
|
QSharedPointer<RoomSettings>(new RoomSettings(room_id)));
|
|
|
|
addRoom(settings, state, room_id);
|
2017-10-01 19:49:36 +03:00
|
|
|
}
|
2017-05-07 17:15:38 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
auto room = rooms_[room_id];
|
2017-05-07 17:15:38 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
auto current_avatar = room->state().getAvatar();
|
|
|
|
auto new_avatar = state.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())
|
|
|
|
client_->fetchRoomAvatar(room_id, new_avatar);
|
2017-05-07 17:15:38 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
room->setState(state);
|
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-11-24 01:10:58 +03:00
|
|
|
void
|
|
|
|
RoomList::clearRoomMessageCount(const QString &room_id)
|
|
|
|
{
|
|
|
|
if (!rooms_.contains(room_id))
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (!rooms_.contains(room_id)) {
|
|
|
|
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();
|
|
|
|
|
2017-10-28 20:46:34 +03:00
|
|
|
for (auto it = rooms_.constBegin(); it != rooms_.constEnd(); ++it) {
|
2017-09-10 12:59:21 +03:00
|
|
|
if (it.key() != room_id) {
|
|
|
|
it.value()->setPressedState(false);
|
|
|
|
} else {
|
|
|
|
it.value()->setPressedState(true);
|
|
|
|
scrollArea_->ensureWidgetVisible(
|
|
|
|
qobject_cast<QWidget *>(it.value().data()));
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
if (!rooms_.contains(roomid)) {
|
|
|
|
qWarning() << "Avatar update on non existent room" << roomid;
|
|
|
|
return;
|
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
rooms_.value(roomid)->setAvatar(img.toImage());
|
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
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
if (!rooms_.contains(roomid)) {
|
|
|
|
qWarning() << "Description update on non existent room" << roomid << info.body;
|
|
|
|
return;
|
|
|
|
}
|
2017-08-06 18:53:31 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
rooms_.value(roomid)->setDescriptionMessage(info);
|
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();
|
|
|
|
|
|
|
|
if (isJoining) {
|
|
|
|
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
|
|
|
|
|
|
|
if (leaving) {
|
|
|
|
client_->leaveRoom(room_id);
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
}
|