certmanager/lib

tdeconfigbasedkeyfilter.cpp
1/*
2 tdeconfigbasedkeyfilter.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 TQt library by Trolltech AS, Norway (or with modified versions
24 of TQt that use the same license as TQt), 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 TQt. 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#ifdef HAVE_CONFIG_H
34#include <config.h>
35#endif
36
37#include "tdeconfigbasedkeyfilter.h"
38
39#include <tdeconfigbase.h>
40#include <tdelocale.h>
41
42static const struct {
43 const char * name;
44 GpgME::Key::OwnerTrust trust;
45 GpgME::UserID::Validity validity;
46} ownerTrustAndValidityMap[] = {
47 { "unknown", GpgME::Key::Unknown, GpgME::UserID::Unknown },
48 { "undefined", GpgME::Key::Undefined, GpgME::UserID::Undefined },
49 { "never", GpgME::Key::Never, GpgME::UserID::Never },
50 { "marginal", GpgME::Key::Marginal, GpgME::UserID::Marginal },
51 { "full", GpgME::Key::Full, GpgME::UserID::Full },
52 { "ultimate", GpgME::Key::Ultimate, GpgME::UserID::Ultimate },
53};
54
55static GpgME::Key::OwnerTrust map2OwnerTrust( const TQString & s ) {
56 for ( unsigned int i = 0 ; i < sizeof ownerTrustAndValidityMap / sizeof *ownerTrustAndValidityMap ; ++i )
57 if ( s.lower() == ownerTrustAndValidityMap[i].name )
58 return ownerTrustAndValidityMap[i].trust;
59 return ownerTrustAndValidityMap[0].trust;
60}
61
62static GpgME::UserID::Validity map2Validity( const TQString & s ) {
63 for ( unsigned int i = 0 ; i < sizeof ownerTrustAndValidityMap / sizeof *ownerTrustAndValidityMap ; ++i )
64 if ( s.lower() == ownerTrustAndValidityMap[i].name )
65 return ownerTrustAndValidityMap[i].validity;
66 return ownerTrustAndValidityMap[0].validity;
67}
68
69
70Kleo::TDEConfigBasedKeyFilter::TDEConfigBasedKeyFilter( const TDEConfigBase & config )
71 : KeyFilter(),
72 mSpecificity( 0 ),
73 mItalic( false ),
74 mBold( false ),
75 mStrikeOut( false ),
76 mUseFullFont( false ),
77 mRevoked( DoesNotMatter ),
78 mExpired( DoesNotMatter ),
79 mDisabled( DoesNotMatter ),
80 mRoot( DoesNotMatter ),
81 mCanEncrypt( DoesNotMatter ),
82 mCanSign( DoesNotMatter ),
83 mCanCertify( DoesNotMatter ),
84 mCanAuthenticate( DoesNotMatter ),
85 mHasSecret( DoesNotMatter ),
86 mIsOpenPGP( DoesNotMatter ),
87 mWasValidated( DoesNotMatter ),
88 mOwnerTrust( LevelDoesNotMatter ),
89 mOwnerTrustReferenceLevel( GpgME::Key::Unknown ),
90 mValidity( LevelDoesNotMatter ),
91 mValidityReferenceLevel( GpgME::UserID::Unknown )
92{
93 mFgColor = config.readColorEntry( "foreground-color" );
94 mBgColor = config.readColorEntry( "background-color" );
95 mName = config.readEntry( "name", i18n("<unnamed>") );
96 mIcon = config.readEntry( "icon" );
97 if ( config.hasKey( "font" ) ) {
98 mUseFullFont = true;
99 mFont = config.readFontEntry( "font" );
100 } else {
101 mItalic = config.readBoolEntry( "font-italic", false );
102 mBold = config.readBoolEntry( "font-bold", false );
103 }
104 mStrikeOut = config.readBoolEntry( "font-strikeout", false );
105#ifdef SET
106#undef SET
107#endif
108#define SET(member,key) \
109 if ( config.hasKey( key ) ) { \
110 member = config.readBoolEntry( key ) ? Set : NotSet ; \
111 ++mSpecificity; \
112 }
113 SET( mRevoked, "is-revoked" );
114 SET( mExpired, "is-expired" );
115 SET( mDisabled, "is-disabled" );
116 SET( mRoot, "is-root-certificate" );
117 SET( mCanEncrypt, "can-encrypt" );
118 SET( mCanSign, "can-sign" );
119 SET( mCanCertify, "can-certify" );
120 SET( mCanAuthenticate, "can-authenticate" );
121 SET( mHasSecret, "has-secret-key" );
122 SET( mIsOpenPGP, "is-openpgp-key" );
123 SET( mWasValidated, "was-validated" );
124#undef SET
125 static const struct {
126 const char * prefix;
127 LevelState state;
128 } prefixMap[] = {
129 { "is-", Is },
130 { "is-not-", IsNot },
131 { "is-at-least-", IsAtLeast },
132 { "is-at-most-", IsAtMost },
133 };
134 for ( unsigned int i = 0 ; i < sizeof prefixMap / sizeof *prefixMap ; ++i ) {
135 const TQString key = TQString( prefixMap[i].prefix ) + "ownertrust";
136 if ( config.hasKey( key ) ) {
137 mOwnerTrust = prefixMap[i].state;
138 mOwnerTrustReferenceLevel = map2OwnerTrust( config.readEntry( key ) );
139 ++mSpecificity;
140 break;
141 }
142 }
143 for ( unsigned int i = 0 ; i < sizeof prefixMap / sizeof *prefixMap ; ++i ) {
144 const TQString key = TQString( prefixMap[i].prefix ) + "validity";
145 if ( config.hasKey( key ) ) {
146 mValidity = prefixMap[i].state;
147 mValidityReferenceLevel = map2Validity( config.readEntry( key ) );
148 ++mSpecificity;
149 break;
150 }
151 }
152}
153
154Kleo::TDEConfigBasedKeyFilter::~TDEConfigBasedKeyFilter() {
155
156}
157
158bool Kleo::TDEConfigBasedKeyFilter::matches( const GpgME::Key & key ) const {
159#ifdef MATCH
160#undef MATCH
161#endif
162#define MATCH(member,method) \
163 if ( member != DoesNotMatter && key.method() != bool( member == Set ) ) \
164 return false
165#define IS_MATCH(what) MATCH( m##what, is##what )
166#define CAN_MATCH(what) MATCH( mCan##what, can##what )
167 IS_MATCH( Revoked );
168 IS_MATCH( Expired );
169 IS_MATCH( Disabled );
170 IS_MATCH( Root );
171 CAN_MATCH( Encrypt );
172 CAN_MATCH( Sign );
173 CAN_MATCH( Certify );
174 CAN_MATCH( Authenticate );
175 MATCH( mHasSecret, isSecret );
176#undef MATCH
177 if ( mIsOpenPGP != DoesNotMatter &&
178 bool( key.protocol() == GpgME::Context::OpenPGP ) != bool( mIsOpenPGP == Set ) )
179 return false;
180 if ( mWasValidated != DoesNotMatter &&
181 bool( key.keyListMode() & GpgME::Context::Validate ) != bool( mWasValidated == Set ) )
182 return false;
183 switch ( mOwnerTrust ) {
184 default:
185 case LevelDoesNotMatter:
186 break;
187 case Is:
188 if ( key.ownerTrust() != mOwnerTrustReferenceLevel )
189 return false;
190 break;
191 case IsNot:
192 if ( key.ownerTrust() == mOwnerTrustReferenceLevel )
193 return false;
194 break;
195 case IsAtLeast:
196 if ( (int)key.ownerTrust() < (int)mOwnerTrustReferenceLevel )
197 return false;
198 break;
199 case IsAtMost:
200 if ( (int)key.ownerTrust() > (int)mOwnerTrustReferenceLevel )
201 return false;
202 break;
203 }
204 const GpgME::UserID uid = key.userID(0);
205 switch ( mValidity ) {
206 default:
207 case LevelDoesNotMatter:
208 break;
209 case Is:
210 if ( uid.validity() != mValidityReferenceLevel )
211 return false;
212 break;
213 case IsNot:
214 if ( uid.validity() == mValidityReferenceLevel )
215 return false;
216 break;
217 case IsAtLeast:
218 if ( (int)uid.validity() < (int)mValidityReferenceLevel )
219 return false;
220 break;
221 case IsAtMost:
222 if ( (int)uid.validity() > (int)mValidityReferenceLevel )
223 return false;
224 break;
225 }
226 return true;
227}
228
229static inline TQFont resizedFont( TQFont font, int pointSize, bool strike ) {
230 font.setPointSize( pointSize );
231 if ( strike )
232 font.setStrikeOut( true );
233 return font;
234}
235
236static inline TQFont adapt( TQFont font, bool it, bool b, bool strike ) {
237 if ( it )
238 font.setItalic( true );
239 if ( b )
240 font.setBold( true );
241 if ( strike )
242 font.setStrikeOut( true );
243 return font;
244}
245
246TQFont Kleo::TDEConfigBasedKeyFilter::font( const TQFont & f ) const {
247 if ( mUseFullFont )
248 return resizedFont( mFont, f.pointSize(), mStrikeOut );
249 else
250 return adapt( f, mItalic, mBold, mStrikeOut );
251}