summaryrefslogtreecommitdiffstats
path: root/kmail/headerstrategy.cpp
blob: 239525d30d4273029628c7201c59ecd5ab999b50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*  -*- c++ -*-
    headerstrategy.cpp

    This file is part of KMail, the KDE mail client.
    Copyright (c) 2003 Marc Mutz <mutz@kde.org>

    KMail is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License, version 2, as
    published by the Free Software Foundation.

    KMail is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    In addition, as a special exception, the copyright holders give
    permission to link the code of this program with any edition of
    the TQt library by Trolltech AS, Norway (or with modified versions
    of TQt that use the same license as TQt), and distribute linked
    combinations including the two.  You must obey the GNU General
    Public License in all respects for all of the code used other than
    TQt.  If you modify this file, you may extend this exception to
    your version of the file, but you are not obligated to do so.  If
    you do not wish to do so, delete this exception statement from
    your version.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "headerstrategy.h"

#include "kmkernel.h"

#include <kdebug.h>
#include <kconfig.h>

namespace KMail {

  //
  // Header tables:
  //

  static const char * briefHeaders[] = {
    "subject", "from", "cc", "bcc", "date"
  };
  static const int numBriefHeaders = sizeof briefHeaders / sizeof *briefHeaders;


  static const char * standardHeaders[] = {
    "subject", "from", "cc", "bcc", "to"
  };
  static const int numStandardHeaders = sizeof standardHeaders / sizeof *standardHeaders;


  static const char * richHeaders[] = {
    "subject", "date", "from", "cc", "bcc", "to",
    "organization", "organisation", "reply-to",
    "user-agent", "x-mailer"
  };
  static const int numRichHeaders = sizeof richHeaders / sizeof *richHeaders;

  //
  // Convenience function
  //

  static TQStringList stringList( const char * headers[], int numHeaders ) {
    TQStringList sl;
    for ( int i = 0 ; i < numHeaders ; ++i )
      sl.push_back( headers[i] );
    return sl;
  }

  //
  // AllHeaderStrategy:
  //   show everything
  //

  class AllHeaderStrategy : public HeaderStrategy {
    friend class ::KMail::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 TQString & ) const {
      return true; // more efficient than default impl
    }
  };

  //
  // RichHeaderStrategy:
  //   Date, Subject, From, To, CC, ### what exactly?
  //

  class RichHeaderStrategy : public HeaderStrategy {
    friend class ::KMail::HeaderStrategy;
  protected:
    RichHeaderStrategy()
      : HeaderStrategy(),
	mHeadersToDisplay( stringList( richHeaders, numRichHeaders ) ) {}
    virtual ~RichHeaderStrategy() {}

  public:
    const char * name() const { return "rich"; }
    const HeaderStrategy * next() const { return standard(); }
    const HeaderStrategy * prev() const { return all(); }

    TQStringList headersToDisplay() const { return mHeadersToDisplay; }
    DefaultPolicy defaultPolicy() const { return Hide; }

  private:
    const TQStringList mHeadersToDisplay;
  };

  //
  // StandardHeaderStrategy:
  //   BCC, CC, Date, From, Subject, To
  //

  class StandardHeaderStrategy : public HeaderStrategy {
    friend class ::KMail::HeaderStrategy;
  protected:
    StandardHeaderStrategy()
      : HeaderStrategy(),
	mHeadersToDisplay( stringList( standardHeaders, numStandardHeaders) ) {}
    virtual ~StandardHeaderStrategy() {}

  public:
    const char * name() const { return "standard"; }
    const HeaderStrategy * next() const { return brief(); }
    const HeaderStrategy * prev() const { return rich(); }

    TQStringList headersToDisplay() const { return mHeadersToDisplay; }
    DefaultPolicy defaultPolicy() const { return Hide; }

  private:
    const TQStringList mHeadersToDisplay;
  };

  //
  // BriefHeaderStrategy
  //   From, Subject, Date
  //

  class BriefHeaderStrategy : public HeaderStrategy {
    friend class ::KMail::HeaderStrategy;
  protected:
    BriefHeaderStrategy()
      : HeaderStrategy(),
	mHeadersToDisplay( stringList( briefHeaders, numBriefHeaders ) ) {}
    virtual ~BriefHeaderStrategy() {}

  public:
    const char * name() const { return "brief"; }
    const HeaderStrategy * next() const { return custom(); }
    const HeaderStrategy * prev() const { return standard(); }

    TQStringList headersToDisplay() const { return mHeadersToDisplay; }
    DefaultPolicy defaultPolicy() const { return Hide; }

  private:
    const TQStringList mHeadersToDisplay;
  };


  //
  // CustomHeaderStrategy
  //   Determined by user
  //

  class CustomHeaderStrategy : public HeaderStrategy {
    friend class ::KMail::HeaderStrategy;
  protected:
    CustomHeaderStrategy();
    virtual ~CustomHeaderStrategy() {}

  public:
    const char * name() const { return "custom"; }
    const HeaderStrategy * next() const { return all(); }
    const HeaderStrategy * prev() const { return brief(); }

    TQStringList headersToDisplay() const { return mHeadersToDisplay; }
    TQStringList headersToHide() const { return mHeadersToHide; }
    DefaultPolicy defaultPolicy() const { return mDefaultPolicy; }

  private:
    TQStringList mHeadersToDisplay;
    TQStringList 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 ( TQStringList::iterator it = mHeadersToDisplay.begin() ; it != mHeadersToDisplay.end() ; ++ it )
	*it = (*it).lower();
    } else
      mHeadersToDisplay = stringList( standardHeaders, numStandardHeaders );

    if ( customHeader.hasKey( "headers to hide" ) ) {
      mHeadersToHide = customHeader.readListEntry( "headers to hide" );
      for ( TQStringList::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() {

  }

  TQStringList HeaderStrategy::headersToDisplay() const {
    return TQStringList();
  }

  TQStringList HeaderStrategy::headersToHide() const {
    return TQStringList();
  }

  bool HeaderStrategy::showHeader( const TQString & 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 TQString & type ) {
    TQString 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