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   //CREATE_BODY_PART_FORMATTER(ApplicationPgp)
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 } // anon namespace
00128 
00129 // FIXME: port some more KMail::BodyPartFormatters to KMail::Interface::BodyPartFormatters
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   //{ "enriched", &TextEnrichedBodyPartFormatter::create },
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   //{ "digest", &MultiPartDigestFormatter::create },
00162   //{ "parallel", &MultiPartParallelFormatter::create },
00163   //{ "related", &MultiPartRelatedFormatter::create },
00164   { "signed", &MultiPartSignedBodyPartFormatter::create },
00165   { "encrypted", &MultiPartEncryptedBodyPartFormatter::create },
00166   //{ "report", &MultiPartReportFormatter::create },
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   //{ "audio", audioSubtypeBuiltins, DIM(audioSubtypeBuiltins) },
00197   //{ "model", modelSubtypeBuiltins, DIM(modelSubtypeBuiltins) },
00198   //{ "video", videoSubtypeBuiltins, DIM(videoSubtypeBuiltins) },
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       // fall through
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       //if ( qstricmp( subtype, "ms-tnef" ) == 0 )
00287       //  return ApplicationMsTnefBodyPartFormatter::create();
00288       break;
00289     }
00290 
00291   return AnyTypeBodyPartFormatter::create();
00292 }
00293 
00294 // OK, replace this with a factory with plugin support later on...
00295 const KMail::BodyPartFormatter * KMail::BodyPartFormatter::createFor( const char * type, const char * subtype ) {
00296   if ( type && *type )
00297     switch ( type[0] ) {
00298     case 'a': // application
00299     case 'A':
00300       if ( qstricmp( type, "application" ) == 0 )
00301     return createForApplication( subtype );
00302       break;
00303     case 'i': // image
00304     case 'I':
00305       if ( qstricmp( type, "image" ) == 0 )
00306     return createForImage( subtype );
00307       break;
00308     case 'm': // multipart / message
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': // text
00316     case 'T':
00317       if ( qstricmp( type, "text" ) == 0 )
00318     return createForText( subtype );
00319       break;
00320     }
00321 
00322   return AnyTypeBodyPartFormatter::create();
00323 }
00324 
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 Mon Apr 4 04:48:21 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003