2
Copyright (c) 2009 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 "contactfiltermodel.h"
22
#include <akonadi_next/entitytreemodel.h>
23
#include <kabc/addressee.h>
24
#include <kabc/contactgroup.h>
26
static bool contactMatchesFilter( const KABC::Addressee &contact, const QString &filterString );
27
static bool contactGroupMatchesFilter( const KABC::ContactGroup &group, const QString &filterString );
29
ContactFilterModel::ContactFilterModel( QObject *parent )
30
: QSortFilterProxyModel( parent )
34
void ContactFilterModel::setFilterString( const QString &filter )
40
bool ContactFilterModel::filterAcceptsRow( int row, const QModelIndex &parent ) const
42
if ( mFilter.isEmpty() )
45
const QModelIndex index = sourceModel()->index( row, 0, parent );
47
const Akonadi::Item item = index.data( Akonadi::EntityTreeModel::ItemRole ).value<Akonadi::Item>();
49
if ( item.hasPayload<KABC::Addressee>() ) {
50
const KABC::Addressee contact = item.payload<KABC::Addressee>();
51
return contactMatchesFilter( contact, mFilter );
52
} else if ( item.hasPayload<KABC::ContactGroup>() ) {
53
const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
54
return contactGroupMatchesFilter( group, mFilter );
60
static bool addressMatchesFilter( const KABC::Address &address, const QString &filterString )
62
if ( address.street().contains( filterString, Qt::CaseInsensitive ) )
65
if ( address.locality().contains( filterString, Qt::CaseInsensitive ) )
68
if ( address.region().contains( filterString, Qt::CaseInsensitive ) )
71
if ( address.postalCode().contains( filterString, Qt::CaseInsensitive ) )
74
if ( address.country().contains( filterString, Qt::CaseInsensitive ) )
77
if ( address.label().contains( filterString, Qt::CaseInsensitive ) )
80
if ( address.postOfficeBox().contains( filterString, Qt::CaseInsensitive ) )
86
static bool contactMatchesFilter( const KABC::Addressee &contact, const QString &filterString )
88
if ( contact.assembledName().contains( filterString, Qt::CaseInsensitive ) )
91
if ( contact.formattedName().contains( filterString, Qt::CaseInsensitive ) )
94
if ( contact.nickName().contains( filterString, Qt::CaseInsensitive ) )
97
if ( contact.birthday().toString().contains( filterString, Qt::CaseInsensitive ) )
100
const KABC::Address::List addresses = contact.addresses();
101
int count = addresses.count();
102
for ( int i = 0; i < count; ++i ) {
103
if ( addressMatchesFilter( addresses.at( i ), filterString ) )
107
const KABC::PhoneNumber::List phoneNumbers = contact.phoneNumbers();
108
count = phoneNumbers.count();
109
for ( int i = 0; i < count; ++i ) {
110
if ( phoneNumbers.at( i ).number().contains( filterString, Qt::CaseInsensitive ) )
114
const QStringList emails = contact.emails();
115
count = emails.count();
116
for ( int i = 0; i < count; ++i ) {
117
if ( emails.at( i ).contains( filterString, Qt::CaseInsensitive ) )
121
if ( contact.mailer().contains( filterString, Qt::CaseInsensitive ) )
124
if ( contact.title().contains( filterString, Qt::CaseInsensitive ) )
127
if ( contact.role().contains( filterString, Qt::CaseInsensitive ) )
130
if ( contact.organization().contains( filterString, Qt::CaseInsensitive ) )
133
if ( contact.department().contains( filterString, Qt::CaseInsensitive ) )
136
if ( contact.note().contains( filterString, Qt::CaseInsensitive ) )
139
if ( contact.url().url().contains( filterString, Qt::CaseInsensitive ) )
142
const QStringList customs = contact.customs();
143
count = customs.count();
144
for ( int i = 0; i < count; ++i ) {
145
if ( customs.at( i ).contains( filterString, Qt::CaseInsensitive ) )
152
bool contactGroupMatchesFilter( const KABC::ContactGroup &group, const QString &filterString )
154
const int count = group.dataCount();
155
for ( int i = 0; i < count; ++i ) {
156
if ( group.data( i ).name().contains( filterString, Qt::CaseInsensitive ) )
158
if ( group.data( i ).email().contains( filterString, Qt::CaseInsensitive ) )
165
#include "contactfiltermodel.moc"