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 "ui_RoomList.h"
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QLabel>
|
2017-04-10 01:34:53 +03:00
|
|
|
#include <QRegularExpression>
|
2017-04-06 02:06:42 +03:00
|
|
|
|
|
|
|
#include "RoomInfoListItem.h"
|
|
|
|
#include "RoomList.h"
|
|
|
|
#include "Sync.h"
|
|
|
|
|
2017-04-11 17:45:47 +03:00
|
|
|
RoomList::RoomList(QSharedPointer<MatrixClient> client, QWidget *parent)
|
2017-04-06 02:06:42 +03:00
|
|
|
: QWidget(parent)
|
|
|
|
, ui(new Ui::RoomList)
|
2017-04-11 17:45:47 +03:00
|
|
|
, client_(client)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2017-04-09 02:17:04 +03:00
|
|
|
ui->scrollVerticalLayout->addStretch(1);
|
2017-04-11 17:45:47 +03:00
|
|
|
|
2017-04-14 15:13:09 +03:00
|
|
|
setStyleSheet("border-top: none");
|
|
|
|
|
2017-04-11 17:45:47 +03:00
|
|
|
connect(client_.data(),
|
|
|
|
SIGNAL(roomAvatarRetrieved(const QString &, const QPixmap &)),
|
|
|
|
this,
|
|
|
|
SLOT(updateRoomAvatar(const QString &, const QPixmap &)));
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RoomList::~RoomList()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2017-04-09 02:17:04 +03:00
|
|
|
void RoomList::clear()
|
|
|
|
{
|
|
|
|
for (const auto &room : rooms_)
|
|
|
|
room->deleteLater();
|
|
|
|
|
|
|
|
rooms_.clear();
|
|
|
|
}
|
|
|
|
|
2017-04-06 02:06:42 +03:00
|
|
|
RoomInfo RoomList::extractRoomInfo(const State &room_state)
|
|
|
|
{
|
|
|
|
RoomInfo info;
|
|
|
|
|
|
|
|
auto events = room_state.events();
|
|
|
|
|
2017-04-09 02:17:04 +03:00
|
|
|
for (const auto &event : events) {
|
|
|
|
if (event.type() == "m.room.name") {
|
|
|
|
info.setName(event.content().value("name").toString());
|
|
|
|
} else if (event.type() == "m.room.topic") {
|
|
|
|
info.setTopic(event.content().value("topic").toString());
|
|
|
|
} else if (event.type() == "m.room.avatar") {
|
|
|
|
info.setAvatarUrl(QUrl(event.content().value("url").toString()));
|
|
|
|
} else if (event.type() == "m.room.canonical_alias") {
|
2017-04-06 02:06:42 +03:00
|
|
|
if (info.name().isEmpty())
|
2017-04-09 02:17:04 +03:00
|
|
|
info.setName(event.content().value("alias").toString());
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 01:34:53 +03:00
|
|
|
// Sanitize info for print.
|
2017-04-10 18:17:41 +03:00
|
|
|
info.setTopic(info.topic().simplified());
|
|
|
|
info.setName(info.name().simplified());
|
2017-04-10 01:34:53 +03:00
|
|
|
|
2017-04-06 02:06:42 +03:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2017-04-15 02:56:04 +03:00
|
|
|
void RoomList::updateUnreadMessageCount(const QString &roomid, int count)
|
|
|
|
{
|
|
|
|
if (!rooms_.contains(roomid)) {
|
|
|
|
qWarning() << "UpdateUnreadMessageCount: Unknown roomid";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rooms_[roomid]->updateUnreadMessageCount(count);
|
2017-04-15 19:04:02 +03:00
|
|
|
|
|
|
|
calculateUnreadMessageCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RoomList::calculateUnreadMessageCount()
|
|
|
|
{
|
|
|
|
int total_unread_msgs = 0;
|
|
|
|
|
|
|
|
for (const auto &room : rooms_)
|
|
|
|
total_unread_msgs += room->unreadMessageCount();
|
|
|
|
|
|
|
|
emit totalUnreadMessageCountUpdated(total_unread_msgs);
|
2017-04-15 02:56:04 +03:00
|
|
|
}
|
|
|
|
|
2017-04-06 02:06:42 +03:00
|
|
|
void RoomList::setInitialRooms(const Rooms &rooms)
|
|
|
|
{
|
2017-04-09 02:17:04 +03:00
|
|
|
rooms_.clear();
|
2017-04-06 02:06:42 +03:00
|
|
|
|
|
|
|
for (auto it = rooms.join().constBegin(); it != rooms.join().constEnd(); it++) {
|
|
|
|
RoomInfo info = RoomList::extractRoomInfo(it.value().state());
|
|
|
|
info.setId(it.key());
|
|
|
|
|
|
|
|
if (info.name().isEmpty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!info.avatarUrl().isEmpty())
|
2017-04-11 17:45:47 +03:00
|
|
|
client_->fetchRoomAvatar(info.id(), info.avatarUrl());
|
2017-04-06 02:06:42 +03:00
|
|
|
|
|
|
|
RoomInfoListItem *room_item = new RoomInfoListItem(info, ui->scrollArea);
|
|
|
|
connect(room_item,
|
|
|
|
SIGNAL(clicked(const RoomInfo &)),
|
|
|
|
this,
|
|
|
|
SLOT(highlightSelectedRoom(const RoomInfo &)));
|
|
|
|
|
2017-04-09 02:17:04 +03:00
|
|
|
rooms_.insert(it.key(), room_item);
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-04-09 02:17:04 +03:00
|
|
|
int pos = ui->scrollVerticalLayout->count() - 1;
|
|
|
|
ui->scrollVerticalLayout->insertWidget(pos, room_item);
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Move this into its own function.
|
2017-04-09 02:17:04 +03:00
|
|
|
auto first_room = rooms_.first();
|
2017-04-06 02:06:42 +03:00
|
|
|
first_room->setPressedState(true);
|
|
|
|
emit roomChanged(first_room->info());
|
|
|
|
}
|
|
|
|
|
|
|
|
void RoomList::highlightSelectedRoom(const RoomInfo &info)
|
|
|
|
{
|
|
|
|
emit roomChanged(info);
|
|
|
|
|
2017-04-15 02:56:04 +03:00
|
|
|
if (!rooms_.contains(info.id())) {
|
|
|
|
qDebug() << "RoomList: clicked unknown roomid";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Send a read receipt for the last event.
|
|
|
|
auto room = rooms_[info.id()];
|
|
|
|
room->clearUnreadMessageCount();
|
|
|
|
|
2017-04-15 19:04:02 +03:00
|
|
|
calculateUnreadMessageCount();
|
|
|
|
|
2017-04-09 02:17:04 +03:00
|
|
|
for (auto it = rooms_.constBegin(); it != rooms_.constEnd(); it++) {
|
2017-04-06 02:06:42 +03:00
|
|
|
if (it.key() != info.id())
|
|
|
|
it.value()->setPressedState(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-11 17:45:47 +03:00
|
|
|
void RoomList::updateRoomAvatar(const QString &roomid, const QPixmap &img)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-04-09 02:17:04 +03:00
|
|
|
if (!rooms_.contains(roomid)) {
|
2017-04-06 02:06:42 +03:00
|
|
|
qDebug() << "Avatar update on non existent room" << roomid;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-09 02:17:04 +03:00
|
|
|
auto list_item = rooms_.value(roomid);
|
2017-04-11 17:45:47 +03:00
|
|
|
list_item->setAvatar(img.toImage());
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|