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

libkleo

  • sources
  • kde-4.14
  • kdepim
  • libkleo
  • kleo
keyfiltermanager.cpp
Go to the documentation of this file.
1 /*
2  keyfiltermanager.cpp
3 
4  This file is part of libkleopatra, the KDE keymanagement library
5  Copyright (c) 2004 Klarälvdalens Datakonsult AB
6 
7  Libkleopatra is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of the
10  License, or (at your option) any later version.
11 
12  Libkleopatra 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 "keyfiltermanager.h"
34 #include "kconfigbasedkeyfilter.h"
35 
36 #include "cryptobackendfactory.h"
37 #include "stl_util.h"
38 
39 #include <kconfig.h>
40 #include <kconfiggroup.h>
41 #include <klocale.h>
42 #include <KIcon>
43 #include <KDebug>
44 
45 #include <QCoreApplication>
46 #include <QRegExp>
47 #include <QStringList>
48 #include <QAbstractListModel>
49 #include <QModelIndex>
50 
51 #ifndef Q_MOC_RUN
52 #include <boost/bind.hpp>
53 #include <boost/iterator/filter_iterator.hpp>
54 #endif
55 
56 #include <algorithm>
57 #include <vector>
58 #include <climits>
59 
60 using namespace Kleo;
61 using namespace boost;
62 using namespace GpgME;
63 
64 namespace {
65 
66  class Model : public QAbstractListModel {
67  KeyFilterManager::Private * m_keyFilterManagerPrivate;
68  public:
69  explicit Model( KeyFilterManager::Private * p )
70  : QAbstractListModel( 0 ), m_keyFilterManagerPrivate( p ) {}
71 
72  /* reimp */ int rowCount( const QModelIndex & ) const;
73  /* reimp */ QVariant data( const QModelIndex & idx, int role ) const;
74  /* upgrade to public */ using QAbstractListModel::reset;
75  };
76 
77  class AllCertificatesKeyFilter : public KeyFilterImplBase {
78  public:
79  AllCertificatesKeyFilter()
80  : KeyFilterImplBase()
81  {
82  mSpecificity = UINT_MAX; // overly high for ordering
83  mName = i18n("All Certificates");
84  mId = QLatin1String("all-certificates");
85  mMatchContexts = Filtering;
86  }
87  };
88 
89  class MyCertificatesKeyFilter : public KeyFilterImplBase {
90  public:
91  MyCertificatesKeyFilter()
92  : KeyFilterImplBase()
93  {
94  mHasSecret = Set;
95  mSpecificity = UINT_MAX-1; // overly high for ordering
96 
97  mName = i18n("My Certificates");
98  mId = QLatin1String("my-certificates");
99  mMatchContexts = AnyMatchContext;
100  mBold = true;
101  }
102  };
103 
104  class TrustedCertificatesKeyFilter : public KeyFilterImplBase {
105  public:
106  TrustedCertificatesKeyFilter()
107  : KeyFilterImplBase()
108  {
109  mRevoked = NotSet;
110  mValidity = IsAtLeast;
111  mValidityReferenceLevel = UserID::Marginal; // Full?
112  mSpecificity = UINT_MAX-2; // overly high for ordering
113 
114  mName = i18n("Trusted Certificates");
115  mId = QLatin1String("trusted-certificates");
116  mMatchContexts = Filtering;
117  }
118  };
119 
120  class OtherCertificatesKeyFilter : public KeyFilterImplBase {
121  public:
122  OtherCertificatesKeyFilter()
123  : KeyFilterImplBase()
124  {
125  mHasSecret = NotSet;
126  mValidity = IsAtMost;
127  mValidityReferenceLevel = UserID::Never;
128  mSpecificity = UINT_MAX-3; // overly high for ordering
129 
130  mName = i18n("Other Certificates");
131  mId = QLatin1String("other-certificates");
132  mMatchContexts = Filtering;
133  }
134  };
135 }
136 
137 static std::vector< shared_ptr<KeyFilter> > defaultFilters() {
138  std::vector<shared_ptr<KeyFilter> > result;
139  result.reserve( 3 );
140  result.push_back( shared_ptr<KeyFilter>( new MyCertificatesKeyFilter ) );
141  result.push_back( shared_ptr<KeyFilter>( new TrustedCertificatesKeyFilter ) );
142  result.push_back( shared_ptr<KeyFilter>( new OtherCertificatesKeyFilter ) );
143  result.push_back( shared_ptr<KeyFilter>( new AllCertificatesKeyFilter ) );
144  return result;
145 }
146 
147 
148 class KeyFilterManager::Private {
149 public:
150  Private() : filters(), model( this ) {}
151  void clear() {
152  filters.clear();
153  model.reset();
154  }
155 
156  std::vector< shared_ptr<KeyFilter> > filters;
157  Model model;
158 };
159 
160 
161 KeyFilterManager * KeyFilterManager::mSelf = 0;
162 
163 KeyFilterManager::KeyFilterManager( QObject * parent )
164  : QObject( parent ), d( new Private )
165 {
166  mSelf = this;
167  // ### DF: doesn't a KStaticDeleter work more reliably?
168  if ( QCoreApplication * app = QCoreApplication::instance() )
169  connect( app, SIGNAL(aboutToQuit()), SLOT(deleteLater()) );
170  reload();
171 }
172 
173 KeyFilterManager::~KeyFilterManager() {
174  mSelf = 0;
175  if ( d )
176  d->clear();
177  delete d; d = 0;
178 }
179 
180 KeyFilterManager * KeyFilterManager::instance() {
181  if ( !mSelf )
182  mSelf = new KeyFilterManager();
183  return mSelf;
184 }
185 
186 const shared_ptr<KeyFilter> & KeyFilterManager::filterMatching( const Key & key, KeyFilter::MatchContexts contexts ) const {
187  const std::vector< shared_ptr<KeyFilter> >::const_iterator it
188  = std::find_if( d->filters.begin(), d->filters.end(),
189  bind( &KeyFilter::matches, _1, cref( key ), contexts ) );
190  if ( it != d->filters.end() )
191  return *it;
192  static const shared_ptr<KeyFilter> null;
193  return null;
194 }
195 
196 std::vector< shared_ptr<KeyFilter> > KeyFilterManager::filtersMatching( const Key & key, KeyFilter::MatchContexts contexts ) const {
197  std::vector< shared_ptr<KeyFilter> > result;
198  result.reserve( d->filters.size() );
199  std::remove_copy_if( d->filters.begin(), d->filters.end(),
200  std::back_inserter( result ),
201  !bind( &KeyFilter::matches, _1, cref( key ), contexts ) );
202  return result;
203 }
204 
205 namespace {
206  struct ByDecreasingSpecificity : std::binary_function<shared_ptr<KeyFilter>,shared_ptr<KeyFilter>,bool> {
207  bool operator()( const shared_ptr<KeyFilter> & lhs, const shared_ptr<KeyFilter> & rhs ) const {
208  return lhs->specificity() > rhs->specificity();
209  }
210  };
211 }
212 
213 void KeyFilterManager::reload() {
214  d->clear();
215 
216  d->filters = defaultFilters();
217 
218  if ( KConfig * config = CryptoBackendFactory::instance()->configObject() ) {
219  const QStringList groups = config->groupList().filter( QRegExp( QLatin1String("^Key Filter #\\d+$") ) );
220  for ( QStringList::const_iterator it = groups.begin() ; it != groups.end() ; ++it ) {
221  const KConfigGroup cfg( config, *it );
222  d->filters.push_back( shared_ptr<KeyFilter>( new KConfigBasedKeyFilter( cfg ) ) );
223  }
224  }
225  std::stable_sort( d->filters.begin(), d->filters.end(), ByDecreasingSpecificity() );
226  kDebug(5150) << "final filter count is" << d->filters.size();
227 }
228 
229 QAbstractItemModel * KeyFilterManager::model() const {
230  return &d->model;
231 }
232 
233 const shared_ptr<KeyFilter> & KeyFilterManager::keyFilterByID( const QString & id ) const {
234  const std::vector< shared_ptr<KeyFilter> >::const_iterator it
235  = std::find_if( d->filters.begin(), d->filters.end(),
236  bind( &KeyFilter::id, _1 ) == id );
237  if ( it != d->filters.end() )
238  return *it;
239  static const shared_ptr<KeyFilter> null;
240  return null;
241 }
242 
243 const shared_ptr<KeyFilter> & KeyFilterManager::fromModelIndex( const QModelIndex & idx ) const {
244  if ( !idx.isValid() || idx.model() != &d->model || idx.row() < 0 ||
245  static_cast<unsigned>(idx.row()) >= d->filters.size() ) {
246  static const shared_ptr<KeyFilter> null;
247  return null;
248  }
249  return d->filters[idx.row()];
250 }
251 
252 QModelIndex KeyFilterManager::toModelIndex( const shared_ptr<KeyFilter> & kf ) const {
253  if ( !kf )
254  return QModelIndex();
255  const std::pair<
256  std::vector<shared_ptr<KeyFilter> >::const_iterator,
257  std::vector<shared_ptr<KeyFilter> >::const_iterator
258  > pair = std::equal_range( d->filters.begin(), d->filters.end(), kf, ByDecreasingSpecificity() );
259  const std::vector<shared_ptr<KeyFilter> >::const_iterator it
260  = std::find( pair.first, pair.second, kf );
261  if ( it != pair.second )
262  return d->model.index( it - d->filters.begin() );
263  else
264  return QModelIndex();
265 }
266 
267 int Model::rowCount( const QModelIndex & ) const {
268  return m_keyFilterManagerPrivate->filters.size();
269 }
270 
271 QVariant Model::data( const QModelIndex & idx, int role ) const {
272  if ( ( role != Qt::DisplayRole && role != Qt::EditRole &&
273  role != Qt::ToolTipRole && role != Qt::DecorationRole ) ||
274  !idx.isValid() || idx.model() != this ||
275  idx.row() < 0 || static_cast<unsigned>(idx.row()) > m_keyFilterManagerPrivate->filters.size() )
276  return QVariant();
277  if ( role == Qt::DecorationRole )
278  return m_keyFilterManagerPrivate->filters[idx.row()]->icon();
279  else
280  return m_keyFilterManagerPrivate->filters[idx.row()]->name();
281 }
282 
283 namespace {
284  template <typename C, typename P1, typename P2>
285  typename C::const_iterator find_if_and( const C & c, P1 p1, P2 p2 ) {
286  return std::find_if( boost::make_filter_iterator( p1, boost::begin( c ), boost::end( c ) ),
287  boost::make_filter_iterator( p1, boost::end( c ), boost::end( c ) ),
288  p2 ).base();
289  }
290 }
291 
292 QFont KeyFilterManager::font( const Key & key, const QFont & baseFont ) const {
293  return kdtools::accumulate_transform_if( d->filters, mem_fn( &KeyFilter::fontDesription ),
294  bind( &KeyFilter::matches, _1, key, KeyFilter::Appearance ),
295  KeyFilter::FontDescription(),
296  bind( &KeyFilter::FontDescription::resolve, _1, _2 ) ).font( baseFont );
297 }
298 
299 static QColor get_color( const std::vector< shared_ptr<KeyFilter> > & filters, const Key & key, QColor (KeyFilter::*fun)() const ) {
300  const std::vector< shared_ptr<KeyFilter> >::const_iterator it
301  = find_if_and( filters,
302  bind( &KeyFilter::matches, _1, key, KeyFilter::Appearance ),
303  bind( &QColor::isValid, bind( fun, _1 ) ) );
304  if ( it == filters.end() )
305  return QColor();
306  else
307  return (it->get()->*fun)();
308 }
309 
310 static QString get_string( const std::vector< shared_ptr<KeyFilter> > & filters, const Key & key, QString (KeyFilter::*fun)() const ) {
311  const std::vector< shared_ptr<KeyFilter> >::const_iterator it
312  = find_if_and( filters,
313  bind( &KeyFilter::matches, _1, key, KeyFilter::Appearance ),
314  !bind( &QString::isEmpty, bind( fun, _1 ) ) );
315  if ( it == filters.end() )
316  return QString();
317  else
318  return (*it)->icon();
319 }
320 
321 QColor KeyFilterManager::bgColor( const Key & key ) const {
322  return get_color( d->filters, key, &KeyFilter::bgColor );
323 }
324 
325 QColor KeyFilterManager::fgColor( const Key & key ) const {
326  return get_color( d->filters, key, &KeyFilter::fgColor );
327 }
328 
329 QIcon KeyFilterManager::icon( const Key & key ) const {
330  const QString icon = get_string( d->filters, key, &KeyFilter::icon );
331  return icon.isEmpty() ? QIcon() : QIcon( KIcon( icon ) ) ;
332 }
333 
QModelIndex
get_color
static QColor get_color(const std::vector< shared_ptr< KeyFilter > > &filters, const Key &key, QColor(KeyFilter::*fun)() const )
Definition: keyfiltermanager.cpp:299
Kleo::KeyFilterManager::fromModelIndex
const boost::shared_ptr< KeyFilter > & fromModelIndex(const QModelIndex &mi) const
Definition: keyfiltermanager.cpp:243
stl_util.h
Kleo::KeyFilter::id
virtual QString id() const =0
Kleo::KeyFilter::bgColor
virtual QColor bgColor() const =0
QCoreApplication
QFont
keyfiltermanager.h
Kleo::KeyFilter::FontDescription::resolve
FontDescription resolve(const FontDescription &other) const
Definition: kconfigbasedkeyfilter.cpp:111
Kleo::KeyFilter::Appearance
Definition: keyfilter.h:60
Kleo::KeyFilterManager::icon
QIcon icon(const GpgME::Key &key) const
Definition: keyfiltermanager.cpp:329
Kleo::KeyFilter::fontDesription
virtual FontDescription fontDesription() const =0
Kleo::CryptoBackendFactory::instance
static CryptoBackendFactory * instance()
Definition: cryptobackendfactory.cpp:102
Kleo::KeyFilter::icon
virtual QString icon() const =0
Kleo::KeyFilterManager
Definition: keyfiltermanager.h:59
QList::const_iterator
Kleo::KeyFilterImplBase
Definition: kconfigbasedkeyfilter.h:49
Kleo::KConfigBasedKeyFilter
Definition: kconfigbasedkeyfilter.h:110
Kleo::KeyFilterManager::toModelIndex
QModelIndex toModelIndex(const boost::shared_ptr< KeyFilter > &kf) const
Definition: keyfiltermanager.cpp:252
QRegExp
QAbstractItemModel::reset
void reset()
QModelIndex::isValid
bool isValid() const
boost::shared_ptr
Definition: checksumdefinition.h:46
QObject
Kleo::KeyFilterManager::reload
void reload()
Definition: keyfiltermanager.cpp:213
QAbstractListModel
QString::isEmpty
bool isEmpty() const
cryptobackendfactory.h
QModelIndex::row
int row() const
Kleo::KeyFilterManager::keyFilterByID
const boost::shared_ptr< KeyFilter > & keyFilterByID(const QString &id) const
Definition: keyfiltermanager.cpp:233
Kleo::KeyFilterManager::model
QAbstractItemModel * model() const
Definition: keyfiltermanager.cpp:229
QCoreApplication::instance
QCoreApplication * instance()
QObject::deleteLater
void deleteLater()
QString
QColor
QStringList
kconfigbasedkeyfilter.h
QList::end
iterator end()
Kleo::KeyFilter
An abstract base class key filters.
Definition: keyfilter.h:54
Kleo::KeyFilterManager::instance
static KeyFilterManager * instance()
Definition: keyfiltermanager.cpp:180
kdtools::accumulate_transform_if
Value accumulate_transform_if(InputIterator first, InputIterator last, UnaryFunction map, UnaryPredicate pred, const Value &value=Value())
Definition: stl_util.h:166
QModelIndex::model
const QAbstractItemModel * model() const
QLatin1String
kdtools::find
boost::range_iterator< C >::type find(C &c, const V &v)
Definition: stl_util.h:324
Kleo::KeyFilterManager::fgColor
QColor fgColor(const GpgME::Key &key) const
Definition: keyfiltermanager.cpp:325
Kleo::KeyFilterManager::bgColor
QColor bgColor(const GpgME::Key &key) const
Definition: keyfiltermanager.cpp:321
QAbstractItemModel
Kleo::KeyFilterManager::font
QFont font(const GpgME::Key &key, const QFont &baseFont) const
Definition: keyfiltermanager.cpp:292
Kleo::KeyFilterManager::filtersMatching
std::vector< boost::shared_ptr< KeyFilter > > filtersMatching(const GpgME::Key &key, KeyFilter::MatchContexts contexts) const
Definition: keyfiltermanager.cpp:196
Kleo::KeyFilter::FontDescription
Definition: keyfilter.h:80
defaultFilters
static std::vector< shared_ptr< KeyFilter > > defaultFilters()
Definition: keyfiltermanager.cpp:137
get_string
static QString get_string(const std::vector< shared_ptr< KeyFilter > > &filters, const Key &key, QString(KeyFilter::*fun)() const )
Definition: keyfiltermanager.cpp:310
QStringList::filter
QStringList filter(const QString &str, Qt::CaseSensitivity cs) const
kdtools::find_if
boost::range_iterator< C >::type find_if(C &c, P p)
Definition: stl_util.h:338
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Kleo::KeyFilterManager::KeyFilterManager
KeyFilterManager(QObject *parent=0)
Definition: keyfiltermanager.cpp:163
Kleo::KeyFilter::matches
virtual bool matches(const GpgME::Key &key, MatchContexts ctx) const =0
Kleo::KeyFilter::fgColor
virtual QColor fgColor() const =0
QList::begin
iterator begin()
Kleo::KeyFilterManager::filterMatching
const boost::shared_ptr< KeyFilter > & filterMatching(const GpgME::Key &key, KeyFilter::MatchContexts contexts) const
Definition: keyfiltermanager.cpp:186
QIcon
QColor::isValid
bool isValid() const
QVariant
Kleo::KeyFilterManager::~KeyFilterManager
~KeyFilterManager()
Definition: keyfiltermanager.cpp:173
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:38 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkleo

Skip menu "libkleo"
  • 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
  • pimprint

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