QGIS API Documentation 3.43.0-Master (261ee7da134)
qgsabstractcontentcache.cpp
Go to the documentation of this file.
1/***************************************************************************
2 QgsAbstractContentCache.cpp
3 -----------------
4 begin : December 2018
5 copyright : (C) 2018 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "moc_qgsabstractcontentcache.cpp"
21#include <QRegularExpression>
22
23//
24// QgsAbstractContentCacheEntry
25//
26
28 : path( path )
29{
30}
31
32//
33// QgsAbstractContentCacheBase
34//
35
37 : QObject( parent )
38{}
39
41{
42 Q_UNUSED( path );
43 return false;
44}
45
47{
48
49}
50
51bool QgsAbstractContentCacheBase::parseBase64DataUrl( const QString &path, QString *mimeType, QString *data )
52{
53 const thread_local QRegularExpression sRx( QStringLiteral( "^data:([a-zA-Z0-9+\\-]*\\/[a-zA-Z0-9+\\-]*?)(?:;(base64|utf8))?,(.*)$" ) );
54 const QRegularExpressionMatch base64Match = sRx.match( path );
55 if ( !base64Match.hasMatch() )
56 return false;
57
58 const QString typeMatch = base64Match.captured( 2 );
59 const QString mimeMatch = base64Match.captured( 1 );
60
61 if ( mimeType )
62 *mimeType = mimeMatch;
63 if ( data )
64 *data = base64Match.captured( 3 );
65
66 if ( typeMatch == QLatin1String( "base64" ) )
67 return true; // definitely base 64
68 else if ( typeMatch == QLatin1String( "utf8" ) )
69 return false; // definitely NOT base 64
70
71 // if we aren't certain it's base 64, and it has an xml mime type, assume it's not.
72 // see https://github.com/qgis/QGIS/issues/59575
73 if ( mimeMatch.endsWith( QLatin1String( "xml" ) ) || mimeMatch.endsWith( QLatin1String( "svg" ) ) )
74 return false;
75
76 return true;
77}
78
79bool QgsAbstractContentCacheBase::parseEmbeddedStringData( const QString &path, QString *mimeType, QString *data )
80{
81 const thread_local QRegularExpression sRx( QStringLiteral( "^data:([a-zA-Z0-9+\\-]*\\/[a-zA-Z0-9+\\-]*?)\\;utf8,(.*)$" ), QRegularExpression::DotMatchesEverythingOption );
82 const QRegularExpressionMatch stringMatch = sRx.match( path );
83
84 if ( !stringMatch.hasMatch() )
85 return false;
86
87 if ( mimeType )
88 *mimeType = stringMatch.captured( 1 );
89 if ( data )
90 *data = stringMatch.captured( 2 );
91
92 return true;
93}
94
96{
97 return path.startsWith( QLatin1String( "base64:" ) )
98 || parseBase64DataUrl( path );
99}
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 void onRemoteContentFetched(const QString &url, bool success)
Triggered after remote content (i.e.
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.
QgsAbstractContentCacheEntry(const QString &path)
Constructor for QgsAbstractContentCacheEntry for an entry relating to the specified path.