19 #include "mergecontactswidget.h"
20 #include "personpresentationwidget.h"
21 #include "persondata.h"
22 #include "personsmodel.h"
23 #include "duplicatesfinder_p.h"
26 #include <QVBoxLayout>
27 #include <QPushButton>
30 #include <KLocalizedString>
33 #include <KPluginFactory>
35 K_PLUGIN_FACTORY(MergeContactsWidgetFactory, registerPlugin<MergeContactsWidget>();)
36 K_EXPORT_PLUGIN(MergeContactsWidgetFactory(
"mergecontactswidgetplugin"))
40 MergeContactsWidget::MergeContactsWidget(QWidget *parent, const QVariantList &args)
41 : AbstractPersonDetailsWidget(parent)
44 , m_containerListDetails(0)
45 , m_duplicatesBuster(0)
48 setLayout(
new QVBoxLayout());
49 m_mergeButton =
new QPushButton(
this);
50 m_mergeButton->setText(i18n(
"Show Merge Suggestions..."));
51 m_mergeButton->setVisible(
false);
53 connect(m_mergeButton, SIGNAL(clicked(
bool)),
this, SLOT(onMergePossibilitiesButtonPressed()));
54 layout()->addWidget(m_mergeButton);
57 void MergeContactsWidget::setPerson(
PersonData *person)
60 searchForDuplicates();
63 void MergeContactsWidget::setPersonsModel(
PersonsModel *model)
66 searchForDuplicates();
69 void MergeContactsWidget::fillDuplicatesWidget(
const QList<QPersistentModelIndex> &duplicates)
72 delete m_containerListDetails;
73 m_listMergeContacts.clear();
74 m_mergeButton->setVisible(!duplicates.isEmpty());
77 m_containerListDetails =
new QWidget(
this);
78 m_containerListDetails->setLayout(
new QVBoxLayout());
79 layout()->addWidget(m_containerListDetails);
80 m_containerListDetails->setVisible(
false);
82 if (!duplicates.size()) {
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);
93 Q_FOREACH (
const QPersistentModelIndex &duplicate, duplicates) {
95 kDebug() <<
"Name retireved form the duplicate :" << duplicate.data(Qt::DisplayRole).toString();
98 QString name = duplicate.data(Qt::DisplayRole).toString();
100 QVariant decoration = duplicate.data(Qt::DecorationRole);
101 if (decoration.type() == (QVariant::Icon)) {
102 avatar = decoration.value<QIcon>();
104 }
else if (decoration.type() == (QVariant::Pixmap)) {
105 avatar = QIcon(decoration.value<QPixmap>());
109 PersonPresentationWidget *myMergeContactWidget =
new PersonPresentationWidget(name, avatar, m_containerListDetails);
110 m_containerListDetails->layout()->addWidget(myMergeContactWidget);
111 m_listMergeContacts.append(qMakePair(duplicate, myMergeContactWidget));
115 QList<QPersistentModelIndex> MergeContactsWidget::duplicateBusterFromPerson(
const QUrl &uri)
const
117 Q_ASSERT(m_duplicatesBuster);
118 QList<Match> wrongFormatResults = m_duplicatesBuster->results();
119 QList<QPersistentModelIndex> duplicateMatching;
121 Q_FOREACH (
const Match &match, wrongFormatResults) {
123 QUrl uriA = match.indexA.data(PersonsModel::UriRole).toUrl();
127 duplicateMatching.append(match.indexB);
129 QUrl uriB = match.indexB.data(PersonsModel::UriRole).toUrl();
131 duplicateMatching.append(match.indexA);
134 kDebug() <<
"Result of the duplicates Buster :" << duplicateMatching.size();
135 return duplicateMatching;
138 void MergeContactsWidget::searchForDuplicates()
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";
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();
151 void MergeContactsWidget::searchForDuplicatesFinished()
153 QList<QPersistentModelIndex> duplicates = duplicateBusterFromPerson(m_person->uri());
155 fillDuplicatesWidget(duplicates);
156 m_duplicatesBuster = 0;
159 void MergeContactsWidget::onMergePossibilitiesButtonPressed()
161 m_mergeButton->setVisible(
false);
162 m_containerListDetails->setVisible(
true);
165 void MergeContactsWidget::onMergeButtonPressed()
168 QList<QUrl> urisToMerge;
169 urisToMerge << m_person->uri();
172 Q_FOREACH (
const QPersistentModelIndex &pIndex, getContactsCheckedToMerge()) {
173 urisToMerge << pIndex.data(PersonsModel::UriRole).toUrl();
175 m_model->createPersonFromUris(urisToMerge);
176 searchForDuplicates();
179 QList<QPersistentModelIndex> MergeContactsWidget::getContactsCheckedToMerge()
const
181 QList<QPersistentModelIndex> indexesToMerge;
184 QPair<QPersistentModelIndex, PersonPresentationWidget *> mergeContact ;
185 Q_FOREACH (mergeContact, m_listMergeContacts) {
186 if (mergeContact.second->isContactSelected()) {
187 indexesToMerge.append(mergeContact.first);
190 kDebug() <<
"Amount of checked box enable :" << indexesToMerge.size();
191 return indexesToMerge;
Allows to query the information about a given person.
The KPeople namespace contains all the classes for Libkpeople.
This class creates a model of all known contacts from all sources Contacts are represented as a tree ...