~ubuntu-branches/ubuntu/quantal/kdepim/quantal

« back to all changes in this revision

Viewing changes to mailcommon/searchpattern.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:51 UTC
  • mto: This revision was merged to the branch mainline in revision 193.
  • Revision ID: package-import@ubuntu.com-20111215141751-bmhdpiwl23wd9w26
Tags: upstream-4.7.90
ImportĀ upstreamĀ versionĀ 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
#include <kglobal.h>
46
46
#include <klocale.h>
47
47
#include <kdebug.h>
48
 
#include <kconfig.h>
49
48
#include <kconfiggroup.h>
50
49
#include <kascii.h>
51
50
 
55
54
#include <akonadi/contact/contactsearchjob.h>
56
55
 
57
56
#include <QRegExp>
58
 
#include <QByteArray>
59
57
#include <QDataStream>
60
58
#include <QXmlStreamWriter>
61
59
 
68
66
  { "contains", "contains-not", "equals", "not-equal", "regexp",
69
67
    "not-regexp", "greater", "less-or-equal", "less", "greater-or-equal",
70
68
    "is-in-addressbook", "is-not-in-addressbook", "is-in-category", "is-not-in-category",
71
 
    "has-attachment", "has-no-attachment"};
 
69
    "has-attachment", "has-no-attachment", "start-with", "not-start-with", "end-with", "not-end-with"};
 
70
 
72
71
static const int numFuncConfigNames = sizeof funcConfigNames / sizeof *funcConfigNames;
73
72
 
74
73
struct _statusNames {
282
281
    case SearchRule::FuncRegExp:
283
282
    case SearchRule::FuncNotRegExp:
284
283
      return Nepomuk::Query::ComparisonTerm::Regexp;
 
284
    case SearchRule::FuncStartWith: //TODO
 
285
    case SearchRule::FuncNotStartWith:
 
286
    case SearchRule::FuncEndWith:
 
287
    case SearchRule::FuncNotEndWith:
285
288
    default:
286
289
      kDebug() << "Unhandled function type: " << function();
287
290
  }
298
301
    case SearchRule::FuncHasNoAttachment:
299
302
    case SearchRule::FuncIsNotInCategory:
300
303
    case SearchRule::FuncIsNotInAddressbook:
 
304
    case SearchRule::FuncNotStartWith:
 
305
    case SearchRule::FuncNotEndWith:
301
306
      negate = true;
302
307
    default:
303
308
      break;
338
343
      // FIXME how to handle the below? full text?
339
344
    case SearchRule::FuncRegExp:
340
345
    case SearchRule::FuncNotRegExp:
 
346
    case SearchRule::FuncStartWith:
 
347
    case SearchRule::FuncNotStartWith:
 
348
    case SearchRule::FuncEndWith:
 
349
    case SearchRule::FuncNotEndWith:
341
350
    default:
342
351
      kDebug() << "Unhandled function type: " << function();
343
352
  }
405
414
  if ( isEmpty() )
406
415
    return false;
407
416
 
 
417
  msg->parse(); // make sure we can access all headers
 
418
 
408
419
  QString msgContents;
409
420
  // Show the value used to compare the rules against in the log.
410
421
  // Overwrite the value for complete messages and all headers!
593
604
      return ( regexp.indexIn( msgContents ) < 0 );
594
605
    }
595
606
 
 
607
  case SearchRule::FuncStartWith:
 
608
    {
 
609
      return msgContents.startsWith( contents() );
 
610
    }
 
611
  case SearchRule::FuncNotStartWith:
 
612
    {
 
613
      return !msgContents.startsWith( contents() );
 
614
    }
 
615
  case SearchRule::FuncEndWith:
 
616
    {
 
617
      return msgContents.endsWith( contents() );
 
618
    }
 
619
  case SearchRule::FuncNotEndWith:
 
620
    {
 
621
      return !msgContents.endsWith( contents() );
 
622
    }
 
623
    
596
624
  case FuncIsGreater:
597
625
      return ( QString::compare( msgContents.toLower(), contents().toLower() ) > 0 );
598
626
 
607
635
 
