• 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
  • viewer
bodypartformatter.cpp
Go to the documentation of this file.
1 /* -*- c++ -*-
2  bodypartformatter.cpp
3 
4  This file is part of KMail, the KDE mail client.
5  Copyright (c) 2003 Marc Mutz <mutz@kde.org>
6 
7  KMail is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License, version 2, as
9  published by the Free Software Foundation.
10 
11  KMail is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 
20  In addition, as a special exception, the copyright holders give
21  permission to link the code of this program with any edition of
22  the Qt library by Trolltech AS, Norway (or with modified versions
23  of Qt that use the same license as Qt), and distribute linked
24  combinations including the two. You must obey the GNU General
25  Public License in all respects for all of the code used other than
26  Qt. If you modify this file, you may extend this exception to
27  your version of the file, but you are not obligated to do so. If
28  you do not wish to do so, delete this exception statement from
29  your version.
30 */
31 
32 
33 
34 
35 #include "bodypartformatter.h"
36 #include "viewer/bodypartformatterfactory_p.h"
37 #include "interfaces/bodypartformatter.h"
38 
39 #include "viewer/objecttreeparser.h"
40 
41 #include <kmime/kmime_content.h>
42 
43 #include <kdebug.h>
44 #include <kascii.h>
45 
46 using namespace MessageViewer;
47 
48 namespace {
49 class AnyTypeBodyPartFormatter
50  : public BodyPartFormatter,
51  public MessageViewer::Interface::BodyPartFormatter {
52  static const AnyTypeBodyPartFormatter * self;
53 public:
54  Result format( Interface::BodyPart *, HtmlWriter * ) const {
55  kDebug() << "Acting as a Interface::BodyPartFormatter!";
56  return AsIcon;
57  }
58 
59  // unhide the overload with three arguments
60  using MessageViewer::Interface::BodyPartFormatter::format;
61 
62  bool process( ObjectTreeParser *, KMime::Content *, ProcessResult & result ) const {
63  result.setNeverDisplayInline( true );
64  return false;
65  }
66  static const ::BodyPartFormatter * create() {
67  if ( !self )
68  self = new AnyTypeBodyPartFormatter();
69  return self;
70  }
71 };
72 
73 const AnyTypeBodyPartFormatter * AnyTypeBodyPartFormatter::self = 0;
74 
75 
76 class ImageTypeBodyPartFormatter : public BodyPartFormatter {
77  static const ImageTypeBodyPartFormatter * self;
78 public:
79  bool process( ObjectTreeParser *, KMime::Content *, ProcessResult & result ) const {
80  result.setIsImage( true );
81  return false;
82  }
83  static const BodyPartFormatter * create() {
84  if ( !self )
85  self = new ImageTypeBodyPartFormatter();
86  return self;
87  }
88 };
89 
90 const ImageTypeBodyPartFormatter * ImageTypeBodyPartFormatter::self = 0;
91 
92 #define CREATE_BODY_PART_FORMATTER(subtype) \
93  class subtype##BodyPartFormatter : public BodyPartFormatter { \
94  static const subtype##BodyPartFormatter * self; \
95  public: \
96  bool process( ObjectTreeParser *, KMime::Content *, ProcessResult & ) const; \
97  static const BodyPartFormatter * create() { \
98  if ( !self ) \
99  self = new subtype##BodyPartFormatter(); \
100  return self; \
101 } \
102 }; \
103  \
104  const subtype##BodyPartFormatter * subtype##BodyPartFormatter::self; \
105  \
106  bool subtype##BodyPartFormatter::process( ObjectTreeParser * otp, KMime::Content * node, ProcessResult & result ) const { \
107  return otp->process##subtype##Subtype( node, result ); \
108 }
109 
110 
111 CREATE_BODY_PART_FORMATTER(TextPlain)
112 CREATE_BODY_PART_FORMATTER(TextHtml)
113 //CREATE_BODY_PART_FORMATTER(TextEnriched)
114 
115 CREATE_BODY_PART_FORMATTER(ApplicationOctetStream)
116 CREATE_BODY_PART_FORMATTER(ApplicationPkcs7Mime)
117 CREATE_BODY_PART_FORMATTER(ApplicationChiasmusText)
118 //CREATE_BODY_PART_FORMATTER(ApplicationPgp)
119 
120 CREATE_BODY_PART_FORMATTER(MessageRfc822)
121 
122 CREATE_BODY_PART_FORMATTER(MultiPartMixed)
123 CREATE_BODY_PART_FORMATTER(MultiPartAlternative)
124 CREATE_BODY_PART_FORMATTER(MultiPartSigned)
125 CREATE_BODY_PART_FORMATTER(MultiPartEncrypted)
126 
127 typedef TextPlainBodyPartFormatter ApplicationPgpBodyPartFormatter;
128 
129 
130 #undef CREATE_BODY_PART_FORMATTER
131 } // anon namespace
132 
133 // FIXME: port some more BodyPartFormatters to Interface::BodyPartFormatters
134 void BodyPartFormatterFactoryPrivate::messageviewer_create_builtin_bodypart_formatters( BodyPartFormatterFactoryPrivate::TypeRegistry * reg ) {
135  if ( !reg ) return;
136  (*reg)["application"]["octet-stream"] = new AnyTypeBodyPartFormatter();
137 }
138 
139 typedef const BodyPartFormatter * (*BodyPartFormatterCreator)();
140 
141 struct SubtypeBuiltin {
142  const char * subtype;
143  BodyPartFormatterCreator create;
144 };
145 
146 static const SubtypeBuiltin applicationSubtypeBuiltins[] = {
147  { "octet-stream", &ApplicationOctetStreamBodyPartFormatter::create },
148  { "pkcs7-mime", &ApplicationPkcs7MimeBodyPartFormatter::create },
149  { "x-pkcs7-mime", &ApplicationPkcs7MimeBodyPartFormatter::create },
150  { "vnd.de.bund.bsi.chiasmus-text", &ApplicationChiasmusTextBodyPartFormatter::create },
151  { "pgp", &ApplicationPgpBodyPartFormatter::create },
152 };
153 
154 static const SubtypeBuiltin textSubtypeBuiltins[] = {
155  { "html", &TextHtmlBodyPartFormatter::create },
156  //{ "enriched", &TextEnrichedBodyPartFormatter::create },
157  { "x-vcard", &AnyTypeBodyPartFormatter::create },
158  { "vcard", &AnyTypeBodyPartFormatter::create },
159  { "rtf", &AnyTypeBodyPartFormatter::create },
160  { "*", &TextPlainBodyPartFormatter::create },
161 };
162 
163 static const SubtypeBuiltin multipartSubtypeBuiltins[] = {
164  { "mixed", &MultiPartMixedBodyPartFormatter::create },
165  { "alternative", &MultiPartAlternativeBodyPartFormatter::create },
166  //{ "digest", &MultiPartDigestFormatter::create },
167  //{ "parallel", &MultiPartParallelFormatter::create },
168  //{ "related", &MultiPartRelatedFormatter::create },
169  { "signed", &MultiPartSignedBodyPartFormatter::create },
170  { "encrypted", &MultiPartEncryptedBodyPartFormatter::create },
171  //{ "report", &MultiPartReportFormatter::create },
172 };
173 
174 static const SubtypeBuiltin messageSubtypeBuiltins[] = {
175  { "rfc822", &MessageRfc822BodyPartFormatter::create },
176 };
177 
178 static const SubtypeBuiltin imageSubtypeBuiltins[] = {
179  { "*", &ImageTypeBodyPartFormatter::create },
180 };
181 
182 static const SubtypeBuiltin anySubtypeBuiltins[] = {
183  { "*", &AnyTypeBodyPartFormatter::create },
184 };
185 
186 #ifdef DIM
187 #undef DIM
188 #endif
189 #define DIM(x) sizeof(x) / sizeof(*x)
190 
191 static const struct {
192  const char * type;
193  const SubtypeBuiltin * subtypes;
194  unsigned int num_subtypes;
195 } builtins[] = {
196 { "application", applicationSubtypeBuiltins, DIM(applicationSubtypeBuiltins) },
197 { "text", textSubtypeBuiltins, DIM(textSubtypeBuiltins) },
198 { "multipart", multipartSubtypeBuiltins, DIM(multipartSubtypeBuiltins) },
199 { "message", messageSubtypeBuiltins, DIM(messageSubtypeBuiltins) },
200 { "image", imageSubtypeBuiltins, DIM(imageSubtypeBuiltins) },
201 //{ "audio", audioSubtypeBuiltins, DIM(audioSubtypeBuiltins) },
202 //{ "model", modelSubtypeBuiltins, DIM(modelSubtypeBuiltins) },
203 //{ "video", videoSubtypeBuiltins, DIM(videoSubtypeBuiltins) },
204 { "*", anySubtypeBuiltins, DIM(anySubtypeBuiltins) },
205 };
206 
207 #undef DIM
208 
209 static const BodyPartFormatter * createForText( const char * subtype ) {
210  if ( subtype && *subtype )
211  switch ( subtype[0] ) {
212  case 'h':
213  case 'H':
214  if ( kasciistricmp( subtype, "html" ) == 0 )
215  return TextHtmlBodyPartFormatter::create();
216  break;
217  case 'r':
218  case 'R':
219  if ( kasciistricmp( subtype, "rtf" ) == 0 )
220  return AnyTypeBodyPartFormatter::create();
221  break;
222  case 'x':
223  case 'X':
224  case 'v':
225  case 'V':
226  if ( kasciistricmp( subtype, "x-vcard" ) == 0 ||
227  kasciistricmp( subtype, "vcard" ) == 0 )
228  return AnyTypeBodyPartFormatter::create();
229  break;
230  }
231 
232  return TextPlainBodyPartFormatter::create();
233 }
234 
235 static const BodyPartFormatter * createForImage( const char * ) {
236  return ImageTypeBodyPartFormatter::create();
237 }
238 
239 static const BodyPartFormatter * createForMessage( const char * subtype ) {
240  if ( kasciistricmp( subtype, "rfc822" ) == 0 )
241  return MessageRfc822BodyPartFormatter::create();
242  return AnyTypeBodyPartFormatter::create();
243 }
244 
245 static const BodyPartFormatter * createForMultiPart( const char * subtype ) {
246  if ( subtype && *subtype )
247  switch ( subtype[0] ) {
248  case 'a':
249  case 'A':
250  if ( kasciistricmp( subtype, "alternative" ) == 0 )
251  return MultiPartAlternativeBodyPartFormatter::create();
252  break;
253  case 'e':
254  case 'E':
255  if ( kasciistricmp( subtype, "encrypted" ) == 0 )
256  return MultiPartEncryptedBodyPartFormatter::create();
257  break;
258  case 's':
259  case 'S':
260  if ( kasciistricmp( subtype, "signed" ) == 0 )
261  return MultiPartSignedBodyPartFormatter::create();
262  break;
263  }
264 
265  return MultiPartMixedBodyPartFormatter::create();
266 }
267 
268 static const BodyPartFormatter * createForApplication( const char * subtype ) {
269  if ( subtype && *subtype )
270  switch ( subtype[0] ) {
271  case 'p':
272  case 'P':
273  if ( kasciistricmp( subtype, "pgp" ) == 0 )
274  return ApplicationPgpBodyPartFormatter::create();
275  // fall through
276  case 'x':
277  case 'X':
278  if ( kasciistricmp( subtype, "pkcs7-mime" ) == 0 ||
279  kasciistricmp( subtype, "x-pkcs7-mime" ) == 0 )
280  return ApplicationPkcs7MimeBodyPartFormatter::create();
281  break;
282  case 'v':
283  case 'V':
284  if ( kasciistricmp( subtype, "vnd.de.bund.bsi.chiasmus-text") == 0)
285  return ApplicationChiasmusTextBodyPartFormatter::create();
286  break;
287  }
288 
289  return AnyTypeBodyPartFormatter::create();
290 }
291 
292 // OK, replace this with a factory with plugin support later on...
293 const BodyPartFormatter * BodyPartFormatter::createFor( const char * type, const char * subtype ) {
294  if ( type && *type )
295  switch ( type[0] ) {
296  case 'a': // application
297  case 'A':
298  if ( kasciistricmp( type, "application" ) == 0 )
299  return createForApplication( subtype );
300  break;
301  case 'i': // image
302  case 'I':
303  if ( kasciistricmp( type, "image" ) == 0 )
304  return createForImage( subtype );
305  break;
306  case 'm': // multipart / message
307  case 'M':
308  if ( kasciistricmp( type, "multipart" ) == 0 )
309  return createForMultiPart( subtype );
310  else if ( kasciistricmp( type, "message" ) == 0 )
311  return createForMessage( subtype );
312  break;
313  case 't': // text
314  case 'T':
315  if ( kasciistricmp( type, "text" ) == 0 )
316  return createForText( subtype );
317  break;
318  }
319 
320  return AnyTypeBodyPartFormatter::create();
321 }
322 
CREATE_BODY_PART_FORMATTER
#define CREATE_BODY_PART_FORMATTER(subtype)
Definition: bodypartformatter.cpp:92
createForImage
static const BodyPartFormatter * createForImage(const char *)
Definition: bodypartformatter.cpp:235
builtins
static const struct @0 builtins[]
MessageViewer::Interface::BodyPartFormatter
Definition: interfaces/bodypartformatter.h:48
MessageViewer::BodyPartFormatter
Definition: viewer/bodypartformatter.h:47
MessageViewer::Interface::BodyPart
interface of message body parts.
Definition: bodypart.h:83
anySubtypeBuiltins
static const SubtypeBuiltin anySubtypeBuiltins[]
Definition: bodypartformatter.cpp:182
MessageViewer::BodyPartFormatter::createFor
static const BodyPartFormatter * createFor(const char *type, const char *subtype)
Definition: bodypartformatter.cpp:293
num_subtypes
unsigned int num_subtypes
Definition: bodypartformatter.cpp:194
messageSubtypeBuiltins
static const SubtypeBuiltin messageSubtypeBuiltins[]
Definition: bodypartformatter.cpp:174
MessageViewer::ProcessResult::setNeverDisplayInline
void setNeverDisplayInline(bool display)
Definition: objecttreeparser.h:96
MessageViewer::ProcessResult
Definition: objecttreeparser.h:69
BodyPartFormatterCreator
const BodyPartFormatter *(* BodyPartFormatterCreator)()
Definition: bodypartformatter.cpp:139
createForMessage
static const BodyPartFormatter * createForMessage(const char *subtype)
Definition: bodypartformatter.cpp:239
createForMultiPart
static const BodyPartFormatter * createForMultiPart(const char *subtype)
Definition: bodypartformatter.cpp:245
bodypartformatterfactory_p.h
createForText
static const BodyPartFormatter * createForText(const char *subtype)
Definition: bodypartformatter.cpp:209
MessageViewer::ProcessResult::setIsImage
void setIsImage(bool image)
Definition: objecttreeparser.h:101
MessageViewer::BodyPartFormatterFactoryPrivate::TypeRegistry
std::map< const char *, SubtypeRegistry, ltstr > TypeRegistry
Definition: bodypartformatterfactory_p.h:57
DIM
#define DIM(x)
Definition: bodypartformatter.cpp:189
multipartSubtypeBuiltins
static const SubtypeBuiltin multipartSubtypeBuiltins[]
Definition: bodypartformatter.cpp:163
applicationSubtypeBuiltins
static const SubtypeBuiltin applicationSubtypeBuiltins[]
Definition: bodypartformatter.cpp:146
type
const char * type
Definition: bodypartformatter.cpp:192
subtypes
const SubtypeBuiltin * subtypes
Definition: bodypartformatter.cpp:193
MessageViewer::BodyPartFormatterFactoryPrivate::messageviewer_create_builtin_bodypart_formatters
void messageviewer_create_builtin_bodypart_formatters(TypeRegistry *)
Definition: bodypartformatter.cpp:134
createForApplication
static const BodyPartFormatter * createForApplication(const char *subtype)
Definition: bodypartformatter.cpp:268
bodypartformatter.h
MessageViewer::ObjectTreeParser
Parses messages and generates HTML display code out of them.
Definition: objecttreeparser.h:287
objecttreeparser.h
MessageViewer::Interface::BodyPartFormatter::format
virtual Result format(BodyPart *part, HtmlWriter *writer) const =0
Format body part part by generating some HTML and writing that to writer.
MessageViewer::HtmlWriter
An interface to HTML sinks.
Definition: htmlwriter.h:98
imageSubtypeBuiltins
static const SubtypeBuiltin imageSubtypeBuiltins[]
Definition: bodypartformatter.cpp:178
textSubtypeBuiltins
static const SubtypeBuiltin textSubtypeBuiltins[]
Definition: bodypartformatter.cpp:154
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