aboutsummaryrefslogtreecommitdiff
path: root/src/client/QXmppFileSharingManager.h
blob: a7f4c5ddddc2233fe6a5495594e07ab2536da8a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// SPDX-FileCopyrightText: 2022 Jonah Brüchert <jbb@kaidan.im>
//
// SPDX-License-Identifier: LGPL-2.1-or-later

#ifndef QXMPPFILESHARINGMANAGER_H
#define QXMPPFILESHARINGMANAGER_H

#include "QXmppClientExtension.h"
#include "QXmppFileShare.h"
#include "QXmppFileSharingProvider.h"
#include "QXmppGlobal.h"

#include <any>
#include <functional>
#include <memory>
#include <typeindex>
#include <variant>

#include <QFuture>
#include <QMimeType>
#include <QObject>
#include <QSize>

class QIODevice;
class QXmppFileMetadata;

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();

    void setMetadataGenerator(MetadataGenerator &&generator);

    ///
    /// \brief Register a provider for automatic downloads
    /// \param manager A shared_ptr to a QXmppFileSharingProvider subclass
    ///
    template<typename ProviderType>
    void registerProivder(std::shared_ptr<ProviderType> manager)
    {
        std::type_index index(typeid(typename ProviderType::SourceType));
        internalRegisterProvider(index, manager);
    }

    std::shared_ptr<QXmppUpload> sendFile(std::shared_ptr<QXmppFileSharingProvider> provider,
                                          const QString &filePath,
                                          const std::optional<QString> &description = {});

    std::shared_ptr<QXmppDownload> downloadFile(const QXmppFileShare &fileShare,
                                                std::unique_ptr<QIODevice> &&output);

private:
    void internalRegisterProvider(std::type_index, std::shared_ptr<QXmppFileSharingProvider> provider);

    MetadataGenerator m_metadataGenerator;
    std::unordered_map<std::type_index, std::shared_ptr<QXmppFileSharingProvider>> m_providers;
};

#endif  // QXMPPFILESHARINGMANAGER_H