File indexing completed on 2025-01-19 04:46:43

0001 /*
0002    SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "confirmaddressdialog.h"
0008 #include "confirmaddresswidget.h"
0009 #include <KConfigGroup>
0010 #include <KLocalizedString>
0011 #include <KMessageBox>
0012 #include <KSharedConfig>
0013 #include <KWindowConfig>
0014 #include <QDialogButtonBox>
0015 #include <QPushButton>
0016 #include <QVBoxLayout>
0017 #include <QWindow>
0018 
0019 namespace
0020 {
0021 static const char myConfigConfirmAddressDialog[] = "ConfirmAddressDialog";
0022 }
0023 ConfirmAddressDialog::ConfirmAddressDialog(QWidget *parent)
0024     : QDialog(parent)
0025     , mConfirmWidget(new ConfirmAddressWidget(this))
0026     , mWhiteListEmailsButton(new QPushButton(i18n("Add Selected Emails to WhiteList"), this))
0027 {
0028     setWindowTitle(i18nc("@title:window", "Confirm Addresses"));
0029     auto mainLayout = new QVBoxLayout(this);
0030     mainLayout->setObjectName(QLatin1StringView("mainlayout"));
0031 
0032     mConfirmWidget->setObjectName(QLatin1StringView("confirmwidget"));
0033     mainLayout->addWidget(mConfirmWidget);
0034 
0035     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0036     buttonBox->setObjectName(QLatin1StringView("buttonbox"));
0037     connect(buttonBox, &QDialogButtonBox::accepted, this, &ConfirmAddressDialog::accept);
0038     connect(buttonBox, &QDialogButtonBox::rejected, this, &ConfirmAddressDialog::reject);
0039 
0040     mWhiteListEmailsButton->setObjectName(QLatin1StringView("whiteListEmailsButton"));
0041     mWhiteListEmailsButton->setEnabled(false);
0042     buttonBox->addButton(mWhiteListEmailsButton, QDialogButtonBox::ActionRole);
0043     connect(mWhiteListEmailsButton, &QPushButton::clicked, this, &ConfirmAddressDialog::slotWhiteListSelectedEmails);
0044     connect(mConfirmWidget, &ConfirmAddressWidget::updateButtonStatus, mWhiteListEmailsButton, &QPushButton::setEnabled);
0045 
0046     mainLayout->addWidget(buttonBox);
0047     readConfig();
0048 }
0049 
0050 ConfirmAddressDialog::~ConfirmAddressDialog()
0051 {
0052     writeConfig();
0053 }
0054 
0055 void ConfirmAddressDialog::writeConfig()
0056 {
0057     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myConfigConfirmAddressDialog));
0058     KWindowConfig::saveWindowSize(windowHandle(), group);
0059     group.sync();
0060 }
0061 
0062 void ConfirmAddressDialog::readConfig()
0063 {
0064     create(); // ensure a window is created
0065     windowHandle()->resize(QSize(500, 300));
0066     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myConfigConfirmAddressDialog));
0067     KWindowConfig::restoreWindowSize(windowHandle(), group);
0068     resize(windowHandle()->size()); // workaround for QTBUG-40584
0069 }
0070 
0071 void ConfirmAddressDialog::setValidAddresses(const QStringList &addresses)
0072 {
0073     mConfirmWidget->setValidAddresses(addresses);
0074 }
0075 
0076 void ConfirmAddressDialog::setInvalidAddresses(const QStringList &addresses)
0077 {
0078     mConfirmWidget->setInvalidAddresses(addresses);
0079 }
0080 
0081 void ConfirmAddressDialog::setCurrentIdentity(uint identity)
0082 {
0083     mCurrentIdentity = identity;
0084 }
0085 
0086 void ConfirmAddressDialog::slotWhiteListSelectedEmails()
0087 {
0088     const QStringList whiteListEmails = mConfirmWidget->whiteListSelectedEmails();
0089     if (!whiteListEmails.isEmpty()) {
0090         Q_EMIT addWhileListEmails(whiteListEmails, mCurrentIdentity);
0091         KMessageBox::information(this, i18n("All selected emails are added to white list"), i18nc("@title:window", "Emails added to white list"));
0092     }
0093 }
0094 
0095 #include "moc_confirmaddressdialog.cpp"