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

KPeople

  • frameworks
  • kpeople
  • examples
duplicates.cpp
1 /*
2  Duplicates Example
3  Copyright (C) 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
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 #include <QCoreApplication>
20 #include <QDebug>
21 #include <QTextStream>
22 #include <QTimer>
23 #include <qcommandlineparser.h>
24 
25 #include <personsmodel.h>
26 #include <duplicatesfinder_p.h>
27 #include <matchessolver_p.h>
28 
29 #include <cstdio>
30 #include <iostream>
31 
32 using namespace KPeople;
33 
34 class ResultPrinter : public QObject
35 {
36  Q_OBJECT
37 public Q_SLOTS:
38  void print(KJob *j)
39  {
40  QList<Match> res = ((DuplicatesFinder *) j)->results();
41  std::cout << "Results: " << res.count() << std::endl;
42  for (QList<Match>::iterator it = res.begin(); it != res.end();) {
43  QStringList roles = it->matchReasons();
44  QStringList rA, rB;
45 
46  AbstractContact::Ptr aA = it->indexA.data(PersonsModel::PersonVCardRole).value<AbstractContact::Ptr>();
47  AbstractContact::Ptr aB = it->indexB.data(PersonsModel::PersonVCardRole).value<AbstractContact::Ptr>();
48 
49  Q_ASSERT(!it->reasons.isEmpty());
50  Q_FOREACH (Match::MatchReason i, it->reasons) {
51  rA += it->matchValue(i, aA);
52  rB += it->matchValue(i, aB);
53  }
54  std::cout << "\t- " << qPrintable(roles.join(QStringLiteral(", "))) << ": " << it->indexA.row() << " " << it->indexB.row()
55  << " because: " << qPrintable(rA.join(QStringLiteral(", "))) << " // " << qPrintable(rB.join(QStringLiteral(", "))) << '.' << std::endl;
56  bool remove = false;
57  if (m_action == Ask) {
58  for (char ans = ' '; ans != 'y' && ans != 'n';) {
59  std::cout << "apply? (y/n) ";
60  std::cin >> ans;
61  remove = ans == 'n';
62  }
63  }
64  if (remove) {
65  it = res.erase(it);
66  } else {
67  ++it;
68  }
69  }
70 
71  if ((m_action == Apply || m_action == Ask) && !res.isEmpty()) {
72  MatchesSolver *s = new MatchesSolver(res, m_model, this);
73  connect(s, SIGNAL(finished(KJob*)), this, SLOT(matchesSolverDone(KJob*)));
74  s->start();
75  } else {
76  QCoreApplication::instance()->quit();
77  }
78  }
79 
80  void matchesSolverDone(KJob *job)
81  {
82  if (job->error() == 0) {
83  std::cout << "Matching successfully finished" << std::endl;
84  } else {
85  std::cout << "Matching failed with error: " << job->error() << std::endl;
86  }
87  QCoreApplication::instance()->quit();
88  }
89 
90 public:
91  enum MatchAction { Apply, NotApply, Ask };
92  MatchAction m_action;
93  PersonsModel *m_model;
94 };
95 
96 int main(int argc, char **argv)
97 {
98  QCoreApplication app(argc, argv);
99  PersonsModel model;
100 
101  ResultPrinter r;
102  r.m_model = &model;
103  {
104  QCommandLineParser parser;
105  parser.addOption(QCommandLineOption(QStringLiteral("ask"), QStringLiteral("Ask whether to actually do the merge")));
106  parser.addOption(QCommandLineOption(QStringLiteral("apply"), QStringLiteral("Actually apply all merges. (!!!)")));
107  parser.addHelpOption();
108  parser.process(app);
109  r.m_action = parser.isSet(QStringLiteral("apply")) ? ResultPrinter::Apply
110  : parser.isSet(QStringLiteral("ask")) ? ResultPrinter::Ask
111  : ResultPrinter::NotApply;
112  }
113 
114  DuplicatesFinder *f = new DuplicatesFinder(&model);
115  QObject::connect(f, SIGNAL(finished(KJob*)), &r, SLOT(print(KJob*)));
116 
117  QTimer *t = new QTimer(&app);
118  t->setInterval(500);
119  t->setSingleShot(true);
120  QObject::connect(&model, SIGNAL(modelInitialized(bool)), t, SLOT(start()));
121  QObject::connect(&model, SIGNAL(rowsInserted(QModelIndex,int,int)), t, SLOT(start()));
122  QObject::connect(t, SIGNAL(timeout()), f, SLOT(start()));
123 
124  app.exec();
125 }
126 
127 #include "duplicates.moc"
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