aboutsummaryrefslogtreecommitdiff
path: root/connection.cpp
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2016-09-06 16:12:11 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2016-09-06 16:12:11 +0900
commit866cb97668fadff50dea8d16792c3b6736d531dc (patch)
treeb110251b7793af45e94671529c73be8c8ad17364 /connection.cpp
parent334e4ba4ce410c8426abcd78a361b9a23b14eec7 (diff)
downloadlibquotient-866cb97668fadff50dea8d16792c3b6736d531dc.tar.gz
libquotient-866cb97668fadff50dea8d16792c3b6736d531dc.zip
Remember the current SyncJob and allow to explicitly abandon it
Diffstat (limited to 'connection.cpp')
-rw-r--r--connection.cpp45
1 files changed, 34 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)