608
636
  case FuncIsInAddressbook: {
609
637
    const QStringList addressList = KPIMUtils::splitAddressList( msgContents.toLower() );
610
 
    for ( QStringList::ConstIterator it = addressList.constBegin(); ( it != addressList.constEnd() ); ++it ) {
 
638
    QStringList::ConstIterator end( addressList.constEnd() );
 
639
    for ( QStringList::ConstIterator it = addressList.constBegin(); ( it != end ); ++it ) {
611
640
      Akonadi::ContactSearchJob *job = new Akonadi::ContactSearchJob();
612
641
      job->setLimit( 1 );
613
642
      job->setQuery( Akonadi::ContactSearchJob::Email, KPIMUtils::extractEmailAddress( *it ) );
621
650
 
622
651
  case FuncIsNotInAddressbook: {
623
652
    const QStringList addressList = KPIMUtils::splitAddressList( msgContents.toLower() );
624
 
    for ( QStringList::ConstIterator it = addressList.constBegin(); ( it != addressList.constEnd() ); ++it ) {
 
653
    QStringList::ConstIterator end( addressList.constEnd() );
 
654
    
 
655
    for ( QStringList::ConstIterator it = addressList.constBegin(); ( it != end ); ++it ) {
625
656
      Akonadi::ContactSearchJob *job = new Akonadi::ContactSearchJob();
626
657
      job->setLimit( 1 );
627
658
      job->setQuery( Akonadi::ContactSearchJob::Email, KPIMUtils::extractEmailAddress( *it ) );
637
668
    QString category = contents();
638
669
    const QStringList addressList =  KPIMUtils::splitAddressList( msgContents.toLower() );
639
670
 
640
 
    for ( QStringList::ConstIterator it = addressList.constBegin(); it != addressList.constEnd(); ++it ) {
 
671
    QStringList::ConstIterator end( addressList.constEnd() );
 
672
    for ( QStringList::ConstIterator it = addressList.constBegin(); it != end; ++it ) {
641
673
      Akonadi::ContactSearchJob *job = new Akonadi::ContactSearchJob();
642
674
      job->setQuery( Akonadi::ContactSearchJob::Email, KPIMUtils::extractEmailAddress( *it ) );
643
675
      job->exec();
656
688
    QString category = contents();
657
689
    const QStringList addressList =  KPIMUtils::splitAddressList( msgContents.toLower() );
658
690
 
659
 
    for ( QStringList::ConstIterator it = addressList.constBegin(); it != addressList.constEnd(); ++it ) {
 
691
    QStringList::ConstIterator end( addressList.constEnd() );
 
692
    for ( QStringList::ConstIterator it = addressList.constBegin(); it != end; ++it ) {
660
693
      Akonadi::ContactSearchJob *job = new Akonadi::ContactSearchJob();
661
694
      job->setQuery( Akonadi::ContactSearchJob::Email, KPIMUtils::extractEmailAddress( *it ) );
662
695
      job->exec();
767
800
 
768
801
  case SearchRule::FuncContainsNot:
769
802
    return ( !msgContents.contains( contents(), Qt::CaseInsensitive ) );
770
 
 
 
803
    
771
804
  case SearchRule::FuncRegExp:
772
805
    {
773
806
      QRegExp regexp( contents(), Qt::CaseInsensitive );
977
1010
    return false;
978
1011
 
979
1012
  QList<SearchRule::Ptr>::const_iterator it;
 
1013
  QList<SearchRule::Ptr>::const_iterator end( constEnd() );
980
1014
  switch ( mOperator ) {
981
1015
  case OpAnd: // all rules must match
982
 
    for ( it = begin() ; it != end() ; ++it )
 
1016
    for ( it = constBegin() ; it != end ; ++it )
983
1017
      if ( !((*it)->requiresBody() && ignoreBody) )
984
1018
        if ( !(*it)->matches( item ) )
985
1019
          return false;
986
1020
    return true;
987
1021
  case OpOr:  // at least one rule must match
988
 
    for ( it = begin() ; it != end() ; ++it )
 
1022
    for ( it = constBegin() ; it != end ; ++it )
989
1023
      if ( !((*it)->requiresBody() && ignoreBody) )
990
1024
        if ( (*it)->matches( item ) )
991
1025
          return true;
997
1031
 
998
1032
bool SearchPattern::requiresBody() const {
999
1033
  QList<SearchRule::Ptr>::const_iterator it;
1000
 
    for ( it = begin() ; it != end() ; ++it )
 
1034
    for ( it = constBegin() ; it != constEnd() ; ++it )
1001
1035
      if ( (*it)->requiresBody() )
1002
1036
        return true;
1003
1037
  return false;
1060
1094
  }
1061
1095
  append( rule );
1062
1096
 
1063
 
  if ( sOperator == "or"  ) {
 
1097
  if ( sOperator == QLatin1String( "or" )  ) {
1064
1098
    mOperator = OpOr;
1065
1099
    return;
1066
1100
  }
1067
1101
  // This is the interesting case...
1068
 
  if ( sOperator == "unless" ) { // meaning "and not", ie we need to...
 
1102
  if ( sOperator == QLatin1String( "unless" ) ) { // meaning "and not", ie we need to...
1069
1103
    // ...invert the function (e.g. "equals" <-> "doesn't equal")
1070
1104
    // We simply toggle the last bit (xor with 0x1)... This assumes that
1071
1105
    // SearchRule::Function's come in adjacent pairs of pros and cons
1136
1170
  const Nepomuk::Query::Query::RequestProperty itemIdProperty( Akonadi::ItemSearchJob::akonadiItemIdUri(), false );
1137
1171
 
1138
1172
  Nepomuk::Query::GroupTerm innerGroup = makeGroupTerm( mOperator );
1139
 
  for ( const_iterator it = begin(); it != end(); ++it )
 
1173
  for ( const_iterator it = constBegin(); it != constEnd(); ++it )
1140
1174
    (*it)->addQueryTerms( innerGroup );
1141
1175
 
1142
1176
  if ( innerGroup.subTerms().isEmpty() )
1195
1229
 
1196
1230
  clear(); // ###
1197
1231
  QList<SearchRule::Ptr>::const_iterator it;
1198
 
  for ( it = other.begin() ; it != other.end() ; ++it )
 
1232
  QList<SearchRule::Ptr>::const_iterator end(other.constEnd());
 
1233
  for ( it = other.constBegin() ; it != end ; ++it )
1199
1234
    append( SearchRule::createInstance( **it ) ); // deep copy
1200
1235
 
1201
1236
  return *this;