From 3929261a8e41bd9a716b168269208c08491277a8 Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Sat, 13 Jan 2018 17:15:47 +0200 Subject: [PATCH] Use QSharedPointer to save RoomStates --- include/Cache.h | 7 +- include/ChatPage.h | 11 +-- include/RoomInfoListItem.h | 14 ++-- include/RoomList.h | 8 +-- src/Cache.cc | 11 +-- src/ChatPage.cc | 134 ++++++++++++++++++------------------- src/RoomInfoListItem.cc | 9 +-- src/RoomList.cc | 21 +++--- 8 files changed, 108 insertions(+), 107 deletions(-) diff --git a/include/Cache.h b/include/Cache.h index ae58e418..761c6060 100644 --- a/include/Cache.h +++ b/include/Cache.h @@ -69,7 +69,8 @@ class Cache public: Cache(const QString &userId); - void setState(const QString &nextBatchToken, const QMap &states); + void setState(const QString &nextBatchToken, + const QMap> &states); bool isInitialized() const; QString nextBatchToken() const; @@ -107,7 +108,9 @@ public: private: void setNextBatchToken(lmdb::txn &txn, const QString &token); - void insertRoomState(lmdb::txn &txn, const QString &roomid, const RoomState &state); + void insertRoomState(lmdb::txn &txn, + const QString &roomid, + const QSharedPointer &state); lmdb::env env_; lmdb::dbi stateDb_; diff --git a/include/ChatPage.h b/include/ChatPage.h index 01fb4c50..93b98a76 100644 --- a/include/ChatPage.h +++ b/include/ChatPage.h @@ -97,7 +97,7 @@ private: static ChatPage *instance_; using UserID = QString; - using RoomStates = QMap; + using RoomStates = QMap>; using Membership = mtx::events::StateEvent; using Memberships = std::map; @@ -107,8 +107,9 @@ private: void removeLeftRooms(const LeftRooms &rooms); void updateJoinedRooms(const JoinedRooms &rooms); - RoomStates generateMembershipDifference(const JoinedRooms &rooms, - const RoomStates &states) const; + QMap> generateMembershipDifference( + const JoinedRooms &rooms, + const RoomStates &states) const; void updateTypingUsers(const QString &roomid, const std::vector &user_ids); @@ -165,8 +166,8 @@ private: UserInfoWidget *user_info_widget_; - QMap state_manager_; - QMap> settingsManager_; + RoomStates roomStates_; + QMap> roomSettings_; QMap> communityManager_; diff --git a/include/RoomInfoListItem.h b/include/RoomInfoListItem.h index 5cfea783..8222dc2b 100644 --- a/include/RoomInfoListItem.h +++ b/include/RoomInfoListItem.h @@ -61,7 +61,7 @@ class RoomInfoListItem : public QWidget public: RoomInfoListItem(QSharedPointer settings, - RoomState state, + QSharedPointer state, QString room_id, QWidget *parent = 0); @@ -71,11 +71,15 @@ public: void updateUnreadMessageCount(int count); void clearUnreadMessageCount(); - void setState(const RoomState &state); + void setState(QSharedPointer state) + { + state_ = state; + update(); + } QString roomId(); bool isPressed() const { return isPressed_; }; - RoomState state() const { return state_; }; + QSharedPointer state() const { return state_; } int unreadMessageCount() const { return unreadMsgCount_; }; void setAvatar(const QImage &avatar_image); @@ -127,7 +131,7 @@ private: QString roomName() { if (roomType_ == RoomType::Joined) - return state_.getName(); + return state_->getName(); return roomName_; } @@ -145,7 +149,7 @@ private: RoomType roomType_ = RoomType::Joined; // State information for the joined rooms. - RoomState state_; + QSharedPointer state_; // State information for the invited rooms. mtx::responses::InvitedRoom invitedRoom_; diff --git a/include/RoomList.h b/include/RoomList.h index d10cf5db..43b95f12 100644 --- a/include/RoomList.h +++ b/include/RoomList.h @@ -51,16 +51,16 @@ public: void setCache(QSharedPointer cache) { cache_ = cache; } void setInitialRooms(const QMap> &settings, - const QMap &states); - void sync(const QMap &states, - QMap> &settings); + const QMap> &states); + void sync(const QMap> &states, + const QMap> &settings); void syncInvites(const std::map &rooms); void clear(); void updateAvatar(const QString &room_id, const QString &url); void addRoom(const QMap> &settings, - const RoomState &state, + const QSharedPointer &state, const QString &room_id); void addInvitedRoom(const QString &room_id, const mtx::responses::InvitedRoom &room); void removeRoom(const QString &room_id, bool reset); diff --git a/src/Cache.cc b/src/Cache.cc index e4e700b2..e27c4083 100644 --- a/src/Cache.cc +++ b/src/Cache.cc @@ -150,7 +150,8 @@ Cache::image(const QString &url) const } void -Cache::setState(const QString &nextBatchToken, const QMap &states) +Cache::setState(const QString &nextBatchToken, + const QMap> &states) { if (!isMounted_) return; @@ -173,14 +174,16 @@ Cache::setState(const QString &nextBatchToken, const QMap &s } void -Cache::insertRoomState(lmdb::txn &txn, const QString &roomid, const RoomState &state) +Cache::insertRoomState(lmdb::txn &txn, + const QString &roomid, + const QSharedPointer &state) { - auto stateEvents = state.serialize(); + auto stateEvents = state->serialize(); auto id = roomid.toUtf8(); lmdb::dbi_put(txn, roomDb_, lmdb::val(id.data(), id.size()), lmdb::val(stateEvents)); - for (const auto &membership : state.memberships) { + for (const auto &membership : state->memberships) { lmdb::dbi membersDb = lmdb::dbi::open(txn, roomid.toStdString().c_str(), MDB_CREATE); diff --git a/src/ChatPage.cc b/src/ChatPage.cc index f49c0a08..4e57e280 100644 --- a/src/ChatPage.cc +++ b/src/ChatPage.cc @@ -179,13 +179,13 @@ ChatPage::ChatPage(QSharedPointer client, &TimelineViewManager::unreadMessages, this, [=](const QString &roomid, int count) { - if (!settingsManager_.contains(roomid)) { + if (!roomSettings_.contains(roomid)) { qWarning() << "RoomId does not have settings" << roomid; room_list_->updateUnreadMessageCount(roomid, count); return; } - if (settingsManager_[roomid]->isNotificationsEnabled()) + if (roomSettings_[roomid]->isNotificationsEnabled()) room_list_->updateUnreadMessageCount(roomid, count); }); @@ -405,8 +405,8 @@ ChatPage::resetUI() { room_avatars_.clear(); room_list_->clear(); - settingsManager_.clear(); - state_manager_.clear(); + roomSettings_.clear(); + roomStates_.clear(); top_bar_->reset(); user_info_widget_->reset(); view_manager_->clearAll(); @@ -493,11 +493,11 @@ ChatPage::syncCompleted(const mtx::responses::Sync &response) const auto nextBatchToken = QString::fromStdString(response.next_batch); - auto stateDiff = generateMembershipDifference(response.rooms.join, state_manager_); + auto stateDiff = generateMembershipDifference(response.rooms.join, roomStates_); QtConcurrent::run(cache_.data(), &Cache::setState, nextBatchToken, stateDiff); QtConcurrent::run(cache_.data(), &Cache::setInvites, response.rooms.invite); - room_list_->sync(state_manager_, settingsManager_); + room_list_->sync(roomStates_, roomSettings_); room_list_->syncInvites(response.rooms.invite); view_manager_->sync(response.rooms); @@ -512,26 +512,26 @@ ChatPage::initialSyncCompleted(const mtx::responses::Sync &response) auto joined = response.rooms.join; for (auto it = joined.cbegin(); it != joined.cend(); ++it) { - RoomState room_state; + auto roomState = QSharedPointer(new RoomState); // Build the current state from the timeline and state events. - room_state.updateFromEvents(it->second.state.events); - room_state.updateFromEvents(it->second.timeline.events); + roomState->updateFromEvents(it->second.state.events); + roomState->updateFromEvents(it->second.timeline.events); // Remove redundant memberships. - room_state.removeLeaveMemberships(); + roomState->removeLeaveMemberships(); // Resolve room name and avatar. e.g in case of one-to-one chats. - room_state.resolveName(); - room_state.resolveAvatar(); + roomState->resolveName(); + roomState->resolveAvatar(); const auto room_id = QString::fromStdString(it->first); - state_manager_.insert(room_id, room_state); - settingsManager_.insert(room_id, - QSharedPointer(new RoomSettings(room_id))); + roomStates_.insert(room_id, roomState); + roomSettings_.insert(room_id, + QSharedPointer(new RoomSettings(room_id))); - for (const auto membership : room_state.memberships) { + for (const auto membership : roomState->memberships) { updateUserDisplayName(membership.second); updateUserAvatarUrl(membership.second); } @@ -542,14 +542,14 @@ ChatPage::initialSyncCompleted(const mtx::responses::Sync &response) QtConcurrent::run(cache_.data(), &Cache::setState, QString::fromStdString(response.next_batch), - state_manager_); + roomStates_); QtConcurrent::run(cache_.data(), &Cache::setInvites, response.rooms.invite); // Populate timelines with messages. view_manager_->initialize(response.rooms); // Initialize room list. - room_list_->setInitialRooms(settingsManager_, state_manager_); + room_list_->setInitialRooms(roomSettings_, roomStates_); room_list_->syncInvites(response.rooms.invite); client_->setNextBatchToken(QString::fromStdString(response.next_batch)); @@ -597,19 +597,19 @@ ChatPage::updateOwnCommunitiesInfo(const QList &own_communities) void ChatPage::changeTopRoomInfo(const QString &room_id) { - if (!state_manager_.contains(room_id)) + if (!roomStates_.contains(room_id)) return; - auto state = state_manager_[room_id]; + auto state = roomStates_[room_id]; - top_bar_->updateRoomName(state.getName()); - top_bar_->updateRoomTopic(state.getTopic()); - top_bar_->setRoomSettings(settingsManager_[room_id]); + top_bar_->updateRoomName(state->getName()); + top_bar_->updateRoomTopic(state->getTopic()); + top_bar_->setRoomSettings(roomSettings_[room_id]); if (room_avatars_.contains(room_id)) top_bar_->updateRoomAvatar(room_avatars_.value(room_id).toImage()); else - top_bar_->updateRoomAvatarFromName(state.getName()); + top_bar_->updateRoomAvatarFromName(state->getName()); current_room_ = room_id; } @@ -638,22 +638,22 @@ ChatPage::loadStateFromCache() auto rooms = cache_->states(); for (auto it = rooms.constBegin(); it != rooms.constEnd(); ++it) { - RoomState room_state = it.value(); + auto roomState = QSharedPointer(new RoomState(it.value())); // Clean up and prepare state for use. - room_state.removeLeaveMemberships(); - room_state.resolveName(); - room_state.resolveAvatar(); + roomState->removeLeaveMemberships(); + roomState->resolveName(); + roomState->resolveAvatar(); // Save the current room state. - state_manager_.insert(it.key(), room_state); + roomStates_.insert(it.key(), roomState); // Create or restore the settings for this room. - settingsManager_.insert(it.key(), - QSharedPointer(new RoomSettings(it.key()))); + roomSettings_.insert(it.key(), + QSharedPointer(new RoomSettings(it.key()))); // Resolve user avatars. - for (const auto membership : room_state.memberships) { + for (const auto membership : roomState->memberships) { updateUserDisplayName(membership.second); updateUserAvatarUrl(membership.second); } @@ -663,7 +663,7 @@ ChatPage::loadStateFromCache() view_manager_->initialize(rooms.keys()); // Initialize room list from the restored state and settings. - room_list_->setInitialRooms(settingsManager_, state_manager_); + room_list_->setInitialRooms(roomSettings_, roomStates_); room_list_->syncInvites(cache_->invites()); // Check periodically if the timelines have been loaded. @@ -706,12 +706,12 @@ ChatPage::showQuickSwitcher() QMap rooms; - for (auto it = state_manager_.constBegin(); it != state_manager_.constEnd(); ++it) { + for (auto it = roomStates_.constBegin(); it != roomStates_.constEnd(); ++it) { QString deambiguator = - QString::fromStdString(it.value().canonical_alias.content.alias); + QString::fromStdString(it.value()->canonical_alias.content.alias); if (deambiguator == "") deambiguator = it.key(); - rooms.insert(it.value().getName() + " (" + deambiguator + ")", it.key()); + rooms.insert(it.value()->getName() + " (" + deambiguator + ")", it.key()); } quickSwitcher_->setRoomList(rooms); @@ -721,14 +721,14 @@ ChatPage::showQuickSwitcher() void ChatPage::addRoom(const QString &room_id) { - if (!state_manager_.contains(room_id)) { - RoomState room_state; + if (!roomStates_.contains(room_id)) { + auto room_state = QSharedPointer(new RoomState); - state_manager_.insert(room_id, room_state); - settingsManager_.insert(room_id, - QSharedPointer(new RoomSettings(room_id))); + roomStates_.insert(room_id, room_state); + roomSettings_.insert(room_id, + QSharedPointer(new RoomSettings(room_id))); - room_list_->addRoom(settingsManager_, state_manager_[room_id], room_id); + room_list_->addRoom(roomSettings_, roomStates_[room_id], room_id); room_list_->highlightSelectedRoom(room_id); changeTopRoomInfo(room_id); @@ -738,8 +738,8 @@ ChatPage::addRoom(const QString &room_id) void ChatPage::removeRoom(const QString &room_id) { - state_manager_.remove(room_id); - settingsManager_.remove(room_id); + roomStates_.remove(room_id); + roomSettings_.remove(room_id); try { cache_->removeRoom(room_id); cache_->removeInvite(room_id); @@ -820,7 +820,7 @@ ChatPage::removeLeftRooms(const std::map for (auto it = rooms.cbegin(); it != rooms.cend(); ++it) { const auto room_id = QString::fromStdString(it->first); - if (state_manager_.contains(room_id)) + if (roomStates_.contains(room_id)) removeRoom(room_id); } } @@ -843,25 +843,25 @@ ChatPage::updateJoinedRooms(const std::mapsecond.timeline; // Merge the new updates for rooms that we are tracking. - if (state_manager_.contains(roomid)) { - auto oldState = &state_manager_[roomid]; + if (roomStates_.contains(roomid)) { + auto oldState = roomStates_[roomid]; oldState->updateFromEvents(newStateEvents.events); oldState->updateFromEvents(newTimelineEvents.events); oldState->resolveName(); oldState->resolveAvatar(); } else { // Build the current state from the timeline and state events. - RoomState room_state; - room_state.updateFromEvents(newStateEvents.events); - room_state.updateFromEvents(newTimelineEvents.events); + auto roomState = QSharedPointer(new RoomState); + roomState->updateFromEvents(newStateEvents.events); + roomState->updateFromEvents(newTimelineEvents.events); // Resolve room name and avatar. e.g in case of one-to-one chats. - room_state.resolveName(); - room_state.resolveAvatar(); + roomState->resolveName(); + roomState->resolveAvatar(); - state_manager_.insert(roomid, room_state); + roomStates_.insert(roomid, roomState); - settingsManager_.insert( + roomSettings_.insert( roomid, QSharedPointer(new RoomSettings(roomid))); view_manager_->addRoom(it->second, roomid); @@ -877,12 +877,12 @@ ChatPage::updateJoinedRooms(const std::map +QMap> ChatPage::generateMembershipDifference( const std::map &rooms, - const QMap &states) const + const QMap> &states) const { - QMap stateDiff; + QMap> stateDiff; for (auto it = rooms.cbegin(); it != rooms.cend(); ++it) { const auto room_id = QString::fromStdString(it->first); @@ -897,16 +897,16 @@ ChatPage::generateMembershipDifference( for (auto mm = timelineMemberships.cbegin(); mm != timelineMemberships.cend(); ++mm) all_memberships.emplace(mm->first, mm->second); - RoomState local; - local.aliases = states[room_id].aliases; - local.avatar = states[room_id].avatar; - local.canonical_alias = states[room_id].canonical_alias; - local.history_visibility = states[room_id].history_visibility; - local.join_rules = states[room_id].join_rules; - local.name = states[room_id].name; - local.power_levels = states[room_id].power_levels; - local.topic = states[room_id].topic; - local.memberships = all_memberships; + auto local = QSharedPointer(new RoomState); + local->aliases = states[room_id]->aliases; + local->avatar = states[room_id]->avatar; + local->canonical_alias = states[room_id]->canonical_alias; + local->history_visibility = states[room_id]->history_visibility; + local->join_rules = states[room_id]->join_rules; + local->name = states[room_id]->name; + local->power_levels = states[room_id]->power_levels; + local->topic = states[room_id]->topic; + local->memberships = all_memberships; stateDiff.insert(room_id, local); } diff --git a/src/RoomInfoListItem.cc b/src/RoomInfoListItem.cc index 3e84051e..3388e5d7 100644 --- a/src/RoomInfoListItem.cc +++ b/src/RoomInfoListItem.cc @@ -69,7 +69,7 @@ RoomInfoListItem::RoomInfoListItem(QString room_id, } RoomInfoListItem::RoomInfoListItem(QSharedPointer settings, - RoomState state, + QSharedPointer state, QString room_id, QWidget *parent) : QWidget(parent) @@ -323,13 +323,6 @@ RoomInfoListItem::setPressedState(bool state) } } -void -RoomInfoListItem::setState(const RoomState &new_state) -{ - state_ = new_state; - update(); -} - void RoomInfoListItem::contextMenuEvent(QContextMenuEvent *event) { diff --git a/src/RoomList.cc b/src/RoomList.cc index 30be6cf6..61acfb83 100644 --- a/src/RoomList.cc +++ b/src/RoomList.cc @@ -83,7 +83,7 @@ RoomList::clear() void RoomList::addRoom(const QMap> &settings, - const RoomState &state, + const QSharedPointer &state, const QString &room_id) { RoomInfoListItem *room_item = @@ -93,8 +93,8 @@ RoomList::addRoom(const QMap> &settings, rooms_.insert(room_id, QSharedPointer(room_item)); - if (!state.getAvatar().toString().isEmpty()) - updateAvatar(room_id, state.getAvatar().toString()); + if (!state->getAvatar().toString().isEmpty()) + updateAvatar(room_id, state->getAvatar().toString()); int pos = contentsLayout_->count() - 1; contentsLayout_->insertWidget(pos, room_item); @@ -161,7 +161,7 @@ RoomList::calculateUnreadMessageCount() void RoomList::setInitialRooms(const QMap> &settings, - const QMap &states) + const QMap> &states) { rooms_.clear(); @@ -212,24 +212,21 @@ RoomList::openLeaveRoomDialog(const QString &room_id) } void -RoomList::sync(const QMap &states, - QMap> &settings) +RoomList::sync(const QMap> &states, + const QMap> &settings) { for (auto it = states.constBegin(); it != states.constEnd(); ++it) { auto room_id = it.key(); auto state = it.value(); - if (!rooms_.contains(room_id)) { - settings.insert(room_id, - QSharedPointer(new RoomSettings(room_id))); + if (!rooms_.contains(room_id)) addRoom(settings, state, room_id); - } auto room = rooms_[room_id]; - auto current_avatar = room->state().getAvatar(); - auto new_avatar = state.getAvatar(); + auto current_avatar = room->state()->getAvatar(); + auto new_avatar = state->getAvatar(); if (current_avatar != new_avatar && !new_avatar.toString().isEmpty()) updateAvatar(room_id, new_avatar.toString());