File indexing completed on 2024-05-05 07:58:25
0001 /* 0002 SPDX-FileCopyrightText: 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com> 0003 0004 SPDX-License-Identifier: LGPL-2.1-or-later 0005 */ 0006 0007 #include "duplicatesfinder_p.h" 0008 #include "personsmodel.h" 0009 0010 #include "kpeople_debug.h" 0011 0012 using namespace KPeople; 0013 0014 DuplicatesFinder::DuplicatesFinder(PersonsModel *model, QObject *parent) 0015 : KJob(parent) 0016 , m_model(model) 0017 { 0018 } 0019 0020 void DuplicatesFinder::setSpecificPerson(const QString &personUri) 0021 { 0022 m_personUri = personUri; 0023 } 0024 0025 void DuplicatesFinder::start() 0026 { 0027 if (m_personUri.isEmpty()) { 0028 QMetaObject::invokeMethod(this, "doSearch", Qt::QueuedConnection); 0029 } else { 0030 QMetaObject::invokeMethod(this, "doSpecificSearch", Qt::QueuedConnection); 0031 } 0032 } 0033 0034 // TODO: start providing partial results so that we can start processing matches while it's not done 0035 void DuplicatesFinder::doSearch() 0036 { 0037 // NOTE: This can probably optimized. I'm just trying to get the semantics right at the moment 0038 // maybe using nepomuk for the matching would help? 0039 0040 QList<AbstractContact::Ptr> collectedValues; 0041 m_matches.clear(); 0042 0043 if (m_model->rowCount() == 0) { 0044 qCWarning(KPEOPLE_LOG) << "finding duplicates on empty model!"; 0045 } 0046 0047 for (int i = 0, rows = m_model->rowCount(); i < rows; i++) { 0048 QModelIndex idx = m_model->index(i, 0); 0049 0050 // we gather the values 0051 AbstractContact::Ptr values = idx.data(PersonsModel::PersonVCardRole).value<AbstractContact::Ptr>(); 0052 0053 // we check if it matches 0054 int j = 0; 0055 for (const AbstractContact::Ptr &valueToCompare : std::as_const(collectedValues)) { 0056 QList<Match::MatchReason> matchedRoles = Match::matchAt(values, valueToCompare); 0057 0058 if (!matchedRoles.isEmpty()) { 0059 QPersistentModelIndex i2(m_model->index(j, 0)); 0060 0061 m_matches.append(Match(matchedRoles, idx, i2)); 0062 } 0063 j++; 0064 } 0065 0066 // we add our data for comparing later 0067 collectedValues.append(values); 0068 } 0069 emitResult(); 0070 } 0071 0072 void DuplicatesFinder::doSpecificSearch() 0073 { 0074 m_matches.clear(); 0075 0076 QModelIndex idx = m_model->indexForPersonUri(m_personUri); 0077 AbstractContact::Ptr values = idx.data(PersonsModel::PersonVCardRole).value<AbstractContact::Ptr>(); 0078 0079 for (int i = 0, rows = m_model->rowCount(); i < rows; i++) { 0080 QModelIndex idx2 = m_model->index(i, 0); 0081 0082 if (idx2.data(PersonsModel::PersonUriRole) == m_personUri) { 0083 continue; 0084 } 0085 0086 AbstractContact::Ptr values2 = idx2.data(PersonsModel::PersonVCardRole).value<AbstractContact::Ptr>(); 0087 QList<Match::MatchReason> matchedRoles = Match::matchAt(values, values2); 0088 if (!matchedRoles.isEmpty()) { 0089 m_matches.append(Match(matchedRoles, idx, idx2)); 0090 } 0091 } 0092 0093 emitResult(); 0094 } 0095 0096 QList<Match> DuplicatesFinder::results() const 0097 { 0098 return m_matches; 0099 } 0100 0101 #include "moc_duplicatesfinder_p.cpp"