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

0001 /*
0002     SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "searchandmergecontactduplicatecontactdialog.h"
0008 
0009 #include "job/searchpotentialduplicatecontactjob.h"
0010 #include "mergecontactselectinformationtabwidget.h"
0011 #include "searchduplicate/searchduplicateresultwidget.h"
0012 #include "widgets/mergecontacterrorlabel.h"
0013 #include "widgets/mergecontactshowresulttabwidget.h"
0014 
0015 #include <KConfigGroup>
0016 #include <KLocalizedString>
0017 #include <KSharedConfig>
0018 
0019 #include <KWindowConfig>
0020 #include <QDialogButtonBox>
0021 #include <QPushButton>
0022 #include <QStackedWidget>
0023 #include <QVBoxLayout>
0024 #include <QWindow>
0025 
0026 using namespace KABMergeContacts;
0027 namespace
0028 {
0029 static const char mySearchAndMergeContactDuplicateContactDialogGroupName[] = "SearchAndMergeContactDuplicateContactDialog";
0030 }
0031 SearchAndMergeContactDuplicateContactDialog::SearchAndMergeContactDuplicateContactDialog(QWidget *parent)
0032     : QDialog(parent)
0033     , mSearchResult(new SearchDuplicateResultWidget(this))
0034     , mMergeContactResult(new MergeContactShowResultTabWidget(this))
0035     , mNoContactSelected(new KABMergeContacts::MergeContactErrorLabel(MergeContactErrorLabel::NoContactSelected, this))
0036     , mNoDuplicateContactFound(new KABMergeContacts::MergeContactErrorLabel(MergeContactErrorLabel::NoContactDuplicatesFound, this))
0037     , mNoEnoughContactSelected(new KABMergeContacts::MergeContactErrorLabel(MergeContactErrorLabel::NotEnoughContactsSelected, this))
0038     , mSelectInformation(new KABMergeContacts::MergeContactSelectInformationTabWidget(this))
0039     , mStackedWidget(new QStackedWidget(this))
0040 {
0041     setWindowTitle(i18nc("@title:window", "Search and Select Duplicate Contacts to merge"));
0042     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
0043     auto mainLayout = new QVBoxLayout(this);
0044     connect(buttonBox, &QDialogButtonBox::accepted, this, &SearchAndMergeContactDuplicateContactDialog::accept);
0045     connect(buttonBox, &QDialogButtonBox::rejected, this, &SearchAndMergeContactDuplicateContactDialog::reject);
0046     mStackedWidget->setObjectName(QLatin1StringView("stackedwidget"));
0047 
0048     mSearchResult->setObjectName(QLatin1StringView("mergecontact"));
0049     mStackedWidget->addWidget(mSearchResult);
0050     connect(mSearchResult, &SearchDuplicateResultWidget::contactMerged, this, &SearchAndMergeContactDuplicateContactDialog::slotContactMerged);
0051     connect(mSearchResult, &SearchDuplicateResultWidget::mergeDone, this, &SearchAndMergeContactDuplicateContactDialog::slotMergeDone);
0052     connect(mSearchResult, &SearchDuplicateResultWidget::customizeMergeContact, this, &SearchAndMergeContactDuplicateContactDialog::slotCustomizeMergeContacts);
0053 
0054     mNoContactSelected->setObjectName(QLatin1StringView("nocontactselected"));
0055     mStackedWidget->addWidget(mNoContactSelected);
0056 
0057     mNoDuplicateContactFound->setObjectName(QLatin1StringView("nocontactduplicatesfound"));
0058     mStackedWidget->addWidget(mNoDuplicateContactFound);
0059 
0060     mMergeContactResult->setObjectName(QLatin1StringView("mergecontactresult"));
0061     mStackedWidget->addWidget(mMergeContactResult);
0062 
0063     mNoEnoughContactSelected->setObjectName(QLatin1StringView("noenoughcontactselected"));
0064     mStackedWidget->addWidget(mNoEnoughContactSelected);
0065     mStackedWidget->setCurrentWidget(mNoContactSelected);
0066 
0067     mSelectInformation->setObjectName(QLatin1StringView("selectioninformation"));
0068     mStackedWidget->addWidget(mSelectInformation);
0069 
0070     mainLayout->addWidget(mStackedWidget);
0071     mainLayout->addWidget(buttonBox);
0072 
0073     readConfig();
0074 }
0075 
0076 SearchAndMergeContactDuplicateContactDialog::~SearchAndMergeContactDuplicateContactDialog()
0077 {
0078     writeConfig();
0079 }
0080 
0081 void SearchAndMergeContactDuplicateContactDialog::searchPotentialDuplicateContacts(const Akonadi::Item::List &list)
0082 {
0083     const auto listCount{list.count()};
0084     if (listCount == 0) {
0085         mStackedWidget->setCurrentWidget(mNoContactSelected);
0086     } else if (listCount < 2) {
0087         mStackedWidget->setCurrentWidget(mNoEnoughContactSelected);
0088     } else {
0089         auto job = new SearchPotentialDuplicateContactJob(list, this);
0090         connect(job, &SearchPotentialDuplicateContactJob::finished, this, &SearchAndMergeContactDuplicateContactDialog::slotDuplicateFound);
0091         job->start();
0092     }
0093 }
0094 
0095 void SearchAndMergeContactDuplicateContactDialog::readConfig()
0096 {
0097     create(); // ensure a window is created
0098     windowHandle()->resize(QSize(300, 200));
0099     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySearchAndMergeContactDuplicateContactDialogGroupName));
0100     KWindowConfig::restoreWindowSize(windowHandle(), group);
0101     resize(windowHandle()->size()); // workaround for QTBUG-40584
0102 }
0103 
0104 void SearchAndMergeContactDuplicateContactDialog::writeConfig()
0105 {
0106     KConfigGroup grp(KSharedConfig::openStateConfig(), QLatin1StringView(mySearchAndMergeContactDuplicateContactDialogGroupName));
0107     KWindowConfig::saveWindowSize(windowHandle(), grp);
0108     grp.sync();
0109 }
0110 
0111 void SearchAndMergeContactDuplicateContactDialog::slotDuplicateFound(const QList<Akonadi::Item::List> &duplicate)
0112 {
0113     if (duplicate.isEmpty()) {
0114         mStackedWidget->setCurrentWidget(mNoDuplicateContactFound);
0115     } else {
0116         mStackedWidget->setCurrentWidget(mSearchResult);
0117         mSearchResult->setContacts(duplicate);
0118     }
0119 }
0120 
0121 void SearchAndMergeContactDuplicateContactDialog::slotContactMerged(const Akonadi::Item &item)
0122 {
0123     mMergeContactResult->addMergedContact(item, true);
0124 }
0125 
0126 void SearchAndMergeContactDuplicateContactDialog::slotMergeDone()
0127 {
0128     mStackedWidget->setCurrentWidget(mMergeContactResult);
0129 }
0130 
0131 void SearchAndMergeContactDuplicateContactDialog::slotCustomizeMergeContacts(const QList<MergeConflictResult> &lst, const Akonadi::Collection &col)
0132 {
0133     mSelectInformation->setRequiresSelectInformationWidgets(lst, col);
0134     mStackedWidget->setCurrentWidget(mSelectInformation);
0135 }
0136 
0137 #include "moc_searchandmergecontactduplicatecontactdialog.cpp"