File indexing completed on 2025-03-09 04:46:33

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 "vcardviewerdialog.h"
0008 #include <Akonadi/GrantleeContactViewer>
0009 #include <KConfigGroup>
0010 #include <KLocalizedString>
0011 #include <KSharedConfig>
0012 #include <KStandardGuiItem>
0013 #include <KWindowConfig>
0014 #include <QDialogButtonBox>
0015 #include <QLabel>
0016 #include <QPushButton>
0017 #include <QVBoxLayout>
0018 #include <QWindow>
0019 
0020 namespace
0021 {
0022 static const char myConfigVCardViewerDialog[] = "VCardViewerDialog";
0023 }
0024 VCardViewerDialog::VCardViewerDialog(const KContacts::Addressee::List &list, QWidget *parent)
0025     : QDialog(parent)
0026     , mContacts(list)
0027 {
0028     setWindowTitle(i18nc("@title:window", "Import vCard"));
0029     auto mainLayout = new QVBoxLayout(this);
0030     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel | QDialogButtonBox::Apply, this);
0031 
0032     auto user1Button = new QPushButton(this);
0033     buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole);
0034     auto user2Button = new QPushButton(this);
0035     buttonBox->addButton(user2Button, QDialogButtonBox::ActionRole);
0036 
0037     connect(buttonBox, &QDialogButtonBox::accepted, this, &VCardViewerDialog::accept);
0038     connect(buttonBox, &QDialogButtonBox::rejected, this, &VCardViewerDialog::reject);
0039     KGuiItem::assign(user1Button, KStandardGuiItem::cancel());
0040     KGuiItem::assign(user2Button, KStandardGuiItem::ok());
0041     mApplyButton = buttonBox->button(QDialogButtonBox::Apply);
0042     user1Button->setDefault(true);
0043     setModal(true);
0044 
0045     auto page = new QFrame(this);
0046     mainLayout->addWidget(page);
0047     mainLayout->addWidget(buttonBox);
0048 
0049     auto layout = new QVBoxLayout(page);
0050 
0051     auto label = new QLabel(i18nc("@info", "Do you want to import this contact into your address book?"), page);
0052     QFont font = label->font();
0053     font.setBold(true);
0054     label->setFont(font);
0055     layout->addWidget(label);
0056 
0057     mView = new KAddressBookGrantlee::GrantleeContactViewer(page);
0058 
0059     layout->addWidget(mView);
0060 
0061     buttonBox->button(QDialogButtonBox::Apply)->setText(i18nc("@action:button", "Import All..."));
0062 
0063     mIt = mContacts.begin();
0064 
0065     connect(user2Button, &QPushButton::clicked, this, &VCardViewerDialog::slotYes);
0066     connect(user1Button, &QPushButton::clicked, this, &VCardViewerDialog::slotNo);
0067     connect(buttonBox->button(QDialogButtonBox::Apply), &QPushButton::clicked, this, &VCardViewerDialog::slotApply);
0068     connect(buttonBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &VCardViewerDialog::slotCancel);
0069 
0070     updateView();
0071     readConfig();
0072 }
0073 
0074 VCardViewerDialog::~VCardViewerDialog()
0075 {
0076     writeConfig();
0077 }
0078 
0079 void VCardViewerDialog::readConfig()
0080 {
0081     create(); // ensure a window is created
0082     windowHandle()->resize(QSize(600, 400));
0083     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myConfigVCardViewerDialog));
0084     KWindowConfig::restoreWindowSize(windowHandle(), group);
0085     resize(windowHandle()->size()); // workaround for QTBUG-40584
0086 }
0087 
0088 void VCardViewerDialog::writeConfig()
0089 {
0090     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myConfigVCardViewerDialog));
0091     KWindowConfig::saveWindowSize(windowHandle(), group);
0092     group.sync();
0093 }
0094 
0095 KContacts::Addressee::List VCardViewerDialog::contacts() const
0096 {
0097     return mContacts;
0098 }
0099 
0100 void VCardViewerDialog::updateView()
0101 {
0102     mView->setRawContact(*mIt);
0103 
0104     KContacts::Addressee::List::Iterator it = mIt;
0105     mApplyButton->setEnabled(++it != mContacts.end());
0106 }
0107 
0108 void VCardViewerDialog::slotYes()
0109 {
0110     mIt++;
0111 
0112     if (mIt == mContacts.end()) {
0113         slotApply();
0114         return;
0115     }
0116 
0117     updateView();
0118 }
0119 
0120 void VCardViewerDialog::slotNo()
0121 {
0122     if (mIt == mContacts.end()) {
0123         accept();
0124         return;
0125     }
0126     // remove the current contact from the result set
0127     mIt = mContacts.erase(mIt);
0128     if (mIt == mContacts.end()) {
0129         return;
0130     }
0131 
0132     updateView();
0133 }
0134 
0135 void VCardViewerDialog::slotApply()
0136 {
0137     QDialog::accept();
0138 }
0139 
0140 void VCardViewerDialog::slotCancel()
0141 {
0142     mContacts.clear();
0143     reject();
0144 }
0145 
0146 #include "moc_vcardviewerdialog.cpp"