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

0001 /*
0002    SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "addresseelineeditutil.h"
0008 #include <QRegularExpression>
0009 #include <QUrl>
0010 
0011 QString PimCommon::AddresseeLineEditUtil::adaptPasteMails(const QString &str)
0012 {
0013     QString newText = str;
0014     // remove newlines in the to-be-pasted string
0015     static QRegularExpression reg2(QStringLiteral("\r?\n"));
0016     QStringList lines = newText.split(reg2, Qt::SkipEmptyParts);
0017     QStringList::iterator end(lines.end());
0018     for (QStringList::iterator it = lines.begin(); it != end; ++it) {
0019         // remove trailing commas and whitespace
0020         static QRegularExpression reg1(QRegularExpression(QStringLiteral(",?\\s*$")));
0021         (*it).remove(reg1);
0022     }
0023     newText = lines.join(QLatin1StringView(", "));
0024 
0025     if (newText.startsWith(QLatin1StringView("mailto:"))) {
0026         const QUrl url(newText);
0027         newText = url.path();
0028     } else if (newText.contains(QLatin1StringView(" at "))) {
0029         // Anti-spam stuff
0030         newText.replace(QStringLiteral(" at "), QStringLiteral("@"));
0031         newText.replace(QStringLiteral(" dot "), QStringLiteral("."));
0032     } else if (newText.contains(QLatin1StringView("(at)"))) {
0033         static QRegularExpression reg((QStringLiteral("\\s*\\(at\\)\\s*")));
0034         newText.replace(reg, QStringLiteral("@"));
0035     }
0036     return newText;
0037 }