18#ifndef QGSABSTRACTCONTENTCACHE_H
19#define QGSABSTRACTCONTENTCACHE_H
35#include <QNetworkReply>
37#include <QRecursiveMutex>
42using namespace Qt::StringLiterals;
203 virtual bool checkReply( QNetworkReply *reply,
const QString &path )
const
218 virtual void onRemoteContentFetched(
const QString &url,
bool success );
253 , mFileModifiedCheckTimeout( fileModifiedCheckTimeout )
254 , mTypeString( typeString.isEmpty() ? QObject::tr(
"Content" ) : typeString )
261 const QMutexLocker locker( &
mMutex );
263 const QList<T *> entries = mEntryLookup.values( path );
264 if ( entries.isEmpty() )
267 for ( T *entry : entries )
269 takeEntryFromList( entry );
270 mEntryLookup.remove( path, entry );
285 if ( mLeastRecentEntry == mMostRecentEntry )
289 T *entry = mLeastRecentEntry;
293 entry =
static_cast< T *
>( entry->nextEntry );
295 takeEntryFromList( bkEntry );
296 mEntryLookup.remove( bkEntry->path, bkEntry );
315 QByteArray getContent(
const QString &path,
const QByteArray &missingContent,
const QByteArray &fetchingContent,
bool blocking =
false )
const;
319 const QMutexLocker locker( &
mMutex );
320 mPendingRemoteUrls.remove( url );
322 T *nextEntry = mLeastRecentEntry;
323 while ( T *entry = nextEntry )
325 nextEntry =
static_cast< T *
>( entry->nextEntry );
326 if ( entry->path == url )
328 takeEntryFromList( entry );
329 mEntryLookup.remove( entry->path, entry );
380 const QString path = entryTemplate->path;
381 T *currentEntry =
nullptr;
382 const QList<T *> entries = mEntryLookup.values( path );
384 for ( T *cacheEntry : entries )
386 if ( cacheEntry->isEqual( entryTemplate ) )
388 if ( mFileModifiedCheckTimeout <= 0 || cacheEntry->fileModifiedLastCheckTimer.hasExpired( mFileModifiedCheckTimeout ) )
390 if ( !modified.isValid() )
391 modified = QFileInfo( path ).lastModified();
393 if ( cacheEntry->fileModified != modified )
396 cacheEntry->fileModifiedLastCheckTimer.restart();
398 currentEntry = cacheEntry;
406 currentEntry = insertCacheEntry( entryTemplate );
410 delete entryTemplate;
411 entryTemplate =
nullptr;
412 ( void ) entryTemplate;
413 takeEntryFromList( currentEntry );
414 if ( !mMostRecentEntry )
416 mMostRecentEntry = currentEntry;
417 mLeastRecentEntry = currentEntry;
421 mMostRecentEntry->nextEntry = currentEntry;
422 currentEntry->previousEntry = mMostRecentEntry;
423 currentEntry->nextEntry =
nullptr;
424 mMostRecentEntry = currentEntry;
447 T *insertCacheEntry( T *entry )
449 entry->mFileModifiedCheckTimeout = mFileModifiedCheckTimeout;
451 if ( !entry->path.startsWith(
"base64:"_L1 ) )
453 entry->fileModified = QFileInfo( entry->path ).lastModified();
454 entry->fileModifiedLastCheckTimer.start();
457 mEntryLookup.insert( entry->path, entry );
460 if ( !mMostRecentEntry )
462 mLeastRecentEntry = entry;
463 mMostRecentEntry = entry;
464 entry->previousEntry =
nullptr;
465 entry->nextEntry =
nullptr;
469 entry->previousEntry = mMostRecentEntry;
470 entry->nextEntry =
nullptr;
471 mMostRecentEntry->nextEntry = entry;
472 mMostRecentEntry = entry;
483 void takeEntryFromList( T *entry )
490 if ( entry->previousEntry )
492 entry->previousEntry->nextEntry = entry->nextEntry;
496 mLeastRecentEntry =
static_cast< T *
>( entry->nextEntry );
498 if ( entry->nextEntry )
500 entry->nextEntry->previousEntry = entry->previousEntry;
504 mMostRecentEntry =
static_cast< T *
>( entry->previousEntry );
511 void printEntryList()
513 QgsDebugMsgLevel( u
"****************cache entry list*************************"_s, 1 );
515 T *entry = mLeastRecentEntry;
520 entry =
static_cast< T *
>( entry->nextEntry );
525 QMultiHash< QString, T * > mEntryLookup;
528 int mFileModifiedCheckTimeout = 30000;
532 T *mLeastRecentEntry =
nullptr;
533 T *mMostRecentEntry =
nullptr;
535 mutable QCache< QString, QByteArray > mRemoteContentCache;
536 mutable QSet< QString > mPendingRemoteUrls;
void remoteContentFetched(const QString &url)
Emitted when the cache has finished retrieving content from a remote url.
static bool parseEmbeddedStringData(const QString &path, QString *mimeType=nullptr, QString *data=nullptr)
Parses a path to determine if it represents a embedded string data, and if so, extracts the component...
virtual bool checkReply(QNetworkReply *reply, const QString &path) const
Runs additional checks on a network reply to ensure that the reply content is consistent with that re...
static bool parseBase64DataUrl(const QString &path, QString *mimeType=nullptr, QString *data=nullptr)
Parses a path to determine if it represents a base 64 encoded HTML data URL, and if so,...
QgsAbstractContentCacheBase(QObject *parent)
Constructor for QgsAbstractContentCacheBase, with the specified parent object.
virtual bool invalidateCacheEntry(const QString &path)
Invalidates a cache entry for the specified path.
static bool isBase64Data(const QString &path)
Returns true if path represents base64 encoded data.
virtual int dataSize() const =0
Returns the memory usage in bytes for the entry.
QgsAbstractContentCacheEntry * nextEntry
Entries are kept on a linked list, sorted by last access.
virtual void dump() const =0
Dumps debugging strings containing the item's properties.
int mFileModifiedCheckTimeout
Timeout before re-checking whether the file modified date has changed.
virtual ~QgsAbstractContentCacheEntry()=default
QElapsedTimer fileModifiedLastCheckTimer
Time since last check of file modified date.
QgsAbstractContentCacheEntry(const QgsAbstractContentCacheEntry &rh)=delete
QgsAbstractContentCacheEntry & operator=(const QgsAbstractContentCacheEntry &rh)=delete
QgsAbstractContentCacheEntry(const QString &path)
Constructor for QgsAbstractContentCacheEntry for an entry relating to the specified path.
QString path
Represents the absolute path to a file, a remote URL, or a base64 encoded string.
virtual bool isEqual(const QgsAbstractContentCacheEntry *other) const =0
Tests whether this entry matches another entry.
QgsAbstractContentCacheEntry * previousEntry
Entries are kept on a linked list, sorted by last access.
QDateTime fileModified
Timestamp when file was last modified.
bool operator==(const QgsAbstractContentCacheEntry &other) const
long mMaxCacheSize
Maximum cache size.
bool invalidateCacheEntry(const QString &path) override
Invalidates a cache entry for the specified path.
friend class TestQgsImageCache
T * findExistingEntry(T *entryTemplate)
Returns the existing entry from the cache which matches entryTemplate (deleting entryTemplate when do...
~QgsAbstractContentCache() override
void onRemoteContentFetched(const QString &url, bool success) override
Triggered after remote content (i.e.
QgsAbstractContentCache(QObject *parent=nullptr, const QString &typeString=QString(), long maxCacheSize=20000000, int fileModifiedCheckTimeout=30000)
Constructor for QgsAbstractContentCache, with the specified parent object.
long mTotalSize
Estimated total size of all cached content.
void trimToMaximumSize()
Removes the least used cache entries until the maximum cache size is under the predefined size limit.
bool waitForTaskFinished(QgsNetworkContentFetcherTask *task) const
Blocks the current thread until the task finishes (or user's preset network timeout expires).
friend class TestQgsSvgCache
static int timeout()
Returns the network timeout length, in milliseconds.
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 ...
TaskStatus status() const
Returns the current task status.
@ Complete
Task successfully completed.
bool waitForFinished(int timeout=30000)
Blocks the current thread until the task finishes or a maximum of timeout milliseconds.
#define QgsDebugMsgLevel(str, level)