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
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
35 EmailsFilterProxyModel::EmailsFilterProxyModel( QObject
*parent
)
36 : QSortFilterProxyModel( parent
), d( new Private
)
38 setSortLocaleAware( true );
39 setDynamicSortFilter( true );
42 EmailsFilterProxyModel::~EmailsFilterProxyModel()
47 void EmailsFilterProxyModel::setFilterString( const QString
&filter
)
53 bool EmailsFilterProxyModel::filterAcceptsRow( int row
, const QModelIndex
&parent
) const
55 if ( d
->mFilter
.isEmpty() )
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
);
70 static bool emailMatchesFilter( const KMime::Message::Ptr
&message
, const QString
&filterString
)
72 if ( message
->subject()->asUnicodeString().contains( filterString
, Qt::CaseInsensitive
) )
75 foreach ( const KMime::Types::Mailbox
&mailbox
, message
->from()->mailboxes() ) {
76 if ( mailbox
.hasName() ) {
77 if ( mailbox
.name().contains( filterString
, Qt::CaseInsensitive
) )
80 if ( mailbox
.addrSpec().asPrettyString().contains( filterString
, Qt::CaseInsensitive
) )
88 #include "emailsfilterproxymodel.moc"