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

Commit

Permalink
[libcontacts] Index initial-'+' numbers in their complete forms
Browse files Browse the repository at this point in the history
A number starting with an initial '+' can be tested at full length
which is preferential to tetsing minimized form.
  • Loading branch information
matthewvogt committed Oct 24, 2013
1 parent 9e381af commit 329e46b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 39 deletions.
107 changes: 68 additions & 39 deletions src/seasidecache.cpp
Expand Up @@ -252,9 +252,24 @@ QContactFilter aggregateFilter()

typedef QPair<QString, QString> StringPair;

StringPair addressPair(const QContactPhoneNumber &phoneNumber)
QList<StringPair> addressPairs(const QContactPhoneNumber &phoneNumber)
{
return qMakePair(QString(), SeasideCache::minimizePhoneNumber(phoneNumber.number()));
QList<StringPair> 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)
Expand Down Expand Up @@ -822,45 +837,19 @@ void SeasideCache::refreshContact(CacheItem *cacheItem)

SeasideCache::CacheItem *SeasideCache::itemByPhoneNumber(const QString &number, bool requireComplete)
{
QString minimizedNumber = minimizePhoneNumber(number);

QMultiHash<QString, quint32>::const_iterator it = instancePtr->m_phoneNumberIds.find(minimizedNumber);
QMultiHash<QString, quint32>::const_iterator end = instancePtr->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;
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)
Expand Down Expand Up @@ -1757,14 +1746,15 @@ bool SeasideCache::updateContactIndexing(const QContact &oldContact, const QCont
if (queryDetailTypes.isEmpty() || queryDetailTypes.contains(detailType<QContactPhoneNumber>())) {
// Addresses which are no longer in the contact should be de-indexed
foreach (const QContactPhoneNumber &phoneNumber, oldContact.details<QContactPhoneNumber>()) {
const StringPair address(addressPair(phoneNumber));
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<QContactPhoneNumber>()) {
const StringPair address(addressPair(phoneNumber));
foreach (const StringPair &address, addressPairs(phoneNumber)) {
if (!validAddressPair(address))
continue;

Expand All @@ -1776,6 +1766,7 @@ bool SeasideCache::updateContactIndexing(const QContact &oldContact, const QCont

m_phoneNumberIds.insert(address.second, iid);
}
}

// Remove any addresses no longer available for this contact
if (!oldAddresses.isEmpty()) {
Expand Down Expand Up @@ -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<QString, quint32>::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;
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<quint32> &cacheIds(m_contacts[filterType]);
Expand Down
2 changes: 2 additions & 0 deletions src/seasidecache.h
Expand Up @@ -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);
Expand Down

0 comments on commit 329e46b

Please sign in to comment.