26#include <QMimeDatabase>
30 QMap<QString, QgsFetchedContent *>::const_iterator it = mFileRegistry.constBegin();
31 for ( ; it != mFileRegistry.constEnd(); ++it )
35 mFileRegistry.clear();
41 if ( mFileRegistry.contains( url ) )
43 return mFileRegistry.value( url );
48 mFileRegistry.insert( url, content );
59 QFile *file =
nullptr;
60 const QString path = filePathOrUrl;
62 if ( !QUrl::fromUserInput( filePathOrUrl ).isLocalFile() )
64 if ( mFileRegistry.contains( path ) )
69 file = content->
file();
83 file =
new QFile( filePathOrUrl );
90 QString path = filePathOrUrl;
92 if ( !QUrl::fromUserInput( filePathOrUrl ).isLocalFile() )
94 if ( mFileRegistry.contains( path ) )
146 if ( mFetchingTask && mFetchingTask->canCancel() )
148 mFetchingTask->cancel();
154void QgsFetchedContent::taskCompleted()
156 if ( !mFetchingTask || !mFetchingTask->reply() )
160 mError = QNetworkReply::OperationCanceledError;
161 mFilePath = QString();
165 QNetworkReply *reply = mFetchingTask->reply();
166 if ( reply->error() == QNetworkReply::NoError )
173 QString extension = QFileInfo( reply->request().url().fileName() ).completeSuffix();
176 const QString contentType = reply->header( QNetworkRequest::ContentTypeHeader ).toString();
177 if ( extension.isEmpty() && !contentType.isEmpty() )
179 const QList<QMimeType> mimeTypes = QMimeDatabase().allMimeTypes();
180 auto it = std::find_if( mimeTypes.constBegin(), mimeTypes.constEnd(), [ = ]( QMimeType mimeType )
182 return mimeType.name() == contentType;
184 if ( it != mimeTypes.constEnd() )
186 extension = ( *it ).preferredSuffix();
190 QTemporaryFile *tf =
new QTemporaryFile( extension.isEmpty() ? QString(
"XXXXXX" ) :
191 QString(
"%1/XXXXXX.%2" ).arg( QDir::tempPath(), extension ) );
194 mFile->write( reply->readAll() );
196 mFilePath = tf->fileName();
203 mError = reply->error();
204 mFilePath = QString();
ActionStart
Enum to determine when an operation would begin.
@ Immediate
Action will start immediately.
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.
QFile * file() const
Returns a pointer to the local file, or nullptr if the file is not accessible yet.
ContentStatus status() const
Returns the status of the download.
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.
void errorOccurred(QNetworkReply::NetworkError code, const QString &errorMsg)
Emitted when an error with code error occurred while processing the request errorMsg is a textual des...
@ 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.
QgsFetchedContent * fetch(const QString &url, Qgis::ActionStart fetchingMode=Qgis::ActionStart::Deferred, const QString &authConfig=QString())
Initialize a download for the given URL.
~QgsNetworkContentFetcherRegistry() override
Handles HTTP network content fetching in a background task.
void errorOccurred(QNetworkReply::NetworkError code, const QString &errorMsg)
Emitted when an error with code error occurred while processing the request errorMsg is a textual des...
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.
void taskTerminated()
Will be emitted by task if it has terminated for any reason other then completion (e....