~ubuntu-branches/ubuntu/raring/kdepimlibs/raring-proposed

« back to all changes in this revision

Viewing changes to akonadi/contact/contactsfilterproxymodel.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-14 14:37:07 UTC
  • mfrom: (1.1.72)
  • Revision ID: package-import@ubuntu.com-20111214143707-nvfc00wnfayzn9ig
Tags: 4:4.7.90-0ubuntu1
* New upstream beta release
* Add packages libkalarmcal2 and libakonadi-notes4

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
class ContactsFilterProxyModel::Private
36
36
{
37
37
  public:
 
38
    Private()
 
39
      : flags( 0 ),
 
40
        mExcludeVirtualCollections( false )
 
41
    {
 
42
    }
38
43
    QString mFilter;
 
44
    ContactsFilterProxyModel::FilterFlags flags;
 
45
    bool mExcludeVirtualCollections;
39
46
};
40
47
 
41
48
ContactsFilterProxyModel::ContactsFilterProxyModel( QObject *parent )
59
66
 
60
67
bool ContactsFilterProxyModel::filterAcceptsRow( int row, const QModelIndex &parent ) const
61
68
{
62
 
  if ( d->mFilter.isEmpty() )
63
 
    return true;
64
 
 
65
69
  const QModelIndex index = sourceModel()->index( row, 0, parent );
 
70
  if ( d->mExcludeVirtualCollections ) {
 
71
    const Akonadi::Collection collection = index.data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
 
72
    if ( collection.isValid() && collection.isVirtual())
 
73
      return false;
 
74
  }
 
75
    
 
76
  if ( ( d->mFilter.isEmpty() ) && ( !( d->flags & ContactsFilterProxyModel::HasEmail ) ))
 
77
    return true;
66
78
 
67
79
  const Akonadi::Item item = index.data( Akonadi::EntityTreeModel::ItemRole ).value<Akonadi::Item>();
68
80
 
69
81
  if ( item.hasPayload<KABC::Addressee>() ) {
70
82
    const KABC::Addressee contact = item.payload<KABC::Addressee>();
71
 
    return contactMatchesFilter( contact, d->mFilter );
72
 
  } else if ( item.hasPayload<KABC::ContactGroup>() ) {
73
 
    const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
74
 
    return contactGroupMatchesFilter( group, d->mFilter );
 
83
    if ( d->flags & ContactsFilterProxyModel::HasEmail ){
 
84
      if ( contact.emails().isEmpty() )
 
85
        return false;
 
86
    }
 
87
    if ( !d->mFilter.isEmpty() ) {
 
88
        return contactMatchesFilter( contact, d->mFilter );
 
89
    }
 
90
  } else {
 
91
    if ( !d->mFilter.isEmpty() ) {
 
92
      if ( item.hasPayload<KABC::ContactGroup>() ) {
 
93
      const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
 
94
      return contactGroupMatchesFilter( group, d->mFilter );
 
95
      }
 
96
    }
75
97
  }
76
98
 
77
99
  return true;
94
116
  return QSortFilterProxyModel::lessThan( leftIndex, rightIndex );
95
117
}
96
118
 
 
119
void ContactsFilterProxyModel::setFilterFlags( ContactsFilterProxyModel::FilterFlags flags )
 
120
{
 
121
  d->flags = flags;
 
122
}
 
123
 
 
124
void ContactsFilterProxyModel::setExcludeVirtualCollections( bool exclude )
 
125
{
 
126
  if ( exclude != d->mExcludeVirtualCollections ) {
 
127
    d->mExcludeVirtualCollections = exclude;
 
128
    invalidateFilter();
 
129
  }
 
130
}
 
131
 
 
132
Qt::ItemFlags ContactsFilterProxyModel::flags( const QModelIndex& index ) const
 
133
{
 
134
  if ( !index.isValid() ) {
 
135
    // Don't crash
 
136
    return 0;
 
137
  }
 
138
  const Akonadi::Collection collection = index.data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
 
139
  if ( collection.isValid() )
 
140
  {
 
141
    return QSortFilterProxyModel::flags( index ) & ~( Qt::ItemIsSelectable );
 
142
  }
 
143
  return QSortFilterProxyModel::flags( index );
 
144
}
 
145
 
97
146
static bool addressMatchesFilter( const KABC::Address &address, const QString &filterString )
98
147
{
99
148
  if ( address.street().contains( filterString, Qt::CaseInsensitive ) )