diff --git a/rpm/libcontacts-qt5.spec b/rpm/libcontacts-qt5.spec index d37e1e1..e174ef1 100644 --- a/rpm/libcontacts-qt5.spec +++ b/rpm/libcontacts-qt5.spec @@ -15,6 +15,7 @@ BuildRequires: pkgconfig(mlite5) BuildRequires: pkgconfig(mlocale5) BuildRequires: pkgconfig(mce) BuildRequires: pkgconfig(qtcontacts-sqlite-qt5-extensions) >= 0.2.31 +BuildRequires: libphonenumber-devel BuildRequires: qt5-qttools BuildRequires: qt5-qttools-linguist diff --git a/src/seasidecache.cpp b/src/seasidecache.cpp index ccee7c2..035ab44 100644 --- a/src/seasidecache.cpp +++ b/src/seasidecache.cpp @@ -73,6 +73,7 @@ #include #include +#include QTVERSIT_USE_NAMESPACE @@ -3313,41 +3314,66 @@ void SeasideCache::resolveAddress(ResolveListener *listener, const QString &firs 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->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->iid)) { - int matchLength = bestPhoneNumberMatchLength(item->contact, normalized); - if (matchLength > bestMatchLength) { - bestMatchLength = matchLength; - matchItem = item; - if (bestMatchLength == ExactMatch) - break; - } - } + if (it == end) + return 0; + + QHash possibleMatches; + ::i18n::phonenumbers::PhoneNumberUtil *util = ::i18n::phonenumbers::PhoneNumberUtil::GetInstance(); + ::std::string normalizedStdStr = normalized.toStdString(); + + for (QMultiHash::const_iterator matchingIt = it; + matchingIt != end && matchingIt.key() == number; + ++matchingIt) { + + const CachedPhoneNumber &cachedPhoneNumber = matchingIt.value(); + + // Bypass libphonenumber if the numbers match exactly + if (matchingIt->normalizedNumber == normalized) + return itemById(cachedPhoneNumber.iid, requireComplete); + + ::i18n::phonenumbers::PhoneNumberUtil::MatchType matchType = + util->IsNumberMatchWithTwoStrings(normalizedStdStr, cachedPhoneNumber.normalizedNumber.toStdString()); + + switch (matchType) { + case ::i18n::phonenumbers::PhoneNumberUtil::EXACT_MATCH: + // This is the optimal outcome + return itemById(cachedPhoneNumber.iid, requireComplete); + case ::i18n::phonenumbers::PhoneNumberUtil::NSN_MATCH: + case ::i18n::phonenumbers::PhoneNumberUtil::SHORT_NSN_MATCH: + // Store numbers whose NSN (national significant number) might match + // Example: if +36701234567 is calling, then 1234567 is an NSN match + possibleMatches.insert(cachedPhoneNumber.normalizedNumber, cachedPhoneNumber.iid); + break; + default: + // Either couldn't parse the number or it was NO_MATCH, ignore it + break; } + } - if (matchItem != 0) { - if (requireComplete) { - ensureCompletion(matchItem); + // Choose the best match from these contacts + int bestMatchLength = 0; + CacheItem *matchItem = 0; + for (QHash::const_iterator matchingIt = possibleMatches.begin(); matchingIt != possibleMatches.end(); ++matchingIt) { + if (CacheItem *item = existingItem(*matchingIt)) { + int matchLength = bestPhoneNumberMatchLength(item->contact, normalized); + if (matchLength > bestMatchLength) { + bestMatchLength = matchLength; + matchItem = item; + if (bestMatchLength == ExactMatch) + break; } - return matchItem; } } + if (matchItem != 0) { + if (requireComplete) { + ensureCompletion(matchItem); + } + return matchItem; + } + return 0; + } int SeasideCache::contactIndex(quint32 iid, FilterType filterType) diff --git a/src/src.pro b/src/src.pro index 78cec88..0351360 100644 --- a/src/src.pro +++ b/src/src.pro @@ -22,6 +22,7 @@ packagesExist(mlite5) { warning("mlite not available. Some functionality may not work as expected.") } PKGCONFIG += mlocale5 mce qtcontacts-sqlite-qt5-extensions +LIBS += -lphonenumber DEFINES += CONTACTCACHE_BUILD diff --git a/tests/tst_resolve/tst_resolve.pro b/tests/tst_resolve/tst_resolve.pro index eeee359..4da24d3 100644 --- a/tests/tst_resolve/tst_resolve.pro +++ b/tests/tst_resolve/tst_resolve.pro @@ -4,6 +4,7 @@ TARGET = tst_resolve QT += contacts-private dbus PKGCONFIG += mlocale5 +LIBS += -lphonenumber # We need the moc output for ContactManagerEngine from sqlite-extensions extensionsIncludePath = $$system(pkg-config --cflags-only-I qtcontacts-sqlite-qt5-extensions)