svn path=/trunk/kdepim/; revision=201205wilder-work
parent
96e8bc5ffa
commit
ad2f92ba54
3 changed files with 350 additions and 1 deletions
@ -0,0 +1,286 @@ |
||||
/* -*- c++ -*-
|
||||
headerstrategy.cpp |
||||
|
||||
KMail, the KDE mail client. |
||||
Copyright (c) 2003 Marc Mutz <mutz@kde.org> |
||||
|
||||
This program is free software; you can redistribute it and/or |
||||
modify it under the terms of the GNU General Public License, |
||||
version 2.0, as published by the Free Software Foundation. |
||||
You should have received a copy of the GNU General Public License |
||||
along with this program; if not, write to the Free Software Foundation, |
||||
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, US |
||||
*/ |
||||
|
||||
#include "headerstrategy.h" |
||||
|
||||
#include "kmkernel.h" |
||||
|
||||
#include <kdebug.h> |
||||
#include <kconfig.h> |
||||
|
||||
#include <qstringlist.h> |
||||
#include <qstring.h> |
||||
|
||||
namespace KMail { |
||||
|
||||
//
|
||||
// Header table:
|
||||
// be sure to keep numFooHeaders and the content of headerTable in sync!
|
||||
//
|
||||
|
||||
static const char * headerTable[] = { |
||||
"subject", "from", "date", // brief
|
||||
"to", "cc", "bcc", "organisation", "organization", // standard
|
||||
"reply-to" // rich
|
||||
}; |
||||
|
||||
static const int numBriefHeaders = 3; |
||||
static const int numRichHeaders = sizeof headerTable / sizeof *headerTable; |
||||
static const int numStandardHeaders = numRichHeaders - 1; |
||||
|
||||
//
|
||||
// Convenience function
|
||||
//
|
||||
|
||||
static QStringList stringList( const char * headers[], int numHeaders ) { |
||||
QStringList sl; |
||||
for ( int i = 0 ; i < numHeaders ; ++i ) |
||||
sl.push_back( headers[i] ); |
||||
return sl; |
||||
} |
||||
|
||||
//
|
||||
// AllHeaderStrategy:
|
||||
// show everything
|
||||
//
|
||||
|
||||
class AllHeaderStrategy : public HeaderStrategy { |
||||
friend class HeaderStrategy; |
||||
protected: |
||||
AllHeaderStrategy() : HeaderStrategy() {} |
||||
virtual ~AllHeaderStrategy() {} |
||||
|
||||
public: |
||||
const char * name() const { return "all"; } |
||||
const HeaderStrategy * next() const { return rich(); } |
||||
const HeaderStrategy * prev() const { return custom(); } |
||||
|
||||
DefaultPolicy defaultPolicy() const { return Display; } |
||||
|
||||
bool showHeader( const QString & ) const { |
||||
return true; // more efficient than default impl
|
||||
} |
||||
}; |
||||
|
||||
//
|
||||
// RichHeaderStrategy:
|
||||
// Date, Subject, From, To, CC, ### what exactly?
|
||||
//
|
||||
|
||||
class RichHeaderStrategy : public HeaderStrategy { |
||||
friend class HeaderStrategy; |
||||
protected: |
||||
RichHeaderStrategy() |
||||
: HeaderStrategy(), |
||||
mHeadersToDisplay( stringList( headerTable, numRichHeaders ) ) {} |
||||
virtual ~RichHeaderStrategy() {} |
||||
|
||||
public: |
||||
const char * name() const { return "rich"; } |
||||
const HeaderStrategy * next() const { return standard(); } |
||||
const HeaderStrategy * prev() const { return all(); } |
||||
|
||||
QStringList headersToDisplay() const { return mHeadersToDisplay; } |
||||
DefaultPolicy defaultPolicy() const { return Hide; } |
||||
|
||||
private: |
||||
const QStringList mHeadersToDisplay; |
||||
}; |
||||
|
||||
//
|
||||
// StandardHeaderStrategy:
|
||||
// BCC, CC, Date, From, Subject, To
|
||||
//
|
||||
|
||||
class StandardHeaderStrategy : public HeaderStrategy { |
||||
friend class HeaderStrategy; |
||||
protected: |
||||
StandardHeaderStrategy() |
||||
: HeaderStrategy(), |
||||
mHeadersToDisplay( stringList( headerTable, numStandardHeaders) ) {} |
||||
virtual ~StandardHeaderStrategy() {} |
||||
|
||||
public: |
||||
const char * name() const { return "standard"; } |
||||
const HeaderStrategy * next() const { return brief(); } |
||||
const HeaderStrategy * prev() const { return rich(); } |
||||
|
||||
QStringList headersToDisplay() const { return mHeadersToDisplay; } |
||||
DefaultPolicy defaultPolicy() const { return Hide; } |
||||
|
||||
private: |
||||
const QStringList mHeadersToDisplay; |
||||
}; |
||||
|
||||
//
|
||||
// BriefHeaderStrategy
|
||||
// From, Subject, Date
|
||||
//
|
||||
|
||||
class BriefHeaderStrategy : public HeaderStrategy { |
||||
friend class HeaderStrategy; |
||||
protected: |
||||
BriefHeaderStrategy() |
||||
: HeaderStrategy(), |
||||
mHeadersToDisplay( stringList( headerTable, numBriefHeaders ) ) {} |
||||
virtual ~BriefHeaderStrategy() {} |
||||
|
||||
public: |
||||
const char * name() const { return "brief"; } |
||||
const HeaderStrategy * next() const { return custom(); } |
||||
const HeaderStrategy * prev() const { return standard(); } |
||||
|
||||
QStringList headersToDisplay() const { return mHeadersToDisplay; } |
||||
DefaultPolicy defaultPolicy() const { return Hide; } |
||||
|
||||
private: |
||||
const QStringList mHeadersToDisplay; |
||||
}; |
||||
|
||||
|
||||
//
|
||||
// CustomHeaderStrategy
|
||||
// Determined by user
|
||||
//
|
||||
|
||||
class CustomHeaderStrategy : public HeaderStrategy { |
||||
friend class HeaderStrategy; |
||||
protected: |
||||
CustomHeaderStrategy(); |
||||
virtual ~CustomHeaderStrategy() {} |
||||
|
||||
public: |
||||
const char * name() const { return "custom"; } |
||||
const HeaderStrategy * next() const { return all(); } |
||||
const HeaderStrategy * prev() const { return brief(); } |
||||
|
||||
QStringList headersToDisplay() const { return mHeadersToDisplay; } |
||||
QStringList headersToHide() const { return mHeadersToHide; } |
||||
DefaultPolicy defaultPolicy() const { return mDefaultPolicy; } |
||||
|
||||
private: |
||||
QStringList mHeadersToDisplay; |
||||
QStringList mHeadersToHide; |
||||
DefaultPolicy mDefaultPolicy; |
||||
}; |
||||
|
||||
|
||||
CustomHeaderStrategy::CustomHeaderStrategy() |
||||
: HeaderStrategy() |
||||
{ |
||||
KConfigGroup customHeader( KMKernel::config(), "Custom Headers" ); |
||||
if ( customHeader.hasKey( "headers to display" ) ) { |
||||
mHeadersToDisplay = customHeader.readListEntry( "headers to display" ); |
||||
for ( QStringList::iterator it = mHeadersToDisplay.begin() ; it != mHeadersToDisplay.end() ; ++ it ) |
||||
*it = (*it).lower(); |
||||
} else |
||||
mHeadersToDisplay = stringList( headerTable, numStandardHeaders ); |
||||
|
||||
if ( customHeader.hasKey( "headers to hide" ) ) { |
||||
mHeadersToHide = customHeader.readListEntry( "headers to hide" ); |
||||
for ( QStringList::iterator it = mHeadersToHide.begin() ; it != mHeadersToHide.end() ; ++ it ) |
||||
*it = (*it).lower(); |
||||
} |
||||
|
||||
mDefaultPolicy = customHeader.readEntry( "default policy", "hide" ) == "display" ? Display : Hide ; |
||||
} |
||||
|
||||
//
|
||||
// HeaderStrategy abstract base:
|
||||
//
|
||||
|
||||
HeaderStrategy::HeaderStrategy() { |
||||
|
||||
} |
||||
|
||||
HeaderStrategy::~HeaderStrategy() { |
||||
|
||||
} |
||||
|
||||
QStringList HeaderStrategy::headersToDisplay() const { |
||||
return QStringList(); |
||||
} |
||||
|
||||
QStringList HeaderStrategy::headersToHide() const { |
||||
return QStringList(); |
||||
} |
||||
|
||||
bool HeaderStrategy::showHeader( const QString & header ) const { |
||||
if ( headersToDisplay().contains( header.lower() ) ) return true; |
||||
if ( headersToHide().contains( header.lower() ) ) return false; |
||||
return defaultPolicy() == Display; |
||||
} |
||||
|
||||
const HeaderStrategy * HeaderStrategy::create( Type type ) { |
||||
switch ( type ) { |
||||
case All: return all(); |
||||
case Rich: return rich(); |
||||
case Standard: return standard(); |
||||
case Brief: return brief(); |
||||
case Custom: return custom(); |
||||
} |
||||
kdFatal( 5006 ) << "HeaderStrategy::create(): Unknown header strategy ( type == " |
||||
<< (int)type << " ) requested!" << endl; |
||||
return 0; // make compiler happy
|
||||
} |
||||
|
||||
const HeaderStrategy * HeaderStrategy::create( const QString & type ) { |
||||
QString lowerType = type.lower(); |
||||
if ( lowerType == "all" ) return all(); |
||||
if ( lowerType == "rich" ) return HeaderStrategy::rich(); |
||||
//if ( lowerType == "standard" ) return standard(); // not needed, see below
|
||||
if ( lowerType == "brief" ) return brief(); |
||||
if ( lowerType == "custom" ) return custom(); |
||||
// don't kdFatal here, b/c the strings are user-provided
|
||||
// (KConfig), so fail gracefully to the default:
|
||||
return standard(); |
||||
} |
||||
|
||||
static const HeaderStrategy * allStrategy = 0; |
||||
static const HeaderStrategy * richStrategy = 0; |
||||
static const HeaderStrategy * standardStrategy = 0; |
||||
static const HeaderStrategy * briefStrategy = 0; |
||||
static const HeaderStrategy * customStrategy = 0; |
||||
|
||||
const HeaderStrategy * HeaderStrategy::all() { |
||||
if ( !allStrategy ) |
||||
allStrategy = new AllHeaderStrategy(); |
||||
return allStrategy; |
||||
} |
||||
|
||||
const HeaderStrategy * HeaderStrategy::rich() { |
||||
if ( !richStrategy ) |
||||
richStrategy = new RichHeaderStrategy(); |
||||
return richStrategy; |
||||
} |
||||
|
||||
const HeaderStrategy * HeaderStrategy::standard() { |
||||
if ( !standardStrategy ) |
||||
standardStrategy = new StandardHeaderStrategy(); |
||||
return standardStrategy; |
||||
} |
||||
|
||||
const HeaderStrategy * HeaderStrategy::brief() { |
||||
if ( !briefStrategy ) |
||||
briefStrategy = new BriefHeaderStrategy(); |
||||
return briefStrategy; |
||||
} |
||||
|
||||
const HeaderStrategy * HeaderStrategy::custom() { |
||||
if ( !customStrategy ) |
||||
customStrategy = new CustomHeaderStrategy(); |
||||
return customStrategy; |
||||
} |
||||
|
||||
}; // namespace KMail
|
||||
@ -0,0 +1,63 @@ |
||||
/* -*- c++ -*-
|
||||
headerstrategy.h |
||||
|
||||
KMail, the KDE mail client. |
||||
Copyright (c) 2003 Marc Mutz <mutz@kde.org> |
||||
|
||||
This program is free software; you can redistribute it and/or |
||||
modify it under the terms of the GNU General Public License, |
||||
version 2.0, as published by the Free Software Foundation. |
||||
You should have received a copy of the GNU General Public License |
||||
along with this program; if not, write to the Free Software Foundation, |
||||
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, US |
||||
*/ |
||||
|
||||
#ifndef __KMAIL_HEADERSTRATEGY_H__ |
||||
#define __KMAIL_HEADERSTRATEGY_H__ |
||||
|
||||
class QString; |
||||
class QStringList; |
||||
|
||||
namespace KMail { |
||||
|
||||
class HeaderStrategy { |
||||
protected: |
||||
HeaderStrategy(); |
||||
virtual ~HeaderStrategy(); |
||||
|
||||
public: |
||||
//
|
||||
// Factory methods:
|
||||
//
|
||||
enum Type { All, Rich, Standard, Brief, Custom }; |
||||
|
||||
static const HeaderStrategy * create( Type type ); |
||||
static const HeaderStrategy * create( const QString & type ); |
||||
|
||||
static const HeaderStrategy * all(); |
||||
static const HeaderStrategy * rich(); |
||||
static const HeaderStrategy * standard(); |
||||
static const HeaderStrategy * brief(); |
||||
static const HeaderStrategy * custom(); |
||||
|
||||
//
|
||||
// Methods for handling the strategies:
|
||||
//
|
||||
virtual const char * name() const = 0; |
||||
virtual const HeaderStrategy * next() const = 0; |
||||
virtual const HeaderStrategy * prev() const = 0; |
||||
|
||||
//
|
||||
// HeaderStrategy interface:
|
||||
//
|
||||
enum DefaultPolicy { Display, Hide }; |
||||
|
||||
virtual QStringList headersToDisplay() const; |
||||
virtual QStringList headersToHide() const; |
||||
virtual DefaultPolicy defaultPolicy() const = 0; |
||||
virtual bool showHeader( const QString & header ) const; |
||||
}; |
||||
|
||||
}; // namespace KMail
|
||||
|
||||
#endif // __KMAIL_HEADERSTRATEGY_H__
|
||||
Loading…
Reference in new issue