• 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
kconfigbasedkeyfilter.cpp
Go to the documentation of this file.
1 /*
2  kconfigbasedkeyfilter.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 
34 #include "kconfigbasedkeyfilter.h"
35 
36 #include <kconfigbase.h>
37 #include <kconfiggroup.h>
38 #include <klocale.h>
39 
40 #include <boost/mem_fn.hpp>
41 #include <algorithm>
42 
43 using namespace Kleo;
44 using namespace GpgME;
45 using namespace boost;
46 
47 //
48 //
49 // FontDescription - intuitive font property resolving
50 // (QFont::resolve doesn't work for us)
51 //
52 //
53 struct KeyFilter::FontDescription::Private {
54  bool bold, italic, strikeOut, fullFont;
55  QFont font;
56 };
57 
58 KeyFilter::FontDescription::FontDescription()
59  : d( new Private )
60 {
61  d->bold = d->italic = d->strikeOut = d->fullFont = false;
62 }
63 
64 KeyFilter::FontDescription::FontDescription( const FontDescription & other )
65  : d( new Private( *other.d ) )
66 {
67 
68 }
69 
70 KeyFilter::FontDescription::~FontDescription() {
71  delete d;
72 }
73 
74 KeyFilter::FontDescription KeyFilter::FontDescription::create( bool b, bool i, bool s ) {
75  FontDescription fd;
76  fd.d->bold = b;
77  fd.d->italic = i;
78  fd.d->strikeOut = s;
79  return fd;
80 }
81 
82 KeyFilter::FontDescription KeyFilter::FontDescription::create( const QFont & f, bool b, bool i, bool s ) {
83  FontDescription fd;
84  fd.d->fullFont = true;
85  fd.d->font = f;
86  fd.d->bold = b;
87  fd.d->italic = i;
88  fd.d->strikeOut = s;
89  return fd;
90 }
91 
92 QFont KeyFilter::FontDescription::font( const QFont & base ) const {
93  QFont font;
94  if ( d->fullFont ) {
95  font = d->font;
96  font.setPointSize( base.pointSize() );
97  } else {
98  font = base;
99  }
100  if ( d->bold )
101  font.setBold( true );
102  if ( d->italic )
103  font.setItalic( true );
104  if ( d->strikeOut )
105  font.setStrikeOut( true );
106  return font;
107 }
108 
109 KeyFilter::FontDescription KeyFilter::FontDescription::resolve( const FontDescription & other ) const {
110  FontDescription fd;
111  fd.d->fullFont = this->d->fullFont || other.d->fullFont ;
112  if ( fd.d->fullFont )
113  fd.d->font = this->d->fullFont ? this->d->font : other.d->font ;
114  fd.d->bold = this->d->bold || other.d->bold ;
115  fd.d->italic = this->d->italic || other.d->italic ;
116  fd.d->strikeOut = this->d->strikeOut || other.d->strikeOut ;
117  return fd;
118 }
119 
120 
121 
122 
123 static const struct {
124  const char * name;
125  Key::OwnerTrust trust;
126  UserID::Validity validity;
127 } ownerTrustAndValidityMap[] = {
128  { "unknown", Key::Unknown, UserID::Unknown },
129  { "undefined", Key::Undefined, UserID::Undefined },
130  { "never", Key::Never, UserID::Never },
131  { "marginal", Key::Marginal, UserID::Marginal },
132  { "full", Key::Full, UserID::Full },
133  { "ultimate", Key::Ultimate, UserID::Ultimate },
134 };
135 
136 static Key::OwnerTrust map2OwnerTrust( const QString & s ) {
137  for ( unsigned int i = 0 ; i < sizeof ownerTrustAndValidityMap / sizeof *ownerTrustAndValidityMap ; ++i )
138  if ( s.toLower() == QLatin1String(ownerTrustAndValidityMap[i].name) )
139  return ownerTrustAndValidityMap[i].trust;
140  return ownerTrustAndValidityMap[0].trust;
141 }
142 
143 static UserID::Validity map2Validity( const QString & s ) {
144  for ( unsigned int i = 0 ; i < sizeof ownerTrustAndValidityMap / sizeof *ownerTrustAndValidityMap ; ++i )
145  if ( s.toLower() == QLatin1String(ownerTrustAndValidityMap[i].name) )
146  return ownerTrustAndValidityMap[i].validity;
147  return ownerTrustAndValidityMap[0].validity;
148 }
149 
150 
151 KeyFilterImplBase::KeyFilterImplBase()
152  : KeyFilter(),
153  mMatchContexts( AnyMatchContext ),
154  mSpecificity( 0 ),
155  mItalic( false ),
156  mBold( false ),
157  mStrikeOut( false ),
158  mUseFullFont( false ),
159  mRevoked( DoesNotMatter ),
160  mExpired( DoesNotMatter ),
161  mDisabled( DoesNotMatter ),
162  mRoot( DoesNotMatter ),
163  mCanEncrypt( DoesNotMatter ),
164  mCanSign( DoesNotMatter ),
165  mCanCertify( DoesNotMatter ),
166  mCanAuthenticate( DoesNotMatter ),
167  mQualified( DoesNotMatter ),
168  mCardKey( DoesNotMatter ),
169  mHasSecret( DoesNotMatter ),
170  mIsOpenPGP( DoesNotMatter ),
171  mWasValidated( DoesNotMatter ),
172  mOwnerTrust( LevelDoesNotMatter ),
173  mOwnerTrustReferenceLevel( Key::Unknown ),
174  mValidity( LevelDoesNotMatter ),
175  mValidityReferenceLevel( UserID::Unknown )
176 {
177 
178 }
179 
180 KeyFilterImplBase::~KeyFilterImplBase() {}
181 
182 KConfigBasedKeyFilter::KConfigBasedKeyFilter( const KConfigGroup & config )
183  : KeyFilterImplBase()
184 {
185  mFgColor = config.readEntry<QColor>( "foreground-color", QColor() );
186  mBgColor = config.readEntry<QColor>( "background-color", QColor() );
187  mName = config.readEntry( "Name", config.name() );
188  mIcon = config.readEntry( "icon" );
189  mId = config.readEntry( "id", config.name() );
190  if ( config.hasKey( "font" ) ) {
191  mUseFullFont = true;
192  mFont = config.readEntry( "font" );
193  } else {
194  mUseFullFont = false;
195  mItalic = config.readEntry( "font-italic", false );
196  mBold = config.readEntry( "font-bold", false );
197  }
198  mStrikeOut = config.readEntry( "font-strikeout", false );
199 #ifdef SET
200 #undef SET
201 #endif
202 #define SET(member,key) \
203  if ( config.hasKey( key ) ) { \
204  member = config.readEntry( key, false ) ? Set : NotSet ; \
205  ++mSpecificity; \
206  }
207  SET( mRevoked, "is-revoked" );
208  SET( mExpired, "is-expired" );
209  SET( mDisabled, "is-disabled" );
210  SET( mRoot, "is-root-certificate" );
211  SET( mCanEncrypt, "can-encrypt" );
212  SET( mCanSign, "can-sign" );
213  SET( mCanCertify, "can-certify" );
214  SET( mCanAuthenticate, "can-authenticate" );
215  SET( mQualified, "is-qualified" );
216  SET( mCardKey, "is-cardkey" );
217  SET( mHasSecret, "has-secret-key" );
218  SET( mIsOpenPGP, "is-openpgp-key" );
219  SET( mWasValidated, "was-validated" );
220 #undef SET
221  static const struct {
222  const char * prefix;
223  LevelState state;
224  } prefixMap[] = {
225  { "is-", Is },
226  { "is-not-", IsNot },
227  { "is-at-least-", IsAtLeast },
228  { "is-at-most-", IsAtMost },
229  };
230  for ( unsigned int i = 0 ; i < sizeof prefixMap / sizeof *prefixMap ; ++i ) {
231  const QString key = QLatin1String( prefixMap[i].prefix ) + QLatin1String("ownertrust");
232  if ( config.hasKey( key ) ) {
233  mOwnerTrust = prefixMap[i].state;
234  mOwnerTrustReferenceLevel = map2OwnerTrust( config.readEntry( key, QString() ) );
235  ++mSpecificity;
236  break;
237  }
238  }
239  for ( unsigned int i = 0 ; i < sizeof prefixMap / sizeof *prefixMap ; ++i ) {
240  const QString key = QLatin1String( prefixMap[i].prefix ) + QLatin1String("validity");
241  if ( config.hasKey( key ) ) {
242  mValidity = prefixMap[i].state;
243  mValidityReferenceLevel = map2Validity( config.readEntry( key, QString() ) );
244  ++mSpecificity;
245  break;
246  }
247  }
248  static const struct {
249  const char * key;
250  MatchContext context;
251  } matchMap[] = {
252  { "any", AnyMatchContext },
253  { "appearance", Appearance },
254  { "filtering", Filtering },
255  };
256  const QStringList contexts = config.readEntry( "match-contexts", "any" ).toLower().split( QRegExp( QLatin1String("[^a-zA-Z0-9_-!]+") ), QString::SkipEmptyParts );
257  mMatchContexts = NoMatchContext;
258  Q_FOREACH( const QString & ctx, contexts ) {
259  bool found = false;
260  for ( unsigned int i = 0 ; i < sizeof matchMap / sizeof *matchMap ; ++i )
261  if ( ctx == QLatin1String(matchMap[i].key) ) {
262  mMatchContexts |= matchMap[i].context;
263  found = true;
264  break;
265  } else if ( ctx.startsWith( QLatin1Char('!') ) && ctx.mid( 1 ) == QLatin1String(matchMap[i].key) ) {
266  mMatchContexts &= ~matchMap[i].context;
267  found = true;
268  break;
269  }
270  if ( !found )
271  qWarning( "KConfigBasedKeyFilter: found unknown match context '%s' in group '%s'",
272  qPrintable( ctx ), qPrintable( config.name() ) );
273  }
274  if ( mMatchContexts == NoMatchContext ) {
275  qWarning( "KConfigBasedKeyFilter: match context in group '%s' evaluates to NoMatchContext, "
276  "replaced by AnyMatchContext", qPrintable( config.name() ) );
277  mMatchContexts = AnyMatchContext;
278  }
279 }
280 
281 static bool is_card_key( const Key & key ) {
282  const std::vector<Subkey> sks = key.subkeys();
283  return std::find_if( sks.begin(), sks.end(),
284  mem_fn( &Subkey::isCardKey ) ) != sks.end() ;
285 }
286 
287 bool KeyFilterImplBase::matches( const Key & key, MatchContexts contexts ) const {
288  if ( !( mMatchContexts & contexts ) )
289  return false;
290 #ifdef MATCH
291 #undef MATCH
292 #endif
293 #define MATCH(member,method) \
294  if ( member != DoesNotMatter && key.method() != bool( member == Set ) ) \
295  return false
296 #define IS_MATCH(what) MATCH( m##what, is##what )
297 #define CAN_MATCH(what) MATCH( mCan##what, can##what )
298  IS_MATCH( Revoked );
299  IS_MATCH( Expired );
300  IS_MATCH( Disabled );
301  IS_MATCH( Root );
302  CAN_MATCH( Encrypt );
303  CAN_MATCH( Sign );
304  CAN_MATCH( Certify );
305  CAN_MATCH( Authenticate );
306  IS_MATCH( Qualified );
307  if ( mCardKey != DoesNotMatter )
308  if ( ( mCardKey == Set && !is_card_key( key ) ) ||
309  ( mCardKey == NotSet && is_card_key( key ) ) )
310  return false;
311  MATCH( mHasSecret, hasSecret );
312 #undef MATCH
313  if ( mIsOpenPGP != DoesNotMatter &&
314  bool( key.protocol() == GpgME::OpenPGP ) != bool( mIsOpenPGP == Set ) )
315  return false;
316  if ( mWasValidated != DoesNotMatter &&
317  bool( key.keyListMode() & GpgME::Validate ) != bool( mWasValidated == Set ) )
318  return false;
319  switch ( mOwnerTrust ) {
320  default:
321  case LevelDoesNotMatter:
322  break;
323  case Is:
324  if ( key.ownerTrust() != mOwnerTrustReferenceLevel )
325  return false;
326  break;
327  case IsNot:
328  if ( key.ownerTrust() == mOwnerTrustReferenceLevel )
329  return false;
330  break;
331  case IsAtLeast:
332  if ( (int)key.ownerTrust() < (int)mOwnerTrustReferenceLevel )
333  return false;
334  break;
335  case IsAtMost:
336  if ( (int)key.ownerTrust() > (int)mOwnerTrustReferenceLevel )
337  return false;
338  break;
339  }
340  const UserID uid = key.userID(0);
341  switch ( mValidity ) {
342  default:
343  case LevelDoesNotMatter:
344  break;
345  case Is:
346  if ( uid.validity() != mValidityReferenceLevel )
347  return false;
348  break;
349  case IsNot:
350  if ( uid.validity() == mValidityReferenceLevel )
351  return false;
352  break;
353  case IsAtLeast:
354  if ( (int)uid.validity() < (int)mValidityReferenceLevel )
355  return false;
356  break;
357  case IsAtMost:
358  if ( (int)uid.validity() > (int)mValidityReferenceLevel )
359  return false;
360  break;
361  }
362  return true;
363 }
364 
365 KeyFilter::FontDescription KeyFilterImplBase::fontDesription() const {
366  if ( mUseFullFont )
367  return FontDescription::create( mFont, mBold, mItalic, mStrikeOut );
368  else
369  return FontDescription::create( mBold, mItalic, mStrikeOut );
370 }
Kleo::KeyFilterImplBase::KeyFilterImplBase
KeyFilterImplBase()
Definition: kconfigbasedkeyfilter.cpp:151
IS_MATCH
#define IS_MATCH(what)
Kleo::KeyFilterImplBase::mId
QString mId
Definition: kconfigbasedkeyfilter.h:70
Kleo::KeyFilterImplBase::mValidityReferenceLevel
GpgME::UserID::Validity mValidityReferenceLevel
Definition: kconfigbasedkeyfilter.h:107
Kleo::KeyFilterImplBase::mUseFullFont
bool mUseFullFont
Definition: kconfigbasedkeyfilter.h:76
Kleo::KeyFilter::FontDescription::resolve
FontDescription resolve(const FontDescription &other) const
Definition: kconfigbasedkeyfilter.cpp:109
Kleo::KeyFilterImplBase::mRoot
TriState mRoot
Definition: kconfigbasedkeyfilter.h:87
Kleo::KeyFilter::AnyMatchContext
Definition: keyfilter.h:63
Kleo::KeyFilter::FontDescription::create
static FontDescription create(bool bold, bool italic, bool strikeOut)
Definition: kconfigbasedkeyfilter.cpp:74
Kleo::KeyFilterImplBase::mValidity
LevelState mValidity
Definition: kconfigbasedkeyfilter.h:106
CAN_MATCH
#define CAN_MATCH(what)
validity
UserID::Validity validity
Definition: kconfigbasedkeyfilter.cpp:126
Kleo::KeyFilterImplBase::mName
QString mName
Definition: kconfigbasedkeyfilter.h:68
QString
Kleo::KeyFilterImplBase::mCanCertify
TriState mCanCertify
Definition: kconfigbasedkeyfilter.h:90
Kleo::KeyFilter::Appearance
Definition: keyfilter.h:60
Kleo::KeyFilterImplBase::mOwnerTrust
LevelState mOwnerTrust
Definition: kconfigbasedkeyfilter.h:104
Kleo::KeyFilterImplBase::mMatchContexts
MatchContexts mMatchContexts
Definition: kconfigbasedkeyfilter.h:71
Kleo::KeyFilter::name
virtual QString name() const =0
is_card_key
static bool is_card_key(const Key &key)
Definition: kconfigbasedkeyfilter.cpp:281
Kleo::KeyFilterImplBase::mFont
QFont mFont
Definition: kconfigbasedkeyfilter.h:77
Kleo::KeyFilterImplBase
Definition: kconfigbasedkeyfilter.h:49
map2Validity
static UserID::Validity map2Validity(const QString &s)
Definition: kconfigbasedkeyfilter.cpp:143
Kleo::KeyFilterImplBase::Is
Definition: kconfigbasedkeyfilter.h:99
Kleo::KeyFilterImplBase::mRevoked
TriState mRevoked
Definition: kconfigbasedkeyfilter.h:84
Kleo::KeyFilterImplBase::mCardKey
TriState mCardKey
Definition: kconfigbasedkeyfilter.h:93
Kleo::KeyFilterImplBase::IsAtLeast
Definition: kconfigbasedkeyfilter.h:101
Kleo::KeyFilterImplBase::LevelDoesNotMatter
Definition: kconfigbasedkeyfilter.h:98
Kleo::KeyFilterImplBase::mBgColor
QColor mBgColor
Definition: kconfigbasedkeyfilter.h:67
Kleo::KeyFilterImplBase::mExpired
TriState mExpired
Definition: kconfigbasedkeyfilter.h:85
Kleo::KeyFilterImplBase::mCanEncrypt
TriState mCanEncrypt
Definition: kconfigbasedkeyfilter.h:88
Kleo::KeyFilterImplBase::mBold
bool mBold
Definition: kconfigbasedkeyfilter.h:74
Kleo::KeyFilterImplBase::IsNot
Definition: kconfigbasedkeyfilter.h:100
MATCH
#define MATCH(member, method)
Kleo::KeyFilterImplBase::~KeyFilterImplBase
~KeyFilterImplBase()
Definition: kconfigbasedkeyfilter.cpp:180
Kleo::KeyFilterImplBase::mDisabled
TriState mDisabled
Definition: kconfigbasedkeyfilter.h:86
Kleo::KeyFilter::Filtering
Definition: keyfilter.h:61
ownerTrustAndValidityMap
static const struct @3 ownerTrustAndValidityMap[]
Kleo::KeyFilterImplBase::Set
Definition: kconfigbasedkeyfilter.h:81
Kleo::KeyFilterImplBase::mFgColor
QColor mFgColor
Definition: kconfigbasedkeyfilter.h:67
Kleo::KeyFilterImplBase::mIsOpenPGP
TriState mIsOpenPGP
Definition: kconfigbasedkeyfilter.h:95
Kleo::KeyFilterImplBase::IsAtMost
Definition: kconfigbasedkeyfilter.h:102
kconfigbasedkeyfilter.h
Kleo::KeyFilterImplBase::mCanSign
TriState mCanSign
Definition: kconfigbasedkeyfilter.h:89
Kleo::KeyFilterImplBase::DoesNotMatter
Definition: kconfigbasedkeyfilter.h:80
Kleo::KeyFilter
An abstract base class key filters.
Definition: keyfilter.h:54
Kleo::KeyFilterImplBase::NotSet
Definition: kconfigbasedkeyfilter.h:82
Kleo::KeyFilterImplBase::mQualified
TriState mQualified
Definition: kconfigbasedkeyfilter.h:92
Kleo::KeyFilter::NoMatchContext
Definition: keyfilter.h:59
Kleo::KeyFilterImplBase::mWasValidated
TriState mWasValidated
Definition: kconfigbasedkeyfilter.h:96
SET
#define SET(member, key)
Kleo::KeyFilterImplBase::mSpecificity
unsigned int mSpecificity
Definition: kconfigbasedkeyfilter.h:72
Kleo::KeyFilter::FontDescription::~FontDescription
~FontDescription()
Definition: kconfigbasedkeyfilter.cpp:70
Kleo::KeyFilterImplBase::mOwnerTrustReferenceLevel
GpgME::Key::OwnerTrust mOwnerTrustReferenceLevel
Definition: kconfigbasedkeyfilter.h:105
trust
Key::OwnerTrust trust
Definition: kconfigbasedkeyfilter.cpp:125
Kleo::KeyFilterImplBase::mCanAuthenticate
TriState mCanAuthenticate
Definition: kconfigbasedkeyfilter.h:91
Kleo::KeyFilterImplBase::mIcon
QString mIcon
Definition: kconfigbasedkeyfilter.h:69
Kleo::KeyFilterImplBase::fontDesription
FontDescription fontDesription() const
Definition: kconfigbasedkeyfilter.cpp:365
Kleo::KConfigBasedKeyFilter::KConfigBasedKeyFilter
KConfigBasedKeyFilter(const KConfigGroup &group)
Definition: kconfigbasedkeyfilter.cpp:182
Kleo::KeyFilter::FontDescription::font
QFont font(const QFont &base) const
Definition: kconfigbasedkeyfilter.cpp:92
Kleo::KeyFilter::FontDescription
Definition: keyfilter.h:80
Kleo::KeyFilterImplBase::mHasSecret
TriState mHasSecret
Definition: kconfigbasedkeyfilter.h:94
kdtools::find_if
boost::range_iterator< C >::type find_if(C &c, P p)
Definition: stl_util.h:336
map2OwnerTrust
static Key::OwnerTrust map2OwnerTrust(const QString &s)
Definition: kconfigbasedkeyfilter.cpp:136
Kleo::KeyFilter::FontDescription::FontDescription
FontDescription()
Definition: kconfigbasedkeyfilter.cpp:58
Kleo::KeyFilterImplBase::mItalic
bool mItalic
Definition: kconfigbasedkeyfilter.h:73
Kleo::KeyFilterImplBase::mStrikeOut
bool mStrikeOut
Definition: kconfigbasedkeyfilter.h:75
Kleo::KeyFilter::MatchContext
MatchContext
Definition: keyfilter.h:58
Kleo::KeyFilterImplBase::matches
bool matches(const GpgME::Key &key, MatchContexts ctx) const
Definition: kconfigbasedkeyfilter.cpp:287
Kleo::KeyFilterImplBase::LevelState
LevelState
Definition: kconfigbasedkeyfilter.h:97
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