29 QMap<QString, QgsFetchedContent *>::const_iterator it = mFileRegistry.constBegin();
30 for ( ; it != mFileRegistry.constEnd(); ++it )
34 mFileRegistry.clear();
40 if ( mFileRegistry.contains( url ) )
42 return mFileRegistry.value( url );
47 mFileRegistry.insert( url, content );
58 QFile *file =
nullptr;
59 QString path = filePathOrUrl;
61 if ( !QUrl::fromUserInput( filePathOrUrl ).isLocalFile() )
63 if ( mFileRegistry.contains( path ) )
68 file = content->
file();
82 file =
new QFile( filePathOrUrl );
89 QString path = filePathOrUrl;
91 if ( !QUrl::fromUserInput( filePathOrUrl ).isLocalFile() )
93 if ( mFileRegistry.contains( path ) )
143 if ( mFetchingTask && mFetchingTask->canCancel() )
145 mFetchingTask->cancel();
149 mFile->deleteLater();
150 mFilePath = QString();
155 void QgsFetchedContent::taskCompleted()
157 if ( !mFetchingTask || !mFetchingTask->reply() )
161 mError = QNetworkReply::OperationCanceledError;
162 mFilePath = QString();
166 QNetworkReply *reply = mFetchingTask->reply();
167 if ( reply->error() == QNetworkReply::NoError )
169 QTemporaryFile *tf =
new QTemporaryFile( QStringLiteral(
"XXXXXX" ) );
172 mFile->write( reply->readAll() );
174 mFilePath = tf->fileName();
181 mError = reply->error();
182 mFilePath = QString();
void taskCompleted()
Will be emitted by task to indicate its successful completion.
static QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
QString localPath(const QString &filePathOrUrl)
Returns the path to a local file or to a temporary file previously fetched by the registry...
The download will start immediately, not need to run QgsFecthedContent::download() ...
Handles HTTP network content fetching in a background task.
QFile * file() const
Returns a pointer to the local file, a null pointer if the file is not accessible yet...
static QgsTaskManager * taskManager()
Returns the application's task manager, used for managing application wide background task handling...
QgsNetworkContentFetcherRegistry()
Create the registry for temporary downloaded files.
FetchedContent holds useful information about a network content being fetched.
long addTask(QgsTask *task, int priority=0)
Adds a task to the manager.
Download finished and successful.
No download started for such URL.
QFile * localFile(const QString &filePathOrUrl)
Returns a QFile from a local file or to a temporary file previously fetched by the registry...
FetchingMode
Enum to determine when the download should start.
ContentStatus status() const
Returns the status of the download.
void cancel()
Cancel the download operation.
const QString filePath() const
Returns the path to the local file, an empty string if the file is not accessible yet...
const QgsFetchedContent * fetch(const QString &url, FetchingMode fetchingMode=DownloadLater)
Initialize a download for the given URL.
~QgsNetworkContentFetcherRegistry() override
void download(bool redownload=false)
Start the download.