summaryrefslogtreecommitdiffstats
path: root/kshowmail/filteritem.cpp
blob: 70a29544bcdad3fc06f69cc2f4778ecab5de4fee (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
//
// C++ Implementation: filteritem
//
// Description:
//
//
// Author: Ulrich Weigelt <ulrich.weigelt@gmx.de>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "filteritem.h"

FilterItem::FilterItem( uint filterNr )
{
  //get the application config object
  config = TDEApplication::kApplication()->config();

  //save number
  filterNumber = filterNr;

  //set config group
  config->setGroup( TQString( "%1%2" ).arg( CONFIG_GROUP_FILTER ).arg( filterNr ) );

  //get name
  name = config->readEntry( CONFIG_ENTRY_FILTER_NAME );

  //get number of criterias
  numberCriterias = config->readNumEntry( CONFIG_ENTRY_FILTER_CRITERIA_NUMBER );

  //get criteria linkage
  switch( config->readNumEntry( CONFIG_ENTRY_FILTER_CRITERIA_LINKAGE, DEFAULT_FILTER_CRITERIA_LINKAGE ) )
  {
    case CONFIG_VALUE_FILTER_CRITERIA_LINKAGE_MATCH_ALL : linkage = LinkAll; break;
    case CONFIG_VALUE_FILTER_CRITERIA_LINKAGE_MATCH_ANY : linkage = LinkAny; break;
    default                                             : kdError() << "FilterItem::FilterItem(): Unknown linkage value read. Set ALL. ( Filter " << filterNumber << ")" << endl;
                                                          linkage = LinkAll;
                                                          break;
  }

  //get action
  switch( config->readNumEntry( CONFIG_ENTRY_FILTER_ACTION, DEFAULT_FILTER_ACTION ) )
  {
    case CONFIG_VALUE_FILTER_ACTION_PASS      : action = FActPass; break;
    case CONFIG_VALUE_FILTER_ACTION_DELETE    : action = FActDelete; break;
    case CONFIG_VALUE_FILTER_ACTION_MARK      : action = FActMark; break;
    case CONFIG_VALUE_FILTER_ACTION_MOVE      : action = FActMove; break;
    case CONFIG_VALUE_FILTER_ACTION_IGNORE    : action = FActIgnore; break;
    case CONFIG_VALUE_FILTER_ACTION_SPAMCHECK : action = FActSpamcheck; break;
    default                                   : kdError() << "FilterItem::FilterItem(): Unknown filter action read. Set PASS. (Filter " << filterNumber << ")" << endl;
                                                action = FActPass;
                                                break;
  }

  //get mailbox name if filter action is move
  if( action == FActMove )
  {
    mailbox = config->readEntry( CONFIG_ENTRY_FILTER_MOVE_MAILBOX );
    if( mailbox.isNull() || mailbox.isEmpty() )
    {
      kdWarning() << "Filter " << filterNumber << ": No mailbox name found. Set default: " << DEFAULT_FILTER_ACTION_MOVE_MAILBOX << endl;
      mailbox = TQString( DEFAULT_FILTER_ACTION_MOVE_MAILBOX );
    }
  }

  //now we get the criterias
  criterias.setAutoDelete( true ); //the list shall delete all criterias if it will be deleted itself
  for( uint critNr = 1; critNr <= numberCriterias; critNr++ )
  {
    criterias.append( new FilterItemCriteria( filterNr, critNr ) ); //a new created criteria loads its settings itself
  }
}


FilterItem::~FilterItem()
{
}

FilterAction_Type FilterItem::check( TQString from, TQString to, uint size, TQString subject, TQString header, TQString account, TQString& mailboxName ) const
{
  bool match = false;   //TRUE, if filter matches

  //return NONE if no criterias available
  if( criterias.isEmpty() ) return FActNone;

  //get iterator
  TQPtrListIterator<FilterItemCriteria> it( criterias );

  FilterItemCriteria* crit;

  //check criterias
  if( linkage == LinkAll )
  {
    match = true;
    while( ( crit = it.current() ) != NULL && match )
    {
      ++it;

      match = match && crit->check( from, to, size, subject, header, account );
    }
  }
  else if( linkage == LinkAny )
  {
    match = false;
    while( ( crit = it.current() ) != NULL && !match )
    {
      ++it;

      match = crit->check( from, to, size, subject, header, account );
    }
  }
  else
    kdError() << "FilterItem::check(): Unknown linkage (Filter " << filterNumber << ")" << endl;

  //return action if filter matches
  if( match )
  {
    //set mailbox name if neccessary
    if( action == FActMove )
    {
      mailboxName.remove( 0, mailboxName.length() );
      mailboxName.append( mailbox );
    }

    return action;
  }

  //default return value, filter doesn't match
  return FActNone;
}

void FilterItem::print( ) const
{
  kdDebug() << "Settings of filter " << filterNumber << ":" << endl;
  kdDebug() << "Name: " << name << endl;
  kdDebug() << "Number of criterias: " << numberCriterias << endl;

  switch( linkage )
  {
    case LinkAll  : kdDebug() << "Criteria Linkage: ALL (AND)" << endl; break;
    case LinkAny  : kdDebug() << "Criteria Linkage: ANY (OR)" << endl; break;
    default       : kdDebug() << "Unknown Criteria LInkage" << endl; break;
  }

  switch( action )
  {
    case FActPass       : kdDebug() << "Action: PASS" << endl; break;
    case FActDelete     : kdDebug() << "Action: DELETE" << endl; break;
    case FActMark       : kdDebug() << "Action: MARK" << endl; break;
    case FActMove       : kdDebug() << "Action: MOVE to " << mailbox << endl; break;
    case FActSpamcheck  : kdDebug() << "Action: SPAMCHECK" << endl; break;
    case FActIgnore     : kdDebug() << "Action: IGNORE" << endl; break;
    default             : kdDebug() << "Unknown Action" << endl; break;
  }

  kdDebug() << "Criterias:" << endl;
  TQPtrListIterator<FilterItemCriteria> it( criterias );
  FilterItemCriteria* crit;
  while( ( crit = it.current() ) != NULL )
  {
    ++it;
    crit->print();
  }
}