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

libkleo

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

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