37 #include "chiasmusbackend.h" 39 #include "config_data.h" 40 #include "obtainkeysjob.h" 41 #include "chiasmusjob.h" 43 #include "kleo/cryptoconfig.h" 45 #include <tdelocale.h> 46 #include <tdeconfig.h> 50 #include <tqstringlist.h> 51 #include <tqvariant.h> 52 #include <tqfileinfo.h> 68 template <
typename T>
class to {};
70 #define MAKE_TO( type, func ) \ 75 to( const TQVariant & v ) : m( v.func() ) {} \ 76 operator type() const { return m; } \ 79 MAKE_TO(
int, toInt );
80 MAKE_TO(
unsigned int, toUInt );
86 to(
const TQVariant & v ) {
87 m.setPath( v.toString() );
89 operator KURL()
const {
return m; }
93 class to< TQValueList<T> > {
96 to(
const TQVariant & v ) {
97 const TQValueList<TQVariant> vl = v.toList();
98 for ( TQValueList<TQVariant>::const_iterator it = vl.begin(), end = vl.end() ; it != end ; ++it )
99 m.push_back( to<T>( *it ) );
101 operator TQValueList<T> ()
const {
return m; }
105 class to<KURL::List> {
108 to(
const TQVariant & v ) {
110 m += to< TQValueList<KURL> >( v );
112 operator KURL::List()
const {
return m; }
118 template <
typename T>
119 struct from_helper :
public TQVariant {
120 from_helper(
const T & t ) : TQVariant( t ) {}
123 template <
typename T>
124 TQVariant from(
const T & t ) {
125 return from_helper<T>( t );
129 template <>
struct from_helper<bool> :
public TQVariant {
130 from_helper(
bool b ) : TQVariant( b, int() ) {}
132 template <>
struct from_helper<KURL> :
public TQVariant {
133 from_helper(
const KURL & url ) : TQVariant( url.path() ) {}
135 template <
typename T>
struct from_helper< TQValueList<T> > :
public TQVariant {
136 from_helper(
const TQValueList<T> & l ) {
137 TQValueList<TQVariant> result;
138 for (
typename TQValueList<T>::const_iterator it = l.begin(), end = l.end() ; it != end ; ++it )
139 result.push_back( from( *it ) );
140 TQVariant::operator=( result );
143 template <>
struct from_helper<KURL::List> :
public from_helper< TQValueList<KURL> > {
144 from_helper(
const KURL::List & l ) : from_helper< TQValueList<KURL> >( l ) {}
152 ChiasmusConfigEntry(
unsigned int i )
153 :
Kleo::CryptoConfigEntry(),
154 mIdx( i ), mValue( defaultValue() ), mDirty( false )
156 assert( i < kleo_chiasmus_config_entries_dim );
158 TQString name()
const {
return kleo_chiasmus_config_entries[mIdx].name; }
159 TQString description()
const {
return i18n( kleo_chiasmus_config_entries[mIdx].description ); }
160 bool isOptional()
const {
return kleo_chiasmus_config_entries[mIdx].is_optional; }
161 bool isReadOnly()
const {
return false; }
162 bool isList()
const {
return kleo_chiasmus_config_entries[mIdx].is_list; }
163 bool isRuntime()
const {
return kleo_chiasmus_config_entries[mIdx].is_runtime; }
164 Level level()
const {
return static_cast<Level
>( kleo_chiasmus_config_entries[mIdx].level ); }
165 ArgType argType()
const {
return static_cast<ArgType
>( kleo_chiasmus_config_entries[mIdx].type ); }
166 bool isSet()
const {
return mValue != defaultValue(); }
167 bool boolValue()
const {
return mValue.toBool(); }
168 TQString stringValue()
const {
return mValue.toString(); }
169 int intValue()
const {
return mValue.toInt(); }
170 unsigned int uintValue()
const {
return mValue.toUInt(); }
171 KURL urlValue()
const {
172 if ( argType() != ArgType_Path && argType() != ArgType_DirPath )
return KURL( mValue.toString() );
173 KURL u; u.setPath( mValue.toString() );
return u;
175 unsigned int numberOfTimesSet()
const {
return 0; }
176 TQStringList stringValueList()
const {
return mValue.toStringList(); }
177 TQValueList<int> intValueList()
const {
return to< TQValueList<int> >( mValue ); }
178 TQValueList<unsigned int> uintValueList()
const {
return to< TQValueList<unsigned int> >( mValue ); }
179 KURL::List urlValueList()
const {
180 if ( argType() != ArgType_Path && argType()!= ArgType_DirPath )
return mValue.toStringList();
181 else return to<KURL::List>( mValue ); }
182 void resetToDefault() { mValue = defaultValue(); mDirty =
false; }
183 void setBoolValue(
bool value ) { setValue( TQVariant( value,
int() ) ); }
184 void setStringValue(
const TQString & value ) { setValue( value ); }
185 void setIntValue(
int value ) { setValue( value ); }
186 void setUIntValue(
unsigned int value ) { setValue( value ); }
187 void setURLValue(
const KURL & value ) {
188 if ( argType() != ArgType_Path && argType()!= ArgType_DirPath ) setValue( value.url() );
189 else setValue( value.path() );
191 void setNumberOfTimesSet(
unsigned int ) {}
192 void setStringValueList(
const TQStringList & value ) { setValue( value ); }
193 void setIntValueList(
const TQValueList<int> & l ) { setValue( from( l ) ); }
194 void setUIntValueList(
const TQValueList<unsigned int> & l ) { setValue( from( l ) ); }
195 void setURLValueList(
const KURL::List & l ) { setValue( from( l ) ); }
196 bool isDirty()
const {
return mDirty; }
198 TQVariant value()
const {
return mValue; }
200 void sync( TDEConfigBase * config ) {
204 config->writeEntry( kleo_chiasmus_config_entries[mIdx].name, mValue );
206 void read(
const TDEConfigBase * config ) {
208 mValue = config->readPropertyEntry( kleo_chiasmus_config_entries[mIdx].name, defaultValue() );
211 TQVariant defaultValue()
const;
212 void setValue(
const TQVariant & value ) { mValue = value; mDirty =
true; }
215 TQVariant ChiasmusConfigEntry::defaultValue()
const {
216 const kleo_chiasmus_config_data & data = kleo_chiasmus_config_entries[mIdx];
217 switch ( data.type ) {
222 return TQValueList<TQVariant>() << TQVariant( data.defaults.boolean.value,
int() );
224 return TQVariant( data.defaults.boolean.value,
int() );
227 return TQStringList( TQString::fromLatin1( data.defaults.string ) );
229 return TQString::fromLatin1( data.defaults.string );
232 return TQValueList<TQVariant>() << data.defaults.integer;
234 return data.defaults.integer;
237 return TQValueList<TQVariant>() << data.defaults.unsigned_integer;
239 return data.defaults.unsigned_integer;
241 case ArgType_DirPath:
243 return TQValueList<TQVariant>() << TQString::fromLatin1( data.defaults.path );
245 return TQString::fromLatin1( data.defaults.path );
247 case ArgType_LDAPURL:
249 return TQValueList<TQVariant>() << TQString::fromLatin1( data.defaults.url );
251 return TQString::fromLatin1( data.defaults.url );
256 mutable std::map<TQString,ChiasmusConfigEntry*> mCache;
257 mutable TDEConfig * mConfigObject;
259 ChiasmusGeneralGroup() :
Kleo::CryptoConfigGroup(), mConfigObject( 0 ) {}
260 ~ChiasmusGeneralGroup() { clear();
delete mConfigObject; }
261 TQString name()
const {
return "General"; }
262 TQString iconName()
const {
return "chiasmus_chi"; }
263 TQString description()
const {
return i18n(
"General" ); }
265 TQStringList entryList()
const {
267 for (
unsigned int i = 0 ; i < kleo_chiasmus_config_entries_dim ; ++i )
268 result.push_back( kleo_chiasmus_config_entries[i].name );
272 if ( ChiasmusConfigEntry * entry = mCache[name] )
274 const TDEConfigGroup group( configObject(),
"Chiasmus" );
275 for (
unsigned int i = 0 ; i < kleo_chiasmus_config_entries_dim ; ++i )
276 if ( name == kleo_chiasmus_config_entries[i].name ) {
277 ChiasmusConfigEntry * entry =
new ChiasmusConfigEntry( i );
278 entry->read( &group );
279 return mCache[name] = entry;
285 TDEConfigGroup group( configObject(),
"Chiasmus" );
286 for ( std::map<TQString,ChiasmusConfigEntry*>::const_iterator it = mCache.begin(), end = mCache.end() ; it != end ; ++it )
287 it->second->sync( &group );
292 TDEConfig * configObject()
const {
293 if ( !mConfigObject )
295 mConfigObject =
new TDEConfig(
"chiasmusbackendrc" );
296 return mConfigObject;
299 for ( std::map<TQString,ChiasmusConfigEntry*>::const_iterator it = mCache.begin(), end = mCache.end() ; it != end ; ++it )
306 mutable ChiasmusGeneralGroup * mGeneralGroup;
308 ChiasmusComponent() :
Kleo::CryptoConfigComponent(), mGeneralGroup( 0 ) {}
309 ~ChiasmusComponent() {
delete mGeneralGroup; }
313 mGeneralGroup->sync();
316 TQString name()
const {
return "Chiasmus"; }
317 TQString iconName()
const {
return "chiasmus_chi"; }
318 TQString description()
const {
return i18n(
"Chiasmus" ); }
319 TQStringList groupList()
const {
return TQStringList() <<
"General"; }
321 if ( name !=
"General" )
323 if ( !mGeneralGroup )
324 mGeneralGroup =
new ChiasmusGeneralGroup();
325 return mGeneralGroup;
332 mutable ChiasmusComponent * mComponent;
334 CryptoConfig() :
Kleo::CryptoConfig(), mComponent( 0 ) {}
335 ~CryptoConfig() {
delete mComponent; }
337 TQStringList componentList()
const {
return TQStringList() <<
"Chiasmus" ; }
338 ChiasmusComponent * component(
const TQString & name )
const {
339 if ( name !=
"Chiasmus" )
342 mComponent =
new ChiasmusComponent();
349 void clear() {
delete mComponent; mComponent = 0; }
352 class Kleo::ChiasmusBackend::Protocol :
public Kleo::CryptoBackend::Protocol {
356 :
Kleo::CryptoBackend::Protocol(), mCryptoConfig( config )
362 TQString name()
const {
return "Chiasmus"; }
363 TQString displayName()
const {
return i18n(
"Chiasmus command line tool" ); }
364 KeyListJob * keyListJob(
bool,
bool,
bool )
const {
return 0; }
365 EncryptJob * encryptJob(
bool,
bool )
const {
return 0; }
366 DecryptJob * decryptJob()
const {
return 0; }
367 SignJob * signJob(
bool,
bool )
const {
return 0; }
368 VerifyDetachedJob * verifyDetachedJob(
bool )
const {
return 0; }
369 VerifyOpaqueJob * verifyOpaqueJob(
bool )
const {
return 0; }
370 KeyGenerationJob * keyGenerationJob()
const {
return 0; }
371 ImportJob * importJob()
const {
return 0; }
372 ExportJob * publicKeyExportJob(
bool )
const {
return 0; }
373 ExportJob * secretKeyExportJob(
bool,
const TQString& )
const {
return 0; }
374 DownloadJob * downloadJob(
bool )
const {
return 0; }
375 DeleteJob * deleteJob()
const {
return 0; }
376 SignEncryptJob * signEncryptJob(
bool,
bool )
const {
return 0; }
377 DecryptVerifyJob * decryptVerifyJob(
bool )
const {
return 0; }
378 RefreshKeysJob * refreshKeysJob()
const {
return 0; }
380 SpecialJob * specialJob(
const char * type,
const TQMap<TQString,TQVariant> & args )
const {
381 if ( tqstricmp( type,
"x-obtain-keys" ) == 0 && args.size() == 0 )
382 return new ObtainKeysJob();
383 if ( tqstricmp( type,
"x-encrypt" ) == 0 && args.size() == 0 )
384 return new ChiasmusJob( ChiasmusJob::Encrypt );
385 if ( tqstricmp( type,
"x-decrypt" ) == 0 && args.size() == 0 )
386 return new ChiasmusJob( ChiasmusJob::Decrypt );
387 kdDebug(5150) <<
"ChiasmusBackend::Protocol: tried to instantiate unknown job type \"" 388 << type <<
"\"" << endl;
394 Kleo::ChiasmusBackend * Kleo::ChiasmusBackend::self = 0;
396 Kleo::ChiasmusBackend::ChiasmusBackend()
397 :
Kleo::CryptoBackend(),
404 Kleo::ChiasmusBackend::~ChiasmusBackend() {
406 delete mCryptoConfig;
410 TQString Kleo::ChiasmusBackend::name()
const {
414 TQString Kleo::ChiasmusBackend::displayName()
const {
415 return i18n(
"Chiasmus" );
419 if ( !mCryptoConfig )
420 mCryptoConfig =
new CryptoConfig();
421 return mCryptoConfig;
424 Kleo::CryptoBackend::Protocol * Kleo::ChiasmusBackend::protocol(
const char * name )
const {
425 if ( tqstricmp( name,
"Chiasmus" ) != 0 )
428 if ( checkForChiasmus() )
429 mProtocol =
new Protocol( config() );
433 bool Kleo::ChiasmusBackend::checkForOpenPGP( TQString * reason )
const {
435 *reason = i18n(
"Unsupported protocol \"%1\"" ).arg(
"OpenPGP" );
439 bool Kleo::ChiasmusBackend::checkForSMIME( TQString * reason )
const {
441 *reason = i18n(
"Unsupported protocol \"%1\"" ).arg(
"SMIME" );
445 bool Kleo::ChiasmusBackend::checkForChiasmus( TQString * reason )
const {
448 std::auto_ptr<Protocol> tmp( mProtocol );
451 const CryptoConfigEntry * path = config()->entry(
"Chiasmus",
"General",
"path" );
452 assert( path ); assert( path->argType() == CryptoConfigEntry::ArgType_Path );
453 const TQString chiasmus = path->urlValue().path();
454 const TQFileInfo fi( KShell::tildeExpand( chiasmus ) );
455 if ( !fi.isExecutable() ) {
457 *reason = i18n(
"File \"%1\" does not exist or is not executable." ).arg( chiasmus );
462 mProtocol = tmp.release();
466 bool Kleo::ChiasmusBackend::checkForProtocol(
const char * name, TQString * reason )
const {
467 if ( tqstricmp( name,
"Chiasmus" ) == 0 )
468 return checkForChiasmus( reason );
470 *reason = i18n(
"Unsupported protocol \"%1\"" ).arg( name );
474 bool Kleo::ChiasmusBackend::supportsProtocol(
const char * name )
const {
475 return tqstricmp( name,
"Chiasmus" ) == 0;
478 const char * Kleo::ChiasmusBackend::enumerateProtocols(
int i )
const {
479 return i == 0 ?
"Chiasmus" : 0 ;
Main interface to crypto configuration.
Crypto config for one component (e.g.
Group containing a set of config options.
Description of a single option.