25#include <QMimeDatabase>
30#include "moc_qgsnetworkcontentfetcherregistry.cpp"
32using namespace Qt::StringLiterals;
36 QMap<QString, QgsFetchedContent *>::const_iterator it = mFileRegistry.constBegin();
37 for ( ; it != mFileRegistry.constEnd(); ++it )
41 mFileRegistry.clear();
47 if ( mFileRegistry.contains( url ) )
49 return mFileRegistry.value( url );
54 mFileRegistry.insert( url, content );
65 QFile *file =
nullptr;
66 const QString path = filePathOrUrl;
68 if ( !QUrl::fromUserInput( filePathOrUrl ).isLocalFile() )
70 if ( mFileRegistry.contains( path ) )
75 file = content->
file();
89 file =
new QFile( filePathOrUrl );
96 QString path = filePathOrUrl;
98 if ( !QUrl::fromUserInput( filePathOrUrl ).isLocalFile() )
100 auto it = mFileRegistry.constFind( path );
101 if ( it != mFileRegistry.constEnd() )
153 if ( mFetchingTask && mFetchingTask->canCancel() )
155 mFetchingTask->cancel();
161void QgsFetchedContent::taskCompleted()
163 if ( !mFetchingTask || !mFetchingTask->reply() )
167 mError = QNetworkReply::OperationCanceledError;
168 mFilePath = QString();
172 QNetworkReply *reply = mFetchingTask->reply();
173 if ( reply->error() == QNetworkReply::NoError )
180 QString extension = QFileInfo( reply->request().url().fileName() ).completeSuffix();
183 const QString contentType = reply->header( QNetworkRequest::ContentTypeHeader ).toString();
184 if ( extension.isEmpty() && !contentType.isEmpty() )
186 const QList<QMimeType> mimeTypes = QMimeDatabase().allMimeTypes();
187 auto it = std::find_if( mimeTypes.constBegin(), mimeTypes.constEnd(), [contentType]( QMimeType mimeType )
189 return mimeType.name() == contentType;
191 if ( it != mimeTypes.constEnd() )
193 extension = ( *it ).preferredSuffix();
197 mFile = std::make_unique<QTemporaryFile>( extension.isEmpty() ? QString(
"XXXXXX" ) :
198 QString(
"%1/XXXXXX.%2" ).arg( QDir::tempPath(), extension ) );
199 if ( !mFile->open() )
201 QgsDebugError( u
"Can't open temporary file %1"_s.arg( mFile->fileName() ) );
205 mFile->write( reply->readAll() );
207 mFilePath = mFile->fileName();
214 mError = reply->error();
215 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.
Holds information about fetched network content.
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.
QgsFetchedContent * fetch(const QString &url, Qgis::ActionStart fetchingMode=Qgis::ActionStart::Deferred, const QString &authConfig=QString(), const QgsHttpHeaders &headers=QgsHttpHeaders())
Initialize a download for the given URL.
QFile * localFile(const QString &filePathOrUrl)
Returns a QFile from a local file or to a temporary file previously fetched by the registry.
~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.
@ CanCancel
Task can be canceled.
void taskTerminated()
Will be emitted by task if it has terminated for any reason other then completion (e....
#define QgsDebugError(str)