28 QMap<QString, QgsFetchedContent *>::const_iterator it = mFileRegistry.constBegin();
29 for ( ; it != mFileRegistry.constEnd(); ++it )
33 mFileRegistry.clear();
39 if ( mFileRegistry.contains( url ) )
41 return mFileRegistry.value( url );
46 mFileRegistry.insert( url, content );
48 if ( fetchingMode == Qgis::ActionStart::Immediate )
57 QFile *file =
nullptr;
58 const QString path = filePathOrUrl;
60 if ( !QUrl::fromUserInput( filePathOrUrl ).isLocalFile() )
62 if ( mFileRegistry.contains( path ) )
67 file = content->
file();
81 file =
new QFile( filePathOrUrl );
88 QString path = filePathOrUrl;
90 if ( !QUrl::fromUserInput( filePathOrUrl ).isLocalFile() )
92 if ( mFileRegistry.contains( path ) )
144 if ( mFetchingTask && mFetchingTask->
canCancel() )
150 mFile->deleteLater();
151 mFilePath = QString();
156void QgsFetchedContent::taskCompleted()
158 if ( !mFetchingTask || !mFetchingTask->
reply() )
162 mError = QNetworkReply::OperationCanceledError;
163 mFilePath = QString();
167 QNetworkReply *reply = mFetchingTask->
reply();
168 if ( reply->error() == QNetworkReply::NoError )
172 const QString extension = QFileInfo( reply->request().url().fileName() ).completeSuffix();
174 QTemporaryFile *tf =
new QTemporaryFile( extension.isEmpty() ? QString(
"XXXXXX" ) :
175 QString(
"%1/XXXXXX.%2" ).arg( QDir::tempPath(), extension ) );
178 mFile->write( reply->readAll() );
180 mFilePath = tf->fileName();
187 mError = reply->error();
188 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.