Strip whitespace from room names for QuickSwitcher

This commit is contained in:
Konstantinos Sideris 2018-04-22 11:27:00 +03:00
parent 7a16e05b14
commit 6d0bc0c05e
3 changed files with 27 additions and 46 deletions

View file

@ -136,7 +136,7 @@ public:
void populateMembers(); void populateMembers();
std::vector<std::string> joinedRooms(); std::vector<std::string> joinedRooms();
QMap<QString, RoomInfo> roomInfo(); QMap<QString, RoomInfo> roomInfo(bool withInvites = true);
//! Calculate & return the name of the room. //! Calculate & return the name of the room.
QString getRoomName(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb); QString getRoomName(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb);
@ -331,9 +331,6 @@ private:
void saveInvites(lmdb::txn &txn, void saveInvites(lmdb::txn &txn,
const std::map<std::string, mtx::responses::InvitedRoom> &rooms); const std::map<std::string, mtx::responses::InvitedRoom> &rooms);
//! Remove any saved invites that are not found in the input.
void removeStaleInvites(lmdb::txn &txn, const std::map<std::string, bool> &curr);
//! Sends signals for the rooms that are removed. //! Sends signals for the rooms that are removed.
void removeLeftRooms(lmdb::txn &txn, void removeLeftRooms(lmdb::txn &txn,
const std::map<std::string, mtx::responses::LeftRoom> &rooms) const std::map<std::string, mtx::responses::LeftRoom> &rooms)

View file

@ -390,31 +390,11 @@ Cache::saveState(const mtx::responses::Sync &res)
saveInvites(txn, res.rooms.invite); saveInvites(txn, res.rooms.invite);
std::map<std::string, bool> invites;
for (const auto &invite : res.rooms.invite)
invites.emplace(std::move(invite.first), true);
// removeStaleInvites(txn, invites);
removeLeftRooms(txn, res.rooms.leave); removeLeftRooms(txn, res.rooms.leave);
txn.commit(); txn.commit();
} }
void
Cache::removeStaleInvites(lmdb::txn &txn, const std::map<std::string, bool> &curr)
{
auto invitesCursor = lmdb::cursor::open(txn, invitesDb_);
std::string room_id, room_data;
while (invitesCursor.get(room_id, room_data, MDB_NEXT)) {
if (curr.find(room_id) == curr.end())
lmdb::cursor_del(invitesCursor);
}
invitesCursor.close();
}
void void
Cache::saveInvites(lmdb::txn &txn, const std::map<std::string, mtx::responses::InvitedRoom> &rooms) Cache::saveInvites(lmdb::txn &txn, const std::map<std::string, mtx::responses::InvitedRoom> &rooms)
{ {
@ -554,31 +534,32 @@ Cache::getRoomInfo(const std::vector<std::string> &rooms)
} }
QMap<QString, RoomInfo> QMap<QString, RoomInfo>
Cache::roomInfo() Cache::roomInfo(bool withInvites)
{ {
QMap<QString, RoomInfo> result; QMap<QString, RoomInfo> result;
auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY); auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
auto roomsCursor = lmdb::cursor::open(txn, roomsDb_);
auto invitesCursor = lmdb::cursor::open(txn, invitesDb_);
std::string room_id; std::string room_id;
std::string room_data; std::string room_data;
// Gather info about the joined rooms. // Gather info about the joined rooms.
auto roomsCursor = lmdb::cursor::open(txn, roomsDb_);
while (roomsCursor.get(room_id, room_data, MDB_NEXT)) { while (roomsCursor.get(room_id, room_data, MDB_NEXT)) {
RoomInfo tmp = json::parse(room_data); RoomInfo tmp = json::parse(std::move(room_data));
result.insert(QString::fromStdString(std::move(room_id)), std::move(tmp)); result.insert(QString::fromStdString(std::move(room_id)), std::move(tmp));
} }
roomsCursor.close();
if (withInvites) {
// Gather info about the invites. // Gather info about the invites.
auto invitesCursor = lmdb::cursor::open(txn, invitesDb_);
while (invitesCursor.get(room_id, room_data, MDB_NEXT)) { while (invitesCursor.get(room_id, room_data, MDB_NEXT)) {
RoomInfo tmp = json::parse(room_data); RoomInfo tmp = json::parse(room_data);
result.insert(QString::fromStdString(std::move(room_id)), std::move(tmp)); result.insert(QString::fromStdString(std::move(room_id)), std::move(tmp));
} }
invitesCursor.close(); invitesCursor.close();
roomsCursor.close(); }
txn.commit(); txn.commit();

View file

@ -624,6 +624,11 @@ ChatPage::updateOwnCommunitiesInfo(const QList<QString> &own_communities)
void void
ChatPage::changeTopRoomInfo(const QString &room_id) ChatPage::changeTopRoomInfo(const QString &room_id)
{ {
if (room_id.isEmpty()) {
qWarning() << "can't switch to empty room_id";
return;
}
try { try {
auto room_info = cache_->getRoomInfo({room_id.toStdString()}); auto room_info = cache_->getRoomInfo({room_id.toStdString()});
@ -640,12 +645,11 @@ ChatPage::changeTopRoomInfo(const QString &room_id)
top_bar_->updateRoomAvatar(roomAvatars_[room_id].toImage()); top_bar_->updateRoomAvatar(roomAvatars_[room_id].toImage());
else else
top_bar_->updateRoomAvatarFromName(name); top_bar_->updateRoomAvatarFromName(name);
} catch (const lmdb::error &e) {
qWarning() << "failed to change top bar room info"
<< QString::fromStdString(e.what());
}
current_room_ = room_id; current_room_ = room_id;
} catch (const lmdb::error &e) {
qWarning() << "failed to change top bar room info" << e.what();
}
} }
void void
@ -712,12 +716,11 @@ ChatPage::showQuickSwitcher()
quickSwitcherModal_->setColor(QColor(30, 30, 30, 170)); quickSwitcherModal_->setColor(QColor(30, 30, 30, 170));
} }
std::map<QString, QString> rooms;
try { try {
auto info = cache_->roomInfo(); std::map<QString, QString> rooms;
auto info = cache_->roomInfo(false);
for (auto it = info.begin(); it != info.end(); ++it) for (auto it = info.begin(); it != info.end(); ++it)
rooms.emplace(QString::fromStdString(it.value().name), it.key()); rooms.emplace(QString::fromStdString(it.value().name).trimmed(), it.key());
quickSwitcher_->setRoomList(rooms); quickSwitcher_->setRoomList(rooms);
quickSwitcherModal_->show(); quickSwitcherModal_->show();
} catch (const lmdb::error &e) { } catch (const lmdb::error &e) {