certmanager/lib

kconfigbasedkeyfilter.cpp

00001 /*
00002     kconfigbasedkeyfilter.cpp
00003 
00004     This file is part of libkleopatra, the KDE keymanagement library
00005     Copyright (c) 2004 Klarälvdalens Datakonsult AB
00006 
00007     Libkleopatra is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU General Public License as
00009     published by the Free Software Foundation; either version 2 of the
00010     License, or (at your option) any later version.
00011 
00012     Libkleopatra is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021     In addition, as a special exception, the copyright holders give
00022     permission to link the code of this program with any edition of
00023     the Qt library by Trolltech AS, Norway (or with modified versions
00024     of Qt that use the same license as Qt), and distribute linked
00025     combinations including the two.  You must obey the GNU General
00026     Public License in all respects for all of the code used other than
00027     Qt.  If you modify this file, you may extend this exception to
00028     your version of the file, but you are not obligated to do so.  If
00029     you do not wish to do so, delete this exception statement from
00030     your version.
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     mHasSecret( DoesNotMatter ),
00086     mIsOpenPGP( DoesNotMatter ),
00087     mWasValidated( DoesNotMatter ),
00088     mOwnerTrust( LevelDoesNotMatter ),
00089     mOwnerTrustReferenceLevel( GpgME::Key::Unknown ),
00090     mValidity( LevelDoesNotMatter ),
00091     mValidityReferenceLevel( GpgME::UserID::Unknown )
00092 {
00093   mFgColor = config.readColorEntry( "foreground-color" );
00094   mBgColor = config.readColorEntry( "background-color" );
00095   mName = config.readEntry( "name", i18n("<unnamed>") );
00096   mIcon = config.readEntry( "icon" );
00097   if ( config.hasKey( "font" ) ) {
00098     mUseFullFont = true;
00099     mFont = config.readFontEntry( "font" );
00100   } else {
00101     mItalic = config.readBoolEntry( "font-italic", false );
00102     mBold = config.readBoolEntry( "font-bold", false );
00103   }
00104   mStrikeOut = config.readBoolEntry( "font-strikeout", false );
00105 #ifdef SET
00106 #undef SET
00107 #endif
00108 #define SET(member,key) \
00109   if ( config.hasKey( key ) ) { \
00110     member = config.readBoolEntry( key ) ? Set : NotSet ; \
00111     ++mSpecificity; \
00112   }
00113   SET( mRevoked, "is-revoked" );
00114   SET( mExpired, "is-expired" );
00115   SET( mDisabled, "is-disabled" );
00116   SET( mRoot, "is-root-certificate" );
00117   SET( mCanEncrypt, "can-encrypt" );
00118   SET( mCanSign, "can-sign" );
00119   SET( mCanCertify, "can-certify" );
00120   SET( mCanAuthenticate, "can-authenticate" );
00121   SET( mHasSecret, "has-secret-key" );
00122   SET( mIsOpenPGP, "is-openpgp-key" );
00123   SET( mWasValidated, "was-validated" );
00124 #undef SET
00125   static const struct {
00126     const char * prefix;
00127     LevelState state;
00128   } prefixMap[] = {
00129     { "is-", Is },
00130     { "is-not-", IsNot },
00131     { "is-at-least-", IsAtLeast },
00132     { "is-at-most-", IsAtMost },
00133   };
00134   for ( unsigned int i = 0 ; i < sizeof prefixMap / sizeof *prefixMap ; ++i ) {
00135     const QString key = QString( prefixMap[i].prefix ) + "ownertrust";
00136     if ( config.hasKey( key ) ) {
00137       mOwnerTrust = prefixMap[i].state;
00138       mOwnerTrustReferenceLevel = map2OwnerTrust( config.readEntry( key ) );
00139       ++mSpecificity;
00140       break;
00141     }
00142   }
00143   for ( unsigned int i = 0 ; i < sizeof prefixMap / sizeof *prefixMap ; ++i ) {
00144     const QString key = QString( prefixMap[i].prefix ) + "validity";
00145     if ( config.hasKey( key ) ) {
00146       mValidity = prefixMap[i].state;
00147       mValidityReferenceLevel = map2Validity( config.readEntry( key ) );
00148       ++mSpecificity;
00149       break;
00150     }
00151   }
00152 }
00153 
00154 Kleo::KConfigBasedKeyFilter::~KConfigBasedKeyFilter() {
00155 
00156 }
00157 
00158 bool Kleo::KConfigBasedKeyFilter::matches( const GpgME::Key & key ) const {
00159 #ifdef MATCH
00160 #undef MATCH
00161 #endif
00162 #define MATCH(member,method) \
00163   if ( member != DoesNotMatter && key.method() != bool( member == Set ) ) \
00164     return false
00165 #define IS_MATCH(what) MATCH( m##what, is##what )
00166 #define CAN_MATCH(what) MATCH( mCan##what, can##what )
00167   IS_MATCH( Revoked );
00168   IS_MATCH( Expired );
00169   IS_MATCH( Disabled );
00170   IS_MATCH( Root );
00171   CAN_MATCH( Encrypt );
00172   CAN_MATCH( Sign );
00173   CAN_MATCH( Certify );
00174   CAN_MATCH( Authenticate );
00175   MATCH( mHasSecret, isSecret );
00176 #undef MATCH
00177   if ( mIsOpenPGP != DoesNotMatter &&
00178        bool( key.protocol() == GpgME::Context::OpenPGP ) != bool( mIsOpenPGP == Set ) )
00179     return false;
00180   if ( mWasValidated != DoesNotMatter &&
00181        bool( key.keyListMode() & GpgME::Context::Validate ) != bool( mWasValidated == Set ) )
00182     return false;
00183   switch ( mOwnerTrust ) {
00184   default:
00185   case LevelDoesNotMatter:
00186     break;
00187   case Is:
00188     if ( key.ownerTrust() != mOwnerTrustReferenceLevel )
00189       return false;
00190     break;
00191   case IsNot:
00192     if ( key.ownerTrust() == mOwnerTrustReferenceLevel )
00193       return false;
00194     break;
00195   case IsAtLeast:
00196     if ( (int)key.ownerTrust() < (int)mOwnerTrustReferenceLevel )
00197       return false;
00198     break;
00199   case IsAtMost:
00200     if ( (int)key.ownerTrust() > (int)mOwnerTrustReferenceLevel )
00201       return false;
00202     break;
00203   }
00204   const GpgME::UserID uid = key.userID(0);
00205   switch ( mValidity ) {
00206   default:
00207   case LevelDoesNotMatter:
00208     break;
00209   case Is:
00210     if ( uid.validity() != mValidityReferenceLevel )
00211       return false;
00212     break;
00213   case IsNot:
00214     if ( uid.validity() == mValidityReferenceLevel )
00215       return false;
00216     break;
00217   case IsAtLeast:
00218     if ( (int)uid.validity() < (int)mValidityReferenceLevel )
00219       return false;
00220     break;
00221   case IsAtMost:
00222     if ( (int)uid.validity() > (int)mValidityReferenceLevel )
00223       return false;
00224     break;
00225   }
00226   return true;
00227 }
00228 
00229 static inline QFont resizedFont( QFont font, int pointSize, bool strike ) {
00230   font.setPointSize( pointSize );
00231   if ( strike )
00232     font.setStrikeOut( true );
00233   return font;
00234 }
00235 
00236 static inline QFont adapt( QFont font, bool it, bool b, bool strike ) {
00237   if ( it )
00238     font.setItalic( true );
00239   if ( b )
00240     font.setBold( true );
00241   if ( strike )
00242     font.setStrikeOut( true );
00243   return font;
00244 }
00245 
00246 QFont Kleo::KConfigBasedKeyFilter::font( const QFont & f ) const {
00247   if ( mUseFullFont )
00248     return resizedFont( mFont, f.pointSize(), mStrikeOut );
00249   else
00250     return adapt( f, mItalic, mBold, mStrikeOut );
00251 }
KDE Home | KDE Accessibility Home | Description of Access Keys