File indexing completed on 2025-01-19 04:46:54

0001 /*  -*- c++ -*-
0002     messageviewer/headerstyle.h
0003 
0004     This file is part of KMail, the KDE mail client.
0005     SPDX-FileCopyrightText: 2003 Marc Mutz <mutz@kde.org>
0006     SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0007 
0008     SPDX-License-Identifier: GPL-2.0-only
0009 */
0010 
0011 #include "briefheaderstyle.h"
0012 #include <MessageViewer/HeaderStrategy>
0013 #include <MessageViewer/MessageViewerSettings>
0014 
0015 #include <MessageCore/StringUtil>
0016 
0017 #include <KLocalizedString>
0018 
0019 #include <QApplication>
0020 #include <QRegularExpression>
0021 
0022 using namespace MessageCore;
0023 using namespace MessageViewer;
0024 //
0025 // BriefHeaderStyle
0026 //   Show everything in a single line, don't show header field names.
0027 //
0028 
0029 QString BriefHeaderStyle::format(KMime::Message *message) const
0030 {
0031     if (!message) {
0032         return {};
0033     }
0034 
0035     const HeaderStrategy *strategy = headerStrategy();
0036     // The direction of the header is determined according to the direction
0037     // of the application layout.
0038 
0039     const QString dir = QApplication::isRightToLeft() ? QStringLiteral("rtl") : QStringLiteral("ltr");
0040 
0041     // However, the direction of the message subject within the header is
0042     // determined according to the contents of the subject itself. Since
0043     // the "Re:" and "Fwd:" prefixes would always cause the subject to be
0044     // considered left-to-right, they are ignored when determining its
0045     // direction.
0046 
0047     const QString subjectDir = mHeaderStyleUtil.subjectDirectionString(message);
0048 
0049     QString headerStr = QLatin1StringView(R"(<div class="header" dir=")") + dir + QLatin1StringView("\">\n");
0050 
0051     if (strategy->showHeader(QStringLiteral("subject"))) {
0052         const KTextToHTML::Options flags = KTextToHTML::PreserveSpaces | KTextToHTML::ReplaceSmileys;
0053 
0054         headerStr += QLatin1StringView("<div dir=\"") + subjectDir + QLatin1StringView("\">\n") + QStringLiteral("<b style=\"font-size:130%\">");
0055 
0056         headerStr += mHeaderStyleUtil.subjectString(message, flags) + QStringLiteral("</b></div>\n");
0057     }
0058     QStringList headerParts;
0059 
0060     if (strategy->showHeader(QStringLiteral("from"))) {
0061         /*TODO(Andras) review if it can happen or not
0062         if ( fromStr.isEmpty() ) // no valid email in from, maybe just a name
0063         fromStr = message->fromStrip(); // let's use that
0064         */
0065         QString fromPart = StringUtil::emailAddrAsAnchor(message->from(), StringUtil::DisplayFullAddress);
0066         if (!vCardName().isEmpty()) {
0067             fromPart += QLatin1StringView("&nbsp;&nbsp;<a href=\"") + vCardName() + QLatin1StringView("\">") + i18n("[vCard]") + QLatin1StringView("</a>");
0068         }
0069         headerParts << fromPart;
0070     }
0071 
0072     if (strategy->showHeader(QStringLiteral("cc")) && message->cc(false)) {
0073         const QString str = StringUtil::emailAddrAsAnchor(message->cc(), StringUtil::DisplayFullAddress);
0074         if (!str.isEmpty()) {
0075             headerParts << i18n("CC: ") + str;
0076         }
0077     }
0078 
0079     if (strategy->showHeader(QStringLiteral("bcc")) && message->bcc(false)) {
0080         const QString str = StringUtil::emailAddrAsAnchor(message->bcc(), StringUtil::DisplayFullAddress);
0081         if (!str.isEmpty()) {
0082             headerParts << i18n("BCC: ") + str;
0083         }
0084     }
0085 
0086     if (strategy->showHeader(QStringLiteral("date"))) {
0087         headerParts << mHeaderStyleUtil.strToHtml(HeaderStyleUtil::dateString(message, /* shortDate = */ MessageViewer::HeaderStyleUtil::ShortDate));
0088     }
0089 
0090     // remove all empty (modulo whitespace) entries and joins them via ", \n"
0091     headerStr += QLatin1StringView(" (") + headerParts.filter(QRegularExpression(QStringLiteral("\\S"))).join(QLatin1StringView(",\n")) + QLatin1Char(')');
0092 
0093     headerStr += QLatin1StringView("</div>\n");
0094 
0095     // ### iterate over the rest of strategy->headerToDisplay() (or
0096     // ### all headers if DefaultPolicy == Display) (elsewhere, too)
0097     return headerStr;
0098 }