Loading...
Searching...
No Matches
QXmppFileSharingManager.h
// SPDX-FileCopyrightText: 2022 Jonah Brüchert <jbb@kaidan.im>
// SPDX-FileCopyrightText: 2022 Linus Jahn <lnj@kaidan.im>
//
// SPDX-License-Identifier: LGPL-2.1-or-later
#ifndef QXMPPFILESHARINGMANAGER_H
#define QXMPPFILESHARINGMANAGER_H
#include "QXmppBitsOfBinaryDataList.h"
#include "QXmppClientExtension.h"
#include "QXmppFileShare.h"
#include "QXmppFileSharingProvider.h"
#include "QXmppGlobal.h"
#include <functional>
#include <memory>
#include <typeindex>
#include <variant>
#include <QFuture>
#include <QMimeType>
#include <QSize>
class QIODevice;
class QXmppFileDownloadPrivate;
class QXmppFileMetadata;
class QXmppFileShare;
class QXmppFileSharingManagerPrivate;
class QXmppFileUploadPrivate;
class QXMPP_EXPORT QXmppFileUpload : public QObject
{
Q_OBJECT
Q_PROPERTY(float progress READ progress NOTIFY progressChanged)
public:
struct FileResult {
QXmppFileShare fileShare;
QXmppBitsOfBinaryDataList dataBlobs;
};
using Result = std::variant<FileResult, QXmpp::Cancelled, QXmppError>;
~QXmppFileUpload();
float progress() const;
Q_SIGNAL void progressChanged();
void cancel();
bool isFinished() const;
quint64 bytesTransferred() const;
quint64 bytesTotal() const;
Result result() const;
Q_SIGNAL void finished();
private:
QXmppFileUpload();
void reportFinished();
std::unique_ptr<QXmppFileUploadPrivate> d;
friend class QXmppFileSharingManager;
};
Q_DECLARE_METATYPE(QXmppFileUpload::Result);
class QXMPP_EXPORT QXmppFileDownload : public QObject
{
Q_OBJECT
Q_PROPERTY(float progress READ progress NOTIFY progressChanged)
public:
enum HashVerificationResult {
NoStrongHashes,
HashVerified,
};
struct Downloaded {
HashVerificationResult hashVerificationResult;
};
using Result = std::variant<Downloaded, QXmpp::Cancelled, QXmppError>;
~QXmppFileDownload();
float progress() const;
Q_SIGNAL void progressChanged();
void cancel();
bool isFinished() const;
quint64 bytesTransferred() const;
quint64 bytesTotal() const;
Result result() const;
Q_SIGNAL void finished();
private:
QXmppFileDownload();
void reportProgress(quint64 bytesReceived, quint64 bytesTotal);
void reportFinished(Result);
std::unique_ptr<QXmppFileDownloadPrivate> d;
friend class QXmppFileSharingManager;
};
Q_DECLARE_METATYPE(QXmppFileDownload::Result);
class QXMPP_EXPORT QXmppFileSharingManager : public QXmppClientExtension
{
public:
struct MetadataThumbnail {
uint32_t width;
uint32_t height;
QByteArray data;
QMimeType mimeType;
};
struct MetadataGeneratorResult {
std::optional<QSize> dimensions;
std::optional<uint32_t> length;
QVector<MetadataThumbnail> thumbnails;
std::unique_ptr<QIODevice> dataDevice;
};
using MetadataGenerator = std::function<QFuture<std::shared_ptr<MetadataGeneratorResult>>(std::unique_ptr<QIODevice>)>;
QXmppFileSharingManager();
~QXmppFileSharingManager();
void setMetadataGenerator(MetadataGenerator &&generator);
template<typename ProviderType>
void registerProvider(std::shared_ptr<ProviderType> manager)
{
std::type_index index(typeid(typename ProviderType::SourceType));
internalRegisterProvider(index, manager);
}
std::shared_ptr<QXmppFileUpload> uploadFile(std::shared_ptr<QXmppFileSharingProvider> provider,
const QString &filePath,
const std::optional<QString> &description = {});
std::shared_ptr<QXmppFileDownload> downloadFile(const QXmppFileShare &fileShare,
std::unique_ptr<QIODevice> output);
private:
friend class QXmppEncryptedFileSharingProvider;
void internalRegisterProvider(std::type_index, std::shared_ptr<QXmppFileSharingProvider> provider);
std::shared_ptr<QXmppFileSharingProvider> providerForSource(const std::any &source) const;
std::unique_ptr<QXmppFileSharingManagerPrivate> d;
};
#endif // QXMPPFILESHARINGMANAGER_H
QXmppBitsOfBinaryDataList
Definition QXmppBitsOfBinaryDataList.h:16
QXmppClientExtension
The QXmppClientExtension class is the base class for QXmppClient extensions.
Definition QXmppClientExtension.h:32
QXmppEncryptedFileSharingProvider
Definition QXmppEncryptedFileSharingProvider.h:16
QXmppFileDownload
Provides progress of stateless file sharing uploads.
Definition QXmppFileSharingManager.h:69
QXmppFileDownload::progressChanged
Q_SIGNAL void progressChanged()
Emitted when new bytes have been transferred.
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::finished
Q_SIGNAL void finished()
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
QXmppFileMetadata
Definition QXmppFileMetadata.h:24
QXmppFileShare
Definition QXmppFileShare.h:50
QXmppFileSharingManager
Definition QXmppFileSharingManager.h:119
QXmppFileSharingManager::MetadataGenerator
std::function< QFuture< std::shared_ptr< MetadataGeneratorResult > >(std::unique_ptr< QIODevice >)> MetadataGenerator
Definition QXmppFileSharingManager.h:135
QXmppFileSharingManager::registerProvider
void registerProvider(std::shared_ptr< ProviderType > manager)
Register a provider for automatic downloads.
Definition QXmppFileSharingManager.h:148
QXmppFileUpload
Provides progress of stateless file sharing uploads.
Definition QXmppFileSharingManager.h:32
QXmppFileUpload::progressChanged
Q_SIGNAL void progressChanged()
Emitted when new bytes have been transferred.
QXmppFileUpload::finished
Q_SIGNAL void finished()
QXmppFileUpload::Result
std::variant< FileResult, QXmpp::Cancelled, QXmppError > Result
Contains FileResult (successfully finished), QXmpp::Cancelled (manually cancelled) or QXmppError (an ...
Definition QXmppFileSharingManager.h:42
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::dataBlobs
QXmppBitsOfBinaryDataList dataBlobs
Data blobs of possibly in the metadata referenced thumbnails.
Definition QXmppFileSharingManager.h:39
QXmppFileUpload::FileResult::fileShare
QXmppFileShare fileShare
File share with file metadata and file shares of the uploaded file.
Definition QXmppFileSharingManager.h:38