Skip to content

Commit 9d45077

Browse files
committed
also add localFile returning a QFile
1 parent 8308ce1 commit 9d45077

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

python/core/qgsnetworkcontentfetcherregistry.sip.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,13 @@ Create the registry for temporary downloaded files
130130
If the download starts immediately, it will not redownload any already fetched or currently fetching file.
131131
%End
132132

133+
133134
QString localPath( const QString &filePathOrUrl );
135+
%Docstring
136+
Returns the path to a local file or to a temporary file previously fetched by the registry
137+
138+
:param filePathOrUrl: can either be a local file path or a remote content which has previously been fetched
139+
%End
134140

135141
};
136142

src/core/qgsnetworkcontentfetcherregistry.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const QgsFetchedContent *QgsNetworkContentFetcherRegistry::fetch( const QUrl &ur
7070
QObject::connect( content, &QgsFetchedContent::cancelTriggered, this, [ = ]()
7171
{
7272
QMutexLocker locker( &mMutex );
73-
if ( content->mFetchingTask )
73+
if ( content->mFetchingTask->canCancel() )
7474
{
7575
content->mFetchingTask->cancel();
7676
}
@@ -115,6 +115,37 @@ const QgsFetchedContent *QgsNetworkContentFetcherRegistry::fetch( const QUrl &ur
115115
return content;
116116
}
117117

118+
const QFile *QgsNetworkContentFetcherRegistry::localFile( const QString &filePathOrUrl )
119+
{
120+
QFile *file = nullptr;
121+
QString path = filePathOrUrl;
122+
123+
if ( !QUrl::fromUserInput( filePathOrUrl ).isLocalFile() )
124+
{
125+
if ( mFileRegistry.contains( QUrl( path ) ) )
126+
{
127+
const QgsFetchedContent *content = mFileRegistry.value( QUrl( path ) );
128+
if ( content->status() == QgsFetchedContent::Finished && !content->file() )
129+
{
130+
file = content->file();
131+
}
132+
else
133+
{
134+
// if the file is not downloaded yet or has failed, return nullptr
135+
}
136+
}
137+
else
138+
{
139+
// if registry doesn't contain the URL, return nullptr
140+
}
141+
}
142+
else
143+
{
144+
file = new QFile( filePathOrUrl );
145+
}
146+
return file;
147+
}
148+
118149
QString QgsNetworkContentFetcherRegistry::localPath( const QString &filePathOrUrl )
119150
{
120151
QString path = filePathOrUrl;

src/core/qgsnetworkcontentfetcherregistry.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,19 @@ class CORE_EXPORT QgsNetworkContentFetcherRegistry : public QObject
147147
*/
148148
const QgsFetchedContent *fetch( const QUrl &url, const FetchingMode &fetchingMode = DownloadLater );
149149

150+
#ifndef SIP_RUN
151+
152+
/**
153+
* \brief Returns a QFile from a local file or to a temporary file previously fetched by the registry
154+
* \param filePathOrUrl can either be a local file path or a remote content which has previously been fetched
155+
*/
156+
const QFile *localFile( const QString &filePathOrUrl );
157+
#endif
158+
159+
/**
160+
* \brief Returns the path to a local file or to a temporary file previously fetched by the registry
161+
* \param filePathOrUrl can either be a local file path or a remote content which has previously been fetched
162+
*/
150163
QString localPath( const QString &filePathOrUrl );
151164

152165
private:

0 commit comments

Comments
 (0)