diff options
author | KitsuneRal <Kitsune-Ral@users.sf.net> | 2016-08-23 08:46:30 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-08-23 08:46:30 +0900 |
commit | ac0336ff600d8b978d3cdb68cd92b3425fe0b100 (patch) | |
tree | 4d48c6f13cfd5494696a3e270421b3ab63124f70 /jobs/basejob.h | |
parent | c2e38f28987b4fa273765b4234c6a57bdf75e446 (diff) | |
parent | f6c623a27bcb5ec2fcc83930e500afb597a32a46 (diff) | |
download | libquotient-ac0336ff600d8b978d3cdb68cd92b3425fe0b100.tar.gz libquotient-ac0336ff600d8b978d3cdb68cd92b3425fe0b100.zip |
Merge pull request #15 from Fxrh/kitsune-dropped-kcoreaddons
Upon discussion with @Fxrh in #quaternion, this now comes in master,
Diffstat (limited to 'jobs/basejob.h')
-rw-r--r-- | jobs/basejob.h | 152 |
1 files changed, 129 insertions, 23 deletions
diff --git a/jobs/basejob.h b/jobs/basejob.h index 150d39e8..07b1f1dd 100644 --- a/jobs/basejob.h +++ b/jobs/basejob.h @@ -19,16 +19,14 @@ #ifndef QMATRIXCLIENT_BASEJOB_H #define QMATRIXCLIENT_BASEJOB_H -#ifdef USING_SYSTEM_KCOREADDONS -#include <KCoreAddons/KJob> -#else -#include "kjob.h" -#endif // KCOREADDONS_FOUND - +#include <QtCore/QObject> #include <QtCore/QJsonDocument> #include <QtCore/QJsonObject> #include <QtCore/QUrlQuery> -#include <QtNetwork/QNetworkReply> +#include <QtCore/QScopedPointer> + +class QNetworkReply; +class QSslError; namespace QMatrixClient { @@ -36,28 +34,104 @@ 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 StatusCode { NoError = 0 // To be compatible with Qt conventions + , Success = 0 + , ErrorLevel = 100 // Errors have codes starting from this + , NetworkError = 100 + , JsonParseError + , TimeoutError + , ContentAccessError + , UserDefinedError = 200 + }; + + /** + * This structure stores the status of a server call job. The status consists + * of a code, that is described (but not delimited) by the respective enum, + * and a freeform message. + * + * To extend the list of error codes, define an (anonymous) enum + * along the lines of StatusCode, with additional values + * starting at UserDefinedError + */ + class Status + { + public: + Status(StatusCode c) : code(c) { } + Status(int c, QString m) : code(c), message(m) { } + + bool good() const { return code < ErrorLevel; } + + int code; + QString message; + }; + + public: BaseJob(ConnectionData* connection, JobHttpType type, QString name, bool needsToken=true); virtual ~BaseJob(); - void start() override; + void start(); + + /** + * 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(); - enum ErrorCode { NetworkError = KJob::UserDefinedError, - JsonParseError, TimeoutError, ContentAccessError, - UserDefinedError = 512 }; + Status status() const; + 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*); @@ -68,23 +142,55 @@ namespace QMatrixClient virtual QString apiPath() const = 0; virtual QUrlQuery query() const; virtual QJsonObject data() const; - virtual void parseJson(const QJsonDocument& data); - - void fail( int errorCode, QString errorString ); - QNetworkReply* networkReply() const; + /** + * Used by gotReply() slot to check the received reply for general + * issues such as network errors or access denial. + * Returning anything except NoError/Success prevents + * further parseReply()/parseJson() invocation. + * + * @param reply the reply received from the server + * @return the result of checking the reply + * + * @see gotReply + */ + virtual Status checkReply(QNetworkReply* reply) const; + + /** + * Processes the reply. By default, parses the reply into + * a QJsonDocument and calls parseJson() if it's a valid JSON. + * + * @param data raw contents of a HTTP reply from the server (without headers) + * + * @see gotReply, parseJson + */ + virtual Status parseReply(QByteArray data); + + /** + * Processes the JSON document received from the Matrix server. + * By default returns succesful status without analysing the JSON. + * + * @param json valid JSON document received from the server + * + * @see parseReply + */ + virtual Status parseJson(const QJsonDocument&); + void setStatus(Status s); + void setStatus(int code, QString message); + protected slots: - virtual void gotReply(); void timeout(); void sslErrors(const QList<QSslError>& errors); - //void networkError(QNetworkReply::NetworkError code); - + private slots: + void gotReply(); private: + void finishJob(bool emitResult); + class Private; - Private* d; + QScopedPointer<Private> d; }; } |