1 /*
2    SPDX-FileCopyrightText: 2013-2021 Laurent Montel <montel@kde.org>
3 
4    SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "enterpriseheaderstyle.h"
8 #include <MessageViewer/HeaderStyle_Util>
9 #include <MessageViewer/MessageViewerSettings>
10 
11 #include <MessageViewer/HeaderStrategy>
12 
13 #include <MessageCore/StringUtil>
14 
15 #include <KColorScheme>
16 #include <KLocalizedString>
17 
18 #include <KMime/DateFormatter>
19 #include <KMime/KMimeMessage>
20 #include <QStandardPaths>
21 
22 using namespace MessageCore;
23 using namespace MessageViewer;
24 
format(KMime::Message * message) const25 QString EnterpriseHeaderStyle::format(KMime::Message *message) const
26 {
27     if (!message) {
28         return QString();
29     }
30     const HeaderStrategy *strategy = headerStrategy();
31     // The direction of the header is determined according to the direction
32     // of the application layout.
33     // However, the direction of the message subject within the header is
34     // determined according to the contents of the subject itself. Since
35     // the "Re:" and "Fwd:" prefixes would always cause the subject to be
36     // considered left-to-right, they are ignored when determining its
37     // direction.
38 
39     // colors depend on if it is encapsulated or not
40     QColor fontColor(Qt::white);
41     QString linkColor = QStringLiteral("class =\"white\"");
42     const QColor activeColor = KColorScheme(QPalette::Active, KColorScheme::Selection).background().color();
43     QColor activeColorDark = activeColor.darker(130);
44     // reverse colors for encapsulated
45     if (!isTopLevel()) {
46         activeColorDark = activeColor.darker(50);
47         fontColor = QColor(Qt::black);
48         linkColor = QStringLiteral("class =\"black\"");
49     }
50 
51     QString imgpath(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("libmessageviewer/pics/"), QStandardPaths::LocateDirectory));
52     imgpath.prepend(QLatin1String("file://"));
53     imgpath.append(QLatin1String("enterprise_"));
54     const QString borderSettings(QStringLiteral(" padding-top: 0px; padding-bottom: 0px; border-width: 0px "));
55     QString headerStr;
56 
57     // 3D borders
58     if (isTopLevel()) {
59         headerStr += QLatin1String(
60             "<div style=\"position: fixed; top: 0px; left: 0px; background-color: #606060; "
61             "width: 10px; min-height: 100%;\">&nbsp;</div>"
62             "<div style=\"position: fixed; top: 0px; right: 0px;  background-color: #606060; "
63             "width: 10px; min-height: 100%;\">&nbsp;</div>");
64     }
65 
66     headerStr += QStringLiteral("<div style=\"margin-left: 10px; top: 0px;\"><span style=\"font-size: 0.8em; font-weight: bold;\">")
67         + HeaderStyleUtil::dateString(message, /* shortDate */ MessageViewer::HeaderStyleUtil::CustomDate)
68         + QStringLiteral(
69                      "</span></div>"
70                      // #0057ae
71                      "<table style=\"background: ")
72         + activeColorDark.name()
73         + QStringLiteral(
74                      "; border-collapse:collapse; top: 14px; min-width: 200px; \" cellpadding='0'> \n"
75                      "  <tr> \n"
76                      "   <td style=\"min-width: 6px; background-image: url(")
77         + imgpath
78         + QStringLiteral(
79                      "top_left.png); \"></td> \n"
80                      "   <td style=\"height: 6px; width: 100%; background: url(")
81         + imgpath
82         + QStringLiteral(
83                      "top.png); \"></td> \n"
84                      "   <td style=\"min-width: 6px; background: url(")
85         + imgpath
86         + QStringLiteral(
87                      "top_right.png); \"></td> </tr> \n"
88                      "   <tr> \n"
89                      "   <td style=\"min-width: 6px; max-width: 6px; background: url(")
90         + imgpath
91         + QStringLiteral(
92                      "left.png); \"></td> \n"
93                      "   <td style=\"\"> \n");
94     headerStr += QStringLiteral(
95                      "<div class=\"noprint\" style=\"z-index: 1; float:right; position: relative; top: -35px; right: 20px ; max-height: 65px\">\n"
96                      "<img src=\"")
97         + imgpath
98         + QStringLiteral(
99                      "icon.png\"/>\n"
100                      "</div>\n");
101     headerStr +=
102         QLatin1String("    <table style=\"color: ") + fontColor.name() + QLatin1String(" ! important; margin: 1px; border-spacing: 0px;\" cellpadding='0'> \n");
103 
104     // subject
105     if (strategy->showHeader(QStringLiteral("subject"))) {
106         const KTextToHTML::Options flags = KTextToHTML::PreserveSpaces | KTextToHTML::ReplaceSmileys;
107 
108         headerStr += QStringLiteral(
109                          "     <tr> \n"
110                          "      <td style=\"font-size: 0.5em; text-align: right; padding-left: 5px; padding-right: 24px; ")
111             + borderSettings
112             + QStringLiteral(
113                          "\"></td> \n"
114                          "      <td style=\"font-weight: bolder; font-size: 120%; padding-right: 91px; ")
115             + borderSettings + QStringLiteral("\">");
116         headerStr += mHeaderStyleUtil.subjectString(message, flags)
117             + QStringLiteral(
118                          "</td> \n"
119                          "     </tr> \n");
120     }
121 
122     // from
123     if (strategy->showHeader(QStringLiteral("from"))) {
124         // We by design use the stripped mail address here, it is more enterprise-like.
125         QString fromPart = StringUtil::emailAddrAsAnchor(message->from(), StringUtil::DisplayFullAddress, linkColor);
126         if (!vCardName().isEmpty()) {
127             fromPart += QLatin1String("&nbsp;&nbsp;<a href=\"") + vCardName() + QLatin1String("\" ") + linkColor + QLatin1Char('>') + i18n("[vCard]")
128                 + QLatin1String("</a>");
129         }
130         // TDDO strategy date
131         // if ( strategy->showHeader( "date" ) )
132         headerStr += QStringLiteral(
133                          "     <tr> \n"
134                          "      <td style=\"font-size: 0.8em; padding-left: 5px; padding-right: 24px; text-align: right; vertical-align:top; ")
135             + borderSettings + QLatin1String("\">") + i18n("From: ")
136             + QStringLiteral(
137                          "</td> \n"
138                          "      <td style=\"")
139             + borderSettings + QStringLiteral("\">") + fromPart
140             + QStringLiteral(
141                          "</td> "
142                          "     </tr> ");
143     }
144 
145     // to line
146     if (strategy->showHeader(QStringLiteral("to"))) {
147         headerStr += QStringLiteral(
148                          "     <tr> "
149                          "      <td style=\"font-size: 0.8em; text-align: right; vertical-align:top; padding-left: 5px; padding-right: 24px; ")
150             + borderSettings + QLatin1String("\">") + i18n("To: ")
151             + QStringLiteral(
152                          "</td> "
153                          "      <td style=\"")
154             + borderSettings + QStringLiteral("\">") + StringUtil::emailAddrAsAnchor(message->to(), StringUtil::DisplayFullAddress, linkColor)
155             + QStringLiteral(
156                          "      </td> "
157                          "     </tr>\n");
158     }
159 
160     // cc line, if any
161     if (strategy->showHeader(QStringLiteral("cc")) && message->cc(false)) {
162         headerStr += QStringLiteral(
163                          "     <tr> "
164                          "      <td style=\"font-size: 0.8em; text-align: right; vertical-align:top; padding-left: 5px; padding-right: 24px; ")
165             + borderSettings + QLatin1String("\">") + i18n("CC: ")
166             + QStringLiteral(
167                          "</td> "
168                          "      <td style=\"")
169             + borderSettings + QStringLiteral("\">") + StringUtil::emailAddrAsAnchor(message->cc(), StringUtil::DisplayFullAddress, linkColor)
170             + QStringLiteral(
171                          "      </td> "
172                          "     </tr>\n");
173     }
174 
175     // bcc line, if any
176     if (strategy->showHeader(QStringLiteral("bcc")) && message->bcc(false)) {
177         headerStr += QStringLiteral(
178                          "     <tr> "
179                          "      <td style=\"font-size: 0.8em; text-align: right; vertical-align:top; padding-left: 5px; padding-right: 24px; ")
180             + borderSettings + QLatin1String("\">") + i18n("BCC: ")
181             + QStringLiteral(
182                          "</td> "
183                          "      <td style=\"")
184             + borderSettings + QStringLiteral("\">") + StringUtil::emailAddrAsAnchor(message->bcc(), StringUtil::DisplayFullAddress, linkColor)
185             + QStringLiteral(
186                          "      </td> "
187                          "     </tr>\n");
188     }
189 
190     // attachments
191     QString attachment;
192     if (isTopLevel()) {
193         attachment = QStringLiteral(
194                          "<tr>"
195                          "<td style='min-width: 6px; max-width: 6px; background: url(")
196             + imgpath
197             + QStringLiteral(
198                   "left.png);'></td>"
199                   "<td style='padding-right:20px;'>"
200                   "<div class=\"noprint\" >"
201                   "<div>%1</div>"
202                   "</div>"
203                   "</td>"
204                   "<td style='min-width: 6px; max-width: 6px; background: url(")
205                   .arg(attachmentHtml())
206             + imgpath
207             + QStringLiteral(
208                          "right.png);'></td>"
209                          "</tr>");
210     }
211 
212     // header-bottom
213     headerStr += QStringLiteral(
214                      "    </table> \n"
215                      "   </td> \n"
216                      "   <td style=\"min-width: 6px; max-height: 15px; background: url(")
217         + imgpath
218         + QStringLiteral(
219                      "right.png); \"></td> \n"
220                      "  </tr> \n")
221         + attachment
222         + QStringLiteral(
223                      "  <tr> \n"
224                      "   <td style=\"min-width: 6px; background: url(")
225         + imgpath
226         + QStringLiteral(
227                      "s_left.png); \"></td> \n"
228                      "   <td style=\"height: 35px; width: 80%; background: url(")
229         + imgpath
230         + QStringLiteral(
231                      "sbar.png);\"> \n"
232                      "    <img src=\"")
233         + imgpath
234         + QStringLiteral(
235                      "sw.png\" style=\"margin: 0px; height: 30px; overflow:hidden; \"/> \n"
236                      "    <img src=\"")
237         + imgpath
238         + QStringLiteral(
239                      "sp_right.png\" style=\"float: right; \"/> </td> \n"
240                      "   <td style=\"min-width: 6px; background: url(")
241         + imgpath
242         + QStringLiteral(
243                      "s_right.png); \"></td> \n"
244                      "  </tr> \n"
245                      " </table> \n");
246 
247     if (isPrinting()) {
248         // provide a bit more left padding when printing
249         // kolab/issue3254 (printed mail cut at the left side)
250         headerStr += QLatin1String("<div style=\"padding: 6px; padding-left: 10px;\">");
251     } else {
252         headerStr += QLatin1String("<div style=\"padding: 6px;\">");
253     }
254 
255     // TODO
256     // spam status
257     // ### iterate over the rest of strategy->headerToDisplay() (or
258     // ### all headers if DefaultPolicy == Display) (elsewhere, too)
259     return headerStr;
260 }
261 
name() const262 const char *MessageViewer::EnterpriseHeaderStyle::name() const
263 {
264     return "enterprise";
265 }
266