QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
QGeoFileTileCacheQGC.cpp
Go to the documentation of this file.
2
3#include <QtCore/QDir>
4#include <QtCore/QLoggingCategory>
5#include <QtCore/QStandardPaths>
6
7#include "AppSettings.h"
8#include "MapsSettings.h"
9#include "AppMessages.h"
10#include "QGCCacheTile.h"
11#include "QGCFileHelper.h"
12#include "QGCLoggingCategory.h"
13#include "QGCMapEngine.h"
14#include "QGCMapTasks.h"
15#include "QGCMapUrlEngine.h"
16#include "SettingsManager.h"
17
18QGC_LOGGING_CATEGORY(QGeoFileTileCacheQGCLog, "QtLocationPlugin.QGeoFileTileCacheQGC")
19
20QString QGeoFileTileCacheQGC::_databaseFilePath;
21QString QGeoFileTileCacheQGC::_cachePath;
22std::atomic<bool> QGeoFileTileCacheQGC::_cacheWasReset = false;
23
24QGeoFileTileCacheQGC::QGeoFileTileCacheQGC(const QVariantMap &parameters, QObject *parent)
25 : QGeoFileTileCache(baseCacheDirectory(), parent)
26{
27 qCDebug(QGeoFileTileCacheQGCLog) << this;
28
29 setCostStrategyDisk(QGeoFileTileCache::ByteSize);
30 setMaxDiskUsage(_getDefaultMaxDiskCache());
31 setCostStrategyMemory(QGeoFileTileCache::ByteSize);
32 setMaxMemoryUsage(_getMemLimit(parameters));
33 setCostStrategyTexture(QGeoFileTileCache::ByteSize);
34 setMinTextureUsage(_getDefaultMinTexture());
35 setExtraTextureUsage(_getDefaultExtraTexture() - minTextureUsage());
36
38
39 directory_ = _getCachePath(parameters);
40}
41
43{
44 static std::once_flag cacheInit;
45 std::call_once(cacheInit, [] {
46 _initCache();
47 });
48}
49
51{
52 if (QGeoFileTileCacheQGCLog().isDebugEnabled()) {
53 printStats();
54 }
55
56 qCDebug(QGeoFileTileCacheQGCLog) << this;
57}
58
59uint32_t QGeoFileTileCacheQGC::_getMemLimit(const QVariantMap &parameters)
60{
61 uint32_t memLimit = 0;
62 if (parameters.contains(QStringLiteral("mapping.cache.memory.size"))) {
63 bool ok = false;
64 memLimit = parameters.value(QStringLiteral("mapping.cache.memory.size")).toString().toUInt(&ok);
65 if (!ok) {
66 memLimit = 0;
67 }
68 }
69
70 if (memLimit == 0) {
71 // Value saved in MB
72 memLimit = _getMaxMemCacheSetting() * qPow(1024, 2);
73 }
74 if (memLimit == 0) {
75 memLimit = _getDefaultMaxMemLimit();
76 }
77
78 // 1MB Minimum Memory Cache Required
79 // MaxMemoryUsage is 32bit Integer, Round down to 1GB
80 memLimit = qBound(static_cast<uint32_t>(qPow(1024, 2)), memLimit, static_cast<uint32_t>(qPow(1024, 3)));
81 return memLimit;
82}
83
84quint32 QGeoFileTileCacheQGC::_getMaxMemCacheSetting()
85{
86 return SettingsManager::instance()->mapsSettings()->maxCacheMemorySize()->rawValue().toUInt();
87}
88
90{
91 return SettingsManager::instance()->mapsSettings()->maxCacheDiskSize()->rawValue().toUInt();
92}
93
94void QGeoFileTileCacheQGC::cacheTile(const QString &type, int x, int y, int z, const QByteArray &image, const QString &format, qulonglong set)
95{
96 const QString hash = UrlFactory::getTileHash(type, x, y, z);
97 cacheTile(type, hash, image, format, set);
98}
99
100void QGeoFileTileCacheQGC::cacheTile(const QString &type, const QString &hash, const QByteArray &image, const QString &format, qulonglong set)
101{
103 if (!appSettings->disableAllPersistence()->rawValue().toBool()) {
104 QGCCacheTile *tile = new QGCCacheTile(hash, image, format, type, set);
105 QGCSaveTileTask *task = new QGCSaveTileTask(tile);
106 if (!getQGCMapEngine()->addTask(task)) {
107 task->deleteLater();
108 }
109 }
110}
111
112QGCFetchTileTask* QGeoFileTileCacheQGC::createFetchTileTask(const QString &type, int x, int y, int z)
113{
114 const QString hash = UrlFactory::getTileHash(type, x, y, z);
115 QGCFetchTileTask *task = new QGCFetchTileTask(hash);
116 return task;
117}
118
119QString QGeoFileTileCacheQGC::_getCachePath(const QVariantMap &parameters)
120{
121 QString cacheDir;
122 if (parameters.contains(QStringLiteral("mapping.cache.directory"))) {
123 cacheDir = parameters.value(QStringLiteral("mapping.cache.directory")).toString();
124 } else {
125 cacheDir = _cachePath + QLatin1String("/providers");
127 qCWarning(QGeoFileTileCacheQGCLog) << "Could not create mapping disk cache directory:" << cacheDir;
128 cacheDir = QDir::homePath() + QStringLiteral("/.qgcmapscache/");
129 }
130 }
131
133 qCWarning(QGeoFileTileCacheQGCLog) << "Could not create mapping disk cache directory:" << cacheDir;
134 cacheDir.clear();
135 }
136
137 return cacheDir;
138}
139
140bool QGeoFileTileCacheQGC::_wipeDirectory(const QString &dirPath)
141{
142 bool result = true;
143
144 const QDir dir(dirPath);
145 if (dir.exists(dirPath)) {
146 _cacheWasReset = true;
147
148 const QFileInfoList fileList = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst);
149 for (const QFileInfo &info : fileList) {
150 if (info.isDir()) {
151 result = _wipeDirectory(info.absoluteFilePath());
152 } else {
153 result = QFile::remove(info.absoluteFilePath());
154 }
155
156 if (!result) {
157 return result;
158 }
159 }
160 result = dir.rmdir(dirPath);
161 }
162
163 return result;
164}
165
166void QGeoFileTileCacheQGC::_wipeOldCaches()
167{
168 const QStringList oldCaches = {"/QGCMapCache55", "/QGCMapCache100", "/QGCMapCache300"};
169 for (const QString &cache : oldCaches) {
170 QString oldCacheDir;
171 #if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
172 oldCacheDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
173 #else
174 oldCacheDir = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation);
175 #endif
176 oldCacheDir += cache;
177 _wipeDirectory(oldCacheDir);
178 }
179}
180
181void QGeoFileTileCacheQGC::_initCache()
182{
183 _wipeOldCaches();
184
185 // QString cacheDir = QAbstractGeoTileCache::baseCacheDirectory()
186#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
187 QString cacheDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
188 cacheDir += QStringLiteral("/QGCMapCache");
189#else
190 QString cacheDir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
191 cacheDir += QStringLiteral("/QGCMapCache");
192#endif
194 qCWarning(QGeoFileTileCacheQGCLog) << "Could not create mapping disk cache directory:" << cacheDir;
195
196 cacheDir = QDir::homePath() + QStringLiteral("/.qgcmapscache/");
198 qCWarning(QGeoFileTileCacheQGCLog) << "Could not create mapping disk cache directory:" << cacheDir;
199 cacheDir.clear();
200 }
201 }
202
203 _cachePath = cacheDir;
204 if (!_cachePath.isEmpty()) {
205 _databaseFilePath = QString(_cachePath + QStringLiteral("/qgcMapCache.db"));
206
207 qCDebug(QGeoFileTileCacheQGCLog) << "Map Cache in:" << _databaseFilePath;
208 } else {
209 qCCritical(QGeoFileTileCacheQGCLog) << "Could not find suitable map cache directory.";
210 }
211
212 if (_cacheWasReset) {
214 "The Offline Map Cache database has been upgraded. "
215 "Your old map cache sets have been reset."));
216 }
217}
#define QGC_LOGGING_CATEGORY(name, categoryStr)
QGCMapEngine * getQGCMapEngine()
Application Settings.
Definition AppSettings.h:10
QGeoFileTileCacheQGC(const QVariantMap &parameters, QObject *parent=nullptr)
static QGCFetchTileTask * createFetchTileTask(const QString &type, int x, int y, int z)
static void cacheTile(const QString &type, int x, int y, int z, const QByteArray &image, const QString &format, qulonglong set=UINT64_MAX)
static quint32 getMaxDiskCacheSetting()
MapsSettings * mapsSettings() const
static SettingsManager * instance()
AppSettings * appSettings() const
static QString getTileHash(QStringView type, int x, int y, int z)
bool ensureDirectoryExists(const QString &path)
void showAppMessage(const QString &message, const QString &title)
Modal application message. Queued if the UI isn't ready yet.
Definition AppMessages.cc:9