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

messagelist

  • sources
  • kde-4.12
  • kdepim
  • messagelist
  • core
filter.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * Copyright 2008 Szymon Tomasz Stefanek <pragma@kvirc.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  *******************************************************************************/
20 
21 #include "core/filter.h"
22 #include "core/messageitem.h"
23 
24 #include <Nepomuk2/Query/AndTerm>
25 #include <Nepomuk2/Query/ComparisonTerm>
26 #include <Nepomuk2/Query/LiteralTerm>
27 #include <Nepomuk2/Query/QueryServiceClient>
28 #include <Nepomuk2/Query/ResourceTerm>
29 #include <Nepomuk2/Vocabulary/NIE>
30 
31 #include <ontologies/nie.h>
32 #include <ontologies/nmo.h>
33 
34 #include <akonadi/itemsearchjob.h>
35 
36 using namespace MessageList::Core;
37 
38 Filter::Filter()
39  : mQueryClient( new Nepomuk2::Query::QueryServiceClient( this ) )
40 {
41  connect( mQueryClient, SIGNAL(newEntries(QList<Nepomuk2::Query::Result>)),
42  this, SLOT(newEntries(QList<Nepomuk2::Query::Result>)) );
43  connect( mQueryClient, SIGNAL(finishedListing()),
44  this, SLOT(finishedListing()) );
45 }
46 
47 bool Filter::containString(const QString& searchInString) const
48 {
49  bool found = false;
50  Q_FOREACH(const QString& str, mSearchList) {
51  if(searchInString.contains(str,Qt::CaseInsensitive)) {
52  found = true;
53  } else {
54  found = false;
55  break;
56  }
57  }
58  return found;
59 }
60 
61 bool Filter::match( const MessageItem * item ) const
62 {
63  if ( !mStatus.isOfUnknownStatus() )
64  {
65  if ( !(mStatus & item->status()) )
66  return false;
67  }
68 
69  if ( !mSearchString.isEmpty() )
70  {
71  if ( mMatchingItemIds.contains( item->itemId() ) )
72  return true;
73 
74  bool searchMatches = false;
75  if ( containString(item->subject()) )
76  searchMatches = true;
77  else if ( containString(item->sender()) )
78  searchMatches = true;
79  else if ( containString(item->receiver()) )
80  searchMatches = true;
81 
82  if ( !searchMatches )
83  return false;
84  }
85 
86  if ( !mTagId.isEmpty() ) {
87  const bool tagMatches = item->findTag( mTagId ) != 0;
88  if ( !tagMatches )
89  return false;
90  }
91 
92  return true;
93 }
94 
95 bool Filter::isEmpty() const
96 {
97  if ( !mStatus.isOfUnknownStatus() )
98  return false;
99 
100  if ( !mSearchString.isEmpty() )
101  return false;
102 
103  if ( !mTagId.isEmpty() )
104  return false;
105 
106  return true;
107 }
108 
109 void Filter::clear()
110 {
111  mStatus = Akonadi::MessageStatus();
112  mSearchString.clear();
113  mTagId.clear();
114  mMatchingItemIds.clear();
115  mQueryClient->close();
116  mSearchList.clear();
117 }
118 
119 void Filter::setCurrentFolder( const KUrl &url )
120 {
121  mCurrentFolder = url;
122 }
123 
124 void Filter::setSearchString( const QString &search )
125 {
126  mSearchString = search;
127  mSearchList = mSearchString.trimmed().split(QLatin1Char(' '));
128 
129  emit finished(); // let the view update according to restrictions
130 
131  if( mSearchString.isEmpty()) {
132  mQueryClient->close();
133  return;
134  }
135  if (mSearchString.count()<4) {
136  mQueryClient->close();
137  return;
138  }
139  const Nepomuk2::Resource parentResource( mCurrentFolder );
140  if( !parentResource.exists() ) {
141  mQueryClient->close();
142  return;
143  }
144  const Nepomuk2::Query::ComparisonTerm isChildTerm( Nepomuk2::Vocabulary::NIE::isPartOf(), Nepomuk2::Query::ResourceTerm( parentResource ) );
145 
146  const Nepomuk2::Query::ComparisonTerm bodyTerm(
147  Vocabulary::NMO::plainTextMessageContent(),
148  Nepomuk2::Query::LiteralTerm( QString::fromLatin1( "\'%1*\'" ).arg( mSearchString ) ),
149  Nepomuk2::Query::ComparisonTerm::Contains );
150 
151  const Nepomuk2::Query::AndTerm andTerm( isChildTerm, bodyTerm );
152 
153  Nepomuk2::Query::Query query( andTerm );
154  query.setRequestProperties( QList<Nepomuk2::Query::Query::RequestProperty>() << Nepomuk2::Types::Property( Akonadi::ItemSearchJob::akonadiItemIdUri() ) );
155 
156  mMatchingItemIds.clear();
157  mQueryClient->close();
158  bool ok = mQueryClient->query( query );
159  if (!ok) {
160  kDebug() << "Cannot start query:" << mQueryClient->errorMessage();
161  }
162 }
163 
164 void Filter::newEntries( const QList<Nepomuk2::Query::Result> &entries )
165 {
166  Q_FOREACH( const Nepomuk2::Query::Result &result, entries ) {
167  const Soprano::Node &property = result.requestProperty( Akonadi::ItemSearchJob::akonadiItemIdUri() );
168  if ( !(property.isValid() && property.isLiteral() && property.literal().isString()) ) {
169  continue;
170  } else {
171  mMatchingItemIds.insert( property.literal().toString().toLongLong() );
172  }
173  }
174 }
175 
176 void Filter::finishedListing()
177 {
178  emit finished(); // let the view update according to restrictions _and_ matching full-text search results
179 }
180 
181 #include "filter.moc"
MessageList::Core::MessageItem
Definition: messageitem.h:50
filter.h
MessageList::Core::Filter::finished
void finished()
MessageList::Core::Filter::setCurrentFolder
void setCurrentFolder(const KUrl &url)
Sets the current folder of this filter.
Definition: filter.cpp:119
MessageList::Core::Item::itemId
qint64 itemId() const
Definition: item.cpp:500
MessageList::Core::Item::receiver
const QString & receiver() const
Returns the receiver associated to this item.
Definition: item.cpp:452
MessageList::Core::Item::subject
const QString & subject() const
Returns the subject associated to this Item.
Definition: item.cpp:472
messageitem.h
MessageList::Core::Filter::Filter
Filter()
Definition: filter.cpp:38
MessageList::Core::MessageItem::findTag
const Tag * findTag(const QString &szTagId) const
Returns Tag associated to this message that has the specified id or 0 if no such tag exists...
Definition: messageitem.cpp:331
MessageList::Core::Filter::isEmpty
bool isEmpty() const
Returns true if this filter is empty (0 status mask, empty search string and empty tag) and it's usel...
Definition: filter.cpp:95
MessageList::Core::Filter::clear
void clear()
Clears this filter (sets status to 0, search string and tag id to empty strings)
Definition: filter.cpp:109
MessageList::Core::Filter::match
bool match(const MessageItem *item) const
Returns true if the specified parameters match this filter and false otherwise.
Definition: filter.cpp:61
MessageList::Core::Item::sender
const QString & sender() const
Returns the sender associated to this item.
Definition: item.cpp:442
MessageList::Core::Item::status
const Akonadi::MessageStatus & status() const
Returns the status associated to this Item.
Definition: item.cpp:402
MessageList::Core::Filter::setSearchString
void setSearchString(const QString &search)
Sets the search string for this filter.
Definition: filter.cpp:124
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:32 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

messagelist

Skip menu "messagelist"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

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