QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
Loading...
Searching...
No Matches
qgssourcecache.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssourcecache.cpp
3 -----------------
4 begin : July 2020
5 copyright : (C) 2020 by Mathieu Pellerin
6 email : nirvn dot asia 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
18#include "qgssourcecache.h"
19
20#include <memory>
21
22#include "qgis.h"
24#include "qgslogger.h"
25
26#include <QBuffer>
27#include <QFile>
28#include <QString>
29#include <QTemporaryDir>
30
31#include "moc_qgssourcecache.cpp"
32
33using namespace Qt::StringLiterals;
34
36
37QgsSourceCacheEntry::QgsSourceCacheEntry( const QString &path )
39{}
40
41bool QgsSourceCacheEntry::isEqual( const QgsAbstractContentCacheEntry *other ) const
42{
43 const QgsSourceCacheEntry *otherSource = dynamic_cast< const QgsSourceCacheEntry * >( other );
44 // cheapest checks first!
45 if ( !otherSource || otherSource->filePath != filePath )
46 return false;
47
48 return true;
49}
50
51int QgsSourceCacheEntry::dataSize() const
52{
53 return filePath.size();
54}
55
56void QgsSourceCacheEntry::dump() const
57{
58 QgsDebugMsgLevel( u"path: %1"_s.arg( path ), 3 );
59}
60
62
64 : QgsAbstractContentCache< QgsSourceCacheEntry >( parent, QObject::tr( "Source" ) )
65{
66 temporaryDir = std::make_unique<QTemporaryDir>();
67
69}
70
71QString QgsSourceCache::localFilePath( const QString &path, bool blocking )
72{
73 const QString file = path.trimmed();
74 if ( file.isEmpty() )
75 return QString();
76
77 const QMutexLocker locker( &mMutex );
78
79 QgsSourceCacheEntry *currentEntry = findExistingEntry( new QgsSourceCacheEntry( file ) );
80
81 //if current entry's temporary file is empty, create it
82 if ( currentEntry->filePath.isEmpty() )
83 {
84 bool isBroken;
85 const QString filePath = fetchSource( file, isBroken, blocking );
86 currentEntry->filePath = filePath;
87 }
88
89 return currentEntry->filePath;
90}
91
92QString QgsSourceCache::fetchSource( const QString &path, bool &isBroken, bool blocking ) const
93{
94 QString filePath;
95
96 if ( !path.startsWith( "base64:"_L1 ) && QFile::exists( path ) )
97 {
98 filePath = path;
99 }
100 else
101 {
102 const QByteArray ba = getContent( path, QByteArray( "broken" ), QByteArray( "fetching" ), blocking );
103
104 if ( ba == "broken" )
105 {
106 isBroken = true;
107 }
108 else
109 {
110 int id = 1;
111 filePath = temporaryDir->filePath( QString::number( id ) );
112 while ( QFile::exists( filePath ) )
113 filePath = temporaryDir->filePath( QString::number( ++id ) );
114
115 QFile file( filePath );
116 if ( !file.open( QIODevice::WriteOnly ) )
117 {
118 QgsDebugError( u"Can't open file %1"_s.arg( filePath ) );
119 return QString();
120 }
121 file.write( ba );
122 file.close();
123 }
124 }
125
126 return filePath;
127}
128
129template class QgsAbstractContentCache<QgsSourceCacheEntry>; // clazy:exclude=missing-qobject-macro
void remoteContentFetched(const QString &url)
Emitted when the cache has finished retrieving content from a remote url.
Base class for entries in a QgsAbstractContentCache.
Abstract base class for file content caches, such as SVG or raster image caches.
QByteArray getContent(const QString &path, const QByteArray &missingContent, const QByteArray &fetchingContent, bool blocking=false) const
QgsSourceCacheEntry * findExistingEntry(QgsSourceCacheEntry *entryTemplate)
QgsAbstractContentCache(QObject *parent=nullptr, const QString &typeString=QString(), long maxCacheSize=20000000, int fileModifiedCheckTimeout=30000)
QgsSourceCache(QObject *parent=nullptr)
Constructor for QgsSourceCache, with the specified parent object.
void remoteSourceFetched(const QString &url)
Emitted when the cache has finished retrieving a 3D model from a remote url.
QString localFilePath(const QString &path, bool blocking=false)
Returns a local file path reflecting the content of a specified source path.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63
#define QgsDebugError(str)
Definition qgslogger.h:59