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

KPeople

  • frameworks
  • kpeople
  • src
  • widgets
  • plugins
mergecontactswidget.cpp
1 /*
2  Copyright (C) 2013 Franck Arrecot <franck.arrecot@gmail.com>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include "mergecontactswidget.h"
20 #include "personpresentationwidget.h"
21 #include "persondata.h"
22 #include "personsmodel.h"
23 #include "duplicatesfinder_p.h"
24 
25 #include <QObject>
26 #include <QVBoxLayout>
27 #include <QPushButton>
28 #include <QLabel>
29 
30 #include <KLocalizedString>
31 #include <KJob>
32 #include <KDebug>
33 #include <KPluginFactory>
34 
35 K_PLUGIN_FACTORY(MergeContactsWidgetFactory, registerPlugin<MergeContactsWidget>();)
36 K_EXPORT_PLUGIN(MergeContactsWidgetFactory("mergecontactswidgetplugin"))
37 
38 using namespace KPeople;
39 
40 MergeContactsWidget::MergeContactsWidget(QWidget *parent, const QVariantList &args)
41  : AbstractPersonDetailsWidget(parent)
42  , m_person(0)
43  , m_model(0)
44  , m_containerListDetails(0)
45  , m_duplicatesBuster(0)
46 {
47  Q_UNUSED(args);
48  setLayout(new QVBoxLayout());
49  m_mergeButton = new QPushButton(this);
50  m_mergeButton->setText(i18n("Show Merge Suggestions..."));
51  m_mergeButton->setVisible(false);
52 
53  connect(m_mergeButton, SIGNAL(clicked(bool)), this, SLOT(onMergePossibilitiesButtonPressed()));
54  layout()->addWidget(m_mergeButton);
55 }
56 
57 void MergeContactsWidget::setPerson(PersonData *person)
58 {
59  m_person = person;
60  searchForDuplicates();
61 }
62 
63 void MergeContactsWidget::setPersonsModel(PersonsModel *model)
64 {
65  m_model = model;
66  searchForDuplicates();
67 }
68 
69 void MergeContactsWidget::fillDuplicatesWidget(const QList<QPersistentModelIndex> &duplicates)
70 {
71  // clean the previous list
72  delete m_containerListDetails;
73  m_listMergeContacts.clear();
74  m_mergeButton->setVisible(!duplicates.isEmpty());
75 
76  // 1- Vertical list of person-presentation-widget : one contact, one checkbox
77  m_containerListDetails = new QWidget(this);
78  m_containerListDetails->setLayout(new QVBoxLayout());
79  layout()->addWidget(m_containerListDetails);
80  m_containerListDetails->setVisible(false);
81 
82  if (!duplicates.size()) {
83  return ;
84  }
85 
86  // building the new button
87  QPushButton *triggerButton = new QPushButton(m_containerListDetails);
88  triggerButton->setText(i18n("Merge with Selected Contacts"));
89  connect(triggerButton, SIGNAL(clicked(bool)), this, SLOT(onMergeButtonPressed()));
90  m_containerListDetails->layout()->addWidget(triggerButton);
91 
92  // building personPresentationWidget to fill up the list
93  Q_FOREACH (const QPersistentModelIndex &duplicate, duplicates) {
94  // displaying contact in a user friendly way
95  kDebug() << "Name retireved form the duplicate :" << duplicate.data(Qt::DisplayRole).toString();
96 
97  QIcon avatar ;
98  QString name = duplicate.data(Qt::DisplayRole).toString();
99 
100  QVariant decoration = duplicate.data(Qt::DecorationRole);
101  if (decoration.type() == (QVariant::Icon)) {
102  avatar = decoration.value<QIcon>();
103 
104  } else if (decoration.type() == (QVariant::Pixmap)) {
105  avatar = QIcon(decoration.value<QPixmap>());
106  }
107 
108  // memorise the link between checkbox widget and model index
109  PersonPresentationWidget *myMergeContactWidget = new PersonPresentationWidget(name, avatar, m_containerListDetails);
110  m_containerListDetails->layout()->addWidget(myMergeContactWidget);
111  m_listMergeContacts.append(qMakePair(duplicate, myMergeContactWidget));
112  }
113 }
114 
115 QList<QPersistentModelIndex> MergeContactsWidget::duplicateBusterFromPerson(const QUrl &uri) const
116 {
117  Q_ASSERT(m_duplicatesBuster);
118  QList<Match> wrongFormatResults = m_duplicatesBuster->results();
119  QList<QPersistentModelIndex> duplicateMatching;
120 
121  Q_FOREACH (const Match &match, wrongFormatResults) {
122  // pick up only the couple with match with our parameter index
123  QUrl uriA = match.indexA.data(PersonsModel::UriRole).toUrl();
124 
125  // Tested with URI because QModelIndex isn't reliable
126  if (uriA == uri) {
127  duplicateMatching.append(match.indexB);
128  }
129  QUrl uriB = match.indexB.data(PersonsModel::UriRole).toUrl();
130  if (uriB == uri) {
131  duplicateMatching.append(match.indexA);
132  }
133  }
134  kDebug() << "Result of the duplicates Buster :" << duplicateMatching.size();
135  return duplicateMatching;
136 }
137 
138 void MergeContactsWidget::searchForDuplicates()
139 {
140  m_mergeButton->setVisible(false);
141  if (m_duplicatesBuster || !m_person || !m_person->isValid() || !m_model) {
142  kDebug() << "Merge Widget failed to launch the duplicates search";
143  return;
144  }
145  m_duplicatesBuster = new DuplicatesFinder(m_model , this);
146  connect(m_duplicatesBuster, SIGNAL(result(KJob*)), SLOT(searchForDuplicatesFinished()));
147  m_duplicatesBuster->setSpecificPerson(m_person->uri());
148  m_duplicatesBuster->start();
149 }
150 
151 void MergeContactsWidget::searchForDuplicatesFinished()
152 {
153  QList<QPersistentModelIndex> duplicates = duplicateBusterFromPerson(m_person->uri());
154 
155  fillDuplicatesWidget(duplicates);
156  m_duplicatesBuster = 0;
157 }
158 
159 void MergeContactsWidget::onMergePossibilitiesButtonPressed()
160 {
161  m_mergeButton->setVisible(false);
162  m_containerListDetails->setVisible(true);
163 }
164 
165 void MergeContactsWidget::onMergeButtonPressed()
166 {
167  // Retrieve the selected contacts
168  QList<QUrl> urisToMerge;
169  urisToMerge << m_person->uri();
170 
171  // do the merge
172  Q_FOREACH (const QPersistentModelIndex &pIndex, getContactsCheckedToMerge()) {
173  urisToMerge << pIndex.data(PersonsModel::UriRole).toUrl();
174  }
175  m_model->createPersonFromUris(urisToMerge);
176  searchForDuplicates();
177 }
178 
179 QList<QPersistentModelIndex> MergeContactsWidget::getContactsCheckedToMerge() const
180 {
181  QList<QPersistentModelIndex> indexesToMerge;
182 
183  // retrieve all the widget where the box is checked
184  QPair<QPersistentModelIndex, PersonPresentationWidget *> mergeContact ;
185  Q_FOREACH (mergeContact, m_listMergeContacts) {
186  if (mergeContact.second->isContactSelected()) {
187  indexesToMerge.append(mergeContact.first);
188  }
189  }
190  kDebug() << "Amount of checked box enable :" << indexesToMerge.size();
191  return indexesToMerge;
192 }
KPeople::PersonData
Allows to query the information about a given person.
Definition: persondata.h:44
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