• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

messageviewer

  • sources
  • kde-4.14
  • kdepim
  • messageviewer
  • header
grantleeheaderformatter.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2013-2015 Montel Laurent <montel@kde.org>
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License, version 2, as
6  published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful, but
9  WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include "grantleeheaderformatter.h"
19 #include "grantleeheaderstyle.h"
20 #include "headerstyle_util.h"
21 #include "settings/globalsettings.h"
22 #include "config-messageviewer.h"
23 
24 #include <kpimutils/email.h>
25 #include <messagecore/utils/stringutil.h>
26 
27 #include <kmime/kmime_message.h>
28 #include <kmime/kmime_dateformatter.h>
29 
30 #include <KLocalizedString>
31 #include <KStandardDirs>
32 
33 #include <grantlee/templateloader.h>
34 #include <grantlee/engine.h>
35 
36 using namespace MessageCore;
37 
38 namespace MessageViewer {
39 
40 class GrantleeHeaderFormatter::Private
41 {
42 public:
43  Private()
44  {
45  engine = new Grantlee::Engine;
46  const QStringList pluginDirs = KGlobal::dirs()->resourceDirs("qtplugins");
47  foreach(const QString & pluginDir, pluginDirs) {
48  engine->addPluginPath(pluginDir);
49  }
50  engine->addDefaultLibrary( QLatin1String("grantlee_messageheaderfilters") );
51  templateLoader = Grantlee::FileSystemTemplateLoader::Ptr( new Grantlee::FileSystemTemplateLoader );
52  engine->addTemplateLoader( templateLoader );
53  }
54  ~Private()
55  {
56  delete engine;
57  }
58 
59  Grantlee::FileSystemTemplateLoader::Ptr templateLoader;
60  Grantlee::Engine *engine;
61 };
62 
63 GrantleeHeaderFormatter::GrantleeHeaderFormatter()
64  : d(new GrantleeHeaderFormatter::Private)
65 {
66 }
67 
68 GrantleeHeaderFormatter::~GrantleeHeaderFormatter()
69 {
70  delete d;
71 }
72 
73 QString GrantleeHeaderFormatter::toHtml(const QStringList &displayExtraHeaders, const QString &absolutPath, const QString &filename, const MessageViewer::HeaderStyle *style, KMime::Message *message, bool isPrinting) const
74 {
75  d->templateLoader->setTemplateDirs( QStringList() << absolutPath );
76  Grantlee::Template headerTemplate = d->engine->loadByName( filename );
77  if ( headerTemplate->error() ) {
78  return headerTemplate->errorString();
79  }
80  return format(absolutPath, headerTemplate, displayExtraHeaders, isPrinting, style, message);
81 }
82 
83 QString GrantleeHeaderFormatter::toHtml(const GrantleeTheme::Theme &theme, bool isPrinting, const MessageViewer::HeaderStyle *style, KMime::Message *message) const
84 {
85  QString errorMessage;
86  if (!theme.isValid()) {
87  errorMessage = i18n("Grantlee theme \"%1\" is not valid.", theme.name());
88  return errorMessage;
89  }
90  d->templateLoader->setTemplateDirs( QStringList() << theme.absolutePath() );
91  Grantlee::Template headerTemplate = d->engine->loadByName( theme.themeFilename() );
92  if ( headerTemplate->error() ) {
93  errorMessage = headerTemplate->errorString();
94  return errorMessage;
95  }
96  return format(theme.absolutePath(), headerTemplate, theme.displayExtraVariables(), isPrinting, style, message);
97 }
98 
99 QString GrantleeHeaderFormatter::format(const QString &absolutePath, Grantlee::Template headerTemplate, const QStringList &displayExtraHeaders, bool isPrinting, const MessageViewer::HeaderStyle *style, KMime::Message *message) const
100 {
101  QVariantHash headerObject;
102 
103  // However, the direction of the message subject within the header is
104  // determined according to the contents of the subject itself. Since
105  // the "Re:" and "Fwd:" prefixes would always cause the subject to be
106  // considered left-to-right, they are ignored when determining its
107  // direction.
108  const QString absoluteThemePath = QLatin1String("file://") + absolutePath + QLatin1Char('/');
109  headerObject.insert(QLatin1String("absoluteThemePath"), absoluteThemePath);
110  headerObject.insert(QLatin1String("applicationDir"), QApplication::isRightToLeft() ? QLatin1String("rtl") : QLatin1String("ltr"));
111  headerObject.insert(QLatin1String("subjectDir"), MessageViewer::HeaderStyleUtil::subjectDirectionString( message ));
112 
113  headerObject.insert(QLatin1String("subjecti18n"), i18n("Subject:") );
114  headerObject.insert(QLatin1String("subject"), MessageViewer::HeaderStyleUtil::subjectString( message ) );
115 
116  headerObject.insert(QLatin1String("toi18n"), i18n("To:") );
117  headerObject.insert(QLatin1String("to"), StringUtil::emailAddrAsAnchor( message->to(), StringUtil::DisplayFullAddress ));
118  headerObject.insert(QLatin1String("toStr"), message->to()->asUnicodeString());
119  const QString val = MessageCore::StringUtil::emailAddrAsAnchor( message->to(), MessageCore::StringUtil::DisplayFullAddress,
120  QString(), MessageCore::StringUtil::ShowLink,
121  MessageCore::StringUtil::ExpandableAddresses, QLatin1String("FullToAddressList"),
122  GlobalSettings::self()->numberOfAddressesToShow() );
123  headerObject.insert(QLatin1String("toExpandable"), val);
124  headerObject.insert(QLatin1String("toMailbox"), QVariant::fromValue(message->to()));
125 
126  if ( message->replyTo( false )) {
127  headerObject.insert(QLatin1String("replyToi18n"), i18n("Reply to:") );
128  headerObject.insert(QLatin1String("replyTo"), StringUtil::emailAddrAsAnchor( message->replyTo(), StringUtil::DisplayFullAddress ));
129  headerObject.insert(QLatin1String("replyToStr"), message->replyTo()->asUnicodeString());
130  }
131 
132  if ( message->cc( false ) ) {
133  headerObject.insert(QLatin1String("cci18n"), i18n("CC:") );
134  headerObject.insert(QLatin1String("cc"), StringUtil::emailAddrAsAnchor( message->cc(), StringUtil::DisplayFullAddress ));
135  headerObject.insert(QLatin1String("ccStr"), message->cc()->asUnicodeString());
136  headerObject.insert(QLatin1String("ccMailbox"), QVariant::fromValue(message->cc()));
137  const QString val = MessageCore::StringUtil::emailAddrAsAnchor( message->cc(), MessageCore::StringUtil::DisplayFullAddress,
138  QString(), MessageCore::StringUtil::ShowLink,
139  MessageCore::StringUtil::ExpandableAddresses, QLatin1String("FullToAddressList"),
140  GlobalSettings::self()->numberOfAddressesToShow() );
141  headerObject.insert(QLatin1String("ccExpandable"), val);
142  }
143 
144  if ( message->bcc( false ) ) {
145  headerObject.insert(QLatin1String("bcci18n"), i18n("BCC:"));
146  headerObject.insert(QLatin1String("bcc"), StringUtil::emailAddrAsAnchor( message->bcc(), StringUtil::DisplayFullAddress ));
147  headerObject.insert(QLatin1String("bccStr"), message->bcc()->asUnicodeString());
148  headerObject.insert(QLatin1String("bccMailbox"), QVariant::fromValue(message->bcc()));
149  const QString val = MessageCore::StringUtil::emailAddrAsAnchor( message->bcc(), MessageCore::StringUtil::DisplayFullAddress,
150  QString(), MessageCore::StringUtil::ShowLink,
151  MessageCore::StringUtil::ExpandableAddresses, QLatin1String("FullToAddressList"),
152  GlobalSettings::self()->numberOfAddressesToShow() );
153  headerObject.insert(QLatin1String("bccExpandable"), val);
154  }
155  headerObject.insert(QLatin1String("fromi18n"), i18n("From:"));
156  headerObject.insert(QLatin1String( "from" ) , StringUtil::emailAddrAsAnchor( message->from(), StringUtil::DisplayFullAddress ) );
157  headerObject.insert(QLatin1String( "fromStr" ) , message->from()->asUnicodeString() );
158 
159 
160  const QString spamHtml = MessageViewer::HeaderStyleUtil::spamStatus(message);
161  if ( !spamHtml.isEmpty() ) {
162  headerObject.insert( QLatin1String("spamstatusi18n"), i18n("Spam Status:"));
163  headerObject.insert( QLatin1String( "spamHTML" ), spamHtml );
164  }
165  headerObject.insert(QLatin1String("datei18n"), i18n("Date:"));
166 
167  headerObject.insert( QLatin1String( "dateshort" ) , MessageViewer::HeaderStyleUtil::strToHtml( MessageViewer::HeaderStyleUtil::dateString(message, isPrinting,true ) ) );
168  headerObject.insert( QLatin1String( "datelong" ) , MessageViewer::HeaderStyleUtil::strToHtml( MessageViewer::HeaderStyleUtil::dateString(message, isPrinting,false ) ) );
169  headerObject.insert( QLatin1String( "date" ), MessageViewer::HeaderStyleUtil::dateStr( message->date()->dateTime() ) );
170 
171  if ( GlobalSettings::self()->showUserAgent() ) {
172  if ( message->headerByType("User-Agent") ) {
173  headerObject.insert( QLatin1String( "useragent" ), MessageViewer::HeaderStyleUtil::strToHtml( message->headerByType("User-Agent")->asUnicodeString() ) );
174  }
175 
176  if ( message->headerByType("X-Mailer") ) {
177  headerObject.insert( QLatin1String( "xmailer" ), MessageViewer::HeaderStyleUtil::strToHtml( message->headerByType("X-Mailer")->asUnicodeString() ) );
178  }
179  }
180 
181  if ( message->headerByType( "Resent-From" ) ) {
182  headerObject.insert( QLatin1String( "resentfromi18n"), i18n("resent from"));
183  const QList<KMime::Types::Mailbox> resentFrom = MessageViewer::HeaderStyleUtil::resentFromList(message);
184  headerObject.insert( QLatin1String( "resentfrom" ), StringUtil::emailAddrAsAnchor( resentFrom, StringUtil::DisplayFullAddress ) );
185  }
186 
187  if ( message->headerByType( "Resent-To" ) ) {
188  const QList<KMime::Types::Mailbox> resentTo = MessageViewer::HeaderStyleUtil::resentToList(message);
189  headerObject.insert( QLatin1String( "resenttoi18n"), i18np("receiver was", "receivers were", resentTo.count()));
190  headerObject.insert( QLatin1String( "resentto" ), StringUtil::emailAddrAsAnchor( resentTo, StringUtil::DisplayFullAddress ) );
191  }
192 
193  if ( KMime::Headers::Base *organization = message->headerByType("Organization") )
194  headerObject.insert( QLatin1String( "organization" ) , MessageViewer::HeaderStyleUtil::strToHtml(organization->asUnicodeString()) );
195 
196  if ( !style->vCardName().isEmpty() )
197  headerObject.insert( QLatin1String( "vcardname" ) , style->vCardName() );
198 
199  if ( isPrinting ) {
200  //provide a bit more left padding when printing
201  //kolab/issue3254 (printed mail cut at the left side)
202  //Use it just for testing if we are in printing mode
203  headerObject.insert( QLatin1String( "isprinting" ) , i18n("Printing mode") );
204  }
205 
206  // colors depend on if it is encapsulated or not
207  QColor fontColor( Qt::white );
208  QString linkColor = QLatin1String("white");
209  const QColor activeColor = KColorScheme( QPalette::Active, KColorScheme::Selection ).background().color();
210  QColor activeColorDark = activeColor.dark(130);
211  // reverse colors for encapsulated
212  if ( !style->isTopLevel() ) {
213  activeColorDark = activeColor.dark(50);
214  fontColor = QColor(Qt::black);
215  linkColor = QLatin1String("black");
216  }
217 
218  // 3D borders
219  headerObject.insert( QLatin1String( "activecolordark" ), activeColorDark.name() );
220  headerObject.insert( QLatin1String( "fontcolor" ), fontColor.name() );
221  headerObject.insert( QLatin1String( "linkcolor" ) , linkColor );
222 
223 
224  MessageViewer::HeaderStyleUtil::xfaceSettings xface = MessageViewer::HeaderStyleUtil::xface(style, message);
225  if ( !xface.photoURL.isEmpty() ) {
226  headerObject.insert( QLatin1String( "photowidth" ) , xface.photoWidth );
227  headerObject.insert( QLatin1String( "photoheight" ) , xface.photoHeight );
228  headerObject.insert( QLatin1String( "photourl" ) , xface.photoURL );
229  }
230 
231  Q_FOREACH (QString header, displayExtraHeaders) {
232  const QByteArray baHeader = header.toLocal8Bit();
233  if (message->headerByType(baHeader) ) {
234  //Grantlee doesn't support '-' in variable name => remove it.
235  header = header.remove(QLatin1Char('-'));
236  headerObject.insert( header , message->headerByType(baHeader)->asUnicodeString() );
237  }
238  }
239 
240  headerObject.insert( QLatin1String( "vcardi18n" ), i18n("[vcard]") );
241 
242 
243  QVariantHash mapping;
244  mapping.insert( QLatin1String("header"), headerObject );
245  Grantlee::Context context( mapping );
246 
247  return headerTemplate->render(&context);
248 }
249 
250 }
251 Q_DECLARE_METATYPE(KMime::Headers::Cc*)
252 Q_DECLARE_METATYPE(KMime::Headers::To*)
253 Q_DECLARE_METATYPE(KMime::Headers::Bcc*)
globalsettings.h
QApplication::isRightToLeft
bool isRightToLeft()
MessageViewer::HeaderStyle
This class encapsulates the visual appearance of message headers.
Definition: headerstyle.h:60
MessageViewer::HeaderStyleUtil::dateString
QString dateString(KMime::Message *message, bool printing, bool shortDate)
Definition: headerstyle_util.cpp:54
QByteArray
QColor::name
QString name() const
MessageViewer::HeaderStyleUtil::resentToList
QList< KMime::Types::Mailbox > resentToList(KMime::Message *message)
Definition: headerstyle_util.cpp:244
grantleeheaderformatter.h
MessageViewer::HeaderStyleUtil::xface
xfaceSettings xface(const MessageViewer::HeaderStyle *style, KMime::Message *message)
Definition: headerstyle_util.cpp:279
MessageViewer::HeaderStyleUtil::xfaceSettings::photoHeight
int photoHeight
Definition: headerstyle_util.h:70
MessageViewer::HeaderStyleUtil::xfaceSettings::photoWidth
int photoWidth
Definition: headerstyle_util.h:69
QString::remove
QString & remove(int position, int n)
MessageViewer::HeaderStyleUtil::xfaceSettings
Definition: headerstyle_util.h:61
QColor::dark
QColor dark(int factor) const
grantleeheaderstyle.h
MessageViewer::HeaderStyleUtil::dateStr
QString dateStr(const KDateTime &dateTime)
Definition: headerstyle_util.cpp:210
QList::count
int count(const T &value) const
QString::insert
QString & insert(int position, QChar ch)
QString::isEmpty
bool isEmpty() const
MessageViewer::GlobalSettings::self
static GlobalSettings * self()
Definition: globalsettings.cpp:34
MessageViewer::HeaderStyle::isTopLevel
bool isTopLevel() const
Definition: headerstyle.h:118
MessageViewer::GrantleeHeaderFormatter
Definition: grantleeheaderformatter.h:33
QString
QList
QColor
MessageViewer::HeaderStyleUtil::subjectDirectionString
QString subjectDirectionString(KMime::Message *message)
Definition: headerstyle_util.cpp:85
QStringList
QString::toLocal8Bit
QByteArray toLocal8Bit() const
MessageViewer::HeaderStyleUtil::subjectString
QString subjectString(KMime::Message *message, int flags)
Definition: headerstyle_util.cpp:69
QLatin1Char
QVariant::fromValue
QVariant fromValue(const T &value)
MessageViewer::HeaderStyleUtil::spamStatus
QString spamStatus(KMime::Message *message)
Definition: headerstyle_util.cpp:95
headerstyle_util.h
QLatin1String
MessageViewer::GrantleeHeaderFormatter::~GrantleeHeaderFormatter
~GrantleeHeaderFormatter()
Definition: grantleeheaderformatter.cpp:68
MessageViewer::HeaderStyleUtil::resentFromList
QList< KMime::Types::Mailbox > resentFromList(KMime::Message *message)
Definition: headerstyle_util.cpp:225
MessageViewer::GrantleeHeaderFormatter::toHtml
QString toHtml(const GrantleeTheme::Theme &theme, bool isPrinting, const MessageViewer::HeaderStyle *style, KMime::Message *message) const
Definition: grantleeheaderformatter.cpp:83
MessageViewer::HeaderStyleUtil::xfaceSettings::photoURL
QString photoURL
Definition: headerstyle_util.h:68
MessageViewer::HeaderStyleUtil::strToHtml
QString strToHtml(const QString &str, int flags)
Definition: headerstyle_util.cpp:49
MessageViewer::HeaderStyle::vCardName
QString vCardName() const
Definition: headerstyle.h:112
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:45 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

messageviewer

Skip menu "messageviewer"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal