aboutsummaryrefslogtreecommitdiff
path: root/jobs
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2016-05-05 19:19:52 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2016-07-26 16:51:11 +0900
commit2eb18a735a5f75a77387a211f4311222d00c2d6c (patch)
treee9b6232d9e75eeeb73354d2b2f0ecf0d37bed3e9 /jobs
parent3c11f55bfd2b626e330277255bb0c38fbbae84fa (diff)
downloadlibquotient-2eb18a735a5f75a77387a211f4311222d00c2d6c.tar.gz
libquotient-2eb18a735a5f75a77387a211f4311222d00c2d6c.zip
Rewritten BaseJob to not depend on KJob.
Some parts of the code were copied from the KCoreAddons sources - surprisingly few, in fact, mostly API with comments. With this commit, libqmatrixclient doesn't depend on KCoreAddons.
Diffstat (limited to 'jobs')
-rw-r--r--jobs/basejob.cpp67
-rw-r--r--jobs/basejob.h102
2 files changed, 139 insertions, 30 deletions
diff --git a/jobs/basejob.cpp b/jobs/basejob.cpp
index 6c68ab66..2c95ef11 100644
--- a/jobs/basejob.cpp
+++ b/jobs/basejob.cpp
@@ -31,24 +31,21 @@ class BaseJob::Private
{
public:
Private(ConnectionData* c, JobHttpType t, bool nt)
- : connection(c), reply(nullptr), type(t), needsToken(nt) {}
+ : connection(c), reply(nullptr), type(t), needsToken(nt), errorCode(NoError)
+ {}
ConnectionData* connection;
QNetworkReply* reply;
JobHttpType type;
bool needsToken;
+
+ int errorCode;
+ QString errorText;
};
BaseJob::BaseJob(ConnectionData* connection, JobHttpType type, QString name, 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);
- });
setObjectName(name);
qDebug() << "Job" << objectName() << " created";
}
@@ -119,6 +116,55 @@ void BaseJob::start()
// this, &BaseJob::networkError ); // http://doc.qt.io/qt-5/qnetworkreply.html#error-1
}
+void BaseJob::finishJob(bool emitResult)
+{
+ if( d->reply->isRunning() )
+ d->reply->abort();
+
+ // Notify those that are interested in any completion of the job (including killing)
+ emit finished(this);
+
+ if (emitResult) {
+ emit result(this);
+ if (error())
+ emit failure(this);
+ else
+ emit success(this);
+ }
+
+ deleteLater();
+}
+
+int BaseJob::error() const
+{
+ return d->errorCode;
+}
+
+QString BaseJob::errorString() const
+{
+ return d->errorText;
+}
+
+void BaseJob::setError(int errorCode)
+{
+ d->errorCode = errorCode;
+}
+
+void BaseJob::setErrorText(QString errorText)
+{
+ d->errorText = errorText;
+}
+
+void BaseJob::emitResult()
+{
+ finishJob(true);
+}
+
+void BaseJob::abandon()
+{
+ finishJob(false);
+}
+
void BaseJob::fail(int errorCode, QString errorString)
{
setError( errorCode );
@@ -134,11 +180,6 @@ QNetworkReply* BaseJob::networkReply() const
return d->reply;
}
-// void BaseJob::networkError(QNetworkReply::NetworkError code)
-// {
-// fail( KJob::UserDefinedError+1, d->reply->errorString() );
-// }
-
void BaseJob::gotReply()
{
switch( d->reply->error() )
diff --git a/jobs/basejob.h b/jobs/basejob.h
index 150d39e8..9d00c0ac 100644
--- a/jobs/basejob.h
+++ b/jobs/basejob.h
@@ -19,12 +19,6 @@
#ifndef QMATRIXCLIENT_BASEJOB_H
#define QMATRIXCLIENT_BASEJOB_H
-#ifdef USING_SYSTEM_KCOREADDONS
-#include <KCoreAddons/KJob>
-#else
-#include "kjob.h"
-#endif // KCOREADDONS_FOUND
-
#include <QtCore/QJsonDocument>
#include <QtCore/QJsonObject>
#include <QtCore/QUrlQuery>
@@ -36,28 +30,75 @@ namespace QMatrixClient
enum class JobHttpType { GetJob, PutJob, PostJob };
- class BaseJob: public KJob
+ class BaseJob: public QObject
{
Q_OBJECT
public:
+ /* Just in case, the values are compatible with KJob
+ * (which BaseJob used to inherit from). */
+ enum ErrorCode { NoError = 0, NetworkError = 100,
+ JsonParseError, TimeoutError, ContentAccessError,
+ UserDefinedError = 512 };
+
BaseJob(ConnectionData* connection, JobHttpType type,
QString name, bool needsToken=true);
virtual ~BaseJob();
- void start() override;
+ void start();
- enum ErrorCode { NetworkError = KJob::UserDefinedError,
- JsonParseError, TimeoutError, ContentAccessError,
- UserDefinedError = 512 };
+ /**
+ * Abandons the result of this job, arrived or unarrived.
+ *
+ * This aborts waiting for a reply from the server (if there was
+ * any pending) and deletes the job object. It is always done quietly
+ * (as opposed to KJob::kill() that can trigger emitting the result).
+ */
+ void abandon();
+
+ int error() const;
+ virtual QString errorString() const;
signals:
/**
- * Emitted together with KJob::result() but only if there's no error.
+ * Emitted when the job is finished, in any case. It is used to notify
+ * observers that the job is terminated and that progress can be hidden.
+ *
+ * This should not be emitted directly by subclasses;
+ * use emitResult() instead.
+ *
+ * In general, to be notified of a job's completion, client code
+ * should connect to success() and failure()
+ * rather than finished(), so that kill() is indeed quiet.
+ * However if you store a list of jobs and they might get killed
+ * silently, then you must connect to this instead of result(),
+ * to avoid dangling pointers in your list.
+ *
+ * @param job the job that emitted this signal
+ * @internal
+ *
+ * @see success, failure
+ */
+ void finished(BaseJob* job);
+
+ /**
+ * Emitted when the job is finished (except when killed).
+ *
+ * Use error to know if the job was finished with error.
+ *
+ * @param job the job that emitted this signal
+ *
+ * @see success, failure
+ */
+ void result(BaseJob* job);
+
+ /**
+ * Emitted together with 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).
+ * Emitted together with result() if there's an error.
+ * Same as result(), this won't be emitted in case of kill().
*/
void failure(BaseJob*);
@@ -70,6 +111,34 @@ namespace QMatrixClient
virtual QJsonObject data() const;
virtual void parseJson(const QJsonDocument& data);
+ /**
+ * Sets the error code.
+ *
+ * It should be called when an error is encountered in the job,
+ * just before calling emitResult(). Normally you might want to
+ * use fail() instead - it sets error code, error text, makes sure
+ * the job has finished and invokes emitResult after that.
+ *
+ * To extend the list of error codes, define an (anonymous) enum
+ * with additional values starting at BaseJob::UserDefinedError
+ *
+ * @param errorCode the error code
+ * @see emitResult(), fail()
+ */
+ void setError(int errorCode);
+ void setErrorText(QString errorText);
+
+ /**
+ * Utility function to emit the result signal, and suicide this job.
+ * It first notifies the observers to hide the progress for this job using
+ * the finished() signal.
+ *
+ * @note: Deletes this job using deleteLater().
+ *
+ * @see result()
+ * @see finished()
+ */
+ void emitResult();
void fail( int errorCode, QString errorString );
QNetworkReply* networkReply() const;
@@ -79,10 +148,9 @@ namespace QMatrixClient
void timeout();
void sslErrors(const QList<QSslError>& errors);
- //void networkError(QNetworkReply::NetworkError code);
-
-
private:
+ void finishJob(bool emitResult);
+
class Private;
Private* d;
};