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

#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.
 

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
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)
}

Definition at line 55 of file QGCCachedFileDownload.h.

Member Function Documentation

◆ cacheDirectoryChanged

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

Emitted when cache directory changes.

◆ 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().

◆ 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 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 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 urlChanged().

◆ downloadProgress

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

Emitted during download with byte counts.

◆ errorStringChanged

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

Emitted when error string changes.

◆ 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.

◆ localPathChanged

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

Emitted when local path changes.

◆ maxCacheSizeChanged

void QGCCachedFileDownload::maxCacheSizeChanged ( qint64  bytes)
signal

Emitted when max cache size changes.

◆ 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 cacheSizeChanged().

◆ runningChanged

void QGCCachedFileDownload::runningChanged ( bool  running)
signal

Emitted when running state changes.

◆ 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: