From 0028fdfe6c7485515b2535bb73b1d9075f9bf4fe Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Tue, 24 Apr 2018 14:13:05 +0300 Subject: [PATCH] Remove map from room_id to QPixmap from ChatPage The avatars are loaded from cache --- include/Cache.h | 3 +++ include/ChatPage.h | 3 --- src/Cache.cc | 37 +++++++++++++++++++++++++++++++++++++ src/ChatPage.cc | 16 ++++++++-------- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/include/Cache.h b/include/Cache.h index 1faf0677..acad45e2 100644 --- a/include/Cache.h +++ b/include/Cache.h @@ -166,6 +166,9 @@ public: bool isFormatValid(); void setCurrentFormat(); + //! Retrieves the saved room avatar. + QImage getRoomAvatar(const QString &id); + //! Adds a user to the read list for the given event. //! //! There should be only one user id present in a receipt list per room. diff --git a/include/ChatPage.h b/include/ChatPage.h index d0be9ce1..c09675fb 100644 --- a/include/ChatPage.h +++ b/include/ChatPage.h @@ -168,9 +168,6 @@ private: QString current_room_; QString current_community_; - std::map roomAvatars_; - std::map community_avatars_; - UserInfoWidget *user_info_widget_; std::map> communities_; diff --git a/src/Cache.cc b/src/Cache.cc index 129b6696..ea31e749 100644 --- a/src/Cache.cc +++ b/src/Cache.cc @@ -859,6 +859,43 @@ Cache::getInviteRoomTopic(lmdb::txn &txn, lmdb::dbi &db) return QString(); } +QImage +Cache::getRoomAvatar(const QString &room_id) +{ + auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY); + + lmdb::val response; + + if (!lmdb::dbi_get(txn, roomsDb_, lmdb::val(room_id.toStdString()), response)) { + txn.commit(); + return QImage(); + } + + std::string media_url; + + try { + RoomInfo info = json::parse(std::string(response.data(), response.size())); + media_url = std::move(info.avatar_url); + + if (media_url.empty()) { + txn.commit(); + return QImage(); + } + } catch (const json::exception &e) { + qWarning() << "failed to parse room info" << e.what() + << QString::fromStdString(std::string(response.data(), response.size())); + } + + if (!lmdb::dbi_get(txn, mediaDb_, lmdb::val(media_url), response)) { + txn.commit(); + return QImage(); + } + + txn.commit(); + + return QImage::fromData(QByteArray(response.data(), response.size())); +} + std::vector Cache::joinedRooms() { diff --git a/src/ChatPage.cc b/src/ChatPage.cc index 58a6fb4b..59fcdc13 100644 --- a/src/ChatPage.cc +++ b/src/ChatPage.cc @@ -459,7 +459,6 @@ ChatPage::logout() void ChatPage::resetUI() { - roomAvatars_.clear(); room_list_->clear(); top_bar_->reset(); user_info_widget_->reset(); @@ -571,8 +570,6 @@ ChatPage::initialSyncCompleted(const mtx::responses::Sync &response) void ChatPage::updateTopBarAvatar(const QString &roomid, const QPixmap &img) { - roomAvatars_.emplace(roomid, img); - if (current_room_ != roomid) return; @@ -646,15 +643,18 @@ ChatPage::changeTopRoomInfo(const QString &room_id) top_bar_->updateRoomName(name); top_bar_->updateRoomTopic(QString::fromStdString(room_info[room_id].topic)); - if (roomAvatars_.find(room_id) != roomAvatars_.end()) - top_bar_->updateRoomAvatar(roomAvatars_[room_id].toImage()); - else - top_bar_->updateRoomAvatarFromName(name); + auto img = cache_->getRoomAvatar(room_id); + + if (img.isNull()) + top_bar_->updateRoomAvatarFromName(name); + else + top_bar_->updateRoomAvatar(img); - current_room_ = room_id; } catch (const lmdb::error &e) { qWarning() << "failed to change top bar room info" << e.what(); } + + current_room_ = room_id; } void