| 1 | // Copyright (C) 2024 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qqmlassetdownloader_p.h" |
| 5 | #include <QtQml/qqmlcontext.h> |
| 6 | |
| 7 | QT_BEGIN_NAMESPACE |
| 8 | |
| 9 | using Assets::Downloader::AssetDownloader; |
| 10 | using Assets::Downloader::AssetDownloaderHelper; |
| 11 | using Assets::Downloader::QQmlAssetDownloader; |
| 12 | |
| 13 | /*! |
| 14 | \qmlmodule Qt.labs.assetdownloader |
| 15 | \title Qt Labs Asset Downloader QML Types |
| 16 | \brief Provides the AssetDownloader type to download external assets for Qt Examples. |
| 17 | |
| 18 | \note This module is not part of the public Qt API and may change without notice. |
| 19 | It is intended for internal use or tightly controlled environments. |
| 20 | */ |
| 21 | |
| 22 | /*! |
| 23 | \qmltype AssetDownloader |
| 24 | //! \nativetype QQmlAssetDownloader |
| 25 | \inqmlmodule Qt.labs.assetdownloader |
| 26 | \brief Downloads assets asynchronously for use in QML Examples. |
| 27 | \since 6.8 |
| 28 | |
| 29 | The AssetDownloader type provides a convenient way to download external assets |
| 30 | such as images, models, or data files from remote URLs and make them available to |
| 31 | QML applications. |
| 32 | |
| 33 | \note This type is not part of the public Qt API and may change without notice. |
| 34 | It is intended for internal use or tightly controlled environments. |
| 35 | |
| 36 | \section1 QML Usage |
| 37 | |
| 38 | To use this type in QML, import the module and instantiate the downloader: |
| 39 | |
| 40 | \qml |
| 41 | import Qt.labs.assetdownloader |
| 42 | |
| 43 | AssetDownloader { |
| 44 | downloadBase: "https://example.com/assets/" |
| 45 | preferredLocalDownloadDir: "file:///home/user/assets/" |
| 46 | jsonFileName: "data.json" |
| 47 | zipFileName: "archive.zip" |
| 48 | onFinished: (success) => { |
| 49 | if (success) |
| 50 | console.log("Download completed successfully"); |
| 51 | else |
| 52 | console.log("Download failed"); |
| 53 | } |
| 54 | } |
| 55 | \endqml |
| 56 | */ |
| 57 | |
| 58 | /*! |
| 59 | \qmlproperty url AssetDownloader::downloadBase |
| 60 | |
| 61 | The base URL from which assets will be downloaded. |
| 62 | */ |
| 63 | |
| 64 | /*! |
| 65 | \qmlproperty url AssetDownloader::preferredLocalDownloadDir |
| 66 | |
| 67 | The preferred local directory where downloaded assets should be stored. |
| 68 | */ |
| 69 | |
| 70 | /*! |
| 71 | \qmlproperty url AssetDownloader::offlineAssetsFilePath |
| 72 | |
| 73 | The file path to offline assets, used when network access is unavailable. |
| 74 | */ |
| 75 | |
| 76 | /*! |
| 77 | \qmlproperty string AssetDownloader::jsonFileName |
| 78 | |
| 79 | The name of the asset JSON file to be downloaded. This file should contain a list |
| 80 | of assets to be downloaded. |
| 81 | |
| 82 | Example format: |
| 83 | |
| 84 | \code |
| 85 | { |
| 86 | "url": "<base URL for asset downloads>", |
| 87 | "assets": [ |
| 88 | "<relative path to asset file>", |
| 89 | ... |
| 90 | ] |
| 91 | } |
| 92 | \endcode |
| 93 | */ |
| 94 | |
| 95 | /*! |
| 96 | \qmlproperty string AssetDownloader::zipFileName |
| 97 | |
| 98 | The name of the ZIP file to be downloaded. |
| 99 | */ |
| 100 | |
| 101 | /*! |
| 102 | \qmlproperty url AssetDownloader::localDownloadDir |
| 103 | |
| 104 | The actual local directory where assets are stored after download. |
| 105 | */ |
| 106 | |
| 107 | /*! |
| 108 | \qmlsignal AssetDownloader::started() |
| 109 | |
| 110 | Emitted when the download process starts. |
| 111 | */ |
| 112 | |
| 113 | /*! |
| 114 | \qmlsignal AssetDownloader::finished(bool success) |
| 115 | |
| 116 | Emitted when the download process finishes. The \a success parameter indicates |
| 117 | whether the download was successful. |
| 118 | |
| 119 | \sa AssetDownloader::networkErrors, AssetDownloader::sslErrors |
| 120 | */ |
| 121 | |
| 122 | /*! |
| 123 | \qmlsignal AssetDownloader::progressChanged(int progressValue, int progressMaximum, string progressText) |
| 124 | |
| 125 | Emitted to indicate progress during the download. \a progressValue is the current |
| 126 | progress, \a progressMaximum is the total expected progress, and \a progressText |
| 127 | provides a textual description. |
| 128 | */ |
| 129 | |
| 130 | /*! |
| 131 | \qmlmethod void AssetDownloader::start() |
| 132 | |
| 133 | Starts the download process. |
| 134 | */ |
| 135 | |
| 136 | /*! |
| 137 | \qmlmethod stringlist AssetDownloader::networkErrors() |
| 138 | |
| 139 | Returns a list of network-related errors encountered during the download. |
| 140 | */ |
| 141 | |
| 142 | /*! |
| 143 | \qmlmethod stringlist AssetDownloader::sslErrors() |
| 144 | |
| 145 | Returns a list of SSL-related errors encountered during the download. |
| 146 | */ |
| 147 | |
| 148 | AssetDownloaderHelper::AssetDownloaderHelper(QObject *parent) |
| 149 | : AssetDownloader(parent) |
| 150 | {} |
| 151 | |
| 152 | QUrl AssetDownloaderHelper::resolvedUrl(const QUrl &url) const |
| 153 | { |
| 154 | const QQmlContext *context = qmlContext(this); |
| 155 | return context ? context->resolvedUrl(url) : url; |
| 156 | } |
| 157 | |
| 158 | AssetDownloaderHelper *QQmlAssetDownloader::create(QQmlEngine *, QJSEngine *) |
| 159 | { |
| 160 | return new AssetDownloaderHelper(); |
| 161 | } |
| 162 | |
| 163 | QT_END_NAMESPACE |
| 164 | |