File indexing completed on 2024-11-24 04:43:04
0001 /* 0002 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "searchpotentialduplicatecontactjob.h" 0008 #include "kaddressbookmergelibprivate_debug.h" 0009 0010 #include <KContacts/Addressee> 0011 0012 using namespace KABMergeContacts; 0013 0014 SearchPotentialDuplicateContactJob::SearchPotentialDuplicateContactJob(const Akonadi::Item::List &list, QObject *parent) 0015 : QObject(parent) 0016 , mListItem(list) 0017 { 0018 } 0019 0020 SearchPotentialDuplicateContactJob::~SearchPotentialDuplicateContactJob() = default; 0021 0022 void SearchPotentialDuplicateContactJob::start() 0023 { 0024 mListDuplicate.clear(); 0025 Akonadi::Item::List result = mListItem; 0026 while (!result.isEmpty()) { 0027 result = checkList(result); 0028 } 0029 Q_EMIT finished(mListDuplicate); 0030 deleteLater(); 0031 } 0032 0033 QList<Akonadi::Item::List> SearchPotentialDuplicateContactJob::potentialDuplicateContacts() const 0034 { 0035 return mListDuplicate; 0036 } 0037 0038 Akonadi::Item::List SearchPotentialDuplicateContactJob::checkList(const Akonadi::Item::List &lstItem) 0039 { 0040 Akonadi::Item::List notDuplicate; 0041 Akonadi::Item::List lst; 0042 if (!lstItem.isEmpty()) { 0043 const Akonadi::Item firstItem = lstItem.at(0); 0044 const int numberOfItems(lstItem.count()); 0045 for (int j = 1; j < numberOfItems; ++j) { 0046 const Akonadi::Item nextItem = lstItem.at(j); 0047 if (isDuplicate(firstItem, nextItem)) { 0048 lst.append(nextItem); 0049 } else { 0050 notDuplicate.append(nextItem); 0051 } 0052 } 0053 if (!lst.isEmpty()) { 0054 lst.append(firstItem); 0055 mListDuplicate.append(lst); 0056 } 0057 qCDebug(KADDRESSBOOKMERGELIBPRIVATE_LOG) << " duplicate number " << lst.count(); 0058 } 0059 return notDuplicate; 0060 } 0061 0062 bool SearchPotentialDuplicateContactJob::isDuplicate(const Akonadi::Item &itemA, const Akonadi::Item &itemB) 0063 { 0064 if (!itemA.hasPayload<KContacts::Addressee>() || !itemB.hasPayload<KContacts::Addressee>()) { 0065 return false; 0066 } 0067 0068 const auto addressA = itemA.payload<KContacts::Addressee>(); 0069 const auto addressB = itemB.payload<KContacts::Addressee>(); 0070 // 0071 if (!addressA.name().isEmpty() && !addressB.name().isEmpty()) { 0072 qCDebug(KADDRESSBOOKMERGELIBPRIVATE_LOG) << " addressB" << addressB.name() << " addressA.name()" << addressA.name(); 0073 if (addressA.name() == addressB.name()) { 0074 qCDebug(KADDRESSBOOKMERGELIBPRIVATE_LOG) << " return true;"; 0075 return true; 0076 } 0077 } 0078 if (!addressA.nickName().isEmpty() && !addressB.nickName().isEmpty()) { 0079 if (addressA.nickName() == addressB.nickName()) { 0080 return true; 0081 } 0082 } 0083 if (!addressA.emails().isEmpty() && !addressB.emails().isEmpty()) { 0084 const QStringList lstEmails = addressA.emails(); 0085 for (const QString &email : lstEmails) { 0086 if (addressB.emails().contains(email)) { 0087 return true; 0088 } 0089 } 0090 } 0091 return false; 0092 } 0093 0094 #include "moc_searchpotentialduplicatecontactjob.cpp"