From 866cb97668fadff50dea8d16792c3b6736d531dc Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Tue, 6 Sep 2016 16:12:11 +0900 Subject: Remember the current SyncJob and allow to explicitly abandon it --- connection.cpp | 45 ++++++++++++++++++++++++++++++++++----------- connection.h | 1 + 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/connection.cpp b/connection.cpp index f13026fc..98ff914a 100644 --- a/connection.cpp +++ b/connection.cpp @@ -45,6 +45,7 @@ class Connection::Private : q(nullptr) , data(new ConnectionData(serverUrl)) , isConnected(false) + , syncJob(nullptr) { } Private(Private&) = delete; ~Private() { delete data; } @@ -57,6 +58,10 @@ class Connection::Private QString username; QString password; QString userId; + + SyncJob* syncJob; + + SyncJob* startSyncJob(const QString& filter, int timeout); }; Connection::Connection(QUrl server, QObject* parent) @@ -140,6 +145,12 @@ void Connection::reconnect() loginJob->start(); } +void Connection::disconnectFromServer() +{ + d->syncJob->abandon(); + d->isConnected = false; +} + void Connection::logout() { auto job = new LogoutJob(d->data); @@ -149,27 +160,39 @@ void Connection::logout() SyncJob* Connection::sync(int timeout) { - QString filter = "{\"room\": { \"timeline\": { \"limit\": 100 } } }"; - SyncJob* syncJob = new SyncJob(d->data, d->data->lastEvent()); - syncJob->setFilter(filter); - syncJob->setTimeout(timeout); - connect( syncJob, &SyncJob::success, [=] () { - d->data->setLastEvent(syncJob->nextBatch()); - for( const auto& roomData: syncJob->roomData() ) + if (d->syncJob) + return d->syncJob; + + const QString filter = "{\"room\": { \"timeline\": { \"limit\": 100 } } }"; + auto job = d->startSyncJob(filter, timeout); + connect( job, &SyncJob::success, [=] () { + d->data->setLastEvent(job->nextBatch()); + for( const auto& roomData: job->roomData() ) { if ( Room* r = provideRoom(roomData.roomId) ) r->updateData(roomData); } + d->syncJob = nullptr; emit syncDone(); }); - connect( syncJob, &SyncJob::failure, [=] () { - if (syncJob->error() == BaseJob::ContentAccessError) - emit loginError(syncJob->errorString()); + connect( job, &SyncJob::failure, [=] () { + d->syncJob = nullptr; + if (job->error() == BaseJob::ContentAccessError) + emit loginError(job->errorString()); else - emit connectionError(syncJob->errorString()); + emit connectionError(job->errorString()); }); + return job; +} + +SyncJob* Connection::Private::startSyncJob(const QString& filter, int timeout) +{ + syncJob = new SyncJob(data, data->lastEvent()); + syncJob->setFilter(filter); + syncJob->setTimeout(timeout); syncJob->start(); return syncJob; + } void Connection::postMessage(Room* room, QString type, QString message) diff --git a/connection.h b/connection.h index bfb4d377..c221d7eb 100644 --- a/connection.h +++ b/connection.h @@ -49,6 +49,7 @@ namespace QMatrixClient Q_INVOKABLE virtual void connectToServer( QString user, QString password ); Q_INVOKABLE virtual void connectWithToken( QString userId, QString token ); Q_INVOKABLE virtual void reconnect(); + Q_INVOKABLE virtual void disconnectFromServer(); Q_INVOKABLE virtual void logout(); Q_INVOKABLE virtual SyncJob* sync(int timeout=-1); -- cgit v1.2.3