kmail

kmsearchpattern.h
1 // -*- mode: C++; c-file-style: "gnu" -*-
2 // kmsearchpattern.h
3 // Author: Marc Mutz <Marc@Mutz.com>
4 // This code is under GPL!
5 
6 #ifndef _kmsearchpattern_h_
7 #define _kmsearchpattern_h_
8 
9 #include <tdelocale.h>
10 #include <tqptrlist.h>
11 #include <tqstring.h>
12 #include <tqcstring.h>
13 #include "kmmsgbase.h" // for KMMsgStatus
14 
15 class KMMessage;
16 class TDEConfig;
17 class DwBoyerMoore;
18 class DwString;
19 
20 
21 // maximum number of filter rules per filter
22 const int FILTER_MAX_RULES=8;
23 
32 {
33 public:
41  enum Function { FuncNone = -1,
42  FuncContains=0, FuncContainsNot,
43  FuncEquals, FuncNotEqual,
44  FuncRegExp, FuncNotRegExp,
45  FuncIsGreater, FuncIsLessOrEqual,
46  FuncIsLess, FuncIsGreaterOrEqual,
47  FuncIsInAddressbook, FuncIsNotInAddressbook,
48  FuncIsInCategory, FuncIsNotInCategory,
49  FuncHasAttachment, FuncHasNoAttachment};
50  KMSearchRule ( const TQCString & field=0, Function=FuncContains,
51  const TQString &contents=TQString() );
52  KMSearchRule ( const KMSearchRule &other );
53 
54  const KMSearchRule & operator=( const KMSearchRule & other );
55 
58  static KMSearchRule* createInstance( const TQCString & field=0,
59  Function function=FuncContains,
60  const TQString & contents=TQString() );
61 
62  static KMSearchRule* createInstance( const TQCString & field,
63  const char * function,
64  const TQString & contents );
65 
66  static KMSearchRule * createInstance( const KMSearchRule & other );
67 
73  static KMSearchRule* createInstanceFromConfig( const TDEConfig * config, int aIdx );
74 
75  virtual ~KMSearchRule() {};
76 
81  virtual bool matches( const KMMessage * msg ) const = 0;
82 
87  virtual bool matches( const DwString & str, KMMessage & msg,
88  const DwBoyerMoore * headerField=0,
89  int headerLen=-1 ) const;
90 
95  virtual bool isEmpty() const = 0;
96 
99  virtual bool requiresBody() const { return true; }
100 
101 
107  void writeConfig( TDEConfig * config, int aIdx ) const;
108 
111  Function function() const { return mFunction; }
112 
114  void setFunction( Function aFunction ) { mFunction = aFunction; }
115 
126  TQCString field() const { return mField; }
127 
130  void setField( const TQCString & field ) { mField = field; }
131 
134  TQString contents() const { return mContents; }
136  void setContents( const TQString & aContents ) { mContents = aContents; }
137 
139  const TQString asString() const;
140 
141 private:
142  static Function configValueToFunc( const char * str );
143  static TQString functionToString( Function function );
144 
145  TQCString mField;
146  Function mFunction;
147  TQString mContents;
148 };
149 
150 
151 // subclasses representing the different kinds of searches
152 
160 {
161 public:
162  KMSearchRuleString( const TQCString & field=0, Function function=FuncContains,
163  const TQString & contents=TQString() );
164  KMSearchRuleString( const KMSearchRuleString & other );
165  const KMSearchRuleString & operator=( const KMSearchRuleString & other );
166 
167  virtual ~KMSearchRuleString();
168  virtual bool isEmpty() const ;
169  virtual bool requiresBody() const;
170 
171  virtual bool matches( const KMMessage * msg ) const;
172 
176  virtual bool matches( const DwString & str, KMMessage & msg,
177  const DwBoyerMoore * headerField=0,
178  int headerLen=-1 ) const;
179 
181  bool matchesInternal( const TQString & msgContents ) const;
182 
183 private:
184  const DwBoyerMoore *mBmHeaderField;
185 };
186 
187 
195 {
196 public:
197  KMSearchRuleNumerical( const TQCString & field=0, Function function=FuncContains,
198  const TQString & contents=TQString() );
199  virtual bool isEmpty() const ;
200 
201  virtual bool matches( const KMMessage * msg ) const;
202 
204  bool matchesInternal( long numericalValue, long numericalMsgContents,
205  const TQString & msgContents ) const;
206 };
207 
208 
209 namespace KMail {
210 // The below are used in several places and here so they are accessible.
211  struct MessageStatus {
212  const char * const text;
213  const char * const icon;
214  };
215 
216  // If you change the ordering here; also do it in the enum below
217  static const MessageStatus StatusValues[] = {
218  { I18N_NOOP( "Important" ), "kmmsgflag" },
219  { I18N_NOOP( "New" ), "kmmsgnew" },
220  { I18N_NOOP( "Unread" ), "kmmsgunseen" },
221  { I18N_NOOP( "Read" ), "kmmsgread" },
222  { I18N_NOOP( "Old" ), 0 },
223  { I18N_NOOP( "Deleted" ), "kmmsgdel" },
224  { I18N_NOOP( "Replied" ), "kmmsgreplied" },
225  { I18N_NOOP( "Forwarded" ), "kmmsgforwarded" },
226  { I18N_NOOP( "Queued" ), "kmmsgqueued" },
227  { I18N_NOOP( "Sent" ), "kmmsgsent" },
228  { I18N_NOOP( "Watched" ), "kmmsgwatched" },
229  { I18N_NOOP( "Ignored" ), "kmmsgignored" },
230  { I18N_NOOP( "Spam" ), "kmmsgspam" },
231  { I18N_NOOP( "Ham" ), "kmmsgham" },
232  { I18N_NOOP( "To Do" ), "kmmsgtodo" },
233  { I18N_NOOP( "Invitation" ), "kmmsginvitation" },
234  { I18N_NOOP( "Has Attachment"), "kmmsgattachment" } //must be last
235  };
236  // If you change the ordering here; also do it in the array above
237  enum StatusValueTypes {
238  StatusImportant = 0,
239  StatusNew = 1,
240  StatusUnread = 2,
241  StatusRead = 3,
242  StatusOld = 4,
243  StatusDeleted = 5,
244  StatusReplied = 6,
245  StatusForwarded = 7,
246  StatusQueued = 8,
247  StatusSent = 9,
248  StatusWatched = 10,
249  StatusIgnored = 11,
250  StatusSpam = 12,
251  StatusHam = 13,
252  StatusToDo = 14,
253  StatusInvitation = 15,
254  StatusHasAttachment = 16 //must be last
255  };
256 
257  static const int StatusValueCount =
258  sizeof( StatusValues ) / sizeof( MessageStatus );
259  // we want to show all status entries in the quick search bar, but only the
260  // ones up to attachment in the search/filter dialog, because there the
261  // attachment case is handled separately.
262  static const int StatusValueCountWithoutHidden = StatusValueCount - 1;
263 }
264 
271 {
272 public:
273  KMSearchRuleStatus( const TQCString & field=0, Function function=FuncContains,
274  const TQString & contents=TQString() );
275  KMSearchRuleStatus( int status, Function function=FuncContains );
276 
277  virtual bool isEmpty() const ;
278  virtual bool matches( const KMMessage * msg ) const;
279  //Not possible to implement this form for status searching
280  virtual bool matches( const DwString &, KMMessage &,
281  const DwBoyerMoore *,
282  int ) const;
283  static KMMsgStatus statusFromEnglishName(const TQString&);
284  private:
285  KMMsgStatus mStatus;
286 };
287 
288 // ------------------------------------------------------------------------
289 
308 class KMSearchPattern : public TQPtrList<KMSearchRule>
309 {
310 
311 public:
317  enum Operator { OpAnd, OpOr };
326  KMSearchPattern( const TDEConfig * config=0 );
327 
330 
340  bool matches( const KMMessage * msg, bool ignoreBody = false ) const;
341  bool matches( const DwString & str, bool ignoreBody = false ) const;
342  bool matches( TQ_UINT32 sernum, bool ignoreBody = false ) const;
343 
346  bool requiresBody() const;
347 
352  void purify();
353 
366  void readConfig( const TDEConfig * config );
373  void writeConfig( TDEConfig * config ) const;
374 
376  TQString name() const { return mName; }
379  void setName( const TQString & newName ) { mName = newName ; }
380 
382  KMSearchPattern::Operator op() const { return mOperator; }
384  void setOp( KMSearchPattern::Operator aOp ) { mOperator = aOp; }
385 
387  TQString asString() const;
388 
390  const KMSearchPattern & operator=( const KMSearchPattern & aPattern );
391 
392 private:
400  void importLegacyConfig( const TDEConfig * config );
403  void init();
404 
405  TQString mName;
406  Operator mOperator;
407 };
408 
409 #endif /* _kmsearchpattern_h_ */