Skip to content

Commit

Permalink
also add localFile returning a QFile
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed May 7, 2018
1 parent 8308ce1 commit 9d45077
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
6 changes: 6 additions & 0 deletions python/core/qgsnetworkcontentfetcherregistry.sip.in
Expand Up @@ -130,7 +130,13 @@ Create the registry for temporary downloaded files
If the download starts immediately, it will not redownload any already fetched or currently fetching file.
%End


QString localPath( const QString &filePathOrUrl );
%Docstring
Returns the path to a local file or to a temporary file previously fetched by the registry

:param filePathOrUrl: can either be a local file path or a remote content which has previously been fetched
%End

};

Expand Down
33 changes: 32 additions & 1 deletion src/core/qgsnetworkcontentfetcherregistry.cpp
Expand Up @@ -70,7 +70,7 @@ const QgsFetchedContent *QgsNetworkContentFetcherRegistry::fetch( const QUrl &ur
QObject::connect( content, &QgsFetchedContent::cancelTriggered, this, [ = ]()
{
QMutexLocker locker( &mMutex );
if ( content->mFetchingTask )
if ( content->mFetchingTask->canCancel() )
{
content->mFetchingTask->cancel();
}
Expand Down Expand Up @@ -115,6 +115,37 @@ const QgsFetchedContent *QgsNetworkContentFetcherRegistry::fetch( const QUrl &ur
return content;
}

const QFile *QgsNetworkContentFetcherRegistry::localFile( const QString &filePathOrUrl )
{
QFile *file = nullptr;
QString path = filePathOrUrl;

if ( !QUrl::fromUserInput( filePathOrUrl ).isLocalFile() )
{
if ( mFileRegistry.contains( QUrl( path ) ) )
{
const QgsFetchedContent *content = mFileRegistry.value( QUrl( path ) );
if ( content->status() == QgsFetchedContent::Finished && !content->file() )
{
file = content->file();
}
else
{
// if the file is not downloaded yet or has failed, return nullptr
}
}
else
{
// if registry doesn't contain the URL, return nullptr
}
}
else
{
file = new QFile( filePathOrUrl );
}
return file;
}

QString QgsNetworkContentFetcherRegistry::localPath( const QString &filePathOrUrl )
{
QString path = filePathOrUrl;
Expand Down
13 changes: 13 additions & 0 deletions src/core/qgsnetworkcontentfetcherregistry.h
Expand Up @@ -147,6 +147,19 @@ class CORE_EXPORT QgsNetworkContentFetcherRegistry : public QObject
*/
const QgsFetchedContent *fetch( const QUrl &url, const FetchingMode &fetchingMode = DownloadLater );

#ifndef SIP_RUN

/**
* \brief Returns a QFile from a local file or to a temporary file previously fetched by the registry
* \param filePathOrUrl can either be a local file path or a remote content which has previously been fetched
*/
const QFile *localFile( const QString &filePathOrUrl );
#endif

/**
* \brief Returns the path to a local file or to a temporary file previously fetched by the registry
* \param filePathOrUrl can either be a local file path or a remote content which has previously been fetched
*/
QString localPath( const QString &filePathOrUrl );

private:
Expand Down

0 comments on commit 9d45077

Please sign in to comment.