2018-07-21 13:35:36 +03:00
|
|
|
#include "CommunitiesList.h"
|
2018-07-22 19:48:58 +03:00
|
|
|
#include "Cache.h"
|
2018-07-17 16:37:25 +03:00
|
|
|
#include "Logging.h"
|
2018-05-08 18:43:56 +03:00
|
|
|
#include "MatrixClient.h"
|
2018-01-09 16:07:32 +03:00
|
|
|
|
|
|
|
#include <QLabel>
|
|
|
|
|
2018-05-08 18:43:56 +03:00
|
|
|
CommunitiesList::CommunitiesList(QWidget *parent)
|
2018-01-09 16:07:32 +03:00
|
|
|
: QWidget(parent)
|
|
|
|
{
|
|
|
|
QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
|
|
|
|
sizePolicy.setHorizontalStretch(0);
|
|
|
|
sizePolicy.setVerticalStretch(1);
|
|
|
|
setSizePolicy(sizePolicy);
|
|
|
|
|
|
|
|
setStyleSheet("border-style: none;");
|
|
|
|
|
|
|
|
topLayout_ = new QVBoxLayout(this);
|
|
|
|
topLayout_->setSpacing(0);
|
|
|
|
topLayout_->setMargin(0);
|
|
|
|
|
|
|
|
setFixedWidth(ui::sidebar::CommunitiesSidebarSize);
|
|
|
|
|
|
|
|
scrollArea_ = new QScrollArea(this);
|
|
|
|
scrollArea_->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
scrollArea_->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
scrollArea_->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
|
|
|
|
scrollArea_->setWidgetResizable(true);
|
|
|
|
scrollArea_->setAlignment(Qt::AlignLeading | Qt::AlignTop | Qt::AlignVCenter);
|
|
|
|
|
|
|
|
scrollAreaContents_ = new QWidget();
|
|
|
|
|
|
|
|
contentsLayout_ = new QVBoxLayout(scrollAreaContents_);
|
|
|
|
contentsLayout_->setSpacing(0);
|
|
|
|
contentsLayout_->setMargin(0);
|
|
|
|
|
2018-04-28 15:27:12 +03:00
|
|
|
addGlobalItem();
|
2018-01-09 16:07:32 +03:00
|
|
|
contentsLayout_->addStretch(1);
|
|
|
|
|
|
|
|
scrollArea_->setWidget(scrollAreaContents_);
|
|
|
|
topLayout_->addWidget(scrollArea_);
|
|
|
|
|
2018-06-09 16:03:14 +03:00
|
|
|
connect(
|
|
|
|
this, &CommunitiesList::avatarRetrieved, this, &CommunitiesList::updateCommunityAvatar);
|
2018-01-09 16:07:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-07-14 12:08:16 +03:00
|
|
|
CommunitiesList::setCommunities(const mtx::responses::JoinedGroups &response)
|
2018-01-09 16:07:32 +03:00
|
|
|
{
|
|
|
|
communities_.clear();
|
|
|
|
|
2018-04-28 15:27:12 +03:00
|
|
|
addGlobalItem();
|
2018-01-09 16:07:32 +03:00
|
|
|
|
2018-07-14 12:08:16 +03:00
|
|
|
for (const auto &group : response.groups)
|
|
|
|
addCommunity(group);
|
2018-01-09 16:07:32 +03:00
|
|
|
|
2018-04-28 15:27:12 +03:00
|
|
|
communities_["world"]->setPressedState(true);
|
2018-01-09 16:07:32 +03:00
|
|
|
emit communityChanged("world");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-07-14 12:08:16 +03:00
|
|
|
CommunitiesList::addCommunity(const std::string &group_id)
|
2018-01-09 16:07:32 +03:00
|
|
|
{
|
2018-07-14 12:08:16 +03:00
|
|
|
const auto id = QString::fromStdString(group_id);
|
2018-01-09 16:07:32 +03:00
|
|
|
|
2018-07-14 12:08:16 +03:00
|
|
|
CommunitiesListItem *list_item = new CommunitiesListItem(id, scrollArea_);
|
|
|
|
communities_.emplace(id, QSharedPointer<CommunitiesListItem>(list_item));
|
2018-01-09 16:07:32 +03:00
|
|
|
contentsLayout_->insertWidget(contentsLayout_->count() - 1, list_item);
|
|
|
|
|
2018-07-14 12:08:16 +03:00
|
|
|
connect(this,
|
|
|
|
&CommunitiesList::groupProfileRetrieved,
|
|
|
|
this,
|
|
|
|
[this](const QString &id, const mtx::responses::GroupProfile &profile) {
|
|
|
|
if (communities_.find(id) == communities_.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
communities_.at(id)->setName(QString::fromStdString(profile.name));
|
|
|
|
|
|
|
|
if (!profile.avatar_url.empty())
|
|
|
|
fetchCommunityAvatar(id,
|
|
|
|
QString::fromStdString(profile.avatar_url));
|
|
|
|
});
|
|
|
|
connect(this,
|
|
|
|
&CommunitiesList::groupRoomsRetrieved,
|
|
|
|
this,
|
2018-07-21 13:35:36 +03:00
|
|
|
[this](const QString &id, const std::map<QString, bool> &rooms) {
|
2018-07-14 12:08:16 +03:00
|
|
|
if (communities_.find(id) == communities_.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
communities_.at(id)->setRooms(rooms);
|
|
|
|
});
|
2018-01-09 16:07:32 +03:00
|
|
|
connect(list_item,
|
|
|
|
&CommunitiesListItem::clicked,
|
|
|
|
this,
|
|
|
|
&CommunitiesList::highlightSelectedCommunity);
|
2018-07-14 12:08:16 +03:00
|
|
|
|
2018-07-15 19:09:08 +03:00
|
|
|
http::client()->group_profile(
|
2018-07-14 12:08:16 +03:00
|
|
|
group_id, [id, this](const mtx::responses::GroupProfile &res, mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit groupProfileRetrieved(id, res);
|
|
|
|
});
|
|
|
|
|
2018-07-15 19:09:08 +03:00
|
|
|
http::client()->group_rooms(
|
2018-07-14 12:08:16 +03:00
|
|
|
group_id, [id, this](const nlohmann::json &res, mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-21 13:35:36 +03:00
|
|
|
std::map<QString, bool> room_ids;
|
2018-07-14 12:08:16 +03:00
|
|
|
for (const auto &room : res.at("chunk"))
|
2018-07-21 13:35:36 +03:00
|
|
|
room_ids.emplace(QString::fromStdString(room.at("room_id")), true);
|
2018-07-14 12:08:16 +03:00
|
|
|
|
|
|
|
emit groupRoomsRetrieved(id, room_ids);
|
|
|
|
});
|
2018-01-09 16:07:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CommunitiesList::updateCommunityAvatar(const QString &community_id, const QPixmap &img)
|
|
|
|
{
|
2018-01-24 21:46:37 +03:00
|
|
|
if (!communityExists(community_id)) {
|
2018-01-09 16:07:32 +03:00
|
|
|
qWarning() << "Avatar update on nonexistent community" << community_id;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-14 12:08:16 +03:00
|
|
|
communities_.at(community_id)->setAvatar(img.toImage());
|
2018-01-09 16:07:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CommunitiesList::highlightSelectedCommunity(const QString &community_id)
|
|
|
|
{
|
2018-01-24 21:46:37 +03:00
|
|
|
if (!communityExists(community_id)) {
|
2018-01-09 16:07:32 +03:00
|
|
|
qDebug() << "CommunitiesList: clicked unknown community";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
emit communityChanged(community_id);
|
|
|
|
|
|
|
|
for (const auto &community : communities_) {
|
|
|
|
if (community.first != community_id) {
|
|
|
|
community.second->setPressedState(false);
|
2018-01-09 16:07:32 +03:00
|
|
|
} else {
|
2018-01-24 21:46:37 +03:00
|
|
|
community.second->setPressedState(true);
|
|
|
|
scrollArea_->ensureWidgetVisible(community.second.data());
|
2018-01-09 16:07:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-09 16:03:14 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
CommunitiesList::fetchCommunityAvatar(const QString &id, const QString &avatarUrl)
|
|
|
|
{
|
|
|
|
auto savedImgData = cache::client()->image(avatarUrl);
|
|
|
|
if (!savedImgData.isNull()) {
|
|
|
|
QPixmap pix;
|
|
|
|
pix.loadFromData(savedImgData);
|
|
|
|
emit avatarRetrieved(id, pix);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-10 20:03:45 +03:00
|
|
|
if (avatarUrl.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2018-06-09 16:03:14 +03:00
|
|
|
mtx::http::ThumbOpts opts;
|
|
|
|
opts.mxc_url = avatarUrl.toStdString();
|
2018-07-15 19:09:08 +03:00
|
|
|
http::client()->get_thumbnail(
|
2018-06-09 16:03:14 +03:00
|
|
|
opts, [this, opts, id](const std::string &res, mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
2018-06-14 02:28:35 +03:00
|
|
|
nhlog::net()->warn("failed to download avatar: {} - ({} {})",
|
2018-06-14 09:36:41 +03:00
|
|
|
opts.mxc_url,
|
|
|
|
mtx::errors::to_string(err->matrix_error.errcode),
|
|
|
|
err->matrix_error.error);
|
2018-06-09 16:03:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cache::client()->saveImage(opts.mxc_url, res);
|
|
|
|
|
|
|
|
auto data = QByteArray(res.data(), res.size());
|
|
|
|
|
|
|
|
QPixmap pix;
|
|
|
|
pix.loadFromData(data);
|
|
|
|
|
|
|
|
emit avatarRetrieved(id, pix);
|
|
|
|
});
|
|
|
|
}
|
2018-07-14 12:08:16 +03:00
|
|
|
|
2018-07-21 13:35:36 +03:00
|
|
|
std::map<QString, bool>
|
2018-07-14 12:08:16 +03:00
|
|
|
CommunitiesList::roomList(const QString &id) const
|
|
|
|
{
|
|
|
|
if (communityExists(id))
|
|
|
|
return communities_.at(id)->rooms();
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|