Pimcommon

blacklistbalooemaillist.cpp
1/*
2 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5
6*/
7
8#include "blacklistbalooemaillist.h"
9
10#include <KEmailAddress>
11#include <KLocalizedString>
12#include <QEvent>
13#include <QPainter>
14using namespace PimCommon;
15
16BlackListBalooEmailList::BlackListBalooEmailList(QWidget *parent)
17 : QListWidget(parent)
18{
20 setSortingEnabled(true);
21 setContextMenuPolicy(Qt::CustomContextMenu);
22}
23
24BlackListBalooEmailList::~BlackListBalooEmailList() = default;
25
26void BlackListBalooEmailList::changeEvent(QEvent *event)
27{
28 if (event->type() == QEvent::PaletteChange) {
29 generalPaletteChanged();
30 }
32}
33
34void BlackListBalooEmailList::setEmailBlackList(const QStringList &list)
35{
36 mEmailBlackList = list;
37}
38
39QHash<QString, bool> BlackListBalooEmailList::blackListItemChanged() const
40{
42 for (int i = 0; i < count(); ++i) {
43 QListWidgetItem *element = item(i);
44 auto blackListItem = static_cast<PimCommon::BlackListBalooEmailListItem *>(element);
45 bool currentStatus = (blackListItem->checkState() == Qt::Checked);
46 if (blackListItem->initializeStatus() != currentStatus) {
47 result.insert(blackListItem->text(), currentStatus);
48 }
49 }
50 return result;
51}
52
53void BlackListBalooEmailList::setExcludeDomains(const QStringList &domain)
54{
55 mExcludeDomains = domain;
56}
57
58int BlackListBalooEmailList::setEmailFound(const QStringList &list)
59{
60 mFirstResult = true;
61 clear();
62 QStringList emailsAdded;
63 QStringList emailsLower;
64 QStringList onlyEmails;
65 for (const QString &mail : list) {
66 bool excludeEmail = false;
67 QString email;
70
71 const QString mailToLower = mail.toLower();
72 const QString emailToLower = email.toLower();
73 for (const QString &domain : std::as_const(mExcludeDomains)) {
74 if (email.endsWith(domain)) {
75 excludeEmail = true;
76 break;
77 }
78 }
79 if (!excludeEmail) {
80 // Make it static
81 for (const QString &excludeEmailRegularExpression : std::as_const(mExcludeEmailsRegularExpressions)) {
82 if (!excludeEmailRegularExpression.isEmpty()) {
83 if (email.contains(QRegularExpression(excludeEmailRegularExpression))) {
84 excludeEmail = true;
85 continue;
86 }
87 }
88 }
89 }
90 if (excludeEmail) {
91 continue;
92 }
93 if (!emailsAdded.contains(mail) && !emailsLower.contains(mailToLower) && !onlyEmails.contains(emailToLower)) {
94 auto item = new BlackListBalooEmailListItem(this);
95 if (mEmailBlackList.contains(mail)) {
97 item->setInitializeStatus(true);
98 } else {
100 }
101 item->setText(mail);
102 emailsAdded << mail;
103 emailsLower << mailToLower;
104 onlyEmails << emailToLower;
105 }
106 }
107 return count();
108}
109
110void BlackListBalooEmailList::generalPaletteChanged()
111{
112 const QPalette palette = viewport()->palette();
113 QColor color = palette.text().color();
114 color.setAlpha(128);
115 mTextColor = color;
116}
117
118void BlackListBalooEmailList::setExcludeEmailsRegularExpressions(const QStringList &newExcludeEmailsRegularExpressions)
119{
120 mExcludeEmailsRegularExpressions = newExcludeEmailsRegularExpressions;
121}
122
123void BlackListBalooEmailList::paintEvent(QPaintEvent *event)
124{
125 if (mFirstResult && (!model() || model()->rowCount() == 0)) {
126 QPainter p(viewport());
127
128 QFont font = p.font();
129 font.setItalic(true);
130 p.setFont(font);
131
132 if (!mTextColor.isValid()) {
133 generalPaletteChanged();
134 }
135 p.setPen(mTextColor);
136
137 p.drawText(QRect(0, 0, width(), height()), Qt::AlignCenter, i18n("No result found"));
138 } else {
140 }
141}
142
143BlackListBalooEmailListItem::BlackListBalooEmailListItem(QListWidget *parent)
144 : QListWidgetItem(parent)
145 , mInitializeStatus(false)
146{
148}
149
150BlackListBalooEmailListItem::~BlackListBalooEmailListItem() = default;
151
152bool BlackListBalooEmailListItem::initializeStatus() const
153{
154 return mInitializeStatus;
155}
156
157void BlackListBalooEmailListItem::setInitializeStatus(bool initializeStatus)
158{
159 mInitializeStatus = initializeStatus;
160}
161
162#include "moc_blacklistbalooemaillist.cpp"
KCODECS_EXPORT bool extractEmailAddressAndName(const QString &aStr, QString &mail, QString &name)
QString i18n(const char *text, const TYPE &arg...)
KCRASH_EXPORT void setFlags(KCrash::CrashFlags flags)
QString name(GameStandardAction id)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
QAction * mail(const QObject *recvr, const char *slot, QObject *parent)
folderdialogacltab.h
QAbstractItemModel * model() const const
virtual void paintEvent(QPaintEvent *event) override
QWidget * viewport() const const
bool isValid() const const
void setAlpha(int alpha)
void setItalic(bool enable)
virtual void changeEvent(QEvent *ev) override
iterator insert(const Key &key, const T &value)
void clear()
virtual bool event(QEvent *e) override
QListWidgetItem * item(int row) const const
void setCheckState(Qt::CheckState state)
void setText(const QString &text)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
QString toLower() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
AlignCenter
CustomContextMenu
ItemIsEnabled
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:12:05 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.