Messagelib

plainheaderstyle.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2025 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "plainheaderstyle.h"
8#include "header/headerstyle_util.h"
9#include "messageviewer/messageviewersettings.h"
10
11#include "header/headerstrategy.h"
12
13#include <MessageCore/StringUtil>
14
15#include <KLocalizedString>
16#include <KMime/Message>
17#include <QApplication>
18
19using namespace MessageCore;
20
21using namespace MessageViewer;
22
23class MessageViewer::PlainHeaderStylePrivate
24{
25public:
26 PlainHeaderStylePrivate() = default;
27
28 [[nodiscard]] QString formatAllMessageHeaders(KMime::Message *message) const;
29 MessageViewer::HeaderStyleUtil mHeaderStyleUtil;
30};
31
32QString PlainHeaderStylePrivate::formatAllMessageHeaders(KMime::Message *message) const
33{
34 QByteArray head = message->head();
35 QByteArrayView headView(head);
36 auto header = KMime::HeaderParsing::parseNextHeader(headView);
37 QString result;
38 while (header) {
39 result += mHeaderStyleUtil.strToHtml(QLatin1StringView(header->type()) + QLatin1StringView(": ") + header->asUnicodeString());
40 result += QLatin1StringView("<br />\n");
41 header = KMime::HeaderParsing::parseNextHeader(headView);
42 }
43
44 return result;
45}
46
47PlainHeaderStyle::PlainHeaderStyle()
48 : d(new MessageViewer::PlainHeaderStylePrivate)
49{
50}
51
52PlainHeaderStyle::~PlainHeaderStyle() = default;
53
54//
55// PlainHeaderStyle:
56// show every header field on a line by itself,
57// show subject larger
58//
59QString PlainHeaderStyle::format(KMime::Message *message) const
60{
61 if (!message) {
62 return {};
63 }
64 const HeaderStrategy *strategy = headerStrategy();
65 // The direction of the header is determined according to the direction
66 // of the application layout.
67
68 const QString dir = QApplication::isRightToLeft() ? QStringLiteral("rtl") : QStringLiteral("ltr");
69
70 // However, the direction of the message subject within the header is
71 // determined according to the contents of the subject itself. Since
72 // the "Re:" and "Fwd:" prefixes would always cause the subject to be
73 // considered left-to-right, they are ignored when determining its
74 // direction.
75
76 const QString subjectDir = d->mHeaderStyleUtil.subjectDirectionString(message);
77 QString headerStr;
78
79 if (strategy->headersToDisplay().isEmpty() && strategy->defaultPolicy() == HeaderStrategy::Display) {
80 // crude way to emulate "all" headers - Note: no strings have
81 // i18n(), so direction should always be ltr.
82 headerStr = QStringLiteral("<div class=\"header\" dir=\"ltr\">");
83 headerStr += d->formatAllMessageHeaders(message);
84 return headerStr + QLatin1StringView("</div>");
85 }
86
87 headerStr = QStringLiteral("<div class=\"header\" dir=\"%1\">").arg(dir);
88
89 // case HdrLong:
90 if (strategy->showHeader(QStringLiteral("subject"))) {
92 if (showEmoticons()) {
94 }
95
96 headerStr += QStringLiteral("<div dir=\"%1\"><b style=\"font-size:130%\">").arg(subjectDir) + d->mHeaderStyleUtil.subjectString(message, flags)
97 + QLatin1StringView("</b></div>\n");
98 }
99
100 if (strategy->showHeader(QStringLiteral("date"))) {
101 const auto dateFormat = isPrinting() ? MessageViewer::HeaderStyleUtil::ShortDate : MessageViewer::HeaderStyleUtil::CustomDate;
102 headerStr.append(i18n("Date: ") + HeaderStyleUtil::strToHtml(HeaderStyleUtil::dateString(message, dateFormat)) + QLatin1StringView("<br/>\n"));
103 }
104
105 if (strategy->showHeader(QStringLiteral("from"))) {
106 headerStr.append(i18n("From: ") + StringUtil::emailAddrAsAnchor(message->from(), StringUtil::DisplayFullAddress, QString(), StringUtil::ShowLink));
107 if (!vCardName().isEmpty()) {
108 headerStr.append(QLatin1StringView("&nbsp;&nbsp;<a href=\"") + vCardName() + QLatin1StringView("\">") + i18n("[vCard]")
109 + QLatin1StringView("</a>"));
110 }
111
112 if (strategy->showHeader(QStringLiteral("organization")) && message->organization(false)) {
113 headerStr.append(QLatin1StringView("&nbsp;&nbsp;(") + d->mHeaderStyleUtil.strToHtml(message->organization(false)->asUnicodeString())
114 + QLatin1Char(')'));
115 }
116 headerStr.append(QLatin1StringView("<br/>\n"));
117 }
118
119 if (strategy->showHeader(QStringLiteral("to"))) {
120 headerStr.append(i18nc("To-field of the mailheader.", "To: ") + StringUtil::emailAddrAsAnchor(message->to(), StringUtil::DisplayFullAddress)
121 + QLatin1StringView("<br/>\n"));
122 }
123
124 if (strategy->showHeader(QStringLiteral("cc")) && message->cc(false)) {
125 const QString str = StringUtil::emailAddrAsAnchor(message->cc(false), StringUtil::DisplayFullAddress);
126 if (!str.isEmpty()) {
127 headerStr.append(i18n("CC: ") + str + QLatin1StringView("<br/>\n"));
128 }
129 }
130
131 if (strategy->showHeader(QStringLiteral("bcc")) && message->bcc(false)) {
132 const QString str = StringUtil::emailAddrAsAnchor(message->bcc(false), StringUtil::DisplayFullAddress);
133 if (!str.isEmpty()) {
134 headerStr.append(i18n("BCC: ") + str + QLatin1StringView("<br/>\n"));
135 }
136 }
137
138 if (strategy->showHeader(QStringLiteral("reply-to")) && message->replyTo(false)) {
139 headerStr.append(i18n("Reply to: ") + StringUtil::emailAddrAsAnchor(message->replyTo(false), StringUtil::DisplayFullAddress)
140 + QLatin1StringView("<br/>\n"));
141 }
142
143 headerStr += QLatin1StringView("</div>\n");
144
145 return headerStr;
146}
147
148const char *MessageViewer::PlainHeaderStyle::name() const
149{
150 return "plain";
151}
@ ShortDate
Locale Short date format, e.g.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
KIOCORE_EXPORT QString dir(const QString &fileClass)
QFlags< Option > Options
bool isRightToLeft()
bool isEmpty() const const
QString & append(QChar ch)
QString arg(Args &&... args) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 25 2025 11:46:37 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.