From 2e49ad5866ffbb9393413db835bda244647135d7 Mon Sep 17 00:00:00 2001 From: Matt Vogt Date: Tue, 14 Jan 2014 17:29:41 -0800 Subject: [PATCH] [libcontacts] Skip UID matches where name details differ UIDs found in VCF are not very reliable; if a UID match is found where the contacts have name details that do not match, ignore the UID match and remove the Guid detail from one contact. --- src/seasideimport.cpp | 41 ++++++++++++++++--- tests/tst_seasideimport/tst_seasideimport.cpp | 6 --- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/seasideimport.cpp b/src/seasideimport.cpp index 80a6918..4fc421a 100644 --- a/src/seasideimport.cpp +++ b/src/seasideimport.cpp @@ -362,7 +362,21 @@ QList SeasideImport::buildImportContacts(const QList QHash::const_iterator git = importGuids.find(guid); if (git != importGuids.end()) { previousIndex = git.value(); - } else { + + if (!emptyName) { + // If we have a GUID match, but names differ, ignore the match + const QContact &previous(importedContacts[previousIndex]); + const QString previousName = contactNameString(previous); + if (!previousName.isEmpty() && (previousName != name)) { + previousIndex = -1; + + // Remove the conflicting GUID from this contact + QContactGuid guidDetail = contact.detail(); + contact.removeDetail(&guidDetail); + } + } + } + if (previousIndex == -1) { if (!emptyName) { QHash::const_iterator nit = importNames.find(name); if (nit != importNames.end()) { @@ -413,6 +427,7 @@ QList SeasideImport::buildImportContacts(const QList QHash existingGuids; QHash existingNames; + QMap existingContactNames; QHash existingNicknames; QContactManager *mgr(SeasideCache::manager()); @@ -426,6 +441,7 @@ QList SeasideImport::buildImportContacts(const QList } if (!name.isEmpty()) { existingNames.insert(name, contact.id()); + existingContactNames.insert(contact.id(), name); } foreach (const QContactNickname &nick, contact.details()) { existingNicknames.insert(nick.nickname(), contact.id()); @@ -438,17 +454,32 @@ QList SeasideImport::buildImportContacts(const QList it = importedContacts.begin(); while (it != importedContacts.end()) { const QString guid = (*it).detail().guid(); + const QString name = contactNameString(*it); + const bool emptyName = name.isEmpty(); QContactId existingId; QHash::const_iterator git = existingGuids.find(guid); if (git != existingGuids.end()) { existingId = *git; - } else { - const bool emptyName = nameIsEmpty((*it).detail()); - if (!emptyName) { - const QString name = contactNameString(*it); + if (!emptyName) { + // If we have a GUID match, but names differ, ignore the match + QMap::iterator nit = existingContactNames.find(existingId); + if (nit != existingContactNames.end()) { + const QString &existingName(*nit); + if (!existingName.isEmpty() && (existingName != name)) { + existingId = QContactId(); + + // Remove the conflicting GUID from this contact + QContactGuid guidDetail = (*it).detail(); + (*it).removeDetail(&guidDetail); + } + } + } + } + if (existingId.isNull()) { + if (!emptyName) { QHash::const_iterator nit = existingNames.find(name); if (nit != existingNames.end()) { existingId = *nit; diff --git a/tests/tst_seasideimport/tst_seasideimport.cpp b/tests/tst_seasideimport/tst_seasideimport.cpp index f52ee6f..d7ff195 100644 --- a/tests/tst_seasideimport/tst_seasideimport.cpp +++ b/tests/tst_seasideimport/tst_seasideimport.cpp @@ -310,12 +310,6 @@ void tst_SeasideImport::nonmergedNickname() void tst_SeasideImport::nonmergedUid() { -#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) - QSKIP("UID matches should be ignored where QContactName details differ"); -#else - QSKIP("UID matches should be ignored where QContactName details differ", SkipAll); -#endif - const char *vCardData = "BEGIN:VCARD\r\n" "N:Springfield;Jebediah;;;\r\n"