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 );
57 QString embeddedString;
58 if ( parseEmbeddedStringData( path,
nullptr, &embeddedString ) )
60 return embeddedString.toUtf8();
65 if ( !path.contains( QLatin1String(
"://" ) ) )
67 return missingContent;
70 const QUrl url( path );
73 return missingContent;
77 if ( url.scheme().compare( QLatin1String(
"file" ), Qt::CaseInsensitive ) == 0 )
79 file.setFileName( url.toLocalFile() );
82 if ( file.open( QIODevice::ReadOnly ) )
84 return file.readAll();
89 return missingContent;
92 const QMutexLocker locker( &mMutex );
95 if ( mPendingRemoteUrls.contains( path ) )
100 return fetchingContent;
105 for (
QgsTask *task : constActiveTasks )
108 if ( !task->description().endsWith( path ) )
118 if ( waitForTaskFinished( ncfTask ) )
120 if ( mRemoteContentCache.contains( path ) )
123 return *mRemoteContentCache[ path ];
134 if ( mRemoteContentCache.contains( path ) )
137 return *mRemoteContentCache[ path ];
140 mPendingRemoteUrls.insert( path );
142 QNetworkRequest request( url );
144 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
145 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute,
true );
150 const QMutexLocker locker( &mMutex );
152 QNetworkReply *reply = task->
reply();
156 QMetaObject::invokeMethod(
const_cast< QgsAbstractContentCacheBase *
>( qobject_cast< const QgsAbstractContentCacheBase * >(
this ) ),
"onRemoteContentFetched", Qt::QueuedConnection, Q_ARG( QString, path ), Q_ARG(
bool,
false ) );
160 if ( reply->error() != QNetworkReply::NoError )
162 QgsMessageLog::logMessage( tr(
"%3 request failed [error: %1 - url: %2]" ).arg( reply->errorString(), path, mTypeString ), mTypeString );
168 const QVariant status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
171 const QVariant phrase = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute );
172 QgsMessageLog::logMessage( tr(
"%4 request error [status: %1 - reason phrase: %2] for %3" ).arg( status.toInt() ).arg( phrase.toString(), path, mTypeString ), mTypeString );
173 mRemoteContentCache.insert( path,
new QByteArray( missingContent ) );
177 if ( !checkReply( reply, path ) )
179 mRemoteContentCache.insert( path,
new QByteArray( missingContent ) );
186 const QByteArray ba = reply->readAll();
191 mRemoteContentCache.insert( path,
new QByteArray( ba ) );
193 QMetaObject::invokeMethod(
const_cast< QgsAbstractContentCacheBase *
>( qobject_cast< const QgsAbstractContentCacheBase * >(
this ) ),
"onRemoteContentFetched", Qt::QueuedConnection, Q_ARG( QString, path ), Q_ARG(
bool,
true ) );
201 if ( waitForTaskFinished( task ) )
203 if ( mRemoteContentCache.contains( path ) )
206 return *mRemoteContentCache[ path ];
210 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.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
Adds a message to the log instance (and creates it if necessary).
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 ...