Skip to content

Commit

Permalink
Merge branch 'jb38835-numbermatching' into 'master'
Browse files Browse the repository at this point in the history
Phone number matching

See merge request mer-core/libcommhistory!32
  • Loading branch information
Venemo committed Feb 7, 2020
2 parents eaa3464 + c24308d commit 0f8702a
Show file tree
Hide file tree
Showing 4 changed files with 48 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
46 changes: 42 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 @@ -206,8 +208,15 @@ bool Recipient::matches(const Recipient &o) const
return false;
if (!d->isPhoneNumber && d->localUid != o.d->localUid)
return false;

// Phone numbers are a special case
if (d->isPhoneNumber)
return matchesPhoneNumber(o.toPhoneNumberMatchDetails());

// For non-phonenumbers, matching the minimized remote uid is sufficient
if (!d->minimizedRemoteUid.isEmpty() || !o.d->minimizedRemoteUid.isEmpty())
return d->minimizedRemoteUid == o.d->minimizedRemoteUid;

return d->remoteUid == o.d->remoteUid;
}

Expand All @@ -222,6 +231,10 @@ bool Recipient::isSameContact(const Recipient &o) const

bool Recipient::matchesRemoteUid(const QString &o) const
{
// Phone numbers are a special case
if (d->isPhoneNumber)
return matchesPhoneNumber(Recipient::phoneNumberMatchDetails(o));

const QString minimizedMatch(::minimizeRemoteUid(o, d->isPhoneNumber));
if (!minimizedMatch.isEmpty())
return d->minimizedRemoteUid == minimizedMatch;
Expand All @@ -232,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 Expand Up @@ -334,6 +357,21 @@ Recipient::PhoneNumberMatchDetails Recipient::phoneNumberMatchDetails(const QStr
return rv;
}

Recipient::PhoneNumberMatchDetails Recipient::toPhoneNumberMatchDetails() const
{
Q_ASSERT(d->isPhoneNumber);

if (d->minimizedRemoteUid.isEmpty() || d->remoteUidHash == 0) {
return phoneNumberMatchDetails(d->remoteUid);
}

PhoneNumberMatchDetails det;
det.number = d->remoteUid;
det.minimizedNumber = d->minimizedRemoteUid;
det.minimizedNumberHash = d->remoteUidHash;
return det;
}

RecipientList::RecipientList()
{
}
Expand Down
4 changes: 4 additions & 0 deletions src/recipient.h
Expand Up @@ -141,6 +141,10 @@ class LIBCOMMHISTORY_EXPORT Recipient
*/
static PhoneNumberMatchDetails phoneNumberMatchDetails(const QString &s);

/* Represent the current recipient as suitable for phone number matching
*/
PhoneNumberMatchDetails toPhoneNumberMatchDetails() const;

private:
QSharedPointer<RecipientPrivate> d;

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 0f8702a

Please sign in to comment.