kmail

headerstrategy.cpp
1 /*
2  headerstrategy.cpp
3 
4  This file is part of KMail, the KDE mail client.
5  Copyright (c) 2003 Marc Mutz <mutz@kde.org>
6 
7  KMail is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License, version 2, as
9  published by the Free Software Foundation.
10 
11  KMail is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 
20  In addition, as a special exception, the copyright holders give
21  permission to link the code of this program with any edition of
22  the TQt library by Trolltech AS, Norway (or with modified versions
23  of TQt that use the same license as TQt), and distribute linked
24  combinations including the two. You must obey the GNU General
25  Public License in all respects for all of the code used other than
26  TQt. If you modify this file, you may extend this exception to
27  your version of the file, but you are not obligated to do so. If
28  you do not wish to do so, delete this exception statement from
29  your version.
30 */
31 
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35 
36 #include "headerstrategy.h"
37 
38 #include "kmkernel.h"
39 
40 #include <kdebug.h>
41 #include <tdeconfig.h>
42 
43 namespace KMail {
44 
45  //
46  // Header tables:
47  //
48 
49  static const char * briefHeaders[] = {
50  "subject", "from", "cc", "bcc", "date"
51  };
52  static const int numBriefHeaders = sizeof briefHeaders / sizeof *briefHeaders;
53 
54 
55  static const char * standardHeaders[] = {
56  "subject", "from", "cc", "bcc", "to"
57  };
58  static const int numStandardHeaders = sizeof standardHeaders / sizeof *standardHeaders;
59 
60 
61  static const char * richHeaders[] = {
62  "subject", "date", "from", "cc", "bcc", "to",
63  "organization", "organisation", "reply-to",
64  "user-agent", "x-mailer"
65  };
66  static const int numRichHeaders = sizeof richHeaders / sizeof *richHeaders;
67 
68  //
69  // Convenience function
70  //
71 
72  static TQStringList stringList( const char * headers[], int numHeaders ) {
73  TQStringList sl;
74  for ( int i = 0 ; i < numHeaders ; ++i )
75  sl.push_back( headers[i] );
76  return sl;
77  }
78 
79  //
80  // AllHeaderStrategy:
81  // show everything
82  //
83 
84  class AllHeaderStrategy : public HeaderStrategy {
85  friend class ::KMail::HeaderStrategy;
86  protected:
87  AllHeaderStrategy() : HeaderStrategy() {}
88  virtual ~AllHeaderStrategy() {}
89 
90  public:
91  const char * name() const { return "all"; }
92  const HeaderStrategy * next() const { return rich(); }
93  const HeaderStrategy * prev() const { return custom(); }
94 
95  DefaultPolicy defaultPolicy() const { return Display; }
96 
97  bool showHeader( const TQString & ) const {
98  return true; // more efficient than default impl
99  }
100  };
101 
102  //
103  // RichHeaderStrategy:
104  // Date, Subject, From, To, CC, ### what exactly?
105  //
106 
107  class RichHeaderStrategy : public HeaderStrategy {
108  friend class ::KMail::HeaderStrategy;
109  protected:
110  RichHeaderStrategy()
111  : HeaderStrategy(),
112  mHeadersToDisplay( stringList( richHeaders, numRichHeaders ) ) {}
113  virtual ~RichHeaderStrategy() {}
114 
115  public:
116  const char * name() const { return "rich"; }
117  const HeaderStrategy * next() const { return standard(); }
118  const HeaderStrategy * prev() const { return all(); }
119 
120  TQStringList headersToDisplay() const { return mHeadersToDisplay; }
121  DefaultPolicy defaultPolicy() const { return Hide; }
122 
123  private:
124  const TQStringList mHeadersToDisplay;
125  };
126 
127  //
128  // StandardHeaderStrategy:
129  // BCC, CC, Date, From, Subject, To
130  //
131 
132  class StandardHeaderStrategy : public HeaderStrategy {
133  friend class ::KMail::HeaderStrategy;
134  protected:
135  StandardHeaderStrategy()
136  : HeaderStrategy(),
137  mHeadersToDisplay( stringList( standardHeaders, numStandardHeaders) ) {}
138  virtual ~StandardHeaderStrategy() {}
139 
140  public:
141  const char * name() const { return "standard"; }
142  const HeaderStrategy * next() const { return brief(); }
143  const HeaderStrategy * prev() const { return rich(); }
144 
145  TQStringList headersToDisplay() const { return mHeadersToDisplay; }
146  DefaultPolicy defaultPolicy() const { return Hide; }
147 
148  private:
149  const TQStringList mHeadersToDisplay;
150  };
151 
152  //
153  // BriefHeaderStrategy
154  // From, Subject, Date
155  //
156 
157  class BriefHeaderStrategy : public HeaderStrategy {
158  friend class ::KMail::HeaderStrategy;
159  protected:
160  BriefHeaderStrategy()
161  : HeaderStrategy(),
162  mHeadersToDisplay( stringList( briefHeaders, numBriefHeaders ) ) {}
163  virtual ~BriefHeaderStrategy() {}
164 
165  public:
166  const char * name() const { return "brief"; }
167  const HeaderStrategy * next() const { return custom(); }
168  const HeaderStrategy * prev() const { return standard(); }
169 
170  TQStringList headersToDisplay() const { return mHeadersToDisplay; }
171  DefaultPolicy defaultPolicy() const { return Hide; }
172 
173  private:
174  const TQStringList mHeadersToDisplay;
175  };
176 
177 
178  //
179  // CustomHeaderStrategy
180  // Determined by user
181  //
182 
183  class CustomHeaderStrategy : public HeaderStrategy {
184  friend class ::KMail::HeaderStrategy;
185  protected:
186  CustomHeaderStrategy();
187  virtual ~CustomHeaderStrategy() {}
188 
189  public:
190  const char * name() const { return "custom"; }
191  const HeaderStrategy * next() const { return all(); }
192  const HeaderStrategy * prev() const { return brief(); }
193 
194  TQStringList headersToDisplay() const { return mHeadersToDisplay; }
195  TQStringList headersToHide() const { return mHeadersToHide; }
196  DefaultPolicy defaultPolicy() const { return mDefaultPolicy; }
197 
198  private:
199  TQStringList mHeadersToDisplay;
200  TQStringList mHeadersToHide;
201  DefaultPolicy mDefaultPolicy;
202  };
203 
204 
205  CustomHeaderStrategy::CustomHeaderStrategy()
206  : HeaderStrategy()
207  {
208  TDEConfigGroup customHeader( KMKernel::config(), "Custom Headers" );
209  if ( customHeader.hasKey( "headers to display" ) ) {
210  mHeadersToDisplay = customHeader.readListEntry( "headers to display" );
211  for ( TQStringList::iterator it = mHeadersToDisplay.begin() ; it != mHeadersToDisplay.end() ; ++ it )
212  *it = (*it).lower();
213  } else
214  mHeadersToDisplay = stringList( standardHeaders, numStandardHeaders );
215 
216  if ( customHeader.hasKey( "headers to hide" ) ) {
217  mHeadersToHide = customHeader.readListEntry( "headers to hide" );
218  for ( TQStringList::iterator it = mHeadersToHide.begin() ; it != mHeadersToHide.end() ; ++ it )
219  *it = (*it).lower();
220  }
221 
222  mDefaultPolicy = customHeader.readEntry( "default policy", "hide" ) == "display" ? Display : Hide ;
223  }
224 
225  //
226  // HeaderStrategy abstract base:
227  //
228 
229  HeaderStrategy::HeaderStrategy() {
230 
231  }
232 
233  HeaderStrategy::~HeaderStrategy() {
234 
235  }
236 
237  TQStringList HeaderStrategy::headersToDisplay() const {
238  return TQStringList();
239  }
240 
241  TQStringList HeaderStrategy::headersToHide() const {
242  return TQStringList();
243  }
244 
245  bool HeaderStrategy::showHeader( const TQString & header ) const {
246  if ( headersToDisplay().contains( header.lower() ) ) return true;
247  if ( headersToHide().contains( header.lower() ) ) return false;
248  return defaultPolicy() == Display;
249  }
250 
251  const HeaderStrategy * HeaderStrategy::create( Type type ) {
252  switch ( type ) {
253  case All: return all();
254  case Rich: return rich();
255  case Standard: return standard();
256  case Brief: return brief();
257  case Custom: return custom();
258  }
259  kdFatal( 5006 ) << "HeaderStrategy::create(): Unknown header strategy ( type == "
260  << (int)type << " ) requested!" << endl;
261  return 0; // make compiler happy
262  }
263 
264  const HeaderStrategy * HeaderStrategy::create( const TQString & type ) {
265  TQString lowerType = type.lower();
266  if ( lowerType == "all" ) return all();
267  if ( lowerType == "rich" ) return HeaderStrategy::rich();
268  //if ( lowerType == "standard" ) return standard(); // not needed, see below
269  if ( lowerType == "brief" ) return brief();
270  if ( lowerType == "custom" ) return custom();
271  // don't kdFatal here, b/c the strings are user-provided
272  // (TDEConfig), so fail gracefully to the default:
273  return standard();
274  }
275 
276  static const HeaderStrategy * allStrategy = 0;
277  static const HeaderStrategy * richStrategy = 0;
278  static const HeaderStrategy * standardStrategy = 0;
279  static const HeaderStrategy * briefStrategy = 0;
280  static const HeaderStrategy * customStrategy = 0;
281 
282  const HeaderStrategy * HeaderStrategy::all() {
283  if ( !allStrategy )
284  allStrategy = new AllHeaderStrategy();
285  return allStrategy;
286  }
287 
288  const HeaderStrategy * HeaderStrategy::rich() {
289  if ( !richStrategy )
290  richStrategy = new RichHeaderStrategy();
291  return richStrategy;
292  }
293 
294  const HeaderStrategy * HeaderStrategy::standard() {
295  if ( !standardStrategy )
296  standardStrategy = new StandardHeaderStrategy();
297  return standardStrategy;
298  }
299 
300  const HeaderStrategy * HeaderStrategy::brief() {
301  if ( !briefStrategy )
302  briefStrategy = new BriefHeaderStrategy();
303  return briefStrategy;
304  }
305 
306  const HeaderStrategy * HeaderStrategy::custom() {
307  if ( !customStrategy )
308  customStrategy = new CustomHeaderStrategy();
309  return customStrategy;
310  }
311 
312 } // namespace KMail
folderdiaquotatab.h
Definition: aboutdata.cpp:40