| 1 | // Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QT3DCORE_QDOWNLOADHELPERSERVICE_P_H |
| 5 | #define QT3DCORE_QDOWNLOADHELPERSERVICE_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists for the convenience |
| 12 | // of other Qt classes. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QSharedPointer> |
| 19 | #include <QObject> |
| 20 | #include <QUrl> |
| 21 | |
| 22 | #include <Qt3DCore/qaspectjob.h> |
| 23 | #include <Qt3DCore/qt3dcore_global.h> |
| 24 | #include <Qt3DCore/private/qservicelocator_p.h> |
| 25 | |
| 26 | QT_BEGIN_NAMESPACE |
| 27 | |
| 28 | class QThread; |
| 29 | class QNetworkAccessManager; |
| 30 | class QNetworkReply; |
| 31 | |
| 32 | namespace Qt3DCore { |
| 33 | |
| 34 | class QAspectEngine; |
| 35 | class QDownloadNetworkWorker; |
| 36 | class QDownloadHelperService; |
| 37 | class QDownloadHelperServicePrivate; |
| 38 | |
| 39 | class Q_3DCORESHARED_EXPORT QDownloadRequest |
| 40 | { |
| 41 | public: |
| 42 | QDownloadRequest(const QUrl &url); |
| 43 | virtual ~QDownloadRequest(); |
| 44 | |
| 45 | QUrl url() const { return m_url; } |
| 46 | bool succeeded() const { return m_succeeded; } |
| 47 | bool cancelled() const { return m_cancelled; } |
| 48 | |
| 49 | virtual void onDownloaded(); // this is called in dl thread |
| 50 | virtual void onCompleted() = 0; // this is called in the aspect thread |
| 51 | |
| 52 | protected: |
| 53 | QUrl m_url; |
| 54 | QByteArray m_data; |
| 55 | |
| 56 | private: |
| 57 | friend class QDownloadNetworkWorker; |
| 58 | friend class QDownloadHelperService; |
| 59 | bool m_succeeded; |
| 60 | bool m_cancelled; |
| 61 | }; |
| 62 | |
| 63 | typedef QSharedPointer<QDownloadRequest> QDownloadRequestPtr; |
| 64 | |
| 65 | |
| 66 | class Q_3DCORESHARED_EXPORT QDownloadHelperService : public QAbstractServiceProvider |
| 67 | { |
| 68 | Q_OBJECT |
| 69 | public: |
| 70 | explicit QDownloadHelperService(const QString &description = QString()); |
| 71 | ~QDownloadHelperService(); |
| 72 | |
| 73 | void submitRequest(const QDownloadRequestPtr &request); |
| 74 | void cancelRequest(const QDownloadRequestPtr &request); |
| 75 | void cancelAllRequests(); |
| 76 | |
| 77 | static QString urlToLocalFileOrQrc(const QUrl &url); |
| 78 | static bool isLocal(const QUrl &url); |
| 79 | static QDownloadHelperService *getService(QAspectEngine *engine); |
| 80 | |
| 81 | private: |
| 82 | Q_DECLARE_PRIVATE(QDownloadHelperService) |
| 83 | Q_PRIVATE_SLOT(d_func(), void _q_onRequestCompleted(const Qt3DCore::QDownloadRequestPtr &)) |
| 84 | }; |
| 85 | |
| 86 | typedef QSharedPointer<QDownloadHelperService> QDownloadHelperServicePtr; |
| 87 | |
| 88 | } // namespace Qt3DCore |
| 89 | |
| 90 | QT_END_NAMESPACE |
| 91 | |
| 92 | Q_DECLARE_METATYPE(Qt3DCore::QDownloadRequestPtr) // LCOV_EXCL_LINE |
| 93 | |
| 94 | #endif // QT3DCORE_QDOWNLOADHELPERSERVICE_P_H |
| 95 | |