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(TextVCal) 00111 //CREATE_BODY_PART_FORMATTER(TextEnriched) 00112 00113 CREATE_BODY_PART_FORMATTER(ApplicationOctetStream) 00114 CREATE_BODY_PART_FORMATTER(ApplicationPkcs7Mime) 00115 //CREATE_BODY_PART_FORMATTER(ApplicationMsTnef) 00116 //CREATE_BODY_PART_FORMATTER(ApplicationPgp) 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 } // anon namespace 00130 00131 // FIXME: port some more KMail::BodyPartFormatters to KMail::Interface::BodyPartFormatters 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 //{ "ms-tnef", &ApplicationMsTnefBodyPartFormatter::create }, 00149 { "pgp", &ApplicationPgpBodyPartFormatter::create }, 00150 }; 00151 00152 static const SubtypeBuiltin textSubtypeBuiltins[] = { 00153 { "html", &TextHtmlBodyPartFormatter::create }, 00154 //{ "enriched", &TextEnrichedBodyPartFormatter::create }, 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 //{ "digest", &MultiPartDigestFormatter::create }, 00166 //{ "parallel", &MultiPartParallelFormatter::create }, 00167 //{ "related", &MultiPartRelatedFormatter::create }, 00168 { "signed", &MultiPartSignedBodyPartFormatter::create }, 00169 { "encrypted", &MultiPartEncryptedBodyPartFormatter::create }, 00170 //{ "report", &MultiPartReportFormatter::create }, 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 //{ "audio", audioSubtypeBuiltins, DIM(audioSubtypeBuiltins) }, 00201 //{ "model", modelSubtypeBuiltins, DIM(modelSubtypeBuiltins) }, 00202 //{ "video", videoSubtypeBuiltins, DIM(videoSubtypeBuiltins) }, 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 // fall through 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 //if ( qstricmp( subtype, "ms-tnef" ) == 0 ) 00296 // return ApplicationMsTnefBodyPartFormatter::create(); 00297 break; 00298 } 00299 00300 return AnyTypeBodyPartFormatter::create(); 00301 } 00302 00303 // OK, replace this with a factory with plugin support later on... 00304 const KMail::BodyPartFormatter * KMail::BodyPartFormatter::createFor( const char * type, const char * subtype ) { 00305 if ( type && *type ) 00306 switch ( type[0] ) { 00307 case 'a': // application 00308 case 'A': 00309 if ( qstricmp( type, "application" ) == 0 ) 00310 return createForApplication( subtype ); 00311 break; 00312 case 'i': // image 00313 case 'I': 00314 if ( qstricmp( type, "image" ) == 0 ) 00315 return createForImage( subtype ); 00316 break; 00317 case 'm': // multipart / message 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': // text 00325 case 'T': 00326 if ( qstricmp( type, "text" ) == 0 ) 00327 return createForText( subtype ); 00328 break; 00329 } 00330 00331 return AnyTypeBodyPartFormatter::create(); 00332 } 00333
KDE Logo
This file is part of the documentation for kmail Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Oct 21 19:46:43 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003