26 QMap<QString, QgsFetchedContent *>::const_iterator it = mFileRegistry.constBegin();
27 for ( ; it != mFileRegistry.constEnd(); ++it )
31 mFileRegistry.clear();
37 if ( mFileRegistry.contains( url ) )
39 return mFileRegistry.value( url );
44 mFileRegistry.insert( url, content );
55 QFile *file =
nullptr;
56 QString path = filePathOrUrl;
58 if ( !QUrl::fromUserInput( filePathOrUrl ).isLocalFile() )
60 if ( mFileRegistry.contains( path ) )
65 file = content->
file();
79 file =
new QFile( filePathOrUrl );
86 QString path = filePathOrUrl;
88 if ( !QUrl::fromUserInput( filePathOrUrl ).isLocalFile() )
90 if ( mFileRegistry.contains( path ) )
140 if ( mFetchingTask && mFetchingTask->
canCancel() )
146 mFile->deleteLater();
147 mFilePath = QString();
152 void QgsFetchedContent::taskCompleted()
154 if ( !mFetchingTask || !mFetchingTask->
reply() )
158 mError = QNetworkReply::OperationCanceledError;
159 mFilePath = QString();
163 QNetworkReply *reply = mFetchingTask->
reply();
164 if ( reply->error() == QNetworkReply::NoError )
166 QTemporaryFile *tf =
new QTemporaryFile( QStringLiteral(
"XXXXXX" ) );
169 mFile->write( reply->readAll() );
171 mFilePath = tf->fileName();
178 mError = reply->error();
179 mFilePath = QString();
static QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
static QgsTaskManager * taskManager()
Returns the application's task manager, used for managing application wide background task handling.
FetchedContent holds useful information about a network content being fetched.
ContentStatus status() const
Returns the status of the download.
QFile * file() const
Returns a pointer to the local file, or nullptr if the file is not accessible yet.
void download(bool redownload=false)
Start the download.
const QString filePath() const
Returns the path to the local file, an empty string if the file is not accessible yet.
@ Finished
Download finished and successful.
@ NotStarted
No download started for such URL.
@ Downloading
Currently downloading.
void fetched()
Emitted when the file is fetched and accessible.
void cancel()
Cancel the download operation.
QString localPath(const QString &filePathOrUrl)
Returns the path to a local file or to a temporary file previously fetched by the registry.
QFile * localFile(const QString &filePathOrUrl)
Returns a QFile from a local file or to a temporary file previously fetched by the registry.
~QgsNetworkContentFetcherRegistry() override
const QgsFetchedContent * fetch(const QString &url, FetchingMode fetchingMode=DownloadLater)
Initialize a download for the given URL.
FetchingMode
Enum to determine when the download should start.
@ DownloadImmediately
The download will start immediately, not need to run QgsFecthedContent::download()
Handles HTTP network content fetching in a background task.
QNetworkReply * reply()
Returns the network reply.
void cancel() override
Notifies the task that it should terminate.
long addTask(QgsTask *task, int priority=0)
Adds a task to the manager.
void taskCompleted()
Will be emitted by task to indicate its successful completion.
bool canCancel() const
Returns true if the task can be canceled.