aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/connection.cpp49
-rw-r--r--lib/connection.h29
-rw-r--r--lib/user.cpp6
3 files changed, 69 insertions, 15 deletions
diff --git a/lib/connection.cpp b/lib/connection.cpp
index c3e46356..2c5bf574 100644
--- a/lib/connection.cpp
+++ b/lib/connection.cpp
@@ -955,6 +955,33 @@ QHash<QPair<QString, bool>, Room*> Connection::roomMap() const
return roomMap;
}
+QVector<Room*> Connection::allRooms() const
+{
+ QVector<Room*> result;
+ result.resize(d->roomMap.size());
+ std::copy(d->roomMap.cbegin(), d->roomMap.cend(), result.begin());
+ return result;
+}
+
+QVector<Room*> Connection::rooms(JoinStates joinStates) const
+{
+ QVector<Room*> result;
+ for (auto* r: qAsConst(d->roomMap))
+ if (joinStates.testFlag(r->joinState()))
+ result.push_back(r);
+ return result;
+}
+
+int Connection::roomsCount(JoinStates joinStates) const
+{
+ // Using int to maintain compatibility with QML
+ // (consider also that QHash<>::size() returns int anyway).
+ return int(std::count_if(d->roomMap.begin(), d->roomMap.end(),
+ [joinStates](Room* r) {
+ return joinStates.testFlag(r->joinState());
+ }));
+}
+
bool Connection::hasAccountData(const QString& type) const
{
return d->accountData.find(type) != d->accountData.cend();
@@ -1262,18 +1289,20 @@ void Connection::saveState() const
{ QStringLiteral("minor"), SyncData::cacheVersion().second } } }
};
{
- QJsonObject rooms;
- QJsonObject inviteRooms;
- const auto& rs = roomMap(); // Pass on rooms in Leave state
- for (const auto* i : rs)
- (i->joinState() == JoinState::Invite ? inviteRooms : rooms)
- .insert(i->id(), QJsonValue::Null);
+ QJsonObject roomsJson;
+ QJsonObject inviteRoomsJson;
+ for (const auto* r: qAsConst(d->roomMap)) {
+ if (r->joinState() == JoinState::Leave)
+ continue;
+ (r->joinState() == JoinState::Invite ? inviteRoomsJson : roomsJson)
+ .insert(r->id(), QJsonValue::Null);
+ }
QJsonObject roomObj;
- if (!rooms.isEmpty())
- roomObj.insert(QStringLiteral("join"), rooms);
- if (!inviteRooms.isEmpty())
- roomObj.insert(QStringLiteral("invite"), inviteRooms);
+ if (!roomsJson.isEmpty())
+ roomObj.insert(QStringLiteral("join"), roomsJson);
+ if (!inviteRoomsJson.isEmpty())
+ roomObj.insert(QStringLiteral("invite"), inviteRoomsJson);
rootObj.insert(QStringLiteral("next_batch"), d->data->lastEvent());
rootObj.insert(QStringLiteral("rooms"), roomObj);
diff --git a/lib/connection.h b/lib/connection.h
index b4b16679..1f1d4cd5 100644
--- a/lib/connection.h
+++ b/lib/connection.h
@@ -135,12 +135,39 @@ public:
explicit Connection(const QUrl& server, QObject* parent = nullptr);
~Connection() override;
- /** Get all Invited and Joined rooms
+ /// Get all Invited and Joined rooms
+ /*!
* \return a hashmap from a composite key - room name and whether
* it's an Invite rather than Join - to room pointers
+ * \sa allRooms, rooms, roomsWithTag
*/
+ [[deprecated("Use allRooms(), roomsWithTag() or rooms(joinStates) instead")]]
QHash<QPair<QString, bool>, Room*> roomMap() const;
+ /// Get all rooms known within this Connection
+ /*!
+ * This includes Invite, Join and Leave rooms, in no particular order.
+ * \note Leave rooms will only show up in the list if they have been left
+ * in the same running session. The library doesn't cache left rooms
+ * between runs and it doesn't retrieve the full list of left rooms
+ * from the server.
+ * \sa rooms, room, roomsWithTag
+ */
+ Q_INVOKABLE QVector<Room*> allRooms() const;
+
+ /// Get rooms that have either of the given join state(s)
+ /*!
+ * This method returns, in no particular order, rooms which join state
+ * matches the mask passed in \p joinStates.
+ * \note Similar to allRooms(), this won't retrieve the full list of
+ * Leave rooms from the server.
+ * \sa allRooms, room, roomsWithTag
+ */
+ Q_INVOKABLE QVector<Room*> rooms(JoinStates joinStates) const;
+
+ /// Get the total number of rooms in the given join state(s)
+ Q_INVOKABLE int roomsCount(JoinStates joinStates) const;
+
/** Check whether the account has data of the given type
* Direct chats map is not supported by this method _yet_.
*/
diff --git a/lib/user.cpp b/lib/user.cpp
index 641f6a6b..67cd1117 100644
--- a/lib/user.cpp
+++ b/lib/user.cpp
@@ -111,8 +111,7 @@ void User::Private::setNameForRoom(const Room* r, QString newName,
et.start();
}
- const auto& roomMap = connection->roomMap();
- for (auto* r1 : roomMap)
+ for (auto* r1: connection->allRooms())
if (nameForRoom(r1) == mostUsedName)
otherNames.insert(mostUsedName, r1);
@@ -176,8 +175,7 @@ void User::Private::setAvatarForRoom(const Room* r, const QUrl& newUrl,
nextMostUsedIt = otherAvatars.end() - 1;
}
std::swap(mostUsedAvatar, *nextMostUsedIt);
- const auto& roomMap = connection->roomMap();
- for (const auto* r1 : roomMap)
+ for (const auto* r1: connection->allRooms())
if (avatarUrlForRoom(r1) == nextMostUsedIt->url())
avatarsToRooms.insert(nextMostUsedIt->url(), r1);