QXmpp: QXmppFileSharingManager.h Source File

QXmpp Version: 1.14.4

Loading...
Searching...
No Matches
QXmppFileSharingManager.h
1// SPDX-FileCopyrightText: 2022 Jonah Brüchert <jbb@kaidan.im>
2// SPDX-FileCopyrightText: 2022 Linus Jahn <lnj@kaidan.im>
3//
4// SPDX-License-Identifier: LGPL-2.1-or-later
5
6#ifndef QXMPPFILESHARINGMANAGER_H
7#define QXMPPFILESHARINGMANAGER_H
8
9#include "QXmppBitsOfBinaryDataList.h"
10#include "QXmppClientExtension.h"
11#include "QXmppFileShare.h"
12#include "QXmppFileSharingProvider.h"
13#include "QXmppGlobal.h"
14
15#include <functional>
16#include <memory>
17#include <typeindex>
18#include <variant>
19
20#include <QFuture>
21#include <QMimeType>
22#include <QSize>
23
24class QIODevice;
25class QXmppFileDownloadPrivate;
26class QXmppFileMetadata;
27class QXmppFileShare;
28class QXmppFileSharingManagerPrivate;
29class QXmppFileUploadPrivate;
30
31 class QXMPP_EXPORT QXmppFileUpload : public QObject
32{
33 Q_OBJECT
35 Q_PROPERTY(float progress READ progress NOTIFY progressChanged)
36public:
37 struct FileResult {
38 QXmppFileShare fileShare;
39 QXmppBitsOfBinaryDataList dataBlobs;
40 };
41
42 using Result = std::variant<FileResult, QXmpp::Cancelled, QXmppError>;
43
44 ~QXmppFileUpload();
45
46 float progress() const;
47 Q_SIGNAL void progressChanged();
48
49 void cancel();
50 bool isFinished() const;
51 quint64 bytesTransferred() const;
52 quint64 bytesTotal() const;
53 Result result() const;
54
55 Q_SIGNAL void finished();
56
57private:
58 QXmppFileUpload();
59
60 void reportFinished();
61
62 std::unique_ptr<QXmppFileUploadPrivate> d;
63 friend class QXmppFileSharingManager;
64};
65
66Q_DECLARE_METATYPE(QXmppFileUpload::Result);
67
68 class QXMPP_EXPORT QXmppFileDownload : public QObject
69{
70 Q_OBJECT
72 Q_PROPERTY(float progress READ progress NOTIFY progressChanged)
73public:
74 enum HashVerificationResult {
82 NoStrongHashes,
84 HashVerified,
85 };
86
87 struct Downloaded {
88 HashVerificationResult hashVerificationResult;
89 };
90
91 using Result = std::variant<Downloaded, QXmpp::Cancelled, QXmppError>;
92
93 ~QXmppFileDownload();
94
95 float progress() const;
96 Q_SIGNAL void progressChanged();
97
98 void cancel();
99 bool isFinished() const;
100 quint64 bytesTransferred() const;
101 quint64 bytesTotal() const;
102 Result result() const;
103
104 Q_SIGNAL void finished();
105
106private:
107 QXmppFileDownload();
108
109 void reportProgress(quint64 bytesReceived, quint64 bytesTotal);
110 void reportFinished(Result);
111
112 std::unique_ptr<QXmppFileDownloadPrivate> d;
113 friend class QXmppFileSharingManager;
114};
115
116Q_DECLARE_METATYPE(QXmppFileDownload::Result);
117
118 class QXMPP_EXPORT QXmppFileSharingManager : public QXmppClientExtension
119{
120public:
121 struct MetadataThumbnail {
122 uint32_t width;
123 uint32_t height;
124 QByteArray data;
125 QMimeType mimeType;
126 };
127
128 struct MetadataGeneratorResult {
129 std::optional<QSize> dimensions;
130 std::optional<uint32_t> length;
131 QVector<MetadataThumbnail> thumbnails;
132 std::unique_ptr<QIODevice> dataDevice;
133 };
134
135 using MetadataGenerator = std::function<QFuture<std::shared_ptr<MetadataGeneratorResult>>(std::unique_ptr<QIODevice>)>;
136
137 QXmppFileSharingManager();
138 ~QXmppFileSharingManager();
139
140 void setMetadataGenerator(MetadataGenerator &&generator);
141
147 template<typename ProviderType>
148 void registerProvider(std::shared_ptr<ProviderType> manager)
149 {
150 std::type_index index(typeid(typename ProviderType::SourceType));
151 internalRegisterProvider(index, manager);
152 }
153
154 std::shared_ptr<QXmppFileUpload> uploadFile(std::shared_ptr<QXmppFileSharingProvider> provider,
155 const QString &filePath,
156 const std::optional<QString> &description = {});
157
158 std::shared_ptr<QXmppFileDownload> downloadFile(const QXmppFileShare &fileShare,
159 std::unique_ptr<QIODevice> output);
160
161private:
162 friend class QXmppEncryptedFileSharingProvider;
163
164 void internalRegisterProvider(std::type_index, std::shared_ptr<QXmppFileSharingProvider> provider);
165 std::shared_ptr<QXmppFileSharingProvider> providerForSource(const std::any &source) const;
166
167 std::unique_ptr<QXmppFileSharingManagerPrivate> d;
168};
169
170#endif // QXMPPFILESHARINGMANAGER_H
QXmppBitsOfBinaryDataList
Definition QXmppBitsOfBinaryDataList.h:16
QXmppClientExtension::QXmppClientExtension
QXmppClientExtension()
Definition QXmppClientExtension.cpp:12
QXmppFileDownload::progressChanged
Q_SIGNAL void progressChanged()
Emitted when new bytes have been transferred.
QXmppFileDownload::isFinished
bool isFinished() const
Returns whether the file transfer is finished.
Definition QXmppFileSharingManager.cpp:273
QXmppFileDownload::result
Result result() const
Returns the result of the download.
Definition QXmppFileSharingManager.cpp:299
QXmppFileDownload::bytesTransferred
quint64 bytesTransferred() const
Returns the number of bytes that have been uploaded or downloaded.
Definition QXmppFileSharingManager.cpp:281
QXmppFileDownload::bytesTotal
quint64 bytesTotal() const
Returns the number of bytes that totally need to be transferred.
Definition QXmppFileSharingManager.cpp:289
QXmppFileDownload::progress
float progress
Progress of the file download between 0.0 and 1.0.
Definition QXmppFileSharingManager.h:72
QXmppFileDownload::Result
std::variant< Downloaded, QXmpp::Cancelled, QXmppError > Result
Contains QXmpp::Success (successfully finished), QXmpp::Cancelled (manually cancelled) or QXmppError ...
Definition QXmppFileSharingManager.h:91
QXmppFileDownload::HashVerificationResult
HashVerificationResult
Definition QXmppFileSharingManager.h:74
QXmppFileDownload::HashVerified
@ HashVerified
The file integrity could be proved using a strong hash algorithm.
Definition QXmppFileSharingManager.h:84
QXmppFileDownload::NoStrongHashes
@ NoStrongHashes
File did not contain strong hashes (or no hashes at all) and no verification was done.
Definition QXmppFileSharingManager.h:82
QXmppFileDownload::cancel
void cancel()
Cancels the file transfer. finished() will be emitted.
Definition QXmppFileSharingManager.cpp:262
QXmppFileDownload::finished
Q_SIGNAL void finished()
QXmppFileMetadata
Definition QXmppFileMetadata.h:24
QXmppFileShare
Definition QXmppFileShare.h:56
QXmppFileSharingManager::setMetadataGenerator
void setMetadataGenerator(MetadataGenerator &&generator)
Register a function that is called when metadata needs to be gererated for a file.
Definition QXmppFileSharingManager.cpp:380
QXmppFileSharingManager::registerProvider
void registerProvider(std::shared_ptr< ProviderType > manager)
Register a provider for automatic downloads.
Definition QXmppFileSharingManager.h:148
QXmppFileSharingManager::MetadataGenerator
std::function< QFuture< std::shared_ptr< MetadataGeneratorResult > >(std::unique_ptr< QIODevice >)> MetadataGenerator
Definition QXmppFileSharingManager.h:135
QXmppFileUpload::finished
Q_SIGNAL void finished()
QXmppFileUpload::cancel
void cancel()
Cancels the file transfer. finished() will be emitted.
Definition QXmppFileSharingManager.cpp:119
QXmppFileUpload::Result
std::variant< FileResult, QXmpp::Cancelled, QXmppError > Result
Contains FileResult (successfully finished), QXmpp::Cancelled (manually cancelled) or QXmppError (an ...
Definition QXmppFileSharingManager.h:42
QXmppFileUpload::bytesTotal
quint64 bytesTotal() const
Returns the number of bytes that totally need to be transferred.
Definition QXmppFileSharingManager.cpp:147
QXmppFileUpload::result
Result result() const
Returns the result of the upload.
Definition QXmppFileSharingManager.cpp:157
QXmppFileUpload::isFinished
bool isFinished() const
Returns whether the file transfer is finished.
Definition QXmppFileSharingManager.cpp:131
QXmppFileUpload::progress
float progress
Progress of the file upload between 0.0 and 1.0.
Definition QXmppFileSharingManager.h:35
QXmppFileUpload::bytesTransferred
quint64 bytesTransferred() const
Returns the number of bytes that have been uploaded or downloaded.
Definition QXmppFileSharingManager.cpp:139
QXmppFileUpload::progressChanged
Q_SIGNAL void progressChanged()
Emitted when new bytes have been transferred.
QXmppFileDownload::Downloaded
Definition QXmppFileSharingManager.h:87
QXmppFileDownload::Downloaded::hashVerificationResult
HashVerificationResult hashVerificationResult
Definition QXmppFileSharingManager.h:88
QXmppFileUpload::FileResult
Contains QXmppFileShare of the uploaded file and possible data blobs containing referenced thumbnails...
Definition QXmppFileSharingManager.h:37
QXmppFileUpload::FileResult::fileShare
QXmppFileShare fileShare
File share with file metadata and file shares of the uploaded file.
Definition QXmppFileSharingManager.h:38
QXmppFileUpload::FileResult::dataBlobs
QXmppBitsOfBinaryDataList dataBlobs
Data blobs of possibly in the metadata referenced thumbnails.
Definition QXmppFileSharingManager.h:39