20#include "moc_qgsnetworkcontentfetcherregistry.cpp" 
   27#include <QMimeDatabase> 
   31  QMap<QString, QgsFetchedContent *>::const_iterator it = mFileRegistry.constBegin();
 
   32  for ( ; it != mFileRegistry.constEnd(); ++it )
 
   36  mFileRegistry.clear();
 
 
   42  if ( mFileRegistry.contains( url ) )
 
   44    return mFileRegistry.value( url );
 
   49  mFileRegistry.insert( url, content );
 
 
   60  QFile *file = 
nullptr;
 
   61  const QString path = filePathOrUrl;
 
   63  if ( !QUrl::fromUserInput( filePathOrUrl ).isLocalFile() )
 
   65    if ( mFileRegistry.contains( path ) )
 
   70        file = content->
file();
 
   84    file = 
new QFile( filePathOrUrl );
 
 
   91  QString path = filePathOrUrl;
 
   93  if ( !QUrl::fromUserInput( filePathOrUrl ).isLocalFile() )
 
   95    if ( mFileRegistry.contains( path ) )
 
 
  147  if ( mFetchingTask && mFetchingTask->canCancel() )
 
  149    mFetchingTask->cancel();
 
 
  155void 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 )
 
  174      QString extension = QFileInfo( reply->request().url().fileName() ).completeSuffix();
 
  177      const QString contentType = reply->header( QNetworkRequest::ContentTypeHeader ).toString();
 
  178      if ( extension.isEmpty() && !contentType.isEmpty() )
 
  180        const QList<QMimeType> mimeTypes = QMimeDatabase().allMimeTypes();
 
  181        auto it = std::find_if( mimeTypes.constBegin(), mimeTypes.constEnd(), [contentType]( QMimeType mimeType )
 
  183          return mimeType.name() == contentType;
 
  185        if ( it != mimeTypes.constEnd() )
 
  187          extension = ( *it ).preferredSuffix();
 
  191      mFile = std::make_unique<QTemporaryFile>( extension.isEmpty() ? QString( 
"XXXXXX" ) :
 
  192              QString( 
"%1/XXXXXX.%2" ).arg( QDir::tempPath(), extension ) );
 
  194      mFile->write( reply->readAll() );
 
  196      mFilePath = mFile->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.
 
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.
 
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....