37 #include "cryptobackendfactory.h"
39 #include <backends/qgpgme/qgpgmebackend.h>
41 #include <backends/kpgp/pgp2backend.h>
42 #include <backends/kpgp/pgp5backend.h>
43 #include <backends/kpgp/pgp6backend.h>
44 #include <backends/kpgp/gpg1backend.h>
46 #include <backends/chiasmus/chiasmusbackend.h>
47 #include <ui/backendconfigwidget.h>
49 #include <tdeconfig.h>
50 #include <tdelocale.h>
52 #include <tdemessagebox.h>
53 #include <tdeapplication.h>
60 Kleo::CryptoBackendFactory * Kleo::CryptoBackendFactory::mSelf = 0;
62 static const char * availableProtocols[] = {
66 static const unsigned int numAvailableProtocols =
sizeof availableProtocols /
sizeof *availableProtocols;
68 Kleo::CryptoBackendFactory::CryptoBackendFactory()
69 : TQObject( tqApp,
"CryptoBackendFactory::instance()" ),
71 mAvailableProtocols( availableProtocols, availableProtocols + numAvailableProtocols )
73 mBackendList.push_back(
new QGpgMEBackend() );
75 mBackendList.push_back(
new PGP2Backend() );
76 mBackendList.push_back(
new PGP5Backend() );
77 mBackendList.push_back(
new PGP6Backend() );
78 mBackendList.push_back(
new GPG1Backend() );
80 mBackendList.push_back(
new ChiasmusBackend() );
87 Kleo::CryptoBackendFactory::~CryptoBackendFactory() {
90 for ( std::vector<CryptoBackend*>::iterator it = mBackendList.begin() ; it != mBackendList.end() ; ++it ) {
98 Kleo::CryptoBackendFactory * Kleo::CryptoBackendFactory::instance() {
100 mSelf =
new CryptoBackendFactory();
113 const Kleo::CryptoBackend::Protocol * Kleo::CryptoBackendFactory::smime()
const {
114 const BackendMap::const_iterator it = mBackends.find(
"SMIME" );
115 if ( it == mBackends.end() )
119 return it->second->smime();
122 const Kleo::CryptoBackend::Protocol * Kleo::CryptoBackendFactory::openpgp()
const {
123 const BackendMap::const_iterator it = mBackends.find(
"OpenPGP" );
124 if ( it == mBackends.end() )
128 return it->second->openpgp();
131 const Kleo::CryptoBackend::Protocol * Kleo::CryptoBackendFactory::protocol(
const char * name )
const {
132 const BackendMap::const_iterator it = mBackends.find( name );
133 if ( it == mBackends.end() )
137 return it->second->protocol( name );
142 return backend( 0 ) ? backend( 0 )->config() : 0;
145 bool Kleo::CryptoBackendFactory::hasBackends()
const {
146 return !mBackendList.empty();
149 void Kleo::CryptoBackendFactory::scanForBackends( TQStringList * reasons ) {
150 for ( std::vector<CryptoBackend*>::const_iterator it = mBackendList.begin() ; it != mBackendList.end() ; ++it ) {
152 for (
int i = 0 ;
const char * protocol = (*it)->enumerateProtocols( i ) ; ++i ) {
154 if ( (*it)->supportsProtocol( protocol ) && !(*it)->checkForProtocol( protocol, &reason ) ) {
156 reasons->push_back( i18n(
"While scanning for %1 support in backend %2:")
157 .arg( protocol, (*it)->displayName() ) );
158 reasons->push_back(
" " + reason );
165 const Kleo::CryptoBackend * Kleo::CryptoBackendFactory::backend(
unsigned int idx )
const {
166 return ( idx < mBackendList.size() ) ? mBackendList[idx] : 0 ;
169 const Kleo::CryptoBackend * Kleo::CryptoBackendFactory::backendByName(
const TQString& name )
const {
170 for ( std::vector<CryptoBackend*>::const_iterator it = mBackendList.begin() ; it != mBackendList.end() ; ++it ) {
171 if ( (*it)->name() == name )
177 Kleo::BackendConfigWidget * Kleo::CryptoBackendFactory::configWidget( TQWidget * parent,
const char * name )
const {
178 return new Kleo::BackendConfigWidget( mSelf, parent, name );
181 TDEConfig* Kleo::CryptoBackendFactory::configObject()
const {
182 if ( !mConfigObject )
184 mConfigObject =
new TDEConfig(
"libkleopatrarc" );
185 return mConfigObject;
188 void Kleo::CryptoBackendFactory::setSMIMEBackend(
const CryptoBackend* backend ) {
189 setProtocolBackend(
"SMIME", backend );
192 void Kleo::CryptoBackendFactory::setOpenPGPBackend(
const CryptoBackend* backend ) {
193 setProtocolBackend(
"OpenPGP", backend );
196 void Kleo::CryptoBackendFactory::setProtocolBackend(
const char * protocol,
const CryptoBackend * backend ) {
197 const TQString name = backend ? backend->name() : TQString() ;
198 TDEConfigGroup group( configObject(),
"Backends" );
199 group.writeEntry( protocol, name );
200 configObject()->sync();
201 mBackends[protocol] = backend;
204 static const char * defaultBackend(
const char * proto ) {
205 static const struct {
207 const char * backend;
209 {
"OpenPGP",
"gpgme" },
210 {
"SMIME",
"gpgme" },
211 {
"Chiasmus",
"chiasmus" },
213 for (
unsigned int i = 0 ; i <
sizeof defaults /
sizeof *defaults ; ++i )
214 if ( tqstricmp( proto, defaults[i].proto ) == 0 )
215 return defaults[i].backend;
219 void Kleo::CryptoBackendFactory::readConfig() {
221 const TDEConfigGroup group( configObject(),
"Backends" );
222 for ( ProtocolSet::const_iterator it = mAvailableProtocols.begin(), end = mAvailableProtocols.end() ; it != end ; ++it ) {
223 const TQString backend = group.readEntry( *it, defaultBackend( *it ) );
224 mBackends[*it] = backendByName( backend );
228 const char * Kleo::CryptoBackendFactory::enumerateProtocols(
int i )
const {
229 if ( i < 0 ||
static_cast<unsigned int>( i ) >= mAvailableProtocols.size() )
231 return mAvailableProtocols[i];
235 class CaseInsensitiveString {
238 CaseInsensitiveString(
const char * s ) : m( s ) {}
239 #define make_operator( op ) \
240 bool operator op( const CaseInsensitiveString & other ) const { \
241 return tqstricmp( m, other.m ) op 0; \
243 bool operator op( const char * other ) const { \
244 return tqstricmp( m, other ) op 0; \
253 operator const char *()
const {
return m; }
255 #define make_ext_operator( op, inv_op ) \
256 inline bool operator op( const char * lhs, const CaseInsensitiveString & rhs ) { \
257 return rhs.operator inv_op( lhs ); \
259 make_ext_operator( ==, == )
260 make_ext_operator( !=, != )
261 make_ext_operator( <, > )
262 make_ext_operator( >, < )
263 make_ext_operator( <=, >= )
264 make_ext_operator( >=, <= )
265 #undef make_ext_operator
269 bool Kleo::CryptoBackendFactory::knowsAboutProtocol(
const char * name )
const {
270 return std::find( mAvailableProtocols.begin(), mAvailableProtocols.end(),
271 CaseInsensitiveString( name ) ) != mAvailableProtocols.end();
274 #include "cryptobackendfactory.moc"
Main interface to crypto configuration.