• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

mailcommon

  • sources
  • kde-4.12
  • kdepim
  • mailcommon
  • filter
  • filterimporter
filterimporterevolution.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2012, 2013 Montel Laurent <montel@kde.org>
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License, version 2, as
6  published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful, but
9  WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include "filterimporterevolution_p.h"
19 #include "filtermanager.h"
20 #include "mailfilter.h"
21 
22 #include <KDebug>
23 
24 #include <QFile>
25 
26 using namespace MailCommon;
27 
28 FilterImporterEvolution::FilterImporterEvolution( QFile *file )
29  :FilterImporterAbstract()
30 {
31  QDomDocument doc;
32  if ( !loadDomElement( doc, file ) ) {
33  return;
34  }
35 
36  QDomElement filters = doc.documentElement();
37 
38  if ( filters.isNull() ) {
39  kDebug() << "No filters defined";
40  return;
41  }
42  filters = filters.firstChildElement( QLatin1String("ruleset") );
43  for ( QDomElement e = filters.firstChildElement(); !e.isNull(); e = e.nextSiblingElement() ) {
44  const QString tag = e.tagName();
45  if ( tag == QLatin1String( "rule" ) ) {
46  parseFilters(e);
47  } else {
48  kDebug() << " unknown tag " << tag;
49  }
50  }
51 }
52 
53 FilterImporterEvolution::~FilterImporterEvolution()
54 {
55 }
56 
57 QString FilterImporterEvolution::defaultFiltersSettingsPath()
58 {
59  return QString::fromLatin1( "%1/.config/evolution/mail/filters.xml" ).arg( QDir::homePath() );
60 }
61 
62 void FilterImporterEvolution::parsePartAction( const QDomElement &ruleFilter,
63  MailCommon::MailFilter *filter,
64  parseType type )
65 {
66  for ( QDomElement partFilter = ruleFilter.firstChildElement();
67  !partFilter.isNull();
68  partFilter = partFilter.nextSiblingElement() ) {
69  const QString nexttag = partFilter.tagName();
70  if ( nexttag == QLatin1String( "part" ) ) {
71  if ( partFilter.hasAttribute( QLatin1String("name") ) ) {
72  const QString name = partFilter.attribute( QLatin1String("name") );
73  kDebug() << " parsePartAction name attribute :" << name;
74  if ( type == FilterImporterEvolution::PartType ) {
75  QByteArray fieldName;
76 
77  if ( name == QLatin1String( "to" ) ) {
78  fieldName = "to";
79  } else if ( name == QLatin1String( "sender" ) ) {
80  fieldName = "from";
81  } else if ( name == QLatin1String( "cc" ) ) {
82  fieldName = "cc";
83  } else if ( name == QLatin1String( "bcc" ) ) {
84  fieldName = "bcc"; //Verify
85  //TODO
86  } else if ( name == QLatin1String( "senderto" ) ) {
87  //TODO
88  } else if ( name == QLatin1String( "subject" ) ) {
89  fieldName = "subject";
90  } else if ( name == QLatin1String( "header" ) ) {
91  fieldName = "<any header>";
92  } else if ( name == QLatin1String( "body" ) ) {
93  fieldName = "<body>";
94  } else if ( name == QLatin1String( "sexp" ) ) {
95  //TODO
96  } else if ( name == QLatin1String( "sent-date" ) ) {
97  //TODO
98  } else if ( name == QLatin1String( "recv-date" ) ) {
99  fieldName = "<date>";
100  } else if ( name == QLatin1String( "label" ) ) {
101  //TODO
102  } else if ( name == QLatin1String( "score" ) ) {
103  //TODO
104  } else if ( name == QLatin1String( "size" ) ) {
105  fieldName = "<size>";
106  } else if ( name == QLatin1String( "status" ) ) {
107  fieldName = "<status>";
108  } else if ( name == QLatin1String( "follow-up" ) ) {
109  //TODO
110  } else if ( name == QLatin1String( "completed-on" ) ) {
111  //TODO
112  } else if ( name == QLatin1String( "attachments" ) ) {
113  //TODO
114  } else if ( name == QLatin1String( "mlist" ) ) {
115  fieldName = "list-id"; //Verify
116  } else if ( name == QLatin1String( "regex" ) ) {
117  //TODO
118  } else if ( name == QLatin1String( "source" ) ) {
119  //TODO
120  } else if ( name == QLatin1String( "pipe" ) ) {
121  //TODO
122  } else if ( name == QLatin1String( "junk" ) ) {
123  //TODO
124  } else if ( name == QLatin1String( "all" ) ) {
125  filter->pattern()->setOp( SearchPattern::OpAll );
126  break;
127  } else {
128  kDebug() << " parttype part : name : not implemented :" << name;
129  }
130  if (fieldName.isEmpty()) {
131  kDebug()<<" parttype part : name : not implemented :" << name;
132  continue;
133  }
134  QString contents;
135  SearchRule::Function functionName = SearchRule::FuncNone;
136 
137  for ( QDomElement valueFilter = partFilter.firstChildElement();
138  !valueFilter.isNull();
139  valueFilter = valueFilter.nextSiblingElement() ) {
140  const QString valueTag = valueFilter.tagName();
141 
142  if ( valueTag == QLatin1String( "value" ) ) {
143 
144  if ( valueFilter.hasAttribute( QLatin1String("name") ) ) {
145  const QString name = valueFilter.attribute( QLatin1String("name") );
146  if (name==QLatin1String("flag")) {
147 
148  const QString flag = valueFilter.attribute( QLatin1String("value") );
149  kDebug()<<" flag :"<<flag;
150  if (flag==QLatin1String("Seen")) {
151  contents = QLatin1String("Read");
152  } else if (flag==QLatin1String("Answered")) {
153  contents = QLatin1String("Sent");
154  } else if (flag==QLatin1String("Draft")) {
155  //FIXME
156  } else if (flag==QLatin1String("Flagged")) { //Important
157  contents = QLatin1String("Important");
158  } else if (flag==QLatin1String("Junk")) {
159  contents = QLatin1String("Spam");
160  } else {
161  kDebug()<<" unknown status flags "<<flag;
162  }
163  }
164  kDebug() << " value filter name :" << name;
165  }
166  if ( valueFilter.hasAttribute( QLatin1String("type") ) ) {
167  const QString name = valueFilter.attribute( QLatin1String("type") );
168  if ( name == QLatin1String( "option" ) ){
169  //Nothing we will look at value
170  } else if ( name == QLatin1String( "string" ) ) {
171  QDomElement string = valueFilter.firstChildElement();
172  contents = string.text();
173  } else if ( name == QLatin1String( "folder" ) ) {
174  QDomElement folder = valueFilter.firstChildElement();
175  if ( folder.hasAttribute( QLatin1String("uri") ) ) {
176  contents = folder.attribute( QLatin1String("uri") );
177  if ( !contents.isEmpty() ) {
178  contents.remove( QLatin1String( "folder://" ) );
179  }
180  }
181  } else if ( name == QLatin1String( "address" ) ) {
182  QDomElement address = valueFilter.firstChildElement();
183  contents = address.text();
184  } else if ( name == QLatin1String( "integer" ) ) {
185  if ( valueFilter.hasAttribute( QLatin1String("integer") ) ) {
186  contents = valueFilter.attribute( QLatin1String("integer") );
187  int val = contents.toInt();
188  val = val * 1024; //store in Ko
189  contents = QString::number(val);
190  }
191  } else {
192  kDebug() << " type not implemented " << name;
193  }
194 
195  }
196  if ( valueFilter.hasAttribute( QLatin1String("value") ) ) {
197  const QString value = valueFilter.attribute( QLatin1String("value") );
198  kDebug() << " value filter value :" << name;
199  if ( value == QLatin1String( "contains" ) ) {
200  functionName = SearchRule::FuncContains;
201  } else if ( value == QLatin1String( "not contains" ) ) {
202  functionName = SearchRule::FuncContainsNot;
203  } else if ( value == QLatin1String( "is not" ) ) {
204  functionName = SearchRule::FuncNotEqual;
205  } else if ( value == QLatin1String( "is" ) ) {
206  functionName = SearchRule::FuncEquals;
207  } else if ( value == QLatin1String( "exist" ) ) {
208  //TODO
209  } else if ( value == QLatin1String( "not exist" ) ) {
210  //TODO
211  } else if ( value == QLatin1String( "not starts with" ) ) {
212  functionName = SearchRule::FuncNotStartWith;
213  } else if ( value == QLatin1String( "ends with" ) ) {
214  functionName = SearchRule::FuncEndWith;
215  } else if ( value == QLatin1String( "not ends with" ) ) {
216  functionName = SearchRule::FuncNotEndWith;
217  } else if ( value == QLatin1String( "matches soundex" ) ) {
218  //TODO
219  } else if ( value == QLatin1String( "not match soundex" ) ) {
220  //TODO
221  } else if ( value == QLatin1String( "before" ) ) {
222  //TODO
223  } else if ( value == QLatin1String( "after" ) ) {
224  //TODO
225  } else if ( value == QLatin1String( "greater-than" ) ) {
226  functionName = SearchRule::FuncIsGreater;
227  } else if ( value == QLatin1String( "less-than" ) ) {
228  functionName = SearchRule::FuncIsLess;
229  } else if ( value == QLatin1String( "starts with" ) ) {
230  functionName = SearchRule::FuncStartWith;
231  }
232  }
233  }
234  }
235  SearchRule::Ptr rule = SearchRule::createInstance( fieldName, functionName, contents );
236  filter->pattern()->append( rule );
237 
238  } else if ( type == FilterImporterEvolution::ActionType ) {
239  QString actionName;
240  if ( name == QLatin1String( "stop" ) ) {
241  filter->setStopProcessingHere(true);
242  break;
243  } else if ( name == QLatin1String( "move-to-folder" ) ) {
244  actionName = QLatin1String( "transfer" );
245  } else if ( name == QLatin1String( "copy-to-folder" ) ) {
246  actionName = QLatin1String( "copy" );
247  } else if ( name == QLatin1String( "delete" ) ) {
248  actionName = QLatin1String( "delete" );
249  } else if ( name == QLatin1String( "label" ) ) {
250  //TODO
251  } else if ( name == QLatin1String( "colour" ) ) {
252  //TODO
253  } else if ( name == QLatin1String( "score" ) ) {
254  //TODO
255  } else if ( name == QLatin1String( "adj-score" ) ) {
256  //TODO
257  } else if ( name == QLatin1String( "set-status" ) ) {
258  actionName = QLatin1String( "set status" );
259  } else if ( name == QLatin1String( "unset-status" ) ) {
260  actionName = QLatin1String( "unset status" );
261  } else if ( name == QLatin1String( "beep" ) ) {
262  actionName = QLatin1String( "beep" );
263  } else if ( name == QLatin1String( "play-sound" ) ) {
264  actionName = QLatin1String( "play sound" );
265  } else if ( name == QLatin1String( "shell" ) ) {
266  actionName = QLatin1String( "execute" );
267  } else if ( name == QLatin1String( "pipe" ) ) {
268  actionName = QLatin1String( "filter app" );
269  } else if ( name == QLatin1String( "forward" ) ) {
270  actionName = QLatin1String( "forward" );
271  }
272  if ( actionName.isEmpty() ){
273  kDebug() << " actiontype part : name : not implemented :" << name;
274  }
275  QString value;
276  for ( QDomElement valueFilter = partFilter.firstChildElement();
277  !valueFilter.isNull();
278  valueFilter = valueFilter.nextSiblingElement() ) {
279  const QString valueTag = valueFilter.tagName();
280  if ( valueTag == QLatin1String( "value" ) ) {
281  if ( valueFilter.hasAttribute( QLatin1String("name") ) ) {
282  const QString name = valueFilter.attribute( QLatin1String("name") );
283  kDebug() << " value filter name :" << name;
284  }
285  if ( valueFilter.hasAttribute( QLatin1String("type") ) ) {
286  const QString name = valueFilter.attribute( QLatin1String("type" ));
287  kDebug() << " value filter type :" << name;
288  if ( name == QLatin1String( "option" ) ){
289  //Nothing we will look at value
290  } else if ( name == QLatin1String( "string" ) ) {
291  //TODO
292  } else if ( name == QLatin1String( "folder" ) ) {
293  QDomElement folder = valueFilter.firstChildElement();
294 
295  if ( folder.hasAttribute( QLatin1String("uri") ) ) {
296  value = folder.attribute( QLatin1String("uri") );
297  if ( !value.isEmpty() ) {
298  value.remove( QLatin1String( "folder://" ) );
299  }
300  kDebug() << " contents folder :" << value;
301  }
302  } else if ( name == QLatin1String( "address" ) ) {
303  //TODO
304  }
305 
306  }
307  if ( valueFilter.hasAttribute( QLatin1String("value") ) ) {
308  const QString name = valueFilter.attribute( QLatin1String("value") );
309  kDebug() << " value filter value :" << name;
310  if ( value == QLatin1String( "contains" ) ) {
311  //TODO
312  }
313  }
314  }
315  }
316  createFilterAction( filter, actionName, value );
317  }
318  }
319  }
320  }
321 }
322 
323 void FilterImporterEvolution::parseFilters( const QDomElement &e )
324 {
325  MailCommon::MailFilter *filter = new MailCommon::MailFilter();
326  if ( e.hasAttribute( QLatin1String("enabled") ) ) {
327  const QString attr = e.attribute( QLatin1String("enabled") );
328  if ( attr == QLatin1String( "false" ) ) {
329  filter->setEnabled( false );
330  }
331  }
332 
333  if ( e.hasAttribute( QLatin1String("grouping") ) ) {
334  const QString attr = e.attribute( QLatin1String("grouping") );
335  if ( attr == QLatin1String( "all" ) ) {
336  filter->pattern()->setOp( SearchPattern::OpAnd );
337  } else if ( attr == QLatin1String( "any" ) ) {
338  filter->pattern()->setOp( SearchPattern::OpOr );
339  } else {
340  kDebug() << " grouping not implemented: " << attr;
341  }
342 
343  }
344 
345  if ( e.hasAttribute( QLatin1String("source") ) ) {
346  const QString attr = e.attribute( QLatin1String("source") );
347  if ( attr == QLatin1String( "incoming" ) ) {
348  filter->setApplyOnInbound( true );
349  } else if ( attr == QLatin1String( "outgoing" ) ) {
350  filter->setApplyOnInbound( false );
351  filter->setApplyOnOutbound( true );
352  } else {
353  kDebug() << " source not implemented :" << attr;
354  }
355  }
356  for ( QDomElement ruleFilter = e.firstChildElement();
357  !ruleFilter.isNull();
358  ruleFilter = ruleFilter.nextSiblingElement() )
359  {
360  const QString nexttag = ruleFilter.tagName();
361  if ( nexttag == QLatin1String( "title" ) ) {
362  filter->pattern()->setName( ruleFilter.text() );
363  filter->setToolbarName( ruleFilter.text() );
364  } else if ( nexttag == QLatin1String( "partset" ) ) {
365  parsePartAction ( ruleFilter, filter, PartType );
366  } else if ( nexttag == QLatin1String( "actionset" ) ) {
367  parsePartAction( ruleFilter, filter, ActionType );
368  } else {
369  kDebug() << " tag not implemented : " << nexttag;
370  }
371  }
372 
373  appendFilter(filter);
374 }
MailCommon::SearchRule::FuncEndWith
Definition: searchpattern.h:100
MailCommon::SearchPattern::OpAll
Definition: searchpattern.h:608
MailCommon::SearchRule::FuncNotEndWith
Definition: searchpattern.h:101
MailCommon::FilterImporterEvolution::~FilterImporterEvolution
~FilterImporterEvolution()
Definition: filterimporterevolution.cpp:53
MailCommon::SearchPattern::OpOr
Definition: searchpattern.h:607
MailCommon::SearchRule::FuncContainsNot
Definition: searchpattern.h:83
MailCommon::SearchRule::FuncNotStartWith
Definition: searchpattern.h:99
MailCommon::MailFilter::setApplyOnOutbound
void setApplyOnOutbound(bool aApply=true)
Set whether this filter should be applied on outbound messages (aApply == true) or not...
Definition: mailfilter.cpp:192
mailfilter.h
MailCommon::SearchRule::Ptr
boost::shared_ptr< SearchRule > Ptr
Defines a pointer to a search rule.
Definition: searchpattern.h:69
MailCommon::SearchRule::FuncStartWith
Definition: searchpattern.h:98
MailCommon::MailFilter::setStopProcessingHere
void setStopProcessingHere(bool aStop)
Definition: mailfilter.cpp:307
MailCommon::SearchRule::Function
Function
Describes operators for comparison of field and contents.
Definition: searchpattern.h:80
MailCommon::SearchRule::FuncNotEqual
Definition: searchpattern.h:85
MailCommon::MailFilter::setToolbarName
void setToolbarName(const QString &toolbarName)
This sets the toolbar name for this filter.
Definition: mailfilter.cpp:338
MailCommon::FilterImporterAbstract
Definition: filterimporterabstract_p.h:33
MailCommon::MailFilter::setEnabled
void setEnabled(bool)
Definition: mailfilter.cpp:739
MailCommon::SearchRule::FuncNone
Definition: searchpattern.h:81
MailCommon::SearchRule::FuncContains
Definition: searchpattern.h:82
MailCommon::SearchPattern::OpAnd
Definition: searchpattern.h:606
MailCommon::SearchRule::FuncIsGreater
Definition: searchpattern.h:88
MailCommon::FilterImporterAbstract::appendFilter
void appendFilter(MailCommon::MailFilter *filter)
Definition: filterimporterabstract.cpp:47
MailCommon::SearchPattern::setOp
void setOp(SearchPattern::Operator aOp)
Sets the filter operator.
Definition: searchpattern.h:713
MailCommon::FilterImporterAbstract::loadDomElement
bool loadDomElement(QDomDocument &doc, QFile *file)
Definition: filterimporterabstract.cpp:90
filterimporterevolution_p.h
MailCommon::FilterImporterEvolution::defaultFiltersSettingsPath
static QString defaultFiltersSettingsPath()
Definition: filterimporterevolution.cpp:57
MailCommon::MailFilter::setApplyOnInbound
void setApplyOnInbound(bool aApply=true)
Set whether this filter should be applied on inbound messages (aApply == true) or not...
Definition: mailfilter.cpp:212
MailCommon::SearchRule::createInstance
static SearchRule::Ptr createInstance(const QByteArray &field=0, Function function=FuncContains, const QString &contents=QString())
Creates a new search rule of a certain type by instantiating the appropriate subclass depending on th...
Definition: searchpattern.cpp:140
MailCommon::FilterImporterEvolution::FilterImporterEvolution
FilterImporterEvolution(QFile *file)
Definition: filterimporterevolution.cpp:28
filtermanager.h
MailCommon::MailFilter::pattern
SearchPattern * pattern()
Provides a reference to the internal pattern.
Definition: mailfilter.cpp:182
MailCommon::MailFilter
Definition: mailfilter.h:42
MailCommon::SearchPattern::setName
void setName(const QString &newName)
Sets the name of the search pattern.
Definition: searchpattern.h:697
MailCommon::SearchRule::FuncIsLess
Definition: searchpattern.h:90
MailCommon::FilterImporterAbstract::createFilterAction
void createFilterAction(MailCommon::MailFilter *filter, const QString &actionName, const QString &value)
Definition: filterimporterabstract.cpp:64
MailCommon::SearchRule::FuncEquals
Definition: searchpattern.h:84
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:14 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

mailcommon

Skip menu "mailcommon"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal