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

0001 /*
0002    SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "mergecontactselectinformationscrollarea.h"
0008 #include "job/mergecontactsjob.h"
0009 #include "kaddressbookmergelibprivate_debug.h"
0010 #include "mergecontactselectinformationwidget.h"
0011 #include "widgets/mergecontactinfowidget.h"
0012 #include <KLocalizedString>
0013 #include <KMessageBox>
0014 #include <QPushButton>
0015 #include <QScrollArea>
0016 #include <QStackedWidget>
0017 #include <QVBoxLayout>
0018 using namespace KABMergeContacts;
0019 
0020 MergeContactSelectInformationScrollArea::MergeContactSelectInformationScrollArea(QWidget *parent)
0021     : QWidget(parent)
0022     , mSelectInformationWidget(new MergeContactSelectInformationWidget(this))
0023     , mStackWidget(new QStackedWidget(this))
0024     , mMergedContactWidget(new MergeContactInfoWidget(this))
0025 {
0026     auto vbox = new QVBoxLayout(this);
0027     vbox->setContentsMargins({});
0028     mStackWidget->setObjectName(QLatin1StringView("stackwidget"));
0029     vbox->addWidget(mStackWidget);
0030 
0031     auto selectMergeWidget = new QWidget;
0032     selectMergeWidget->setObjectName(QLatin1StringView("selectwidget"));
0033     auto layout = new QVBoxLayout;
0034     selectMergeWidget->setLayout(layout);
0035     auto area = new QScrollArea(this);
0036     area->setWidgetResizable(true);
0037     area->setObjectName(QLatin1StringView("scrollarea"));
0038     layout->addWidget(area);
0039     mSelectInformationWidget->setObjectName(QLatin1StringView("selectinformationwidget"));
0040     area->setWidget(mSelectInformationWidget);
0041 
0042     auto hbox = new QHBoxLayout;
0043     hbox->addStretch();
0044     auto mergeButton = new QPushButton(i18n("Merge"), this);
0045     mergeButton->setObjectName(QLatin1StringView("merge"));
0046     hbox->addWidget(mergeButton);
0047     layout->addLayout(hbox);
0048     connect(mergeButton, &QPushButton::clicked, this, &MergeContactSelectInformationScrollArea::slotMergeContacts);
0049 
0050     mStackWidget->addWidget(selectMergeWidget);
0051 
0052     mMergedContactWidget->setObjectName(QLatin1StringView("mergedcontactwidget"));
0053     mStackWidget->addWidget(mMergedContactWidget);
0054     mStackWidget->setCurrentWidget(selectMergeWidget);
0055 }
0056 
0057 MergeContactSelectInformationScrollArea::~MergeContactSelectInformationScrollArea() = default;
0058 
0059 void MergeContactSelectInformationScrollArea::setContacts(MergeContacts::ConflictInformations conflictTypes,
0060                                                           const Akonadi::Item::List &listItem,
0061                                                           const Akonadi::Collection &col)
0062 {
0063     mCollection = col;
0064     mListItem = listItem;
0065     if (conflictTypes == MergeContacts::None) {
0066         MergeContacts contact(mListItem);
0067         KContacts::Addressee addr = contact.mergedContact();
0068         mergeContact(addr);
0069     } else {
0070         mSelectInformationWidget->setContacts(conflictTypes, listItem);
0071     }
0072 }
0073 
0074 void MergeContactSelectInformationScrollArea::slotMergeContacts()
0075 {
0076     if (!mCollection.isValid()) {
0077         qCDebug(KADDRESSBOOKMERGELIBPRIVATE_LOG) << " Invalid collection";
0078         return;
0079     }
0080     if (mListItem.isEmpty()) {
0081         qCDebug(KADDRESSBOOKMERGELIBPRIVATE_LOG) << " item list is empty";
0082         return;
0083     }
0084     const bool result = mSelectInformationWidget->verifySelectedInfo();
0085     if (!result) {
0086         if (KMessageBox::ButtonCode::SecondaryAction
0087             == KMessageBox::warningTwoActions(this,
0088                                               i18n("Some information was not selected. You can lose this information. Do you want to continue merging?"),
0089                                               i18nc("@title:window", "Missing Selected Information"),
0090                                               KStandardGuiItem::cont(),
0091                                               KStandardGuiItem::cancel())) {
0092             return;
0093         }
0094     }
0095     MergeContacts contact(mListItem);
0096     KContacts::Addressee addr = contact.mergedContact(true);
0097     mSelectInformationWidget->createContact(addr);
0098     mergeContact(addr);
0099 }
0100 
0101 void MergeContactSelectInformationScrollArea::mergeContact(const KContacts::Addressee &addr)
0102 {
0103     if (!addr.isEmpty()) {
0104         auto job = new KABMergeContacts::MergeContactsJob(this);
0105         job->setNewContact(addr);
0106         job->setDestination(mCollection);
0107         job->setListItem(mListItem);
0108         connect(job, &MergeContactsJob::finished, this, &MergeContactSelectInformationScrollArea::slotMergeDone);
0109         job->start();
0110     } else {
0111         qCDebug(KADDRESSBOOKMERGELIBPRIVATE_LOG) << " Address is empty";
0112     }
0113 }
0114 
0115 void MergeContactSelectInformationScrollArea::slotMergeDone(const Akonadi::Item &item)
0116 {
0117     mMergedContactWidget->setContact(item);
0118     mStackWidget->setCurrentWidget(mMergedContactWidget);
0119 }
0120 
0121 #include "moc_mergecontactselectinformationscrollarea.cpp"