doc fixes found while translating
[kdepim.git] / mobile / mail / emailsfilterproxymodel.cpp
blobbd9ccd9c98d7a80a1f64ffb435bfa8986b010148
1 /*
2 Copyright (c) 2010 Tobias Koenig <tokoe@kde.org>
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
20 #include "emailsfilterproxymodel.h"
22 #include <akonadi/entitytreemodel.h>
23 #include <KMime/Message>
25 static bool emailMatchesFilter( const KMime::Message::Ptr &message, const QString &filterString );
27 using namespace Akonadi;
29 class EmailsFilterProxyModel::Private
31 public:
32 QString mFilter;
35 EmailsFilterProxyModel::EmailsFilterProxyModel( QObject *parent )
36 : QSortFilterProxyModel( parent ), d( new Private )
38 setSortLocaleAware( true );
39 setDynamicSortFilter( true );
42 EmailsFilterProxyModel::~EmailsFilterProxyModel()
44 delete d;
47 void EmailsFilterProxyModel::setFilterString( const QString &filter )
49 d->mFilter = filter;
50 invalidateFilter();
53 bool EmailsFilterProxyModel::filterAcceptsRow( int row, const QModelIndex &parent ) const
55 if ( d->mFilter.isEmpty() )
56 return true;
58 const QModelIndex index = sourceModel()->index( row, 0, parent );
60 const Akonadi::Item item = index.data( Akonadi::EntityTreeModel::ItemRole ).value<Akonadi::Item>();
62 if ( item.hasPayload<KMime::Message::Ptr>() ) {
63 const KMime::Message::Ptr message = item.payload<KMime::Message::Ptr>();
64 return emailMatchesFilter( message, d->mFilter );
67 return true;
70 static bool emailMatchesFilter( const KMime::Message::Ptr &message, const QString &filterString )
72 if ( message->subject()->asUnicodeString().contains( filterString, Qt::CaseInsensitive ) )
73 return true;
75 foreach ( const KMime::Types::Mailbox &mailbox, message->from()->mailboxes() ) {
76 if ( mailbox.hasName() ) {
77 if ( mailbox.name().contains( filterString, Qt::CaseInsensitive ) )
78 return true;
79 } else {
80 if ( mailbox.addrSpec().asPrettyString().contains( filterString, Qt::CaseInsensitive ) )
81 return true;
85 return false;
88 #include "emailsfilterproxymodel.moc"