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 "mergecontactsdialog.h"
0008 #include "manualmerge/mergecontactwidget.h"
0009 #include "widgets/mergecontacterrorlabel.h"
0010 #include "widgets/mergecontactinfowidget.h"
0011 #include "widgets/mergecontactselectinformationscrollarea.h"
0012 
0013 #include <KConfigGroup>
0014 #include <KLocalizedString>
0015 #include <KSharedConfig>
0016 
0017 #include <KWindowConfig>
0018 #include <QDialogButtonBox>
0019 #include <QPushButton>
0020 #include <QStackedWidget>
0021 #include <QVBoxLayout>
0022 #include <QWindow>
0023 
0024 using namespace KABMergeContacts;
0025 namespace
0026 {
0027 static const char myConfigGroupName[] = "MergeContactsDialog";
0028 }
0029 MergeContactsDialog::MergeContactsDialog(QWidget *parent)
0030     : QDialog(parent)
0031     , mButtonBox(new QDialogButtonBox(QDialogButtonBox::Close, this))
0032     , mStackedWidget(new QStackedWidget(this))
0033     , mNoEnoughContactSelected(new KABMergeContacts::MergeContactErrorLabel(KABMergeContacts::MergeContactErrorLabel::NotEnoughContactsSelected, this))
0034     , mNoContactSelected(new KABMergeContacts::MergeContactErrorLabel(MergeContactErrorLabel::NoContactSelected, this))
0035     , mManualMergeResultWidget(new KABMergeContacts::MergeContactWidget(this))
0036     , mSelectInformation(new KABMergeContacts::MergeContactSelectInformationScrollArea(this))
0037     , mMergeContactInfo(new KABMergeContacts::MergeContactInfoWidget(this))
0038 {
0039     setWindowTitle(i18nc("@title:window", "Select Contacts to merge"));
0040     mButtonBox->setObjectName(QLatin1StringView("buttonbox"));
0041     auto mainLayout = new QVBoxLayout(this);
0042     connect(mButtonBox, &QDialogButtonBox::rejected, this, &MergeContactsDialog::reject);
0043     readConfig();
0044 
0045     mStackedWidget->setObjectName(QLatin1StringView("stackedwidget"));
0046     mainLayout->addWidget(mStackedWidget);
0047     mainLayout->addWidget(mButtonBox);
0048 
0049     mNoEnoughContactSelected->setObjectName(QLatin1StringView("notenoughcontactselected"));
0050     mStackedWidget->addWidget(mNoEnoughContactSelected);
0051 
0052     mNoContactSelected->setObjectName(QLatin1StringView("nocontactselected"));
0053     mStackedWidget->addWidget(mNoContactSelected);
0054 
0055     mManualMergeResultWidget->setObjectName(QLatin1StringView("manualmergeresultwidget"));
0056     mStackedWidget->addWidget(mManualMergeResultWidget);
0057     connect(mManualMergeResultWidget, &MergeContactWidget::customizeMergeContact, this, &MergeContactsDialog::slotCustomizeMergeContact);
0058     connect(mManualMergeResultWidget, &MergeContactWidget::contactMerged, this, &MergeContactsDialog::slotContactMerged);
0059 
0060     mSelectInformation->setObjectName(QLatin1StringView("selectioninformation"));
0061     mStackedWidget->addWidget(mSelectInformation);
0062 
0063     mMergeContactInfo->setObjectName(QLatin1StringView("mergecontactinfowidget"));
0064     mStackedWidget->addWidget(mMergeContactInfo);
0065 
0066     mStackedWidget->setCurrentWidget(mNoContactSelected);
0067 }
0068 
0069 MergeContactsDialog::~MergeContactsDialog()
0070 {
0071     writeConfig();
0072 }
0073 
0074 void MergeContactsDialog::setContacts(const Akonadi::Item::List &list)
0075 {
0076     if (list.isEmpty()) {
0077         mStackedWidget->setCurrentWidget(mNoContactSelected);
0078     } else if (list.count() < 2) {
0079         mStackedWidget->setCurrentWidget(mNoEnoughContactSelected);
0080     } else {
0081         mManualMergeResultWidget->setContacts(list);
0082         mStackedWidget->setCurrentWidget(mManualMergeResultWidget);
0083     }
0084     mButtonBox->button(QDialogButtonBox::Close)->setEnabled(true);
0085 }
0086 
0087 void MergeContactsDialog::slotCustomizeMergeContact(const Akonadi::Item::List &lst,
0088                                                     MergeContacts::ConflictInformations conflictType,
0089                                                     const Akonadi::Collection &col)
0090 {
0091     mSelectInformation->setContacts(conflictType, lst, col);
0092     mStackedWidget->setCurrentWidget(mSelectInformation);
0093 }
0094 
0095 void MergeContactsDialog::readConfig()
0096 {
0097     create(); // ensure a window is created
0098     windowHandle()->resize(QSize(300, 200));
0099     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myConfigGroupName));
0100     KWindowConfig::restoreWindowSize(windowHandle(), group);
0101     resize(windowHandle()->size()); // workaround for QTBUG-40584
0102 }
0103 
0104 void MergeContactsDialog::writeConfig()
0105 {
0106     KConfigGroup grp(KSharedConfig::openStateConfig(), QLatin1StringView(myConfigGroupName));
0107     KWindowConfig::saveWindowSize(windowHandle(), grp);
0108     grp.sync();
0109 }
0110 
0111 void MergeContactsDialog::slotContactMerged(const Akonadi::Item &item)
0112 {
0113     mMergeContactInfo->setContact(item);
0114     mStackedWidget->setCurrentWidget(mMergeContactInfo);
0115 }
0116 
0117 #include "moc_mergecontactsdialog.cpp"