File indexing completed on 2024-11-24 04:52:56

0001 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
0002    Copyright (C) 2018 Erik Quaeghebeur <kde@equaeghe.nospammail.net>
0003 
0004    This file is part of the Trojita Qt IMAP e-mail client,
0005    http://trojita.flaska.net/
0006 
0007    This program is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU General Public License as
0009    published by the Free Software Foundation; either version 2 of
0010    the License or (at your option) version 3 or any later version
0011    accepted by the membership of KDE e.V. (or its successor approved
0012    by the membership of KDE e.V.), which shall act as a proxy
0013    defined in Section 14 of version 3 of the license.
0014 
0015    This program is distributed in the hope that it will be useful,
0016    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018    GNU General Public License for more details.
0019 
0020    You should have received a copy of the GNU General Public License
0021    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022 */
0023 
0024 #include <QObject>
0025 #include <QRegularExpression>
0026 #include <QRegularExpressionMatchIterator>
0027 #include <QStringList>
0028 
0029 #include "SubjectMangling.h"
0030 
0031 namespace Composer {
0032 namespace Util {
0033 
0034 /** @short Prepare a subject to be used in a reply message */
0035 QString replySubject(const QString &subject)
0036 {
0037     // These operations should *not* check for internationalized variants of "Re"; these are evil.
0038     static const QRegularExpression rePrefixMatcher(QLatin1String(
0039                               /* initial whitespace */ "\\s*"
0040                   /* either Re: or mailing list tag */ "(?:Re:|(\\[.+?\\]))"
0041                         /* repetitions of the above */ "(?:\\s|Re:|\\1)*"),
0042                                               QRegularExpression::CaseInsensitiveOption);
0043 
0044     QStringList reply_subject(QStringLiteral("Re:"));
0045     int start = 0;
0046 
0047     // extract mailing list tags & find start of ‘base’ subject
0048     QRegularExpressionMatchIterator i = rePrefixMatcher.globalMatch(subject);
0049     while (i.hasNext() && i.peekNext().capturedStart() == start) {
0050         if (!i.peekNext().captured(1).isEmpty())
0051             reply_subject << i.peekNext().captured(1);
0052         start = i.next().capturedEnd();
0053     }
0054 
0055     // no trailing space after last mailing list tag before empty ‘base’ subject
0056     // TODO: this is for test-suite compliance only; remove?
0057     if (start < subject.length() || reply_subject.length() == 1)
0058         reply_subject << subject.mid(start);
0059 
0060     return reply_subject.join(QLatin1Char(' '));
0061 }
0062 
0063 /** @short Prepare a subject to be used in a message to be forwarded */
0064 QString forwardSubject(const QString &subject)
0065 {
0066     QLatin1String forwardPrefix("Fwd: ");
0067     return forwardPrefix + subject;
0068 }
0069 
0070 }
0071 }
0072 
0073