32 if ( file.open( QIODevice::ReadOnly ) )
34 return file.readAll();
38 return missingContent;
43 if ( path.startsWith( QLatin1String(
"base64:" ), Qt::CaseInsensitive ) )
45 const QByteArray base64 = path.mid( 7 ).toLocal8Bit();
46 return QByteArray::fromBase64( base64, QByteArray::OmitTrailingEquals );
52 if ( parseBase64DataUrl( path,
nullptr, &base64String ) )
54 return QByteArray::fromBase64( base64String.toLocal8Bit(), QByteArray::OmitTrailingEquals );
59 if ( !path.contains( QLatin1String(
"://" ) ) )
61 return missingContent;
64 const QUrl url( path );
67 return missingContent;
71 if ( url.scheme().compare( QLatin1String(
"file" ), Qt::CaseInsensitive ) == 0 )
73 file.setFileName( url.toLocalFile() );
76 if ( file.open( QIODevice::ReadOnly ) )
78 return file.readAll();
83 return missingContent;
86 const QMutexLocker locker( &mMutex );
89 if ( mPendingRemoteUrls.contains( path ) )
94 return fetchingContent;
99 for (
QgsTask *task : constActiveTasks )
102 if ( !task->description().endsWith( path ) )
112 if ( waitForTaskFinished( ncfTask ) )
114 if ( mRemoteContentCache.contains( path ) )
117 return *mRemoteContentCache[ path ];
128 if ( mRemoteContentCache.contains( path ) )
131 return *mRemoteContentCache[ path ];
134 mPendingRemoteUrls.insert( path );
136 QNetworkRequest request( url );
138 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
139 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute,
true );
144 const QMutexLocker locker( &mMutex );
146 QNetworkReply *reply = task->
reply();
150 QMetaObject::invokeMethod(
const_cast< QgsAbstractContentCacheBase *
>( qobject_cast< const QgsAbstractContentCacheBase * >(
this ) ),
"onRemoteContentFetched", Qt::QueuedConnection, Q_ARG( QString, path ), Q_ARG(
bool,
false ) );
154 if ( reply->error() != QNetworkReply::NoError )
156 QgsMessageLog::logMessage( tr(
"%3 request failed [error: %1 - url: %2]" ).arg( reply->errorString(), path, mTypeString ), mTypeString );
162 const QVariant status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
165 const QVariant phrase = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute );
166 QgsMessageLog::logMessage( tr(
"%4 request error [status: %1 - reason phrase: %2] for %3" ).arg( status.toInt() ).arg( phrase.toString(), path, mTypeString ), mTypeString );
167 mRemoteContentCache.insert( path,
new QByteArray( missingContent ) );
171 if ( !checkReply( reply, path ) )
173 mRemoteContentCache.insert( path,
new QByteArray( missingContent ) );
180 const QByteArray ba = reply->readAll();
185 mRemoteContentCache.insert( path,
new QByteArray( ba ) );
187 QMetaObject::invokeMethod(
const_cast< QgsAbstractContentCacheBase *
>( qobject_cast< const QgsAbstractContentCacheBase * >(
this ) ),
"onRemoteContentFetched", Qt::QueuedConnection, Q_ARG( QString, path ), Q_ARG(
bool,
true ) );
195 if ( waitForTaskFinished( task ) )
197 if ( mRemoteContentCache.contains( path ) )
200 return *mRemoteContentCache[ path ];
204 return fetchingContent;
A QObject derived base class for QgsAbstractContentCache.
QByteArray getContent(const QString &path, const QByteArray &missingContent, const QByteArray &fetchingContent, bool blocking=false) const
Gets the file content corresponding to the given path.
Handles HTTP network content fetching in a background task.
void fetched()
Emitted when the network content has been fetched, regardless of whether the fetch was successful or ...