Messagelib

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

KDE's Doxygen guidelines are available online.