00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifdef HAVE_CONFIG_H
00034 #include <config.h>
00035 #endif
00036
00037 #include "kconfigbasedkeyfilter.h"
00038
00039 #include <kconfigbase.h>
00040 #include <klocale.h>
00041
00042 static const struct {
00043 const char * name;
00044 GpgME::Key::OwnerTrust trust;
00045 GpgME::UserID::Validity validity;
00046 } ownerTrustAndValidityMap[] = {
00047 { "unknown", GpgME::Key::Unknown, GpgME::UserID::Unknown },
00048 { "undefined", GpgME::Key::Undefined, GpgME::UserID::Undefined },
00049 { "never", GpgME::Key::Never, GpgME::UserID::Never },
00050 { "marginal", GpgME::Key::Marginal, GpgME::UserID::Marginal },
00051 { "full", GpgME::Key::Full, GpgME::UserID::Full },
00052 { "ultimate", GpgME::Key::Ultimate, GpgME::UserID::Ultimate },
00053 };
00054
00055 static GpgME::Key::OwnerTrust map2OwnerTrust( const QString & s ) {
00056 for ( unsigned int i = 0 ; i < sizeof ownerTrustAndValidityMap / sizeof *ownerTrustAndValidityMap ; ++i )
00057 if ( s.lower() == ownerTrustAndValidityMap[i].name )
00058 return ownerTrustAndValidityMap[i].trust;
00059 return ownerTrustAndValidityMap[0].trust;
00060 }
00061
00062 static GpgME::UserID::Validity map2Validity( const QString & s ) {
00063 for ( unsigned int i = 0 ; i < sizeof ownerTrustAndValidityMap / sizeof *ownerTrustAndValidityMap ; ++i )
00064 if ( s.lower() == ownerTrustAndValidityMap[i].name )
00065 return ownerTrustAndValidityMap[i].validity;
00066 return ownerTrustAndValidityMap[0].validity;
00067 }
00068
00069
00070 Kleo::KConfigBasedKeyFilter::KConfigBasedKeyFilter( const KConfigBase & config )
00071 : KeyFilter(),
00072 mSpecificity( 0 ),
00073 mItalic( false ),
00074 mBold( false ),
00075 mStrikeOut( false ),
00076 mUseFullFont( false ),
00077 mRevoked( DoesNotMatter ),
00078 mExpired( DoesNotMatter ),
00079 mDisabled( DoesNotMatter ),
00080 mRoot( DoesNotMatter ),
00081 mCanEncrypt( DoesNotMatter ),
00082 mCanSign( DoesNotMatter ),
00083 mCanCertify( DoesNotMatter ),
00084 mCanAuthenticate( DoesNotMatter ),
00085 mQualified( DoesNotMatter ),
00086 mHasSecret( DoesNotMatter ),
00087 mIsOpenPGP( DoesNotMatter ),
00088 mWasValidated( DoesNotMatter ),
00089 mOwnerTrust( LevelDoesNotMatter ),
00090 mOwnerTrustReferenceLevel( GpgME::Key::Unknown ),
00091 mValidity( LevelDoesNotMatter ),
00092 mValidityReferenceLevel( GpgME::UserID::Unknown )
00093 {
00094 mFgColor = config.readColorEntry( "foreground-color" );
00095 mBgColor = config.readColorEntry( "background-color" );
00096 mName = config.readEntry( "name", i18n("<unnamed>") );
00097 mIcon = config.readEntry( "icon" );
00098 if ( config.hasKey( "font" ) ) {
00099 mUseFullFont = true;
00100 mFont = config.readFontEntry( "font" );
00101 } else {
00102 mItalic = config.readBoolEntry( "font-italic", false );
00103 mBold = config.readBoolEntry( "font-bold", false );
00104 }
00105 mStrikeOut = config.readBoolEntry( "font-strikeout", false );
00106 #ifdef SET
00107 #undef SET
00108 #endif
00109 #define SET(member,key) \
00110 if ( config.hasKey( key ) ) { \
00111 member = config.readBoolEntry( key ) ? Set : NotSet ; \
00112 ++mSpecificity; \
00113 }
00114 SET( mRevoked, "is-revoked" );
00115 SET( mExpired, "is-expired" );
00116 SET( mDisabled, "is-disabled" );
00117 SET( mRoot, "is-root-certificate" );
00118 SET( mCanEncrypt, "can-encrypt" );
00119 SET( mCanSign, "can-sign" );
00120 SET( mCanCertify, "can-certify" );
00121 SET( mCanAuthenticate, "can-authenticate" );
00122 SET( mQualified, "is-qualified" );
00123 SET( mHasSecret, "has-secret-key" );
00124 SET( mIsOpenPGP, "is-openpgp-key" );
00125 SET( mWasValidated, "was-validated" );
00126 #undef SET
00127 static const struct {
00128 const char * prefix;
00129 LevelState state;
00130 } prefixMap[] = {
00131 { "is-", Is },
00132 { "is-not-", IsNot },
00133 { "is-at-least-", IsAtLeast },
00134 { "is-at-most-", IsAtMost },
00135 };
00136 for ( unsigned int i = 0 ; i < sizeof prefixMap / sizeof *prefixMap ; ++i ) {
00137 const QString key = QString( prefixMap[i].prefix ) + "ownertrust";
00138 if ( config.hasKey( key ) ) {
00139 mOwnerTrust = prefixMap[i].state;
00140 mOwnerTrustReferenceLevel = map2OwnerTrust( config.readEntry( key ) );
00141 ++mSpecificity;
00142 break;
00143 }
00144 }
00145 for ( unsigned int i = 0 ; i < sizeof prefixMap / sizeof *prefixMap ; ++i ) {
00146 const QString key = QString( prefixMap[i].prefix ) + "validity";
00147 if ( config.hasKey( key ) ) {
00148 mValidity = prefixMap[i].state;
00149 mValidityReferenceLevel = map2Validity( config.readEntry( key ) );
00150 ++mSpecificity;
00151 break;
00152 }
00153 }
00154 }
00155
00156 Kleo::KConfigBasedKeyFilter::~KConfigBasedKeyFilter() {
00157
00158 }
00159
00160 bool Kleo::KConfigBasedKeyFilter::matches( const GpgME::Key & key ) const {
00161 #ifdef MATCH
00162 #undef MATCH
00163 #endif
00164 #define MATCH(member,method) \
00165 if ( member != DoesNotMatter && key.method() != bool( member == Set ) ) \
00166 return false
00167 #define IS_MATCH(what) MATCH( m##what, is##what )
00168 #define CAN_MATCH(what) MATCH( mCan##what, can##what )
00169 IS_MATCH( Revoked );
00170 IS_MATCH( Expired );
00171 IS_MATCH( Disabled );
00172 IS_MATCH( Root );
00173 CAN_MATCH( Encrypt );
00174 CAN_MATCH( Sign );
00175 CAN_MATCH( Certify );
00176 CAN_MATCH( Authenticate );
00177 IS_MATCH( Qualified );
00178 MATCH( mHasSecret, isSecret );
00179 #undef MATCH
00180 if ( mIsOpenPGP != DoesNotMatter &&
00181 bool( key.protocol() == GpgME::Context::OpenPGP ) != bool( mIsOpenPGP == Set ) )
00182 return false;
00183 if ( mWasValidated != DoesNotMatter &&
00184 bool( key.keyListMode() & GpgME::Context::Validate ) != bool( mWasValidated == Set ) )
00185 return false;
00186 switch ( mOwnerTrust ) {
00187 default:
00188 case LevelDoesNotMatter:
00189 break;
00190 case Is:
00191 if ( key.ownerTrust() != mOwnerTrustReferenceLevel )
00192 return false;
00193 break;
00194 case IsNot:
00195 if ( key.ownerTrust() == mOwnerTrustReferenceLevel )
00196 return false;
00197 break;
00198 case IsAtLeast:
00199 if ( (int)key.ownerTrust() < (int)mOwnerTrustReferenceLevel )
00200 return false;
00201 break;
00202 case IsAtMost:
00203 if ( (int)key.ownerTrust() > (int)mOwnerTrustReferenceLevel )
00204 return false;
00205 break;
00206 }
00207 const GpgME::UserID uid = key.userID(0);
00208 switch ( mValidity ) {
00209 default:
00210 case LevelDoesNotMatter:
00211 break;
00212 case Is:
00213 if ( uid.validity() != mValidityReferenceLevel )
00214 return false;
00215 break;
00216 case IsNot:
00217 if ( uid.validity() == mValidityReferenceLevel )
00218 return false;
00219 break;
00220 case IsAtLeast:
00221 if ( (int)uid.validity() < (int)mValidityReferenceLevel )
00222 return false;
00223 break;
00224 case IsAtMost:
00225 if ( (int)uid.validity() > (int)mValidityReferenceLevel )
00226 return false;
00227 break;
00228 }
00229 return true;
00230 }
00231
00232 static inline QFont resizedFont( QFont font, int pointSize, bool strike ) {
00233 font.setPointSize( pointSize );
00234 if ( strike )
00235 font.setStrikeOut( true );
00236 return font;
00237 }
00238
00239 static inline QFont adapt( QFont font, bool it, bool b, bool strike ) {
00240 if ( it )
00241 font.setItalic( true );
00242 if ( b )
00243 font.setBold( true );
00244 if ( strike )
00245 font.setStrikeOut( true );
00246 return font;
00247 }
00248
00249 QFont Kleo::KConfigBasedKeyFilter::font( const QFont & f ) const {
00250 if ( mUseFullFont )
00251 return resizedFont( mFont, f.pointSize(), mStrikeOut );
00252 else
00253 return adapt( f, mItalic, mBold, mStrikeOut );
00254 }