aboutsummaryrefslogtreecommitdiff
path: root/jobs/basejob.cpp
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/basejob.cpp
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/basejob.cpp')
-rw-r--r--jobs/basejob.cpp67
1 files changed, 54 insertions, 13 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() )