certmanager/lib
keyfiltermanager.cpp00001
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 "keyfiltermanager.h"
00038 #include "kconfigbasedkeyfilter.h"
00039
00040 #include "cryptobackendfactory.h"
00041
00042 #include <kconfig.h>
00043
00044 #include <qapplication.h>
00045 #include <qregexp.h>
00046 #include <qstringlist.h>
00047 #include <qvaluevector.h>
00048
00049 #include <algorithm>
00050
00051 namespace {
00052 template <typename T>
00053 struct Delete {
00054 void operator()( T * item ) { delete item; }
00055 };
00056 }
00057
00058 struct Kleo::KeyFilterManager::Private {
00059 void clear() {
00060 std::for_each( filters.begin(), filters.end(), Delete<KeyFilter>() );
00061 filters.clear();
00062 }
00063
00064 QValueVector<KeyFilter*> filters;
00065 };
00066
00067 Kleo::KeyFilterManager * Kleo::KeyFilterManager::mSelf = 0;
00068
00069 Kleo::KeyFilterManager::KeyFilterManager( QObject * parent, const char * name )
00070 : QObject( parent, name ), d( 0 )
00071 {
00072 mSelf = this;
00073 d = new Private();
00074
00075 if ( qApp )
00076 connect( qApp, SIGNAL(aboutToQuit()), SLOT(deleteLater()) );
00077 reload();
00078 }
00079
00080 Kleo::KeyFilterManager::~KeyFilterManager() {
00081 mSelf = 0;
00082 if ( d )
00083 d->clear();
00084 delete d; d = 0;
00085 }
00086
00087 Kleo::KeyFilterManager * Kleo::KeyFilterManager::instance() {
00088 if ( !mSelf )
00089 mSelf = new Kleo::KeyFilterManager();
00090 return mSelf;
00091 }
00092
00093 const Kleo::KeyFilter * Kleo::KeyFilterManager::filterMatching( const GpgME::Key & key ) const {
00094 for ( QValueVector<KeyFilter*>::const_iterator it = d->filters.begin() ; it != d->filters.end() ; ++it )
00095 if ( (*it)->matches( key ) )
00096 return *it;
00097 return 0;
00098 }
00099
00100 static inline bool by_increasing_specificity( const Kleo::KeyFilter * left, const Kleo::KeyFilter * right ) {
00101 return left->specificity() > right->specificity();
00102 }
00103
00104 void Kleo::KeyFilterManager::reload() {
00105 d->clear();
00106
00107 KConfig * config = Kleo::CryptoBackendFactory::instance()->configObject();
00108 if ( !config )
00109 return;
00110 const QStringList groups = config->groupList().grep( QRegExp( "^Key Filter #\\d+$" ) );
00111 for ( QStringList::const_iterator it = groups.begin() ; it != groups.end() ; ++it ) {
00112 const KConfigGroup cfg( config, *it );
00113 d->filters.push_back( new KConfigBasedKeyFilter( cfg ) );
00114 }
00115 std::stable_sort( d->filters.begin(), d->filters.end(), by_increasing_specificity );
00116 }
00117
00118 #include "keyfiltermanager.moc"
|