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

0001 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
0002 
0003    This file is part of the Trojita Qt IMAP e-mail client,
0004    http://trojita.flaska.net/
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public License as
0008    published by the Free Software Foundation; either version 2 of
0009    the License or (at your option) version 3 or any later version
0010    accepted by the membership of KDE e.V. (or its successor approved
0011    by the membership of KDE e.V.), which shall act as a proxy
0012    defined in Section 14 of version 3 of the license.
0013 
0014    This program is distributed in the hope that it will be useful,
0015    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017    GNU General Public License for more details.
0018 
0019    You should have received a copy of the GNU General Public License
0020    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #include "Composer/QuoteText.h"
0024 #include "UiUtils/PlainTextFormatter.h"
0025 
0026 namespace Composer {
0027 
0028 /** @short Take the initial text and mark it as a quotation */
0029 QStringList quoteText(QStringList inputLines)
0030 {
0031     QStringList quote;
0032     for (QStringList::iterator line = inputLines.begin(); line != inputLines.end(); ++line) {
0033         if (UiUtils::signatureSeparator().match(*line).hasMatch()) {
0034             // This is the signature separator, we should not include anything below that in the quote
0035             break;
0036         }
0037         // rewrap - we need to keep the quotes at < 79 chars, yet the grow with every level
0038         if (line->length() < 79-2) {
0039             // this line is short enough, prepend quote mark and continue
0040             if (line->isEmpty() || line->at(0) == QLatin1Char('>'))
0041                 line->prepend(QLatin1Char('>'));
0042             else
0043                 line->prepend(QLatin1String("> "));
0044             quote << *line;
0045             continue;
0046         }
0047         // long line -> needs to be wrapped
0048         // 1st, detect the quote depth and eventually stript the quotes from the line
0049         int quoteLevel = 0;
0050         int contentStart = 0;
0051         if (line->at(0) == QLatin1Char('>')) {
0052             quoteLevel = 1;
0053             while (quoteLevel < line->length() && line->at(quoteLevel) == QLatin1Char('>'))
0054                 ++quoteLevel;
0055             contentStart = quoteLevel;
0056             if (quoteLevel < line->length() && line->at(quoteLevel) == QLatin1Char(' '))
0057                 ++contentStart;
0058         }
0059 
0060         // 2nd, build a quote string
0061         QString quotemarks;
0062         for (int i = 0; i < quoteLevel; ++i)
0063             quotemarks += QLatin1Char('>');
0064         quotemarks += QLatin1String("> ");
0065 
0066         // 3rd, wrap the line, prepend the quotemarks to each line and add it to the quote text
0067         int space(contentStart), lastSpace(contentStart), pos(contentStart), length(0);
0068         while (pos < line->length()) {
0069             if (line->at(pos) == QLatin1Char(' '))
0070                 space = pos+1;
0071             ++length;
0072             if (length > 65-quotemarks.length() && space != lastSpace) {
0073                 // wrap
0074                 quote << quotemarks + line->mid(lastSpace, space - lastSpace);
0075                 lastSpace = space;
0076                 length = pos - space;
0077             }
0078             ++pos;
0079         }
0080         quote << quotemarks + line->mid(lastSpace);
0081     }
0082     return quote;
0083 }
0084 
0085 }