aboutsummaryrefslogtreecommitdiff
path: root/jobs
diff options
context:
space:
mode:
Diffstat (limited to 'jobs')
-rw-r--r--jobs/basejob.cpp15
-rw-r--r--jobs/basejob.h39
-rw-r--r--jobs/generated/content-repo.cpp20
-rw-r--r--jobs/generated/content-repo.h11
-rw-r--r--jobs/mediathumbnailjob.cpp13
-rw-r--r--jobs/mediathumbnailjob.h4
-rw-r--r--jobs/requestdata.cpp38
-rw-r--r--jobs/requestdata.h59
8 files changed, 140 insertions, 59 deletions
diff --git a/jobs/basejob.cpp b/jobs/basejob.cpp
index 1f079966..4e0879b0 100644
--- a/jobs/basejob.cpp
+++ b/jobs/basejob.cpp
@@ -45,7 +45,7 @@ class BaseJob::Private
public:
// Using an idiom from clang-tidy:
// http://clang.llvm.org/extra/clang-tidy/checks/modernize-pass-by-value.html
- Private(HttpVerb v, QString endpoint, QUrlQuery q, Data data, bool nt)
+ Private(HttpVerb v, QString endpoint, QUrlQuery q, Data&& data, bool nt)
: verb(v), apiEndpoint(std::move(endpoint))
, requestQuery(std::move(q)), requestData(std::move(data))
, needsToken(nt)
@@ -100,8 +100,8 @@ BaseJob::BaseJob(HttpVerb verb, const QString& name, const QString& endpoint, bo
{ }
BaseJob::BaseJob(HttpVerb verb, const QString& name, const QString& endpoint,
- const Query& query, const Data& data, bool needsToken)
- : d(new Private(verb, endpoint, query, data, needsToken))
+ const Query& query, Data&& data, bool needsToken)
+ : d(new Private(verb, endpoint, query, std::move(data), needsToken))
{
setObjectName(name);
setExpectedContentTypes({ "application/json" });
@@ -158,9 +158,9 @@ const BaseJob::Data& BaseJob::requestData() const
return d->requestData;
}
-void BaseJob::setRequestData(const BaseJob::Data& data)
+void BaseJob::setRequestData(Data&& data)
{
- d->requestData = data;
+ std::swap(d->requestData, data);
}
const QByteArrayList& BaseJob::expectedContentTypes() const
@@ -205,10 +205,10 @@ void BaseJob::Private::sendRequest()
reply.reset( connection->nam()->get(req) );
break;
case HttpVerb::Post:
- reply.reset( connection->nam()->post(req, requestData.serialize()) );
+ reply.reset( connection->nam()->post(req, requestData.source()) );
break;
case HttpVerb::Put:
- reply.reset( connection->nam()->put(req, requestData.serialize()) );
+ reply.reset( connection->nam()->put(req, requestData.source()) );
break;
case HttpVerb::Delete:
reply.reset( connection->nam()->deleteResource(req) );
@@ -446,4 +446,3 @@ void BaseJob::setLoggingCategory(LoggingCategory lcf)
{
d->logCat = lcf;
}
-
diff --git a/jobs/basejob.h b/jobs/basejob.h
index 1fe3a24d..4567bca7 100644
--- a/jobs/basejob.h
+++ b/jobs/basejob.h
@@ -18,13 +18,15 @@
#pragma once
-#include "logging.h"
+#include "../logging.h"
+#include "requestdata.h"
#include <QtCore/QObject>
+#include <QtCore/QUrlQuery>
+
+// Any job that parses the response will need the below two.
#include <QtCore/QJsonDocument>
#include <QtCore/QJsonObject>
-#include <QtCore/QJsonArray>
-#include <QtCore/QUrlQuery>
class QNetworkReply;
class QSslError;
@@ -76,33 +78,8 @@ namespace QMatrixClient
setQueryItems(l);
}
};
- /**
- * A simple wrapper that represents the request body.
- * Provides a unified interface to dump an unstructured byte stream
- * as well as JSON (and possibly other structures in the future) to
- * a QByteArray consumed by QNetworkAccessManager request methods.
- */
- class Data
- {
- public:
- Data() = default;
- Data(const QByteArray& a) : _payload(a) { }
- Data(const QJsonObject& jo)
- : _payload(fromJson(QJsonDocument(jo))) { }
- Data(const QJsonArray& ja)
- : _payload(fromJson(QJsonDocument(ja))) { }
- QByteArray serialize() const
- {
- return _payload;
- }
- private:
- static QByteArray fromJson(const QJsonDocument& jd)
- {
- return jd.toJson(QJsonDocument::Compact);
- }
- QByteArray _payload;
- };
+ using Data = RequestData;
/**
* This structure stores the status of a server call job. The status consists
@@ -132,7 +109,7 @@ namespace QMatrixClient
BaseJob(HttpVerb verb, const QString& name, const QString& endpoint,
bool needsToken = true);
BaseJob(HttpVerb verb, const QString& name, const QString& endpoint,
- const Query& query, const Data& data = {},
+ const Query& query, Data&& data = {},
bool needsToken = true);
Status status() const;
@@ -227,7 +204,7 @@ namespace QMatrixClient
const QUrlQuery& query() const;
void setRequestQuery(const QUrlQuery& query);
const Data& requestData() const;
- void setRequestData(const Data& data);
+ void setRequestData(Data&& data);
const QByteArrayList& expectedContentTypes() const;
void addExpectedContentType(const QByteArray& contentType);
void setExpectedContentTypes(const QByteArrayList& contentTypes);
diff --git a/jobs/generated/content-repo.cpp b/jobs/generated/content-repo.cpp
index ec6683bb..93aa838c 100644
--- a/jobs/generated/content-repo.cpp
+++ b/jobs/generated/content-repo.cpp
@@ -19,7 +19,7 @@ class UploadContentJob::Private
QString contentUri;
};
-UploadContentJob::UploadContentJob(QByteArray content, const QString& filename, const QString& contentType)
+UploadContentJob::UploadContentJob(QIODevice* content, const QString& filename, const QString& contentType)
: BaseJob(HttpVerb::Post, "UploadContentJob",
basePath % "/upload")
, d(new Private)
@@ -55,7 +55,7 @@ class GetContentJob::Private
public:
QString contentType;
QString contentDisposition;
- QByteArray content;
+ QIODevice* content;
};
GetContentJob::GetContentJob(const QString& serverName, const QString& mediaId)
@@ -78,7 +78,7 @@ const QString& GetContentJob::contentDisposition() const
return d->contentDisposition;
}
-QByteArray GetContentJob::content() const
+QIODevice* GetContentJob::content() const
{
return d->content;
}
@@ -87,7 +87,7 @@ BaseJob::Status GetContentJob::parseReply(QNetworkReply* reply)
{
d->contentType = reply->rawHeader("Content-Type");
d->contentDisposition = reply->rawHeader("Content-Disposition");
- d->content = reply->readAll();
+ d->content = reply;
return Success;
}
@@ -96,7 +96,7 @@ class GetContentOverrideNameJob::Private
public:
QString contentType;
QString contentDisposition;
- QByteArray content;
+ QIODevice* content;
};
GetContentOverrideNameJob::GetContentOverrideNameJob(const QString& serverName, const QString& mediaId, const QString& fileName)
@@ -119,7 +119,7 @@ const QString& GetContentOverrideNameJob::contentDisposition() const
return d->contentDisposition;
}
-QByteArray GetContentOverrideNameJob::content() const
+QIODevice* GetContentOverrideNameJob::content() const
{
return d->content;
}
@@ -128,7 +128,7 @@ BaseJob::Status GetContentOverrideNameJob::parseReply(QNetworkReply* reply)
{
d->contentType = reply->rawHeader("Content-Type");
d->contentDisposition = reply->rawHeader("Content-Disposition");
- d->content = reply->readAll();
+ d->content = reply;
return Success;
}
@@ -136,7 +136,7 @@ class GetContentThumbnailJob::Private
{
public:
QString contentType;
- QByteArray content;
+ QIODevice* content;
};
GetContentThumbnailJob::GetContentThumbnailJob(const QString& serverName, const QString& mediaId, int width, int height, const QString& method)
@@ -160,7 +160,7 @@ const QString& GetContentThumbnailJob::contentType() const
return d->contentType;
}
-QByteArray GetContentThumbnailJob::content() const
+QIODevice* GetContentThumbnailJob::content() const
{
return d->content;
}
@@ -168,7 +168,7 @@ QByteArray GetContentThumbnailJob::content() const
BaseJob::Status GetContentThumbnailJob::parseReply(QNetworkReply* reply)
{
d->contentType = reply->rawHeader("Content-Type");
- d->content = reply->readAll();
+ d->content = reply;
return Success;
}
diff --git a/jobs/generated/content-repo.h b/jobs/generated/content-repo.h
index 1d844651..0796322b 100644
--- a/jobs/generated/content-repo.h
+++ b/jobs/generated/content-repo.h
@@ -6,8 +6,7 @@
#include "../basejob.h"
-#include <QtCore/QByteArray>
-#include <QtCore/QString>
+#include <QtCore/QIODevice>
namespace QMatrixClient
@@ -17,7 +16,7 @@ namespace QMatrixClient
class UploadContentJob : public BaseJob
{
public:
- explicit UploadContentJob(QByteArray content, const QString& filename = {}, const QString& contentType = {});
+ explicit UploadContentJob(QIODevice* content, const QString& filename = {}, const QString& contentType = {});
~UploadContentJob() override;
const QString& contentUri() const;
@@ -38,7 +37,7 @@ namespace QMatrixClient
const QString& contentType() const;
const QString& contentDisposition() const;
- QByteArray content() const;
+ QIODevice* content() const;
protected:
Status parseReply(QNetworkReply* reply) override;
@@ -56,7 +55,7 @@ namespace QMatrixClient
const QString& contentType() const;
const QString& contentDisposition() const;
- QByteArray content() const;
+ QIODevice* content() const;
protected:
Status parseReply(QNetworkReply* reply) override;
@@ -73,7 +72,7 @@ namespace QMatrixClient
~GetContentThumbnailJob() override;
const QString& contentType() const;
- QByteArray content() const;
+ QIODevice* content() const;
protected:
Status parseReply(QNetworkReply* reply) override;
diff --git a/jobs/mediathumbnailjob.cpp b/jobs/mediathumbnailjob.cpp
index d768d253..ec82f57b 100644
--- a/jobs/mediathumbnailjob.cpp
+++ b/jobs/mediathumbnailjob.cpp
@@ -20,8 +20,15 @@
using namespace QMatrixClient;
-MediaThumbnailJob::MediaThumbnailJob(QUrl url, QSize requestedSize)
- : GetContentThumbnailJob(url.host(), url.path().mid(1),
+MediaThumbnailJob::MediaThumbnailJob(const QString& serverName,
+ const QString& mediaId, QSize requestedSize)
+ : GetContentThumbnailJob(serverName, mediaId,
+ requestedSize.width(), requestedSize.height())
+{ }
+
+MediaThumbnailJob::MediaThumbnailJob(const QUrl& mxcUri, QSize requestedSize)
+ : GetContentThumbnailJob(mxcUri.authority(),
+ mxcUri.path().mid(1), // sans leading '/'
requestedSize.width(), requestedSize.height())
{ }
@@ -39,7 +46,7 @@ QImage MediaThumbnailJob::scaledThumbnail(QSize toSize) const
BaseJob::Status MediaThumbnailJob::parseReply(QNetworkReply* reply)
{
GetContentThumbnailJob::parseReply(reply);
- if( !_thumbnail.loadFromData(content()) )
+ if( !_thumbnail.loadFromData(content()->readAll()) )
{
qCDebug(JOBS) << "MediaThumbnailJob: could not read image data";
}
diff --git a/jobs/mediathumbnailjob.h b/jobs/mediathumbnailjob.h
index 66960b75..ef834cd7 100644
--- a/jobs/mediathumbnailjob.h
+++ b/jobs/mediathumbnailjob.h
@@ -27,7 +27,9 @@ namespace QMatrixClient
class MediaThumbnailJob: public GetContentThumbnailJob
{
public:
- MediaThumbnailJob(QUrl url, QSize requestedSize);
+ MediaThumbnailJob(const QString& serverName, const QString& mediaId,
+ QSize requestedSize);
+ MediaThumbnailJob(const QUrl& mxcUri, QSize requestedSize);
QImage thumbnail() const;
QImage scaledThumbnail(QSize toSize) const;
diff --git a/jobs/requestdata.cpp b/jobs/requestdata.cpp
new file mode 100644
index 00000000..f5516c5f
--- /dev/null
+++ b/jobs/requestdata.cpp
@@ -0,0 +1,38 @@
+#include "requestdata.h"
+
+#include <QtCore/QByteArray>
+#include <QtCore/QJsonObject>
+#include <QtCore/QJsonArray>
+#include <QtCore/QJsonDocument>
+#include <QtCore/QBuffer>
+
+using namespace QMatrixClient;
+
+std::unique_ptr<QIODevice> fromData(const QByteArray& data)
+{
+ auto source = std::make_unique<QBuffer>();
+ source->open(QIODevice::WriteOnly);
+ source->write(data);
+ source->close();
+ return source;
+}
+
+template <typename JsonDataT>
+inline std::unique_ptr<QIODevice> fromJson(const JsonDataT& jdata)
+{
+ return fromData(QJsonDocument(jdata).toJson(QJsonDocument::Compact));
+}
+
+RequestData::RequestData(const QByteArray& a)
+ : _source(fromData(a))
+{ }
+
+RequestData::RequestData(const QJsonObject& jo)
+ : _source(fromJson(jo))
+{ }
+
+RequestData::RequestData(const QJsonArray& ja)
+ : _source(fromJson(ja))
+{ }
+
+RequestData::~RequestData() = default;
diff --git a/jobs/requestdata.h b/jobs/requestdata.h
new file mode 100644
index 00000000..aa03b744
--- /dev/null
+++ b/jobs/requestdata.h
@@ -0,0 +1,59 @@
+/******************************************************************************
+ * Copyright (C) 2018 Kitsune Ral <kitsune-ral@users.sf.net>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#pragma once
+
+#include <memory>
+
+class QByteArray;
+class QJsonObject;
+class QJsonArray;
+class QJsonDocument;
+class QIODevice;
+
+namespace QMatrixClient
+{
+ /**
+ * A simple wrapper that represents the request body.
+ * Provides a unified interface to dump an unstructured byte stream
+ * as well as JSON (and possibly other structures in the future) to
+ * a QByteArray consumed by QNetworkAccessManager request methods.
+ */
+ class RequestData
+ {
+ public:
+ RequestData() = default;
+ RequestData(const QByteArray& a);
+ RequestData(const QJsonObject& jo);
+ RequestData(const QJsonArray& ja);
+ RequestData(QIODevice* source)
+ : _source(std::unique_ptr<QIODevice>(source))
+ { }
+ RequestData(RequestData&&) = default;
+ RequestData& operator=(RequestData&&) = default;
+ ~RequestData();
+
+ QIODevice* source() const
+ {
+ return _source.get();
+ }
+
+ private:
+ std::unique_ptr<QIODevice> _source;
+ };
+} // namespace QMatrixClient