File indexing completed on 2024-11-24 04:43:04

0001 /*
0002     SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "mergecontactwidget.h"
0008 #include "mergecontactwidgetlist.h"
0009 
0010 #include <KLocalizedString>
0011 #include <QSplitter>
0012 
0013 #include "job/mergecontacts.h"
0014 #include "job/mergecontactsjob.h"
0015 #include "widgets/mergecontactinfowidget.h"
0016 #include "widgets/mergecontactloseinformationwarning.h"
0017 #include <Akonadi/CollectionComboBox>
0018 #include <QLabel>
0019 #include <QPushButton>
0020 #include <QVBoxLayout>
0021 
0022 namespace KABMergeContacts
0023 {
0024 KADDRESSBOOKMERGELIB_EXPORT QAbstractItemModel *_k_mergeStubModel = nullptr;
0025 }
0026 
0027 using namespace KABMergeContacts;
0028 MergeContactWidget::MergeContactWidget(QWidget *parent)
0029     : QWidget(parent)
0030     , mConflictTypes(MergeContacts::None)
0031     , mListWidget(new MergeContactWidgetList(this))
0032     , mMergeButton(new QPushButton(i18n("merge"), this))
0033     , mMergeContactWarning(new MergeContactLoseInformationWarning(this))
0034     , mMergeContactInfoWidget(new MergeContactInfoWidget(this))
0035 {
0036     auto lay = new QVBoxLayout(this);
0037     lay->setContentsMargins({});
0038 
0039     auto splitter = new QSplitter;
0040     splitter->setObjectName(QLatin1StringView("splitter"));
0041     splitter->setChildrenCollapsible(false);
0042     lay->addWidget(splitter);
0043 
0044     auto selectContactWidget = new QWidget(this);
0045     selectContactWidget->setObjectName(QLatin1StringView("selectcontactwidget"));
0046     auto vbox = new QVBoxLayout;
0047     selectContactWidget->setLayout(vbox);
0048     auto lab = new QLabel(i18n("Select contacts that you really want to merge:"), this);
0049     vbox->addWidget(lab);
0050     mListWidget->setObjectName(QLatin1StringView("listcontact"));
0051     mListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
0052     vbox->addWidget(mListWidget);
0053     connect(mListWidget, &MergeContactWidgetList::itemSelectionChanged, this, &MergeContactWidget::slotUpdateMergeButton);
0054     connect(mListWidget, &MergeContactWidgetList::itemChanged, this, &MergeContactWidget::slotUpdateMergeButton);
0055     splitter->addWidget(selectContactWidget);
0056 
0057     mMergeContactInfoWidget->setObjectName(QLatin1StringView("mergecontactinfowidget"));
0058     splitter->addWidget(mMergeContactInfoWidget);
0059 
0060     mMergeContactWarning->setObjectName(QLatin1StringView("mergecontactwarning"));
0061     connect(mMergeContactWarning, &MergeContactLoseInformationWarning::continueMerging, this, &MergeContactWidget::slotAutomaticMerging);
0062     connect(mMergeContactWarning, &MergeContactLoseInformationWarning::customizeMergingContacts, this, &MergeContactWidget::slotCustomizeMergingContacts);
0063     lay->addWidget(mMergeContactWarning);
0064 
0065     auto hbox = new QHBoxLayout;
0066     hbox->addStretch();
0067 
0068     lab = new QLabel(i18n("Select the addressbook in which to store merged contacts:"));
0069     hbox->addWidget(lab);
0070 
0071     mCollectionCombobox = new Akonadi::CollectionComboBox(_k_mergeStubModel, this);
0072     mCollectionCombobox->setAccessRightsFilter(Akonadi::Collection::CanCreateItem);
0073     mCollectionCombobox->setMinimumWidth(250);
0074     mCollectionCombobox->setMimeTypeFilter(QStringList() << KContacts::Addressee::mimeType());
0075     mCollectionCombobox->setObjectName(QLatin1StringView("akonadicombobox"));
0076     connect(mCollectionCombobox, &Akonadi::CollectionComboBox::currentIndexChanged, this, &MergeContactWidget::slotUpdateMergeButton);
0077     connect(mCollectionCombobox, &Akonadi::CollectionComboBox::activated, this, &MergeContactWidget::slotUpdateMergeButton);
0078 
0079     hbox->addWidget(mCollectionCombobox);
0080 
0081     lay->addLayout(hbox);
0082 
0083     mMergeButton->setObjectName(QLatin1StringView("mergebutton"));
0084     hbox->addWidget(mMergeButton);
0085     mMergeButton->setEnabled(false);
0086 
0087     connect(mMergeButton, &QPushButton::clicked, this, &MergeContactWidget::slotMergeContacts);
0088 }
0089 
0090 MergeContactWidget::~MergeContactWidget() = default;
0091 
0092 void MergeContactWidget::setContacts(const Akonadi::Item::List &items)
0093 {
0094     mItems = items;
0095     mListWidget->fillListContact(mItems);
0096 }
0097 
0098 Akonadi::Item::List MergeContactWidget::listSelectedContacts() const
0099 {
0100     return mListWidget->listSelectedContacts();
0101 }
0102 
0103 Akonadi::Item MergeContactWidget::currentItem() const
0104 {
0105     return mListWidget->currentAkonadiItem();
0106 }
0107 
0108 void MergeContactWidget::slotUpdateMergeButton()
0109 {
0110     const Akonadi::Item::List listCheckedItems = listSelectedContacts();
0111     Akonadi::Item item = currentItem();
0112     mMergeContactInfoWidget->setContact(item);
0113     mMergeButton->setEnabled((listCheckedItems.count() >= 2) && mCollectionCombobox->currentCollection().isValid());
0114 }
0115 
0116 void MergeContactWidget::slotMergeContacts()
0117 {
0118     mSelectedItems = listSelectedContacts();
0119     // const Akonadi::Collection col = mCollectionCombobox->currentCollection();
0120 
0121     KABMergeContacts::MergeContacts mergeContacts;
0122     mergeContacts.setItems(mSelectedItems);
0123     mConflictTypes = mergeContacts.requiresManualSelectionOfInformation();
0124     if (mConflictTypes != MergeContacts::None) {
0125         mMergeContactWarning->animatedShow();
0126     } else {
0127         slotAutomaticMerging();
0128     }
0129 }
0130 
0131 void MergeContactWidget::slotAutomaticMerging()
0132 {
0133     auto job = new KABMergeContacts::MergeContactsJob(this);
0134     job->setListItem(mSelectedItems);
0135     job->setDestination(mCollectionCombobox->currentCollection());
0136     connect(job, &MergeContactsJob::finished, this, &MergeContactWidget::slotMergeDone);
0137     job->start();
0138 }
0139 
0140 void MergeContactWidget::slotCustomizeMergingContacts()
0141 {
0142     Q_EMIT customizeMergeContact(mSelectedItems, mConflictTypes, mCollectionCombobox->currentCollection());
0143 }
0144 
0145 void MergeContactWidget::slotMergeDone(const Akonadi::Item &item)
0146 {
0147     Q_EMIT contactMerged(item);
0148 }
0149 
0150 #include "moc_mergecontactwidget.cpp"