headerstrategy.cpp
00001 /* -*- c++ -*- 00002 headerstrategy.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 TQt library by Trolltech AS, Norway (or with modified versions 00023 of TQt that use the same license as TQt), 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 TQt. 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 "headerstrategy.h" 00037 00038 #include "kmkernel.h" 00039 00040 #include <kdebug.h> 00041 #include <tdeconfig.h> 00042 00043 namespace KMail { 00044 00045 // 00046 // Header tables: 00047 // 00048 00049 static const char * briefHeaders[] = { 00050 "subject", "from", "cc", "bcc", "date" 00051 }; 00052 static const int numBriefHeaders = sizeof briefHeaders / sizeof *briefHeaders; 00053 00054 00055 static const char * standardHeaders[] = { 00056 "subject", "from", "cc", "bcc", "to" 00057 }; 00058 static const int numStandardHeaders = sizeof standardHeaders / sizeof *standardHeaders; 00059 00060 00061 static const char * richHeaders[] = { 00062 "subject", "date", "from", "cc", "bcc", "to", 00063 "organization", "organisation", "reply-to", 00064 "user-agent", "x-mailer" 00065 }; 00066 static const int numRichHeaders = sizeof richHeaders / sizeof *richHeaders; 00067 00068 // 00069 // Convenience function 00070 // 00071 00072 static TQStringList stringList( const char * headers[], int numHeaders ) { 00073 TQStringList sl; 00074 for ( int i = 0 ; i < numHeaders ; ++i ) 00075 sl.push_back( headers[i] ); 00076 return sl; 00077 } 00078 00079 // 00080 // AllHeaderStrategy: 00081 // show everything 00082 // 00083 00084 class AllHeaderStrategy : public HeaderStrategy { 00085 friend class ::KMail::HeaderStrategy; 00086 protected: 00087 AllHeaderStrategy() : HeaderStrategy() {} 00088 virtual ~AllHeaderStrategy() {} 00089 00090 public: 00091 const char * name() const { return "all"; } 00092 const HeaderStrategy * next() const { return rich(); } 00093 const HeaderStrategy * prev() const { return custom(); } 00094 00095 DefaultPolicy defaultPolicy() const { return Display; } 00096 00097 bool showHeader( const TQString & ) const { 00098 return true; // more efficient than default impl 00099 } 00100 }; 00101 00102 // 00103 // RichHeaderStrategy: 00104 // Date, Subject, From, To, CC, ### what exactly? 00105 // 00106 00107 class RichHeaderStrategy : public HeaderStrategy { 00108 friend class ::KMail::HeaderStrategy; 00109 protected: 00110 RichHeaderStrategy() 00111 : HeaderStrategy(), 00112 mHeadersToDisplay( stringList( richHeaders, numRichHeaders ) ) {} 00113 virtual ~RichHeaderStrategy() {} 00114 00115 public: 00116 const char * name() const { return "rich"; } 00117 const HeaderStrategy * next() const { return standard(); } 00118 const HeaderStrategy * prev() const { return all(); } 00119 00120 TQStringList headersToDisplay() const { return mHeadersToDisplay; } 00121 DefaultPolicy defaultPolicy() const { return Hide; } 00122 00123 private: 00124 const TQStringList mHeadersToDisplay; 00125 }; 00126 00127 // 00128 // StandardHeaderStrategy: 00129 // BCC, CC, Date, From, Subject, To 00130 // 00131 00132 class StandardHeaderStrategy : public HeaderStrategy { 00133 friend class ::KMail::HeaderStrategy; 00134 protected: 00135 StandardHeaderStrategy() 00136 : HeaderStrategy(), 00137 mHeadersToDisplay( stringList( standardHeaders, numStandardHeaders) ) {} 00138 virtual ~StandardHeaderStrategy() {} 00139 00140 public: 00141 const char * name() const { return "standard"; } 00142 const HeaderStrategy * next() const { return brief(); } 00143 const HeaderStrategy * prev() const { return rich(); } 00144 00145 TQStringList headersToDisplay() const { return mHeadersToDisplay; } 00146 DefaultPolicy defaultPolicy() const { return Hide; } 00147 00148 private: 00149 const TQStringList mHeadersToDisplay; 00150 }; 00151 00152 // 00153 // BriefHeaderStrategy 00154 // From, Subject, Date 00155 // 00156 00157 class BriefHeaderStrategy : public HeaderStrategy { 00158 friend class ::KMail::HeaderStrategy; 00159 protected: 00160 BriefHeaderStrategy() 00161 : HeaderStrategy(), 00162 mHeadersToDisplay( stringList( briefHeaders, numBriefHeaders ) ) {} 00163 virtual ~BriefHeaderStrategy() {} 00164 00165 public: 00166 const char * name() const { return "brief"; } 00167 const HeaderStrategy * next() const { return custom(); } 00168 const HeaderStrategy * prev() const { return standard(); } 00169 00170 TQStringList headersToDisplay() const { return mHeadersToDisplay; } 00171 DefaultPolicy defaultPolicy() const { return Hide; } 00172 00173 private: 00174 const TQStringList mHeadersToDisplay; 00175 }; 00176 00177 00178 // 00179 // CustomHeaderStrategy 00180 // Determined by user 00181 // 00182 00183 class CustomHeaderStrategy : public HeaderStrategy { 00184 friend class ::KMail::HeaderStrategy; 00185 protected: 00186 CustomHeaderStrategy(); 00187 virtual ~CustomHeaderStrategy() {} 00188 00189 public: 00190 const char * name() const { return "custom"; } 00191 const HeaderStrategy * next() const { return all(); } 00192 const HeaderStrategy * prev() const { return brief(); } 00193 00194 TQStringList headersToDisplay() const { return mHeadersToDisplay; } 00195 TQStringList headersToHide() const { return mHeadersToHide; } 00196 DefaultPolicy defaultPolicy() const { return mDefaultPolicy; } 00197 00198 private: 00199 TQStringList mHeadersToDisplay; 00200 TQStringList mHeadersToHide; 00201 DefaultPolicy mDefaultPolicy; 00202 }; 00203 00204 00205 CustomHeaderStrategy::CustomHeaderStrategy() 00206 : HeaderStrategy() 00207 { 00208 TDEConfigGroup customHeader( KMKernel::config(), "Custom Headers" ); 00209 if ( customHeader.hasKey( "headers to display" ) ) { 00210 mHeadersToDisplay = customHeader.readListEntry( "headers to display" ); 00211 for ( TQStringList::iterator it = mHeadersToDisplay.begin() ; it != mHeadersToDisplay.end() ; ++ it ) 00212 *it = (*it).lower(); 00213 } else 00214 mHeadersToDisplay = stringList( standardHeaders, numStandardHeaders ); 00215 00216 if ( customHeader.hasKey( "headers to hide" ) ) { 00217 mHeadersToHide = customHeader.readListEntry( "headers to hide" ); 00218 for ( TQStringList::iterator it = mHeadersToHide.begin() ; it != mHeadersToHide.end() ; ++ it ) 00219 *it = (*it).lower(); 00220 } 00221 00222 mDefaultPolicy = customHeader.readEntry( "default policy", "hide" ) == "display" ? Display : Hide ; 00223 } 00224 00225 // 00226 // HeaderStrategy abstract base: 00227 // 00228 00229 HeaderStrategy::HeaderStrategy() { 00230 00231 } 00232 00233 HeaderStrategy::~HeaderStrategy() { 00234 00235 } 00236 00237 TQStringList HeaderStrategy::headersToDisplay() const { 00238 return TQStringList(); 00239 } 00240 00241 TQStringList HeaderStrategy::headersToHide() const { 00242 return TQStringList(); 00243 } 00244 00245 bool HeaderStrategy::showHeader( const TQString & header ) const { 00246 if ( headersToDisplay().contains( header.lower() ) ) return true; 00247 if ( headersToHide().contains( header.lower() ) ) return false; 00248 return defaultPolicy() == Display; 00249 } 00250 00251 const HeaderStrategy * HeaderStrategy::create( Type type ) { 00252 switch ( type ) { 00253 case All: return all(); 00254 case Rich: return rich(); 00255 case Standard: return standard(); 00256 case Brief: return brief(); 00257 case Custom: return custom(); 00258 } 00259 kdFatal( 5006 ) << "HeaderStrategy::create(): Unknown header strategy ( type == " 00260 << (int)type << " ) requested!" << endl; 00261 return 0; // make compiler happy 00262 } 00263 00264 const HeaderStrategy * HeaderStrategy::create( const TQString & type ) { 00265 TQString lowerType = type.lower(); 00266 if ( lowerType == "all" ) return all(); 00267 if ( lowerType == "rich" ) return HeaderStrategy::rich(); 00268 //if ( lowerType == "standard" ) return standard(); // not needed, see below 00269 if ( lowerType == "brief" ) return brief(); 00270 if ( lowerType == "custom" ) return custom(); 00271 // don't kdFatal here, b/c the strings are user-provided 00272 // (TDEConfig), so fail gracefully to the default: 00273 return standard(); 00274 } 00275 00276 static const HeaderStrategy * allStrategy = 0; 00277 static const HeaderStrategy * richStrategy = 0; 00278 static const HeaderStrategy * standardStrategy = 0; 00279 static const HeaderStrategy * briefStrategy = 0; 00280 static const HeaderStrategy * customStrategy = 0; 00281 00282 const HeaderStrategy * HeaderStrategy::all() { 00283 if ( !allStrategy ) 00284 allStrategy = new AllHeaderStrategy(); 00285 return allStrategy; 00286 } 00287 00288 const HeaderStrategy * HeaderStrategy::rich() { 00289 if ( !richStrategy ) 00290 richStrategy = new RichHeaderStrategy(); 00291 return richStrategy; 00292 } 00293 00294 const HeaderStrategy * HeaderStrategy::standard() { 00295 if ( !standardStrategy ) 00296 standardStrategy = new StandardHeaderStrategy(); 00297 return standardStrategy; 00298 } 00299 00300 const HeaderStrategy * HeaderStrategy::brief() { 00301 if ( !briefStrategy ) 00302 briefStrategy = new BriefHeaderStrategy(); 00303 return briefStrategy; 00304 } 00305 00306 const HeaderStrategy * HeaderStrategy::custom() { 00307 if ( !customStrategy ) 00308 customStrategy = new CustomHeaderStrategy(); 00309 return customStrategy; 00310 } 00311 00312 } // namespace KMail