KPeople

duplicatesfinder.cpp
1 /*
2  SPDX-FileCopyrightText: 2012 Aleix Pol Gonzalez <[email protected]>
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 
12 using namespace KPeople;
13 
14 DuplicatesFinder::DuplicatesFinder(PersonsModel *model, QObject *parent)
15  : KJob(parent)
16  , m_model(model)
17 {
18 }
19 
20 void DuplicatesFinder::setSpecificPerson(const QString &personUri)
21 {
22  m_personUri = personUri;
23 }
24 
25 void 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
35 void 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  QVector<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 
72 void 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 
96 QList<Match> DuplicatesFinder::results() const
97 {
98  return m_matches;
99 }
void append(const T &value)
T value() const const
QVector< V > values(const QMultiHash< K, V > &c)
void clear()
bool isEmpty() const const
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
QVariant data(int role) const const
This class creates a model of all known contacts from all sources Contacts are represented as a tree ...
Definition: personsmodel.h:33
QueuedConnection
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jan 27 2022 22:42:01 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.