263 QByteArray
getContent(
const QString &path,
const QByteArray &missingContent,
const QByteArray &fetchingContent,
bool blocking =
false )
const
269 if ( file.open( QIODevice::ReadOnly ) )
271 return file.readAll();
275 return missingContent;
280 if ( path.startsWith( QLatin1String(
"base64:" ), Qt::CaseInsensitive ) )
282 const QByteArray base64 = path.mid( 7 ).toLocal8Bit();
283 return QByteArray::fromBase64( base64, QByteArray::OmitTrailingEquals );
287 if ( !path.contains( QLatin1String(
"://" ) ) )
289 return missingContent;
292 const QUrl url( path );
293 if ( !url.isValid() )
295 return missingContent;
299 if ( url.scheme().compare( QLatin1String(
"file" ), Qt::CaseInsensitive ) == 0 )
301 file.setFileName( url.toLocalFile() );
304 if ( file.open( QIODevice::ReadOnly ) )
306 return file.readAll();
311 return missingContent;
314 const QMutexLocker locker( &mMutex );
317 if ( mPendingRemoteUrls.contains( path ) )
322 return fetchingContent;
327 for (
QgsTask *task : constActiveTasks )
330 if ( !task->description().endsWith( path ) )
340 if ( waitForTaskFinished( ncfTask ) )
342 if ( mRemoteContentCache.contains( path ) )
345 return *mRemoteContentCache[ path ];
356 if ( mRemoteContentCache.contains( path ) )
359 return *mRemoteContentCache[ path ];
362 mPendingRemoteUrls.insert( path );
364 QNetworkRequest request( url );
366 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
367 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute,
true );
372 const QMutexLocker locker( &mMutex );
374 QNetworkReply *reply = task->
reply();
378 QMetaObject::invokeMethod(
const_cast< QgsAbstractContentCacheBase *
>( qobject_cast< const QgsAbstractContentCacheBase * >(
this ) ),
"onRemoteContentFetched", Qt::QueuedConnection, Q_ARG( QString, path ), Q_ARG(
bool,
false ) );
382 if ( reply->error() != QNetworkReply::NoError )
384 QgsMessageLog::logMessage( tr(
"%3 request failed [error: %1 - url: %2]" ).arg( reply->errorString(), path, mTypeString ), mTypeString );
390 const QVariant status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
393 const QVariant phrase = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute );
394 QgsMessageLog::logMessage( tr(
"%4 request error [status: %1 - reason phrase: %2] for %3" ).arg( status.toInt() ).arg( phrase.toString(), path, mTypeString ), mTypeString );
395 mRemoteContentCache.insert( path,
new QByteArray( missingContent ) );
399 if ( !checkReply( reply, path ) )
401 mRemoteContentCache.insert( path,
new QByteArray( missingContent ) );
408 const QByteArray ba = reply->readAll();
413 mRemoteContentCache.insert( path,
new QByteArray( ba ) );
415 QMetaObject::invokeMethod(
const_cast< QgsAbstractContentCacheBase *
>( qobject_cast< const QgsAbstractContentCacheBase * >(
this ) ),
"onRemoteContentFetched", Qt::QueuedConnection, Q_ARG( QString, path ), Q_ARG(
bool,
true ) );
423 if ( waitForTaskFinished( task ) )
425 if ( mRemoteContentCache.contains( path ) )
428 return *mRemoteContentCache[ path ];
432 return fetchingContent;
498 const QString path = entryTemplate->path;
499 T *currentEntry =
nullptr;
500 const QList<T *> entries = mEntryLookup.values( path );
502 for ( T *cacheEntry : entries )
504 if ( cacheEntry->isEqual( entryTemplate ) )
506 if ( mFileModifiedCheckTimeout <= 0 || cacheEntry->fileModifiedLastCheckTimer.hasExpired( mFileModifiedCheckTimeout ) )
508 if ( !modified.isValid() )
509 modified = QFileInfo( path ).lastModified();
511 if ( cacheEntry->fileModified != modified )
514 cacheEntry->fileModifiedLastCheckTimer.restart();
516 currentEntry = cacheEntry;
524 currentEntry = insertCacheEntry( entryTemplate );
528 delete entryTemplate;
529 entryTemplate =
nullptr;
530 ( void )entryTemplate;
531 takeEntryFromList( currentEntry );
532 if ( !mMostRecentEntry )
534 mMostRecentEntry = currentEntry;
535 mLeastRecentEntry = currentEntry;
539 mMostRecentEntry->nextEntry = currentEntry;
540 currentEntry->previousEntry = mMostRecentEntry;
541 currentEntry->nextEntry =
nullptr;
542 mMostRecentEntry = currentEntry;