From 4884893ca09e1062bc66dcdb7b89181b0af19d30 Mon Sep 17 00:00:00 2001 From: Matt Vogt Date: Thu, 19 Jun 2014 18:45:25 +1000 Subject: [PATCH] [libcontacts] Ensure search terms are trimmed When searching for merge candidates, trim the strings used as search tokens in case they contain problematic whitespace. --- src/seasidecache.cpp | 60 +++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/src/seasidecache.cpp b/src/seasidecache.cpp index cde461b..fc65a26 100644 --- a/src/seasidecache.cpp +++ b/src/seasidecache.cpp @@ -1271,33 +1271,35 @@ static QContactFilter filterForMergeCandidates(const QContact &contact) QContactFilter rv; QContactName name(contact.detail()); - const QString firstName(name.firstName()); - const QString lastName(name.lastName()); + const QString firstName(name.firstName().trimmed()); + const QString lastName(name.lastName().trimmed()); if (firstName.isEmpty() && lastName.isEmpty()) { // Use the displayLabel to match with - QString label(contact.detail().label()); - - // Partial match to first name - QContactDetailFilter firstNameFilter; - setDetailType(firstNameFilter, QContactName::FieldFirstName); - firstNameFilter.setMatchFlags(QContactFilter::MatchContains | QContactFilter::MatchFixedString); - firstNameFilter.setValue(label); - rv = rv | firstNameFilter; - - // Partial match to last name - QContactDetailFilter lastNameFilter; - setDetailType(lastNameFilter, QContactName::FieldLastName); - lastNameFilter.setMatchFlags(QContactFilter::MatchContains | QContactFilter::MatchFixedString); - lastNameFilter.setValue(label); - rv = rv | lastNameFilter; - - // Partial match to nickname - QContactDetailFilter nicknameFilter; - setDetailType(nicknameFilter, QContactNickname::FieldNickname); - nicknameFilter.setMatchFlags(QContactFilter::MatchContains | QContactFilter::MatchFixedString); - nicknameFilter.setValue(label); - rv = rv | nicknameFilter; + const QString label(contact.detail().label().trimmed()); + + if (!label.isEmpty()) { + // Partial match to first name + QContactDetailFilter firstNameFilter; + setDetailType(firstNameFilter, QContactName::FieldFirstName); + firstNameFilter.setMatchFlags(QContactFilter::MatchContains | QContactFilter::MatchFixedString); + firstNameFilter.setValue(label); + rv = rv | firstNameFilter; + + // Partial match to last name + QContactDetailFilter lastNameFilter; + setDetailType(lastNameFilter, QContactName::FieldLastName); + lastNameFilter.setMatchFlags(QContactFilter::MatchContains | QContactFilter::MatchFixedString); + lastNameFilter.setValue(label); + rv = rv | lastNameFilter; + + // Partial match to nickname + QContactDetailFilter nicknameFilter; + setDetailType(nicknameFilter, QContactNickname::FieldNickname); + nicknameFilter.setMatchFlags(QContactFilter::MatchContains | QContactFilter::MatchFixedString); + nicknameFilter.setValue(label); + rv = rv | nicknameFilter; + } } else { if (!firstName.isEmpty()) { // Partial match to first name @@ -1342,7 +1344,7 @@ static QContactFilter filterForMergeCandidates(const QContact &contact) // Phone number match foreach (const QContactPhoneNumber &phoneNumber, contact.details()) { - const QString number(phoneNumber.number()); + const QString number(phoneNumber.number().trimmed()); if (number.isEmpty()) continue; @@ -1351,11 +1353,11 @@ static QContactFilter filterForMergeCandidates(const QContact &contact) // Email address match foreach (const QContactEmailAddress &emailAddress, contact.details()) { - QString address(emailAddress.emailAddress()); + QString address(emailAddress.emailAddress().trimmed()); int index = address.indexOf(QChar::fromLatin1('@')); if (index > 0) { // Match any address that is the same up to the @ symbol - address = address.left(index); + address = address.left(index).trimmed(); } if (address.isEmpty()) @@ -1370,11 +1372,11 @@ static QContactFilter filterForMergeCandidates(const QContact &contact) // Account URI match foreach (const QContactOnlineAccount &account, contact.details()) { - QString uri(account.accountUri()); + QString uri(account.accountUri().trimmed()); int index = uri.indexOf(QChar::fromLatin1('@')); if (index > 0) { // Match any account URI that is the same up to the @ symbol - uri = uri.left(index); + uri = uri.left(index).trimmed(); } if (uri.isEmpty())