aboutsummaryrefslogtreecommitdiff
path: root/lib/syncdata.cpp
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2018-11-22 16:51:49 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2018-11-22 16:53:59 +0900
commit5fb74ca3d253b658fe77aaeb6a106cf6c0a9e7f0 (patch)
tree203d3533550e6c665220578f7922317833efb22c /lib/syncdata.cpp
parent0c3a45356a803baa0eb5e553262a85cac897ac4f (diff)
downloadlibquotient-5fb74ca3d253b658fe77aaeb6a106cf6c0a9e7f0.tar.gz
libquotient-5fb74ca3d253b658fe77aaeb6a106cf6c0a9e7f0.zip
Save state cache per-room
Closes #257.
Diffstat (limited to 'lib/syncdata.cpp')
-rw-r--r--lib/syncdata.cpp37
1 files changed, 22 insertions, 15 deletions
diff --git a/lib/syncdata.cpp b/lib/syncdata.cpp
index f0d55fd6..d141a7cc 100644
--- a/lib/syncdata.cpp
+++ b/lib/syncdata.cpp
@@ -21,6 +21,7 @@
#include "events/eventloader.h"
#include <QtCore/QFile>
+#include <QtCore/QFileInfo>
using namespace QMatrixClient;
@@ -69,7 +70,17 @@ SyncRoomData::SyncRoomData(const QString& roomId_, JoinState joinState_,
SyncData::SyncData(const QString& cacheFileName)
{
- parseJson(loadJson(cacheFileName));
+ QFileInfo cacheFileInfo { cacheFileName };
+ auto json = loadJson(cacheFileName);
+ auto requiredVersion = std::get<0>(cacheVersion());
+ auto actualVersion = json.value("cache_version").toObject()
+ .value("major").toInt();
+ if (actualVersion == requiredVersion)
+ parseJson(json, cacheFileInfo.absolutePath() + '/');
+ else
+ qCWarning(MAIN)
+ << "Major version of the cache file is" << actualVersion << "but"
+ << requiredVersion << "is required; discarding the cache";
}
SyncDataList&& SyncData::takeRoomData()
@@ -77,6 +88,12 @@ SyncDataList&& SyncData::takeRoomData()
return move(roomData);
}
+QString SyncData::fileNameForRoom(QString roomId)
+{
+ roomId.replace(':', '_');
+ return roomId + ".json";
+}
+
Events&& SyncData::takePresenceData()
{
return std::move(presenceData);
@@ -115,22 +132,11 @@ QJsonObject SyncData::loadJson(const QString& fileName)
{
qCWarning(MAIN) << "State cache in" << fileName
<< "is broken or empty, discarding";
- return {};
- }
- auto requiredVersion = std::get<0>(cacheVersion());
- auto actualVersion = json.value("cache_version").toObject()
- .value("major").toInt();
- if (actualVersion < requiredVersion)
- {
- qCWarning(MAIN)
- << "Major version of the cache file is" << actualVersion << "but"
- << requiredVersion << "is required; discarding the cache";
- return {};
}
return json;
}
-void SyncData::parseJson(const QJsonObject& json)
+void SyncData::parseJson(const QJsonObject& json, const QString& baseDir)
{
QElapsedTimer et; et.start();
@@ -150,8 +156,9 @@ void SyncData::parseJson(const QJsonObject& json)
roomData.reserve(static_cast<size_t>(rs.size()));
for(auto roomIt = rs.begin(); roomIt != rs.end(); ++roomIt)
{
- auto roomJson = roomIt->isString() ? loadJson(roomIt->toString())
- : roomIt->toObject();
+ auto roomJson = roomIt->isString()
+ ? loadJson(baseDir + fileNameForRoom(roomIt.key()))
+ : roomIt->toObject();
if (roomJson.isEmpty())
{
unresolvedRoomIds.push_back(roomIt.key());