QXmpp: QXmppHttpUploadManager.h Source File

QXmpp Version: 1.14.2

Loading...
Searching...
No Matches
QXmppHttpUploadManager.h
1// SPDX-FileCopyrightText: 2022 Linus Jahn <lnj@kaidan.im>
2// SPDX-FileCopyrightText: 2025 Filipe Azevedo <pasnox@gmail.com>
3//
4// SPDX-License-Identifier: LGPL-2.1-or-later
5
6#ifndef QXMPPHTTPUPLOADMANAGER_H
7#define QXMPPHTTPUPLOADMANAGER_H
8
9#include "QXmppClientExtension.h"
10#include "QXmppError.h"
11
12#include <optional>
13#include <variant>
14
15#include <QUrl>
16
17class QFileInfo;
18class QNetworkAccessManager;
19class QXmppHttpUploadServicePrivate;
20class QXmppHttpUploadSlotIq;
21template<typename T>
22class QXmppTask;
23struct QXmppHttpUploadPrivate;
24struct QXmppHttpUploadManagerPrivate;
25
26 class QXMPP_EXPORT QXmppHttpUploadService
27{
28public:
29 QXmppHttpUploadService();
30 QXMPP_PRIVATE_DECLARE_RULE_OF_SIX(QXmppHttpUploadService)
31
32 QString jid() const;
33 void setJid(const QString &jid);
34
35 std::optional<quint64> sizeLimit() const;
36 void setSizeLimit(std::optional<quint64> sizeLimit);
37
38private:
39 QSharedDataPointer<QXmppHttpUploadServicePrivate> d;
40};
41
42 class QXMPP_EXPORT QXmppHttpUpload : public QObject
43{
44 Q_OBJECT
45 Q_PROPERTY(float progress READ progress NOTIFY progressChanged)
46 Q_PROPERTY(quint64 bytesSent READ bytesSent NOTIFY progressChanged)
47 Q_PROPERTY(quint64 bytesTotal READ bytesTotal NOTIFY progressChanged)
48
49public:
50 using Result = std::variant<QUrl, QXmpp::Cancelled, QXmppError>;
51
52 ~QXmppHttpUpload();
53
54 float progress() const;
55 quint64 bytesSent() const;
56 quint64 bytesTotal() const;
57
58 void cancel();
59 bool isFinished() const;
60 std::optional<Result> result() const;
61
62 Q_SIGNAL void progressChanged();
63 Q_SIGNAL void finished(const QXmppHttpUpload::Result &result);
64
65private:
66 friend class QXmppHttpUploadManager;
67
68 QXmppHttpUpload();
69
70 std::unique_ptr<QXmppHttpUploadPrivate> d;
71};
72
73Q_DECLARE_METATYPE(QXmppHttpUpload::Result);
74
75class QXMPP_EXPORT QXmppHttpUploadManager : public QXmppClientExtension
76{
77 Q_OBJECT
78 Q_PROPERTY(QVector<QXmppHttpUploadService> services READ services NOTIFY servicesChanged)
79 Q_PROPERTY(QXmppHttpUploadManager::Support support READ support NOTIFY supportChanged)
80
81public:
82 enum class Support {
83 Unknown,
84 Unsupported,
85 Supported,
86 };
87 Q_ENUM(Support)
88
89 QXmppHttpUploadManager();
90 explicit QXmppHttpUploadManager(QNetworkAccessManager *netManager);
91 ~QXmppHttpUploadManager();
92
93 QVector<QXmppHttpUploadService> services() const;
94 Q_SIGNAL void servicesChanged();
95
96 Support support() const;
97 Q_SIGNAL void supportChanged();
98
99 std::shared_ptr<QXmppHttpUpload> uploadFile(std::unique_ptr<QIODevice> data, const QString &filename, const QMimeType &mimeType, qint64 fileSize = -1, const QString &uploadServiceJid = {});
100 std::shared_ptr<QXmppHttpUpload> uploadFile(const QFileInfo &fileInfo, const QString &filename = {}, const QString &uploadServiceJid = {});
101
102protected:
103 void onRegistered(QXmppClient *client) override;
104 void onUnregistered(QXmppClient *client) override;
105
106private:
107 QXmppTask<void> updateService(const QString &jid);
108 void updateServices();
109 void resetCachedData();
110 void updateCachedData();
111
112 using SlotResult = std::variant<QXmppHttpUploadSlotIq, QXmppError>;
113 QXmppTask<SlotResult> requestSlot(const QFileInfo &file,
114 const QString &uploadService = {});
115 QXmppTask<SlotResult> requestSlot(const QFileInfo &file,
116 const QString &customFileName,
117 const QString &uploadService = {});
118 QXmppTask<SlotResult> requestSlot(const QString &fileName,
119 qint64 fileSize,
120 const QMimeType &mimeType,
121 const QString &uploadService = {});
122
123 friend struct QXmppHttpUploadManagerPrivate;
124
125 std::unique_ptr<QXmppHttpUploadManagerPrivate> d;
126};
127
128#endif // QXMPPHTTPUPLOADMANAGER_H
QXmppClientExtension::QXmppClientExtension
QXmppClientExtension()
Definition QXmppClientExtension.cpp:12
QXmppHttpUpload::finished
Q_SIGNAL void finished(const QXmppHttpUpload::Result &result)
QXmppHttpUpload::isFinished
bool isFinished() const
Definition QXmppHttpUploadManager.cpp:273
QXmppHttpUpload::Result
std::variant< QUrl, QXmpp::Cancelled, QXmppError > Result
Definition QXmppHttpUploadManager.h:50
QXmppHttpUpload::cancel
void cancel()
Definition QXmppHttpUploadManager.cpp:262
QXmppHttpUpload::progress
float progress
Definition QXmppHttpUploadManager.h:45
QXmppHttpUpload::progressChanged
Q_SIGNAL void progressChanged()
QXmppHttpUpload::bytesTotal
quint64 bytesTotal
Definition QXmppHttpUploadManager.h:47
QXmppHttpUpload::bytesSent
quint64 bytesSent
Definition QXmppHttpUploadManager.h:46
QXmppHttpUpload::result
std::optional< Result > result() const
Definition QXmppHttpUploadManager.cpp:282
QXmppHttpUploadManager
Definition QXmppHttpUploadManager.h:76
QXmppHttpUploadManager::support
QXmppHttpUploadManager::Support support
Definition QXmppHttpUploadManager.h:79
QXmppHttpUploadManager::QXmppHttpUploadManager
QXmppHttpUploadManager()
Definition QXmppHttpUploadManager.cpp:321
QXmppHttpUploadManager::supportChanged
Q_SIGNAL void supportChanged()
Emitted when support has changed.
QXmppHttpUploadManager::Support
Support
Definition QXmppHttpUploadManager.h:82
QXmppHttpUploadManager::servicesChanged
Q_SIGNAL void servicesChanged()
Emitted when services have changed.
QXmppHttpUploadManager::uploadFile
std::shared_ptr< QXmppHttpUpload > uploadFile(std::unique_ptr< QIODevice > data, const QString &filename, const QMimeType &mimeType, qint64 fileSize=-1, const QString &uploadServiceJid={})
Definition QXmppHttpUploadManager.cpp:401
QXmppHttpUploadManager::services
QVector< QXmppHttpUploadService > services
Definition QXmppHttpUploadManager.h:78
QXmppHttpUploadService::sizeLimit
std::optional< quint64 > sizeLimit() const
Definition QXmppHttpUploadManager.cpp:103
QXmppHttpUploadService::jid
QString jid() const
Returns the JID of the HTTP File Upload service.
Definition QXmppHttpUploadManager.cpp:88
QXmppHttpUploadService::setJid
void setJid(const QString &jid)
Sets the JID of the HTTP File Upload service.
Definition QXmppHttpUploadManager.cpp:94
QXmppHttpUploadService::setSizeLimit
void setSizeLimit(std::optional< quint64 > sizeLimit)
Sets the size limit of files that can be uploaded to this upload service.
Definition QXmppHttpUploadManager.cpp:109
QXmppHttpUploadSlotIq
Represents an HTTP File Upload IQ result for receiving an upload slot as defined by XEP-0363: HTTP Fi...
Definition QXmppHttpUploadIq.h:69
QXmppTask
Definition QXmppTask.h:46
QXmpp
Definition Algorithms.h:14
QXmppError
Definition QXmppError.h:17