kmail
attachmentstrategy.cpp
Go to the documentation of this file.00001 /* -*- c++ -*- 00002 attachmentstrategy.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 "attachmentstrategy.h" 00037 00038 #include "partNode.h" 00039 #include "kmmsgpart.h" 00040 00041 #include <qstring.h> 00042 00043 #include <kdebug.h> 00044 00045 00046 namespace KMail { 00047 00048 00049 // 00050 // IconicAttachmentStrategy: 00051 // show everything but the first text/plain body as icons 00052 // 00053 00054 class IconicAttachmentStrategy : public AttachmentStrategy { 00055 friend class ::KMail::AttachmentStrategy; 00056 protected: 00057 IconicAttachmentStrategy() : AttachmentStrategy() {} 00058 virtual ~IconicAttachmentStrategy() {} 00059 00060 public: 00061 const char * name() const { return "iconic"; } 00062 const AttachmentStrategy * next() const { return smart(); } 00063 const AttachmentStrategy * prev() const { return hidden(); } 00064 00065 bool inlineNestedMessages() const { return false; } 00066 Display defaultDisplay( const partNode * ) const { return AsIcon; } 00067 }; 00068 00069 // 00070 // SmartAttachmentStrategy: 00071 // in addition to Iconic, show all body parts 00072 // with content-disposition == "inline" and 00073 // all text parts without a filename or name parameter inline 00074 // 00075 00076 class SmartAttachmentStrategy : public AttachmentStrategy { 00077 friend class ::KMail::AttachmentStrategy; 00078 protected: 00079 SmartAttachmentStrategy() : AttachmentStrategy() {} 00080 virtual ~SmartAttachmentStrategy() {} 00081 00082 public: 00083 const char * name() const { return "smart"; } 00084 const AttachmentStrategy * next() const { return inlined(); } 00085 const AttachmentStrategy * prev() const { return iconic(); } 00086 00087 bool inlineNestedMessages() const { return true; } 00088 Display defaultDisplay( const partNode * node ) const { 00089 if ( node->hasContentDispositionInline() ) 00090 // explict "inline" disposition: 00091 return Inline; 00092 if ( node->isAttachment() ) 00093 // explicit "attachment" disposition: 00094 return AsIcon; 00095 if ( node->type() == DwMime::kTypeText && 00096 node->msgPart().fileName().stripWhiteSpace().isEmpty() && 00097 node->msgPart().name().stripWhiteSpace().isEmpty() ) 00098 // text/* w/o filename parameter: 00099 return Inline; 00100 return AsIcon; 00101 } 00102 }; 00103 00104 // 00105 // InlinedAttachmentStrategy: 00106 // show everything possible inline 00107 // 00108 00109 class InlinedAttachmentStrategy : public AttachmentStrategy { 00110 friend class ::KMail::AttachmentStrategy; 00111 protected: 00112 InlinedAttachmentStrategy() : AttachmentStrategy() {} 00113 virtual ~InlinedAttachmentStrategy() {} 00114 00115 public: 00116 const char * name() const { return "inlined"; } 00117 const AttachmentStrategy * next() const { return hidden(); } 00118 const AttachmentStrategy * prev() const { return smart(); } 00119 00120 bool inlineNestedMessages() const { return true; } 00121 Display defaultDisplay( const partNode * ) const { return Inline; } 00122 }; 00123 00124 // 00125 // HiddenAttachmentStrategy 00126 // show nothing except the first text/plain body part _at all_ 00127 // 00128 00129 class HiddenAttachmentStrategy : public AttachmentStrategy { 00130 friend class ::KMail::AttachmentStrategy; 00131 protected: 00132 HiddenAttachmentStrategy() : AttachmentStrategy() {} 00133 virtual ~HiddenAttachmentStrategy() {} 00134 00135 public: 00136 const char * name() const { return "hidden"; } 00137 const AttachmentStrategy * next() const { return iconic(); } 00138 const AttachmentStrategy * prev() const { return inlined(); } 00139 00140 bool inlineNestedMessages() const { return false; } 00141 Display defaultDisplay( const partNode * ) const { return None; } 00142 }; 00143 00144 00145 // 00146 // AttachmentStrategy abstract base: 00147 // 00148 00149 AttachmentStrategy::AttachmentStrategy() { 00150 00151 } 00152 00153 AttachmentStrategy::~AttachmentStrategy() { 00154 00155 } 00156 00157 const AttachmentStrategy * AttachmentStrategy::create( Type type ) { 00158 switch ( type ) { 00159 case Iconic: return iconic(); 00160 case Smart: return smart(); 00161 case Inlined: return inlined(); 00162 case Hidden: return hidden(); 00163 } 00164 kdFatal( 5006 ) << "AttachmentStrategy::create(): Unknown attachment startegy ( type == " 00165 << (int)type << " ) requested!" << endl; 00166 return 0; // make compiler happy 00167 } 00168 00169 const AttachmentStrategy * AttachmentStrategy::create( const QString & type ) { 00170 QString lowerType = type.lower(); 00171 if ( lowerType == "iconic" ) return iconic(); 00172 //if ( lowerType == "smart" ) return smart(); // not needed, see below 00173 if ( lowerType == "inlined" ) return inlined(); 00174 if ( lowerType == "hidden" ) return hidden(); 00175 // don't kdFatal here, b/c the strings are user-provided 00176 // (KConfig), so fail gracefully to the default: 00177 return smart(); 00178 } 00179 00180 static const AttachmentStrategy * iconicStrategy = 0; 00181 static const AttachmentStrategy * smartStrategy = 0; 00182 static const AttachmentStrategy * inlinedStrategy = 0; 00183 static const AttachmentStrategy * hiddenStrategy = 0; 00184 00185 const AttachmentStrategy * AttachmentStrategy::iconic() { 00186 if ( !iconicStrategy ) 00187 iconicStrategy = new IconicAttachmentStrategy(); 00188 return iconicStrategy; 00189 } 00190 00191 const AttachmentStrategy * AttachmentStrategy::smart() { 00192 if ( !smartStrategy ) 00193 smartStrategy = new SmartAttachmentStrategy(); 00194 return smartStrategy; 00195 } 00196 00197 const AttachmentStrategy * AttachmentStrategy::inlined() { 00198 if ( !inlinedStrategy ) 00199 inlinedStrategy = new InlinedAttachmentStrategy(); 00200 return inlinedStrategy; 00201 } 00202 00203 const AttachmentStrategy * AttachmentStrategy::hidden() { 00204 if ( !hiddenStrategy ) 00205 hiddenStrategy = new HiddenAttachmentStrategy(); 00206 return hiddenStrategy; 00207 } 00208 00209 } // namespace KMail