00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
#ifdef HAVE_CONFIG_H
00033
#include <config.h>
00034
#endif
00035
00036
#include "bodypartformatter.h"
00037
#include "bodypartformatterfactory_p.h"
00038
#include "interfaces/bodypartformatter.h"
00039
00040
#include "objecttreeparser.h"
00041
#include "partNode.h"
00042
00043
#include <mimelib/enum.h>
00044
#include <mimelib/string.h>
00045
#include <mimelib/utility.h>
00046
00047
#include <kdebug.h>
00048
00049
namespace {
00050
class AnyTypeBodyPartFormatter
00051 :
public KMail::BodyPartFormatter,
00052
public KMail::Interface::BodyPartFormatter {
00053
static const AnyTypeBodyPartFormatter *
self;
00054
public:
00055 Result format(
KMail::Interface::BodyPart *,
KMail::HtmlWriter * )
const {
00056 kdDebug(5006) <<
"AnyTypeBodyPartFormatter::format() acting as a KMail::Interface::BodyPartFormatter!" << endl;
00057
return AsIcon;
00058 }
00059
00060
bool process( KMail::ObjectTreeParser *, partNode *, KMail::ProcessResult & result )
const {
00061 result.setNeverDisplayInline(
true );
00062
return false;
00063 }
00064
static const KMail::BodyPartFormatter * create() {
00065
if ( !
self )
00066
self =
new AnyTypeBodyPartFormatter();
00067
return self;
00068 }
00069 };
00070
00071
const AnyTypeBodyPartFormatter * AnyTypeBodyPartFormatter::self = 0;
00072
00073
00074
class ImageTypeBodyPartFormatter :
public KMail::BodyPartFormatter {
00075
static const ImageTypeBodyPartFormatter *
self;
00076
public:
00077
bool process( KMail::ObjectTreeParser *, partNode *, KMail::ProcessResult & result )
const {
00078 result.setIsImage(
true );
00079
return false;
00080 }
00081
static const KMail::BodyPartFormatter * create() {
00082
if ( !
self )
00083
self =
new ImageTypeBodyPartFormatter();
00084
return self;
00085 }
00086 };
00087
00088
const ImageTypeBodyPartFormatter * ImageTypeBodyPartFormatter::self = 0;
00089
00090
#define CREATE_BODY_PART_FORMATTER(subtype) \
00091
class subtype##BodyPartFormatter : public KMail::BodyPartFormatter { \
00092
static const subtype##BodyPartFormatter * self; \
00093
public: \
00094
bool process( KMail::ObjectTreeParser *, partNode *, KMail::ProcessResult & ) const; \
00095
static const KMail::BodyPartFormatter * create() { \
00096
if ( !self ) \
00097
self = new subtype##BodyPartFormatter(); \
00098
return self; \
00099
} \
00100
}; \
00101
\
00102
const subtype##BodyPartFormatter * subtype##BodyPartFormatter::self; \
00103
\
00104
bool subtype##BodyPartFormatter::process( KMail::ObjectTreeParser * otp, partNode * node, KMail::ProcessResult & result ) const { \
00105
return otp->process##subtype##Subtype( node, result ); \
00106
}
00107
00108 CREATE_BODY_PART_FORMATTER(TextPlain)
00109 CREATE_BODY_PART_FORMATTER(TextHtml)
00110 CREATE_BODY_PART_FORMATTER(TextVCal)
00111
00112
00113 CREATE_BODY_PART_FORMATTER(ApplicationOctetStream)
00114 CREATE_BODY_PART_FORMATTER(ApplicationPkcs7Mime)
00115
00116
00117
00118 CREATE_BODY_PART_FORMATTER(MessageRfc822)
00119
00120 CREATE_BODY_PART_FORMATTER(MultiPartMixed)
00121 CREATE_BODY_PART_FORMATTER(MultiPartAlternative)
00122 CREATE_BODY_PART_FORMATTER(MultiPartSigned)
00123 CREATE_BODY_PART_FORMATTER(MultiPartEncrypted)
00124
00125 typedef TextPlainBodyPartFormatter ApplicationPgpBodyPartFormatter;
00126
00127
00128 #undef CREATE_BODY_PART_FORMATTER
00129 }
00130
00131
00132
void KMail::BodyPartFormatterFactoryPrivate::kmail_create_builtin_bodypart_formatters( KMail::BodyPartFormatterFactoryPrivate::TypeRegistry * reg ) {
00133
if ( !reg )
return;
00134 (*reg)[
"application"][
"octet-stream"] =
new AnyTypeBodyPartFormatter();
00135 }
00136
00137
typedef const KMail::BodyPartFormatter * (*BodyPartFormatterCreator)();
00138
00139
struct SubtypeBuiltin {
00140
const char * subtype;
00141 BodyPartFormatterCreator create;
00142 };
00143
00144
static const SubtypeBuiltin applicationSubtypeBuiltins[] = {
00145 {
"octet-stream", &ApplicationOctetStreamBodyPartFormatter::create },
00146 {
"pkcs7-mime", &ApplicationPkcs7MimeBodyPartFormatter::create },
00147 {
"x-pkcs7-mime", &ApplicationPkcs7MimeBodyPartFormatter::create },
00148
00149 {
"pgp", &ApplicationPgpBodyPartFormatter::create },
00150 };
00151
00152
static const SubtypeBuiltin textSubtypeBuiltins[] = {
00153 {
"html", &TextHtmlBodyPartFormatter::create },
00154
00155 {
"calendar", &TextVCalBodyPartFormatter::create },
00156 {
"x-vcard", &AnyTypeBodyPartFormatter::create },
00157 {
"vcard", &AnyTypeBodyPartFormatter::create },
00158 {
"rtf", &AnyTypeBodyPartFormatter::create },
00159 {
"*", &TextPlainBodyPartFormatter::create },
00160 };
00161
00162
static const SubtypeBuiltin multipartSubtypeBuiltins[] = {
00163 {
"mixed", &MultiPartMixedBodyPartFormatter::create },
00164 {
"alternative", &MultiPartAlternativeBodyPartFormatter::create },
00165
00166
00167
00168 {
"signed", &MultiPartSignedBodyPartFormatter::create },
00169 {
"encrypted", &MultiPartEncryptedBodyPartFormatter::create },
00170
00171 };
00172
00173
static const SubtypeBuiltin messageSubtypeBuiltins[] = {
00174 {
"rfc822", &MessageRfc822BodyPartFormatter::create },
00175 };
00176
00177
static const SubtypeBuiltin imageSubtypeBuiltins[] = {
00178 {
"*", &ImageTypeBodyPartFormatter::create },
00179 };
00180
00181
static const SubtypeBuiltin anySubtypeBuiltins[] = {
00182 {
"*", &AnyTypeBodyPartFormatter::create },
00183 };
00184
00185
#ifdef DIM
00186
#undef DIM
00187
#endif
00188
#define DIM(x) sizeof(x) / sizeof(*x)
00189
00190
static const struct {
00191
const char * type;
00192
const SubtypeBuiltin * subtypes;
00193
unsigned int num_subtypes;
00194 } builtins[] = {
00195 {
"application", applicationSubtypeBuiltins, DIM(applicationSubtypeBuiltins) },
00196 {
"text", textSubtypeBuiltins, DIM(textSubtypeBuiltins) },
00197 {
"multipart", multipartSubtypeBuiltins, DIM(multipartSubtypeBuiltins) },
00198 {
"message", messageSubtypeBuiltins, DIM(messageSubtypeBuiltins) },
00199 {
"image", imageSubtypeBuiltins, DIM(imageSubtypeBuiltins) },
00200
00201
00202
00203 {
"*", anySubtypeBuiltins, DIM(anySubtypeBuiltins) },
00204 };
00205
00206
#undef DIM
00207
00208
const KMail::BodyPartFormatter * KMail::BodyPartFormatter::createFor(
int type,
int subtype ) {
00209 DwString t, st;
00210 DwTypeEnumToStr( type, t );
00211 DwSubtypeEnumToStr( subtype, st );
00212
return createFor( t.c_str(), st.c_str() );
00213 }
00214
00215
static const KMail::BodyPartFormatter * createForText(
const char * subtype ) {
00216
if ( subtype && *subtype )
00217
switch ( subtype[0] ) {
00218
case 'h':
00219
case 'H':
00220
if ( qstricmp( subtype,
"html" ) == 0 )
00221
return TextHtmlBodyPartFormatter::create();
00222
break;
00223
case 'c':
00224
case 'C':
00225
if ( qstricmp( subtype,
"calendar" ) == 0 )
00226
return TextVCalBodyPartFormatter::create();
00227
break;
00228
case 'r':
00229
case 'R':
00230
if ( qstricmp( subtype,
"rtf" ) == 0 )
00231
return AnyTypeBodyPartFormatter::create();
00232
break;
00233
case 'x':
00234
case 'X':
00235
case 'v':
00236
case 'V':
00237
if ( qstricmp( subtype,
"x-vcard" ) == 0 ||
00238 qstricmp( subtype,
"vcard" ) == 0 )
00239
return AnyTypeBodyPartFormatter::create();
00240
break;
00241 }
00242
00243
return TextPlainBodyPartFormatter::create();
00244 }
00245
00246
static const KMail::BodyPartFormatter * createForImage(
const char * ) {
00247
return ImageTypeBodyPartFormatter::create();
00248 }
00249
00250
static const KMail::BodyPartFormatter * createForMessage(
const char * subtype ) {
00251
if ( qstricmp( subtype,
"rfc822" ) == 0 )
00252
return MessageRfc822BodyPartFormatter::create();
00253
return AnyTypeBodyPartFormatter::create();
00254 }
00255
00256
static const KMail::BodyPartFormatter * createForMultiPart(
const char * subtype ) {
00257
if ( subtype && *subtype )
00258
switch ( subtype[0] ) {
00259
case 'a':
00260
case 'A':
00261
if ( qstricmp( subtype,
"alternative" ) == 0 )
00262
return MultiPartAlternativeBodyPartFormatter::create();
00263
break;
00264
case 'e':
00265
case 'E':
00266
if ( qstricmp( subtype,
"encrypted" ) == 0 )
00267
return MultiPartEncryptedBodyPartFormatter::create();
00268
break;
00269
case 's':
00270
case 'S':
00271
if ( qstricmp( subtype,
"signed" ) == 0 )
00272
return MultiPartSignedBodyPartFormatter::create();
00273
break;
00274 }
00275
00276
return MultiPartMixedBodyPartFormatter::create();
00277 }
00278
00279
static const KMail::BodyPartFormatter * createForApplication(
const char * subtype ) {
00280
if ( subtype && *subtype )
00281
switch ( subtype[0] ) {
00282
case 'p':
00283
case 'P':
00284
if ( qstricmp( subtype,
"pgp" ) == 0 )
00285
return ApplicationPgpBodyPartFormatter::create();
00286
00287
case 'x':
00288
case 'X':
00289
if ( qstricmp( subtype,
"pkcs7-mime" ) == 0 ||
00290 qstricmp( subtype,
"x-pkcs7-mime" ) == 0 )
00291
return ApplicationPkcs7MimeBodyPartFormatter::create();
00292
break;
00293
case 'm':
00294
case 'M':
00295
00296
00297
break;
00298 }
00299
00300
return AnyTypeBodyPartFormatter::create();
00301 }
00302
00303
00304
const KMail::BodyPartFormatter * KMail::BodyPartFormatter::createFor(
const char * type,
const char * subtype ) {
00305
if ( type && *type )
00306
switch ( type[0] ) {
00307
case 'a':
00308
case 'A':
00309
if ( qstricmp( type,
"application" ) == 0 )
00310
return createForApplication( subtype );
00311
break;
00312
case 'i':
00313
case 'I':
00314
if ( qstricmp( type,
"image" ) == 0 )
00315
return createForImage( subtype );
00316
break;
00317
case 'm':
00318
case 'M':
00319
if ( qstricmp( type,
"multipart" ) == 0 )
00320
return createForMultiPart( subtype );
00321
else if ( qstricmp( type,
"message" ) == 0 )
00322
return createForMessage( subtype );
00323
break;
00324
case 't':
00325
case 'T':
00326
if ( qstricmp( type,
"text" ) == 0 )
00327
return createForText( subtype );
00328
break;
00329 }
00330
00331
return AnyTypeBodyPartFormatter::create();
00332 }
00333