diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2020-06-05 07:42:33 +0200 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2020-06-05 07:42:33 +0200 |
commit | db8b9569f83ba3643c9005359ba556f2c3e357f4 (patch) | |
tree | dd3f65ba8007b6f08e776880995cfbba00ba63ff /lib | |
parent | 79ede9bcb6d4fc5e269f506b2a10bfe4c302040a (diff) | |
download | libquotient-db8b9569f83ba3643c9005359ba556f2c3e357f4.tar.gz libquotient-db8b9569f83ba3643c9005359ba556f2c3e357f4.zip |
Use CBOR for binary JSON caching on Qt 5.15+
Qt 5.15 deprecates binary JSON format in favour of CBOR now used as
a backend for its JSON classes.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/connection.cpp | 25 | ||||
-rw-r--r-- | lib/syncdata.cpp | 12 | ||||
-rw-r--r-- | lib/syncdata.h | 2 |
3 files changed, 30 insertions, 9 deletions
diff --git a/lib/connection.cpp b/lib/connection.cpp index f497601e..26de0c5f 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -48,6 +48,10 @@ # include "account.h" // QtOlm #endif // Quotient_E2EE_ENABLED +#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) +# include <QtCore/QCborValue> +#endif + #include <QtCore/QCoreApplication> #include <QtCore/QDir> #include <QtCore/QElapsedTimer> @@ -1492,9 +1496,16 @@ void Connection::saveRoomState(Room* r) const QFile outRoomFile { stateCacheDir().filePath( SyncData::fileNameForRoom(r->id())) }; if (outRoomFile.open(QFile::WriteOnly)) { +#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) + const auto data = + d->cacheToBinary + ? QCborValue::fromJsonValue(r->toJson()).toCbor() + : QJsonDocument(r->toJson()).toJson(QJsonDocument::Compact); +#else QJsonDocument json { r->toJson() }; - auto data = d->cacheToBinary ? json.toBinaryData() - : json.toJson(QJsonDocument::Compact); + const auto data = d->cacheToBinary ? json.toBinaryData() + : json.toJson(QJsonDocument::Compact); +#endif outRoomFile.write(data.data(), data.size()); qCDebug(MAIN) << "Room state cache saved to" << outRoomFile.fileName(); } else { @@ -1558,9 +1569,15 @@ void Connection::saveState() const { QStringLiteral("events"), accountDataEvents } }); } +#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) + const auto data = + d->cacheToBinary ? QCborValue::fromJsonValue(rootObj).toCbor() + : QJsonDocument(rootObj).toJson(QJsonDocument::Compact); +#else QJsonDocument json { rootObj }; - auto data = d->cacheToBinary ? json.toBinaryData() - : json.toJson(QJsonDocument::Compact); + const auto data = d->cacheToBinary ? json.toBinaryData() + : json.toJson(QJsonDocument::Compact); +#endif qCDebug(PROFILER) << "Cache for" << userId() << "generated in" << et; outFile.write(data.data(), data.size()); diff --git a/lib/syncdata.cpp b/lib/syncdata.cpp index 6e68e2cd..a9ec5ee7 100644 --- a/lib/syncdata.cpp +++ b/lib/syncdata.cpp @@ -157,10 +157,14 @@ QJsonObject SyncData::loadJson(const QString& fileName) } auto data = roomFile.readAll(); - const auto json = (data.startsWith('{') - ? QJsonDocument::fromJson(data) - : QJsonDocument::fromBinaryData(data)) - .object(); + const auto json = data.startsWith('{') + ? QJsonDocument::fromJson(data).object() +#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) + : QCborValue::fromCbor(data).toJsonValue().toObject() +#else + : QJsonDocument::fromBinaryData(data).object() +#endif + ; if (json.isEmpty()) { qCWarning(MAIN) << "State cache in" << fileName << "is broken or empty, discarding"; diff --git a/lib/syncdata.h b/lib/syncdata.h index 41b8186e..752dd485 100644 --- a/lib/syncdata.h +++ b/lib/syncdata.h @@ -102,7 +102,7 @@ public: QStringList unresolvedRooms() const { return unresolvedRoomIds; } - static std::pair<int, int> cacheVersion() { return { 10, 0 }; } + static std::pair<int, int> cacheVersion() { return { 11, 0 }; } static QString fileNameForRoom(QString roomId); private: |