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 );
50 if ( fetchingMode == Qgis::ActionStart::Immediate )
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() )
152 mFile->deleteLater();
153 mFilePath = QString();
158void QgsFetchedContent::taskCompleted()
160 if ( !mFetchingTask || !mFetchingTask->
reply() )
164 mError = QNetworkReply::OperationCanceledError;
165 mFilePath = QString();
169 QNetworkReply *reply = mFetchingTask->
reply();
170 if ( reply->error() == QNetworkReply::NoError )
177 QString extension = QFileInfo( reply->request().url().fileName() ).completeSuffix();
180 const QString contentType = reply->header( QNetworkRequest::ContentTypeHeader ).toString();
181 if ( extension.isEmpty() && !contentType.isEmpty() )
183 const QList<QMimeType> mimeTypes = QMimeDatabase().allMimeTypes();
184 auto it = std::find_if( mimeTypes.constBegin(), mimeTypes.constEnd(), [ = ]( QMimeType mimeType )
186 return mimeType.name() == contentType;
188 if ( it != mimeTypes.constEnd() )
190 extension = ( *it ).preferredSuffix();
194 QTemporaryFile *tf =
new QTemporaryFile( extension.isEmpty() ? QString(
"XXXXXX" ) :
195 QString(
"%1/XXXXXX.%2" ).arg( QDir::tempPath(), extension ) );
198 mFile->write( reply->readAll() );
200 mFilePath = tf->fileName();
207 mError = reply->error();
208 mFilePath = QString();
ActionStart
Enum to determine when an operation would begin.
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.
QNetworkReply * reply()
Returns the network reply.
void cancel() override
Notifies the task that it should terminate.
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....
bool canCancel() const
Returns true if the task can be canceled.