Remove map from room_id to QPixmap from ChatPage

The avatars are loaded from cache
This commit is contained in:
Konstantinos Sideris 2018-04-24 14:13:05 +03:00
parent 4f6ffb6e73
commit 0028fdfe6c
4 changed files with 48 additions and 11 deletions

View File

@ -166,6 +166,9 @@ public:
bool isFormatValid(); bool isFormatValid();
void setCurrentFormat(); void setCurrentFormat();
//! Retrieves the saved room avatar.
QImage getRoomAvatar(const QString &id);
//! Adds a user to the read list for the given event. //! 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. //! There should be only one user id present in a receipt list per room.

View File

@ -168,9 +168,6 @@ private:
QString current_room_; QString current_room_;
QString current_community_; QString current_community_;
std::map<QString, QPixmap> roomAvatars_;
std::map<QString, QPixmap> community_avatars_;
UserInfoWidget *user_info_widget_; UserInfoWidget *user_info_widget_;
std::map<QString, QSharedPointer<Community>> communities_; std::map<QString, QSharedPointer<Community>> communities_;

View File

@ -859,6 +859,43 @@ Cache::getInviteRoomTopic(lmdb::txn &txn, lmdb::dbi &db)
return QString(); 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<std::string> std::vector<std::string>
Cache::joinedRooms() Cache::joinedRooms()
{ {

View File

@ -459,7 +459,6 @@ ChatPage::logout()
void void
ChatPage::resetUI() ChatPage::resetUI()
{ {
roomAvatars_.clear();
room_list_->clear(); room_list_->clear();
top_bar_->reset(); top_bar_->reset();
user_info_widget_->reset(); user_info_widget_->reset();
@ -571,8 +570,6 @@ ChatPage::initialSyncCompleted(const mtx::responses::Sync &response)
void void
ChatPage::updateTopBarAvatar(const QString &roomid, const QPixmap &img) ChatPage::updateTopBarAvatar(const QString &roomid, const QPixmap &img)
{ {
roomAvatars_.emplace(roomid, img);
if (current_room_ != roomid) if (current_room_ != roomid)
return; return;
@ -646,15 +643,18 @@ ChatPage::changeTopRoomInfo(const QString &room_id)
top_bar_->updateRoomName(name); top_bar_->updateRoomName(name);
top_bar_->updateRoomTopic(QString::fromStdString(room_info[room_id].topic)); top_bar_->updateRoomTopic(QString::fromStdString(room_info[room_id].topic));
if (roomAvatars_.find(room_id) != roomAvatars_.end()) auto img = cache_->getRoomAvatar(room_id);
top_bar_->updateRoomAvatar(roomAvatars_[room_id].toImage());
else if (img.isNull())
top_bar_->updateRoomAvatarFromName(name); top_bar_->updateRoomAvatarFromName(name);
else
top_bar_->updateRoomAvatar(img);
current_room_ = room_id;
} catch (const lmdb::error &e) { } catch (const lmdb::error &e) {
qWarning() << "failed to change top bar room info" << e.what(); qWarning() << "failed to change top bar room info" << e.what();
} }
current_room_ = room_id;
} }
void void