kmail

kmsearchpattern.h

00001 // -*- mode: C++; c-file-style: "gnu" -*-
00002 // kmsearchpattern.h
00003 // Author: Marc Mutz <Marc@Mutz.com>
00004 // This code is under GPL!
00005 
00006 #ifndef _kmsearchpattern_h_
00007 #define _kmsearchpattern_h_
00008 
00009 #include <klocale.h>
00010 #include <tqptrlist.h>
00011 #include <tqstring.h>
00012 #include <tqcstring.h>
00013 #include "kmmsgbase.h" // for KMMsgStatus
00014 
00015 class KMMessage;
00016 class KConfig;
00017 class DwBoyerMoore;
00018 class DwString;
00019 
00020 
00021 // maximum number of filter rules per filter
00022 const int FILTER_MAX_RULES=8;
00023 
00031 class KMSearchRule
00032 {
00033 public:
00041   enum Function { FuncNone = -1,
00042                   FuncContains=0, FuncContainsNot,
00043           FuncEquals, FuncNotEqual,
00044           FuncRegExp, FuncNotRegExp,
00045           FuncIsGreater, FuncIsLessOrEqual,
00046           FuncIsLess, FuncIsGreaterOrEqual,
00047           FuncIsInAddressbook, FuncIsNotInAddressbook,
00048                   FuncIsInCategory, FuncIsNotInCategory,
00049           FuncHasAttachment, FuncHasNoAttachment};
00050   KMSearchRule ( const TQCString & field=0, Function=FuncContains,
00051                  const TQString &contents=TQString() );
00052   KMSearchRule ( const KMSearchRule &other );
00053 
00054   const KMSearchRule & operator=( const KMSearchRule & other );
00055 
00058   static KMSearchRule* createInstance( const TQCString & field=0,
00059                                       Function function=FuncContains,
00060                               const TQString & contents=TQString() );
00061 
00062   static KMSearchRule* createInstance( const TQCString & field,
00063                                        const char * function,
00064                                        const TQString & contents );
00065 
00066   static KMSearchRule * createInstance( const KMSearchRule & other );
00067 
00073   static KMSearchRule* createInstanceFromConfig( const KConfig * config, int aIdx );
00074 
00075   virtual ~KMSearchRule() {};
00076 
00081   virtual bool matches( const KMMessage * msg ) const = 0;
00082 
00087    virtual bool matches( const DwString & str, KMMessage & msg,
00088                          const DwBoyerMoore * headerField=0,
00089                          int headerLen=-1 ) const;
00090 
00095   virtual bool isEmpty() const = 0;
00096 
00099   virtual bool requiresBody() const { return true; }
00100 
00101 
00107   void writeConfig( KConfig * config, int aIdx ) const;
00108 
00111   Function function() const { return mFunction; }
00112 
00114   void setFunction( Function aFunction ) { mFunction = aFunction; }
00115 
00126   TQCString field() const { return mField; }
00127 
00130   void setField( const TQCString & field ) { mField = field; }
00131 
00134   TQString contents() const { return mContents; }
00136   void setContents( const TQString & aContents ) { mContents = aContents; }
00137 
00139   const TQString asString() const;
00140 
00141 private:
00142   static Function configValueToFunc( const char * str );
00143   static TQString functionToString( Function function );
00144 
00145   TQCString mField;
00146   Function mFunction;
00147   TQString  mContents;
00148 };
00149 
00150 
00151 // subclasses representing the different kinds of searches
00152 
00159 class KMSearchRuleString : public KMSearchRule
00160 {
00161 public:
00162   KMSearchRuleString( const TQCString & field=0, Function function=FuncContains,
00163         const TQString & contents=TQString() );
00164   KMSearchRuleString( const KMSearchRuleString & other );
00165   const KMSearchRuleString & operator=( const KMSearchRuleString & other );
00166 
00167   virtual ~KMSearchRuleString();
00168   virtual bool isEmpty() const ;
00169   virtual bool requiresBody() const;
00170 
00171   virtual bool matches( const KMMessage * msg ) const;
00172 
00176   virtual bool matches( const DwString & str, KMMessage & msg,
00177                         const DwBoyerMoore * headerField=0,
00178                         int headerLen=-1 ) const;
00179 
00181   bool matchesInternal( const TQString & msgContents ) const;
00182 
00183 private:
00184   const DwBoyerMoore *mBmHeaderField;
00185 };
00186 
00187 
00194 class KMSearchRuleNumerical : public KMSearchRule
00195 {
00196 public:
00197   KMSearchRuleNumerical( const TQCString & field=0, Function function=FuncContains,
00198                  const TQString & contents=TQString() );
00199   virtual bool isEmpty() const ;
00200 
00201   virtual bool matches( const KMMessage * msg ) const;
00202 
00204   bool matchesInternal( long numericalValue, long numericalMsgContents,
00205                         const TQString & msgContents ) const;
00206 };
00207 
00208 
00209 namespace KMail {
00210 // The below are used in several places and here so they are accessible.
00211   struct MessageStatus {
00212     const char * const text;
00213     const char * const icon;
00214   };
00215 
00216   // If you change the ordering here; also do it in the enum below
00217   static const MessageStatus StatusValues[] = {
00218     { I18N_NOOP( "Important" ),        "kmmsgflag"   },
00219     { I18N_NOOP( "New" ),              "kmmsgnew"   },
00220     { I18N_NOOP( "Unread" ),           "kmmsgunseen"   },
00221     { I18N_NOOP( "Read" ),             "kmmsgread"   },
00222     { I18N_NOOP( "Old" ),              0   },
00223     { I18N_NOOP( "Deleted" ),          "kmmsgdel"   },
00224     { I18N_NOOP( "Replied" ),          "kmmsgreplied"   },
00225     { I18N_NOOP( "Forwarded" ),        "kmmsgforwarded"   },
00226     { I18N_NOOP( "Queued" ),           "kmmsgqueued"   },
00227     { I18N_NOOP( "Sent" ),             "kmmsgsent"   },
00228     { I18N_NOOP( "Watched" ),          "kmmsgwatched"   },
00229     { I18N_NOOP( "Ignored" ),          "kmmsgignored"   },
00230     { I18N_NOOP( "Spam" ),             "kmmsgspam"   },
00231     { I18N_NOOP( "Ham" ),              "kmmsgham"   },
00232     { I18N_NOOP( "To Do" ),            "kmmsgtodo"   },
00233     { I18N_NOOP( "Invitation" ),       "kmmsginvitation"   },
00234     { I18N_NOOP( "Has Attachment"),    "kmmsgattachment"   }  //must be last
00235   };
00236   // If you change the ordering here; also do it in the array above
00237   enum StatusValueTypes {
00238     StatusImportant = 0,
00239     StatusNew = 1,
00240     StatusUnread = 2,
00241     StatusRead = 3,
00242     StatusOld = 4,
00243     StatusDeleted = 5,
00244     StatusReplied = 6,
00245     StatusForwarded = 7,
00246     StatusQueued = 8,
00247     StatusSent = 9,
00248     StatusWatched = 10,
00249     StatusIgnored = 11,
00250     StatusSpam = 12,
00251     StatusHam = 13,
00252     StatusToDo = 14,
00253     StatusInvitation = 15,
00254     StatusHasAttachment = 16  //must be last
00255   };
00256 
00257   static const int StatusValueCount =
00258     sizeof( StatusValues ) / sizeof( MessageStatus );
00259   // we want to show all status entries in the quick search bar, but only the
00260   // ones up to attachment in the search/filter dialog, because there the
00261   // attachment case is handled separately.
00262   static const int StatusValueCountWithoutHidden = StatusValueCount - 1;
00263 }
00264 
00270 class KMSearchRuleStatus : public KMSearchRule
00271 {
00272 public:
00273    KMSearchRuleStatus( const TQCString & field=0, Function function=FuncContains,
00274                const TQString & contents=TQString() );
00275    KMSearchRuleStatus( int status, Function function=FuncContains );
00276 
00277   virtual bool isEmpty() const ;
00278   virtual bool matches( const KMMessage * msg ) const;
00279   //Not possible to implement this form for status searching
00280   virtual bool matches( const DwString &, KMMessage &,
00281                         const DwBoyerMoore *,
00282             int ) const;
00283   static KMMsgStatus statusFromEnglishName(const TQString&);
00284   private:
00285   KMMsgStatus mStatus;
00286 };
00287 
00288 // ------------------------------------------------------------------------
00289 
00308 class KMSearchPattern : public TQPtrList<KMSearchRule>
00309 {
00310 
00311 public:
00317   enum Operator { OpAnd, OpOr };
00326   KMSearchPattern( const KConfig * config=0 );
00327 
00329   ~KMSearchPattern();
00330 
00340   bool matches( const KMMessage * msg, bool ignoreBody = false ) const;
00341   bool matches( const DwString & str, bool ignoreBody = false ) const;
00342   bool matches( TQ_UINT32 sernum, bool ignoreBody = false ) const;
00343 
00346   bool requiresBody() const;
00347 
00352   void purify();
00353 
00366   void readConfig( const KConfig * config );
00373   void writeConfig( KConfig * config ) const;
00374 
00376   TQString name() const { return mName; }
00379   void setName( const TQString & newName ) { mName = newName ; }
00380 
00382   KMSearchPattern::Operator op() const { return mOperator; }
00384   void setOp( KMSearchPattern::Operator aOp ) { mOperator = aOp; }
00385 
00387   TQString asString() const;
00388 
00390   const KMSearchPattern & operator=( const KMSearchPattern & aPattern );
00391 
00392 private:
00400   void importLegacyConfig( const KConfig * config );
00403   void init();
00404 
00405   TQString  mName;
00406   Operator mOperator;
00407 };
00408 
00409 #endif /* _kmsearchpattern_h_ */