QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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#include "moc_qgssourcecache.cpp"
21
22#include "qgis.h"
23#include "qgslogger.h"
24
25#include <QFile>
26#include <QBuffer>
27#include <QTemporaryDir>
28
30
31QgsSourceCacheEntry::QgsSourceCacheEntry( const QString &path )
33{
34}
35
36bool QgsSourceCacheEntry::isEqual( const QgsAbstractContentCacheEntry *other ) const
37{
38 const QgsSourceCacheEntry *otherSource = dynamic_cast< const QgsSourceCacheEntry * >( other );
39 // cheapest checks first!
40 if ( !otherSource || otherSource->filePath != filePath )
41 return false;
42
43 return true;
44}
45
46int QgsSourceCacheEntry::dataSize() const
47{
48 return filePath.size();
49}
50
51void QgsSourceCacheEntry::dump() const
52{
53 QgsDebugMsgLevel( QStringLiteral( "path: %1" ).arg( path ), 3 );
54}
55
57
59 : QgsAbstractContentCache< QgsSourceCacheEntry >( parent, QObject::tr( "Source" ) )
60{
61 temporaryDir.reset( new QTemporaryDir() );
62
64}
65
66QString QgsSourceCache::localFilePath( const QString &path, bool blocking )
67{
68 const QString file = path.trimmed();
69 if ( file.isEmpty() )
70 return QString();
71
72 const QMutexLocker locker( &mMutex );
73
74 QgsSourceCacheEntry *currentEntry = findExistingEntry( new QgsSourceCacheEntry( file ) );
75
76 //if current entry's temporary file is empty, create it
77 if ( currentEntry->filePath.isEmpty() )
78 {
79 bool isBroken;
80 const QString filePath = fetchSource( file, isBroken, blocking );
81 currentEntry->filePath = filePath;
82 }
83
84 return currentEntry->filePath;
85}
86
87QString QgsSourceCache::fetchSource( const QString &path, bool &isBroken, bool blocking ) const
88{
89 QString filePath;
90
91 if ( !path.startsWith( QLatin1String( "base64:" ) ) && QFile::exists( path ) )
92 {
93 filePath = path;
94 }
95 else
96 {
97 const QByteArray ba = getContent( path, QByteArray( "broken" ), QByteArray( "fetching" ), blocking );
98
99 if ( ba == "broken" )
100 {
101 isBroken = true;
102 }
103 else
104 {
105 int id = 1;
106 filePath = temporaryDir->filePath( QString::number( id ) );
107 while ( QFile::exists( filePath ) )
108 filePath = temporaryDir->filePath( QString::number( ++id ) );
109
110 QFile file( filePath );
111 file.open( QIODevice::WriteOnly );
112 file.write( ba );
113 file.close();
114 }
115 }
116
117 return filePath;
118}
119
120template 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
Gets the file content corresponding to the given path.
QgsSourceCacheEntry * findExistingEntry(QgsSourceCacheEntry *entryTemplate)
Returns the existing entry from the cache which matches entryTemplate (deleting entryTemplate when do...
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:39