Messagelib

plainheaderstyle.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 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 : HeaderStyle()
49 , d(new MessageViewer::PlainHeaderStylePrivate)
50{
51}
52
53PlainHeaderStyle::~PlainHeaderStyle() = default;
54
55//
56// PlainHeaderStyle:
57// show every header field on a line by itself,
58// show subject larger
59//
60QString PlainHeaderStyle::format(KMime::Message *message) const
61{
62 if (!message) {
63 return {};
64 }
65 const HeaderStrategy *strategy = headerStrategy();
66 // The direction of the header is determined according to the direction
67 // of the application layout.
68
69 const QString dir = QApplication::isRightToLeft() ? QStringLiteral("rtl") : QStringLiteral("ltr");
70
71 // However, the direction of the message subject within the header is
72 // determined according to the contents of the subject itself. Since
73 // the "Re:" and "Fwd:" prefixes would always cause the subject to be
74 // considered left-to-right, they are ignored when determining its
75 // direction.
76
77 const QString subjectDir = d->mHeaderStyleUtil.subjectDirectionString(message);
78 QString headerStr;
79
80 if (strategy->headersToDisplay().isEmpty() && strategy->defaultPolicy() == HeaderStrategy::Display) {
81 // crude way to emulate "all" headers - Note: no strings have
82 // i18n(), so direction should always be ltr.
83 headerStr = QStringLiteral("<div class=\"header\" dir=\"ltr\">");
84 headerStr += d->formatAllMessageHeaders(message);
85 return headerStr + QLatin1StringView("</div>");
86 }
87
88 headerStr = QStringLiteral("<div class=\"header\" dir=\"%1\">").arg(dir);
89
90 // case HdrLong:
91 if (strategy->showHeader(QStringLiteral("subject"))) {
93 if (showEmoticons()) {
95 }
96
97 headerStr += QStringLiteral("<div dir=\"%1\"><b style=\"font-size:130%\">").arg(subjectDir) + d->mHeaderStyleUtil.subjectString(message, flags)
98 + QLatin1StringView("</b></div>\n");
99 }
100
101 if (strategy->showHeader(QStringLiteral("date"))) {
102 const auto dateFormat = isPrinting() ? MessageViewer::HeaderStyleUtil::ShortDate : MessageViewer::HeaderStyleUtil::CustomDate;
103 headerStr.append(i18n("Date: ") + HeaderStyleUtil::strToHtml(HeaderStyleUtil::dateString(message, dateFormat)) + QLatin1StringView("<br/>\n"));
104 }
105
106 if (strategy->showHeader(QStringLiteral("from"))) {
107 headerStr.append(i18n("From: ") + StringUtil::emailAddrAsAnchor(message->from(), StringUtil::DisplayFullAddress, QString(), StringUtil::ShowLink));
108 if (!vCardName().isEmpty()) {
109 headerStr.append(QLatin1StringView("&nbsp;&nbsp;<a href=\"") + vCardName() + QLatin1StringView("\">") + i18n("[vCard]")
110 + QLatin1StringView("</a>"));
111 }
112
113 if (strategy->showHeader(QStringLiteral("organization")) && message->organization(false)) {
114 headerStr.append(QLatin1StringView("&nbsp;&nbsp;(") + d->mHeaderStyleUtil.strToHtml(message->organization(false)->asUnicodeString())
115 + QLatin1Char(')'));
116 }
117 headerStr.append(QLatin1StringView("<br/>\n"));
118 }
119
120 if (strategy->showHeader(QStringLiteral("to"))) {
121 headerStr.append(i18nc("To-field of the mailheader.", "To: ") + StringUtil::emailAddrAsAnchor(message->to(), StringUtil::DisplayFullAddress)
122 + QLatin1StringView("<br/>\n"));
123 }
124
125 if (strategy->showHeader(QStringLiteral("cc")) && message->cc(false)) {
126 const QString str = StringUtil::emailAddrAsAnchor(message->cc(false), StringUtil::DisplayFullAddress);
127 if (!str.isEmpty()) {
128 headerStr.append(i18n("CC: ") + str + QLatin1StringView("<br/>\n"));
129 }
130 }
131
132 if (strategy->showHeader(QStringLiteral("bcc")) && message->bcc(false)) {
133 const QString str = StringUtil::emailAddrAsAnchor(message->bcc(false), StringUtil::DisplayFullAddress);
134 if (!str.isEmpty()) {
135 headerStr.append(i18n("BCC: ") + str + QLatin1StringView("<br/>\n"));
136 }
137 }
138
139 if (strategy->showHeader(QStringLiteral("reply-to")) && message->replyTo(false)) {
140 headerStr.append(i18n("Reply to: ") + StringUtil::emailAddrAsAnchor(message->replyTo(false), StringUtil::DisplayFullAddress)
141 + QLatin1StringView("<br/>\n"));
142 }
143
144 headerStr += QLatin1StringView("</div>\n");
145
146 return headerStr;
147}
148
149const char *MessageViewer::PlainHeaderStyle::name() const
150{
151 return "plain";
152}
The HeaderStrategy class.
The HeaderStyleUtil class.
@ ShortDate
Locale Short date format, e.g.
This class encapsulates the visual appearance of message headers.
Definition headerstyle.h:47
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)
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-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.