18#ifndef QGSABSTRACTCONTENTCACHE_P_H
19#define QGSABSTRACTCONTENTCACHE_P_H
31 if ( file.open( QIODevice::ReadOnly ) )
33 return file.readAll();
37 return missingContent;
42 if ( path.startsWith( QLatin1String(
"base64:" ), Qt::CaseInsensitive ) )
44 const QByteArray base64 = path.mid( 7 ).toLocal8Bit();
45 return QByteArray::fromBase64( base64, QByteArray::OmitTrailingEquals );
49 if ( !path.contains( QLatin1String(
"://" ) ) )
51 return missingContent;
54 const QUrl url( path );
57 return missingContent;
61 if ( url.scheme().compare( QLatin1String(
"file" ), Qt::CaseInsensitive ) == 0 )
63 file.setFileName( url.toLocalFile() );
66 if ( file.open( QIODevice::ReadOnly ) )
68 return file.readAll();
73 return missingContent;
76 const QMutexLocker locker( &mMutex );
79 if ( mPendingRemoteUrls.contains( path ) )
84 return fetchingContent;
89 for (
QgsTask *task : constActiveTasks )
92 if ( !task->description().endsWith( path ) )
102 if ( waitForTaskFinished( ncfTask ) )
104 if ( mRemoteContentCache.contains( path ) )
107 return *mRemoteContentCache[ path ];
118 if ( mRemoteContentCache.contains( path ) )
121 return *mRemoteContentCache[ path ];
124 mPendingRemoteUrls.insert( path );
126 QNetworkRequest request( url );
128 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
129 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute,
true );
134 const QMutexLocker locker( &mMutex );
136 QNetworkReply *reply = task->
reply();
140 QMetaObject::invokeMethod(
const_cast< QgsAbstractContentCacheBase *
>( qobject_cast< const QgsAbstractContentCacheBase * >(
this ) ),
"onRemoteContentFetched", Qt::QueuedConnection, Q_ARG( QString, path ), Q_ARG(
bool,
false ) );
144 if ( reply->error() != QNetworkReply::NoError )
146 QgsMessageLog::logMessage( tr(
"%3 request failed [error: %1 - url: %2]" ).arg( reply->errorString(), path, mTypeString ), mTypeString );
152 const QVariant status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
155 const QVariant phrase = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute );
156 QgsMessageLog::logMessage( tr(
"%4 request error [status: %1 - reason phrase: %2] for %3" ).arg( status.toInt() ).arg( phrase.toString(), path, mTypeString ), mTypeString );
157 mRemoteContentCache.insert( path,
new QByteArray( missingContent ) );
161 if ( !checkReply( reply, path ) )
163 mRemoteContentCache.insert( path,
new QByteArray( missingContent ) );
170 const QByteArray ba = reply->readAll();
175 mRemoteContentCache.insert( path,
new QByteArray( ba ) );
177 QMetaObject::invokeMethod(
const_cast< QgsAbstractContentCacheBase *
>( qobject_cast< const QgsAbstractContentCacheBase * >(
this ) ),
"onRemoteContentFetched", Qt::QueuedConnection, Q_ARG( QString, path ), Q_ARG(
bool,
true ) );
185 if ( waitForTaskFinished( task ) )
187 if ( mRemoteContentCache.contains( path ) )
190 return *mRemoteContentCache[ path ];
194 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 QgsTaskManager * taskManager()
Returns the application's task manager, used for managing application wide background task handling.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
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 ...
QNetworkReply * reply()
Returns the network reply.
QList< QgsTask * > activeTasks() const
Returns a list of the active (queued or running) tasks.
long addTask(QgsTask *task, int priority=0)
Adds a task to the manager.
Abstract base class for long running background tasks.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
#define QgsSetRequestInitiatorClass(request, _class)