QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
QGCCachedFileDownload Class Reference

Cached file download with time-based expiration. More...

#include <QGCCachedFileDownload.h>

+ Inheritance diagram for QGCCachedFileDownload:
+ Collaboration diagram for QGCCachedFileDownload:

Public Slots

bool download (const QString &url, int maxCacheAgeSec)
 
bool downloadPreferCache (const QString &url)
 
bool downloadNoCache (const QString &url)
 
void cancel ()
 Cancel current download.
 
void clearCache ()
 Clear all cached files.
 
bool removeFromCache (const QString &url)
 

Signals

void progressChanged (qreal progress)
 Emitted when download progress changes.
 
void runningChanged (bool running)
 Emitted when running state changes.
 
void errorStringChanged (const QString &errorString)
 Emitted when error string changes.
 
void urlChanged (const QUrl &url)
 Emitted when URL changes.
 
void localPathChanged (const QString &localPath)
 Emitted when local path changes.
 
void fromCacheChanged (bool fromCache)
 Emitted when fromCache state changes.
 
void cacheDirectoryChanged (const QString &directory)
 Emitted when cache directory changes.
 
void maxCacheSizeChanged (qint64 bytes)
 Emitted when max cache size changes.
 
void cacheSizeChanged (qint64 bytes)
 Emitted when cache size changes.
 
void finished (bool success, const QString &localPath, const QString &errorMessage, bool fromCache)
 
void downloadProgress (qint64 bytesReceived, qint64 totalBytes)
 Emitted during download with byte counts.
 

Public Member Functions

 QGCCachedFileDownload (const QString &cacheDirectory, QObject *parent=nullptr)
 Current download progress (0.0 to 1.0)
 
 QGCCachedFileDownload (QObject *parent=nullptr)
 Construct without cache directory (must call setCacheDirectory before use)
 
 ~QGCCachedFileDownload () override
 
qreal progress () const
 
bool isRunning () const
 
QString errorString () const
 
QUrl url () const
 
QString localPath () const
 
bool isFromCache () const
 
QString cacheDirectory () const
 
qint64 maxCacheSize () const
 
qint64 cacheSize () const
 
void setCacheDirectory (const QString &directory)
 
void setMaxCacheSize (qint64 bytes)
 
Q_INVOKABLE bool isCached (const QString &url, int maxAgeSec=0) const
 
Q_INVOKABLE QString cachedPath (const QString &url) const
 
Q_INVOKABLE int cacheAge (const QString &url) const
 
QGCFileDownloadfileDownloader () const
 Get the underlying file downloader (for advanced configuration)
 
QNetworkDiskCache * diskCache () const
 Get the disk cache (for advanced configuration)
 

Detailed Description

Cached file download with time-based expiration.

Features:

  • Downloads files with Qt's network disk cache
  • Time-based cache expiration (configurable per-request)
  • Automatic fallback to cached version on network failure
  • Progress reporting
  • QML-compatible properties

Cache behavior:

  1. If cached file exists and is not expired → use cache immediately
  2. If cached file exists but is expired → try network, fallback to cache on failure
  3. If no cached file → download from network

Example C++ usage:

auto *downloader = new QGCCachedFileDownload("/path/to/cache", this);
connect(downloader, &QGCCachedFileDownload::finished, this,
[](bool success, const QString &path, const QString &error, bool fromCache) {
qDebug() << "Downloaded:" << path << "from cache:" << fromCache;
});
downloader->download("https://example.com/data.json", 3600); // 1 hour cache
Error error
Cached file download with time-based expiration.
void finished(bool success, const QString &localPath, const QString &errorMessage, bool fromCache)

Example QML usage:

id: cachedDownloader
cacheDirectory: StandardPaths.writableLocation(StandardPaths.CacheLocation)
onFinished: (success, path, error, fromCache) => {
console.log("Downloaded:", path, "cached:", fromCache)
}
}
Button {
text: cachedDownloader.running ? "Downloading..." : "Download"
onClicked: cachedDownloader.download(urlField.text, 3600)
}
bool download(const QString &url, int maxCacheAgeSec)

Definition at line 53 of file QGCCachedFileDownload.h.

Constructor & Destructor Documentation

◆ QGCCachedFileDownload() [1/2]

QGCCachedFileDownload::QGCCachedFileDownload ( const QString &  cacheDirectory,
QObject *  parent = nullptr 
)
explicit

