filterelem.cpp

00001 /***************************************************************************
00002                           filterelem.cpp  -  description
00003                              -------------------
00004     begin                : Die Sep 23 2003
00005     copyright            : (C) 2003 by Eggert Ehmke
00006     email                : eggert.ehmke@berlin.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include <tqregexp.h>
00019 #include <tdelocale.h>
00020 #include <kdebug.h>
00021 #include "filterelem.h"
00022 
00023 FilterRecord::FilterRecord ():
00024   _type (from), _expression (contains), _CaseSensitive (false), _RegExp (false)
00025 {}
00026 
00027 FilterRecord::FilterRecord (filterType type, expressionType expression, const TQString& match, bool caseSensitive, bool regExp):
00028   _type (type),
00029   _expression (expression),
00030   _match (match),
00031   _CaseSensitive (caseSensitive),
00032   _RegExp (regExp)
00033 {}
00034 
00035 TQString FilterRecord::toString () const
00036 {
00037   TQString name;
00038   switch (_type)
00039   {
00040     case from:
00041       name += i18n ("From");
00042       break;
00043     case subject:
00044       name += i18n ("Subject");
00045       break;
00046     case header:
00047       name += i18n ("Header");
00048       break;
00049     case size:
00050       name += i18n ("Size");
00051       break;
00052     case to:
00053       name += i18n ("To");
00054       break;
00055   }
00056   name += " ";
00057   switch (_expression)
00058   {
00059     case contains:
00060       name += i18n ("Contains");
00061       break;
00062     case contains_not:
00063       name += i18n ("Doesn't contain");
00064       break;
00065     case equals:
00066       name += i18n ("Equals");
00067       break;
00068     case equals_not:
00069       name += i18n ("Doesn't equal");
00070       break;
00071     case greater:
00072       name += i18n ("Greater");
00073       break;
00074     case less:
00075       name += i18n ("Less");
00076       break;
00077   }
00078   name += " ";
00079   name += _match;
00080   return name;
00081 }
00082 
00083 FilterElem::FilterElem():
00084   _secondCondition (noSecondCondition),
00085   _counter (0)
00086 {
00087 }
00088 
00089 FilterElem::FilterElem (const FilterRecord& record):
00090   _filter1 (record), _secondCondition (noSecondCondition), _counter (0)
00091 {
00092 }
00093 
00094 FilterElem::FilterElem (const FilterElem& elem):
00095   _filter1 (elem._filter1),
00096   _filter2 (elem._filter2),
00097   _secondCondition (elem._secondCondition),
00098   _counter (elem._counter)
00099 {
00100 }
00101 
00102 FilterElem::~FilterElem()
00103 {
00104 }
00105 
00106 void FilterElem::setSecondCondition (secondCondition scnd, const FilterRecord& filter)
00107 {
00108   _secondCondition = scnd;
00109   _filter2 = filter;
00110 }
00111 
00112 TQString FilterElem::toString () const
00113 {
00114   TQString name;
00115   name.sprintf ("%06d_", _counter);
00116   name += _filter1.toString();
00117   if (_secondCondition != FilterElem::noSecondCondition)
00118   {
00119     name += " ";
00120     switch (_secondCondition)
00121     {
00122       case condAnd:
00123         name += i18n ("And");
00124         break;
00125       case condOr:
00126         name += i18n ("Or");
00127         break;
00128       default:
00129         break;
00130     }
00131     name += " " + _filter2.toString();
00132   }
00133   return name;
00134 }
00135 
00136 bool FilterRecord::matches (ShowRecordElem* pElem)
00137 {
00138   TQString text;
00139   int msgsize = 0;
00140   switch (_type)
00141   {
00142     case subject:
00143       text = pElem->subject();
00144       break;
00145     case from:
00146       text = pElem->from();
00147       break;
00148     case to:
00149       text = pElem->to();
00150       break;
00151     case header:
00152       text = pElem->header();
00153       break;
00154     case size:
00155       {
00156         msgsize = pElem->size();
00157         int value = _match.toInt();
00158         switch (_expression)
00159         {
00160           case equals:
00161             return (msgsize == value);
00162             break;
00163           case equals_not:
00164             return (msgsize != value);
00165             break;
00166           case greater:
00167             return (msgsize > value);
00168             break;
00169           case less:
00170             return (msgsize < value);
00171             break;
00172           default:
00173             // contains does not make sense for size
00174             return false;
00175         }
00176         return false;
00177       }
00178       break;
00179     default:
00180       return false;
00181   }
00182 
00183   switch (_expression)
00184   {
00185     case contains:
00186       if (_RegExp)
00187       {
00188         return (TQRegExp (_match, _CaseSensitive).search(text) >= 0);
00189       }
00190       else
00191       {
00192         return (text.contains (_match, _CaseSensitive) > 0);
00193       }
00194       break;
00195     case contains_not:
00196       if (_RegExp)
00197       {
00198         return (TQRegExp (_match, _CaseSensitive).search(text) < 0);
00199       }
00200       else
00201       {
00202         return (text.contains (_match, _CaseSensitive) == 0);
00203       }
00204       break;
00205     case equals:
00206       if (_RegExp)
00207       {
00208         return (TQRegExp (_match, _CaseSensitive).exactMatch (text));
00209       }
00210       else
00211       {
00212         if (_CaseSensitive)
00213         {
00214           return (text == _match);
00215         }
00216         else
00217         {
00218           return (text.upper() == _match.upper());
00219         }
00220       }
00221       break;
00222     case equals_not:
00223       if (_RegExp)
00224       {
00225         return !(TQRegExp (_match, _CaseSensitive).exactMatch (text));
00226       }
00227       else
00228       {
00229         if (_CaseSensitive)
00230         {
00231           return (text != _match);
00232         }
00233         else
00234         {
00235           return (text.upper() != _match.upper());
00236         }
00237       }
00238       break;
00239     case greater:
00240       if (_CaseSensitive)
00241       {
00242         return (text > _match);
00243       }
00244       else
00245       {
00246         return (text.upper() > _match.upper());
00247       }
00248       break;
00249     case less:
00250       if (_CaseSensitive)
00251       {
00252         return (text < _match);
00253       }
00254       else
00255       {
00256         return (text.upper() < _match.upper());
00257       }
00258       break;
00259     default:
00260       return false;
00261   }
00262 
00263   return false;
00264 }
00265 
00266 bool FilterElem::matches (ShowRecordElem* pElem)
00267 {
00268   bool result = _filter1.matches (pElem);
00269   switch (_secondCondition)
00270   {
00271     case noSecondCondition:
00272       return result;
00273       break;
00274     case condAnd:
00275       if (result)
00276         return _filter2.matches (pElem);
00277       else
00278         return false;
00279       break;
00280     case condOr:
00281       if (result)
00282         return true;
00283       else
00284         return _filter2.matches (pElem);
00285   }
00286   return false;
00287 }
00288 
00289 unsigned int FilterElem::getCounter( )
00290 {
00291   return _counter;
00292 }
00293 
00294 void FilterElem::incCounter( )
00295 {
00296   _counter++;
00297 }
00298 
00299 void FilterElem::setCounter( unsigned int number )
00300 {
00301   _counter = number;
00302 }

Generated on Thu Jul 5 19:36:06 2007 for kshowmail by  doxygen 1.5.0