File indexing completed on 2025-01-05 04:58:19
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 "baloocompletionemail.h" 0009 #include <KEmailAddress> 0010 #include <QMap> 0011 #include <QRegularExpression> 0012 0013 using namespace PimCommon; 0014 0015 BalooCompletionEmail::BalooCompletionEmail() = default; 0016 QList<QRegularExpression> BalooCompletionEmail::mExcludeEmailsRegularExpressions = {}; 0017 QStringList BalooCompletionEmail::cleanupEmailList() 0018 { 0019 if (mBalooCompletionEmailInfo.mListEmail.isEmpty()) { 0020 return {}; 0021 } 0022 QMap<QString, QString> hashEmail; 0023 for (QString email : std::as_const(mBalooCompletionEmailInfo.mListEmail)) { 0024 if (!mBalooCompletionEmailInfo.mBlackList.contains(email)) { 0025 QString address; 0026 email = stripEmail(email, address); 0027 if (address.isEmpty()) { 0028 address = email; 0029 } 0030 bool excludeMail = false; 0031 for (const QString &excludeDomain : std::as_const(mBalooCompletionEmailInfo.mExcludeDomains)) { 0032 if (!excludeDomain.isEmpty()) { 0033 if (address.endsWith(excludeDomain)) { 0034 excludeMail = true; 0035 continue; 0036 } 0037 } 0038 } 0039 if (!excludeMail) { 0040 for (const QRegularExpression &excludeEmailRegularExpression : std::as_const(mExcludeEmailsRegularExpressions)) { 0041 if (address.contains(excludeEmailRegularExpression)) { 0042 excludeMail = true; 0043 continue; 0044 } 0045 } 0046 } 0047 0048 const QString addressLower = address.toLower(); 0049 if (!excludeMail && !hashEmail.contains(addressLower)) { 0050 hashEmail.insert(addressLower, email); 0051 } 0052 } 0053 } 0054 return hashEmail.values(); 0055 } 0056 0057 /* stips the name of an email address email 0058 * 0059 * 'a' <a@example.com> -> a <a@example.com> 0060 * "a" <a@example.com> -> a <a@example.com> 0061 * "\"'a'\"" <a@example.com> -> a <a@example.com> 0062 * 0063 * but "\"'a" <a@example.com> -> "\"'a" <a@example.com> 0064 * cause the start and end is not the same. 0065 */ 0066 QString BalooCompletionEmail::stripEmail(const QString &email, QString &address) 0067 { 0068 QString displayName; 0069 QString addrSpec; 0070 QString comment; 0071 if (KEmailAddress::AddressOk == KEmailAddress::splitAddress(email, displayName, addrSpec, comment)) { 0072 address = addrSpec; 0073 while (true) { 0074 if ((displayName.startsWith(QLatin1StringView("\\\"")) && displayName.endsWith(QLatin1StringView("\\\"")))) { 0075 displayName = displayName.mid(2, displayName.length() - 4).trimmed(); 0076 } else if ((displayName.startsWith(QLatin1Char('\'')) && displayName.endsWith(QLatin1Char('\''))) 0077 || (displayName.startsWith(QLatin1Char('"')) && displayName.endsWith(QLatin1Char('"')))) { 0078 displayName = displayName.mid(1, displayName.length() - 2).trimmed(); 0079 } else { 0080 break; 0081 } 0082 } 0083 return KEmailAddress::normalizedAddress(displayName, addrSpec, comment); 0084 } else { 0085 return email; 0086 } 0087 } 0088 0089 BalooCompletionEmail::BalooCompletionEmailInfo BalooCompletionEmail::balooCompletionEmailInfo() const 0090 { 0091 return mBalooCompletionEmailInfo; 0092 } 0093 0094 void BalooCompletionEmail::setBalooCompletionEmailInfo(const BalooCompletionEmailInfo &newBalooCompletionEmailInfo) 0095 { 0096 mBalooCompletionEmailInfo = newBalooCompletionEmailInfo; 0097 mExcludeEmailsRegularExpressions.clear(); 0098 for (const auto &str : std::as_const(mBalooCompletionEmailInfo.mExcludeEmailsRegularExpressions)) { 0099 const QRegularExpression exp(str); 0100 if (exp.isValid()) { 0101 mExcludeEmailsRegularExpressions.append(exp); 0102 } 0103 } 0104 }