File indexing completed on 2025-01-05 04:58:20

0001 /*
0002   SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 
0006 */
0007 
0008 #include "blacklistbalooemaillist.h"
0009 
0010 #include <KEmailAddress>
0011 #include <KLocalizedString>
0012 #include <QEvent>
0013 #include <QPainter>
0014 using namespace PimCommon;
0015 
0016 BlackListBalooEmailList::BlackListBalooEmailList(QWidget *parent)
0017     : QListWidget(parent)
0018 {
0019     setSelectionMode(QAbstractItemView::ExtendedSelection);
0020     setSortingEnabled(true);
0021     setContextMenuPolicy(Qt::CustomContextMenu);
0022 }
0023 
0024 BlackListBalooEmailList::~BlackListBalooEmailList() = default;
0025 
0026 void BlackListBalooEmailList::changeEvent(QEvent *event)
0027 {
0028     if (event->type() == QEvent::PaletteChange) {
0029         generalPaletteChanged();
0030     }
0031     QListWidget::changeEvent(event);
0032 }
0033 
0034 void BlackListBalooEmailList::setEmailBlackList(const QStringList &list)
0035 {
0036     mEmailBlackList = list;
0037 }
0038 
0039 QHash<QString, bool> BlackListBalooEmailList::blackListItemChanged() const
0040 {
0041     QHash<QString, bool> result;
0042     for (int i = 0; i < count(); ++i) {
0043         QListWidgetItem *element = item(i);
0044         auto blackListItem = static_cast<PimCommon::BlackListBalooEmailListItem *>(element);
0045         bool currentStatus = (blackListItem->checkState() == Qt::Checked);
0046         if (blackListItem->initializeStatus() != currentStatus) {
0047             result.insert(blackListItem->text(), currentStatus);
0048         }
0049     }
0050     return result;
0051 }
0052 
0053 void BlackListBalooEmailList::setExcludeDomains(const QStringList &domain)
0054 {
0055     mExcludeDomains = domain;
0056 }
0057 
0058 int BlackListBalooEmailList::setEmailFound(const QStringList &list)
0059 {
0060     mFirstResult = true;
0061     clear();
0062     QStringList emailsAdded;
0063     QStringList emailsLower;
0064     QStringList onlyEmails;
0065     for (const QString &mail : list) {
0066         bool excludeEmail = false;
0067         QString email;
0068         QString name;
0069         KEmailAddress::extractEmailAddressAndName(mail, email, name);
0070 
0071         const QString mailToLower = mail.toLower();
0072         const QString emailToLower = email.toLower();
0073         for (const QString &domain : std::as_const(mExcludeDomains)) {
0074             if (email.endsWith(domain)) {
0075                 excludeEmail = true;
0076                 break;
0077             }
0078         }
0079         if (!excludeEmail) {
0080             // Make it static
0081             for (const QString &excludeEmailRegularExpression : std::as_const(mExcludeEmailsRegularExpressions)) {
0082                 if (!excludeEmailRegularExpression.isEmpty()) {
0083                     if (email.contains(QRegularExpression(excludeEmailRegularExpression))) {
0084                         excludeEmail = true;
0085                         continue;
0086                     }
0087                 }
0088             }
0089         }
0090         if (excludeEmail) {
0091             continue;
0092         }
0093         if (!emailsAdded.contains(mail) && !emailsLower.contains(mailToLower) && !onlyEmails.contains(emailToLower)) {
0094             auto item = new BlackListBalooEmailListItem(this);
0095             if (mEmailBlackList.contains(mail)) {
0096                 item->setCheckState(Qt::Checked);
0097                 item->setInitializeStatus(true);
0098             } else {
0099                 item->setCheckState(Qt::Unchecked);
0100             }
0101             item->setText(mail);
0102             emailsAdded << mail;
0103             emailsLower << mailToLower;
0104             onlyEmails << emailToLower;
0105         }
0106     }
0107     return count();
0108 }
0109 
0110 void BlackListBalooEmailList::generalPaletteChanged()
0111 {
0112     const QPalette palette = viewport()->palette();
0113     QColor color = palette.text().color();
0114     color.setAlpha(128);
0115     mTextColor = color;
0116 }
0117 
0118 void BlackListBalooEmailList::setExcludeEmailsRegularExpressions(const QStringList &newExcludeEmailsRegularExpressions)
0119 {
0120     mExcludeEmailsRegularExpressions = newExcludeEmailsRegularExpressions;
0121 }
0122 
0123 void BlackListBalooEmailList::paintEvent(QPaintEvent *event)
0124 {
0125     if (mFirstResult && (!model() || model()->rowCount() == 0)) {
0126         QPainter p(viewport());
0127 
0128         QFont font = p.font();
0129         font.setItalic(true);
0130         p.setFont(font);
0131 
0132         if (!mTextColor.isValid()) {
0133             generalPaletteChanged();
0134         }
0135         p.setPen(mTextColor);
0136 
0137         p.drawText(QRect(0, 0, width(), height()), Qt::AlignCenter, i18n("No result found"));
0138     } else {
0139         QListWidget::paintEvent(event);
0140     }
0141 }
0142 
0143 BlackListBalooEmailListItem::BlackListBalooEmailListItem(QListWidget *parent)
0144     : QListWidgetItem(parent)
0145     , mInitializeStatus(false)
0146 {
0147     setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable);
0148 }
0149 
0150 BlackListBalooEmailListItem::~BlackListBalooEmailListItem() = default;
0151 
0152 bool BlackListBalooEmailListItem::initializeStatus() const
0153 {
0154     return mInitializeStatus;
0155 }
0156 
0157 void BlackListBalooEmailListItem::setInitializeStatus(bool initializeStatus)
0158 {
0159     mInitializeStatus = initializeStatus;
0160 }
0161 
0162 #include "moc_blacklistbalooemaillist.cpp"