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 "confirmaddresswidget.h"
0008 
0009 #include <KLocalizedString>
0010 #include <QLabel>
0011 #include <QListWidget>
0012 #include <QVBoxLayout>
0013 
0014 ConfirmAddressWidget::ConfirmAddressWidget(QWidget *parent)
0015     : QWidget(parent)
0016     , mListEmails(new QListWidget(this))
0017 {
0018     auto mainLayout = new QVBoxLayout(this);
0019     mainLayout->setContentsMargins({});
0020     mainLayout->setObjectName(QLatin1StringView("mainlayout"));
0021 
0022     auto lab = new QLabel(i18n("Potentially invalid emails are displayed in red:"), this);
0023     lab->setObjectName(QLatin1StringView("label"));
0024     mainLayout->addWidget(lab);
0025 
0026     mListEmails->setObjectName(QLatin1StringView("listemails"));
0027     connect(mListEmails, &QListWidget::itemChanged, this, &ConfirmAddressWidget::slotItemChanged);
0028     mainLayout->addWidget(mListEmails);
0029 }
0030 
0031 ConfirmAddressWidget::~ConfirmAddressWidget() = default;
0032 
0033 void ConfirmAddressWidget::setValidAddresses(const QStringList &addresses)
0034 {
0035     createAddressItems(addresses, true);
0036 }
0037 
0038 void ConfirmAddressWidget::setInvalidAddresses(const QStringList &addresses)
0039 {
0040     createAddressItems(addresses, false);
0041 }
0042 
0043 void ConfirmAddressWidget::createAddressItems(const QStringList &address, bool valid)
0044 {
0045     for (const QString &email : address) {
0046         auto item = new QListWidgetItem(email, mListEmails);
0047         item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
0048         if (valid) {
0049             item->setFlags(item->flags() & ~Qt::ItemIsUserCheckable);
0050         } else {
0051             item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
0052             item->setCheckState(Qt::Unchecked);
0053             item->setForeground(Qt::red);
0054         }
0055         mListEmails->addItem(item);
0056     }
0057 }
0058 
0059 QStringList ConfirmAddressWidget::whiteListSelectedEmails() const
0060 {
0061     QStringList lst;
0062     const int nbItems(mListEmails->count());
0063     for (int i = 0; i < nbItems; ++i) {
0064         QListWidgetItem *item = mListEmails->item(i);
0065         if (item->checkState() == Qt::Checked) {
0066             lst << item->text();
0067         }
0068     }
0069     return lst;
0070 }
0071 
0072 void ConfirmAddressWidget::slotItemChanged(QListWidgetItem *)
0073 {
0074     bool hasElementChecked = false;
0075     const int nbItems(mListEmails->count());
0076     for (int i = 0; i < nbItems; ++i) {
0077         QListWidgetItem *item = mListEmails->item(i);
0078         if (item->checkState() == Qt::Checked) {
0079             hasElementChecked = true;
0080             break;
0081         }
0082     }
0083     Q_EMIT updateButtonStatus(hasElementChecked);
0084 }
0085 
0086 #include "moc_confirmaddresswidget.cpp"