From 3c95599dfed9e89c3b83738e85675125de80a44c Mon Sep 17 00:00:00 2001 From: Matt Vogt Date: Thu, 9 Jan 2014 00:14:10 -0800 Subject: [PATCH] [libcontacts] Improve handling of imports without names When a vCard record does not contain valid name information, ensure that is nickname is present. This allows us to be certain that each contact has either a valid name or a nickname. --- src/seasideimport.cpp | 77 ++++++++++++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 27 deletions(-) diff --git a/src/seasideimport.cpp b/src/seasideimport.cpp index 32fd8bf..80a6918 100644 --- a/src/seasideimport.cpp +++ b/src/seasideimport.cpp @@ -102,10 +102,25 @@ QContactFilter localContactFilter() return filterLocal | filterWasLocal; } +bool nameIsEmpty(const QContactName &name) +{ + if (name.isEmpty()) + return true; + + return (name.prefix().isEmpty() && + name.firstName().isEmpty() && + name.middleName().isEmpty() && + name.lastName().isEmpty() && + name.suffix().isEmpty()); +} + QString contactNameString(const QContact &contact) { QStringList details; QContactName name(contact.detail()); + if (nameIsEmpty(name)) + return QString(); + details.append(name.prefix()); details.append(name.firstName()); details.append(name.middleName()); @@ -329,25 +344,35 @@ QList SeasideImport::buildImportContacts(const QList QContact &contact(*it); const QString guid = contact.detail().guid(); - const bool emptyName = contact.detail().isEmpty(); const QString name = contactNameString(contact); - const QString label = contact.detail().label(); + const bool emptyName = name.isEmpty(); + + QString label; + if (emptyName) { + QContactName nameDetail = contact.detail(); + contact.removeDetail(&nameDetail); + + label = contact.detail().label(); + if (label.isEmpty()) { + label = SeasideCache::generateDisplayLabelFromNonNameDetails(contact); + } + } int previousIndex = -1; QHash::const_iterator git = importGuids.find(guid); if (git != importGuids.end()) { previousIndex = git.value(); } else { - QHash::const_iterator nit = importNames.find(name); - if (nit != importNames.end()) { - previousIndex = nit.value(); - } else { + if (!emptyName) { + QHash::const_iterator nit = importNames.find(name); + if (nit != importNames.end()) { + previousIndex = nit.value(); + } + } else if (!label.isEmpty()) { // Only if name is empty, use displayLabel - probably SIM import - if (emptyName) { - QHash::const_iterator lit = importLabels.find(label); - if (lit != importLabels.end()) { - previousIndex = lit.value(); - } + QHash::const_iterator lit = importLabels.find(label); + if (lit != importLabels.end()) { + previousIndex = lit.value(); } } } @@ -368,8 +393,10 @@ QList SeasideImport::buildImportContacts(const QList } else if (!label.isEmpty()) { importLabels.insert(label, index); - // Modify this contact to have the label as a nickname - setNickname(contact, label); + if (contact.details().isEmpty()) { + // Modify this contact to have the label as a nickname + setNickname(contact, label); + } } ++it; @@ -414,37 +441,33 @@ QList SeasideImport::buildImportContacts(const QList QContactId existingId; - bool existing = true; QHash::const_iterator git = existingGuids.find(guid); if (git != existingGuids.end()) { existingId = *git; } else { - const bool emptyName = (*it).detail().isEmpty(); + const bool emptyName = nameIsEmpty((*it).detail()); if (!emptyName) { const QString name = contactNameString(*it); QHash::const_iterator nit = existingNames.find(name); if (nit != existingNames.end()) { existingId = *nit; - } else { - existing = false; } } else { - const QString label = (*it).detail().label(); - if (!label.isEmpty()) { - QHash::const_iterator nit = existingNicknames.find(label); - if (nit != existingNicknames.end()) { - existingId = *nit; - } else { - existing = false; + foreach (const QContactNickname nick, (*it).details()) { + const QString nickname(nick.nickname()); + if (!nickname.isEmpty()) { + QHash::const_iterator nit = existingNicknames.find(nickname); + if (nit != existingNicknames.end()) { + existingId = *nit; + break; + } } - } else { - existing = false; } } } - if (existing) { + if (!existingId.isNull()) { QMap::iterator eit = existingIds.find(existingId); if (eit == existingIds.end()) { existingIds.insert(existingId, (it - importedContacts.begin()));