Pimcommon

baloocompletionemail.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 "baloocompletionemail.h"
9#include <KEmailAddress>
10#include <QMap>
11#include <QRegularExpression>
12
13using namespace PimCommon;
14
15BalooCompletionEmail::BalooCompletionEmail() = default;
16QList<QRegularExpression> BalooCompletionEmail::mExcludeEmailsRegularExpressions = {};
17QStringList BalooCompletionEmail::cleanupEmailList()
18{
19 if (mBalooCompletionEmailInfo.mListEmail.isEmpty()) {
20 return {};
21 }
22 QMap<QString, QString> hashEmail;
23 for (QString email : std::as_const(mBalooCompletionEmailInfo.mListEmail)) {
24 if (!mBalooCompletionEmailInfo.mBlackList.contains(email)) {
26 email = stripEmail(email, address);
27 if (address.isEmpty()) {
28 address = email;
29 }
30 bool excludeMail = false;
31 for (const QString &excludeDomain : std::as_const(mBalooCompletionEmailInfo.mExcludeDomains)) {
32 if (!excludeDomain.isEmpty()) {
33 if (address.endsWith(excludeDomain)) {
34 excludeMail = true;
35 continue;
36 }
37 }
38 }
39 if (!excludeMail) {
40 for (const QRegularExpression &excludeEmailRegularExpression : std::as_const(mExcludeEmailsRegularExpressions)) {
41 if (address.contains(excludeEmailRegularExpression)) {
42 excludeMail = true;
43 continue;
44 }
45 }
46 }
47
48 const QString addressLower = address.toLower();
49 if (!excludeMail && !hashEmail.contains(addressLower)) {
50 hashEmail.insert(addressLower, email);
51 }
52 }
53 }
54 return hashEmail.values();
55}
56
57/* stips the name of an email address email
58 *
59 * 'a' <a@example.com> -> a <a@example.com>
60 * "a" <a@example.com> -> a <a@example.com>
61 * "\"'a'\"" <a@example.com> -> a <a@example.com>
62 *
63 * but "\"'a" <a@example.com> -> "\"'a" <a@example.com>
64 * cause the start and end is not the same.
65 */
66QString BalooCompletionEmail::stripEmail(const QString &email, QString &address)
67{
69 QString addrSpec;
70 QString comment;
71 if (KEmailAddress::AddressOk == KEmailAddress::splitAddress(email, displayName, addrSpec, comment)) {
72 address = addrSpec;
73 while (true) {
79 } else {
80 break;
81 }
82 }
83 return KEmailAddress::normalizedAddress(displayName, addrSpec, comment);
84 } else {
85 return email;
86 }
87}
88
89BalooCompletionEmail::BalooCompletionEmailInfo BalooCompletionEmail::balooCompletionEmailInfo() const
90{
91 return mBalooCompletionEmailInfo;
92}
93
94void BalooCompletionEmail::setBalooCompletionEmailInfo(const BalooCompletionEmailInfo &newBalooCompletionEmailInfo)
95{
96 mBalooCompletionEmailInfo = newBalooCompletionEmailInfo;
97 mExcludeEmailsRegularExpressions.clear();
98 for (const auto &str : std::as_const(mBalooCompletionEmailInfo.mExcludeEmailsRegularExpressions)) {
99 const QRegularExpression exp(str);
100 if (exp.isValid()) {
101 mExcludeEmailsRegularExpressions.append(exp);
102 }
103 }
104}
KCODECS_EXPORT QString normalizedAddress(const QString &displayName, const QString &addrSpec, const QString &comment=QString())
KCODECS_EXPORT EmailParseResult splitAddress(const QByteArray &address, QByteArray &displayName, QByteArray &addrSpec, QByteArray &comment)
AKONADI_CALENDAR_EXPORT QString displayName(Akonadi::ETMCalendar *calendar, const Akonadi::Collection &collection)
PostalAddress address(const QVariant &location)
folderdialogacltab.h
void append(QList< T > &&value)
void clear()
bool isEmpty() const const
bool contains(const Key &key) const const
iterator insert(const Key &key, const T &value)
QList< T > values() const const
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
qsizetype length() const const
QString mid(qsizetype position, qsizetype n) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString trimmed() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:14:35 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.