22 #include "contactsfilterproxymodel.h"
24 #include "contactstreemodel.h"
26 #include <akonadi/entitytreemodel.h>
27 #include <kabc/addressee.h>
28 #include <kabc/contactgroup.h>
30 static bool contactMatchesFilter(
const KABC::Addressee &contact,
const QString &filterString );
31 static bool contactGroupMatchesFilter(
const KABC::ContactGroup &group,
const QString &filterString );
33 using namespace Akonadi;
35 class ContactsFilterProxyModel::Private
40 mExcludeVirtualCollections( false )
44 ContactsFilterProxyModel::FilterFlags flags;
45 bool mExcludeVirtualCollections;
49 : QSortFilterProxyModel( parent ), d( new Private )
52 setSortLocaleAware(
true );
53 setDynamicSortFilter(
true );
67 bool ContactsFilterProxyModel::filterAcceptsRow(
int row,
const QModelIndex &parent )
const
69 const QModelIndex index = sourceModel()->index( row, 0, parent );
70 if ( d->mExcludeVirtualCollections ) {
77 if ( ( d->mFilter.isEmpty() ) && ( !( d->flags & ContactsFilterProxyModel::HasEmail ) ) ) {
83 if ( item.hasPayload<KABC::Addressee>() ) {
84 const KABC::Addressee contact = item.payload<KABC::Addressee>();
85 if ( d->flags & ContactsFilterProxyModel::HasEmail ) {
86 if ( contact.emails().isEmpty() ) {
90 if ( !d->mFilter.isEmpty() ) {
91 return contactMatchesFilter( contact, d->mFilter );
94 if ( !d->mFilter.isEmpty() ) {
95 if ( item.hasPayload<KABC::ContactGroup>() ) {
96 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
97 return contactGroupMatchesFilter( group, d->mFilter );
105 bool ContactsFilterProxyModel::lessThan(
const QModelIndex &leftIndex,
const QModelIndex &rightIndex )
const
109 if ( leftDate.isValid() && rightDate.isValid() ) {
110 if ( leftDate.year() < rightDate.year() ) {
112 }
else if ( leftDate.year() == rightDate.year() ) {
113 if ( leftDate.month() < rightDate.month() ) {
115 }
else if ( leftDate.month() == rightDate.month() ) {
116 return ( leftDate.day() < rightDate.day() );
123 return QSortFilterProxyModel::lessThan( leftIndex, rightIndex );
133 if ( exclude != d->mExcludeVirtualCollections ) {
134 d->mExcludeVirtualCollections = exclude;
139 Qt::ItemFlags ContactsFilterProxyModel::flags(
const QModelIndex& index )
const
141 if ( !index.isValid() ) {
147 return QSortFilterProxyModel::flags( index ) & ~( Qt::ItemIsSelectable );
149 return QSortFilterProxyModel::flags( index );
152 static bool addressMatchesFilter(
const KABC::Address &address,
const QString &filterString )
154 if ( address.street().contains( filterString, Qt::CaseInsensitive ) ) {
158 if ( address.locality().contains( filterString, Qt::CaseInsensitive ) ) {
162 if ( address.region().contains( filterString, Qt::CaseInsensitive ) ) {
166 if ( address.postalCode().contains( filterString, Qt::CaseInsensitive ) ) {
170 if ( address.country().contains( filterString, Qt::CaseInsensitive ) ) {
174 if ( address.label().contains( filterString, Qt::CaseInsensitive ) ) {
178 if ( address.postOfficeBox().contains( filterString, Qt::CaseInsensitive ) ) {
185 static bool contactMatchesFilter(
const KABC::Addressee &contact,
const QString &filterString )
187 if ( contact.assembledName().contains( filterString, Qt::CaseInsensitive ) ) {
191 if ( contact.formattedName().contains( filterString, Qt::CaseInsensitive ) ) {
195 if ( contact.nickName().contains( filterString, Qt::CaseInsensitive ) ) {
199 if ( contact.birthday().toString().contains( filterString, Qt::CaseInsensitive ) ) {
203 const KABC::Address::List addresses = contact.addresses();
204 int count = addresses.count();
205 for (
int i = 0; i < count; ++i ) {
206 if ( addressMatchesFilter( addresses.at( i ), filterString ) ) {
211 const KABC::PhoneNumber::List phoneNumbers = contact.phoneNumbers();
212 count = phoneNumbers.count();
213 for (
int i = 0; i < count; ++i ) {
214 if ( phoneNumbers.at( i ).number().contains( filterString, Qt::CaseInsensitive ) ) {
219 const QStringList emails = contact.emails();
220 count = emails.count();
221 for (
int i = 0; i < count; ++i ) {
222 if ( emails.at( i ).contains( filterString, Qt::CaseInsensitive ) ) {
227 const QStringList categories = contact.categories();
228 count = categories.count();
229 for (
int i = 0; i < count; ++i ) {
230 if ( categories.at( i ).contains( filterString, Qt::CaseInsensitive ) ) {
235 if ( contact.mailer().contains( filterString, Qt::CaseInsensitive ) ) {
239 if ( contact.title().contains( filterString, Qt::CaseInsensitive ) ) {
243 if ( contact.role().contains( filterString, Qt::CaseInsensitive ) ) {
247 if ( contact.organization().contains( filterString, Qt::CaseInsensitive ) ) {
251 if ( contact.department().contains( filterString, Qt::CaseInsensitive ) ) {
255 if ( contact.note().contains( filterString, Qt::CaseInsensitive ) ) {
259 if ( contact.url().url().contains( filterString, Qt::CaseInsensitive ) ) {
263 const QStringList customs = contact.customs();
264 count = customs.count();
265 for (
int i = 0; i < count; ++i ) {
266 if ( customs.at( i ).contains( filterString, Qt::CaseInsensitive ) ) {
274 bool contactGroupMatchesFilter(
const KABC::ContactGroup &group,
const QString &filterString )
276 if ( group.name().contains( filterString, Qt::CaseInsensitive ) ) {
280 const uint count = group.dataCount();
281 for ( uint i = 0; i < count; ++i ) {
282 if ( group.data( i ).name().contains( filterString, Qt::CaseInsensitive ) ) {
285 if ( group.data( i ).email().contains( filterString, Qt::CaseInsensitive ) ) {
Represents a collection of PIM items.
bool isValid() const
Returns whether the entity is valid.
bool isVirtual() const
Returns whether the collection is virtual, for example a search collection.