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/KMimeMessage>
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()->asUnicodeString()) + 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(), 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(), 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(), StringUtil::DisplayFullAddress) + QLatin1StringView("<br/>\n"));
140 }
141
142 headerStr += QLatin1StringView("</div>\n");
143
144 return headerStr;
145}
146
147const char *MessageViewer::PlainHeaderStyle::name() const
148{
149 return "plain";
150}
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(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:37:31 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.