presentation/QuickContactMapLookup.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // The online address lookup of QuickContactMap (Sprint 103 S103.1.9, D-10): | ||
| 2 | // settings, the background lookups while the map is in use (setting on) and | ||
| 3 | // a card's single *Look up exactly…*. PlaceLookupSource does the requests | ||
| 4 | // (1 per second, the address text only); ContactPlaceLookup stores the | ||
| 5 | // answers locally (place_cache, card_places) — never in the card. | ||
| 6 | |||
| 7 | #include "QuickContactMap.h" | ||
| 8 | |||
| 9 | #include "data/ContactQueries.h" | ||
| 10 | #include "data/ContactStore.h" | ||
| 11 | |||
| 12 | #include <QLoggingCategory> | ||
| 13 | #include <QSettings> | ||
| 14 | #include <QSqlQuery> | ||
| 15 | #include <QTimer> | ||
| 16 | #include <QUrl> | ||
| 17 | #include <utility> | ||
| 18 | |||
| 19 | Q_DECLARE_LOGGING_CATEGORY(lcMapTiles) | ||
| 20 | |||
| 21 | namespace { | ||
| 22 | |||
| 23 | // Addresses per background pass; the next pass follows the next placement. | ||
| 24 | constexpr int LookupBatch = 60; | ||
| 25 | |||
| 26 | } // namespace | ||
| 27 | |||
| 28 | 812 | void QuickContactMap::initLookup() { | |
| 29 |
3/6✓ Branch 2 → 3 taken 812 times.
✓ Branch 3 → 4 taken 812 times.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 812 times.
✗ Branch 41 → 42 not taken.
✗ Branch 41 → 43 not taken.
|
812 | m_lookupSource = new PlaceLookupSource(this); |
| 30 |
1/1✓ Branch 6 → 7 taken 812 times.
|
812 | const QSettings settings; |
| 31 |
3/3✓ Branch 11 → 12 taken 812 times.
✓ Branch 12 → 13 taken 812 times.
✓ Branch 13 → 14 taken 812 times.
|
1624 | m_lookupSource->setServer(settings.value(QStringLiteral("contacts/placeLookupServer")).toString()); |
| 32 |
2/2✓ Branch 23 → 24 taken 812 times.
✓ Branch 24 → 25 taken 812 times.
|
1624 | m_onlineLookup = settings.value(QStringLiteral("contacts/placeLookup"), false).toBool(); |
| 33 |
3/6✓ Branch 29 → 30 taken 812 times.
✓ Branch 30 → 31 taken 812 times.
✗ Branch 31 → 32 not taken.
✓ Branch 31 → 33 taken 812 times.
✗ Branch 70 → 71 not taken.
✗ Branch 70 → 72 not taken.
|
812 | m_lookupReread = new QTimer(this); |
| 34 |
1/1✓ Branch 33 → 34 taken 812 times.
|
812 | m_lookupReread->setSingleShot(true); |
| 35 |
1/1✓ Branch 34 → 35 taken 812 times.
|
812 | m_lookupReread->setInterval(500); |
| 36 |
1/1✓ Branch 35 → 36 taken 812 times.
|
812 | connect(m_lookupReread, &QTimer::timeout, this, [this] { |
| 37 |
1/2✓ Branch 2 → 3 taken 2 times.
✗ Branch 2 → 4 not taken.
|
2 | if (m_active) |
| 38 | 2 | readPlaces(); | |
| 39 | 2 | }); | |
| 40 |
1/1✓ Branch 37 → 38 taken 812 times.
|
812 | connect(m_lookupSource, &PlaceLookupSource::finished, this, &QuickContactMap::lookupAnswered); |
| 41 | 812 | } | |
| 42 | |||
| 43 | 7 | void QuickContactMap::setOnlineLookup(bool on) { | |
| 44 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 7 times.
|
7 | if (on == m_onlineLookup) |
| 45 | ✗ | return; | |
| 46 | 7 | m_onlineLookup = on; | |
| 47 |
2/2✓ Branch 4 → 5 taken 7 times.
✓ Branch 10 → 11 taken 7 times.
|
14 | QSettings().setValue(QStringLiteral("contacts/placeLookup"), on); |
| 48 |
8/8✓ Branch 15 → 16 taken 7 times.
✓ Branch 19 → 20 taken 7 times.
✓ Branch 20 → 21 taken 7 times.
✓ Branch 21 → 22 taken 4 times.
✓ Branch 21 → 23 taken 3 times.
✓ Branch 24 → 25 taken 7 times.
✓ Branch 28 → 17 taken 7 times.
✓ Branch 28 → 29 taken 7 times.
|
14 | qCInfo(lcMapTiles) << "Online address lookup" << (on ? "on" : "off"); |
| 49 |
2/2✓ Branch 29 → 30 taken 4 times.
✓ Branch 29 → 31 taken 3 times.
|
7 | if (on) { |
| 50 | 4 | queueLookups(); | |
| 51 | } else { | ||
| 52 | // Background lookups stop, also those already handed to the source | ||
| 53 | // (S106-284); a confirmed card lookup stays. | ||
| 54 | 3 | stopBackgroundLookups(); | |
| 55 | } | ||
| 56 | 7 | emit lookupSettingsChanged(); | |
| 57 | 7 | emit lookupPendingChanged(); | |
| 58 | } | ||
| 59 | |||
| 60 | 17 | QString QuickContactMap::lookupServer() const { return m_lookupSource->server(); } | |
| 61 | |||
| 62 | 5 | void QuickContactMap::setLookupServer(const QString &server) { | |
| 63 |
3/5✓ Branch 2 → 3 taken 5 times.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 5 times.
✗ Branch 5 → 7 not taken.
✓ Branch 6 → 7 taken 5 times.
|
5 | const QString text = server.trimmed().isEmpty() ? PlaceLookupSource::defaultServer() : server.trimmed(); |
| 64 |
2/3✓ Branch 8 → 9 taken 5 times.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 5 times.
|
5 | if (!PlaceLookupSource::validate(text).isEmpty()) |
| 65 | ✗ | return; | |
| 66 |
2/2✓ Branch 16 → 17 taken 4 times.
✓ Branch 16 → 51 taken 1 time.
|
5 | if (text != m_lookupSource->server()) { |
| 67 | // S106-284: nothing asked for the old server goes to the new one; a | ||
| 68 | // confirmed card lookup ends unanswered (the card offers it again). | ||
| 69 |
1/1✓ Branch 17 → 18 taken 4 times.
|
4 | m_lookupSource->cancelAll(); |
| 70 | 4 | const QHash<QString, LookupRequest> dropped = std::exchange(m_lookups, {}); | |
| 71 |
2/2✓ Branch 32 → 23 taken 1 time.
✓ Branch 32 → 33 taken 4 times.
|
5 | for (const LookupRequest &request : dropped) |
| 72 |
1/2✓ Branch 25 → 26 taken 1 time.
✗ Branch 25 → 30 not taken.
|
1 | if (!request.cardKey.isEmpty()) |
| 73 |
1/1✓ Branch 27 → 28 taken 1 time.
|
1 | emit addressLookedUp(request.cardKey, false, true, QString()); |
| 74 | 4 | m_lookupFailed.clear(); | |
| 75 | 4 | m_lookupPause = QDeadlineTimer(); | |
| 76 |
7/7✓ Branch 35 → 36 taken 4 times.
✓ Branch 39 → 40 taken 4 times.
✓ Branch 40 → 41 taken 4 times.
✓ Branch 42 → 43 taken 4 times.
✓ Branch 43 → 44 taken 4 times.
✓ Branch 47 → 37 taken 4 times.
✓ Branch 47 → 48 taken 4 times.
|
8 | qCInfo(lcMapTiles) << "Lookup server changed:" << dropped.size() << "lookups dropped"; |
| 77 |
1/1✓ Branch 48 → 49 taken 4 times.
|
4 | emit lookupPendingChanged(); |
| 78 | 4 | } | |
| 79 |
1/1✓ Branch 51 → 52 taken 5 times.
|
5 | m_lookupSource->setServer(text); |
| 80 |
2/2✓ Branch 52 → 53 taken 5 times.
✓ Branch 63 → 64 taken 5 times.
|
15 | QSettings().setValue(QStringLiteral("contacts/placeLookupServer"), |
| 81 |
2/3✓ Branch 53 → 54 taken 5 times.
✗ Branch 55 → 56 not taken.
✓ Branch 55 → 57 taken 5 times.
|
10 | text == PlaceLookupSource::defaultServer() ? QString() : text); |
| 82 |
1/1✓ Branch 70 → 71 taken 5 times.
|
5 | emit lookupSettingsChanged(); |
| 83 | 5 | } | |
| 84 | |||
| 85 |
2/2✓ Branch 3 → 4 taken 2 times.
✓ Branch 5 → 6 taken 2 times.
|
4 | QString QuickContactMap::lookupServerHost() const { return QUrl(m_lookupSource->server()).host(); } |
| 86 | |||
| 87 | 18 | QString QuickContactMap::lookupExample() const { | |
| 88 | 18 | PlaceLookupSource::Query sample; | |
| 89 | 18 | sample.street = QStringLiteral("Main Street 1"); | |
| 90 | 18 | sample.postalCode = QStringLiteral("10115"); | |
| 91 | 18 | sample.locality = QStringLiteral("Berlin"); | |
| 92 | 18 | sample.country = QStringLiteral("Germany"); | |
| 93 |
2/2✓ Branch 27 → 28 taken 18 times.
✓ Branch 29 → 30 taken 18 times.
|
36 | return PlaceLookupSource::requestUrl(m_lookupSource->server(), sample).toString(QUrl::PrettyDecoded); |
| 94 | 18 | } | |
| 95 | |||
| 96 | 9 | int QuickContactMap::lookupPending() const { return m_lookupSource->queued(); } | |
| 97 | |||
| 98 | ✗ | QString QuickContactMap::checkLookupServer(const QString &server) const { | |
| 99 | ✗ | return server.trimmed().isEmpty() ? QString() : PlaceLookupSource::validate(server); | |
| 100 | } | ||
| 101 | |||
| 102 | 26 | void QuickContactMap::stopBackgroundLookups() { | |
| 103 |
3/3✓ Branch 2 → 3 taken 26 times.
✓ Branch 19 → 4 taken 4 times.
✓ Branch 19 → 20 taken 26 times.
|
30 | for (auto it = m_lookups.begin(); it != m_lookups.end();) |
| 104 |
3/3✓ Branch 6 → 7 taken 3 times.
✓ Branch 6 → 9 taken 1 time.
✓ Branch 8 → 16 taken 3 times.
|
5 | it = it->cardKey.isEmpty() ? m_lookups.erase(it) : std::next(it); |
| 105 |
1/1✓ Branch 23 → 24 taken 26 times.
|
52 | m_lookupSource->cancelMatching(QStringLiteral("auto:")); |
| 106 | 26 | emit lookupPendingChanged(); | |
| 107 | 26 | } | |
| 108 | |||
| 109 | // The street addresses placed only roughly, while the map is in use. | ||
| 110 | 26 | void QuickContactMap::queueLookups() { | |
| 111 |
7/10✓ Branch 2 → 3 taken 8 times.
✓ Branch 2 → 8 taken 18 times.
✓ Branch 3 → 4 taken 8 times.
✗ Branch 3 → 8 not taken.
✓ Branch 5 → 6 taken 8 times.
✗ Branch 5 → 8 not taken.
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 8 times.
✓ Branch 10 → 11 taken 18 times.
✓ Branch 10 → 12 taken 8 times.
|
26 | if (!m_onlineLookup || !m_active || m_databasePath.isEmpty() || !m_lookups.isEmpty()) |
| 112 | 19 | return; | |
| 113 | // S106-283: after a batch without any answer (offline, refused) nothing | ||
| 114 | // is asked until the pause ends; the next reload or placement pass after | ||
| 115 | // it starts again. | ||
| 116 |
2/2✓ Branch 13 → 14 taken 1 time.
✓ Branch 13 → 15 taken 7 times.
|
8 | if (!m_lookupPause.hasExpired()) |
| 117 | 1 | return; | |
| 118 |
1/1✓ Branch 15 → 16 taken 7 times.
|
9 | m_lookupFailed.removeIf([](const QHash<QString, QDeadlineTimer>::iterator &it) { return it.value().hasExpired(); }); |
| 119 | 7 | QSet<QString> skip; | |
| 120 |
1/2✗ Branch 23 → 18 not taken.
✓ Branch 23 → 24 taken 7 times.
|
7 | for (auto it = m_lookupFailed.cbegin(); it != m_lookupFailed.cend(); ++it) |
| 121 | ✗ | skip.insert(it.key()); | |
| 122 |
1/1✓ Branch 24 → 25 taken 7 times.
|
7 | ContactStore store; |
| 123 |
2/3✓ Branch 25 → 26 taken 7 times.
✗ Branch 26 → 27 not taken.
✓ Branch 26 → 28 taken 7 times.
|
7 | if (!store.open(m_databasePath)) |
| 124 | ✗ | return; | |
| 125 |
1/1✓ Branch 28 → 29 taken 7 times.
|
7 | QSqlDatabase db = store.database(); |
| 126 |
1/1✓ Branch 29 → 30 taken 7 times.
|
7 | const QList<ContactPlaceLookup::Address> pending = ContactPlaceLookup::pending(db, LookupBatch, skip); |
| 127 | 7 | m_batchAnswered = false; | |
| 128 |
2/2✓ Branch 52 → 32 taken 9 times.
✓ Branch 52 → 53 taken 7 times.
|
16 | for (const ContactPlaceLookup::Address &address : pending) { |
| 129 |
1/1✓ Branch 36 → 37 taken 9 times.
|
18 | const QString id = QStringLiteral("auto:") + address.cacheKey; |
| 130 | 9 | m_lookups.insert(id, {address, QString()}); | |
| 131 | 9 | m_lookupSource->lookup(id, {address.street, address.postalCode, address.locality, address.region, | |
| 132 | 9 | address.country, QString()}); | |
| 133 | 9 | } | |
| 134 |
6/6✓ Branch 53 → 54 taken 7 times.
✓ Branch 57 → 58 taken 7 times.
✓ Branch 58 → 59 taken 7 times.
✓ Branch 60 → 61 taken 7 times.
✓ Branch 64 → 55 taken 7 times.
✓ Branch 64 → 65 taken 7 times.
|
14 | qCInfo(lcMapTiles) << "Online address lookups queued:" << pending.size(); |
| 135 |
1/1✓ Branch 65 → 66 taken 7 times.
|
7 | emit lookupPendingChanged(); |
| 136 | 25 | } | |
| 137 | |||
| 138 | 4 | bool QuickContactMap::lookUpAddress(const QString &key, int index) { | |
| 139 | 4 | qint64 bookId = 0; | |
| 140 | 4 | QString uid; | |
| 141 |
4/7✓ Branch 3 → 4 taken 4 times.
✗ Branch 3 → 6 not taken.
✓ Branch 4 → 5 taken 4 times.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 4 times.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 4 times.
|
4 | if (m_databasePath.isEmpty() || !ContactQueries::splitKey(key, &bookId, &uid)) |
| 142 | ✗ | return false; | |
| 143 |
1/1✓ Branch 10 → 11 taken 4 times.
|
4 | ContactStore store; |
| 144 |
2/3✓ Branch 11 → 12 taken 4 times.
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 4 times.
|
4 | if (!store.open(m_databasePath)) |
| 145 | ✗ | return false; | |
| 146 |
1/1✓ Branch 14 → 15 taken 4 times.
|
4 | QSqlDatabase db = store.database(); |
| 147 |
1/1✓ Branch 15 → 16 taken 4 times.
|
4 | QSqlQuery card(db); |
| 148 |
1/1✓ Branch 19 → 20 taken 4 times.
|
4 | card.prepare(QStringLiteral("SELECT id FROM cards WHERE book_id = ? AND uid = ?")); |
| 149 |
1/1✓ Branch 24 → 25 taken 4 times.
|
4 | card.addBindValue(bookId); |
| 150 |
1/1✓ Branch 28 → 29 taken 4 times.
|
4 | card.addBindValue(uid); |
| 151 |
5/8✓ Branch 30 → 31 taken 4 times.
✓ Branch 31 → 32 taken 4 times.
✗ Branch 31 → 34 not taken.
✓ Branch 32 → 33 taken 4 times.
✗ Branch 33 → 34 not taken.
✓ Branch 33 → 35 taken 4 times.
✗ Branch 36 → 37 not taken.
✓ Branch 36 → 38 taken 4 times.
|
4 | if (!card.exec() || !card.next()) |
| 152 | ✗ | return false; | |
| 153 |
3/3✓ Branch 38 → 39 taken 4 times.
✓ Branch 39 → 40 taken 4 times.
✓ Branch 40 → 41 taken 4 times.
|
4 | const ContactPlaceLookup::Address address = ContactPlaceLookup::forCard(db, card.value(0).toLongLong(), index); |
| 154 |
2/2✓ Branch 43 → 44 taken 1 time.
✓ Branch 43 → 45 taken 3 times.
|
4 | if (address.cacheKey.isEmpty()) |
| 155 | 1 | return false; | |
| 156 |
4/4✓ Branch 45 → 46 taken 3 times.
✓ Branch 51 → 52 taken 3 times.
✓ Branch 52 → 53 taken 3 times.
✓ Branch 53 → 54 taken 3 times.
|
6 | const QString id = QStringLiteral("card:") + key + QLatin1Char('\n') + QString::number(index); |
| 157 | 3 | m_lookups.insert(id, {address, key}); | |
| 158 | 3 | m_lookupSource->lookup(id, {address.street, address.postalCode, address.locality, address.region, address.country, | |
| 159 | QString()}); | ||
| 160 |
5/5✓ Branch 70 → 71 taken 3 times.
✓ Branch 74 → 75 taken 3 times.
✓ Branch 75 → 76 taken 3 times.
✓ Branch 79 → 72 taken 3 times.
✓ Branch 79 → 80 taken 3 times.
|
6 | qCInfo(lcMapTiles) << "Online address lookup of one card address requested"; |
| 161 |
1/1✓ Branch 80 → 81 taken 3 times.
|
3 | emit lookupPendingChanged(); |
| 162 | 3 | return true; | |
| 163 | 10 | } | |
| 164 | |||
| 165 | 7 | void QuickContactMap::lookupAnswered(const QString &id, const PlaceLookupSource::Result &result) { | |
| 166 |
1/1✓ Branch 2 → 3 taken 7 times.
|
7 | const LookupRequest request = m_lookups.take(id); |
| 167 |
1/1✓ Branch 3 → 4 taken 7 times.
|
7 | emit lookupPendingChanged(); |
| 168 |
1/2✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 7 times.
|
7 | if (request.address.cacheKey.isEmpty()) |
| 169 | ✗ | return; | |
| 170 | 7 | const bool background = request.cardKey.isEmpty(); | |
| 171 |
4/4✓ Branch 8 → 9 taken 5 times.
✓ Branch 8 → 13 taken 2 times.
✓ Branch 9 → 10 taken 2 times.
✓ Branch 9 → 13 taken 3 times.
|
7 | if (background && !result.answered) |
| 172 |
1/1✓ Branch 11 → 12 taken 2 times.
|
2 | m_lookupFailed.insert(request.address.cacheKey, QDeadlineTimer(m_lookupRetryMs)); |
| 173 |
4/4✓ Branch 13 → 14 taken 5 times.
✓ Branch 13 → 16 taken 2 times.
✓ Branch 14 → 15 taken 3 times.
✓ Branch 14 → 16 taken 2 times.
|
7 | if (background && result.answered) |
| 174 | 3 | m_batchAnswered = true; | |
| 175 |
2/2✓ Branch 16 → 17 taken 5 times.
✓ Branch 16 → 31 taken 2 times.
|
7 | if (result.answered) { |
| 176 |
1/1✓ Branch 17 → 18 taken 5 times.
|
5 | ContactStore store; |
| 177 |
2/3✓ Branch 18 → 19 taken 5 times.
✓ Branch 19 → 20 taken 5 times.
✗ Branch 19 → 29 not taken.
|
5 | if (store.open(m_databasePath)) { |
| 178 |
1/1✓ Branch 20 → 21 taken 5 times.
|
5 | QSqlDatabase db = store.database(); |
| 179 | 5 | ContactPlaceLookup::Answer answer; | |
| 180 | 5 | answer.found = result.found; | |
| 181 | 5 | answer.latitude = result.latitude; | |
| 182 | 5 | answer.longitude = result.longitude; | |
| 183 | 5 | answer.precision = result.precision; | |
| 184 | 5 | answer.placeName = result.placeName; | |
| 185 |
3/3✓ Branch 23 → 24 taken 5 times.
✓ Branch 24 → 25 taken 2 times.
✓ Branch 24 → 26 taken 3 times.
|
5 | if (ContactPlaceLookup::store(db, request.address, answer) > 0) |
| 186 |
1/1✓ Branch 25 → 26 taken 2 times.
|
2 | m_lookupReread->start(); |
| 187 | 5 | } | |
| 188 | 5 | } | |
| 189 |
2/2✓ Branch 32 → 33 taken 2 times.
✓ Branch 32 → 34 taken 5 times.
|
7 | if (!request.cardKey.isEmpty()) |
| 190 |
1/1✓ Branch 33 → 34 taken 2 times.
|
2 | emit addressLookedUp(request.cardKey, result.found, !result.answered, result.placeName); |
| 191 |
8/8✓ Branch 35 → 36 taken 4 times.
✓ Branch 35 → 39 taken 3 times.
✓ Branch 36 → 37 taken 2 times.
✓ Branch 36 → 39 taken 2 times.
✓ Branch 37 → 38 taken 1 time.
✓ Branch 37 → 39 taken 1 time.
✓ Branch 40 → 41 taken 1 time.
✓ Branch 40 → 53 taken 6 times.
|
7 | if (m_lookups.isEmpty() && background && !m_batchAnswered) { |
| 192 | 1 | m_lookupPause = QDeadlineTimer(m_lookupRetryMs); | |
| 193 |
5/5✓ Branch 42 → 43 taken 1 time.
✓ Branch 46 → 47 taken 1 time.
✓ Branch 47 → 48 taken 1 time.
✓ Branch 51 → 44 taken 1 time.
✓ Branch 51 → 52 taken 1 time.
|
2 | qCInfo(lcMapTiles) << "Online address lookups paused: no answer in the last batch"; |
| 194 | 1 | return; | |
| 195 | } | ||
| 196 |
2/2✓ Branch 54 → 55 taken 3 times.
✓ Branch 54 → 56 taken 3 times.
|
6 | if (m_lookups.isEmpty()) |
| 197 |
1/1✓ Branch 55 → 56 taken 3 times.
|
3 | queueLookups(); // the next batch, if any is left |
| 198 | 7 | } | |
| 199 |