diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2019-02-17 17:41:47 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2019-02-17 17:41:47 +0900 |
commit | ac5daf2ed495a932aba23606f5b3d0dca5aaf676 (patch) | |
tree | 5aba1f69aa19a269f73579a86d69a32e776d4fa9 | |
parent | 73e6bd47f4bafa7e65f8d826d8c6527c59aeb865 (diff) | |
download | libquotient-ac5daf2ed495a932aba23606f5b3d0dca5aaf676.tar.gz libquotient-ac5daf2ed495a932aba23606f5b3d0dca5aaf676.zip |
Connection: loadingCapabilities(); sort availableRoomVersions
-rw-r--r-- | lib/connection.cpp | 37 | ||||
-rw-r--r-- | lib/connection.h | 41 |
2 files changed, 73 insertions, 5 deletions
diff --git a/lib/connection.cpp b/lib/connection.cpp index 22fa2f15..4c0fe6b8 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -274,6 +274,13 @@ void Connection::reloadCapabilities() }); } +bool Connection::loadingCapabilities() const +{ + // (Ab)use the fact that room versions cannot be omitted after + // the capabilities have been loaded (see reloadCapabilities() above). + return d->capabilities.roomVersions.omitted(); +} + void Connection::Private::connectWithToken(const QString& user, const QString& accessToken, const QString& deviceId) @@ -1300,6 +1307,9 @@ void Connection::getTurnServers() this, [=] { emit turnServersChanged(job->data()); }); } +const QString Connection::SupportedRoomVersion::StableTag = + QStringLiteral("stable"); + QString Connection::defaultRoomVersion() const { Q_ASSERT(!d->capabilities.roomVersions.omitted()); @@ -1312,13 +1322,34 @@ QStringList Connection::stableRoomVersions() const QStringList l; const auto& allVersions = d->capabilities.roomVersions->available; for (auto it = allVersions.begin(); it != allVersions.end(); ++it) - if (it.value() == "stable") + if (it.value() == SupportedRoomVersion::StableTag) l.push_back(it.key()); return l; } -const QHash<QString, QString>& Connection::availableRoomVersions() const +inline bool roomVersionLess(const Connection::SupportedRoomVersion& v1, + const Connection::SupportedRoomVersion& v2) +{ + bool ok1 = false, ok2 = false; + const auto vNum1 = v1.id.toFloat(&ok1); + const auto vNum2 = v2.id.toFloat(&ok2); + return ok1 && ok2 ? vNum1 < vNum2 : v1.id < v2.id; +} + +QVector<Connection::SupportedRoomVersion> Connection::availableRoomVersions() const { Q_ASSERT(!d->capabilities.roomVersions.omitted()); - return d->capabilities.roomVersions->available; + QVector<SupportedRoomVersion> result; + result.reserve(d->capabilities.roomVersions->available.size()); + for (auto it = d->capabilities.roomVersions->available.begin(); + it != d->capabilities.roomVersions->available.end(); ++it) + result.push_back({ it.key(), it.value() }); + // Put stable versions over unstable; within each group, + // sort numeric versions as numbers, the rest as strings. + const auto mid = std::partition(result.begin(), result.end(), + std::mem_fn(&SupportedRoomVersion::isStable)); + std::sort(result.begin(), mid, roomVersionLess); + std::sort(mid, result.end(), roomVersionLess); + + return result; } diff --git a/lib/connection.h b/lib/connection.h index e5bce52e..1faee255 100644 --- a/lib/connection.h +++ b/lib/connection.h @@ -258,9 +258,43 @@ namespace QMatrixClient Q_INVOKABLE QString token() const; Q_INVOKABLE void getTurnServers(); + struct SupportedRoomVersion + { + QString id; + QString status; + + static const QString StableTag; // "stable", as of CS API 0.5 + bool isStable() const { return status == StableTag; } + + // Pretty-printing + + friend QDebug operator<<(QDebug dbg, + const SupportedRoomVersion& v) + { + QDebugStateSaver _(dbg); + return dbg.nospace() << v.id << '/' << v.status; + } + + friend QDebug operator<<(QDebug dbg, + const QVector<SupportedRoomVersion>& vs) + { + return QtPrivate::printSequentialContainer( + dbg, "", vs); + } + }; + + /// Get the room version recommended by the server + /** Only works after server capabilities have been loaded. + * \sa loadingCapabilities */ QString defaultRoomVersion() const; + /// Get the room version considered stable by the server + /** Only works after server capabilities have been loaded. + * \sa loadingCapabilities */ QStringList stableRoomVersions() const; - const QHash<QString, QString>& availableRoomVersions() const; + /// Get all room versions supported by the server + /** Only works after server capabilities have been loaded. + * \sa loadingCapabilities */ + QVector<SupportedRoomVersion> availableRoomVersions() const; /** * Call this before first sync to load from previously saved file. @@ -370,9 +404,12 @@ namespace QMatrixClient const QString& deviceId = {}); void connectWithToken(const QString& userId, const QString& accessToken, const QString& deviceId); - /** Explicitly request capabilities from the server */ + /// Explicitly request capabilities from the server void reloadCapabilities(); + /// Find out if capabilites are still loading from the server + bool loadingCapabilities() const; + /** @deprecated Use stopSync() instead */ void disconnectFromServer() { stopSync(); } void logout(); |