diff options
-rw-r--r-- | connection.cpp | 32 | ||||
-rw-r--r-- | connection.h | 10 | ||||
-rw-r--r-- | jobs/syncjob.cpp | 32 | ||||
-rw-r--r-- | jobs/syncjob.h | 19 |
4 files changed, 58 insertions, 35 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); } diff --git a/connection.h b/connection.h index f199ed35..58a3de6b 100644 --- a/connection.h +++ b/connection.h @@ -31,6 +31,7 @@ namespace QMatrixClient class ConnectionData; class SyncJob; + class SyncData; class RoomMessagesJob; class PostReceiptJob; class MediaThumbnailJob; @@ -143,6 +144,15 @@ namespace QMatrixClient */ virtual Room* createRoom(const QString& roomId); + /** + * Returns the path to file for saving state (rooms, presence, ...) + */ + QString getStateSaveFile() const; + + /** + * Completes loading sync data. + */ + void onSyncSuccess(SyncData &data); private: class Private; Private* d; diff --git a/jobs/syncjob.cpp b/jobs/syncjob.cpp index 29ddc2e6..3adf6b0c 100644 --- a/jobs/syncjob.cpp +++ b/jobs/syncjob.cpp @@ -22,20 +22,13 @@ using namespace QMatrixClient; -class SyncJob::Private -{ - public: - QString nextBatch; - SyncData roomData; -}; - static size_t jobId = 0; SyncJob::SyncJob(const ConnectionData* connection, const QString& since, const QString& filter, int timeout, const QString& presence) : BaseJob(connection, HttpVerb::Get, QString("SyncJob-%1").arg(++jobId), "_matrix/client/r0/sync") - , d(new Private) + , d(new SyncData) { setLoggingCategory(SYNCJOB); QUrlQuery query; @@ -57,21 +50,26 @@ SyncJob::~SyncJob() delete d; } -QString SyncJob::nextBatch() const +QString SyncData::nextBatch() const { - return d->nextBatch; + return nextBatch_; } -SyncData&& SyncJob::takeRoomData() +SyncDataList&& SyncData::takeRoomData() { - return std::move(d->roomData); + return std::move(roomData); } BaseJob::Status SyncJob::parseJson(const QJsonDocument& data) { + d->parseJson(data); + return Success; +} + +void SyncData::parseJson(const QJsonDocument &data) { QElapsedTimer et; et.start(); QJsonObject json = data.object(); - d->nextBatch = json.value("next_batch").toString(); + nextBatch_ = json.value("next_batch").toString(); // TODO: presence // TODO: account_data QJsonObject rooms = json.value("rooms").toObject(); @@ -86,13 +84,11 @@ BaseJob::Status SyncJob::parseJson(const QJsonDocument& data) { const QJsonObject rs = rooms.value(roomState.jsonKey).toObject(); // We have a Qt container on the right and an STL one on the left - d->roomData.reserve(static_cast<size_t>(rs.size())); + roomData.reserve(static_cast<size_t>(rs.size())); for( auto rkey: rs.keys() ) - d->roomData.emplace_back(rkey, roomState.enumVal, rs[rkey].toObject()); + roomData.emplace_back(rkey, roomState.enumVal, rs[rkey].toObject()); } - qCDebug(PROFILER) << "*** SyncJob::parseJson():" << et.elapsed() << "ms"; - - return Success; + qCDebug(PROFILER) << "*** SyncData::parseJson():" << et.elapsed() << "ms"; } SyncRoomData::SyncRoomData(const QString& roomId_, JoinState joinState_, diff --git a/jobs/syncjob.h b/jobs/syncjob.h index 07824e23..9dc221b5 100644 --- a/jobs/syncjob.h +++ b/jobs/syncjob.h @@ -67,7 +67,18 @@ Q_DECLARE_TYPEINFO(QMatrixClient::SyncRoomData, Q_MOVABLE_TYPE); namespace QMatrixClient { // QVector cannot work with non-copiable objects, std::vector can. - using SyncData = std::vector<SyncRoomData>; + using SyncDataList = std::vector<SyncRoomData>; + + class SyncData { + public: + void parseJson(const QJsonDocument &data); + SyncDataList&& takeRoomData(); + QString nextBatch() const; + + private: + QString nextBatch_; + SyncDataList roomData; + }; class SyncJob: public BaseJob { @@ -77,14 +88,12 @@ namespace QMatrixClient int timeout = -1, const QString& presence = {}); virtual ~SyncJob(); - SyncData&& takeRoomData(); - QString nextBatch() const; + SyncData *data() const { return d; } protected: Status parseJson(const QJsonDocument& data) override; private: - class Private; - Private* d; + SyncData* d; }; } // namespace QMatrixClient |