• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kleopatra

  • sources
  • kde-4.12
  • kdepim
  • kleopatra
  • models
keylistsortfilterproxymodel.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  models/keylistsortfilterproxymodel.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2007 Klarälvdalens Datakonsult AB
6 
7  Kleopatra is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Kleopatra is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  Qt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #include <config-kleopatra.h>
34 
35 #include "keylistsortfilterproxymodel.h"
36 
37 #include "keylistmodel.h"
38 
39 #include <kleo/keyfilter.h>
40 
41 #include <gpgme++/key.h>
42 
43 #include <kleo/stl_util.h>
44 
45 #include <boost/bind.hpp>
46 
47 #include <cassert>
48 
49 using namespace Kleo;
50 using namespace boost;
51 using namespace GpgME;
52 
53 AbstractKeyListSortFilterProxyModel::AbstractKeyListSortFilterProxyModel( QObject * p )
54  : QSortFilterProxyModel( p ), KeyListModelInterface()
55 {
56  init();
57 }
58 
59 AbstractKeyListSortFilterProxyModel::AbstractKeyListSortFilterProxyModel( const AbstractKeyListSortFilterProxyModel & other )
60  : QSortFilterProxyModel(), KeyListModelInterface()
61 {
62  Q_UNUSED( other );
63  init();
64 }
65 
66 void AbstractKeyListSortFilterProxyModel::init() {
67  setDynamicSortFilter( true );
68  setSortRole( Qt::EditRole ); // EditRole can be expected to be in a less formatted way, better for sorting
69  setFilterRole( Qt::DisplayRole );
70  setFilterCaseSensitivity( Qt::CaseInsensitive );
71 }
72 
73 AbstractKeyListSortFilterProxyModel::~AbstractKeyListSortFilterProxyModel() {}
74 
75 Key AbstractKeyListSortFilterProxyModel::key( const QModelIndex & idx ) const {
76  const KeyListModelInterface * const klmi = dynamic_cast<KeyListModelInterface*>( sourceModel() );
77  if ( !klmi ) {
78  static Key null;
79  return null;
80  }
81  return klmi->key( mapToSource( idx ) );
82 }
83 
84 std::vector<Key> AbstractKeyListSortFilterProxyModel::keys( const QList<QModelIndex> & indexes ) const {
85  const KeyListModelInterface * const klmi = dynamic_cast<KeyListModelInterface*>( sourceModel() );
86  if ( !klmi )
87  return std::vector<Key>();
88  QList<QModelIndex> mapped;
89  std::transform( indexes.begin(), indexes.end(),
90  std::back_inserter( mapped ),
91  boost::bind( &QAbstractProxyModel::mapToSource, this, _1 ) );
92  return klmi->keys( mapped );
93 }
94 
95 QModelIndex AbstractKeyListSortFilterProxyModel::index( const Key & key ) const {
96  if ( const KeyListModelInterface * const klmi = dynamic_cast<KeyListModelInterface*>( sourceModel() ) )
97  return mapFromSource( klmi->index( key ) );
98  else
99  return QModelIndex();
100 }
101 
102 QList<QModelIndex> AbstractKeyListSortFilterProxyModel::indexes( const std::vector<Key> & keys ) const {
103  if ( const KeyListModelInterface * const klmi = dynamic_cast<KeyListModelInterface*>( sourceModel() ) ) {
104  const QList<QModelIndex> source = klmi->indexes( keys );
105  QList<QModelIndex> mapped;
106  std::transform( source.begin(), source.end(),
107  std::back_inserter( mapped ),
108  boost::bind( &QAbstractProxyModel::mapFromSource, this, _1 ) );
109  return mapped;
110  } else {
111  return QList<QModelIndex>();
112  }
113 }
114 
115 
116 class KeyListSortFilterProxyModel::Private {
117  friend class ::Kleo::KeyListSortFilterProxyModel;
118 public:
119  explicit Private()
120  : keyFilter() {}
121  ~Private() {}
122 
123 private:
124  shared_ptr<const KeyFilter> keyFilter;
125 };
126 
127 
128 KeyListSortFilterProxyModel::KeyListSortFilterProxyModel( QObject * p )
129  : AbstractKeyListSortFilterProxyModel( p ), d( new Private )
130 {
131 
132 }
133 
134 KeyListSortFilterProxyModel::KeyListSortFilterProxyModel( const KeyListSortFilterProxyModel & other )
135  : AbstractKeyListSortFilterProxyModel( other ), d( new Private( *other.d ) )
136 {
137 
138 }
139 
140 KeyListSortFilterProxyModel::~KeyListSortFilterProxyModel() {}
141 
142 KeyListSortFilterProxyModel * KeyListSortFilterProxyModel::clone() const {
143  return new KeyListSortFilterProxyModel( *this );
144 }
145 
146 shared_ptr<const KeyFilter> KeyListSortFilterProxyModel::keyFilter() const {
147  return d->keyFilter;
148 }
149 
150 void KeyListSortFilterProxyModel::setKeyFilter( const shared_ptr<const KeyFilter> & kf ) {
151  if ( kf == d->keyFilter )
152  return;
153  d->keyFilter = kf;
154  invalidateFilter();
155 }
156 
157 bool KeyListSortFilterProxyModel::filterAcceptsRow( int source_row, const QModelIndex & source_parent ) const {
158 
159  //
160  // 0. Keep parents of matching children:
161  //
162  const QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
163  for ( int i = 0, end = sourceModel()->rowCount( index ) ; i != end ; ++i )
164  if ( filterAcceptsRow( i, index ) )
165  return true;
166 
167  //
168  // 1. Check that name or email matches filterRegExp
169  //
170  const int role = filterRole();
171 
172  const QModelIndex nameIndex = sourceModel()->index( source_row, PrettyName, source_parent );
173  const QString name = nameIndex.data( role ).toString();
174 
175 #ifndef KDEPIM_MOBILE_UI
176  const QModelIndex emailIndex = sourceModel()->index( source_row, PrettyEMail, source_parent );
177  const QString email = emailIndex.data( role ).toString();
178 #endif
179 
180  const QRegExp rx = filterRegExp();
181  if ( !name.contains( rx ) )
182 #ifndef KDEPIM_MOBILE_UI
183  if ( !email.contains( rx ) )
184 #endif
185  return false;
186 
187  //
188  // 2. Check that key filters match (if any are defined)
189  //
190  if ( d->keyFilter ) { // avoid artifacts when no filters are defined
191 
192  const KeyListModelInterface * const klm = dynamic_cast<KeyListModelInterface*>( sourceModel() );
193  assert( klm );
194  const Key key = klm->key( nameIndex );
195 
196  return d->keyFilter->matches( key, KeyFilter::Filtering );
197  }
198 
199  // 3. match by default:
200  return true;
201 }
202 
203 #include "moc_keylistsortfilterproxymodel.cpp"
Kleo::KeyListModelInterface::PrettyName
Definition: keylistmodelinterface.h:54
Kleo::AbstractKeyListSortFilterProxyModel::key
GpgME::Key key(const QModelIndex &idx) const
Definition: keylistsortfilterproxymodel.cpp:75
Kleo::KeyListSortFilterProxyModel::~KeyListSortFilterProxyModel
~KeyListSortFilterProxyModel()
Definition: keylistsortfilterproxymodel.cpp:140
Kleo::AbstractKeyListSortFilterProxyModel::index
QModelIndex index(const GpgME::Key &key) const
Definition: keylistsortfilterproxymodel.cpp:95
email
static std::string email(const UserID &uid)
Definition: keycache.cpp:593
Kleo::KeyListModelInterface::PrettyEMail
Definition: keylistmodelinterface.h:56
Kleo::KeyListSortFilterProxyModel::filterAcceptsRow
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
Definition: keylistsortfilterproxymodel.cpp:157
Kleo::KeyListModelInterface::key
virtual GpgME::Key key(const QModelIndex &idx) const =0
Kleo::AbstractKeyListSortFilterProxyModel::indexes
QList< QModelIndex > indexes(const std::vector< GpgME::Key > &keys) const
Definition: keylistsortfilterproxymodel.cpp:102
boost::shared_ptr< const KeyFilter >
d
#define d
Definition: adduseridcommand.cpp:90
keylistsortfilterproxymodel.h
Kleo::AbstractKeyListSortFilterProxyModel
Definition: keylistsortfilterproxymodel.h:51
Kleo::KeyListSortFilterProxyModel::KeyListSortFilterProxyModel
KeyListSortFilterProxyModel(const KeyListSortFilterProxyModel &)
Definition: keylistsortfilterproxymodel.cpp:134
Kleo::AbstractKeyListSortFilterProxyModel::keys
std::vector< GpgME::Key > keys(const QList< QModelIndex > &indexes) const
Definition: keylistsortfilterproxymodel.cpp:84
Kleo::KeyListSortFilterProxyModel::clone
KeyListSortFilterProxyModel * clone() const
Definition: keylistsortfilterproxymodel.cpp:142
Kleo::AbstractKeyListSortFilterProxyModel::AbstractKeyListSortFilterProxyModel
AbstractKeyListSortFilterProxyModel(const AbstractKeyListSortFilterProxyModel &)
Definition: keylistsortfilterproxymodel.cpp:59
Kleo::KeyListModelInterface
Definition: keylistmodelinterface.h:47
QSortFilterProxyModel
keylistmodel.h
Kleo::KeyListSortFilterProxyModel::setKeyFilter
void setKeyFilter(const boost::shared_ptr< const KeyFilter > &kf)
Definition: keylistsortfilterproxymodel.cpp:150
Kleo::KeyListSortFilterProxyModel
Definition: keylistsortfilterproxymodel.h:72
name
const char * name
Definition: uiserver/selectcertificatecommand.cpp:114
Kleo::KeyListModelInterface::keys
virtual std::vector< GpgME::Key > keys(const QList< QModelIndex > &idxs) const =0
Kleo::AbstractKeyListSortFilterProxyModel::~AbstractKeyListSortFilterProxyModel
~AbstractKeyListSortFilterProxyModel()
Definition: keylistsortfilterproxymodel.cpp:73
Kleo::KeyListSortFilterProxyModel::keyFilter
boost::shared_ptr< const KeyFilter > keyFilter() const
Definition: keylistsortfilterproxymodel.cpp:146
QList
Definition: commands/command.h:46
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:41 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kleopatra

Skip menu "kleopatra"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal