Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
[libcontacts] Store the normalized number in cache. Contributes to JB…
…#38835
  • Loading branch information
Timur Kristóf committed Jan 29, 2020
1 parent 1283203 commit 2df678a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/seasidecache.cpp
Expand Up @@ -2209,16 +2209,18 @@ bool SeasideCache::updateContactIndexing(const QContact &oldContact, const QCont
resolveUnknownAddresses(address.first, address.second, item);
}

if (!m_phoneNumberIds.contains(address.second, iid))
m_phoneNumberIds.insert(address.second, iid);
CachedPhoneNumber cachedPhoneNumber(normalizePhoneNumber(phoneNumber.number()), iid);

if (!m_phoneNumberIds.contains(address.second, cachedPhoneNumber))
m_phoneNumberIds.insert(address.second, cachedPhoneNumber);
}
}

// Remove any addresses no longer available for this contact
if (!oldAddresses.isEmpty()) {
modified = true;
foreach (const StringPair &address, oldAddresses) {
m_phoneNumberIds.remove(address.second, iid);
m_phoneNumberIds.remove(address.second);
}
oldAddresses.clear();
}
Expand Down Expand Up @@ -3310,23 +3312,23 @@ void SeasideCache::resolveAddress(ResolveListener *listener, const QString &firs

SeasideCache::CacheItem *SeasideCache::itemMatchingPhoneNumber(const QString &number, const QString &normalized, bool requireComplete)
{
QMultiHash<QString, quint32>::const_iterator it = m_phoneNumberIds.find(number), end = m_phoneNumberIds.constEnd();
QMultiHash<QString, CachedPhoneNumber>::const_iterator it = m_phoneNumberIds.find(number), end = m_phoneNumberIds.constEnd();
if (it != end) {
// How many matches are there for this number?
int matchCount = 1;
QMultiHash<QString, quint32>::const_iterator matchingIt = it + 1;
QMultiHash<QString, CachedPhoneNumber>::const_iterator matchingIt = it + 1;
while ((matchingIt != end) && (matchingIt.key() == number)) {
++matchCount;
++matchingIt;
}
if (matchCount == 1)
return itemById(*it, requireComplete);
return itemById(it->iid, requireComplete);

// Choose the best match from these contacts
int bestMatchLength = 0;
CacheItem *matchItem = 0;
for ( ; matchCount > 0; ++it, --matchCount) {
if (CacheItem *item = existingItem(*it)) {
if (CacheItem *item = existingItem(it->iid)) {
int matchLength = bestPhoneNumberMatchLength(item->contact, normalized);
if (matchLength > bestMatchLength) {
bestMatchLength = matchLength;
Expand Down
17 changes: 16 additions & 1 deletion src/seasidecache.h
Expand Up @@ -139,6 +139,21 @@ class CONTACTCACHE_EXPORT SeasideCache : public QObject
void *key;
};

struct CachedPhoneNumber
{
CachedPhoneNumber() {}
CachedPhoneNumber(const QString &n, quint32 i) : normalizedNumber(n), iid(i) {}
CachedPhoneNumber(const CachedPhoneNumber &other) : normalizedNumber(other.normalizedNumber), iid(other.iid) {}

bool operator==(const CachedPhoneNumber &other) const
{
return other.normalizedNumber == normalizedNumber && other.iid == iid;
}

QString normalizedNumber;
quint32 iid;
};

struct CacheItem
{
CacheItem() : itemData(0), iid(0), statusFlags(0), contactState(ContactAbsent), listeners(0), filterMatchRole(-1) {}
Expand Down Expand Up @@ -422,7 +437,7 @@ private slots:
QBasicTimer m_expiryTimer;
QBasicTimer m_fetchTimer;
QHash<quint32, CacheItem> m_people;
QMultiHash<QString, quint32> m_phoneNumberIds;
QMultiHash<QString, CachedPhoneNumber> m_phoneNumberIds;
QHash<QString, quint32> m_emailAddressIds;
QHash<QPair<QString, QString>, quint32> m_onlineAccountIds;
QHash<QContactId, QContact> m_contactsToSave;
Expand Down

0 comments on commit 2df678a

Please sign in to comment.