• 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
filterimportersylpheed.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 "filterimportersylpheed_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 FilterImporterSylpheed::FilterImporterSylpheed( QFile *file )
29  :FilterImporterAbstract()
30 {
31  QDomDocument doc;
32  if ( !loadDomElement( doc, file ) ) {
33  return;
34  }
35  QDomElement filters = doc.documentElement();
36 
37  if ( filters.isNull() ) {
38  kDebug() << "No filters defined";
39  return;
40  }
41 
42  for ( QDomElement e = filters.firstChildElement(); !e.isNull(); e = e.nextSiblingElement() ) {
43  const QString tag = e.tagName();
44  if ( tag == QLatin1String( "rule" ) ) {
45  parseFilters(e);
46  } else {
47  kDebug() << " unknown tag " << tag;
48  }
49  }
50 }
51 
52 FilterImporterSylpheed::~FilterImporterSylpheed()
53 {
54 }
55 
56 QString FilterImporterSylpheed::defaultFiltersSettingsPath()
57 {
58  return QString::fromLatin1( "%1/.sylpheed-2.0/filter.xml" ).arg( QDir::homePath() );
59 }
60 
61 void FilterImporterSylpheed::parseConditions( const QDomElement &e, MailCommon::MailFilter *filter )
62 {
63  if ( e.hasAttribute( QLatin1String("bool") ) ) {
64  const QString attr = e.attribute( QLatin1String("bool") );
65  if ( attr == QLatin1String( "and" ) ) {
66  filter->pattern()->setOp( SearchPattern::OpAnd );
67  } else if ( attr == QLatin1String( "or" ) ) {
68  filter->pattern()->setOp( SearchPattern::OpOr );
69  } else {
70  kDebug() << " bool not defined: " << attr;
71  }
72  }
73  for ( QDomElement ruleFilter = e.firstChildElement();
74  !ruleFilter.isNull();
75  ruleFilter = ruleFilter.nextSiblingElement() ) {
76  QString contentsName;
77  QByteArray fieldName;
78  SearchRule::Function functionName = SearchRule::FuncNone;
79 
80  const QString nexttag = ruleFilter.tagName();
81  if ( nexttag == QLatin1String( "match-header" ) ){
82  if ( ruleFilter.hasAttribute( QLatin1String("name") ) ) {
83  const QString attr = ruleFilter.attribute( QLatin1String("name") );
84  if ( attr == QLatin1String( "From" ) ) {
85  fieldName = "from";
86  } else if ( attr == QLatin1String( "Cc" ) ) {
87  fieldName = "cc";
88  } else if ( attr == QLatin1String( "To" ) ) {
89  fieldName = "to";
90  } else if ( attr == QLatin1String( "Reply-To" ) ) {
91  fieldName = "reply-to";
92  } else if ( attr == QLatin1String( "Subject" ) ) {
93  fieldName = "subject";
94  } else if ( attr == QLatin1String( "List-Id" ) ) {
95  fieldName = "list-id";
96  } else if ( attr == QLatin1String( "X-ML-Name" ) ) {
97  fieldName = "x-mailing-list";
98  }
99  if ( fieldName.isEmpty() ) {
100  kDebug()<<" match-header not implemented " << attr;
101  }
102  }
103  contentsName = ruleFilter.text();
104  } else if ( nexttag == QLatin1String( "match-any-header" ) ) {
105  fieldName = "<any header>";
106  contentsName = ruleFilter.text();
107  } else if ( nexttag == QLatin1String( "match-to-or-cc" ) ) {
108  fieldName = "<recipients>";
109  contentsName = ruleFilter.text();
110  } else if ( nexttag == QLatin1String( "match-body-text" ) ) {
111  fieldName = "<body>";
112  contentsName = ruleFilter.text();
113  } else if ( nexttag == QLatin1String( "command-test" ) ) {
114  //TODO
115  //Not implemented in kmail
116  } else if ( nexttag == QLatin1String( "size" ) ) {
117  fieldName = "<size>";
118  contentsName = QString::number(ruleFilter.text().toInt()*1024); //Stored as kb
119  } else if ( nexttag == QLatin1String( "age" ) ) {
120  fieldName = "<age in days>";
121  contentsName = ruleFilter.text();
122  } else if ( nexttag == QLatin1String( "unread" ) ) {
123  fieldName = "<status>";
124  contentsName = QLatin1String( "Unread" );
125  } else if ( nexttag == QLatin1String( "mark" ) ) {
126  //TODO
127  } else if ( nexttag == QLatin1String( "color-label" ) ) {
128  //TODO
129  } else if ( nexttag == QLatin1String( "mime" ) ) {
130  //TODO
131  } else if ( nexttag == QLatin1String( "account-id" ) ) {
132  //TODO
133  } else if ( nexttag == QLatin1String( "target-folder" ) ) {
134  //TODO
135  } else {
136  kDebug() << " tag not recognize " << nexttag;
137  }
138  if (fieldName.isEmpty()) {
139  kDebug()<<" field not implemented "<<nexttag;
140  }
141 
142  if ( ruleFilter.hasAttribute( QLatin1String("type") ) ) {
143  const QString attr = ruleFilter.attribute( QLatin1String("type") );
144  if ( attr == QLatin1String( "not-contain" ) ) {
145  functionName = SearchRule::FuncContainsNot;
146  } else if ( attr == QLatin1String( "contains" ) ) {
147  functionName = SearchRule::FuncContains;
148  } else if ( attr == QLatin1String( "is-not" ) ) {
149  functionName = SearchRule::FuncNotEqual;
150  } else if ( attr == QLatin1String( "is" ) ) {
151  functionName = SearchRule::FuncEquals;
152  } else if ( attr == QLatin1String( "not-regex" ) ) {
153  functionName = SearchRule::FuncNotRegExp;
154  } else if ( attr == QLatin1String( "regex" ) ) {
155  functionName = SearchRule::FuncRegExp;
156  } else if ( attr == QLatin1String( "not-in-addressbook" ) ) {
157  functionName = SearchRule::FuncIsNotInAddressbook;
158  } else if ( attr == QLatin1String( "in-addressbook" ) ) {
159  functionName = SearchRule::FuncIsInAddressbook;
160  } else if ( attr == QLatin1String( "gt" ) ) {
161  functionName = SearchRule::FuncIsGreater;
162  } else if ( attr == QLatin1String( "lt" ) ) {
163  functionName = SearchRule::FuncIsLess;
164  } else {
165  kDebug() << " Attr type not implemented :" << attr;
166  }
167  }
168  SearchRule::Ptr rule = SearchRule::createInstance( fieldName, functionName, contentsName );
169  filter->pattern()->append( rule );
170  }
171 }
172 
173 void FilterImporterSylpheed::parseActions( const QDomElement &e,
174  MailCommon::MailFilter *filter )
175 {
176  for ( QDomElement ruleFilter = e.firstChildElement();
177  !ruleFilter.isNull();
178  ruleFilter = ruleFilter.nextSiblingElement() ) {
179  QString actionName;
180  const QString nexttag = ruleFilter.tagName();
181  QString value = ruleFilter.text();
182  if ( nexttag == QLatin1String( "move" ) ){
183  actionName = QLatin1String( "transfer" );
184  value = ruleFilter.text();
185  } else if ( nexttag == QLatin1String( "copy" ) ) {
186  actionName = QLatin1String( "copy" );
187  value = ruleFilter.text();
188  } else if ( nexttag == QLatin1String( "not-receive" ) ) {
189  //TODO
190  } else if ( nexttag == QLatin1String( "delete" ) ) {
191  actionName = QLatin1String( "delete" );
192  } else if ( nexttag == QLatin1String( "exec" ) ) {
193  actionName = QLatin1String( "execute" );
194  value = ruleFilter.text();
195  } else if ( nexttag == QLatin1String( "exec-async" ) ) {
196  actionName = QLatin1String( "filter app" );
197  value = ruleFilter.text();
198  } else if ( nexttag == QLatin1String( "mark" ) ) {
199  //FIXME add tag ?
200  } else if ( nexttag == QLatin1String( "color-label" ) ) {
201  //TODO
202  } else if ( nexttag == QLatin1String( "mark-as-read" ) ) {
203  actionName = QLatin1String( "set status" );
204  value = QLatin1String( "R" );
205  } else if ( nexttag == QLatin1String( "forward" ) ) {
206  actionName = QLatin1String( "forward" );
207  value = ruleFilter.text();
208  } else if ( nexttag == QLatin1String( "forward-as-attachment" ) ) {
209  //TODO
210  } else if ( nexttag == QLatin1String( "redirect" ) ) {
211  actionName = QLatin1String( "redirect" );
212  value = ruleFilter.text();
213  } else if ( nexttag == QLatin1String( "stop-eval" ) ) {
214  filter->setStopProcessingHere( true );
215  break;
216  }
217 
218  if ( actionName.isEmpty() ) {
219  kDebug() << " tag not recognize " << nexttag;
220  }
221  createFilterAction( filter, actionName, value );
222  }
223 }
224 
225 void FilterImporterSylpheed::parseFilters( const QDomElement &e )
226 {
227  MailCommon::MailFilter *filter = new MailCommon::MailFilter();
228  if ( e.hasAttribute( QLatin1String("enabled") ) ) {
229  const QString attr = e.attribute( QLatin1String("enabled") );
230  if ( attr == QLatin1String( "false" ) ) {
231  filter->setEnabled( false );
232  }
233  }
234 
235  if ( e.hasAttribute( QLatin1String("name") ) ) {
236  const QString attr = e.attribute( QLatin1String("name") );
237  filter->pattern()->setName( attr );
238  filter->setToolbarName( attr );
239  }
240 
241  if ( e.hasAttribute( QLatin1String("timing") ) ) {
242  const QString attr = e.attribute( QLatin1String("timing") );
243  if ( attr == QLatin1String( "any" ) ) {
244  filter->setApplyOnInbound( true );
245  filter->setApplyOnExplicit( true );
246  } else if ( attr == QLatin1String( "receiver" ) ) {
247  filter->setApplyOnInbound( true );
248  } else if ( attr == QLatin1String( "manual" ) ) {
249  filter->setApplyOnInbound( false );
250  filter->setApplyOnExplicit( true );
251  } else {
252  kDebug() << " timing not defined: " << attr;
253  }
254 
255  }
256  for ( QDomElement ruleFilter = e.firstChildElement();
257  !ruleFilter.isNull();
258  ruleFilter = ruleFilter.nextSiblingElement() ) {
259  const QString nexttag = ruleFilter.tagName();
260  if ( nexttag == QLatin1String( "condition-list" ) ){
261  parseConditions( ruleFilter, filter );
262  } else if ( nexttag == QLatin1String( "action-list" ) ) {
263  parseActions( ruleFilter, filter );
264  } else {
265  kDebug() << " next tag not implemented " << nexttag;
266  }
267  }
268 
269  appendFilter(filter);
270 }
MailCommon::SearchRule::FuncIsInAddressbook
Definition: searchpattern.h:92
MailCommon::SearchPattern::OpOr
Definition: searchpattern.h:607
MailCommon::SearchRule::FuncContainsNot
Definition: searchpattern.h:83
MailCommon::SearchRule::FuncIsNotInAddressbook
Definition: searchpattern.h:93
MailCommon::FilterImporterSylpheed::defaultFiltersSettingsPath
static QString defaultFiltersSettingsPath()
Definition: filterimportersylpheed.cpp:56
mailfilter.h
MailCommon::SearchRule::Ptr
boost::shared_ptr< SearchRule > Ptr
Defines a pointer to a search rule.
Definition: searchpattern.h:69
MailCommon::FilterImporterSylpheed::~FilterImporterSylpheed
~FilterImporterSylpheed()
Definition: filterimportersylpheed.cpp:52
filterimportersylpheed_p.h
MailCommon::SearchRule::FuncNotRegExp
Definition: searchpattern.h:87
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::FuncRegExp
Definition: searchpattern.h:86
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
MailCommon::MailFilter::setApplyOnExplicit
void setApplyOnExplicit(bool aApply=true)
Set whether this filter should be applied on explicit (CTRL-J) filtering (aApply == true) or not...
Definition: mailfilter.cpp:222
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
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
MailCommon::FilterImporterSylpheed::FilterImporterSylpheed
FilterImporterSylpheed(QFile *file)
Definition: filterimportersylpheed.cpp:28
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