|
46 | 46 | # include <proj.h>
|
47 | 47 | #endif
|
48 | 48 |
|
| 49 | +#include "mapper_config.h" // IWYU pragma: keep |
49 | 50 | #include "core/crs_template.h"
|
50 | 51 | #include "fileformats/file_format.h"
|
51 | 52 | #include "fileformats/xml_file_format.h"
|
@@ -91,21 +92,58 @@ namespace OpenOrienteering {
|
91 | 92 | namespace
|
92 | 93 | {
|
93 | 94 | #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 | + |
94 | 141 | /**
|
95 | 142 | * Provides a cache directory for RROJ resource files.
|
96 | 143 | */
|
97 | 144 | const QDir& projCacheDirectory()
|
98 | 145 | {
|
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(); |
109 | 147 | return dir;
|
110 | 148 | }
|
111 | 149 |
|
|
0 commit comments