From f951ab8a931c68f61b9710a540b2c971bbf502ca Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Wed, 3 Jan 2018 12:11:16 +0900 Subject: jobs/generated: Polish formatting; other minor cleanup --- jobs/generated/administrative_contact.cpp | 8 +------- jobs/generated/administrative_contact.h | 8 ++++---- jobs/generated/banning.cpp | 1 - jobs/generated/banning.h | 1 - jobs/generated/directory.cpp | 6 +----- jobs/generated/directory.h | 7 +++---- jobs/generated/inviting.cpp | 1 - jobs/generated/inviting.h | 1 - jobs/generated/kicking.cpp | 1 - jobs/generated/kicking.h | 1 - jobs/generated/leaving.cpp | 1 - jobs/generated/leaving.h | 1 - jobs/generated/list_public_rooms.cpp | 13 ++----------- jobs/generated/list_public_rooms.h | 13 ++++++------- jobs/generated/login.cpp | 6 +----- jobs/generated/login.h | 7 +++---- jobs/generated/logout.cpp | 1 - jobs/generated/logout.h | 1 - jobs/generated/profile.cpp | 16 +++------------- jobs/generated/profile.h | 19 +++++++++---------- jobs/generated/receipts.cpp | 1 - jobs/generated/receipts.h | 1 - jobs/generated/redaction.cpp | 6 +----- jobs/generated/redaction.h | 7 +++---- jobs/generated/third_party_membership.cpp | 1 - jobs/generated/third_party_membership.h | 1 - jobs/generated/typing.cpp | 1 - jobs/generated/typing.h | 1 - jobs/generated/versions.cpp | 6 +----- jobs/generated/versions.h | 7 +++---- jobs/generated/whoami.cpp | 6 +----- jobs/generated/whoami.h | 7 +++---- 32 files changed, 45 insertions(+), 113 deletions(-) (limited to 'jobs') diff --git a/jobs/generated/administrative_contact.cpp b/jobs/generated/administrative_contact.cpp index 705c5d54..ca029f58 100644 --- a/jobs/generated/administrative_contact.cpp +++ b/jobs/generated/administrative_contact.cpp @@ -2,11 +2,8 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #include "administrative_contact.h" -#include "converters.h" - #include using namespace QMatrixClient; @@ -52,10 +49,7 @@ GetAccount3PIDsJob::GetAccount3PIDsJob() ), d(new Private) { } -GetAccount3PIDsJob::~GetAccount3PIDsJob() -{ - delete d; -} +GetAccount3PIDsJob::~GetAccount3PIDsJob() = default; const QVector& GetAccount3PIDsJob::threepids() const { diff --git a/jobs/generated/administrative_contact.h b/jobs/generated/administrative_contact.h index fa6beba9..a5f04781 100644 --- a/jobs/generated/administrative_contact.h +++ b/jobs/generated/administrative_contact.h @@ -2,12 +2,12 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #pragma once #include "../basejob.h" #include +#include #include "converters.h" @@ -34,13 +34,13 @@ namespace QMatrixClient ~GetAccount3PIDsJob() override; const QVector& threepids() const; - + protected: Status parseJson(const QJsonDocument& data) override; - + private: class Private; - Private* d; + QScopedPointer d; }; class Post3PIDsJob : public BaseJob diff --git a/jobs/generated/banning.cpp b/jobs/generated/banning.cpp index 96f80ea8..d7708cc6 100644 --- a/jobs/generated/banning.cpp +++ b/jobs/generated/banning.cpp @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #include "banning.h" #include "converters.h" diff --git a/jobs/generated/banning.h b/jobs/generated/banning.h index 6db096ee..930020a5 100644 --- a/jobs/generated/banning.h +++ b/jobs/generated/banning.h @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #pragma once #include "../basejob.h" diff --git a/jobs/generated/directory.cpp b/jobs/generated/directory.cpp index dcec75ac..1fd1e443 100644 --- a/jobs/generated/directory.cpp +++ b/jobs/generated/directory.cpp @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #include "directory.h" #include "converters.h" @@ -39,10 +38,7 @@ GetRoomIdByAliasJob::GetRoomIdByAliasJob(const QString& roomAlias) ), d(new Private) { } -GetRoomIdByAliasJob::~GetRoomIdByAliasJob() -{ - delete d; -} +GetRoomIdByAliasJob::~GetRoomIdByAliasJob() = default; const QString& GetRoomIdByAliasJob::roomId() const { diff --git a/jobs/generated/directory.h b/jobs/generated/directory.h index 1dd4e7ed..8290a2b5 100644 --- a/jobs/generated/directory.h +++ b/jobs/generated/directory.h @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #pragma once #include "../basejob.h" @@ -29,13 +28,13 @@ namespace QMatrixClient const QString& roomId() const; const QVector& servers() const; - + protected: Status parseJson(const QJsonDocument& data) override; - + private: class Private; - Private* d; + QScopedPointer d; }; class DeleteRoomAliasJob : public BaseJob diff --git a/jobs/generated/inviting.cpp b/jobs/generated/inviting.cpp index 5f89adf7..bdf257dd 100644 --- a/jobs/generated/inviting.cpp +++ b/jobs/generated/inviting.cpp @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #include "inviting.h" #include "converters.h" diff --git a/jobs/generated/inviting.h b/jobs/generated/inviting.h index 225cb516..7ed49637 100644 --- a/jobs/generated/inviting.h +++ b/jobs/generated/inviting.h @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #pragma once #include "../basejob.h" diff --git a/jobs/generated/kicking.cpp b/jobs/generated/kicking.cpp index 86dde629..3488b387 100644 --- a/jobs/generated/kicking.cpp +++ b/jobs/generated/kicking.cpp @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #include "kicking.h" #include "converters.h" diff --git a/jobs/generated/kicking.h b/jobs/generated/kicking.h index 7c834e45..84d88945 100644 --- a/jobs/generated/kicking.h +++ b/jobs/generated/kicking.h @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #pragma once #include "../basejob.h" diff --git a/jobs/generated/leaving.cpp b/jobs/generated/leaving.cpp index 2cf7fda3..604fcc73 100644 --- a/jobs/generated/leaving.cpp +++ b/jobs/generated/leaving.cpp @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #include "leaving.h" #include "converters.h" diff --git a/jobs/generated/leaving.h b/jobs/generated/leaving.h index 28ba3d92..f006ce19 100644 --- a/jobs/generated/leaving.h +++ b/jobs/generated/leaving.h @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #pragma once #include "../basejob.h" diff --git a/jobs/generated/list_public_rooms.cpp b/jobs/generated/list_public_rooms.cpp index 8a96966f..d15e9de3 100644 --- a/jobs/generated/list_public_rooms.cpp +++ b/jobs/generated/list_public_rooms.cpp @@ -2,11 +2,8 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #include "list_public_rooms.h" -#include "converters.h" - #include using namespace QMatrixClient; @@ -80,10 +77,7 @@ GetPublicRoomsJob::GetPublicRoomsJob(double limit, const QString& since, const Q ), d(new Private) { } -GetPublicRoomsJob::~GetPublicRoomsJob() -{ - delete d; -} +GetPublicRoomsJob::~GetPublicRoomsJob() = default; const QVector& GetPublicRoomsJob::chunk() const { @@ -213,10 +207,7 @@ QueryPublicRoomsJob::QueryPublicRoomsJob(const QString& server, double limit, co setRequestData(_data); } -QueryPublicRoomsJob::~QueryPublicRoomsJob() -{ - delete d; -} +QueryPublicRoomsJob::~QueryPublicRoomsJob() = default; const QVector& QueryPublicRoomsJob::chunk() const { diff --git a/jobs/generated/list_public_rooms.h b/jobs/generated/list_public_rooms.h index 74dd8626..f6467a21 100644 --- a/jobs/generated/list_public_rooms.h +++ b/jobs/generated/list_public_rooms.h @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #pragma once #include "../basejob.h" @@ -45,13 +44,13 @@ namespace QMatrixClient const QString& nextBatch() const; const QString& prevBatch() const; double totalRoomCountEstimate() const; - + protected: Status parseJson(const QJsonDocument& data) override; - + private: class Private; - Private* d; + QScopedPointer d; }; class QueryPublicRoomsJob : public BaseJob @@ -90,12 +89,12 @@ namespace QMatrixClient const QString& nextBatch() const; const QString& prevBatch() const; double totalRoomCountEstimate() const; - + protected: Status parseJson(const QJsonDocument& data) override; - + private: class Private; - Private* d; + QScopedPointer d; }; } // namespace QMatrixClient diff --git a/jobs/generated/login.cpp b/jobs/generated/login.cpp index 4c159517..bbfff0da 100644 --- a/jobs/generated/login.cpp +++ b/jobs/generated/login.cpp @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #include "login.h" #include "converters.h" @@ -47,10 +46,7 @@ LoginJob::LoginJob(const QString& type, const QString& user, const QString& medi setRequestData(_data); } -LoginJob::~LoginJob() -{ - delete d; -} +LoginJob::~LoginJob() = default; const QString& LoginJob::userId() const { diff --git a/jobs/generated/login.h b/jobs/generated/login.h index 1c017877..0f68a13b 100644 --- a/jobs/generated/login.h +++ b/jobs/generated/login.h @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #pragma once #include "../basejob.h" @@ -24,12 +23,12 @@ namespace QMatrixClient const QString& accessToken() const; const QString& homeServer() const; const QString& deviceId() const; - + protected: Status parseJson(const QJsonDocument& data) override; - + private: class Private; - Private* d; + QScopedPointer d; }; } // namespace QMatrixClient diff --git a/jobs/generated/logout.cpp b/jobs/generated/logout.cpp index c250bddf..b6904070 100644 --- a/jobs/generated/logout.cpp +++ b/jobs/generated/logout.cpp @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #include "logout.h" #include "converters.h" diff --git a/jobs/generated/logout.h b/jobs/generated/logout.h index ae9e54b8..d2b85db5 100644 --- a/jobs/generated/logout.h +++ b/jobs/generated/logout.h @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #pragma once #include "../basejob.h" diff --git a/jobs/generated/profile.cpp b/jobs/generated/profile.cpp index 6ec566f7..27bab0b8 100644 --- a/jobs/generated/profile.cpp +++ b/jobs/generated/profile.cpp @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #include "profile.h" #include "converters.h" @@ -38,10 +37,7 @@ GetDisplayNameJob::GetDisplayNameJob(const QString& userId) ), d(new Private) { } -GetDisplayNameJob::~GetDisplayNameJob() -{ - delete d; -} +GetDisplayNameJob::~GetDisplayNameJob() = default; const QString& GetDisplayNameJob::displayname() const { @@ -80,10 +76,7 @@ GetAvatarUrlJob::GetAvatarUrlJob(const QString& userId) ), d(new Private) { } -GetAvatarUrlJob::~GetAvatarUrlJob() -{ - delete d; -} +GetAvatarUrlJob::~GetAvatarUrlJob() = default; const QString& GetAvatarUrlJob::avatarUrl() const { @@ -111,10 +104,7 @@ GetUserProfileJob::GetUserProfileJob(const QString& userId) ), d(new Private) { } -GetUserProfileJob::~GetUserProfileJob() -{ - delete d; -} +GetUserProfileJob::~GetUserProfileJob() = default; const QString& GetUserProfileJob::avatarUrl() const { diff --git a/jobs/generated/profile.h b/jobs/generated/profile.h index 30e858de..9cbf3865 100644 --- a/jobs/generated/profile.h +++ b/jobs/generated/profile.h @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #pragma once #include "../basejob.h" @@ -27,13 +26,13 @@ namespace QMatrixClient ~GetDisplayNameJob() override; const QString& displayname() const; - + protected: Status parseJson(const QJsonDocument& data) override; - + private: class Private; - Private* d; + QScopedPointer d; }; class SetAvatarUrlJob : public BaseJob @@ -49,13 +48,13 @@ namespace QMatrixClient ~GetAvatarUrlJob() override; const QString& avatarUrl() const; - + protected: Status parseJson(const QJsonDocument& data) override; - + private: class Private; - Private* d; + QScopedPointer d; }; class GetUserProfileJob : public BaseJob @@ -66,12 +65,12 @@ namespace QMatrixClient const QString& avatarUrl() const; const QString& displayname() const; - + protected: Status parseJson(const QJsonDocument& data) override; - + private: class Private; - Private* d; + QScopedPointer d; }; } // namespace QMatrixClient diff --git a/jobs/generated/receipts.cpp b/jobs/generated/receipts.cpp index 2820b583..1c8bd80f 100644 --- a/jobs/generated/receipts.cpp +++ b/jobs/generated/receipts.cpp @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #include "receipts.h" #include "converters.h" diff --git a/jobs/generated/receipts.h b/jobs/generated/receipts.h index 6f36d7fc..e4065ddb 100644 --- a/jobs/generated/receipts.h +++ b/jobs/generated/receipts.h @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #pragma once #include "../basejob.h" diff --git a/jobs/generated/redaction.cpp b/jobs/generated/redaction.cpp index a9b8ed7e..9207f344 100644 --- a/jobs/generated/redaction.cpp +++ b/jobs/generated/redaction.cpp @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #include "redaction.h" #include "converters.h" @@ -31,10 +30,7 @@ RedactEventJob::RedactEventJob(const QString& roomId, const QString& eventId, co setRequestData(_data); } -RedactEventJob::~RedactEventJob() -{ - delete d; -} +RedactEventJob::~RedactEventJob() = default; const QString& RedactEventJob::eventId() const { diff --git a/jobs/generated/redaction.h b/jobs/generated/redaction.h index 600e0daa..0a68f8cd 100644 --- a/jobs/generated/redaction.h +++ b/jobs/generated/redaction.h @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #pragma once #include "../basejob.h" @@ -21,12 +20,12 @@ namespace QMatrixClient ~RedactEventJob() override; const QString& eventId() const; - + protected: Status parseJson(const QJsonDocument& data) override; - + private: class Private; - Private* d; + QScopedPointer d; }; } // namespace QMatrixClient diff --git a/jobs/generated/third_party_membership.cpp b/jobs/generated/third_party_membership.cpp index 7a2aa4f4..dbb3ebee 100644 --- a/jobs/generated/third_party_membership.cpp +++ b/jobs/generated/third_party_membership.cpp @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #include "third_party_membership.h" #include "converters.h" diff --git a/jobs/generated/third_party_membership.h b/jobs/generated/third_party_membership.h index 6c1193ed..b1669795 100644 --- a/jobs/generated/third_party_membership.h +++ b/jobs/generated/third_party_membership.h @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #pragma once #include "../basejob.h" diff --git a/jobs/generated/typing.cpp b/jobs/generated/typing.cpp index 44bbb131..a6817cb9 100644 --- a/jobs/generated/typing.cpp +++ b/jobs/generated/typing.cpp @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #include "typing.h" #include "converters.h" diff --git a/jobs/generated/typing.h b/jobs/generated/typing.h index e20bca1a..6eb3ddf4 100644 --- a/jobs/generated/typing.h +++ b/jobs/generated/typing.h @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #pragma once #include "../basejob.h" diff --git a/jobs/generated/versions.cpp b/jobs/generated/versions.cpp index 66b31290..8ff58365 100644 --- a/jobs/generated/versions.cpp +++ b/jobs/generated/versions.cpp @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #include "versions.h" #include "converters.h" @@ -26,10 +25,7 @@ GetVersionsJob::GetVersionsJob() ), d(new Private) { } -GetVersionsJob::~GetVersionsJob() -{ - delete d; -} +GetVersionsJob::~GetVersionsJob() = default; const QVector& GetVersionsJob::versions() const { diff --git a/jobs/generated/versions.h b/jobs/generated/versions.h index eab8cf9e..a7add8ba 100644 --- a/jobs/generated/versions.h +++ b/jobs/generated/versions.h @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #pragma once #include "../basejob.h" @@ -22,12 +21,12 @@ namespace QMatrixClient ~GetVersionsJob() override; const QVector& versions() const; - + protected: Status parseJson(const QJsonDocument& data) override; - + private: class Private; - Private* d; + QScopedPointer d; }; } // namespace QMatrixClient diff --git a/jobs/generated/whoami.cpp b/jobs/generated/whoami.cpp index dce091ec..d4da99d4 100644 --- a/jobs/generated/whoami.cpp +++ b/jobs/generated/whoami.cpp @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #include "whoami.h" #include "converters.h" @@ -26,10 +25,7 @@ GetTokenOwnerJob::GetTokenOwnerJob() ), d(new Private) { } -GetTokenOwnerJob::~GetTokenOwnerJob() -{ - delete d; -} +GetTokenOwnerJob::~GetTokenOwnerJob() = default; const QString& GetTokenOwnerJob::userId() const { diff --git a/jobs/generated/whoami.h b/jobs/generated/whoami.h index 1b04f337..21cb1a17 100644 --- a/jobs/generated/whoami.h +++ b/jobs/generated/whoami.h @@ -2,7 +2,6 @@ * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN */ - #pragma once #include "../basejob.h" @@ -21,12 +20,12 @@ namespace QMatrixClient ~GetTokenOwnerJob() override; const QString& userId() const; - + protected: Status parseJson(const QJsonDocument& data) override; - + private: class Private; - Private* d; + QScopedPointer d; }; } // namespace QMatrixClient -- cgit v1.2.3 From 986fc27e451b21cdd118e74da9e9ff22e275ef75 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Wed, 3 Jan 2018 12:19:25 +0900 Subject: BaseJob, MediaThumbnailJob: Support request and response headers Enable specifying headers in the request and checking/using headers in the response. --- jobs/basejob.cpp | 110 ++++++++++++++++++++++++++++++++++++--------- jobs/basejob.h | 18 ++++++-- jobs/mediathumbnailjob.cpp | 6 +-- jobs/mediathumbnailjob.h | 2 +- 4 files changed, 107 insertions(+), 29 deletions(-) (limited to 'jobs') diff --git a/jobs/basejob.cpp b/jobs/basejob.cpp index 9df3e430..980814c4 100644 --- a/jobs/basejob.cpp +++ b/jobs/basejob.cpp @@ -60,10 +60,16 @@ class BaseJob::Private // Contents for the network request HttpVerb verb; QString apiEndpoint; + QHash requestHeaders; QUrlQuery requestQuery; Data requestData; bool needsToken; + // There's no use of QMimeType here because we don't want to match + // content types against the known MIME type hierarchy; and at the same + // type QMimeType is of little help with MIME type globs (`text/*` etc.) + QByteArrayList expectedContentTypes; + QScopedPointer reply; Status status = Pending; @@ -117,6 +123,22 @@ void BaseJob::setApiEndpoint(const QString& apiEndpoint) d->apiEndpoint = apiEndpoint; } +const BaseJob::headers_t&BaseJob::requestHeaders() const +{ + return d->requestHeaders; +} + +void BaseJob::setRequestHeader(const headers_t::key_type& headerName, + const headers_t::mapped_type& headerValue) +{ + d->requestHeaders[headerName] = headerValue; +} + +void BaseJob::setRequestHeaders(const BaseJob::headers_t& headers) +{ + d->requestHeaders = headers; +} + const QUrlQuery& BaseJob::query() const { return d->requestQuery; @@ -137,6 +159,21 @@ void BaseJob::setRequestData(const BaseJob::Data& data) d->requestData = data; } +const QByteArrayList& BaseJob::expectedContentTypes() const +{ + return d->expectedContentTypes; +} + +void BaseJob::addExpectedContentType(const QByteArray& contentType) +{ + d->expectedContentTypes << contentType; +} + +void BaseJob::setExpectedContentTypes(const QByteArrayList& contentTypes) +{ + d->expectedContentTypes = contentTypes; +} + void BaseJob::Private::sendRequest() { QUrl url = connection->baseUrl(); @@ -148,13 +185,16 @@ void BaseJob::Private::sendRequest() url.setQuery(requestQuery); QNetworkRequest req {url}; - req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); + if (!requestHeaders.contains("Content-Type")) + req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); req.setRawHeader(QByteArray("Authorization"), QByteArray("Bearer ") + connection->accessToken()); #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true); req.setMaximumRedirectsAllowed(10); #endif + for (auto it = requestHeaders.cbegin(); it != requestHeaders.cend(); ++it) + req.setRawHeader(it.key(), it.value()); switch( verb ) { case HttpVerb::Get: @@ -206,11 +246,34 @@ void BaseJob::gotReply() { setStatus(checkReply(d->reply.data())); if (status().good()) - setStatus(parseReply(d->reply->readAll())); + setStatus(parseReply(d->reply.data())); finishJob(); } +bool checkContentType(const QByteArray& type, const QByteArrayList& patterns) +{ + if (patterns.isEmpty()) + return true; + + for (const auto& pattern: patterns) + { + if (pattern.startsWith('*') || type == pattern) // Fast lane + return true; + + auto patternParts = pattern.split('/'); + Q_ASSERT_X(patternParts.size() <= 2, __FUNCTION__, + "BaseJob: Expected content type should have up to two" + " /-separated parts; violating pattern: " + pattern); + + if (type.split('/').front() == patternParts.front() && + patternParts.back() == "*") + return true; // Exact match already went on fast lane + } + + return false; +} + BaseJob::Status BaseJob::checkReply(QNetworkReply* reply) const { qCDebug(d->logCat) << this << "returned from" << reply->url().toDisplayString(); @@ -218,30 +281,35 @@ BaseJob::Status BaseJob::checkReply(QNetworkReply* reply) const qCDebug(d->logCat) << this << "returned" << reply->error(); switch( reply->error() ) { - case QNetworkReply::NoError: - return NoError; - - case QNetworkReply::AuthenticationRequiredError: - case QNetworkReply::ContentAccessDenied: - case QNetworkReply::ContentOperationNotPermittedError: - return { ContentAccessError, reply->errorString() }; - - case QNetworkReply::ProtocolInvalidOperationError: - case QNetworkReply::UnknownContentError: - return { IncorrectRequestError, reply->errorString() }; - - case QNetworkReply::ContentNotFoundError: - return { NotFoundError, reply->errorString() }; - - default: - return { NetworkError, reply->errorString() }; + case QNetworkReply::NoError: + if (checkContentType(reply->rawHeader("Content-Type"), + d->expectedContentTypes)) + return NoError; + else + return { IncorrectResponseError, + "Incorrect content type of the response" }; + + case QNetworkReply::AuthenticationRequiredError: + case QNetworkReply::ContentAccessDenied: + case QNetworkReply::ContentOperationNotPermittedError: + return { ContentAccessError, reply->errorString() }; + + case QNetworkReply::ProtocolInvalidOperationError: + case QNetworkReply::UnknownContentError: + return { IncorrectRequestError, reply->errorString() }; + + case QNetworkReply::ContentNotFoundError: + return { NotFoundError, reply->errorString() }; + + default: + return { NetworkError, reply->errorString() }; } } -BaseJob::Status BaseJob::parseReply(QByteArray data) +BaseJob::Status BaseJob::parseReply(QNetworkReply* reply) { QJsonParseError error; - QJsonDocument json = QJsonDocument::fromJson(data, &error); + QJsonDocument json = QJsonDocument::fromJson(reply->readAll(), &error); if( error.error == QJsonParseError::NoError ) return parseJson(json); else diff --git a/jobs/basejob.h b/jobs/basejob.h index 2f75c095..1d7f2388 100644 --- a/jobs/basejob.h +++ b/jobs/basejob.h @@ -25,7 +25,6 @@ #include #include #include -#include class QNetworkReply; class QSslError; @@ -59,6 +58,7 @@ namespace QMatrixClient , ContentAccessError , NotFoundError , IncorrectRequestError + , IncorrectResponseError , UserDefinedError = 200 }; @@ -214,12 +214,21 @@ namespace QMatrixClient void failure(BaseJob*); protected: + using headers_t = QHash; + const QString& apiEndpoint() const; void setApiEndpoint(const QString& apiEndpoint); + const headers_t& requestHeaders() const; + void setRequestHeader(const headers_t::key_type& headerName, + const headers_t::mapped_type& headerValue); + void setRequestHeaders(const headers_t& headers); const QUrlQuery& query() const; void setRequestQuery(const QUrlQuery& query); const Data& requestData() const; void setRequestData(const Data& data); + const QByteArrayList& expectedContentTypes() const; + void addExpectedContentType(const QByteArray& contentType); + void setExpectedContentTypes(const QByteArrayList& contentTypes); virtual void beforeStart(const ConnectionData* connData); @@ -240,11 +249,11 @@ namespace QMatrixClient * 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) + * @param reply raw contents of a HTTP reply from the server (without headers) * * @see gotReply, parseJson */ - virtual Status parseReply(QByteArray data); + virtual Status parseReply(QNetworkReply* reply); /** * Processes the JSON document received from the Matrix server. @@ -265,7 +274,8 @@ namespace QMatrixClient void setLoggingCategory(LoggingCategory lcf); // Job objects should only be deleted via QObject::deleteLater - virtual ~BaseJob(); + ~BaseJob() override; + protected slots: void timeout(); diff --git a/jobs/mediathumbnailjob.cpp b/jobs/mediathumbnailjob.cpp index c0d67a63..9337549e 100644 --- a/jobs/mediathumbnailjob.cpp +++ b/jobs/mediathumbnailjob.cpp @@ -17,8 +17,8 @@ */ #include "mediathumbnailjob.h" -#include "util.h" +#include #include using namespace QMatrixClient; @@ -47,9 +47,9 @@ QImage MediaThumbnailJob::scaledThumbnail(QSize toSize) const Qt::KeepAspectRatio, Qt::SmoothTransformation); } -BaseJob::Status MediaThumbnailJob::parseReply(QByteArray data) +BaseJob::Status MediaThumbnailJob::parseReply(QNetworkReply* reply) { - if( !_thumbnail.loadFromData(data) ) + if( !_thumbnail.loadFromData(reply->readAll()) ) { qCDebug(JOBS) << "MediaThumbnailJob: could not read image data"; } diff --git a/jobs/mediathumbnailjob.h b/jobs/mediathumbnailjob.h index f8f36fe9..2d6853c7 100644 --- a/jobs/mediathumbnailjob.h +++ b/jobs/mediathumbnailjob.h @@ -36,7 +36,7 @@ namespace QMatrixClient QImage scaledThumbnail(QSize toSize) const; protected: - Status parseReply(QByteArray data) override; + Status parseReply(QNetworkReply* reply) override; private: QImage _thumbnail; -- cgit v1.2.3 From b142117d78a2a4ce21e818c62cb7a10cff80af0d Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 5 Jan 2018 19:21:54 +0900 Subject: BaseJob: Provide a simplified constructor --- jobs/basejob.cpp | 4 ++++ jobs/basejob.h | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'jobs') diff --git a/jobs/basejob.cpp b/jobs/basejob.cpp index 980814c4..74031909 100644 --- a/jobs/basejob.cpp +++ b/jobs/basejob.cpp @@ -96,6 +96,10 @@ QDebug QMatrixClient::operator<<(QDebug dbg, const BaseJob::Status& s) << QString(s.message).replace(filter, "\\1 HIDDEN"); } +BaseJob::BaseJob(HttpVerb verb, const QString& name, const QString& endpoint, bool needsToken) + : BaseJob(verb, name, endpoint, Query { }, Data { }, needsToken) +{ } + 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)) diff --git a/jobs/basejob.h b/jobs/basejob.h index 1d7f2388..1fe3a24d 100644 --- a/jobs/basejob.h +++ b/jobs/basejob.h @@ -130,7 +130,9 @@ namespace QMatrixClient public: BaseJob(HttpVerb verb, const QString& name, const QString& endpoint, - const Query& query = {}, const Data& data = {}, + bool needsToken = true); + BaseJob(HttpVerb verb, const QString& name, const QString& endpoint, + const Query& query, const Data& data = {}, bool needsToken = true); Status status() const; -- cgit v1.2.3 From 7169338dedbf0184da4c971e7cecb6be13b1d129 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 5 Jan 2018 11:01:18 +0900 Subject: jobs/generated: Use BaseJob::addExpectedContentType() --- jobs/generated/administrative_contact.cpp | 2 ++ jobs/generated/banning.cpp | 4 ++++ jobs/generated/directory.cpp | 2 ++ jobs/generated/inviting.cpp | 2 ++ jobs/generated/kicking.cpp | 2 ++ jobs/generated/list_public_rooms.cpp | 2 ++ jobs/generated/login.cpp | 2 ++ jobs/generated/profile.cpp | 4 ++++ jobs/generated/receipts.cpp | 2 ++ jobs/generated/redaction.cpp | 2 ++ jobs/generated/third_party_membership.cpp | 2 ++ jobs/generated/typing.cpp | 2 ++ 12 files changed, 28 insertions(+) (limited to 'jobs') diff --git a/jobs/generated/administrative_contact.cpp b/jobs/generated/administrative_contact.cpp index ca029f58..584c447f 100644 --- a/jobs/generated/administrative_contact.cpp +++ b/jobs/generated/administrative_contact.cpp @@ -102,6 +102,8 @@ Post3PIDsJob::Post3PIDsJob(const ThreePidCredentials& threePidCreds, bool bind) _data.insert("three_pid_creds", toJson(threePidCreds)); _data.insert("bind", toJson(bind)); setRequestData(_data); + + addExpectedContentType("application/json"); } RequestTokenTo3PIDJob::RequestTokenTo3PIDJob() diff --git a/jobs/generated/banning.cpp b/jobs/generated/banning.cpp index d7708cc6..b7888062 100644 --- a/jobs/generated/banning.cpp +++ b/jobs/generated/banning.cpp @@ -23,6 +23,8 @@ BanJob::BanJob(const QString& roomId, const QString& userId, const QString& reas if (!reason.isEmpty()) _data.insert("reason", toJson(reason)); setRequestData(_data); + + addExpectedContentType("application/json"); } UnbanJob::UnbanJob(const QString& roomId, const QString& userId) @@ -34,5 +36,7 @@ UnbanJob::UnbanJob(const QString& roomId, const QString& userId) QJsonObject _data; _data.insert("user_id", toJson(userId)); setRequestData(_data); + + addExpectedContentType("application/json"); } diff --git a/jobs/generated/directory.cpp b/jobs/generated/directory.cpp index 1fd1e443..ea6d493f 100644 --- a/jobs/generated/directory.cpp +++ b/jobs/generated/directory.cpp @@ -22,6 +22,8 @@ SetRoomAliasJob::SetRoomAliasJob(const QString& roomAlias, const QString& roomId if (!roomId.isEmpty()) _data.insert("room_id", toJson(roomId)); setRequestData(_data); + + addExpectedContentType("application/json"); } class GetRoomIdByAliasJob::Private diff --git a/jobs/generated/inviting.cpp b/jobs/generated/inviting.cpp index bdf257dd..6cd67ad7 100644 --- a/jobs/generated/inviting.cpp +++ b/jobs/generated/inviting.cpp @@ -21,5 +21,7 @@ InviteUserJob::InviteUserJob(const QString& roomId, const QString& userId) QJsonObject _data; _data.insert("user_id", toJson(userId)); setRequestData(_data); + + addExpectedContentType("application/json"); } diff --git a/jobs/generated/kicking.cpp b/jobs/generated/kicking.cpp index 3488b387..bf94fe59 100644 --- a/jobs/generated/kicking.cpp +++ b/jobs/generated/kicking.cpp @@ -23,5 +23,7 @@ KickJob::KickJob(const QString& roomId, const QString& userId, const QString& re if (!reason.isEmpty()) _data.insert("reason", toJson(reason)); setRequestData(_data); + + addExpectedContentType("application/json"); } diff --git a/jobs/generated/list_public_rooms.cpp b/jobs/generated/list_public_rooms.cpp index d15e9de3..1e775f1b 100644 --- a/jobs/generated/list_public_rooms.cpp +++ b/jobs/generated/list_public_rooms.cpp @@ -205,6 +205,8 @@ QueryPublicRoomsJob::QueryPublicRoomsJob(const QString& server, double limit, co _data.insert("since", toJson(since)); _data.insert("filter", toJson(filter)); setRequestData(_data); + + addExpectedContentType("application/json"); } QueryPublicRoomsJob::~QueryPublicRoomsJob() = default; diff --git a/jobs/generated/login.cpp b/jobs/generated/login.cpp index bbfff0da..67d991db 100644 --- a/jobs/generated/login.cpp +++ b/jobs/generated/login.cpp @@ -44,6 +44,8 @@ LoginJob::LoginJob(const QString& type, const QString& user, const QString& medi if (!initialDeviceDisplayName.isEmpty()) _data.insert("initial_device_display_name", toJson(initialDeviceDisplayName)); setRequestData(_data); + + addExpectedContentType("application/json"); } LoginJob::~LoginJob() = default; diff --git a/jobs/generated/profile.cpp b/jobs/generated/profile.cpp index 27bab0b8..b30b9a36 100644 --- a/jobs/generated/profile.cpp +++ b/jobs/generated/profile.cpp @@ -22,6 +22,8 @@ SetDisplayNameJob::SetDisplayNameJob(const QString& userId, const QString& displ if (!displayname.isEmpty()) _data.insert("displayname", toJson(displayname)); setRequestData(_data); + + addExpectedContentType("application/json"); } class GetDisplayNameJob::Private @@ -61,6 +63,8 @@ SetAvatarUrlJob::SetAvatarUrlJob(const QString& userId, const QString& avatarUrl if (!avatarUrl.isEmpty()) _data.insert("avatar_url", toJson(avatarUrl)); setRequestData(_data); + + addExpectedContentType("application/json"); } class GetAvatarUrlJob::Private diff --git a/jobs/generated/receipts.cpp b/jobs/generated/receipts.cpp index 1c8bd80f..20e58ffb 100644 --- a/jobs/generated/receipts.cpp +++ b/jobs/generated/receipts.cpp @@ -19,5 +19,7 @@ PostReceiptJob::PostReceiptJob(const QString& roomId, const QString& receiptType ) { setRequestData(Data(receipt)); + + addExpectedContentType("application/json"); } diff --git a/jobs/generated/redaction.cpp b/jobs/generated/redaction.cpp index 9207f344..218e1aa6 100644 --- a/jobs/generated/redaction.cpp +++ b/jobs/generated/redaction.cpp @@ -28,6 +28,8 @@ RedactEventJob::RedactEventJob(const QString& roomId, const QString& eventId, co if (!reason.isEmpty()) _data.insert("reason", toJson(reason)); setRequestData(_data); + + addExpectedContentType("application/json"); } RedactEventJob::~RedactEventJob() = default; diff --git a/jobs/generated/third_party_membership.cpp b/jobs/generated/third_party_membership.cpp index dbb3ebee..f5ccf123 100644 --- a/jobs/generated/third_party_membership.cpp +++ b/jobs/generated/third_party_membership.cpp @@ -23,5 +23,7 @@ InviteBy3PIDJob::InviteBy3PIDJob(const QString& roomId, const QString& idServer, _data.insert("medium", toJson(medium)); _data.insert("address", toJson(address)); setRequestData(_data); + + addExpectedContentType("application/json"); } diff --git a/jobs/generated/typing.cpp b/jobs/generated/typing.cpp index a6817cb9..05e5fd1e 100644 --- a/jobs/generated/typing.cpp +++ b/jobs/generated/typing.cpp @@ -22,5 +22,7 @@ SetTypingJob::SetTypingJob(const QString& userId, const QString& roomId, bool ty _data.insert("typing", toJson(typing)); _data.insert("timeout", toJson(timeout)); setRequestData(_data); + + addExpectedContentType("application/json"); } -- cgit v1.2.3 From 01806d00977578681a401ad294a957ecec0a3d53 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 5 Jan 2018 19:46:41 +0900 Subject: jobs: expect application/json by default; set query in constructor body; properly convert numbers to strings in query The query should be set in constructor body because there's no reason to pass non-required parameters into the query. As for numbers to strings conversion - there was an attempt to use QJsonValue(a).toString() for that. That doesn't work; QJsonValue does not turn numbers to strings. --- jobs/basejob.cpp | 1 + jobs/generated/administrative_contact.cpp | 21 +++++++----------- jobs/generated/banning.cpp | 12 ++--------- jobs/generated/directory.cpp | 21 +++++++----------- jobs/generated/inviting.cpp | 6 +----- jobs/generated/kicking.cpp | 6 +----- jobs/generated/leaving.cpp | 14 ++++++------ jobs/generated/list_public_rooms.cpp | 32 ++++++++++++++------------- jobs/generated/login.cpp | 7 ++---- jobs/generated/logout.cpp | 7 +++--- jobs/generated/profile.cpp | 36 ++++++++++++------------------- jobs/generated/receipts.cpp | 6 +----- jobs/generated/redaction.cpp | 7 ++---- jobs/generated/third_party_membership.cpp | 6 +----- jobs/generated/typing.cpp | 6 +----- jobs/generated/versions.cpp | 8 +++---- jobs/generated/whoami.cpp | 8 +++---- 17 files changed, 76 insertions(+), 128 deletions(-) (limited to 'jobs') diff --git a/jobs/basejob.cpp b/jobs/basejob.cpp index 74031909..dddff800 100644 --- a/jobs/basejob.cpp +++ b/jobs/basejob.cpp @@ -105,6 +105,7 @@ BaseJob::BaseJob(HttpVerb verb, const QString& name, const QString& endpoint, : d(new Private(verb, endpoint, query, data, needsToken)) { setObjectName(name); + setExpectedContentTypes({ "application/json" }); d->timer.setSingleShot(true); connect (&d->timer, &QTimer::timeout, this, &BaseJob::timeout); d->retryTimer.setSingleShot(true); diff --git a/jobs/generated/administrative_contact.cpp b/jobs/generated/administrative_contact.cpp index 584c447f..479bee52 100644 --- a/jobs/generated/administrative_contact.cpp +++ b/jobs/generated/administrative_contact.cpp @@ -44,10 +44,10 @@ class GetAccount3PIDsJob::Private GetAccount3PIDsJob::GetAccount3PIDsJob() : BaseJob(HttpVerb::Get, "GetAccount3PIDsJob", - basePath % "/account/3pid", - Query { } - ), d(new Private) -{ } + basePath % "/account/3pid") + , d(new Private) +{ +} GetAccount3PIDsJob::~GetAccount3PIDsJob() = default; @@ -94,22 +94,17 @@ namespace QMatrixClient Post3PIDsJob::Post3PIDsJob(const ThreePidCredentials& threePidCreds, bool bind) : BaseJob(HttpVerb::Post, "Post3PIDsJob", - basePath % "/account/3pid", - Query { } - ) + basePath % "/account/3pid") { QJsonObject _data; _data.insert("three_pid_creds", toJson(threePidCreds)); _data.insert("bind", toJson(bind)); setRequestData(_data); - - addExpectedContentType("application/json"); } RequestTokenTo3PIDJob::RequestTokenTo3PIDJob() : BaseJob(HttpVerb::Post, "RequestTokenTo3PIDJob", - basePath % "/account/3pid/email/requestToken", - Query { }, Data { }, false - ) -{ } + basePath % "/account/3pid/email/requestToken", false) +{ +} diff --git a/jobs/generated/banning.cpp b/jobs/generated/banning.cpp index b7888062..f66b27b6 100644 --- a/jobs/generated/banning.cpp +++ b/jobs/generated/banning.cpp @@ -14,29 +14,21 @@ static const auto basePath = QStringLiteral("/_matrix/client/r0"); BanJob::BanJob(const QString& roomId, const QString& userId, const QString& reason) : BaseJob(HttpVerb::Post, "BanJob", - basePath % "/rooms/" % roomId % "/ban", - Query { } - ) + basePath % "/rooms/" % roomId % "/ban") { QJsonObject _data; _data.insert("user_id", toJson(userId)); if (!reason.isEmpty()) _data.insert("reason", toJson(reason)); setRequestData(_data); - - addExpectedContentType("application/json"); } UnbanJob::UnbanJob(const QString& roomId, const QString& userId) : BaseJob(HttpVerb::Post, "UnbanJob", - basePath % "/rooms/" % roomId % "/unban", - Query { } - ) + basePath % "/rooms/" % roomId % "/unban") { QJsonObject _data; _data.insert("user_id", toJson(userId)); setRequestData(_data); - - addExpectedContentType("application/json"); } diff --git a/jobs/generated/directory.cpp b/jobs/generated/directory.cpp index ea6d493f..4e61ed74 100644 --- a/jobs/generated/directory.cpp +++ b/jobs/generated/directory.cpp @@ -14,16 +14,12 @@ static const auto basePath = QStringLiteral("/_matrix/client/r0/directory"); SetRoomAliasJob::SetRoomAliasJob(const QString& roomAlias, const QString& roomId) : BaseJob(HttpVerb::Put, "SetRoomAliasJob", - basePath % "/room/" % roomAlias, - Query { } - ) + basePath % "/room/" % roomAlias) { QJsonObject _data; if (!roomId.isEmpty()) _data.insert("room_id", toJson(roomId)); setRequestData(_data); - - addExpectedContentType("application/json"); } class GetRoomIdByAliasJob::Private @@ -35,10 +31,10 @@ class GetRoomIdByAliasJob::Private GetRoomIdByAliasJob::GetRoomIdByAliasJob(const QString& roomAlias) : BaseJob(HttpVerb::Get, "GetRoomIdByAliasJob", - basePath % "/room/" % roomAlias, - Query { }, Data { }, false - ), d(new Private) -{ } + basePath % "/room/" % roomAlias, false) + , d(new Private) +{ +} GetRoomIdByAliasJob::~GetRoomIdByAliasJob() = default; @@ -62,8 +58,7 @@ BaseJob::Status GetRoomIdByAliasJob::parseJson(const QJsonDocument& data) DeleteRoomAliasJob::DeleteRoomAliasJob(const QString& roomAlias) : BaseJob(HttpVerb::Delete, "DeleteRoomAliasJob", - basePath % "/room/" % roomAlias, - Query { } - ) -{ } + basePath % "/room/" % roomAlias) +{ +} diff --git a/jobs/generated/inviting.cpp b/jobs/generated/inviting.cpp index 6cd67ad7..d2ee2107 100644 --- a/jobs/generated/inviting.cpp +++ b/jobs/generated/inviting.cpp @@ -14,14 +14,10 @@ static const auto basePath = QStringLiteral("/_matrix/client/r0"); InviteUserJob::InviteUserJob(const QString& roomId, const QString& userId) : BaseJob(HttpVerb::Post, "InviteUserJob", - basePath % "/rooms/" % roomId % "/invite", - Query { } - ) + basePath % "/rooms/" % roomId % "/invite") { QJsonObject _data; _data.insert("user_id", toJson(userId)); setRequestData(_data); - - addExpectedContentType("application/json"); } diff --git a/jobs/generated/kicking.cpp b/jobs/generated/kicking.cpp index bf94fe59..bf2490b7 100644 --- a/jobs/generated/kicking.cpp +++ b/jobs/generated/kicking.cpp @@ -14,16 +14,12 @@ static const auto basePath = QStringLiteral("/_matrix/client/r0"); KickJob::KickJob(const QString& roomId, const QString& userId, const QString& reason) : BaseJob(HttpVerb::Post, "KickJob", - basePath % "/rooms/" % roomId % "/kick", - Query { } - ) + basePath % "/rooms/" % roomId % "/kick") { QJsonObject _data; _data.insert("user_id", toJson(userId)); if (!reason.isEmpty()) _data.insert("reason", toJson(reason)); setRequestData(_data); - - addExpectedContentType("application/json"); } diff --git a/jobs/generated/leaving.cpp b/jobs/generated/leaving.cpp index 604fcc73..89c110dd 100644 --- a/jobs/generated/leaving.cpp +++ b/jobs/generated/leaving.cpp @@ -14,15 +14,13 @@ static const auto basePath = QStringLiteral("/_matrix/client/r0"); LeaveRoomJob::LeaveRoomJob(const QString& roomId) : BaseJob(HttpVerb::Post, "LeaveRoomJob", - basePath % "/rooms/" % roomId % "/leave", - Query { } - ) -{ } + basePath % "/rooms/" % roomId % "/leave") +{ +} ForgetRoomJob::ForgetRoomJob(const QString& roomId) : BaseJob(HttpVerb::Post, "ForgetRoomJob", - basePath % "/rooms/" % roomId % "/forget", - Query { } - ) -{ } + basePath % "/rooms/" % roomId % "/forget") +{ +} diff --git a/jobs/generated/list_public_rooms.cpp b/jobs/generated/list_public_rooms.cpp index 1e775f1b..a2c0e406 100644 --- a/jobs/generated/list_public_rooms.cpp +++ b/jobs/generated/list_public_rooms.cpp @@ -68,14 +68,17 @@ class GetPublicRoomsJob::Private GetPublicRoomsJob::GetPublicRoomsJob(double limit, const QString& since, const QString& server) : BaseJob(HttpVerb::Get, "GetPublicRoomsJob", - basePath % "/publicRooms", - Query { - { "limit", toJson(limit).toString() }, - { "since", toJson(since).toString() }, - { "server", toJson(server).toString() } - }, Data { }, false - ), d(new Private) -{ } + basePath % "/publicRooms", false) + , d(new Private) +{ + QUrlQuery _q; + _q.addQueryItem("limit", QString("%1").arg(limit)); + if (!since.isEmpty()) + _q.addQueryItem("since", since); + if (!server.isEmpty()) + _q.addQueryItem("server", server); + setRequestQuery(_q); +} GetPublicRoomsJob::~GetPublicRoomsJob() = default; @@ -193,20 +196,19 @@ class QueryPublicRoomsJob::Private QueryPublicRoomsJob::QueryPublicRoomsJob(const QString& server, double limit, const QString& since, const Filter& filter) : BaseJob(HttpVerb::Post, "QueryPublicRoomsJob", - basePath % "/publicRooms", - Query { - { "server", toJson(server).toString() } - } - ), d(new Private) + basePath % "/publicRooms") + , d(new Private) { + QUrlQuery _q; + if (!server.isEmpty()) + _q.addQueryItem("server", server); + setRequestQuery(_q); QJsonObject _data; _data.insert("limit", toJson(limit)); if (!since.isEmpty()) _data.insert("since", toJson(since)); _data.insert("filter", toJson(filter)); setRequestData(_data); - - addExpectedContentType("application/json"); } QueryPublicRoomsJob::~QueryPublicRoomsJob() = default; diff --git a/jobs/generated/login.cpp b/jobs/generated/login.cpp index 67d991db..a4dab428 100644 --- a/jobs/generated/login.cpp +++ b/jobs/generated/login.cpp @@ -23,9 +23,8 @@ class LoginJob::Private LoginJob::LoginJob(const QString& type, const QString& user, const QString& medium, const QString& address, const QString& password, const QString& token, const QString& deviceId, const QString& initialDeviceDisplayName) : BaseJob(HttpVerb::Post, "LoginJob", - basePath % "/login", - Query { }, Data { }, false - ), d(new Private) + basePath % "/login", false) + , d(new Private) { QJsonObject _data; _data.insert("type", toJson(type)); @@ -44,8 +43,6 @@ LoginJob::LoginJob(const QString& type, const QString& user, const QString& medi if (!initialDeviceDisplayName.isEmpty()) _data.insert("initial_device_display_name", toJson(initialDeviceDisplayName)); setRequestData(_data); - - addExpectedContentType("application/json"); } LoginJob::~LoginJob() = default; diff --git a/jobs/generated/logout.cpp b/jobs/generated/logout.cpp index b6904070..f7f8eff9 100644 --- a/jobs/generated/logout.cpp +++ b/jobs/generated/logout.cpp @@ -14,8 +14,7 @@ static const auto basePath = QStringLiteral("/_matrix/client/r0"); LogoutJob::LogoutJob() : BaseJob(HttpVerb::Post, "LogoutJob", - basePath % "/logout", - Query { } - ) -{ } + basePath % "/logout") +{ +} diff --git a/jobs/generated/profile.cpp b/jobs/generated/profile.cpp index b30b9a36..9523ca96 100644 --- a/jobs/generated/profile.cpp +++ b/jobs/generated/profile.cpp @@ -14,16 +14,12 @@ static const auto basePath = QStringLiteral("/_matrix/client/r0"); SetDisplayNameJob::SetDisplayNameJob(const QString& userId, const QString& displayname) : BaseJob(HttpVerb::Put, "SetDisplayNameJob", - basePath % "/profile/" % userId % "/displayname", - Query { } - ) + basePath % "/profile/" % userId % "/displayname") { QJsonObject _data; if (!displayname.isEmpty()) _data.insert("displayname", toJson(displayname)); setRequestData(_data); - - addExpectedContentType("application/json"); } class GetDisplayNameJob::Private @@ -34,10 +30,10 @@ class GetDisplayNameJob::Private GetDisplayNameJob::GetDisplayNameJob(const QString& userId) : BaseJob(HttpVerb::Get, "GetDisplayNameJob", - basePath % "/profile/" % userId % "/displayname", - Query { }, Data { }, false - ), d(new Private) -{ } + basePath % "/profile/" % userId % "/displayname", false) + , d(new Private) +{ +} GetDisplayNameJob::~GetDisplayNameJob() = default; @@ -55,16 +51,12 @@ BaseJob::Status GetDisplayNameJob::parseJson(const QJsonDocument& data) SetAvatarUrlJob::SetAvatarUrlJob(const QString& userId, const QString& avatarUrl) : BaseJob(HttpVerb::Put, "SetAvatarUrlJob", - basePath % "/profile/" % userId % "/avatar_url", - Query { } - ) + basePath % "/profile/" % userId % "/avatar_url") { QJsonObject _data; if (!avatarUrl.isEmpty()) _data.insert("avatar_url", toJson(avatarUrl)); setRequestData(_data); - - addExpectedContentType("application/json"); } class GetAvatarUrlJob::Private @@ -75,10 +67,10 @@ class GetAvatarUrlJob::Private GetAvatarUrlJob::GetAvatarUrlJob(const QString& userId) : BaseJob(HttpVerb::Get, "GetAvatarUrlJob", - basePath % "/profile/" % userId % "/avatar_url", - Query { }, Data { }, false - ), d(new Private) -{ } + basePath % "/profile/" % userId % "/avatar_url", false) + , d(new Private) +{ +} GetAvatarUrlJob::~GetAvatarUrlJob() = default; @@ -103,10 +95,10 @@ class GetUserProfileJob::Private GetUserProfileJob::GetUserProfileJob(const QString& userId) : BaseJob(HttpVerb::Get, "GetUserProfileJob", - basePath % "/profile/" % userId, - Query { }, Data { }, false - ), d(new Private) -{ } + basePath % "/profile/" % userId, false) + , d(new Private) +{ +} GetUserProfileJob::~GetUserProfileJob() = default; diff --git a/jobs/generated/receipts.cpp b/jobs/generated/receipts.cpp index 20e58ffb..83c38b6f 100644 --- a/jobs/generated/receipts.cpp +++ b/jobs/generated/receipts.cpp @@ -14,12 +14,8 @@ static const auto basePath = QStringLiteral("/_matrix/client/r0"); PostReceiptJob::PostReceiptJob(const QString& roomId, const QString& receiptType, const QString& eventId, const QJsonObject& receipt) : BaseJob(HttpVerb::Post, "PostReceiptJob", - basePath % "/rooms/" % roomId % "/receipt/" % receiptType % "/" % eventId, - Query { } - ) + basePath % "/rooms/" % roomId % "/receipt/" % receiptType % "/" % eventId) { setRequestData(Data(receipt)); - - addExpectedContentType("application/json"); } diff --git a/jobs/generated/redaction.cpp b/jobs/generated/redaction.cpp index 218e1aa6..0da35dfc 100644 --- a/jobs/generated/redaction.cpp +++ b/jobs/generated/redaction.cpp @@ -20,16 +20,13 @@ class RedactEventJob::Private RedactEventJob::RedactEventJob(const QString& roomId, const QString& eventId, const QString& txnId, const QString& reason) : BaseJob(HttpVerb::Put, "RedactEventJob", - basePath % "/rooms/" % roomId % "/redact/" % eventId % "/" % txnId, - Query { } - ), d(new Private) + basePath % "/rooms/" % roomId % "/redact/" % eventId % "/" % txnId) + , d(new Private) { QJsonObject _data; if (!reason.isEmpty()) _data.insert("reason", toJson(reason)); setRequestData(_data); - - addExpectedContentType("application/json"); } RedactEventJob::~RedactEventJob() = default; diff --git a/jobs/generated/third_party_membership.cpp b/jobs/generated/third_party_membership.cpp index f5ccf123..b637d481 100644 --- a/jobs/generated/third_party_membership.cpp +++ b/jobs/generated/third_party_membership.cpp @@ -14,16 +14,12 @@ static const auto basePath = QStringLiteral("/_matrix/client/r0"); InviteBy3PIDJob::InviteBy3PIDJob(const QString& roomId, const QString& idServer, const QString& medium, const QString& address) : BaseJob(HttpVerb::Post, "InviteBy3PIDJob", - basePath % "/rooms/" % roomId % "/invite", - Query { } - ) + basePath % "/rooms/" % roomId % "/invite") { QJsonObject _data; _data.insert("id_server", toJson(idServer)); _data.insert("medium", toJson(medium)); _data.insert("address", toJson(address)); setRequestData(_data); - - addExpectedContentType("application/json"); } diff --git a/jobs/generated/typing.cpp b/jobs/generated/typing.cpp index 05e5fd1e..fa700290 100644 --- a/jobs/generated/typing.cpp +++ b/jobs/generated/typing.cpp @@ -14,15 +14,11 @@ static const auto basePath = QStringLiteral("/_matrix/client/r0"); SetTypingJob::SetTypingJob(const QString& userId, const QString& roomId, bool typing, int timeout) : BaseJob(HttpVerb::Put, "SetTypingJob", - basePath % "/rooms/" % roomId % "/typing/" % userId, - Query { } - ) + basePath % "/rooms/" % roomId % "/typing/" % userId) { QJsonObject _data; _data.insert("typing", toJson(typing)); _data.insert("timeout", toJson(timeout)); setRequestData(_data); - - addExpectedContentType("application/json"); } diff --git a/jobs/generated/versions.cpp b/jobs/generated/versions.cpp index 8ff58365..938c1d34 100644 --- a/jobs/generated/versions.cpp +++ b/jobs/generated/versions.cpp @@ -20,10 +20,10 @@ class GetVersionsJob::Private GetVersionsJob::GetVersionsJob() : BaseJob(HttpVerb::Get, "GetVersionsJob", - basePath % "/versions", - Query { }, Data { }, false - ), d(new Private) -{ } + basePath % "/versions", false) + , d(new Private) +{ +} GetVersionsJob::~GetVersionsJob() = default; diff --git a/jobs/generated/whoami.cpp b/jobs/generated/whoami.cpp index d4da99d4..4f7b052c 100644 --- a/jobs/generated/whoami.cpp +++ b/jobs/generated/whoami.cpp @@ -20,10 +20,10 @@ class GetTokenOwnerJob::Private GetTokenOwnerJob::GetTokenOwnerJob() : BaseJob(HttpVerb::Get, "GetTokenOwnerJob", - basePath % "/account/whoami", - Query { } - ), d(new Private) -{ } + basePath % "/account/whoami") + , d(new Private) +{ +} GetTokenOwnerJob::~GetTokenOwnerJob() = default; -- cgit v1.2.3 From 4c1d52601be66caafa913171b5d5ed47ee4eb67d Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 5 Jan 2018 11:12:48 +0900 Subject: jobs/generated: content-repo.*, create_room.* --- jobs/generated/content-repo.cpp | 212 ++++++++++++++++++++++++++++++++++++++++ jobs/generated/content-repo.h | 102 +++++++++++++++++++ jobs/generated/create_room.cpp | 114 +++++++++++++++++++++ jobs/generated/create_room.h | 56 +++++++++++ 4 files changed, 484 insertions(+) create mode 100644 jobs/generated/content-repo.cpp create mode 100644 jobs/generated/content-repo.h create mode 100644 jobs/generated/create_room.cpp create mode 100644 jobs/generated/create_room.h (limited to 'jobs') diff --git a/jobs/generated/content-repo.cpp b/jobs/generated/content-repo.cpp new file mode 100644 index 00000000..ec6683bb --- /dev/null +++ b/jobs/generated/content-repo.cpp @@ -0,0 +1,212 @@ +/****************************************************************************** + * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN + */ + +#include "content-repo.h" + +#include "converters.h" + +#include +#include + +using namespace QMatrixClient; + +static const auto basePath = QStringLiteral("/_matrix/media/r0"); + +class UploadContentJob::Private +{ + public: + QString contentUri; +}; + +UploadContentJob::UploadContentJob(QByteArray content, const QString& filename, const QString& contentType) + : BaseJob(HttpVerb::Post, "UploadContentJob", + basePath % "/upload") + , d(new Private) +{ + setRequestHeader("Content-Type", contentType.toLatin1()); + + QUrlQuery _q; + if (!filename.isEmpty()) + _q.addQueryItem("filename", filename); + setRequestQuery(_q); + setRequestData(Data(content)); +} + +UploadContentJob::~UploadContentJob() = default; + +const QString& UploadContentJob::contentUri() const +{ + return d->contentUri; +} + +BaseJob::Status UploadContentJob::parseJson(const QJsonDocument& data) +{ + auto json = data.object(); + if (!json.contains("content_uri")) + return { JsonParseError, + "The key 'content_uri' not found in the response" }; + d->contentUri = fromJson(json.value("content_uri")); + return Success; +} + +class GetContentJob::Private +{ + public: + QString contentType; + QString contentDisposition; + QByteArray content; +}; + +GetContentJob::GetContentJob(const QString& serverName, const QString& mediaId) + : BaseJob(HttpVerb::Get, "GetContentJob", + basePath % "/download/" % serverName % "/" % mediaId, false) + , d(new Private) +{ + setExpectedContentTypes({ "*/*" }); +} + +GetContentJob::~GetContentJob() = default; + +const QString& GetContentJob::contentType() const +{ + return d->contentType; +} + +const QString& GetContentJob::contentDisposition() const +{ + return d->contentDisposition; +} + +QByteArray GetContentJob::content() const +{ + return d->content; +} + +BaseJob::Status GetContentJob::parseReply(QNetworkReply* reply) +{ + d->contentType = reply->rawHeader("Content-Type"); + d->contentDisposition = reply->rawHeader("Content-Disposition"); + d->content = reply->readAll(); + return Success; +} + +class GetContentOverrideNameJob::Private +{ + public: + QString contentType; + QString contentDisposition; + QByteArray content; +}; + +GetContentOverrideNameJob::GetContentOverrideNameJob(const QString& serverName, const QString& mediaId, const QString& fileName) + : BaseJob(HttpVerb::Get, "GetContentOverrideNameJob", + basePath % "/download/" % serverName % "/" % mediaId % "/" % fileName, false) + , d(new Private) +{ + setExpectedContentTypes({ "*/*" }); +} + +GetContentOverrideNameJob::~GetContentOverrideNameJob() = default; + +const QString& GetContentOverrideNameJob::contentType() const +{ + return d->contentType; +} + +const QString& GetContentOverrideNameJob::contentDisposition() const +{ + return d->contentDisposition; +} + +QByteArray GetContentOverrideNameJob::content() const +{ + return d->content; +} + +BaseJob::Status GetContentOverrideNameJob::parseReply(QNetworkReply* reply) +{ + d->contentType = reply->rawHeader("Content-Type"); + d->contentDisposition = reply->rawHeader("Content-Disposition"); + d->content = reply->readAll(); + return Success; +} + +class GetContentThumbnailJob::Private +{ + public: + QString contentType; + QByteArray content; +}; + +GetContentThumbnailJob::GetContentThumbnailJob(const QString& serverName, const QString& mediaId, int width, int height, const QString& method) + : BaseJob(HttpVerb::Get, "GetContentThumbnailJob", + basePath % "/thumbnail/" % serverName % "/" % mediaId, false) + , d(new Private) +{ + QUrlQuery _q; + _q.addQueryItem("width", QString("%1").arg(width)); + _q.addQueryItem("height", QString("%1").arg(height)); + if (!method.isEmpty()) + _q.addQueryItem("method", method); + setRequestQuery(_q); + setExpectedContentTypes({ "image/jpeg", "image/png" }); +} + +GetContentThumbnailJob::~GetContentThumbnailJob() = default; + +const QString& GetContentThumbnailJob::contentType() const +{ + return d->contentType; +} + +QByteArray GetContentThumbnailJob::content() const +{ + return d->content; +} + +BaseJob::Status GetContentThumbnailJob::parseReply(QNetworkReply* reply) +{ + d->contentType = reply->rawHeader("Content-Type"); + d->content = reply->readAll(); + return Success; +} + +class GetUrlPreviewJob::Private +{ + public: + double matrixImageSize; + QString ogImage; +}; + +GetUrlPreviewJob::GetUrlPreviewJob(const QString& url, double ts) + : BaseJob(HttpVerb::Get, "GetUrlPreviewJob", + basePath % "/preview_url") + , d(new Private) +{ + QUrlQuery _q; + _q.addQueryItem("url", url); + _q.addQueryItem("ts", QString("%1").arg(ts)); + setRequestQuery(_q); +} + +GetUrlPreviewJob::~GetUrlPreviewJob() = default; + +double GetUrlPreviewJob::matrixImageSize() const +{ + return d->matrixImageSize; +} + +const QString& GetUrlPreviewJob::ogImage() const +{ + return d->ogImage; +} + +BaseJob::Status GetUrlPreviewJob::parseJson(const QJsonDocument& data) +{ + auto json = data.object(); + d->matrixImageSize = fromJson(json.value("matrix:image:size")); + d->ogImage = fromJson(json.value("og:image")); + return Success; +} + diff --git a/jobs/generated/content-repo.h b/jobs/generated/content-repo.h new file mode 100644 index 00000000..1d844651 --- /dev/null +++ b/jobs/generated/content-repo.h @@ -0,0 +1,102 @@ +/****************************************************************************** + * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN + */ + +#pragma once + +#include "../basejob.h" + +#include +#include + + +namespace QMatrixClient +{ + // Operations + + class UploadContentJob : public BaseJob + { + public: + explicit UploadContentJob(QByteArray content, const QString& filename = {}, const QString& contentType = {}); + ~UploadContentJob() override; + + const QString& contentUri() const; + + protected: + Status parseJson(const QJsonDocument& data) override; + + private: + class Private; + QScopedPointer d; + }; + + class GetContentJob : public BaseJob + { + public: + explicit GetContentJob(const QString& serverName, const QString& mediaId); + ~GetContentJob() override; + + const QString& contentType() const; + const QString& contentDisposition() const; + QByteArray content() const; + + protected: + Status parseReply(QNetworkReply* reply) override; + + private: + class Private; + QScopedPointer d; + }; + + class GetContentOverrideNameJob : public BaseJob + { + public: + explicit GetContentOverrideNameJob(const QString& serverName, const QString& mediaId, const QString& fileName); + ~GetContentOverrideNameJob() override; + + const QString& contentType() const; + const QString& contentDisposition() const; + QByteArray content() const; + + protected: + Status parseReply(QNetworkReply* reply) override; + + private: + class Private; + QScopedPointer d; + }; + + class GetContentThumbnailJob : public BaseJob + { + public: + explicit GetContentThumbnailJob(const QString& serverName, const QString& mediaId, int width = {}, int height = {}, const QString& method = {}); + ~GetContentThumbnailJob() override; + + const QString& contentType() const; + QByteArray content() const; + + protected: + Status parseReply(QNetworkReply* reply) override; + + private: + class Private; + QScopedPointer d; + }; + + class GetUrlPreviewJob : public BaseJob + { + public: + explicit GetUrlPreviewJob(const QString& url, double ts = {}); + ~GetUrlPreviewJob() override; + + double matrixImageSize() const; + const QString& ogImage() const; + + protected: + Status parseJson(const QJsonDocument& data) override; + + private: + class Private; + QScopedPointer d; + }; +} // namespace QMatrixClient diff --git a/jobs/generated/create_room.cpp b/jobs/generated/create_room.cpp new file mode 100644 index 00000000..be06873a --- /dev/null +++ b/jobs/generated/create_room.cpp @@ -0,0 +1,114 @@ +/****************************************************************************** + * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN + */ + +#include "create_room.h" + +#include + +using namespace QMatrixClient; + +static const auto basePath = QStringLiteral("/_matrix/client/r0"); + +CreateRoomJob::Invite3pid::operator QJsonObject() const +{ + QJsonObject o; + o.insert("id_server", toJson(idServer)); + o.insert("medium", toJson(medium)); + o.insert("address", toJson(address)); + + return o; +} +namespace QMatrixClient +{ + template <> struct FromJson + { + CreateRoomJob::Invite3pid operator()(QJsonValue jv) + { + QJsonObject o = jv.toObject(); + CreateRoomJob::Invite3pid result; + result.idServer = + fromJson(o.value("id_server")); + result.medium = + fromJson(o.value("medium")); + result.address = + fromJson(o.value("address")); + + return result; + } + }; +} // namespace QMatrixClient + +CreateRoomJob::StateEvent::operator QJsonObject() const +{ + QJsonObject o; + o.insert("type", toJson(type)); + o.insert("state_key", toJson(stateKey)); + o.insert("content", toJson(content)); + + return o; +} +namespace QMatrixClient +{ + template <> struct FromJson + { + CreateRoomJob::StateEvent operator()(QJsonValue jv) + { + QJsonObject o = jv.toObject(); + CreateRoomJob::StateEvent result; + result.type = + fromJson(o.value("type")); + result.stateKey = + fromJson(o.value("state_key")); + result.content = + fromJson(o.value("content")); + + return result; + } + }; +} // namespace QMatrixClient + +class CreateRoomJob::Private +{ + public: + QString roomId; +}; + +CreateRoomJob::CreateRoomJob(const QString& visibility, const QString& roomAliasName, const QString& name, const QString& topic, const QVector& invite, const QVector& invite3pid, const QJsonObject& creationContent, const QVector& initialState, const QString& preset, bool isDirect) + : BaseJob(HttpVerb::Post, "CreateRoomJob", + basePath % "/createRoom") + , d(new Private) +{ + QJsonObject _data; + if (!visibility.isEmpty()) + _data.insert("visibility", toJson(visibility)); + if (!roomAliasName.isEmpty()) + _data.insert("room_alias_name", toJson(roomAliasName)); + if (!name.isEmpty()) + _data.insert("name", toJson(name)); + if (!topic.isEmpty()) + _data.insert("topic", toJson(topic)); + _data.insert("invite", toJson(invite)); + _data.insert("invite_3pid", toJson(invite3pid)); + _data.insert("creation_content", toJson(creationContent)); + _data.insert("initial_state", toJson(initialState)); + if (!preset.isEmpty()) + _data.insert("preset", toJson(preset)); + _data.insert("is_direct", toJson(isDirect)); + setRequestData(_data); +} + +CreateRoomJob::~CreateRoomJob() = default; + +const QString& CreateRoomJob::roomId() const +{ + return d->roomId; +} + +BaseJob::Status CreateRoomJob::parseJson(const QJsonDocument& data) +{ + auto json = data.object(); + d->roomId = fromJson(json.value("room_id")); + return Success; +} + diff --git a/jobs/generated/create_room.h b/jobs/generated/create_room.h new file mode 100644 index 00000000..a92cb106 --- /dev/null +++ b/jobs/generated/create_room.h @@ -0,0 +1,56 @@ +/****************************************************************************** + * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN + */ + +#pragma once + +#include "../basejob.h" + +#include +#include +#include + +#include "converters.h" + +namespace QMatrixClient +{ + // Operations + + class CreateRoomJob : public BaseJob + { + public: + // Inner data structures + + struct Invite3pid + { + QString idServer; + QString medium; + QString address; + + operator QJsonObject() const; + }; + + struct StateEvent + { + QString type; + QString stateKey; + QJsonObject content; + + operator QJsonObject() const; + }; + + // End of inner data structures + + explicit CreateRoomJob(const QString& visibility = {}, const QString& roomAliasName = {}, const QString& name = {}, const QString& topic = {}, const QVector& invite = {}, const QVector& invite3pid = {}, const QJsonObject& creationContent = {}, const QVector& initialState = {}, const QString& preset = {}, bool isDirect = {}); + ~CreateRoomJob() override; + + const QString& roomId() const; + + protected: + Status parseJson(const QJsonDocument& data) override; + + private: + class Private; + QScopedPointer d; + }; +} // namespace QMatrixClient -- cgit v1.2.3 From a671d1aeff6bc9c01aa4204e0c05da894c72b603 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 5 Jan 2018 11:17:13 +0900 Subject: MediaThumbnailJob: Rewire to GetContentThumbnailJob; decommission ThumbnailType "crop" thumbnail type didn't seem to be ever used. Once GTAD is able to generate enums, the respective code will show up in GetContentThumbnailJob and this parameter can be reintroduced in MediaThumbnailJob. As of now, just rely on the default "scale" value. --- jobs/mediathumbnailjob.cpp | 20 +++++--------------- jobs/mediathumbnailjob.h | 9 +++------ 2 files changed, 8 insertions(+), 21 deletions(-) (limited to 'jobs') diff --git a/jobs/mediathumbnailjob.cpp b/jobs/mediathumbnailjob.cpp index 9337549e..d768d253 100644 --- a/jobs/mediathumbnailjob.cpp +++ b/jobs/mediathumbnailjob.cpp @@ -18,22 +18,11 @@ #include "mediathumbnailjob.h" -#include -#include - using namespace QMatrixClient; -MediaThumbnailJob::MediaThumbnailJob(QUrl url, QSize requestedSize, - ThumbnailType thumbnailType) - : BaseJob(HttpVerb::Get, "MediaThumbnailJob", - QStringLiteral("/_matrix/media/v1/thumbnail/%1%2") - .arg(url.host(), url.path()), - Query( - { { "width", QString::number(requestedSize.width()) } - , { "height", QString::number(requestedSize.height()) } - , { "method", - thumbnailType == ThumbnailType::Scale ? "scale" : "crop" } - })) +MediaThumbnailJob::MediaThumbnailJob(QUrl url, QSize requestedSize) + : GetContentThumbnailJob(url.host(), url.path().mid(1), + requestedSize.width(), requestedSize.height()) { } QImage MediaThumbnailJob::thumbnail() const @@ -49,7 +38,8 @@ QImage MediaThumbnailJob::scaledThumbnail(QSize toSize) const BaseJob::Status MediaThumbnailJob::parseReply(QNetworkReply* reply) { - if( !_thumbnail.loadFromData(reply->readAll()) ) + GetContentThumbnailJob::parseReply(reply); + if( !_thumbnail.loadFromData(content()) ) { qCDebug(JOBS) << "MediaThumbnailJob: could not read image data"; } diff --git a/jobs/mediathumbnailjob.h b/jobs/mediathumbnailjob.h index 2d6853c7..66960b75 100644 --- a/jobs/mediathumbnailjob.h +++ b/jobs/mediathumbnailjob.h @@ -18,19 +18,16 @@ #pragma once -#include "basejob.h" +#include "generated/content-repo.h" #include namespace QMatrixClient { - enum class ThumbnailType {Crop, Scale}; - - class MediaThumbnailJob: public BaseJob + class MediaThumbnailJob: public GetContentThumbnailJob { public: - MediaThumbnailJob(QUrl url, QSize requestedSize, - ThumbnailType thumbnailType = ThumbnailType::Scale); + MediaThumbnailJob(QUrl url, QSize requestedSize); QImage thumbnail() const; QImage scaledThumbnail(QSize toSize) const; -- cgit v1.2.3 From 71b9445d7f696cdfabaf05ec7d26a52891dea873 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 12 Jan 2018 22:01:26 +0900 Subject: BaseJob::Data -> RequestData; support QIODevice* input/output --- jobs/basejob.cpp | 15 +++++------ jobs/basejob.h | 39 ++++++--------------------- jobs/generated/content-repo.cpp | 20 +++++++------- jobs/generated/content-repo.h | 11 ++++---- jobs/mediathumbnailjob.cpp | 13 ++++++--- jobs/mediathumbnailjob.h | 4 ++- jobs/requestdata.cpp | 38 ++++++++++++++++++++++++++ jobs/requestdata.h | 59 +++++++++++++++++++++++++++++++++++++++++ 8 files changed, 140 insertions(+), 59 deletions(-) create mode 100644 jobs/requestdata.cpp create mode 100644 jobs/requestdata.h (limited to 'jobs') diff --git a/jobs/basejob.cpp b/jobs/basejob.cpp index f508026f..0a81ad5c 100644 --- a/jobs/basejob.cpp +++ b/jobs/basejob.cpp @@ -46,7 +46,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) @@ -101,8 +101,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" }); @@ -159,9 +159,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 @@ -206,10 +206,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) ); @@ -447,4 +447,3 @@ void BaseJob::setLoggingCategory(LoggingCategory lcf) { d->logCat = lcf; } - diff --git a/jobs/basejob.h b/jobs/basejob.h index 42f5ecc2..6648dc1b 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 +#include + +// Any job that parses the response will need the below two. #include #include -#include -#include 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 -#include +#include 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 +#include +#include +#include +#include + +using namespace QMatrixClient; + +std::unique_ptr fromData(const QByteArray& data) +{ + auto source = std::make_unique(); + source->open(QIODevice::WriteOnly); + source->write(data); + source->close(); + return source; +} + +template +inline std::unique_ptr 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 + * + * 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 + +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(source)) + { } + RequestData(RequestData&&) = default; + RequestData& operator=(RequestData&&) = default; + ~RequestData(); + + QIODevice* source() const + { + return _source.get(); + } + + private: + std::unique_ptr _source; + }; +} // namespace QMatrixClient -- cgit v1.2.3 From 290ddb17189cf4c3a476b72eef13df1d26e8ede0 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 12 Jan 2018 22:02:42 +0900 Subject: Drop extraneous #include --- jobs/generated/administrative_contact.h | 1 - jobs/generated/banning.h | 1 - jobs/generated/create_room.h | 3 +-- jobs/generated/directory.h | 1 - jobs/generated/inviting.h | 1 - jobs/generated/kicking.h | 1 - jobs/generated/leaving.h | 1 - jobs/generated/list_public_rooms.h | 1 - jobs/generated/login.h | 1 - jobs/generated/profile.h | 1 - jobs/generated/receipts.h | 1 - jobs/generated/redaction.h | 1 - jobs/generated/third_party_membership.h | 1 - jobs/generated/typing.h | 1 - jobs/generated/versions.h | 1 - jobs/generated/whoami.h | 1 - 16 files changed, 1 insertion(+), 17 deletions(-) (limited to 'jobs') diff --git a/jobs/generated/administrative_contact.h b/jobs/generated/administrative_contact.h index a5f04781..67563719 100644 --- a/jobs/generated/administrative_contact.h +++ b/jobs/generated/administrative_contact.h @@ -7,7 +7,6 @@ #include "../basejob.h" #include -#include #include "converters.h" diff --git a/jobs/generated/banning.h b/jobs/generated/banning.h index 930020a5..2d6fbd9b 100644 --- a/jobs/generated/banning.h +++ b/jobs/generated/banning.h @@ -6,7 +6,6 @@ #include "../basejob.h" -#include namespace QMatrixClient diff --git a/jobs/generated/create_room.h b/jobs/generated/create_room.h index a92cb106..13c9d2c0 100644 --- a/jobs/generated/create_room.h +++ b/jobs/generated/create_room.h @@ -6,9 +6,8 @@ #include "../basejob.h" -#include #include -#include +#include #include "converters.h" diff --git a/jobs/generated/directory.h b/jobs/generated/directory.h index 8290a2b5..eeda563b 100644 --- a/jobs/generated/directory.h +++ b/jobs/generated/directory.h @@ -7,7 +7,6 @@ #include "../basejob.h" #include -#include namespace QMatrixClient diff --git a/jobs/generated/inviting.h b/jobs/generated/inviting.h index 7ed49637..eaa884df 100644 --- a/jobs/generated/inviting.h +++ b/jobs/generated/inviting.h @@ -6,7 +6,6 @@ #include "../basejob.h" -#include namespace QMatrixClient diff --git a/jobs/generated/kicking.h b/jobs/generated/kicking.h index 84d88945..3814bef7 100644 --- a/jobs/generated/kicking.h +++ b/jobs/generated/kicking.h @@ -6,7 +6,6 @@ #include "../basejob.h" -#include namespace QMatrixClient diff --git a/jobs/generated/leaving.h b/jobs/generated/leaving.h index f006ce19..cd39b612 100644 --- a/jobs/generated/leaving.h +++ b/jobs/generated/leaving.h @@ -6,7 +6,6 @@ #include "../basejob.h" -#include namespace QMatrixClient diff --git a/jobs/generated/list_public_rooms.h b/jobs/generated/list_public_rooms.h index f6467a21..7dcb8cf7 100644 --- a/jobs/generated/list_public_rooms.h +++ b/jobs/generated/list_public_rooms.h @@ -7,7 +7,6 @@ #include "../basejob.h" #include -#include #include "converters.h" diff --git a/jobs/generated/login.h b/jobs/generated/login.h index 0f68a13b..3ac955d4 100644 --- a/jobs/generated/login.h +++ b/jobs/generated/login.h @@ -6,7 +6,6 @@ #include "../basejob.h" -#include namespace QMatrixClient diff --git a/jobs/generated/profile.h b/jobs/generated/profile.h index 9cbf3865..1e09791d 100644 --- a/jobs/generated/profile.h +++ b/jobs/generated/profile.h @@ -6,7 +6,6 @@ #include "../basejob.h" -#include namespace QMatrixClient diff --git a/jobs/generated/receipts.h b/jobs/generated/receipts.h index e4065ddb..9eb7a489 100644 --- a/jobs/generated/receipts.h +++ b/jobs/generated/receipts.h @@ -7,7 +7,6 @@ #include "../basejob.h" #include -#include namespace QMatrixClient diff --git a/jobs/generated/redaction.h b/jobs/generated/redaction.h index 0a68f8cd..e3b3ff4f 100644 --- a/jobs/generated/redaction.h +++ b/jobs/generated/redaction.h @@ -6,7 +6,6 @@ #include "../basejob.h" -#include namespace QMatrixClient diff --git a/jobs/generated/third_party_membership.h b/jobs/generated/third_party_membership.h index b1669795..c7b5214e 100644 --- a/jobs/generated/third_party_membership.h +++ b/jobs/generated/third_party_membership.h @@ -6,7 +6,6 @@ #include "../basejob.h" -#include namespace QMatrixClient diff --git a/jobs/generated/typing.h b/jobs/generated/typing.h index 6eb3ddf4..0495ed0a 100644 --- a/jobs/generated/typing.h +++ b/jobs/generated/typing.h @@ -6,7 +6,6 @@ #include "../basejob.h" -#include namespace QMatrixClient diff --git a/jobs/generated/versions.h b/jobs/generated/versions.h index a7add8ba..686d7069 100644 --- a/jobs/generated/versions.h +++ b/jobs/generated/versions.h @@ -6,7 +6,6 @@ #include "../basejob.h" -#include #include diff --git a/jobs/generated/whoami.h b/jobs/generated/whoami.h index 21cb1a17..8e1952da 100644 --- a/jobs/generated/whoami.h +++ b/jobs/generated/whoami.h @@ -6,7 +6,6 @@ #include "../basejob.h" -#include namespace QMatrixClient -- cgit v1.2.3 From b3ad6aa8fe62f461c99ae7728482bd9958e38909 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 12 Jan 2018 22:04:28 +0900 Subject: BaseJob: afterStart(), beforeAbandon(), up/downloadProgress() To support the upcoming DownloadFileJob --- jobs/basejob.cpp | 17 ++++++++++++++--- jobs/basejob.h | 6 ++++++ 2 files changed, 20 insertions(+), 3 deletions(-) (limited to 'jobs') diff --git a/jobs/basejob.cpp b/jobs/basejob.cpp index 0a81ad5c..720ac560 100644 --- a/jobs/basejob.cpp +++ b/jobs/basejob.cpp @@ -217,15 +217,21 @@ void BaseJob::Private::sendRequest() } } -void BaseJob::beforeStart(const ConnectionData* connData) -{ -} +void BaseJob::beforeStart(const ConnectionData*) +{ } + +void BaseJob::afterStart(const ConnectionData*, QNetworkReply*) +{ } + +void BaseJob::beforeAbandon(QNetworkReply*) +{ } void BaseJob::start(const ConnectionData* connData) { d->connection = connData; beforeStart(connData); sendRequest(); + afterStart(connData, d->reply.data()); } void BaseJob::sendRequest() @@ -239,6 +245,10 @@ void BaseJob::sendRequest() connect( d->reply.data(), &QNetworkReply::finished, this, &BaseJob::gotReply ); if (d->reply->isRunning()) { + connect( d->reply.data(), &QNetworkReply::uploadProgress, + this, &BaseJob::uploadProgress); + connect( d->reply.data(), &QNetworkReply::downloadProgress, + this, &BaseJob::downloadProgress); d->timer.start(getCurrentTimeout()); qCDebug(d->logCat) << this << "request has been sent"; emit started(); @@ -431,6 +441,7 @@ void BaseJob::setStatus(int code, QString message) void BaseJob::abandon() { + beforeAbandon(d->reply.data()); this->disconnect(); if (d->reply) d->reply->disconnect(this); diff --git a/jobs/basejob.h b/jobs/basejob.h index 6648dc1b..e9e108c6 100644 --- a/jobs/basejob.h +++ b/jobs/basejob.h @@ -192,6 +192,9 @@ namespace QMatrixClient */ void failure(BaseJob*); + void downloadProgress(qint64 bytesReceived, qint64 bytesTotal); + void uploadProgress(qint64 bytesSent, qint64 bytesTotal); + protected: using headers_t = QHash; @@ -210,6 +213,9 @@ namespace QMatrixClient void setExpectedContentTypes(const QByteArrayList& contentTypes); virtual void beforeStart(const ConnectionData* connData); + virtual void afterStart(const ConnectionData* connData, + QNetworkReply* reply); + virtual void beforeAbandon(QNetworkReply*); /** * Used by gotReply() to check the received reply for general -- cgit v1.2.3 From 43710d6a5731778e28d907a3a264bcf74550073e Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 12 Jan 2018 22:07:33 +0900 Subject: DownloadFileJob Instead of exposing a QIODevice as GetContentJob does it gets a filename and saves the incoming payload into it. --- jobs/downloadfilejob.cpp | 113 +++++++++++++++++++++++++++++++++++++++++++++++ jobs/downloadfilejob.h | 27 +++++++++++ 2 files changed, 140 insertions(+) create mode 100644 jobs/downloadfilejob.cpp create mode 100644 jobs/downloadfilejob.h (limited to 'jobs') diff --git a/jobs/downloadfilejob.cpp b/jobs/downloadfilejob.cpp new file mode 100644 index 00000000..2530e259 --- /dev/null +++ b/jobs/downloadfilejob.cpp @@ -0,0 +1,113 @@ +#include "downloadfilejob.h" + +#include +#include +#include + +using namespace QMatrixClient; + +class DownloadFileJob::Private +{ + public: + Private() : tempFile(new QTemporaryFile()) { } + + explicit Private(const QString& localFilename) + : targetFile(new QFile(localFilename)) + , tempFile(new QFile(targetFile->fileName() + ".qmcdownload")) + { } + + QScopedPointer targetFile; + QScopedPointer tempFile; +}; + +DownloadFileJob::DownloadFileJob(const QString& serverName, + const QString& mediaId, + const QString& localFilename) + : GetContentJob(serverName, mediaId) + , d(localFilename.isEmpty() ? new Private : new Private(localFilename)) +{ + setObjectName("DownloadFileJob"); +} + +QString DownloadFileJob::targetFileName() const +{ + return (d->targetFile ? d->targetFile : d->tempFile)->fileName(); +} + +void DownloadFileJob::beforeStart(const ConnectionData*) +{ + if (d->targetFile && !d->targetFile->open(QIODevice::WriteOnly)) + { + qCWarning(JOBS) << "Couldn't open the file" + << d->targetFile->fileName() << "for writing"; + setStatus(FileError, "Could not open the target file for writing"); + return; + } + if (!d->tempFile->open(QIODevice::WriteOnly)) + { + qCWarning(JOBS) << "Couldn't open the temporary file" + << d->tempFile->fileName() << "for writing"; + setStatus(FileError, "Could not open the temporary download file"); + } + qCDebug(JOBS) << "Downloading to" << d->tempFile->fileName(); +} + +void DownloadFileJob::afterStart(const ConnectionData*, QNetworkReply* reply) +{ + connect(reply, &QNetworkReply::metaDataChanged, this, [this,reply] { + auto sizeHeader = reply->header(QNetworkRequest::ContentLengthHeader); + if (sizeHeader.isValid()) + { + auto targetSize = sizeHeader.value(); + if (targetSize != -1) + if (!d->tempFile->resize(targetSize)) + { + qCWarning(JOBS) << "Failed to allocate" << targetSize + << "bytes for" << d->tempFile->fileName(); + setStatus(FileError, + "Could not reserve disk space for download"); + } + } + }); + connect(reply, &QIODevice::readyRead, this, [this,reply] { + auto bytes = reply->read(reply->bytesAvailable()); + if (bytes.isEmpty()) + { + qCWarning(JOBS) + << "Unexpected empty chunk when downloading from" + << reply->url() << "to" << d->tempFile->fileName(); + } else { + d->tempFile->write(bytes); + } + }); +} + +void DownloadFileJob::beforeAbandon(QNetworkReply*) +{ + if (d->targetFile) + d->targetFile->remove(); + d->tempFile->remove(); +} + +BaseJob::Status DownloadFileJob::parseReply(QNetworkReply*) +{ + if (d->targetFile) + { + d->targetFile->close(); + if (!d->targetFile->remove()) + { + qCWarning(JOBS) << "Failed to remove the target file placeholder"; + return { FileError, "Couldn't finalise the download" }; + } + if (!d->tempFile->rename(d->targetFile->fileName())) + { + qCWarning(JOBS) << "Failed to rename" << d->tempFile->fileName() + << "to" << d->targetFile->fileName(); + return { FileError, "Couldn't finalise the download" }; + } + } + else + d->tempFile->close(); + qCDebug(JOBS) << "Saved a file as" << targetFileName(); + return Success; +} diff --git a/jobs/downloadfilejob.h b/jobs/downloadfilejob.h new file mode 100644 index 00000000..d798506c --- /dev/null +++ b/jobs/downloadfilejob.h @@ -0,0 +1,27 @@ +#pragma once + +#include "generated/content-repo.h" + +namespace QMatrixClient +{ + class DownloadFileJob : public GetContentJob + { + public: + enum { FileError = BaseJob::UserDefinedError + 1 }; + + DownloadFileJob(const QString& serverName, const QString& mediaId, + const QString& localFilename = {}); + + QString targetFileName() const; + + private: + class Private; + QScopedPointer d; + + void beforeStart(const ConnectionData*) override; + void afterStart(const ConnectionData*, + QNetworkReply* reply) override; + void beforeAbandon(QNetworkReply*) override; + Status parseReply(QNetworkReply*) override; + }; +} -- cgit v1.2.3 From 0de5e3b421633bb58805aa4b9d1bf3f07535702c Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Mon, 15 Jan 2018 10:27:31 +0900 Subject: DownloadFileJob: Be tolerant to request retries --- jobs/downloadfilejob.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'jobs') diff --git a/jobs/downloadfilejob.cpp b/jobs/downloadfilejob.cpp index 2530e259..06fa3b48 100644 --- a/jobs/downloadfilejob.cpp +++ b/jobs/downloadfilejob.cpp @@ -36,14 +36,15 @@ QString DownloadFileJob::targetFileName() const void DownloadFileJob::beforeStart(const ConnectionData*) { - if (d->targetFile && !d->targetFile->open(QIODevice::WriteOnly)) + if (d->targetFile && !d->targetFile->isReadable() && + !d->targetFile->open(QIODevice::WriteOnly)) { qCWarning(JOBS) << "Couldn't open the file" << d->targetFile->fileName() << "for writing"; setStatus(FileError, "Could not open the target file for writing"); return; } - if (!d->tempFile->open(QIODevice::WriteOnly)) + if (!d->tempFile->isReadable() && !d->tempFile->open(QIODevice::WriteOnly)) { qCWarning(JOBS) << "Couldn't open the temporary file" << d->tempFile->fileName() << "for writing"; -- cgit v1.2.3 From 84e183a70b831d1e18c373099988420f5050254b Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Thu, 25 Jan 2018 11:11:50 +0900 Subject: BaseJob::checkReply: log job returned status more explicitly --- jobs/basejob.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'jobs') diff --git a/jobs/basejob.cpp b/jobs/basejob.cpp index 720ac560..2d0d8b1b 100644 --- a/jobs/basejob.cpp +++ b/jobs/basejob.cpp @@ -291,9 +291,10 @@ bool checkContentType(const QByteArray& type, const QByteArrayList& patterns) BaseJob::Status BaseJob::checkReply(QNetworkReply* reply) const { - qCDebug(d->logCat) << this << "returned from" << reply->url().toDisplayString(); - if (reply->error() != QNetworkReply::NoError) - qCDebug(d->logCat) << this << "returned" << reply->error(); + qCDebug(d->logCat) << this << "returned" + << (reply->error() == QNetworkReply::NoError ? + "Success" : reply->errorString()) + << "from" << reply->url().toDisplayString(); switch( reply->error() ) { case QNetworkReply::NoError: -- cgit v1.2.3