kmail
headerstrategy.cppGo to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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 <kconfig.h>
00042
00043 namespace KMail {
00044
00045
00046
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
00070
00071
00072 static QStringList stringList( const char * headers[], int numHeaders ) {
00073 QStringList sl;
00074 for ( int i = 0 ; i < numHeaders ; ++i )
00075 sl.push_back( headers[i] );
00076 return sl;
00077 }
00078
00079
00080
00081
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 QString & ) const {
00098 return true;
00099 }
00100 };
00101
00102
00103
00104
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 QStringList headersToDisplay() const { return mHeadersToDisplay; }
00121 DefaultPolicy defaultPolicy() const { return Hide; }
00122
00123 private:
00124 const QStringList mHeadersToDisplay;
00125 };
00126
00127
00128
00129
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 QStringList headersToDisplay() const { return mHeadersToDisplay; }
00146 DefaultPolicy defaultPolicy() const { return Hide; }
00147
00148 private:
00149 const QStringList mHeadersToDisplay;
00150 };
00151
00152
00153
00154
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 QStringList headersToDisplay() const { return mHeadersToDisplay; }
00171 DefaultPolicy defaultPolicy() const { return Hide; }
00172
00173 private:
00174 const QStringList mHeadersToDisplay;
00175 };
00176
00177
00178
00179
00180
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 QStringList headersToDisplay() const { return mHeadersToDisplay; }
00195 QStringList headersToHide() const { return mHeadersToHide; }
00196 DefaultPolicy defaultPolicy() const { return mDefaultPolicy; }
00197
00198 private:
00199 QStringList mHeadersToDisplay;
00200 QStringList mHeadersToHide;
00201 DefaultPolicy mDefaultPolicy;
00202 };
00203
00204
00205 CustomHeaderStrategy::CustomHeaderStrategy()
00206 : HeaderStrategy()
00207 {
00208 KConfigGroup customHeader( KMKernel::config(), "Custom Headers" );
00209 if ( customHeader.hasKey( "headers to display" ) ) {
00210 mHeadersToDisplay = customHeader.readListEntry( "headers to display" );
00211 for ( QStringList::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 ( QStringList::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
00227
00228
00229 HeaderStrategy::HeaderStrategy() {
00230
00231 }
00232
00233 HeaderStrategy::~HeaderStrategy() {
00234
00235 }
00236
00237 QStringList HeaderStrategy::headersToDisplay() const {
00238 return QStringList();
00239 }
00240
00241 QStringList HeaderStrategy::headersToHide() const {
00242 return QStringList();
00243 }
00244
00245 bool HeaderStrategy::showHeader( const QString & 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;
00262 }
00263
00264 const HeaderStrategy * HeaderStrategy::create( const QString & type ) {
00265 QString lowerType = type.lower();
00266 if ( lowerType == "all" ) return all();
00267 if ( lowerType == "rich" ) return HeaderStrategy::rich();
00268
00269 if ( lowerType == "brief" ) return brief();
00270 if ( lowerType == "custom" ) return custom();
00271
00272
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 }
|