Skip to content

Commit 1004cd0

Browse files
committed
ProjSetup: Reset Android cache on Mapper change
It is not enough to rely on the presence of PROJ resource file names in the cache directory: The contents of resources (esp. proj.db) may change over time. It is also better to get rid of obsolete files.
1 parent 4e99a84 commit 1004cd0

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

src/core/georeferencing.cpp

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
# include <proj.h>
4747
#endif
4848

49+
#include "mapper_config.h" // IWYU pragma: keep
4950
#include "core/crs_template.h"
5051
#include "fileformats/file_format.h"
5152
#include "fileformats/xml_file_format.h"
@@ -91,21 +92,58 @@ namespace OpenOrienteering {
9192
namespace
9293
{
9394
#ifdef Q_OS_ANDROID
95+
/**
96+
* Maintains and returns the cache directory for PROJ resource files.
97+
*
98+
* PROJ resources may change with different versions of Mapper.
99+
* This function creates the directory if it is missing,
100+
* but also resets it on Mapper version changes.
101+
*/
102+
QDir ensureProjCacheDirectory()
103+
{
104+
auto cache = QDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
105+
auto const proj = QStringLiteral("proj");
106+
auto const version_filename = QStringLiteral("proj/org.openorienteering.mapper");
107+
if (cache.exists(proj))
108+
{
109+
QFile version_file(cache.filePath(version_filename));
110+
if (!version_file.open(QIODevice::ReadOnly)
111+
|| version_file.readAll() != APP_VERSION)
112+
{
113+
qDebug("PROJ cache version mismatch");
114+
auto proj_dir = cache;
115+
if (!proj_dir.cd(proj) || !proj_dir.removeRecursively())
116+
qDebug("Failed to remove PROJ cache");
117+
}
118+
}
119+
if (!cache.exists(proj))
120+
{
121+
qDebug("Creating PROJ cache, version '" APP_VERSION "'");
122+
if (cache.mkpath(proj))
123+
{
124+
QFile version_file(cache.filePath(version_filename));
125+
if (!version_file.open(QIODevice::WriteOnly)
126+
|| version_file.write(APP_VERSION) != qstrlen(APP_VERSION))
127+
{
128+
qDebug("Failed to write PROJ cache version file");
129+
}
130+
}
131+
else
132+
{
133+
qDebug("Could not create a PROJ cache");
134+
}
135+
}
136+
137+
cache.cd(proj);
138+
return cache;
139+
}
140+
94141
/**
95142
* Provides a cache directory for RROJ resource files.
96143
*/
97144
const QDir& projCacheDirectory()
98145
{
99-
static auto const dir = []() -> QDir {
100-
auto cache = QDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
101-
auto proj = QStringLiteral("proj");
102-
if (!cache.exists(proj))
103-
cache.mkpath(proj);
104-
if (!cache.exists(proj))
105-
qDebug("Could not create a cache directory for PROJ resource files");
106-
cache.cd(proj);
107-
return cache;
108-
}();
146+
static auto const dir = ensureProjCacheDirectory();
109147
return dir;
110148
}
111149

0 commit comments

Comments
 (0)