presentation/MapTileSource.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "MapTileSource.h" | ||
| 2 | |||
| 3 | #include "util/RasterImageFormat.h" | ||
| 4 | |||
| 5 | #include <QBuffer> | ||
| 6 | #include <QCoreApplication> | ||
| 7 | #include <QCryptographicHash> | ||
| 8 | #include <QDateTime> | ||
| 9 | #include <QDirIterator> | ||
| 10 | #include <QFile> | ||
| 11 | #include <QFileInfo> | ||
| 12 | #include <QHostAddress> | ||
| 13 | #include <QImage> | ||
| 14 | #include <QImageReader> | ||
| 15 | #include <QLoggingCategory> | ||
| 16 | #include <QNetworkAccessManager> | ||
| 17 | #include <QNetworkReply> | ||
| 18 | #include <QNetworkRequest> | ||
| 19 | #include <QSaveFile> | ||
| 20 | #include <QUrl> | ||
| 21 | #include <algorithm> | ||
| 22 | |||
| 23 | Q_DECLARE_LOGGING_CATEGORY(lcMapTiles) | ||
| 24 | |||
| 25 | namespace { | ||
| 26 | |||
| 27 | 12 | bool loopback(const QString &host) { | |
| 28 |
1/2✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 12 times.
|
12 | if (host.compare(QLatin1String("localhost"), Qt::CaseInsensitive) == 0) |
| 29 | ✗ | return true; | |
| 30 |
1/1✓ Branch 6 → 7 taken 12 times.
|
12 | const QHostAddress address(host); |
| 31 |
5/6✓ Branch 7 → 8 taken 12 times.
✓ Branch 8 → 9 taken 11 times.
✓ Branch 8 → 12 taken 1 time.
✓ Branch 9 → 10 taken 11 times.
✓ Branch 10 → 11 taken 11 times.
✗ Branch 10 → 12 not taken.
|
12 | return !address.isNull() && address.isLoopback(); |
| 32 | 12 | } | |
| 33 | |||
| 34 | // "z/x/y" → its three numbers; false for anything else. | ||
| 35 | 181 | bool parseTile(const QString &tile, int *zoom, int *x, int *y) { | |
| 36 |
1/1✓ Branch 5 → 6 taken 181 times.
|
181 | const QStringList parts = tile.split(QLatin1Char('/')); |
| 37 |
1/2✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 181 times.
|
181 | if (parts.size() != 3) |
| 38 | ✗ | return false; | |
| 39 | 181 | bool ok[3] = {false, false, false}; | |
| 40 |
1/1✓ Branch 10 → 11 taken 181 times.
|
181 | *zoom = parts.at(0).toInt(&ok[0]); |
| 41 |
1/1✓ Branch 12 → 13 taken 181 times.
|
181 | *x = parts.at(1).toInt(&ok[1]); |
| 42 |
1/1✓ Branch 14 → 15 taken 181 times.
|
181 | *y = parts.at(2).toInt(&ok[2]); |
| 43 |
3/6✓ Branch 15 → 16 taken 181 times.
✗ Branch 15 → 19 not taken.
✓ Branch 16 → 17 taken 181 times.
✗ Branch 16 → 19 not taken.
✓ Branch 17 → 18 taken 181 times.
✗ Branch 17 → 19 not taken.
|
181 | const int count = ok[0] && *zoom >= 0 && *zoom <= 22 ? 1 << *zoom : 0; |
| 44 |
8/16✓ Branch 20 → 21 taken 181 times.
✗ Branch 20 → 29 not taken.
✓ Branch 21 → 22 taken 181 times.
✗ Branch 21 → 29 not taken.
✓ Branch 22 → 23 taken 181 times.
✗ Branch 22 → 29 not taken.
✓ Branch 23 → 24 taken 181 times.
✗ Branch 23 → 29 not taken.
✓ Branch 24 → 25 taken 181 times.
✗ Branch 24 → 29 not taken.
✓ Branch 25 → 26 taken 181 times.
✗ Branch 25 → 29 not taken.
✓ Branch 26 → 27 taken 181 times.
✗ Branch 26 → 29 not taken.
✓ Branch 27 → 28 taken 181 times.
✗ Branch 27 → 29 not taken.
|
181 | return ok[0] && ok[1] && ok[2] && count > 0 && *x >= 0 && *x < count && *y >= 0 && *y < count; |
| 45 | 181 | } | |
| 46 | |||
| 47 | } // namespace | ||
| 48 | |||
| 49 | 813 | MapTileSource::MapTileSource(QDir cacheRoot, QObject *parent) | |
| 50 |
4/7✓ Branch 4 → 5 taken 813 times.
✓ Branch 5 → 6 taken 813 times.
✓ Branch 6 → 7 taken 813 times.
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 813 times.
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 17 not taken.
|
813 | : QObject(parent), m_cacheRoot(std::move(cacheRoot)), m_network(new QNetworkAccessManager(this)), |
| 51 |
1/1✓ Branch 9 → 10 taken 813 times.
|
1626 | m_template(defaultTemplate()) { |
| 52 | // No cookies or credentials ever reach a tile server; redirects are not followed. | ||
| 53 |
1/1✓ Branch 13 → 14 taken 813 times.
|
813 | m_network->setRedirectPolicy(QNetworkRequest::ManualRedirectPolicy); |
| 54 | 813 | } | |
| 55 | |||
| 56 | 1625 | MapTileSource::~MapTileSource() { abortAll(m_active.keys()); } | |
| 57 | |||
| 58 | // Aborting emits `finished` synchronously: the transfers leave m_active and | ||
| 59 | // lose their handler first, so nothing changes the hash while it is walked. | ||
| 60 | 829 | void MapTileSource::abortAll(const QStringList &tiles) { | |
| 61 | 829 | QList<QPointer<QNetworkReply>> replies; | |
| 62 |
1/2✗ Branch 10 → 4 not taken.
✓ Branch 10 → 11 taken 829 times.
|
829 | for (const QString &tile : tiles) |
| 63 | ✗ | replies.append(m_active.take(tile)); | |
| 64 |
1/2✗ Branch 25 → 14 not taken.
✓ Branch 25 → 26 taken 829 times.
|
829 | for (const QPointer<QNetworkReply> &reply : std::as_const(replies)) |
| 65 | ✗ | if (reply) { | |
| 66 | ✗ | reply->disconnect(this); | |
| 67 | ✗ | reply->abort(); | |
| 68 | ✗ | reply->deleteLater(); | |
| 69 | } | ||
| 70 | 829 | } | |
| 71 | |||
| 72 | 1644 | QString MapTileSource::defaultTemplate() { return QStringLiteral("https://tile.openstreetmap.org/{z}/{x}/{y}.png"); } | |
| 73 | |||
| 74 | 854 | QString MapTileSource::validate(const QString &templateUrl) { | |
| 75 |
1/1✓ Branch 2 → 3 taken 854 times.
|
854 | const QString text = templateUrl.trimmed(); |
| 76 |
9/11✓ Branch 4 → 5 taken 854 times.
✓ Branch 5 → 6 taken 854 times.
✗ Branch 5 → 12 not taken.
✓ Branch 7 → 8 taken 854 times.
✓ Branch 8 → 9 taken 854 times.
✗ Branch 8 → 12 not taken.
✓ Branch 10 → 11 taken 854 times.
✓ Branch 11 → 12 taken 1 time.
✓ Branch 11 → 13 taken 853 times.
✓ Branch 14 → 15 taken 1 time.
✓ Branch 14 → 17 taken 853 times.
|
854 | if (!text.contains(QLatin1String("{z}")) || !text.contains(QLatin1String("{x}")) || !text.contains(QLatin1String("{y}"))) |
| 77 |
1/1✓ Branch 15 → 16 taken 1 time.
|
1 | return tr("The address needs {z}, {x} and {y}."); |
| 78 | // S106-313: only {z}, {x} and {y} are filled in; another placeholder such | ||
| 79 | // as {s} (a subdomain) would make every tile request fail. | ||
| 80 |
5/5✓ Branch 19 → 20 taken 853 times.
✓ Branch 21 → 22 taken 853 times.
✓ Branch 23 → 24 taken 853 times.
✓ Branch 28 → 29 taken 1 time.
✓ Branch 28 → 31 taken 852 times.
|
1706 | if (QString(text).remove(QLatin1String("{z}")).remove(QLatin1String("{x}")).remove(QLatin1String("{y}")) |
| 81 |
1/1✓ Branch 26 → 27 taken 853 times.
|
853 | .contains(QLatin1Char('{'))) |
| 82 |
1/1✓ Branch 29 → 30 taken 1 time.
|
1 | return tr("Only {z}, {x} and {y} can be filled in; write the server name without other placeholders."); |
| 83 |
1/1✓ Branch 34 → 35 taken 852 times.
|
852 | const QUrl url(QString(text).replace(QLatin1String("{z}"), QLatin1String("0")) |
| 84 |
1/1✓ Branch 37 → 38 taken 852 times.
|
852 | .replace(QLatin1String("{x}"), QLatin1String("0")) |
| 85 |
2/2✓ Branch 40 → 41 taken 852 times.
✓ Branch 41 → 42 taken 852 times.
|
852 | .replace(QLatin1String("{y}"), QLatin1String("0"))); |
| 86 |
6/12✓ Branch 43 → 44 taken 852 times.
✓ Branch 44 → 45 taken 852 times.
✗ Branch 44 → 49 not taken.
✓ Branch 46 → 47 taken 852 times.
✗ Branch 48 → 49 not taken.
✓ Branch 48 → 50 taken 852 times.
✓ Branch 51 → 52 taken 852 times.
✗ Branch 51 → 53 not taken.
✗ Branch 53 → 54 not taken.
✓ Branch 53 → 62 taken 852 times.
✗ Branch 117 → 118 not taken.
✗ Branch 117 → 119 not taken.
|
1704 | if (!url.isValid() || url.host().isEmpty()) |
| 87 | ✗ | return tr("Enter a web address such as %1.").arg(defaultTemplate()); | |
| 88 |
16/24✓ Branch 63 → 64 taken 852 times.
✓ Branch 65 → 66 taken 12 times.
✓ Branch 65 → 75 taken 840 times.
✓ Branch 67 → 68 taken 12 times.
✓ Branch 69 → 70 taken 12 times.
✗ Branch 69 → 74 not taken.
✓ Branch 71 → 72 taken 12 times.
✓ Branch 72 → 73 taken 12 times.
✓ Branch 73 → 74 taken 1 time.
✓ Branch 73 → 75 taken 11 times.
✓ Branch 76 → 77 taken 12 times.
✓ Branch 76 → 78 taken 840 times.
✓ Branch 78 → 79 taken 12 times.
✓ Branch 78 → 80 taken 840 times.
✓ Branch 80 → 81 taken 852 times.
✗ Branch 80 → 82 not taken.
✓ Branch 82 → 83 taken 1 time.
✓ Branch 82 → 85 taken 851 times.
✗ Branch 129 → 130 not taken.
✗ Branch 129 → 131 not taken.
✗ Branch 134 → 135 not taken.
✗ Branch 134 → 136 not taken.
✗ Branch 139 → 140 not taken.
✗ Branch 139 → 141 not taken.
|
864 | if (url.scheme() != QLatin1String("https") && !(url.scheme() == QLatin1String("http") && loopback(url.host()))) |
| 89 |
1/1✓ Branch 83 → 84 taken 1 time.
|
1 | return tr("Only secure (https) addresses are allowed."); |
| 90 |
3/3✓ Branch 86 → 87 taken 851 times.
✓ Branch 89 → 90 taken 1 time.
✓ Branch 89 → 92 taken 850 times.
|
851 | if (!url.userInfo().isEmpty()) |
| 91 |
1/1✓ Branch 90 → 91 taken 1 time.
|
1 | return tr("The address must not contain a user name or password."); |
| 92 | 850 | return {}; | |
| 93 | 854 | } | |
| 94 | |||
| 95 | 1666 | void MapTileSource::setEnabled(bool enabled) { | |
| 96 |
2/2✓ Branch 2 → 3 taken 1651 times.
✓ Branch 2 → 4 taken 15 times.
|
1666 | if (enabled == m_enabled) |
| 97 | 1651 | return; | |
| 98 | 15 | m_enabled = enabled; | |
| 99 |
8/8✓ Branch 4 → 5 taken 15 times.
✓ Branch 8 → 9 taken 15 times.
✓ Branch 9 → 10 taken 15 times.
✓ Branch 10 → 11 taken 9 times.
✓ Branch 10 → 12 taken 6 times.
✓ Branch 13 → 14 taken 15 times.
✓ Branch 17 → 6 taken 15 times.
✓ Branch 17 → 18 taken 15 times.
|
30 | qCInfo(lcMapTiles) << "Online tiles" << (enabled ? "on" : "off"); |
| 100 |
2/2✓ Branch 18 → 19 taken 6 times.
✓ Branch 18 → 24 taken 9 times.
|
15 | if (!enabled) { |
| 101 | 6 | m_wanted.clear(); | |
| 102 |
2/2✓ Branch 20 → 21 taken 6 times.
✓ Branch 21 → 22 taken 6 times.
|
6 | abortAll(m_active.keys()); |
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | 843 | void MapTileSource::setTemplate(const QString &templateUrl) { | |
| 107 |
3/5✓ Branch 2 → 3 taken 843 times.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 843 times.
✗ Branch 5 → 7 not taken.
✓ Branch 6 → 7 taken 843 times.
|
843 | const QString text = templateUrl.trimmed().isEmpty() ? defaultTemplate() : templateUrl.trimmed(); |
| 108 |
7/11✓ Branch 8 → 9 taken 843 times.
✓ Branch 10 → 11 taken 843 times.
✗ Branch 10 → 13 not taken.
✓ Branch 12 → 13 taken 810 times.
✓ Branch 12 → 14 taken 33 times.
✓ Branch 15 → 16 taken 843 times.
✗ Branch 15 → 17 not taken.
✓ Branch 17 → 18 taken 810 times.
✓ Branch 17 → 19 taken 33 times.
✗ Branch 31 → 32 not taken.
✗ Branch 31 → 33 not taken.
|
843 | if (!validate(text).isEmpty() || text == m_template) |
| 109 | 810 | return; | |
| 110 | 33 | m_template = text; | |
| 111 | 33 | m_failed.clear(); | |
| 112 | 843 | } | |
| 113 | |||
| 114 | 5690 | QDir MapTileSource::serverDir() const { | |
| 115 | // One folder per server, so tiles of two servers never mix. | ||
| 116 |
4/4✓ Branch 2 → 3 taken 5690 times.
✓ Branch 4 → 5 taken 5690 times.
✓ Branch 5 → 6 taken 5690 times.
✓ Branch 6 → 7 taken 5690 times.
|
5690 | const QByteArray hash = QCryptographicHash::hash(m_template.toUtf8(), QCryptographicHash::Sha256).toHex().left(16); |
| 117 |
3/3✓ Branch 10 → 11 taken 5690 times.
✓ Branch 11 → 12 taken 5690 times.
✓ Branch 12 → 13 taken 5690 times.
|
11380 | return QDir(m_cacheRoot.filePath(QString::fromLatin1(hash))); |
| 118 | 5690 | } | |
| 119 | |||
| 120 |
3/3✓ Branch 2 → 3 taken 5690 times.
✓ Branch 6 → 7 taken 5690 times.
✓ Branch 7 → 8 taken 5690 times.
|
11380 | QString MapTileSource::fileOf(const QString &tile) const { return serverDir().filePath(tile + QStringLiteral(".tile")); } |
| 121 | |||
| 122 | 5379 | QString MapTileSource::cachedFile(int zoom, int x, int y) const { | |
| 123 |
4/4✓ Branch 6 → 7 taken 5379 times.
✓ Branch 8 → 9 taken 5379 times.
✓ Branch 10 → 11 taken 5379 times.
✓ Branch 11 → 12 taken 5379 times.
|
21516 | const QString file = fileOf(QStringLiteral("%1/%2/%3").arg(zoom).arg(x).arg(y)); |
| 124 |
3/3✓ Branch 17 → 18 taken 5379 times.
✓ Branch 18 → 19 taken 540 times.
✓ Branch 18 → 20 taken 4839 times.
|
10758 | return QFileInfo::exists(file) ? file : QString(); |
| 125 | 5379 | } | |
| 126 | |||
| 127 | 181 | bool MapTileSource::fresh(const QString &tile) const { | |
| 128 |
2/2✓ Branch 2 → 3 taken 181 times.
✓ Branch 3 → 4 taken 181 times.
|
181 | const QFileInfo info(fileOf(tile)); |
| 129 |
4/16✓ Branch 5 → 6 taken 181 times.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 12 taken 181 times.
✗ Branch 7 → 8 not taken.
✗ Branch 8 → 9 not taken.
✗ Branch 9 → 10 not taken.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 16 taken 181 times.
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 181 times.
✗ Branch 24 → 25 not taken.
✗ Branch 24 → 26 not taken.
✗ Branch 28 → 29 not taken.
✗ Branch 28 → 30 not taken.
|
362 | return info.exists() && info.lastModified().daysTo(QDateTime::currentDateTime()) < RefreshDays; |
| 130 | 181 | } | |
| 131 | |||
| 132 | 10 | void MapTileSource::want(const QStringList &tiles) { | |
| 133 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 10 times.
|
10 | if (!m_enabled) |
| 134 | ✗ | return; | |
| 135 | 10 | QStringList wanted; | |
| 136 |
2/2✓ Branch 19 → 6 taken 99 times.
✓ Branch 19 → 20 taken 10 times.
|
109 | for (const QString &tile : tiles) { |
| 137 | 99 | int zoom = 0, x = 0, y = 0; | |
| 138 |
8/10✓ Branch 7 → 8 taken 99 times.
✓ Branch 8 → 9 taken 99 times.
✗ Branch 8 → 14 not taken.
✓ Branch 9 → 10 taken 99 times.
✓ Branch 10 → 11 taken 99 times.
✗ Branch 10 → 14 not taken.
✓ Branch 12 → 13 taken 83 times.
✓ Branch 12 → 14 taken 16 times.
✓ Branch 15 → 16 taken 83 times.
✓ Branch 15 → 17 taken 16 times.
|
99 | if (parseTile(tile, &zoom, &x, &y) && !fresh(tile) && !wanted.contains(tile)) |
| 139 |
1/1✓ Branch 16 → 17 taken 83 times.
|
83 | wanted.append(tile); |
| 140 | } | ||
| 141 | 10 | m_wanted = wanted; | |
| 142 | // A pan or zoom away: transfers of tiles no longer in view stop (policy: no prefetching). | ||
| 143 | 10 | QStringList gone; | |
| 144 |
1/2✗ Branch 30 → 22 not taken.
✓ Branch 30 → 31 taken 10 times.
|
10 | for (auto it = m_active.cbegin(); it != m_active.cend(); ++it) |
| 145 | ✗ | if (!m_wanted.contains(it.key())) | |
| 146 | ✗ | gone.append(it.key()); | |
| 147 |
1/1✓ Branch 31 → 32 taken 10 times.
|
10 | abortAll(gone); |
| 148 |
1/1✓ Branch 32 → 33 taken 10 times.
|
10 | pump(); |
| 149 | 10 | } | |
| 150 | |||
| 151 | 92 | void MapTileSource::pump() { | |
| 152 |
5/6✓ Branch 93 → 94 taken 174 times.
✗ Branch 93 → 97 not taken.
✓ Branch 95 → 96 taken 100 times.
✓ Branch 95 → 97 taken 74 times.
✓ Branch 98 → 3 taken 100 times.
✓ Branch 98 → 99 taken 74 times.
|
174 | while (m_enabled && m_active.size() < m_maxParallel) { |
| 153 | 100 | QString next; | |
| 154 | // A tile wanted while in flight stays on the list; once stored it is not fetched again. | ||
| 155 |
2/2✓ Branch 23 → 6 taken 83 times.
✓ Branch 23 → 24 taken 18 times.
|
101 | for (const QString &tile : std::as_const(m_wanted)) |
| 156 |
7/8✓ Branch 8 → 9 taken 83 times.
✗ Branch 8 → 17 not taken.
✓ Branch 10 → 11 taken 2 times.
✓ Branch 10 → 14 taken 81 times.
✓ Branch 13 → 14 taken 1 time.
✓ Branch 13 → 17 taken 1 time.
✓ Branch 18 → 19 taken 82 times.
✓ Branch 18 → 21 taken 1 time.
|
165 | if (!m_active.contains(tile) && !(m_failed.contains(tile) && !m_failed.value(tile).hasExpired()) && |
| 157 |
2/3✓ Branch 14 → 15 taken 82 times.
✓ Branch 15 → 16 taken 82 times.
✗ Branch 15 → 17 not taken.
|
82 | !fresh(tile)) { |
| 158 | 82 | next = tile; | |
| 159 | 82 | break; | |
| 160 | } | ||
| 161 |
2/2✓ Branch 25 → 26 taken 18 times.
✓ Branch 25 → 27 taken 82 times.
|
100 | if (next.isEmpty()) |
| 162 | 18 | return; | |
| 163 |
1/1✓ Branch 27 → 28 taken 82 times.
|
82 | m_wanted.removeAll(next); |
| 164 | 82 | int zoom = 0, x = 0, y = 0; | |
| 165 |
1/1✓ Branch 28 → 29 taken 82 times.
|
82 | parseTile(next, &zoom, &x, &y); |
| 166 | 82 | QString address = m_template; | |
| 167 |
2/2✓ Branch 30 → 31 taken 82 times.
✓ Branch 32 → 33 taken 82 times.
|
82 | address.replace(QLatin1String("{z}"), QString::number(zoom)) |
| 168 |
2/2✓ Branch 33 → 34 taken 82 times.
✓ Branch 35 → 36 taken 82 times.
|
164 | .replace(QLatin1String("{x}"), QString::number(x)) |
| 169 |
2/2✓ Branch 36 → 37 taken 82 times.
✓ Branch 38 → 39 taken 82 times.
|
82 | .replace(QLatin1String("{y}"), QString::number(y)); |
| 170 |
2/2✓ Branch 42 → 43 taken 82 times.
✓ Branch 43 → 44 taken 82 times.
|
82 | QNetworkRequest request{QUrl(address)}; |
| 171 | // OSM tile usage policy: identify the application. | ||
| 172 |
1/1✓ Branch 58 → 59 taken 82 times.
|
82 | request.setHeader(QNetworkRequest::UserAgentHeader, |
| 173 | 164 | QStringLiteral("MailJD/%1 (+https://git.jdsoft.de/jens/qmailjd)") | |
| 174 |
4/9✓ Branch 49 → 50 taken 82 times.
✓ Branch 51 → 52 taken 82 times.
✗ Branch 51 → 55 not taken.
✗ Branch 55 → 56 not taken.
✓ Branch 56 → 57 taken 82 times.
✓ Branch 62 → 63 taken 82 times.
✗ Branch 62 → 64 not taken.
✗ Branch 123 → 124 not taken.
✗ Branch 123 → 125 not taken.
|
246 | .arg(QCoreApplication::applicationVersion().isEmpty() ? QStringLiteral("dev") |
| 175 | : QCoreApplication::applicationVersion())); | ||
| 176 |
1/1✓ Branch 68 → 69 taken 82 times.
|
82 | request.setAttribute(QNetworkRequest::CookieSaveControlAttribute, QNetworkRequest::Manual); |
| 177 |
1/1✓ Branch 71 → 72 taken 82 times.
|
82 | request.setAttribute(QNetworkRequest::CookieLoadControlAttribute, QNetworkRequest::Manual); |
| 178 |
1/1✓ Branch 74 → 75 taken 82 times.
|
82 | request.setAttribute(QNetworkRequest::AuthenticationReuseAttribute, QNetworkRequest::Manual); |
| 179 |
1/1✓ Branch 76 → 77 taken 82 times.
|
82 | request.setTransferTimeout(15000); |
| 180 |
1/1✓ Branch 77 → 78 taken 82 times.
|
82 | QNetworkReply *reply = m_network->get(request); |
| 181 | 82 | ++m_requestsStarted; | |
| 182 |
2/2✓ Branch 78 → 79 taken 82 times.
✓ Branch 79 → 80 taken 82 times.
|
82 | m_active.insert(next, reply); |
| 183 |
1/1✓ Branch 82 → 83 taken 82 times.
|
164 | connect(reply, &QNetworkReply::finished, this, [this, reply, next] { finished(reply, next); }); |
| 184 | 100 | } | |
| 185 | } | ||
| 186 | |||
| 187 | 82 | void MapTileSource::finished(QNetworkReply *reply, const QString &tile) { | |
| 188 |
1/1✓ Branch 2 → 3 taken 82 times.
|
82 | reply->deleteLater(); |
| 189 |
1/2✓ Branch 6 → 7 taken 82 times.
✗ Branch 6 → 8 not taken.
|
82 | if (m_active.value(tile) == reply) |
| 190 |
1/1✓ Branch 7 → 8 taken 82 times.
|
82 | m_active.remove(tile); |
| 191 |
2/3✓ Branch 8 → 9 taken 82 times.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 12 taken 82 times.
|
82 | if (reply->error() == QNetworkReply::OperationCanceledError) { |
| 192 | ✗ | pump(); | |
| 193 | 17 | return; | |
| 194 | } | ||
| 195 |
2/2✓ Branch 12 → 13 taken 82 times.
✓ Branch 13 → 14 taken 82 times.
|
82 | const int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); |
| 196 |
4/5✓ Branch 15 → 16 taken 82 times.
✓ Branch 16 → 17 taken 81 times.
✓ Branch 16 → 20 taken 1 time.
✓ Branch 17 → 18 taken 81 times.
✗ Branch 17 → 20 not taken.
|
82 | const QByteArray bytes = reply->error() == QNetworkReply::NoError && status == 200 ? reply->read(MaxTileBytes + 1) |
| 197 |
1/1✓ Branch 18 → 19 taken 81 times.
|
82 | : QByteArray(); |
| 198 | 82 | QImage image; | |
| 199 |
5/6✓ Branch 23 → 24 taken 81 times.
✓ Branch 23 → 27 taken 1 time.
✓ Branch 25 → 26 taken 81 times.
✗ Branch 25 → 27 not taken.
✓ Branch 28 → 29 taken 81 times.
✓ Branch 28 → 49 taken 1 time.
|
82 | if (bytes.size() > 0 && bytes.size() <= MaxTileBytes) { |
| 200 |
1/1✓ Branch 29 → 30 taken 81 times.
|
81 | QBuffer buffer; |
| 201 |
1/1✓ Branch 30 → 31 taken 81 times.
|
81 | buffer.setData(bytes); |
| 202 |
1/1✓ Branch 32 → 33 taken 81 times.
|
81 | QImageReader reader(&buffer); |
| 203 |
7/8✓ Branch 35 → 36 taken 81 times.
✓ Branch 36 → 37 taken 81 times.
✗ Branch 36 → 40 not taken.
✓ Branch 37 → 38 taken 81 times.
✓ Branch 38 → 39 taken 65 times.
✓ Branch 38 → 40 taken 16 times.
✓ Branch 41 → 42 taken 65 times.
✓ Branch 41 → 46 taken 16 times.
|
81 | if (buffer.open(QIODevice::ReadOnly) && RasterImageFormat::accept(reader)) |
| 204 |
1/1✓ Branch 42 → 43 taken 65 times.
|
65 | image = reader.read(); |
| 205 | 81 | } | |
| 206 |
9/11✓ Branch 49 → 50 taken 82 times.
✓ Branch 50 → 51 taken 65 times.
✓ Branch 50 → 55 taken 17 times.
✓ Branch 51 → 52 taken 65 times.
✓ Branch 52 → 53 taken 65 times.
✗ Branch 52 → 55 not taken.
✓ Branch 53 → 54 taken 65 times.
✗ Branch 54 → 55 not taken.
✓ Branch 54 → 56 taken 65 times.
✓ Branch 57 → 58 taken 17 times.
✓ Branch 57 → 77 taken 65 times.
|
82 | if (image.isNull() || image.width() > 1024 || image.height() > 1024) { |
| 207 | // Offline, refused or not an image: the outline map stays; the tile is | ||
| 208 | // asked for again after RetryAfterMs. | ||
| 209 |
1/1✓ Branch 59 → 60 taken 17 times.
|
17 | m_failed.insert(tile, QDeadlineTimer(m_retryAfterMs)); |
| 210 |
8/8✓ Branch 60 → 61 taken 17 times.
✓ Branch 64 → 65 taken 17 times.
✓ Branch 65 → 66 taken 17 times.
✓ Branch 66 → 67 taken 17 times.
✓ Branch 67 → 68 taken 17 times.
✓ Branch 68 → 69 taken 17 times.
✓ Branch 72 → 62 taken 17 times.
✓ Branch 72 → 73 taken 17 times.
|
34 | qCInfo(lcMapTiles) << "Tile request failed:" << int(reply->error()) << status; |
| 211 |
2/2✓ Branch 73 → 74 taken 2 times.
✓ Branch 73 → 75 taken 15 times.
|
17 | if (!m_unavailable) { |
| 212 | 2 | m_unavailable = true; | |
| 213 |
1/1✓ Branch 74 → 75 taken 2 times.
|
2 | emit unavailableChanged(true); |
| 214 | } | ||
| 215 |
1/1✓ Branch 75 → 76 taken 17 times.
|
17 | pump(); |
| 216 | 17 | return; | |
| 217 | } | ||
| 218 |
5/5✓ Branch 78 → 79 taken 65 times.
✓ Branch 79 → 80 taken 65 times.
✓ Branch 80 → 81 taken 65 times.
✓ Branch 81 → 82 taken 65 times.
✓ Branch 82 → 83 taken 65 times.
|
65 | QDir().mkpath(QFileInfo(fileOf(tile)).absolutePath()); |
| 219 |
2/2✓ Branch 88 → 89 taken 65 times.
✓ Branch 89 → 90 taken 65 times.
|
65 | QSaveFile file(fileOf(tile)); |
| 220 |
7/11✓ Branch 92 → 93 taken 65 times.
✓ Branch 93 → 94 taken 65 times.
✗ Branch 93 → 100 not taken.
✓ Branch 94 → 95 taken 65 times.
✓ Branch 96 → 97 taken 65 times.
✗ Branch 96 → 100 not taken.
✓ Branch 97 → 98 taken 65 times.
✓ Branch 98 → 99 taken 65 times.
✗ Branch 98 → 100 not taken.
✓ Branch 101 → 102 taken 65 times.
✗ Branch 101 → 108 not taken.
|
65 | if (file.open(QIODevice::WriteOnly) && file.write(bytes) == bytes.size() && file.commit()) { |
| 221 |
1/1✓ Branch 102 → 103 taken 65 times.
|
65 | m_failed.remove(tile); |
| 222 |
2/2✓ Branch 103 → 104 taken 1 time.
✓ Branch 103 → 105 taken 64 times.
|
65 | if (m_unavailable) { |
| 223 | 1 | m_unavailable = false; | |
| 224 |
1/1✓ Branch 104 → 105 taken 1 time.
|
1 | emit unavailableChanged(false); |
| 225 | } | ||
| 226 |
1/1✓ Branch 105 → 106 taken 65 times.
|
65 | emit tileReady(tile); |
| 227 |
1/2✗ Branch 106 → 107 not taken.
✓ Branch 106 → 124 taken 65 times.
|
65 | if (++m_storedSinceTrim >= 200) { |
| 228 | ✗ | m_storedSinceTrim = 0; | |
| 229 | ✗ | trimCache(); | |
| 230 | } | ||
| 231 | } else { | ||
| 232 | // S106-355: a full disk or a read-only cache is logged, and the tile | ||
| 233 | // waits for the retry time like a failed fetch. | ||
| 234 | ✗ | qCWarning(lcMapTiles) << "Tile not stored in the cache:" << file.errorString(); | |
| 235 | ✗ | m_failed.insert(tile, QDeadlineTimer(m_retryAfterMs)); | |
| 236 | } | ||
| 237 |
1/1✓ Branch 124 → 125 taken 65 times.
|
65 | pump(); |
| 238 | 99 | } | |
| 239 | |||
| 240 | 27 | qint64 MapTileSource::cacheBytes() const { | |
| 241 | 27 | qint64 total = 0; | |
| 242 |
4/6✓ Branch 8 → 9 taken 27 times.
✓ Branch 9 → 10 taken 27 times.
✓ Branch 13 → 14 taken 27 times.
✓ Branch 13 → 15 taken 27 times.
✗ Branch 34 → 35 not taken.
✗ Branch 34 → 36 not taken.
|
81 | QDirIterator it(m_cacheRoot.absolutePath(), {QStringLiteral("*.tile")}, QDir::Files, QDirIterator::Subdirectories); |
| 243 |
3/3✓ Branch 23 → 24 taken 48 times.
✓ Branch 24 → 17 taken 21 times.
✓ Branch 24 → 25 taken 27 times.
|
48 | while (it.hasNext()) { |
| 244 |
1/1✓ Branch 17 → 18 taken 21 times.
|
21 | it.next(); |
| 245 |
2/2✓ Branch 19 → 20 taken 21 times.
✓ Branch 20 → 21 taken 21 times.
|
21 | total += it.fileInfo().size(); |
| 246 | } | ||
| 247 | 27 | return total; | |
| 248 | 81 | } | |
| 249 | |||
| 250 | // The oldest tiles go first; a tile younger than 7 days is kept (policy). | ||
| 251 | ✗ | void MapTileSource::trimCache() { | |
| 252 | ✗ | QList<QFileInfo> files; | |
| 253 | ✗ | qint64 total = 0; | |
| 254 | ✗ | QDirIterator it(m_cacheRoot.absolutePath(), {QStringLiteral("*.tile")}, QDir::Files, QDirIterator::Subdirectories); | |
| 255 | ✗ | while (it.hasNext()) { | |
| 256 | ✗ | it.next(); | |
| 257 | ✗ | files.append(it.fileInfo()); | |
| 258 | ✗ | total += it.fileInfo().size(); | |
| 259 | } | ||
| 260 | ✗ | if (total <= m_maxCacheBytes) | |
| 261 | ✗ | return; | |
| 262 | ✗ | std::sort(files.begin(), files.end(), | |
| 263 | ✗ | [](const QFileInfo &a, const QFileInfo &b) { return a.lastModified() < b.lastModified(); }); | |
| 264 | ✗ | const QDateTime keep = QDateTime::currentDateTime().addDays(-7); | |
| 265 | ✗ | int removed = 0; | |
| 266 | ✗ | for (const QFileInfo &info : std::as_const(files)) { | |
| 267 | ✗ | if (total <= m_maxCacheBytes || info.lastModified() > keep) | |
| 268 | ✗ | break; | |
| 269 | // S106-248: only a removed file frees its bytes; a file that could not | ||
| 270 | // be removed is skipped, the next older one goes instead. | ||
| 271 | ✗ | if (!QFile::remove(info.absoluteFilePath())) continue; | |
| 272 | ✗ | total -= info.size(); | |
| 273 | ✗ | ++removed; | |
| 274 | } | ||
| 275 | ✗ | qCInfo(lcMapTiles) << "Tile cache trimmed:" << removed << "files," << total << "bytes left"; | |
| 276 | ✗ | } | |
| 277 | |||
| 278 | 7 | void MapTileSource::clearCache() { | |
| 279 |
3/3✓ Branch 2 → 3 taken 7 times.
✓ Branch 3 → 4 taken 7 times.
✓ Branch 4 → 5 taken 7 times.
|
7 | QDir(m_cacheRoot.absolutePath()).removeRecursively(); |
| 280 | 7 | m_failed.clear(); | |
| 281 |
5/5✓ Branch 8 → 9 taken 7 times.
✓ Branch 12 → 13 taken 7 times.
✓ Branch 13 → 14 taken 7 times.
✓ Branch 17 → 10 taken 7 times.
✓ Branch 17 → 18 taken 7 times.
|
14 | qCInfo(lcMapTiles) << "Tile cache cleared"; |
| 282 | 7 | } | |
| 283 |