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
00111
00112 CREATE_BODY_PART_FORMATTER(ApplicationOctetStream)
00113 CREATE_BODY_PART_FORMATTER(ApplicationPkcs7Mime)
00114 #ifdef KLEO_CHIASMUS
00115 CREATE_BODY_PART_FORMATTER(ApplicationChiasmusText)
00116 #endif
00117
00118
00119 CREATE_BODY_PART_FORMATTER(MessageRfc822)
00120
00121 CREATE_BODY_PART_FORMATTER(MultiPartMixed)
00122 CREATE_BODY_PART_FORMATTER(MultiPartAlternative)
00123 CREATE_BODY_PART_FORMATTER(MultiPartSigned)
00124 CREATE_BODY_PART_FORMATTER(MultiPartEncrypted)
00125
00126 typedef TextPlainBodyPartFormatter ApplicationPgpBodyPartFormatter;
00127
00128
00129 #undef CREATE_BODY_PART_FORMATTER
00130 }
00131
00132
00133 void KMail::BodyPartFormatterFactoryPrivate::kmail_create_builtin_bodypart_formatters( KMail::BodyPartFormatterFactoryPrivate::TypeRegistry * reg ) {
00134 if ( !reg ) return;
00135 (*reg)["application"]["octet-stream"] = new AnyTypeBodyPartFormatter();
00136 }
00137
00138 typedef const KMail::BodyPartFormatter * (*BodyPartFormatterCreator)();
00139
00140 struct SubtypeBuiltin {
00141 const char * subtype;
00142 BodyPartFormatterCreator create;
00143 };
00144
00145 static const SubtypeBuiltin applicationSubtypeBuiltins[] = {
00146 { "octet-stream", &ApplicationOctetStreamBodyPartFormatter::create },
00147 { "pkcs7-mime", &ApplicationPkcs7MimeBodyPartFormatter::create },
00148 { "x-pkcs7-mime", &ApplicationPkcs7MimeBodyPartFormatter::create },
00149 #ifdef KLEO_CHIASMUS
00150 { "vnd.de.bund.bsi.chiasmus-text", &ApplicationChiasmusTextBodyPartFormatter::create },
00151 #endif
00152 { "pgp", &ApplicationPgpBodyPartFormatter::create }
00153 };
00154
00155 static const SubtypeBuiltin textSubtypeBuiltins[] = {
00156 { "html", &TextHtmlBodyPartFormatter::create },
00157
00158 { "x-vcard", &AnyTypeBodyPartFormatter::create },
00159 { "vcard", &AnyTypeBodyPartFormatter::create },
00160 { "rtf", &AnyTypeBodyPartFormatter::create },
00161 { "*", &TextPlainBodyPartFormatter::create },
00162 };
00163
00164 static const SubtypeBuiltin multipartSubtypeBuiltins[] = {
00165 { "mixed", &MultiPartMixedBodyPartFormatter::create },
00166 { "alternative", &MultiPartAlternativeBodyPartFormatter::create },
00167
00168
00169
00170 { "signed", &MultiPartSignedBodyPartFormatter::create },
00171 { "encrypted", &MultiPartEncryptedBodyPartFormatter::create },
00172
00173 };
00174
00175 static const SubtypeBuiltin messageSubtypeBuiltins[] = {
00176 { "rfc822", &MessageRfc822BodyPartFormatter::create },
00177 };
00178
00179 static const SubtypeBuiltin imageSubtypeBuiltins[] = {
00180 { "*", &ImageTypeBodyPartFormatter::create },
00181 };
00182
00183 static const SubtypeBuiltin anySubtypeBuiltins[] = {
00184 { "*", &AnyTypeBodyPartFormatter::create },
00185 };
00186
00187 #ifdef DIM
00188 #undef DIM
00189 #endif
00190 #define DIM(x) sizeof(x) / sizeof(*x)
00191
00192 static const struct {
00193 const char * type;
00194 const SubtypeBuiltin * subtypes;
00195 unsigned int num_subtypes;
00196 } builtins[] = {
00197 { "application", applicationSubtypeBuiltins, DIM(applicationSubtypeBuiltins) },
00198 { "text", textSubtypeBuiltins, DIM(textSubtypeBuiltins) },
00199 { "multipart", multipartSubtypeBuiltins, DIM(multipartSubtypeBuiltins) },
00200 { "message", messageSubtypeBuiltins, DIM(messageSubtypeBuiltins) },
00201 { "image", imageSubtypeBuiltins, DIM(imageSubtypeBuiltins) },
00202
00203
00204
00205 { "*", anySubtypeBuiltins, DIM(anySubtypeBuiltins) },
00206 };
00207
00208 #undef DIM
00209
00210 const KMail::BodyPartFormatter * KMail::BodyPartFormatter::createFor( int type, int subtype ) {
00211 DwString t, st;
00212 DwTypeEnumToStr( type, t );
00213 DwSubtypeEnumToStr( subtype, st );
00214 return createFor( t.c_str(), st.c_str() );
00215 }
00216
00217 static const KMail::BodyPartFormatter * createForText( const char * subtype ) {
00218 if ( subtype && *subtype )
00219 switch ( subtype[0] ) {
00220 case 'h':
00221 case 'H':
00222 if ( qstricmp( subtype, "html" ) == 0 )
00223 return TextHtmlBodyPartFormatter::create();
00224 break;
00225 case 'r':
00226 case 'R':
00227 if ( qstricmp( subtype, "rtf" ) == 0 )
00228 return AnyTypeBodyPartFormatter::create();
00229 break;
00230 case 'x':
00231 case 'X':
00232 case 'v':
00233 case 'V':
00234 if ( qstricmp( subtype, "x-vcard" ) == 0 ||
00235 qstricmp( subtype, "vcard" ) == 0 )
00236 return AnyTypeBodyPartFormatter::create();
00237 break;
00238 }
00239
00240 return TextPlainBodyPartFormatter::create();
00241 }
00242
00243 static const KMail::BodyPartFormatter * createForImage( const char * ) {
00244 return ImageTypeBodyPartFormatter::create();
00245 }
00246
00247 static const KMail::BodyPartFormatter * createForMessage( const char * subtype ) {
00248 if ( qstricmp( subtype, "rfc822" ) == 0 )
00249 return MessageRfc822BodyPartFormatter::create();
00250 return AnyTypeBodyPartFormatter::create();
00251 }
00252
00253 static const KMail::BodyPartFormatter * createForMultiPart( const char * subtype ) {
00254 if ( subtype && *subtype )
00255 switch ( subtype[0] ) {
00256 case 'a':
00257 case 'A':
00258 if ( qstricmp( subtype, "alternative" ) == 0 )
00259 return MultiPartAlternativeBodyPartFormatter::create();
00260 break;
00261 case 'e':
00262 case 'E':
00263 if ( qstricmp( subtype, "encrypted" ) == 0 )
00264 return MultiPartEncryptedBodyPartFormatter::create();
00265 break;
00266 case 's':
00267 case 'S':
00268 if ( qstricmp( subtype, "signed" ) == 0 )
00269 return MultiPartSignedBodyPartFormatter::create();
00270 break;
00271 }
00272
00273 return MultiPartMixedBodyPartFormatter::create();
00274 }
00275
00276 static const KMail::BodyPartFormatter * createForApplication( const char * subtype ) {
00277 if ( subtype && *subtype )
00278 switch ( subtype[0] ) {
00279 case 'p':
00280 case 'P':
00281 if ( qstricmp( subtype, "pgp" ) == 0 )
00282 return ApplicationPgpBodyPartFormatter::create();
00283
00284 case 'x':
00285 case 'X':
00286 if ( qstricmp( subtype, "pkcs7-mime" ) == 0 ||
00287 qstricmp( subtype, "x-pkcs7-mime" ) == 0 )
00288 return ApplicationPkcs7MimeBodyPartFormatter::create();
00289 break;
00290 case 'm':
00291 case 'M':
00292
00293
00294 break;
00295 #ifdef KLEO_CHIASMUS
00296 case 'v':
00297 case 'V':
00298 if ( qstricmp( subtype, "vnd.de.bund.bsi.chiasmus-text") == 0)
00299 return ApplicationChiasmusTextBodyPartFormatter::create();
00300 break;
00301 #endif
00302 }
00303
00304 return AnyTypeBodyPartFormatter::create();
00305 }
00306
00307
00308 const KMail::BodyPartFormatter * KMail::BodyPartFormatter::createFor( const char * type, const char * subtype ) {
00309 if ( type && *type )
00310 switch ( type[0] ) {
00311 case 'a':
00312 case 'A':
00313 if ( qstricmp( type, "application" ) == 0 )
00314 return createForApplication( subtype );
00315 break;
00316 case 'i':
00317 case 'I':
00318 if ( qstricmp( type, "image" ) == 0 )
00319 return createForImage( subtype );
00320 break;
00321 case 'm':
00322 case 'M':
00323 if ( qstricmp( type, "multipart" ) == 0 )
00324 return createForMultiPart( subtype );
00325 else if ( qstricmp( type, "message" ) == 0 )
00326 return createForMessage( subtype );
00327 break;
00328 case 't':
00329 case 'T':
00330 if ( qstricmp( type, "text" ) == 0 )
00331 return createForText( subtype );
00332 break;
00333 }
00334
00335 return AnyTypeBodyPartFormatter::create();
00336 }
00337