certmanager/lib

keyfiltermanager.cpp
1/*
2 keyfiltermanager.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 "keyfiltermanager.h"
38#include "tdeconfigbasedkeyfilter.h"
39
40#include "cryptobackendfactory.h"
41
42#include <tdeconfig.h>
43
44#include <tqapplication.h>
45#include <tqregexp.h>
46#include <tqstringlist.h>
47#include <tqvaluevector.h>
48
49#include <algorithm>
50
51namespace {
52 template <typename T>
53 struct Delete {
54 void operator()( T * item ) { delete item; }
55 };
56}
57
58struct Kleo::KeyFilterManager::Private {
59 void clear() {
60 std::for_each( filters.begin(), filters.end(), Delete<KeyFilter>() );
61 filters.clear();
62 }
63
64 TQValueVector<KeyFilter*> filters;
65};
66
67Kleo::KeyFilterManager * Kleo::KeyFilterManager::mSelf = 0;
68
69Kleo::KeyFilterManager::KeyFilterManager( TQObject * parent, const char * name )
70 : TQObject( parent, name ), d( 0 )
71{
72 mSelf = this;
73 d = new Private();
74 // ### DF: doesn't a KStaticDeleter work more reliably?
75 if ( tqApp )
76 connect( tqApp, TQ_SIGNAL(aboutToQuit()), TQ_SLOT(deleteLater()) );
77 reload();
78}
79
80Kleo::KeyFilterManager::~KeyFilterManager() {
81 mSelf = 0;
82 if ( d )
83 d->clear();
84 delete d; d = 0;
85}
86
87Kleo::KeyFilterManager * Kleo::KeyFilterManager::instance() {
88 if ( !mSelf )
89 mSelf = new Kleo::KeyFilterManager();
90 return mSelf;
91}
92
93const Kleo::KeyFilter * Kleo::KeyFilterManager::filterMatching( const GpgME::Key & key ) const {
94 for ( TQValueVector<KeyFilter*>::const_iterator it = d->filters.begin() ; it != d->filters.end() ; ++it )
95 if ( (*it)->matches( key ) )
96 return *it;
97 return 0;
98}
99
100static inline bool by_increasing_specificity( const Kleo::KeyFilter * left, const Kleo::KeyFilter * right ) {
101 return left->specificity() > right->specificity();
102}
103
104void Kleo::KeyFilterManager::reload() {
105 d->clear();
106
107 TDEConfig * config = Kleo::CryptoBackendFactory::instance()->configObject();
108 if ( !config )
109 return;
110 const TQStringList groups = config->groupList().grep( TQRegExp( "^Key Filter #\\d+$" ) );
111 for ( TQStringList::const_iterator it = groups.begin() ; it != groups.end() ; ++it ) {
112 const TDEConfigGroup cfg( config, *it );
113 d->filters.push_back( new TDEConfigBasedKeyFilter( cfg ) );
114 }
115 std::stable_sort( d->filters.begin(), d->filters.end(), by_increasing_specificity );
116}
117
118#include "keyfiltermanager.moc"
An abstract base class key filters.
Definition: keyfilter.h:50