diff options
author | Felix Rohrbach <fxrh@gmx.de> | 2016-05-08 18:59:11 +0200 |
---|---|---|
committer | Felix Rohrbach <fxrh@gmx.de> | 2016-05-08 18:59:11 +0200 |
commit | 1cd8802c73ee3c0cc0095f8be30a9bbf3442c3ca (patch) | |
tree | fa0f4aded71f27974c9922bd587ba78774ade46c /jobs | |
parent | fc95edb7a63bcacb02418c55e15a1aa21ac080cd (diff) | |
parent | 5a0e6080a6245aa2c68f254d7105f19629a5a654 (diff) | |
download | libquotient-1cd8802c73ee3c0cc0095f8be30a9bbf3442c3ca.tar.gz libquotient-1cd8802c73ee3c0cc0095f8be30a9bbf3442c3ca.zip |
Merge pull request #5 from KitsuneRal/basejob-improvement
Basejob improvement
Diffstat (limited to 'jobs')
-rw-r--r-- | jobs/basejob.cpp | 19 | ||||
-rw-r--r-- | jobs/basejob.h | 16 |
2 files changed, 30 insertions, 5 deletions
diff --git a/jobs/basejob.cpp b/jobs/basejob.cpp index 519e1517..cfdf8a28 100644 --- a/jobs/basejob.cpp +++ b/jobs/basejob.cpp @@ -42,12 +42,23 @@ class BaseJob::Private BaseJob::BaseJob(ConnectionData* connection, JobHttpType type, bool needsToken) : d(new Private(connection, type, needsToken)) { + // Work around KJob inability to separate success and failure signals + connect(this, &BaseJob::result, [this]() { + if (error() == NoError) + emit success(this); + else + emit failure(this); + }); } BaseJob::~BaseJob() { if( d->reply ) + { + if( d->reply->isRunning() ) + d->reply->abort(); d->reply->deleteLater(); + } delete d; } @@ -108,6 +119,9 @@ void BaseJob::fail(int errorCode, QString errorString) { setError( errorCode ); setErrorText( errorString ); + if( d->reply->isRunning() ) + d->reply->abort(); + qWarning() << this << "failed:" << errorString; emitResult(); } @@ -125,7 +139,7 @@ void BaseJob::gotReply() { if( d->reply->error() != QNetworkReply::NoError ) { - qDebug() << "NetworkError!!!" << d->reply->error(); + qDebug() << "NetworkError:" << d->reply->error(); fail( NetworkError, d->reply->errorString() ); return; } @@ -142,8 +156,7 @@ void BaseJob::gotReply() void BaseJob::timeout() { qDebug() << "Timeout!"; - if( d->reply->isRunning() ) - d->reply->abort(); + fail( TimeoutError, "The job has timed out" ); } void BaseJob::sslErrors(const QList<QSslError>& errors) diff --git a/jobs/basejob.h b/jobs/basejob.h index 95cf4232..f1ad66d1 100644 --- a/jobs/basejob.h +++ b/jobs/basejob.h @@ -45,8 +45,20 @@ namespace QMatrixClient void start() override; - enum ErrorCode { NetworkError = KJob::UserDefinedError, JsonParseError, UserDefinedError }; - + enum ErrorCode { NetworkError = KJob::UserDefinedError, + JsonParseError, TimeoutError, UserDefinedError }; + + signals: + /** + * Emitted together with KJob::result() but only if there's no error. + */ + void success(BaseJob*); + /** + * Emitted together with KJob::result() if there's an error. + * Same as result(), this won't be emitted in case of kill(Quietly). + */ + void failure(BaseJob*); + protected: ConnectionData* connection() const; |