KPeople

duplicatesfinder.cpp
1/*
2 SPDX-FileCopyrightText: 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#include "duplicatesfinder_p.h"
8#include "personsmodel.h"
9
10#include "kpeople_debug.h"
11
12using namespace KPeople;
13
14DuplicatesFinder::DuplicatesFinder(PersonsModel *model, QObject *parent)
15 : KJob(parent)
16 , m_model(model)
17{
18}
19
20void DuplicatesFinder::setSpecificPerson(const QString &personUri)
21{
22 m_personUri = personUri;
23}
24
25void DuplicatesFinder::start()
26{
27 if (m_personUri.isEmpty()) {
29 } else {
30 QMetaObject::invokeMethod(this, "doSpecificSearch", Qt::QueuedConnection);
31 }
32}
33
34// TODO: start providing partial results so that we can start processing matches while it's not done
35void DuplicatesFinder::doSearch()
36{
37 // NOTE: This can probably optimized. I'm just trying to get the semantics right at the moment
38 // maybe using nepomuk for the matching would help?
39
40 QList<AbstractContact::Ptr> collectedValues;
41 m_matches.clear();
42
43 if (m_model->rowCount() == 0) {
44 qCWarning(KPEOPLE_LOG) << "finding duplicates on empty model!";
45 }
46
47 for (int i = 0, rows = m_model->rowCount(); i < rows; i++) {
48 QModelIndex idx = m_model->index(i, 0);
49
50 // we gather the values
51 AbstractContact::Ptr values = idx.data(PersonsModel::PersonVCardRole).value<AbstractContact::Ptr>();
52
53 // we check if it matches
54 int j = 0;
55 for (const AbstractContact::Ptr &valueToCompare : std::as_const(collectedValues)) {
56 QList<Match::MatchReason> matchedRoles = Match::matchAt(values, valueToCompare);
57
58 if (!matchedRoles.isEmpty()) {
59 QPersistentModelIndex i2(m_model->index(j, 0));
60
61 m_matches.append(Match(matchedRoles, idx, i2));
62 }
63 j++;
64 }
65
66 // we add our data for comparing later
67 collectedValues.append(values);
68 }
69 emitResult();
70}
71
72void DuplicatesFinder::doSpecificSearch()
73{
74 m_matches.clear();
75
76 QModelIndex idx = m_model->indexForPersonUri(m_personUri);
77 AbstractContact::Ptr values = idx.data(PersonsModel::PersonVCardRole).value<AbstractContact::Ptr>();
78
79 for (int i = 0, rows = m_model->rowCount(); i < rows; i++) {
80 QModelIndex idx2 = m_model->index(i, 0);
81
82 if (idx2.data(PersonsModel::PersonUriRole) == m_personUri) {
83 continue;
84 }
85
86 AbstractContact::Ptr values2 = idx2.data(PersonsModel::PersonVCardRole).value<AbstractContact::Ptr>();
87 QList<Match::MatchReason> matchedRoles = Match::matchAt(values, values2);
88 if (!matchedRoles.isEmpty()) {
89 m_matches.append(Match(matchedRoles, idx, idx2));
90 }
91 }
92
93 emitResult();
94}
95
96QList<Match> DuplicatesFinder::results() const
97{
98 return m_matches;
99}
100
101#include "moc_duplicatesfinder_p.cpp"
This class creates a model of all known contacts from all sources Contacts are represented as a tree ...
void append(QList< T > &&value)
void clear()
bool isEmpty() const const
bool invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret)
QVariant data(int role) const const
QueuedConnection
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:17:45 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.