certmanager/lib

keyfiltermanager.cpp

00001 /*
00002     keyfiltermanager.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 "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   // ### DF: doesn't a KStaticDeleter work more reliably?
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"
KDE Home | KDE Accessibility Home | Description of Access Keys