~ubuntu-branches/ubuntu/karmic/kdepim/karmic-backports

« back to all changes in this revision

Viewing changes to kcontactmanager/contactfiltermodel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi, Alessandro Ghersi, Harald Sitter
  • Date: 2009-06-27 04:40:05 UTC
  • mfrom: (1.1.39 upstream)
  • Revision ID: james.westby@ubuntu.com-20090627044005-4y2vm9xz7rvmzi4p
Tags: 4:4.2.95svn20090701-0ubuntu1
[ Alessandro Ghersi ]
* New upstream release
  - Bump build-deps
* Remove akonadi-kde and libmaildir4 packages
  - remove akonadi-kde.install and libmaildir4.install
  - remove libmaildir4 from debian/rules
  - remove akonadi-kde and libmaildir4 from depends
  - remove akonadi-kde and libmaildir4 from installgen
* Update kdepim-dev.install
* Update kpilot.install
* Add akonadi-kde and libmaildir4 transitional packages

[ Harald Sitter ]
* KAddressbook replaces Kontact << 4.2.85 (LP: #378373)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
3
 
 
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.
8
 
 
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.
13
 
 
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.
18
 
*/
19
 
 
20
 
#include "contactfiltermodel.h"
21
 
 
22
 
#include <akonadi_next/entitytreemodel.h>
23
 
#include <kabc/addressee.h>
24
 
#include <kabc/contactgroup.h>
25
 
 
26
 
static bool contactMatchesFilter( const KABC::Addressee &contact, const QString &filterString );
27
 
static bool contactGroupMatchesFilter( const KABC::ContactGroup &group, const QString &filterString );
28
 
 
29
 
ContactFilterModel::ContactFilterModel( QObject *parent )
30
 
  : QSortFilterProxyModel( parent )
31
 
{
32
 
}
33
 
 
34
 
void ContactFilterModel::setFilterString( const QString &filter )
35
 
{
36
 
  mFilter = filter;
37
 
  invalidateFilter();
38
 
}
39
 
 
40
 
bool ContactFilterModel::filterAcceptsRow( int row, const QModelIndex &parent ) const
41
 
{
42
 
  if ( mFilter.isEmpty() )
43
 
    return true;
44
 
 
45
 
  const QModelIndex index = sourceModel()->index( row, 0, parent );
46
 
 
47
 
  const Akonadi::Item item = index.data( Akonadi::EntityTreeModel::ItemRole ).value<Akonadi::Item>();
48
 
 
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 );
55
 
  }
56
 
 
57
 
  return true;
58
 
}
59
 
 
60
 
static bool addressMatchesFilter( const KABC::Address &address, const QString &filterString )
61
 
{
62
 
  if ( address.street().contains( filterString, Qt::CaseInsensitive ) )
63
 
    return true;
64
 
 
65
 
  if ( address.locality().contains( filterString, Qt::CaseInsensitive ) )
66
 
    return true;
67
 
 
68
 
  if ( address.region().contains( filterString, Qt::CaseInsensitive ) )
69
 
    return true;
70
 
 
71
 
  if ( address.postalCode().contains( filterString, Qt::CaseInsensitive ) )
72
 
    return true;
73
 
 
74
 
  if ( address.country().contains( filterString, Qt::CaseInsensitive ) )
75
 
    return true;
76
 
 
77
 
  if ( address.label().contains( filterString, Qt::CaseInsensitive ) )
78
 
    return true;
79
 
 
80
 
  if ( address.postOfficeBox().contains( filterString, Qt::CaseInsensitive ) )
81
 
    return true;
82
 
 
83
 
  return false;
84
 
}
85
 
 
86
 
static bool contactMatchesFilter( const KABC::Addressee &contact, const QString &filterString )
87
 
{
88
 
  if ( contact.assembledName().contains( filterString, Qt::CaseInsensitive ) )
89
 
    return true;
90
 
 
91
 
  if ( contact.formattedName().contains( filterString, Qt::CaseInsensitive ) )
92
 
    return true;
93
 
 
94
 
  if ( contact.nickName().contains( filterString, Qt::CaseInsensitive ) )
95
 
    return true;
96
 
 
97
 
  if ( contact.birthday().toString().contains( filterString, Qt::CaseInsensitive ) )
98
 
    return true;
99
 
 
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 ) )
104
 
      return true;
105
 
  }
106
 
 
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 ) )
111
 
      return true;
112
 
  }
113
 
 
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 ) )
118
 
      return true;
119
 
  }
120
 
 
121
 
  if ( contact.mailer().contains( filterString, Qt::CaseInsensitive ) )
122
 
    return true;
123
 
 
124
 
  if ( contact.title().contains( filterString, Qt::CaseInsensitive ) )
125
 
    return true;
126
 
 
127
 
  if ( contact.role().contains( filterString, Qt::CaseInsensitive ) )
128
 
    return true;
129
 
 
130
 
  if ( contact.organization().contains( filterString, Qt::CaseInsensitive ) )
131
 
    return true;
132
 
 
133
 
  if ( contact.department().contains( filterString, Qt::CaseInsensitive ) )
134
 
    return true;
135
 
 
136
 
  if ( contact.note().contains( filterString, Qt::CaseInsensitive ) )
137
 
    return true;
138
 
 
139
 
  if ( contact.url().url().contains( filterString, Qt::CaseInsensitive ) )
140
 
    return true;
141
 
 
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 ) )
146
 
      return true;
147
 
  }
148
 
 
149
 
  return false;
150
 
}
151
 
 
152
 
bool contactGroupMatchesFilter( const KABC::ContactGroup &group, const QString &filterString )
153
 
{
154
 
  const int count = group.dataCount();
155
 
  for ( int i = 0; i < count; ++i ) {
156
 
    if ( group.data( i ).name().contains( filterString, Qt::CaseInsensitive ) )
157
 
      return true;
158
 
    if ( group.data( i ).email().contains( filterString, Qt::CaseInsensitive ) )
159
 
      return true;
160
 
  }
161
 
 
162
 
  return false;
163
 
}
164
 
 
165
 
#include "contactfiltermodel.moc"