Current download progress (0.0 to 1.0)

Whether a download is currently in progress Error message from last failed download URL being downloaded Local file path (cached or downloaded) Whether the last result came from cache Cache directory path Maximum cache size in bytes (0 = unlimited) Current cache size in bytes Construct with cache directory

Parameters
cacheDirectoryDirectory to store cached files
parentParent object

Definition at line 18 of file QGCCachedFileDownload.cc.

References QGCFileDownload::downloadProgress(), and QGCFileDownload::finished().

◆ QGCCachedFileDownload() [2/2]

QGCCachedFileDownload::QGCCachedFileDownload ( QObject *  parent = nullptr)
explicit

Construct without cache directory (must call setCacheDirectory before use)

Definition at line 33 of file QGCCachedFileDownload.cc.

References QGCFileDownload::downloadProgress(), and QGCFileDownload::finished().

◆ ~QGCCachedFileDownload()

QGCCachedFileDownload::~QGCCachedFileDownload ( )
override

Definition at line 46 of file QGCCachedFileDownload.cc.

Member Function Documentation

◆ cacheAge()

int QGCCachedFileDownload::cacheAge ( const QString &  url) const

Get cache entry age in seconds (-1 if not cached)

Parameters
urlURL to look up
Returns
Age in seconds, or -1 if not cached

Definition at line 133 of file QGCCachedFileDownload.cc.

References url().

◆ cacheDirectory()

QString QGCCachedFileDownload::cacheDirectory ( ) const

Definition at line 55 of file QGCCachedFileDownload.cc.

Referenced by download(), downloadPreferCache(), and setCacheDirectory().

◆ cacheDirectoryChanged

void QGCCachedFileDownload::cacheDirectoryChanged ( const QString &  directory)
signal

Emitted when cache directory changes.

Referenced by setCacheDirectory().

◆ cachedPath()

QString QGCCachedFileDownload::cachedPath ( const QString &  url) const

Get the cached file path for a URL (empty if not cached)

Parameters
urlURL to look up
Returns
Path to a temporary file copy of cached data, or empty string. Caller owns the temporary file and should remove it when no longer needed.

Definition at line 109 of file QGCCachedFileDownload.cc.

References QGCFileHelper::atomicWrite(), QGCFileHelper::uniqueTempPath(), and url().

◆ cacheSize()

qint64 QGCCachedFileDownload::cacheSize ( ) const

Definition at line 81 of file QGCCachedFileDownload.cc.

Referenced by removeFromCache().

◆ cacheSizeChanged

void QGCCachedFileDownload::cacheSizeChanged ( qint64  bytes)
signal

Emitted when cache size changes.

Referenced by clearCache(), and removeFromCache().

◆ cancel

void QGCCachedFileDownload::cancel ( )
slot

Cancel current download.

Definition at line 260 of file QGCCachedFileDownload.cc.

References QGCFileDownload::cancel().

◆ clearCache

void QGCCachedFileDownload::clearCache ( )
slot

Clear all cached files.

Definition at line 283 of file QGCCachedFileDownload.cc.

References cacheSizeChanged().

◆ diskCache()

QNetworkDiskCache * QGCCachedFileDownload::diskCache ( ) const
inline

Get the disk cache (for advanced configuration)

Definition at line 134 of file QGCCachedFileDownload.h.

◆ download

bool QGCCachedFileDownload::download ( const QString &  url,
int  maxCacheAgeSec 
)
slot

Download a file with cache support

Parameters
urlURL to download
maxCacheAgeSecMaximum cache age in seconds (0 = any age is valid)
Returns
true if download started successfully

Definition at line 147 of file QGCCachedFileDownload.cc.

References cacheDirectory(), url(), and urlChanged().

Referenced by ComponentInformationTranslation::downloadAndTranslate().

◆ downloadNoCache

bool QGCCachedFileDownload::downloadNoCache ( const QString &  url)
slot

Download a file, bypassing cache entirely

Parameters
urlURL to download
Returns
true if download started successfully

Definition at line 235 of file QGCCachedFileDownload.cc.

References url(), and urlChanged().

◆ downloadPreferCache

bool QGCCachedFileDownload::downloadPreferCache ( const QString &  url)
slot

Download a file, always using cache if available (no expiration check)

