diff options
-rw-r--r-- | lib/connection.cpp | 72 | ||||
-rw-r--r-- | lib/converters.cpp | 20 | ||||
-rw-r--r-- | lib/converters.h | 28 |
3 files changed, 44 insertions, 76 deletions
diff --git a/lib/connection.cpp b/lib/connection.cpp index b702c1f9..46494a56 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -357,23 +357,24 @@ void Connection::assumeIdentity(const QString& userId, void Connection::reloadCapabilities() { d->capabilitiesJob = callApi<GetCapabilitiesJob>(BackgroundRequest); - connect(d->capabilitiesJob, &BaseJob::finished, this, [this] { - if (d->capabilitiesJob->error() == BaseJob::Success) - d->capabilities = d->capabilitiesJob->capabilities(); - else if (d->capabilitiesJob->error() == BaseJob::IncorrectRequestError) - qCDebug(MAIN) << "Server doesn't support /capabilities"; - - if (!d->capabilities.roomVersions) { - qCWarning(MAIN) << "Pinning supported room version to 1"; - d->capabilities.roomVersions = { "1", { { "1", "stable" } } }; - } else { + connect(d->capabilitiesJob, &BaseJob::success, this, [this] { + d->capabilities = d->capabilitiesJob->capabilities(); + + if (d->capabilities.roomVersions) { qCDebug(MAIN) << "Room versions:" << defaultRoomVersion() << "is default, full list:" << availableRoomVersions(); - } - Q_ASSERT(d->capabilities.roomVersions.has_value()); - emit capabilitiesLoaded(); - for (auto* r : qAsConst(d->roomMap)) - r->checkVersion(); + emit capabilitiesLoaded(); + for (auto* r: std::as_const(d->roomMap)) + r->checkVersion(); + } else + qCWarning(MAIN) + << "The server returned an empty set of supported versions;" + " disabling version upgrade recommendations to reduce noise"; + }); + connect(d->capabilitiesJob, &BaseJob::failure, this, [this] { + if (d->capabilitiesJob->error() == BaseJob::IncorrectRequestError) + qCDebug(MAIN) << "Server doesn't support /capabilities;" + " version upgrade recommendations won't be issued"; }); } @@ -1685,18 +1686,20 @@ const QString Connection::SupportedRoomVersion::StableTag = QString Connection::defaultRoomVersion() const { - Q_ASSERT(d->capabilities.roomVersions.has_value()); - return d->capabilities.roomVersions->defaultVersion; + return d->capabilities.roomVersions + ? d->capabilities.roomVersions->defaultVersion + : QString(); } QStringList Connection::stableRoomVersions() const { - Q_ASSERT(d->capabilities.roomVersions.has_value()); QStringList l; - const auto& allVersions = d->capabilities.roomVersions->available; - for (auto it = allVersions.begin(); it != allVersions.end(); ++it) - if (it.value() == SupportedRoomVersion::StableTag) - l.push_back(it.key()); + if (d->capabilities.roomVersions) { + const auto& allVersions = d->capabilities.roomVersions->available; + for (auto it = allVersions.begin(); it != allVersions.end(); ++it) + if (it.value() == SupportedRoomVersion::StableTag) + l.push_back(it.key()); + } return l; } @@ -1711,18 +1714,19 @@ inline bool roomVersionLess(const Connection::SupportedRoomVersion& v1, QVector<Connection::SupportedRoomVersion> Connection::availableRoomVersions() const { - Q_ASSERT(d->capabilities.roomVersions.has_value()); 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); - + if (d->capabilities.roomVersions) { + const auto& allVersions = d->capabilities.roomVersions->available; + result.reserve(allVersions.size()); + for (auto it = allVersions.begin(); it != allVersions.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/converters.cpp b/lib/converters.cpp index 9f4b9360..e5236bb9 100644 --- a/lib/converters.cpp +++ b/lib/converters.cpp @@ -32,24 +32,12 @@ QVariant JsonConverter<QVariant>::load(const QJsonValue& jv) return jv.toVariant(); } -QJsonObject JsonConverter<variant_map_t>::dump(const variant_map_t& map) +QJsonObject JsonConverter<QVariantHash>::dump(const QVariantHash& map) { - return -#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)) - QJsonObject::fromVariantHash -#else - QJsonObject::fromVariantMap -#endif - (map); + return QJsonObject::fromVariantHash(map); } -variant_map_t JsonConverter<QVariantHash>::load(const QJsonValue& jv) +QVariantHash JsonConverter<QVariantHash>::load(const QJsonValue& jv) { - return jv.toObject(). -#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)) - toVariantHash -#else - toVariantMap -#endif - (); + return jv.toObject().toVariantHash(); } diff --git a/lib/converters.h b/lib/converters.h index 075af7ef..543e9496 100644 --- a/lib/converters.h +++ b/lib/converters.h @@ -30,21 +30,6 @@ #include <vector> -#if QT_VERSION < QT_VERSION_CHECK(5,14,0) -// Enable std::unordered_map<QString, T> -// REMOVEME in favor of UnorderedMap, once we regenerate API files -namespace std { -template <> -struct hash<QString> { - size_t operator()(const QString& s) const Q_DECL_NOEXCEPT - { - return qHash(s, uint(qGlobalQHashSeed()) - ); - } -}; -} // namespace std -#endif - class QVariant; namespace Quotient { @@ -296,18 +281,9 @@ template <typename T> struct JsonObjectConverter<QHash<QString, T>> : public HashMapFromJson<QHash<QString, T>> {}; -// We could use std::conditional<> below but QT_VERSION* macros in C++ code -// cause (kinda valid but useless and noisy) compiler warnings about -// bitwise operations on signed integers; so use the preprocessor for now. -using variant_map_t = -#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)) - QVariantHash; -#else - QVariantMap; -#endif template <> -struct JsonConverter<variant_map_t> { - static QJsonObject dump(const variant_map_t& vh); +struct JsonConverter<QVariantHash> { + static QJsonObject dump(const QVariantHash& vh); static QVariantHash load(const QJsonValue& jv); }; |