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

mailcommon

  • sources
  • kde-4.14
  • kdepim
  • mailcommon
  • search
  • searchrule
searchrulenumerical.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2015 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 
19 #include "searchrulenumerical.h"
20 
21 #include "filterlog.h"
22 using MailCommon::FilterLog;
23 
24 #include <QDateTime>
25 #include <KMime/KMimeMessage>
26 
27 
28 //note: lowercase include for compatibility
29 #include <kascii.h>
30 #include <KGlobal>
31 
32 #include <QDataStream>
33 #include <QRegExp>
34 #include <QXmlStreamWriter>
35 
36 #include <algorithm>
37 
38 
39 using namespace MailCommon;
40 
41 SearchRuleNumerical::SearchRuleNumerical( const QByteArray &field,
42  Function func,
43  const QString &contents )
44  : SearchRule( field, func, contents )
45 {
46 }
47 
48 bool SearchRuleNumerical::isEmpty() const
49 {
50  bool ok = false;
51  contents().toLongLong( &ok );
52 
53  return !ok;
54 }
55 
56 bool SearchRuleNumerical::matches( const Akonadi::Item &item ) const
57 {
58  const KMime::Message::Ptr msg = item.payload<KMime::Message::Ptr>();
59 
60  QString msgContents;
61  qint64 numericalMsgContents = 0;
62  qint64 numericalValue = 0;
63 
64  if ( kasciistricmp( field(), "<size>" ) == 0 ) {
65  numericalMsgContents = item.size();
66  numericalValue = contents().toLongLong();
67  msgContents.setNum( numericalMsgContents );
68  } else if ( kasciistricmp( field(), "<age in days>" ) == 0 ) {
69  QDateTime msgDateTime = msg->date()->dateTime().dateTime();
70  numericalMsgContents = msgDateTime.daysTo( QDateTime::currentDateTime() );
71  numericalValue = contents().toInt();
72  msgContents.setNum( numericalMsgContents );
73  } else {
74  return false;
75  }
76  bool rc = matchesInternal( numericalValue, numericalMsgContents, msgContents );
77  if ( FilterLog::instance()->isLogging() ) {
78  QString msg = ( rc ? "<font color=#00FF00>1 = </font>"
79  : "<font color=#FF0000>0 = </font>" );
80  msg += FilterLog::recode( asString() );
81  msg += " ( <i>" + QString::number( numericalMsgContents ) + "</i> )";
82  FilterLog::instance()->add( msg, FilterLog::RuleResult );
83  }
84  return rc;
85 }
86 
87 SearchRule::RequiredPart SearchRuleNumerical::requiredPart() const
88 {
89  return SearchRule::Envelope;
90 }
91 
92 
93 bool SearchRuleNumerical::matchesInternal( long numericalValue,
94  long numericalMsgContents, const QString & msgContents ) const
95 {
96  switch ( function() ) {
97  case SearchRule::FuncEquals:
98  return ( numericalValue == numericalMsgContents );
99 
100  case SearchRule::FuncNotEqual:
101  return ( numericalValue != numericalMsgContents );
102 
103  case SearchRule::FuncContains:
104  return ( msgContents.contains( contents(), Qt::CaseInsensitive ) );
105 
106  case SearchRule::FuncContainsNot:
107  return ( !msgContents.contains( contents(), Qt::CaseInsensitive ) );
108 
109  case SearchRule::FuncRegExp:
110  {
111  QRegExp regexp( contents(), Qt::CaseInsensitive );
112  return ( regexp.indexIn( msgContents ) >= 0 );
113  }
114 
115  case SearchRule::FuncNotRegExp:
116  {
117  QRegExp regexp( contents(), Qt::CaseInsensitive );
118  return ( regexp.indexIn( msgContents ) < 0 );
119  }
120 
121  case FuncIsGreater:
122  return ( numericalMsgContents > numericalValue );
123 
124  case FuncIsLessOrEqual:
125  return ( numericalMsgContents <= numericalValue );
126 
127  case FuncIsLess:
128  return ( numericalMsgContents < numericalValue );
129 
130  case FuncIsGreaterOrEqual:
131  return ( numericalMsgContents >= numericalValue );
132 
133  case FuncIsInAddressbook: // since email-addresses are not numerical, I settle for false here
134  return false;
135 
136  case FuncIsNotInAddressbook:
137  return false;
138 
139  default:
140  ;
141  }
142 
143  return false;
144 }
145 
146 void SearchRuleNumerical::addQueryTerms( Akonadi::SearchTerm &groupTerm, bool &emptyIsNotAnError ) const
147 {
148  using namespace Akonadi;
149  emptyIsNotAnError = false;
150  if ( kasciistricmp( field(), "<size>" ) == 0 ) {
151  EmailSearchTerm term(EmailSearchTerm::ByteSize, contents().toInt(), akonadiComparator());
152  term.setIsNegated( isNegated() );
153  groupTerm.addSubTerm(term);
154  } else if ( kasciistricmp( field(), "<age in days>" ) == 0 ) {
155  QDate date(QDate::currentDate());
156  date = date.addDays( contents().toInt() );
157  EmailSearchTerm term(EmailSearchTerm::HeaderOnlyDate, date, akonadiComparator());
158  term.setIsNegated( isNegated() );
159  groupTerm.addSubTerm(term);
160  }
161 }
162 
163 QString SearchRuleNumerical::informationAboutNotValidRules() const
164 {
165  //KF5 add i18n
166  return QLatin1String("Content is not a number.");
167 }
MailCommon::SearchRule::FuncIsInAddressbook
Definition: searchrule.h:67
MailCommon::SearchRule::FuncIsGreaterOrEqual
Definition: searchrule.h:66
MailCommon::FilterLog::recode
static QString recode(const QString &plain)
Returns an escaped version of the log which can be used in a HTML document.
Definition: filterlog.cpp:228
QByteArray
MailCommon::SearchRule::FuncContainsNot
Definition: searchrule.h:58
MailCommon::SearchRule::FuncIsNotInAddressbook
Definition: searchrule.h:68
MailCommon::SearchRuleNumerical::requiredPart
virtual RequiredPart requiredPart() const
Returns the required part from the item that is needed for the search to operate. ...
Definition: searchrulenumerical.cpp:87
QDateTime::daysTo
int daysTo(const QDateTime &other) const
MailCommon::SearchRule::akonadiComparator
Akonadi::SearchTerm::Condition akonadiComparator() const
Converts the rule function into the corresponding Akonadi query operator.
Definition: searchrule.cpp:526
QString::size
int size() const
MailCommon::SearchRuleNumerical::isEmpty
virtual bool isEmpty() const
Determines whether the rule is worth considering.
Definition: searchrulenumerical.cpp:48
MailCommon::FilterLog::instance
static FilterLog * instance()
Returns the single global instance of the filter log.
Definition: filterlog.cpp:107
MailCommon::FilterLog
KMail Filter Log Collector.
Definition: filterlog.h:55
filterlog.h
MailCommon::SearchRule::FuncNotRegExp
Definition: searchrule.h:62
QRegExp::indexIn
int indexIn(const QString &str, int offset, CaretMode caretMode) const
QRegExp
QString::number
QString number(int n, int base)
MailCommon::SearchRule::Function
Function
Describes operators for comparison of field and contents.
Definition: searchrule.h:55
MailCommon::SearchRule::FuncNotEqual
Definition: searchrule.h:60
MailCommon::SearchRuleNumerical::informationAboutNotValidRules
virtual QString informationAboutNotValidRules() const
Definition: searchrulenumerical.cpp:163
MailCommon::SearchRule::asString
const QString asString() const
Returns the rule as string for debugging purpose.
Definition: searchrule.cpp:516
QString::toInt
int toInt(bool *ok, int base) const
MailCommon::SearchRule::contents
QString contents() const
Returns the contents of the rule.
Definition: searchrule.cpp:511
MailCommon::SearchRuleNumerical::matches
virtual bool matches(const Akonadi::Item &item) const
Tries to match the rule against the KMime::Message in the given item.
Definition: searchrulenumerical.cpp:56
QDate
MailCommon::SearchRule::RequiredPart
RequiredPart
Definition: searchrule.h:79
QString
MailCommon::SearchRule::field
QByteArray field() const
Returns the message header field name (without the trailing ':').
Definition: searchrule.cpp:501
MailCommon::SearchRule::FuncRegExp
Definition: searchrule.h:61
MailCommon::SearchRule::isNegated
bool isNegated() const
Helper that returns whether the rule has a negated function.
Definition: searchrule.cpp:567
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
MailCommon::SearchRule
This class represents one search pattern rule.
Definition: searchrule.h:38
MailCommon::SearchRule::Envelope
Definition: searchrule.h:80
MailCommon::SearchRule::FuncContains
Definition: searchrule.h:57
MailCommon::SearchRule::FuncIsGreater
Definition: searchrule.h:63
QDateTime::currentDateTime
QDateTime currentDateTime()
QDateTime::date
QDate date() const
MailCommon::SearchRule::FuncIsLessOrEqual
Definition: searchrule.h:64
QLatin1String
MailCommon::SearchRuleNumerical::SearchRuleNumerical
SearchRuleNumerical(const QByteArray &field=QByteArray(), Function function=FuncContains, const QString &contents=QString())
Creates new numerical search rule.
Definition: searchrulenumerical.cpp:41
QDate::currentDate
QDate currentDate()
searchrulenumerical.h
MailCommon::FilterLog::add
void add(const QString &entry, ContentType type)
Adds the given log entry under the given content type to the log.
Definition: filterlog.cpp:164
QDate::addDays
QDate addDays(int ndays) const
MailCommon::SearchRuleNumerical::addQueryTerms
virtual void addQueryTerms(Akonadi::SearchTerm &groupTerm, bool &emptyIsNotAnError) const
Adds query terms to the given term group.
Definition: searchrulenumerical.cpp:146
MailCommon::SearchRuleNumerical::matchesInternal
bool matchesInternal(long numericalValue, long numericalContents, const QString &contents) const
A helper method for the main matches() method.
Definition: searchrulenumerical.cpp:93
QString::toLongLong
qlonglong toLongLong(bool *ok, int base) const
MailCommon::SearchRule::FuncIsLess
Definition: searchrule.h:65
MailCommon::SearchRule::FuncEquals
Definition: searchrule.h:59
QDateTime
MailCommon::FilterLog::RuleResult
Log all rule matching results.
Definition: filterlog.h:76
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:31:41 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
  • pimprint

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