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

messageviewer

  • sources
  • kde-4.12
  • kdepim
  • messageviewer
  • header
grantleeheaderformatter.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2013 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 
31 #include <KLocale>
32 #include <KStandardDirs>
33 
34 #include <grantlee/templateloader.h>
35 #include <grantlee/engine.h>
36 
37 using namespace MessageCore;
38 
39 namespace MessageViewer {
40 
41 class GrantleeHeaderFormatter::Private
42 {
43 public:
44  Private()
45  {
46  engine = new Grantlee::Engine;
47  engine->setPluginPaths( QStringList() << QLatin1String(GRANTLEE_PLUGIN_PATH) << QLatin1String(MESSAGEVIEWER_GRANTLEE_PLUGIN_PATH));
48  templateLoader = Grantlee::FileSystemTemplateLoader::Ptr( new Grantlee::FileSystemTemplateLoader );
49  engine->addTemplateLoader( templateLoader );
50  }
51  ~Private()
52  {
53  delete engine;
54  }
55 
56  Grantlee::FileSystemTemplateLoader::Ptr templateLoader;
57  Grantlee::Engine *engine;
58 };
59 
60 GrantleeHeaderFormatter::GrantleeHeaderFormatter()
61  : d(new GrantleeHeaderFormatter::Private)
62 {
63 }
64 
65 GrantleeHeaderFormatter::~GrantleeHeaderFormatter()
66 {
67  delete d;
68 }
69 
70 QString GrantleeHeaderFormatter::toHtml(const QStringList &displayExtraHeaders, const QString &absolutPath, const QString &filename, const MessageViewer::HeaderStyle *style, KMime::Message *message, bool isPrinting) const
71 {
72  d->templateLoader->setTemplateDirs( QStringList() << absolutPath );
73  Grantlee::Template headerTemplate = d->engine->loadByName( filename );
74  qDebug()<<" absolutPath"<<absolutPath;
75  if ( headerTemplate->error() ) {
76  return headerTemplate->errorString();
77  }
78  return format(absolutPath, headerTemplate, displayExtraHeaders, isPrinting, style, message);
79 }
80 
81 QString GrantleeHeaderFormatter::toHtml(const GrantleeTheme::Theme &theme, bool isPrinting, const MessageViewer::HeaderStyle *style, KMime::Message *message) const
82 {
83  QString errorMessage;
84  if (!theme.isValid()) {
85  errorMessage = i18n("Grantlee theme \"%1\" is not valid.", theme.name());
86  return errorMessage;
87  }
88  d->templateLoader->setTemplateDirs( QStringList() << theme.absolutePath() );
89  Grantlee::Template headerTemplate = d->engine->loadByName( theme.filename() );
90  if ( headerTemplate->error() ) {
91  errorMessage = headerTemplate->errorString();
92  return errorMessage;
93  }
94  return format(theme.absolutePath(), headerTemplate, theme.displayExtraVariables(), isPrinting, style, message);
95 }
96 
97 QString GrantleeHeaderFormatter::format(const QString &absolutePath, Grantlee::Template headerTemplate, const QStringList &displayExtraHeaders, bool isPrinting, const MessageViewer::HeaderStyle *style, KMime::Message *message) const
98 {
99  QVariantHash headerObject;
100 
101  // However, the direction of the message subject within the header is
102  // determined according to the contents of the subject itself. Since
103  // the "Re:" and "Fwd:" prefixes would always cause the subject to be
104  // considered left-to-right, they are ignored when determining its
105  // direction.
106  const QString absoluteThemePath = QLatin1String("file://") + absolutePath + QLatin1Char('/');
107  headerObject.insert(QLatin1String("absoluteThemePath"), absoluteThemePath);
108  headerObject.insert(QLatin1String("applicationDir"), QApplication::isRightToLeft() ? QLatin1String("rtl") : QLatin1String("ltr"));
109  headerObject.insert(QLatin1String("subjectDir"), MessageViewer::HeaderStyleUtil::subjectDirectionString( message ));
110 
111  headerObject.insert(QLatin1String("subjecti18n"), i18n("Subject:") );
112  headerObject.insert(QLatin1String("subject"), MessageViewer::HeaderStyleUtil::subjectString( message ) );
113 
114  headerObject.insert(QLatin1String("toi18n"), i18n("To:") );
115  headerObject.insert(QLatin1String("to"), StringUtil::emailAddrAsAnchor( message->to(), StringUtil::DisplayFullAddress ));
116  headerObject.insert(QLatin1String("toStr"), message->to()->asUnicodeString());
117 
118  if ( message->replyTo( false )) {
119  headerObject.insert(QLatin1String("replyToi18n"), i18n("Reply to:") );
120  headerObject.insert(QLatin1String("replyTo"), StringUtil::emailAddrAsAnchor( message->replyTo(), StringUtil::DisplayFullAddress ));
121  headerObject.insert(QLatin1String("replyToStr"), message->replyTo()->asUnicodeString());
122  }
123 
124  if ( message->cc( false ) ) {
125  headerObject.insert(QLatin1String("cci18n"), i18n("CC:") );
126  headerObject.insert(QLatin1String("cc"), StringUtil::emailAddrAsAnchor( message->cc(), StringUtil::DisplayFullAddress ));
127  headerObject.insert(QLatin1String("ccStr"), message->cc()->asUnicodeString());
128  }
129 
130  if ( message->bcc( false ) ) {
131  headerObject.insert(QLatin1String("bcci18n"), i18n("BCC:"));
132  headerObject.insert(QLatin1String("bcc"), StringUtil::emailAddrAsAnchor( message->bcc(), StringUtil::DisplayFullAddress ));
133  headerObject.insert(QLatin1String("bccStr"), message->bcc()->asUnicodeString());
134  }
135  headerObject.insert(QLatin1String("fromi18n"), i18n("From:"));
136  headerObject.insert(QLatin1String( "from" ) , StringUtil::emailAddrAsAnchor( message->from(), StringUtil::DisplayFullAddress ) );
137  headerObject.insert(QLatin1String( "fromStr" ) , message->from()->asUnicodeString() );
138 
139 
140  const QString spamHtml = MessageViewer::HeaderStyleUtil::spamStatus(message);
141  if ( !spamHtml.isEmpty() ) {
142  headerObject.insert( QLatin1String("spamstatusi18n"), i18n("Spam Status:"));
143  headerObject.insert( QLatin1String( "spamHTML" ), spamHtml );
144  }
145  headerObject.insert(QLatin1String("datei18n"), i18n("Date:"));
146 
147  headerObject.insert( QLatin1String( "dateshort" ) , MessageViewer::HeaderStyleUtil::strToHtml( MessageViewer::HeaderStyleUtil::dateString(message, isPrinting,true ) ) );
148  headerObject.insert( QLatin1String( "datelong" ) , MessageViewer::HeaderStyleUtil::strToHtml( MessageViewer::HeaderStyleUtil::dateString(message, isPrinting,false ) ) );
149  headerObject.insert( QLatin1String( "date" ), MessageViewer::HeaderStyleUtil::dateStr( message->date()->dateTime() ) );
150 
151  if ( GlobalSettings::self()->showUserAgent() ) {
152  if ( message->headerByType("User-Agent") ) {
153  headerObject.insert( QLatin1String( "useragent" ), MessageViewer::HeaderStyleUtil::strToHtml( QLatin1String(message->headerByType("User-Agent")->as7BitString() )) );
154  }
155 
156  if ( message->headerByType("X-Mailer") ) {
157  headerObject.insert( QLatin1String( "xmailer" ), MessageViewer::HeaderStyleUtil::strToHtml( QLatin1String(message->headerByType("X-Mailer")->as7BitString() )) );
158  }
159  }
160 
161  if ( message->headerByType( "Resent-From" ) ) {
162  headerObject.insert( QLatin1String( "resentfromi18n"), i18n("resent from"));
163  const QList<KMime::Types::Mailbox> resentFrom = MessageViewer::HeaderStyleUtil::resentFromList(message);
164  headerObject.insert( QLatin1String( "resentfrom" ), StringUtil::emailAddrAsAnchor( resentFrom, StringUtil::DisplayFullAddress ) );
165  }
166 
167  if ( message->headerByType( "Resent-To" ) ) {
168  const QList<KMime::Types::Mailbox> resentTo = MessageViewer::HeaderStyleUtil::resentToList(message);
169  headerObject.insert( QLatin1String( "resenttoi18n"), i18np("receiver was", "receivers were", resentTo.count()));
170  headerObject.insert( QLatin1String( "resentto" ), StringUtil::emailAddrAsAnchor( resentTo, StringUtil::DisplayFullAddress ) );
171  }
172 
173  if ( KMime::Headers::Base *organization = message->headerByType("Organization") )
174  headerObject.insert( QLatin1String( "organization" ) , MessageViewer::HeaderStyleUtil::strToHtml(organization->asUnicodeString()) );
175 
176  if ( !style->vCardName().isEmpty() )
177  headerObject.insert( QLatin1String( "vcardname" ) , style->vCardName() );
178 
179  if ( isPrinting ) {
180  //provide a bit more left padding when printing
181  //kolab/issue3254 (printed mail cut at the left side)
182  //Use it just for testing if we are in printing mode
183  headerObject.insert( QLatin1String( "isprinting" ) , i18n("Printing mode") );
184  }
185 
186  // colors depend on if it is encapsulated or not
187  QColor fontColor( Qt::white );
188  QString linkColor = QLatin1String("white");
189  const QColor activeColor = KColorScheme( QPalette::Active, KColorScheme::Selection ).background().color();
190  QColor activeColorDark = activeColor.dark(130);
191  // reverse colors for encapsulated
192  if ( !style->isTopLevel() ) {
193  activeColorDark = activeColor.dark(50);
194  fontColor = QColor(Qt::black);
195  linkColor = QLatin1String("black");
196  }
197 
198  // 3D borders
199  headerObject.insert( QLatin1String( "activecolordark" ), activeColorDark.name() );
200  headerObject.insert( QLatin1String( "fontcolor" ), fontColor.name() );
201  headerObject.insert( QLatin1String( "linkcolor" ) , linkColor );
202 
203 
204  MessageViewer::HeaderStyleUtil::xfaceSettings xface = MessageViewer::HeaderStyleUtil::xface(style, message);
205  if ( !xface.photoURL.isEmpty() ) {
206  headerObject.insert( QLatin1String( "photowidth" ) , xface.photoWidth );
207  headerObject.insert( QLatin1String( "photoheight" ) , xface.photoHeight );
208  headerObject.insert( QLatin1String( "photourl" ) , xface.photoURL );
209  }
210 
211  Q_FOREACH (QString header, displayExtraHeaders) {
212  const QByteArray baHeader = header.toLocal8Bit();
213  if (message->headerByType(baHeader) ) {
214  //Grantlee doesn't support '-' in variable name => remove it.
215  header = header.remove(QLatin1Char('-'));
216  headerObject.insert( header , message->headerByType(baHeader)->asUnicodeString() );
217  }
218  }
219 
220  headerObject.insert( QLatin1String( "vcardi18n" ), i18n("[vcard]") );
221 
222 
223  QVariantHash mapping;
224  mapping.insert( QLatin1String("header"), headerObject );
225  Grantlee::Context context( mapping );
226 
227  return headerTemplate->render(&context);
228 }
229 
230 }
globalsettings.h
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
MessageViewer::HeaderStyleUtil::resentToList
QList< KMime::Types::Mailbox > resentToList(KMime::Message *message)
Definition: headerstyle_util.cpp:243
grantleeheaderformatter.h
MessageViewer::HeaderStyleUtil::xface
xfaceSettings xface(const MessageViewer::HeaderStyle *style, KMime::Message *message)
Definition: headerstyle_util.cpp:262
MessageViewer::HeaderStyleUtil::xfaceSettings::photoHeight
int photoHeight
Definition: headerstyle_util.h:70
MessageViewer::HeaderStyleUtil::xfaceSettings::photoWidth
int photoWidth
Definition: headerstyle_util.h:69
MessageViewer::HeaderStyleUtil::xfaceSettings
Definition: headerstyle_util.h:61
grantleeheaderstyle.h
MessageViewer::HeaderStyleUtil::dateStr
QString dateStr(const KDateTime &dateTime)
Definition: headerstyle_util.cpp:209
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
MessageViewer::HeaderStyleUtil::subjectDirectionString
QString subjectDirectionString(KMime::Message *message)
Definition: headerstyle_util.cpp:84
MessageViewer::HeaderStyleUtil::subjectString
QString subjectString(KMime::Message *message, int flags)
Definition: headerstyle_util.cpp:69
MessageViewer::HeaderStyleUtil::spamStatus
QString spamStatus(KMime::Message *message)
Definition: headerstyle_util.cpp:94
headerstyle_util.h
MessageViewer::GrantleeHeaderFormatter::~GrantleeHeaderFormatter
~GrantleeHeaderFormatter()
Definition: grantleeheaderformatter.cpp:65
MessageViewer::HeaderStyleUtil::resentFromList
QList< KMime::Types::Mailbox > resentFromList(KMime::Message *message)
Definition: headerstyle_util.cpp:224
MessageViewer::GrantleeHeaderFormatter::toHtml
QString toHtml(const GrantleeTheme::Theme &theme, bool isPrinting, const MessageViewer::HeaderStyle *style, KMime::Message *message) const
Definition: grantleeheaderformatter.cpp:81
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-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:57 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

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