• Skip to content
  • Skip to link menu
KDE API Documentation - matchessolver.cpp Source File (KPeople)
  • KDE Home
  • Contact Us
 

KPeople

  • frameworks
  • kpeople
  • src
matchessolver.cpp
1 /*
2  Copyright (C) 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
3  Copyright (C) 2013 David Edmundson <david@davidedmundson.co.uk>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
20 #include "matchessolver_p.h"
21 #include "match_p.h"
22 #include "personsmodel.h"
23 #include "personmanager_p.h"
24 #include <QString>
25 #include <QDebug>
26 
27 using namespace KPeople;
28 
29 MatchesSolver::MatchesSolver(const QList<Match> &matches, PersonsModel *model, QObject *parent)
30  : KJob(parent)
31  , m_matches(matches)
32  , m_model(model)
33 {
34 }
35 
36 void MatchesSolver::start()
37 {
38  QMetaObject::invokeMethod(this, "startMatching", Qt::QueuedConnection);
39 }
40 
41 void MatchesSolver::startMatching()
42 {
43  //Will contain all the sets to be merged
44  QHash<QString, QSet<QString> > jobsData;
45  // has a relation of each person, to know where it is
46  QHash<QString, QString> destinationResolver;
47  Q_FOREACH (const Match &m, m_matches) {
48  QString urlA = m.indexA.data(PersonsModel::PersonUriRole).toString();
49  QString urlB = m.indexB.data(PersonsModel::PersonUriRole).toString();
50  Q_ASSERT(urlA != urlB);
51 
52  const bool inA = destinationResolver.contains(urlA);
53  const bool inB = destinationResolver.contains(urlB);
54  if (inA && inB) {
55  //Both are in already, so we have to merge both sets
56  destinationResolver[urlB] = urlA;
57  jobsData[urlA] = jobsData.take(urlB);
58 
59  //we've put all items pointed to by urlA, to the B set
60  //now we re-assign anything pointing to B as pointing to A
61  //because they are the same
62  QList<QString> keys = destinationResolver.keys(urlB);
63  foreach (const QString &key, keys) {
64  destinationResolver[key] = urlA;
65  }
66  } else {
67  //if A is in but not B, we want B wherever A is
68  if (inA) {
69  qSwap(urlB, urlA);
70  }
71  //in case B is anywhere, add A to that set, otherwise just insert B
72  const QString mergeUrl = destinationResolver.value(urlB, urlB);
73 
74  QSet<QString> &jobs = jobsData[mergeUrl];
75  jobs.insert(urlB);
76  jobs.insert(urlA);
77 
78  //remember where urlA and urlB are
79  Q_ASSERT(urlA != mergeUrl);
80  destinationResolver.insert(urlA, mergeUrl);
81  if (urlB != mergeUrl) {
82  destinationResolver.insert(urlB, mergeUrl);
83  }
84  }
85  }
86 
87  Q_FOREACH (const QSet<QString> &uris, jobsData) {
88  if (PersonManager::instance()->mergeContacts(uris.toList()).isEmpty()) {
89  qWarning() << "error: failing to merge contacts: " << uris;
90  }
91  }
92  emitResult();
93 }
KPeople::mergeContacts
KPEOPLE_EXPORT QString mergeContacts(const QStringList &uris)
Merge all uris into a single person.
Definition: global.cpp:28
KPeople
The KPeople namespace contains all the classes for Libkpeople.
KPeople::PersonsModel
This class creates a model of all known contacts from all sources Contacts are represented as a tree ...
Definition: personsmodel.h:42
This file is part of the KDE documentation.
Documentation copyright © 1996-2015 The KDE developers.
Generated on Fri Feb 13 2015 15:16:39 by doxygen 1.8.9.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KPeople

Skip menu "KPeople"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • File List

Class Picker

Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal