diff options
| author | Linus Jahn <lnj@kaidan.im> | 2021-04-28 19:20:26 +0200 |
|---|---|---|
| committer | Linus Jahn <lnj@kaidan.im> | 2021-06-27 20:17:19 +0200 |
| commit | cbc61b2439a54fc31fd98cd5a0899d3e0aa9c5d9 (patch) | |
| tree | 0fb51bde3420ab6791bca78b777c113c42fbc3b1 /src/client/QXmppUploadRequestManager.cpp | |
| parent | c02b2a1d93b75e5210a2024b398df83c121ae7e7 (diff) | |
UploadRequestManager: Add QFuture-based requests
Diffstat (limited to 'src/client/QXmppUploadRequestManager.cpp')
| -rw-r--r-- | src/client/QXmppUploadRequestManager.cpp | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/client/QXmppUploadRequestManager.cpp b/src/client/QXmppUploadRequestManager.cpp index aed73cd1..cc6780c2 100644 --- a/src/client/QXmppUploadRequestManager.cpp +++ b/src/client/QXmppUploadRequestManager.cpp @@ -28,8 +28,10 @@ #include "QXmppDataForm.h" #include "QXmppDiscoveryIq.h" #include "QXmppDiscoveryManager.h" +#include "QXmppGlobal_p.h" #include "QXmppHttpUploadIq.h" +#include <QDomElement> #include <QFileInfo> #include <QMimeDatabase> @@ -93,6 +95,15 @@ public: QVector<QXmppUploadService> uploadServices; }; +/// +/// \typedef QXmppUploadRequestManager::SlotResult +/// +/// Contains the requested upload slot from the service or a QXmppStanza::Error +/// in case the request failed. +/// +/// \since QXmpp 1.5 +/// + QXmppUploadRequestManager::QXmppUploadRequestManager() : d(new QXmppUploadRequestManagerPrivate) { @@ -175,6 +186,88 @@ QString QXmppUploadRequestManager::requestUploadSlot(const QString &fileName, return {}; } +/// +/// Requests an upload slot from the server. +/// +/// \param file The info of the file to be uploaded. +/// \param uploadService The HTTP File Upload service that is used to request +/// the upload slot. If this is empty, the first +/// discovered one is used. +/// +/// \warning THIS API IS NOT FINALIZED YET! +/// +/// \since QXmpp 1.5 +/// +auto QXmppUploadRequestManager::requestSlot(const QFileInfo &file, + const QString &uploadService) -> QFuture<SlotResult> +{ + return requestSlot(file, file.fileName(), uploadService); +} + +/// +/// Requests an upload slot from the server. +/// +/// \param file The info of the file to be uploaded. +/// \param customFileName This name is used instead of the file's actual name +/// for requesting the upload slot. +/// \param uploadService The HTTP File Upload service that is used to request +/// the upload slot. If this is empty, the first +/// discovered one is used. +/// +/// \warning THIS API IS NOT FINALIZED YET! +/// +/// \since QXmpp 1.5 +/// +auto QXmppUploadRequestManager::requestSlot(const QFileInfo &file, + const QString &customFileName, + const QString &uploadService) -> QFuture<SlotResult> +{ + return requestSlot(customFileName, file.size(), + QMimeDatabase().mimeTypeForFile(file), + uploadService); +} + +/// +/// Requests an upload slot from the server. +/// +/// \param fileName The name of the file to be uploaded. This may be used by +/// the server to generate the URL. +/// \param fileSize The size of the file to be uploaded. The server may reject +/// too large files. +/// \param mimeType The content-type of the file. This can be used by the +/// server to set the HTTP MIME-type of the URL. +/// \param uploadService The HTTP File Upload service that is used to request +/// the upload slot. If this is empty, the first +/// discovered one is used. +/// +/// \warning THIS API IS NOT FINALIZED YET! +/// +/// \since QXmpp 1.5 +/// +auto QXmppUploadRequestManager::requestSlot(const QString &fileName, + qint64 fileSize, + const QMimeType &mimeType, + const QString &uploadService) -> QFuture<SlotResult> +{ + if (!serviceFound() && uploadService.isEmpty()) { + using Error = QXmppStanza::Error; + const auto errorMessage = QStringLiteral("Couldn't request upload slot: No service found."); + return makeReadyFuture(SlotResult(Error(Error::Cancel, Error::FeatureNotImplemented, errorMessage))); + } + + QXmppHttpUploadRequestIq iq; + if (uploadService.isEmpty()) + iq.setTo(d->uploadServices.first().jid()); + else + iq.setTo(uploadService); + iq.setType(QXmppIq::Get); + iq.setFileName(fileName); + iq.setSize(fileSize); + iq.setContentType(mimeType); + + return chainIq<SlotResult, QXmppHttpUploadSlotIq>(client()->sendIq(iq), this); +} + /// Returns true if an HTTP File Upload service has been discovered. bool QXmppUploadRequestManager::serviceFound() const |
