From 329e46b7b68e0b4064bb99edf9384851c5c86624 Mon Sep 17 00:00:00 2001 From: Matt Vogt Date: Thu, 24 Oct 2013 12:35:13 +1000 Subject: [PATCH] [libcontacts] Index initial-'+' numbers in their complete forms A number starting with an initial '+' can be tested at full length which is preferential to tetsing minimized form. --- src/seasidecache.cpp | 127 ++++++++++++++++++++++++++----------------- src/seasidecache.h | 2 + 2 files changed, 80 insertions(+), 49 deletions(-) diff --git a/src/seasidecache.cpp b/src/seasidecache.cpp index 8062eaf..5c8c711 100644 --- a/src/seasidecache.cpp +++ b/src/seasidecache.cpp @@ -252,9 +252,24 @@ QContactFilter aggregateFilter() typedef QPair StringPair; -StringPair addressPair(const QContactPhoneNumber &phoneNumber) +QList addressPairs(const QContactPhoneNumber &phoneNumber) { - return qMakePair(QString(), SeasideCache::minimizePhoneNumber(phoneNumber.number())); + QList rv; + + const QString normalized(SeasideCache::normalizePhoneNumber(phoneNumber.number())); + if (!normalized.isEmpty()) { + const QChar plus(QChar::fromLatin1('+')); + if (normalized.startsWith(plus)) { + // Also index the complete form of this number + rv.append(qMakePair(QString(), normalized)); + } + + // Always index the minimized form of the number + const QString minimized(SeasideCache::minimizePhoneNumber(normalized)); + rv.append(qMakePair(QString(), minimized)); + } + + return rv; } StringPair addressPair(const QContactEmailAddress &emailAddress) @@ -822,45 +837,19 @@ void SeasideCache::refreshContact(CacheItem *cacheItem) SeasideCache::CacheItem *SeasideCache::itemByPhoneNumber(const QString &number, bool requireComplete) { - QString minimizedNumber = minimizePhoneNumber(number); - - QMultiHash::const_iterator it = instancePtr->m_phoneNumberIds.find(minimizedNumber); - QMultiHash::const_iterator end = instancePtr->m_phoneNumberIds.constEnd(); - if (it != end) { - // How many matches are there for this number? - int matchCount = 1; - QMultiHash::const_iterator matchingIt = it + 1; - while ((matchingIt != end) && (matchingIt.key() == minimizedNumber)) { - ++matchCount; - ++matchingIt; - } - if (matchCount == 1) - return itemById(*it, requireComplete); - - QString normalizedNumber = normalizePhoneNumber(number); - - // Choose the best match from these contacts - int bestMatchLength = 0; - CacheItem *matchItem = 0; - for ( ; matchCount > 0; ++it, --matchCount) { - if (CacheItem *item = existingItem(*it)) { - int matchLength = bestPhoneNumberMatchLength(item->contact, normalizedNumber); - if (matchLength > bestMatchLength) { - bestMatchLength = matchLength; - matchItem = item; - } - } - } + const QString normalized(normalizePhoneNumber(number)); + if (normalized.isEmpty()) + return 0; - if (matchItem != 0) { - if (requireComplete) { - ensureCompletion(matchItem); - } - return matchItem; + const QChar plus(QChar::fromLatin1('+')); + if (normalized.startsWith(plus)) { + // See if there is a match for the complete form of this number + if (CacheItem *item = instancePtr->itemMatchingPhoneNumber(normalized, normalized, requireComplete)) { + return item; } } - return 0; + return instancePtr->itemMatchingPhoneNumber(minimizePhoneNumber(normalized), normalized, requireComplete); } SeasideCache::CacheItem *SeasideCache::itemByEmailAddress(const QString &email, bool requireComplete) @@ -1757,24 +1746,26 @@ bool SeasideCache::updateContactIndexing(const QContact &oldContact, const QCont if (queryDetailTypes.isEmpty() || queryDetailTypes.contains(detailType())) { // Addresses which are no longer in the contact should be de-indexed foreach (const QContactPhoneNumber &phoneNumber, oldContact.details()) { - const StringPair address(addressPair(phoneNumber)); - if (validAddressPair(address)) - oldAddresses.insert(address); + foreach (const StringPair &address, addressPairs(phoneNumber)) { + if (validAddressPair(address)) + oldAddresses.insert(address); + } } // Update our address indexes for any address details in this contact foreach (const QContactPhoneNumber &phoneNumber, contact.details()) { - const StringPair address(addressPair(phoneNumber)); - if (!validAddressPair(address)) - continue; + foreach (const StringPair &address, addressPairs(phoneNumber)) { + if (!validAddressPair(address)) + continue; + + if (!oldAddresses.remove(address)) { + // This address was not previously recorded + modified = true; + resolveUnknownAddresses(address.first, address.second, item); + } - if (!oldAddresses.remove(address)) { - // This address was not previously recorded - modified = true; - resolveUnknownAddresses(address.first, address.second, item); + m_phoneNumberIds.insert(address.second, iid); } - - m_phoneNumberIds.insert(address.second, iid); } // Remove any addresses no longer available for this contact @@ -2737,6 +2728,44 @@ void SeasideCache::resolveAddress(ResolveListener *listener, const QString &firs requestUpdate(); } +SeasideCache::CacheItem *SeasideCache::itemMatchingPhoneNumber(const QString &number, const QString &normalized, bool requireComplete) +{ + QMultiHash::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::const_iterator matchingIt = it + 1; + while ((matchingIt != end) && (matchingIt.key() == number)) { + ++matchCount; + ++matchingIt; + } + if (matchCount == 1) + return itemById(*it, requireComplete); + + // Choose the best match from these contacts + int bestMatchLength = 0; + CacheItem *matchItem = 0; + for ( ; matchCount > 0; ++it, --matchCount) { + if (CacheItem *item = existingItem(*it)) { + int matchLength = bestPhoneNumberMatchLength(item->contact, normalized); + if (matchLength > bestMatchLength) { + bestMatchLength = matchLength; + matchItem = item; + } + } + } + + if (matchItem != 0) { + if (requireComplete) { + ensureCompletion(matchItem); + } + return matchItem; + } + } + + return 0; +} + int SeasideCache::contactIndex(quint32 iid, FilterType filterType) { const QList &cacheIds(m_contacts[filterType]); diff --git a/src/seasidecache.h b/src/seasidecache.h index 75dd758..f55a8a5 100644 --- a/src/seasidecache.h +++ b/src/seasidecache.h @@ -418,6 +418,8 @@ private slots: void resolveAddress(ResolveListener *listener, const QString &first, const QString &second, bool requireComplete); + CacheItem *itemMatchingPhoneNumber(const QString &number, const QString &normalized, bool requireComplete); + int contactIndex(quint32 iid, FilterType filter); static QContactRelationship makeRelationship(const QString &type, const QContact &contact1, const QContact &contact2);