aboutsummaryrefslogtreecommitdiff
path: root/connection.cpp
diff options
context:
space:
mode:
authorRoman Plášil <me@rplasil.name>2017-08-16 13:56:13 +0800
committerRoman Plášil <me@rplasil.name>2017-08-16 14:09:54 +0800
commit6ae8e3d78b5c4a75ca7d5ca88af730071047d148 (patch)
treed295ad7bff2fcf5ac19f0f35ebe5bd355b928dca /connection.cpp
parentc1929dc22c87ac61e5369cb752e6ddd0ef6a79bf (diff)
downloadlibquotient-6ae8e3d78b5c4a75ca7d5ca88af730071047d148.tar.gz
libquotient-6ae8e3d78b5c4a75ca7d5ca88af730071047d148.zip
Implement saving save to enable incremental sync even after shutdown
Diffstat (limited to 'connection.cpp')
-rw-r--r--connection.cpp32
1 files changed, 20 insertions, 12 deletions
diff --git a/connection.cpp b/connection.cpp
index 3ecabdc5..7650b4dd 100644
--- a/connection.cpp
+++ b/connection.cpp
@@ -159,12 +159,7 @@ void Connection::sync(int timeout)
auto job = d->syncJob =
callApi<SyncJob>(d->data->lastEvent(), filter, timeout);
connect( job, &SyncJob::success, [=] () {
- d->data->setLastEvent(job->nextBatch());
- for( auto&& roomData: job->takeRoomData() )
- {
- if ( auto* r = provideRoom(roomData.roomId) )
- r->updateData(std::move(roomData));
- }
+ onSyncSuccess(*job->data());
d->syncJob = nullptr;
emit syncDone();
});
@@ -178,6 +173,17 @@ void Connection::sync(int timeout)
});
}
+void Connection::onSyncSuccess(SyncData &data) {
+ d->data->setLastEvent(data.nextBatch());
+ qInfo() << "last event " << d->data->lastEvent();
+ for( auto&& roomData: data.takeRoomData() )
+ {
+ if ( auto* r = provideRoom(roomData.roomId) )
+ r->updateData(std::move(roomData));
+ }
+
+}
+
void Connection::stopSync()
{
if (d->syncJob)
@@ -323,8 +329,8 @@ QByteArray Connection::generateTxnId()
return d->data->generateTxnId();
}
-QFile Connection::getStateSaveFile() const {
- return QFile(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/data.json");
+QString Connection::getStateSaveFile() const {
+ return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/state.json";
}
void Connection::saveState() {
@@ -348,18 +354,20 @@ void Connection::saveState() {
QJsonDocument doc { rootObj };
QByteArray data = doc.toJson();
- QFile outfile = getStateSaveFile();
+ QFile outfile { getStateSaveFile() };
outfile.open(QFile::WriteOnly);
qInfo() << "Writing state to file=" << outfile.fileName();
- //QFile outfile(path);
outfile.write(data.data(), data.size());
}
void Connection::loadState() {
- QFile file = getStateSaveFile();
+ QFile file { getStateSaveFile() };
if (!file.exists()) return;
file.open(QFile::ReadOnly);
QByteArray data = file.readAll();
- QJsonDocument doc = QJsonDocument.fromJson(data.data(), data.size());
+ QJsonDocument doc { QJsonDocument::fromJson(data) };
+ SyncData sync;
+ sync.parseJson(doc);
+ onSyncSuccess(sync);
}