QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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
21
22#include <QRegularExpression>
23#include <QString>
24
25#include "moc_qgsabstractcontentcache.cpp"
26
27using namespace Qt::StringLiterals;
28
29//
30// QgsAbstractContentCacheEntry
31//
32
37
38//
39// QgsAbstractContentCacheBase
40//
41
43 : QObject( parent )
44{}
45
47{
48 Q_UNUSED( path );
49 return false;
50}
51
53{
54
55}
56
57bool QgsAbstractContentCacheBase::parseBase64DataUrl( const QString &path, QString *mimeType, QString *data )
58{
59 const thread_local QRegularExpression sRx( u"^data:([a-zA-Z0-9+\\-]*\\/[a-zA-Z0-9+\\-]*?)(?:;(base64|utf8))?,(.*)$"_s );
60 const QRegularExpressionMatch base64Match = sRx.match( path );
61 if ( !base64Match.hasMatch() )
62 return false;
63
64 const QString typeMatch = base64Match.captured( 2 );
65 const QString mimeMatch = base64Match.captured( 1 );
66
67 if ( mimeType )
68 *mimeType = mimeMatch;
69 if ( data )
70 *data = base64Match.captured( 3 );
71
72 if ( typeMatch == "base64"_L1 )
73 return true; // definitely base 64
74 else if ( typeMatch == "utf8"_L1 )
75 return false; // definitely NOT base 64
76
77 // if we aren't certain it's base 64, and it has an xml mime type, assume it's not.
78 // see https://github.com/qgis/QGIS/issues/59575
79 if ( mimeMatch.endsWith( "xml"_L1 ) || mimeMatch.endsWith( "svg"_L1 ) )
80 return false;
81
82 return true;
83}
84
85bool QgsAbstractContentCacheBase::parseEmbeddedStringData( const QString &path, QString *mimeType, QString *data )
86{
87 const thread_local QRegularExpression sRx( u"^data:([a-zA-Z0-9+\\-]*\\/[a-zA-Z0-9+\\-]*?)\\;utf8,(.*)$"_s, QRegularExpression::DotMatchesEverythingOption );
88 const QRegularExpressionMatch stringMatch = sRx.match( path );
89
90 if ( !stringMatch.hasMatch() )
91 return false;
92
93 if ( mimeType )
94 *mimeType = stringMatch.captured( 1 );
95 if ( data )
96 *data = stringMatch.captured( 2 );
97
98 return true;
99}
100
102{
103 return path.startsWith( "base64:"_L1 )
104 || parseBase64DataUrl( path );
105}
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.
QString path
Represents the absolute path to a file, a remote URL, or a base64 encoded string.