Messagelib

plainheaderstyle.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2023 Laurent Montel <[email protected]>
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 
19 using namespace MessageCore;
20 
21 using namespace MessageViewer;
22 
23 class MessageViewer::PlainHeaderStylePrivate
24 {
25 public:
26  PlainHeaderStylePrivate() = default;
27 
28  Q_REQUIRED_RESULT QString formatAllMessageHeaders(KMime::Message *message) const;
29  MessageViewer::HeaderStyleUtil mHeaderStyleUtil;
30 };
31 
32 QString PlainHeaderStylePrivate::formatAllMessageHeaders(KMime::Message *message) const
33 {
34  QByteArray head = message->head();
35  KMime::Headers::Base *header = KMime::HeaderParsing::extractFirstHeader(head);
36  QString result;
37  while (header) {
38  result += mHeaderStyleUtil.strToHtml(QLatin1String(header->type()) + QLatin1String(": ") + header->asUnicodeString());
39  result += QLatin1String("<br />\n");
40  delete header;
41  header = KMime::HeaderParsing::extractFirstHeader(head);
42  }
43 
44  return result;
45 }
46 
47 PlainHeaderStyle::PlainHeaderStyle()
48  : HeaderStyle()
49  , d(new MessageViewer::PlainHeaderStylePrivate)
50 {
51 }
52 
53 PlainHeaderStyle::~PlainHeaderStyle() = default;
54 
55 //
56 // PlainHeaderStyle:
57 // show every header field on a line by itself,
58 // show subject larger
59 //
60 QString 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 + QLatin1String("</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  + QLatin1String("</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)) + QLatin1String("<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(QLatin1String("&nbsp;&nbsp;<a href=\"") + vCardName() + QLatin1String("\">") + i18n("[vCard]") + QLatin1String("</a>"));
110  }
111 
112  if (strategy->showHeader(QStringLiteral("organization")) && message->organization(false)) {
113  headerStr.append(QLatin1String("&nbsp;&nbsp;(") + d->mHeaderStyleUtil.strToHtml(message->organization()->asUnicodeString()) + QLatin1Char(')'));
114  }
115  headerStr.append(QLatin1String("<br/>\n"));
116  }
117 
118  if (strategy->showHeader(QStringLiteral("to"))) {
119  headerStr.append(i18nc("To-field of the mailheader.", "To: ") + StringUtil::emailAddrAsAnchor(message->to(), StringUtil::DisplayFullAddress)
120  + QLatin1String("<br/>\n"));
121  }
122 
123  if (strategy->showHeader(QStringLiteral("cc")) && message->cc(false)) {
124  const QString str = StringUtil::emailAddrAsAnchor(message->cc(), StringUtil::DisplayFullAddress);
125  if (!str.isEmpty()) {
126  headerStr.append(i18n("CC: ") + str + QLatin1String("<br/>\n"));
127  }
128  }
129 
130  if (strategy->showHeader(QStringLiteral("bcc")) && message->bcc(false)) {
131  const QString str = StringUtil::emailAddrAsAnchor(message->bcc(), StringUtil::DisplayFullAddress);
132  if (!str.isEmpty()) {
133  headerStr.append(i18n("BCC: ") + str + QLatin1String("<br/>\n"));
134  }
135  }
136 
137  if (strategy->showHeader(QStringLiteral("reply-to")) && message->replyTo(false)) {
138  headerStr.append(i18n("Reply to: ") + StringUtil::emailAddrAsAnchor(message->replyTo(), StringUtil::DisplayFullAddress) + QLatin1String("<br/>\n"));
139  }
140 
141  headerStr += QLatin1String("</div>\n");
142 
143  return headerStr;
144 }
145 
146 const char *MessageViewer::PlainHeaderStyle::name() const
147 {
148  return "plain";
149 }
The HeaderStrategy class.
QString i18n(const char *text, const TYPE &arg...)
This class encapsulates the visual appearance of message headers.
Definition: headerstyle.h:46
bool isEmpty() const const
bool isEmpty() const const
bool isRightToLeft()
@ ShortDate
Locale Short date format, e.g.
KIOFILEWIDGETS_EXPORT QString dir(const QString &fileClass)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
virtual QString asUnicodeString() const=0
The HeaderStyleUtil class.
QString message
QString & append(QChar ch)
virtual const char * type() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Mar 27 2023 04:08:18 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.