diff options
Diffstat (limited to 'jobs/basejob.h')
-rw-r--r-- | jobs/basejob.h | 102 |
1 files changed, 85 insertions, 17 deletions
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; }; |