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