diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2020-03-26 22:49:06 +0100 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2020-03-26 22:52:01 +0100 |
commit | 2216c5f844e5227976f4b0a8208c6c7e834934ae (patch) | |
tree | 485f087033ef9789d0c8469538ee62ec1ff1336f /lib/connection.cpp | |
parent | 123d58fc3c29d8e9adbb5b654df53d5cbb0a32fa (diff) | |
download | libquotient-2216c5f844e5227976f4b0a8208c6c7e834934ae.tar.gz libquotient-2216c5f844e5227976f4b0a8208c6c7e834934ae.zip |
Connection: allRooms(), rooms(), roomsCount(); deprecate roomMap()
Backport of #354.
Diffstat (limited to 'lib/connection.cpp')
-rw-r--r-- | lib/connection.cpp | 49 |
1 files changed, 39 insertions, 10 deletions
diff --git a/lib/connection.cpp b/lib/connection.cpp index 647fc006..8c87d775 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -1046,6 +1046,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(); @@ -1371,18 +1398,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); |