kmail

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