Skip to content

Commit

Permalink
[libcommhistory] Use libphonenumber for matching. Contributes to JB#3…
Browse files Browse the repository at this point in the history
…8835

Examples of solved false positive matches:
"+36 20 123 4567" and "+36 30 123 4567" were matched incorrectly.
"+36 20 123 4567" and "+44 20 123 4567" were matched incorrectly.

Examples of local and IDD numbers that still work:
"+36 20 123 4567" still matches "06 20 123 4567" (local)
"+36 20 123 4567" still matches "00 36 70 381 0581" (IID)

Examples of remaining false positives:
"06 20 123 4567" is a false positive match of "+1 0620 123 4567"
"00 36 20 123 4567" is a false positive match of "+1 00 36 20 123 4567"

We deem that the remaining false positives are unlikely enough that
they don't matter in practice. If this becomes relevant, we will need
to make it possible to pass a region code to the matching algorithm.
  • Loading branch information
Timur Kristóf committed Feb 4, 2020
1 parent 713fede commit c24308d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions rpm/libcommhistory-qt5.spec
Expand Up @@ -14,6 +14,7 @@ BuildRequires: pkgconfig(Qt5DBus)
BuildRequires: pkgconfig(Qt5Test)
BuildRequires: pkgconfig(qtcontacts-sqlite-qt5-extensions) >= 0.1.41
BuildRequires: pkgconfig(contactcache-qt5) >= 0.0.17
BuildRequires: libphonenumber-devel

%{!?qtc_qmake5:%define qtc_qmake5 %qmake5}
%{!?qtc_make:%define qtc_make make}
Expand Down
20 changes: 16 additions & 4 deletions src/recipient.cpp
Expand Up @@ -26,6 +26,8 @@
#include <QHash>
#include <QDebug>

#include <phonenumbers/phonenumberutil.h>

namespace {

bool initializeTypes()
Expand Down Expand Up @@ -243,11 +245,21 @@ bool Recipient::matchesPhoneNumber(const PhoneNumberMatchDetails &phoneNumber) c
{
if (!d->isPhoneNumber)
return false;
if (d->remoteUidHash != phoneNumber.minimizedNumberHash)

// Matching the minimized phone number is necessary, but insufficient
if (d->remoteUidHash != 0 && phoneNumber.minimizedNumberHash != 0 && d->remoteUidHash != phoneNumber.minimizedNumberHash)
return false;
if (!phoneNumber.minimizedNumber.isEmpty() && !d->minimizedRemoteUid.isEmpty() && d->minimizedRemoteUid != phoneNumber.minimizedNumber)
return false;
if (!phoneNumber.minimizedNumber.isEmpty())
return d->minimizedRemoteUid == phoneNumber.minimizedNumber;
return d->remoteUid == phoneNumber.number;

// Full match of the full phone number is always sufficient
if (d->remoteUid == phoneNumber.number)
return true;

// TODO: consider plumbing the region code here for potentially more accurate matching
::i18n::phonenumbers::PhoneNumberUtil *util = ::i18n::phonenumbers::PhoneNumberUtil::GetInstance();
::i18n::phonenumbers::PhoneNumberUtil::MatchType match = util->IsNumberMatchWithTwoStrings(d->remoteUid.toStdString(), phoneNumber.number.toStdString());
return match == ::i18n::phonenumbers::PhoneNumberUtil::EXACT_MATCH || match == ::i18n::phonenumbers::PhoneNumberUtil::NSN_MATCH;
}

bool Recipient::matchesAddressFlags(quint64 flags) const
Expand Down
1 change: 1 addition & 0 deletions src/src.pro
Expand Up @@ -43,6 +43,7 @@ QT -= gui

TARGET = commhistory-qt5
PKGCONFIG += qtcontacts-sqlite-qt5-extensions contactcache-qt5
LIBS += -lphonenumber

DEFINES += LIBCOMMHISTORY_SHARED
CONFIG += hide_symbols
Expand Down

0 comments on commit c24308d

Please sign in to comment.