kmail Library API Documentation

bodypartformatter.cpp

00001 /*  -*- c++ -*-
00002     bodypartformatter.cpp
00003 
00004     This file is part of KMail, the KDE mail client.
00005     Copyright (c) 2003 Marc Mutz <mutz@kde.org>
00006 
00007     KMail is free software; you can redistribute it and/or modify it
00008     under the terms of the GNU General Public License, version 2, as
00009     published by the Free Software Foundation.
00010 
00011     KMail is distributed in the hope that it will be useful, but
00012     WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020     In addition, as a special exception, the copyright holders give
00021     permission to link the code of this program with any edition of
00022     the Qt library by Trolltech AS, Norway (or with modified versions
00023     of Qt that use the same license as Qt), and distribute linked
00024     combinations including the two.  You must obey the GNU General
00025     Public License in all respects for all of the code used other than
00026     Qt.  If you modify this file, you may extend this exception to
00027     your version of the file, but you are not obligated to do so.  If
00028     you do not wish to do so, delete this exception statement from
00029     your version.
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(TextEnriched)
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   //CREATE_BODY_PART_FORMATTER(ApplicationPgp)
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 } // anon namespace
00131 
00132 // FIXME: port some more KMail::BodyPartFormatters to KMail::Interface::BodyPartFormatters
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   //{ "enriched", &TextEnrichedBodyPartFormatter::create },
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   //{ "digest", &MultiPartDigestFormatter::create },
00168   //{ "parallel", &MultiPartParallelFormatter::create },
00169   //{ "related", &MultiPartRelatedFormatter::create },
00170   { "signed", &MultiPartSignedBodyPartFormatter::create },
00171   { "encrypted", &MultiPartEncryptedBodyPartFormatter::create },
00172   //{ "report", &MultiPartReportFormatter::create },
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   //{ "audio", audioSubtypeBuiltins, DIM(audioSubtypeBuiltins) },
00203   //{ "model", modelSubtypeBuiltins, DIM(modelSubtypeBuiltins) },
00204   //{ "video", videoSubtypeBuiltins, DIM(videoSubtypeBuiltins) },
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       // fall through
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       //if ( qstricmp( subtype, "ms-tnef" ) == 0 )
00293       //  return ApplicationMsTnefBodyPartFormatter::create();
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 // OK, replace this with a factory with plugin support later on...
00308 const KMail::BodyPartFormatter * KMail::BodyPartFormatter::createFor( const char * type, const char * subtype ) {
00309   if ( type && *type )
00310     switch ( type[0] ) {
00311     case 'a': // application
00312     case 'A':
00313       if ( qstricmp( type, "application" ) == 0 )
00314     return createForApplication( subtype );
00315       break;
00316     case 'i': // image
00317     case 'I':
00318       if ( qstricmp( type, "image" ) == 0 )
00319     return createForImage( subtype );
00320       break;
00321     case 'm': // multipart / message
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': // text
00329     case 'T':
00330       if ( qstricmp( type, "text" ) == 0 )
00331     return createForText( subtype );
00332       break;
00333     }
00334 
00335   return AnyTypeBodyPartFormatter::create();
00336 }
00337 
KDE Logo
This file is part of the documentation for kmail Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Aug 23 18:21:04 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003