certmanager/lib Library API Documentation

cryptobackendfactory.cpp

00001 /*
00002     cryptobackendfactory.cpp
00003 
00004     This file is part of libkleopatra, the KDE key management library
00005     Copyright (c) 2001,2004,2005 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 "cryptobackendfactory.h"
00038 
00039 #include <backends/qgpgme/qgpgmebackend.h>
00040 #if 0 // disabled for kde-3.3
00041 #include <backends/kpgp/pgp2backend.h>
00042 #include <backends/kpgp/pgp5backend.h>
00043 #include <backends/kpgp/pgp6backend.h>
00044 #include <backends/kpgp/gpg1backend.h>
00045 #endif
00046 #ifdef KLEO_CHIASMUS
00047 #include <backends/chiasmus/chiasmusbackend.h>
00048 #endif
00049 #include <ui/backendconfigwidget.h>
00050 
00051 #include <kconfig.h>
00052 #include <klocale.h>
00053 #include <kdebug.h>
00054 #include <kmessagebox.h>
00055 #include <kapplication.h>
00056 
00057 #include <iterator>
00058 #include <algorithm>
00059 
00060 #include <cassert>
00061 
00062 Kleo::CryptoBackendFactory * Kleo::CryptoBackendFactory::mSelf = 0;
00063 
00064 static const char * availableProtocols[] = {
00065 #ifdef KLEO_CHIASMUS
00066   "Chiasmus",
00067 #endif
00068   "OpenPGP", "SMIME",
00069 };
00070 static const unsigned int numAvailableProtocols = sizeof availableProtocols / sizeof *availableProtocols;
00071 
00072 Kleo::CryptoBackendFactory::CryptoBackendFactory()
00073   : QObject( qApp, "CryptoBackendFactory::instance()" ),
00074     mConfigObject( 0 ),
00075     mAvailableProtocols( availableProtocols, availableProtocols + numAvailableProtocols )
00076 {
00077   mBackendList.push_back( new QGpgMEBackend() );
00078 #if 0 // disabled for kde-3.3
00079   mBackendList.push_back( new PGP2Backend() );
00080   mBackendList.push_back( new PGP5Backend() );
00081   mBackendList.push_back( new PGP6Backend() );
00082   mBackendList.push_back( new GPG1Backend() );
00083 #endif
00084 #ifdef KLEO_CHIASMUS
00085   mBackendList.push_back( new ChiasmusBackend() );
00086 #endif
00087   scanForBackends();
00088   readConfig();
00089 
00090   mSelf = this; // last!
00091 }
00092 
00093 Kleo::CryptoBackendFactory::~CryptoBackendFactory() {
00094   mSelf = 0; // first!
00095 
00096   for ( std::vector<CryptoBackend*>::iterator it = mBackendList.begin() ; it != mBackendList.end() ; ++it ) {
00097     delete *it;
00098     *it = 0;
00099   }
00100   delete mConfigObject;
00101   mConfigObject = 0;
00102 }
00103 
00104 Kleo::CryptoBackendFactory * Kleo::CryptoBackendFactory::instance() {
00105   if ( !mSelf )
00106     mSelf = new CryptoBackendFactory();
00107   return mSelf;
00108 }
00109 
00110 
00111 // const Kleo::CryptoBackend* Kleo::CryptoBackendFactory::smimeBackend() const {
00112 //   return mSMIMEBackend;
00113 // }
00114 
00115 // const Kleo::CryptoBackend* Kleo::CryptoBackendFactory::openpgpBackend() const {
00116 //   return mOpenPGPBackend;
00117 // }
00118 
00119 const Kleo::CryptoBackend::Protocol * Kleo::CryptoBackendFactory::smime() const {
00120   const BackendMap::const_iterator it = mBackends.find( "SMIME" );
00121   if ( it == mBackends.end() )
00122     return 0;
00123   if ( !it->second )
00124     return 0;
00125   return it->second->smime();
00126 }
00127 
00128 const Kleo::CryptoBackend::Protocol * Kleo::CryptoBackendFactory::openpgp() const {
00129   const BackendMap::const_iterator it = mBackends.find( "OpenPGP" );
00130   if ( it == mBackends.end() )
00131     return 0;
00132   if ( !it->second )
00133     return 0;
00134   return it->second->openpgp();
00135 }
00136 
00137 const Kleo::CryptoBackend::Protocol * Kleo::CryptoBackendFactory::protocol( const char * name ) const {
00138   const BackendMap::const_iterator it = mBackends.find( name );
00139   if ( it == mBackends.end() )
00140     return 0;
00141   if ( !it->second )
00142     return 0;
00143   return it->second->protocol( name );
00144 }
00145 
00146 Kleo::CryptoConfig * Kleo::CryptoBackendFactory::config() const {
00147   // ## should we use mSMIMEBackend? mOpenPGPBackend? backend(0) i.e. always qgpgme?
00148   return backend( 0 ) ? backend( 0 )->config() : 0;
00149 }
00150 
00151 bool Kleo::CryptoBackendFactory::hasBackends() const {
00152   return !mBackendList.empty();
00153 }
00154 
00155 void Kleo::CryptoBackendFactory::scanForBackends( QStringList * reasons ) {
00156   for ( std::vector<CryptoBackend*>::const_iterator it = mBackendList.begin() ; it != mBackendList.end() ; ++it ) {
00157     assert( *it );
00158     for ( int i = 0 ; const char * protocol = (*it)->enumerateProtocols( i ) ; ++i ) {
00159       QString reason;
00160       if ( (*it)->supportsProtocol( protocol ) && !(*it)->checkForProtocol( protocol, &reason ) ) {
00161         if ( reasons ) {
00162           reasons->push_back( i18n("While scanning for %1 support in backend %2:")
00163                               .arg( protocol, (*it)->displayName() ) );
00164           reasons->push_back( "  " + reason );
00165         }
00166       }
00167     }
00168   }
00169 }
00170 
00171 const Kleo::CryptoBackend * Kleo::CryptoBackendFactory::backend( unsigned int idx ) const {
00172   return ( idx < mBackendList.size() ) ? mBackendList[idx] : 0 ;
00173 }
00174 
00175 const Kleo::CryptoBackend * Kleo::CryptoBackendFactory::backendByName( const QString& name ) const {
00176   for ( std::vector<CryptoBackend*>::const_iterator it = mBackendList.begin() ; it != mBackendList.end() ; ++it ) {
00177     if ( (*it)->name() == name )
00178       return *it;
00179   }
00180   return 0;
00181 }
00182 
00183 Kleo::BackendConfigWidget * Kleo::CryptoBackendFactory::configWidget( QWidget * parent, const char * name ) const {
00184   return new Kleo::BackendConfigWidget( mSelf, parent, name );
00185 }
00186 
00187 KConfig* Kleo::CryptoBackendFactory::configObject() const {
00188   if ( !mConfigObject )
00189     // this is unsafe. We're a lib, used by concurrent apps.
00190     mConfigObject = new KConfig( "libkleopatrarc" );
00191   return mConfigObject;
00192 }
00193 
00194 void Kleo::CryptoBackendFactory::setSMIMEBackend( const CryptoBackend* backend ) {
00195   setProtocolBackend( "SMIME", backend );
00196 }
00197 
00198 void Kleo::CryptoBackendFactory::setOpenPGPBackend( const CryptoBackend* backend ) {
00199   setProtocolBackend( "OpenPGP", backend );
00200 }
00201 
00202 void Kleo::CryptoBackendFactory::setProtocolBackend( const char * protocol, const CryptoBackend * backend ) {
00203   const QString name = backend ? backend->name() : QString::null ;
00204   KConfigGroup group( configObject(), "Backends" );
00205   group.writeEntry( protocol, name );
00206   configObject()->sync();
00207   mBackends[protocol] = backend;
00208 }
00209 
00210 static const char * defaultBackend( const char * proto ) {
00211   static const struct {
00212     const char * proto;
00213     const char * backend;
00214   } defaults[] = {
00215     { "OpenPGP", "gpgme" },
00216     { "SMIME", "gpgme" },
00217 #ifdef KLEO_CHIASMUS
00218     { "Chiasmus", "chiasmus" },
00219 #endif
00220   };
00221   for ( unsigned int i = 0 ; i < sizeof defaults / sizeof *defaults ; ++i )
00222     if ( qstricmp( proto, defaults[i].proto ) == 0 )
00223       return defaults[i].backend;
00224   return 0;
00225 }
00226 
00227 void Kleo::CryptoBackendFactory::readConfig() {
00228   mBackends.clear();
00229   const KConfigGroup group( configObject(), "Backends" );
00230   for ( ProtocolSet::const_iterator it = mAvailableProtocols.begin(), end = mAvailableProtocols.end() ; it != end ; ++it ) {
00231     const QString backend = group.readEntry( *it, defaultBackend( *it ) );
00232     mBackends[*it] = backendByName( backend );
00233   }
00234 }
00235 
00236 const char * Kleo::CryptoBackendFactory::enumerateProtocols( int i ) const {
00237   if ( i < 0 || static_cast<unsigned int>( i ) >= mAvailableProtocols.size() )
00238     return 0;
00239   return mAvailableProtocols[i];
00240 }
00241 
00242 namespace {
00243   class CaseInsensitiveString {
00244     const char * m;
00245   public:
00246     CaseInsensitiveString( const char * s ) : m( s ) {}
00247 #define make_operator( op ) \
00248     bool operator op( const CaseInsensitiveString & other ) const { \
00249       return qstricmp( m, other.m ) op 0; \
00250     } \
00251     bool operator op( const char * other ) const { \
00252       return qstricmp( m, other ) op 0; \
00253     }
00254     make_operator( == )
00255     make_operator( != )
00256     make_operator( < )
00257     make_operator( > )
00258     make_operator( <= )
00259     make_operator( >= )
00260 #undef make_operator
00261     operator const char *() const { return m; }
00262   };
00263 #define make_ext_operator( op, inv_op ) \
00264   inline bool operator op( const char * lhs, const CaseInsensitiveString & rhs ) { \
00265     return rhs.operator inv_op( lhs ); \
00266   }
00267   make_ext_operator( ==, == )
00268   make_ext_operator( !=, != )
00269   make_ext_operator( <, > )
00270   make_ext_operator( >, < )
00271   make_ext_operator( <=, >= )
00272   make_ext_operator( >=, <= )
00273 #undef make_ext_operator
00274 
00275 }
00276 
00277 bool Kleo::CryptoBackendFactory::knowsAboutProtocol( const char * name ) const {
00278   return std::find( mAvailableProtocols.begin(), mAvailableProtocols.end(),
00279                     CaseInsensitiveString( name ) ) != mAvailableProtocols.end();
00280 }
00281 
00282 #include "cryptobackendfactory.moc"
00283 
KDE Logo
This file is part of the documentation for certmanager/lib Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Oct 17 09:53:43 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003