QXmpp  Version: 1.5.1
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 
24 class QIODevice;
25 class QXmppFileDownloadPrivate;
26 class QXmppFileMetadata;
27 class QXmppFileShare;
28 class QXmppFileSharingManagerPrivate;
29 class QXmppFileUploadPrivate;
30 
31 class QXMPP_EXPORT QXmppFileUpload : public QObject
32 {
33  Q_OBJECT
35  Q_PROPERTY(float progress READ progress NOTIFY progressChanged)
36 public:
37  struct FileResult
38  {
41  };
42 
43  using Result = std::variant<FileResult, QXmpp::Cancelled, QXmppError>;
44 
45  ~QXmppFileUpload();
46 
47  float progress() const;
48  Q_SIGNAL void progressChanged();
49 
50  void cancel();
51  bool isFinished() const;
52  quint64 bytesTransferred() const;
53  quint64 bytesTotal() const;
54  Result result() const;
55 
56  Q_SIGNAL void finished();
57 
58 private:
60 
61  void reportFinished();
62 
63  std::unique_ptr<QXmppFileUploadPrivate> d;
64  friend class QXmppFileSharingManager;
65 };
66 
67 Q_DECLARE_METATYPE(QXmppFileUpload::Result);
68 
69 class QXMPP_EXPORT QXmppFileDownload : public QObject
70 {
71  Q_OBJECT
73  Q_PROPERTY(float progress READ progress NOTIFY progressChanged)
74 public:
86  };
87 
88  struct Downloaded
89  {
91  };
92 
93  using Result = std::variant<Downloaded, QXmpp::Cancelled, QXmppError>;
94 
96 
97  float progress() const;
98  Q_SIGNAL void progressChanged();
99 
100  void cancel();
101  bool isFinished() const;
102  quint64 bytesTransferred() const;
103  quint64 bytesTotal() const;
104  Result result() const;
105 
106  Q_SIGNAL void finished();
107 
108 private:
110 
111  void reportProgress(quint64 bytesReceived, quint64 bytesTotal);
112  void reportFinished(Result);
113 
114  std::unique_ptr<QXmppFileDownloadPrivate> d;
115  friend class QXmppFileSharingManager;
116 };
117 
118 Q_DECLARE_METATYPE(QXmppFileDownload::Result);
119 
121 {
122 public:
123  struct MetadataThumbnail
124  {
125  uint32_t width;
126  uint32_t height;
127  QByteArray data;
128  QMimeType mimeType;
129  };
130 
131  struct MetadataGeneratorResult
132  {
133  std::optional<QSize> dimensions;
134  std::optional<uint32_t> length;
135  QVector<MetadataThumbnail> thumbnails;
136  std::unique_ptr<QIODevice> dataDevice;
137  };
138 
139  using MetadataGenerator = std::function<QFuture<std::shared_ptr<MetadataGeneratorResult>>(std::unique_ptr<QIODevice>)>;
140 
143 
144  void setMetadataGenerator(MetadataGenerator &&generator);
145 
151  template<typename ProviderType>
152  void registerProvider(std::shared_ptr<ProviderType> manager)
153  {
154  std::type_index index(typeid(typename ProviderType::SourceType));
155  internalRegisterProvider(index, manager);
156  }
157 
158  std::shared_ptr<QXmppFileUpload> uploadFile(std::shared_ptr<QXmppFileSharingProvider> provider,
159  const QString &filePath,
160  const std::optional<QString> &description = {});
161 
162  std::shared_ptr<QXmppFileDownload> downloadFile(const QXmppFileShare &fileShare,
163  std::unique_ptr<QIODevice> output);
164 
165 private:
167 
168  void internalRegisterProvider(std::type_index, std::shared_ptr<QXmppFileSharingProvider> provider);
169  std::shared_ptr<QXmppFileSharingProvider> providerForSource(const std::any &source) const;
170 
171  std::unique_ptr<QXmppFileSharingManagerPrivate> d;
172 };
173 
174 #endif // QXMPPFILESHARINGMANAGER_H
Definition: QXmppBitsOfBinaryDataList.h:16
The QXmppClientExtension class is the base class for QXmppClient extensions.
Definition: QXmppClientExtension.h:33
Definition: QXmppEncryptedFileSharingProvider.h:16
Provides progress of stateless file sharing uploads.
Definition: QXmppFileSharingManager.h:70
Q_SIGNAL void progressChanged()
Emitted when new bytes have been transferred.
std::variant< Downloaded, QXmpp::Cancelled, QXmppError > Result
Contains QXmpp::Success (successfully finished), QXmpp::Cancelled (manually cancelled) or QXmppError ...
Definition: QXmppFileSharingManager.h:93
Q_SIGNAL void finished()
HashVerificationResult
Definition: QXmppFileSharingManager.h:75
@ HashVerified
The file integrity could be proved using a strong hash algorithm.
Definition: QXmppFileSharingManager.h:85
@ NoStrongHashes
File did not contain strong hashes (or no hashes at all) and no verification was done.
Definition: QXmppFileSharingManager.h:83
Definition: QXmppFileMetadata.h:24
Definition: QXmppFileShare.h:24
Definition: QXmppFileSharingManager.h:121
std::function< QFuture< std::shared_ptr< MetadataGeneratorResult > >(std::unique_ptr< QIODevice >)> MetadataGenerator
Definition: QXmppFileSharingManager.h:139
void registerProvider(std::shared_ptr< ProviderType > manager)
Register a provider for automatic downloads.
Definition: QXmppFileSharingManager.h:152
Provides progress of stateless file sharing uploads.
Definition: QXmppFileSharingManager.h:32
Q_SIGNAL void progressChanged()
Emitted when new bytes have been transferred.
Q_SIGNAL void finished()
std::variant< FileResult, QXmpp::Cancelled, QXmppError > Result
Contains FileResult (successfully finished), QXmpp::Cancelled (manually cancelled) or QXmppError (an ...
Definition: QXmppFileSharingManager.h:43
Definition: QXmppFileSharingManager.h:89
HashVerificationResult hashVerificationResult
Definition: QXmppFileSharingManager.h:90
Contains QXmppFileShare of the uploaded file and possible data blobs containing referenced thumbnails...
Definition: QXmppFileSharingManager.h:38
QXmppBitsOfBinaryDataList dataBlobs
Data blobs of possibly in the metadata referenced thumbnails.
Definition: QXmppFileSharingManager.h:40
QXmppFileShare fileShare
File share with file metadata and file shares of the uploaded file.
Definition: QXmppFileSharingManager.h:39