Parameters
urlURL to download
Returns
true if download started successfully

Definition at line 204 of file QGCCachedFileDownload.cc.

References cacheDirectory(), url(), and urlChanged().

◆ downloadProgress

void QGCCachedFileDownload::downloadProgress ( qint64  bytesReceived,
qint64  totalBytes 
)
signal

Emitted during download with byte counts.

◆ errorString()

QString QGCCachedFileDownload::errorString ( ) const
inline

Definition at line 101 of file QGCCachedFileDownload.h.

◆ errorStringChanged

void QGCCachedFileDownload::errorStringChanged ( const QString &  errorString)
signal

Emitted when error string changes.

◆ fileDownloader()

QGCFileDownload * QGCCachedFileDownload::fileDownloader ( ) const
inline

Get the underlying file downloader (for advanced configuration)

Definition at line 131 of file QGCCachedFileDownload.h.

◆ finished

void QGCCachedFileDownload::finished ( bool  success,
const QString &  localPath,
const QString &  errorMessage,
bool  fromCache 
)
signal

Emitted when download completes

Parameters
successtrue if download succeeded
localPathPath to downloaded/cached file
errorMessageError message (empty on success)
fromCachetrue if result came from cache

Referenced by ComponentInformationTranslation::downloadAndTranslate().

◆ fromCacheChanged

void QGCCachedFileDownload::fromCacheChanged ( bool  fromCache)
signal

Emitted when fromCache state changes.

◆ isCached()

bool QGCCachedFileDownload::isCached ( const QString &  url,
int  maxAgeSec = 0 
) const

Check if a URL is cached and not expired

Parameters
urlURL to check
maxAgeSecMaximum age in seconds (0 = any age is valid)
Returns
true if valid cached version exists

Definition at line 90 of file QGCCachedFileDownload.cc.

References url().

◆ isFromCache()

bool QGCCachedFileDownload::isFromCache ( ) const
inline

Definition at line 104 of file QGCCachedFileDownload.h.

◆ isRunning()

bool QGCCachedFileDownload::isRunning ( ) const
inline

Definition at line 100 of file QGCCachedFileDownload.h.

◆ localPath()

QString QGCCachedFileDownload::localPath ( ) const
inline

Definition at line 103 of file QGCCachedFileDownload.h.

◆ localPathChanged

void QGCCachedFileDownload::localPathChanged ( const QString &  localPath)
signal

Emitted when local path changes.

◆ maxCacheSize()

qint64 QGCCachedFileDownload::maxCacheSize ( ) const

Definition at line 68 of file QGCCachedFileDownload.cc.

Referenced by setMaxCacheSize().

◆ maxCacheSizeChanged

void QGCCachedFileDownload::maxCacheSizeChanged ( qint64  bytes)
signal

Emitted when max cache size changes.

Referenced by setMaxCacheSize().

◆ progress()

qreal QGCCachedFileDownload::progress ( ) const
inline

Definition at line 99 of file QGCCachedFileDownload.h.

◆ progressChanged

void QGCCachedFileDownload::progressChanged ( qreal  progress)
signal

Emitted when download progress changes.

◆ removeFromCache

bool QGCCachedFileDownload::removeFromCache ( const QString &  url)
slot

Remove a specific URL from cache

Parameters
urlURL to remove
Returns
true if removed successfully

Definition at line 290 of file QGCCachedFileDownload.cc.

References cacheSize(), cacheSizeChanged(), and url().

◆ runningChanged

void QGCCachedFileDownload::runningChanged ( bool  running)
signal

Emitted when running state changes.

◆ setCacheDirectory()

void QGCCachedFileDownload::setCacheDirectory ( const QString &  directory)

Definition at line 60 of file QGCCachedFileDownload.cc.

References cacheDirectory(), and cacheDirectoryChanged().

◆ setMaxCacheSize()

void QGCCachedFileDownload::setMaxCacheSize ( qint64  bytes)

Definition at line 73 of file QGCCachedFileDownload.cc.

References maxCacheSize(), and maxCacheSizeChanged().

◆ url()

QUrl QGCCachedFileDownload::url ( ) const
inline

◆ urlChanged

void QGCCachedFileDownload::urlChanged ( const QUrl &  url)
signal

Emitted when URL changes.

Referenced by download(), downloadNoCache(), and downloadPreferCache().


The documentation for this class was generated from the following files: