File indexing completed on 2023-10-01 08:41:44

0001 /*
0002     Copyright (C) 2012  Lasath Fernando <kde@lasath.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Lesser General Public
0006     License as published by the Free Software Foundation; either
0007     version 2.1 of the License, or (at your option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012     Lesser General Public License for more details.
0013 
0014     You should have received a copy of the GNU Lesser General Public
0015     License along with this library; if not, write to the Free Software
0016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0017 */
0018 
0019 #include "message-filters-private.h"
0020 
0021 #include <QTextDocument> //needed for Qt::escape
0022 
0023 #include <KTp/text-parser.h>
0024 
0025 MessageEscapeFilter::MessageEscapeFilter(QObject *parent)
0026     : KTp::AbstractMessageFilter(parent)
0027 {
0028 }
0029 
0030 void MessageEscapeFilter::filterMessage(KTp::Message &message, const KTp::MessageContext &context)
0031 {
0032     Q_UNUSED(context)
0033 
0034     // Here we do URL detection first, before any escaping is done.
0035     // If escaping would be done first, the link detection of eg. "<http://kde.org/>"
0036     // would become &lt;http://kde.org/&gt; and the URL filter would
0037     // match "http://kde.org/&gt" as the link. On the other hand if it's escaped afterwards,
0038     // it would also escape the newly inserted <a href...> links and the user would
0039     // get &lt;a href.../a&gt; and no clickable links.
0040     //
0041     // Therefore we first detect the links, replace them with placeholders,
0042     // then escape everything, then replace placeholders with actual links.
0043 
0044     QString messageText = message.mainMessagePart();
0045 
0046     QVariantList urls = message.property("Urls").toList();
0047 
0048     // link detection
0049     KTp::TextUrlData parsedUrl = KTp::TextParser::instance()->extractUrlData(messageText);
0050 
0051     QList<QPair<QString, QString> > placeholders;
0052 
0053     int offset = 0;
0054     for (int i = 0; i < parsedUrl.fixedUrls.size(); i++) {
0055          QUrl url(parsedUrl.fixedUrls.at(i));
0056          if (url.scheme() != QLatin1String("mailto")) {
0057              QString originalText = messageText.mid(parsedUrl.urlRanges.at(i).first + offset, parsedUrl.urlRanges.at(i).second);
0058              QString link = QString::fromLatin1("<a href=\"%1\">%2</a>").arg(QString::fromLatin1(url.toEncoded()), originalText);
0059 
0060              QString placeholder = QString::fromLatin1("#K#T#P%1#LINK").arg(i);
0061 
0062              // replace the link with a placeholder^ so it passes through the escaping phase,
0063              // then it will be replaced back for the actual link
0064              messageText.replace(parsedUrl.urlRanges.at(i).first + offset, parsedUrl.urlRanges.at(i).second, placeholder);
0065 
0066              placeholders << qMakePair(placeholder, link);
0067 
0068              urls.append(url);
0069 
0070              //after the first replacement is made, the original position values are not valid anymore, this adjusts them
0071              offset += placeholder.length() - originalText.length();
0072          }
0073      }
0074 
0075     message.setProperty("Urls", urls);
0076 
0077     QString escapedMessage = messageText.toHtmlEscaped();
0078 
0079     escapedMessage.replace(QLatin1String("\n "), QLatin1String("<br/>&nbsp;")); //keep leading whitespaces
0080     escapedMessage.replace(QLatin1Char('\n'), QLatin1String("<br/>"));
0081     escapedMessage.replace(QLatin1Char('\r'), QLatin1String("<br/>"));
0082     escapedMessage.replace(QLatin1Char('\t'), QLatin1String("&nbsp; &nbsp; ")); // replace tabs by 4 spaces
0083     escapedMessage.replace(QLatin1String("  "), QLatin1String(" &nbsp;")); // keep multiple whitespaces
0084 
0085     // replace link placeholders with actual links
0086     for (int i = 0; i < placeholders.size(); i++) {
0087         escapedMessage.replace(placeholders.at(i).first, placeholders.at(i).second);
0088     }
0089 
0090     message.setMainMessagePart(escapedMessage);
0091 }