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<KURL> :
public TQVariant {
130 from_helper(
const KURL & url ) : TQVariant( url.path() ) {}
132 template <
typename T>
struct from_helper< TQValueList<T> > :
public TQVariant {
133 from_helper(
const TQValueList<T> & l ) {
134 TQValueList<TQVariant> result;
135 for (
typename TQValueList<T>::const_iterator it = l.begin(), end = l.end() ; it != end ; ++it )
136 result.push_back( from( *it ) );
137 TQVariant::operator=( result );
140 template <>
struct from_helper<KURL::List> :
public from_helper< TQValueList<KURL> > {
141 from_helper(
const KURL::List & l ) : from_helper< TQValueList<KURL> >( l ) {}
149 ChiasmusConfigEntry(
unsigned int i )
150 : Kleo::CryptoConfigEntry(),
151 mIdx( i ), mValue( defaultValue() ), mDirty( false )
153 assert( i < kleo_chiasmus_config_entries_dim );
155 TQString
name()
const {
return kleo_chiasmus_config_entries[mIdx].name; }
156 TQString
description()
const {
return i18n( kleo_chiasmus_config_entries[mIdx].description ); }
157 bool isOptional()
const {
return kleo_chiasmus_config_entries[mIdx].is_optional; }
159 bool isList()
const {
return kleo_chiasmus_config_entries[mIdx].is_list; }
160 bool isRuntime()
const {
return kleo_chiasmus_config_entries[mIdx].is_runtime; }
161 Level
level()
const {
return static_cast<Level
>( kleo_chiasmus_config_entries[mIdx].level ); }
162 ArgType
argType()
const {
return static_cast<ArgType
>( kleo_chiasmus_config_entries[mIdx].type ); }
163 bool isSet()
const {
return mValue != defaultValue(); }
164 bool boolValue()
const {
return mValue.toBool(); }
165 TQString
stringValue()
const {
return mValue.toString(); }
166 int intValue()
const {
return mValue.toInt(); }
167 unsigned int uintValue()
const {
return mValue.toUInt(); }
169 if (
argType() != ArgType_Path &&
argType() != ArgType_DirPath )
return KURL( mValue.toString() );
170 KURL u; u.setPath( mValue.toString() );
return u;
174 TQValueList<int>
intValueList()
const {
return to< TQValueList<int> >( mValue ); }
175 TQValueList<unsigned int>
uintValueList()
const {
return to< TQValueList<unsigned int> >( mValue ); }
177 if (
argType() != ArgType_Path &&
argType()!= ArgType_DirPath )
return mValue.toStringList();
178 else return to<KURL::List>( mValue ); }
179 void resetToDefault() { mValue = defaultValue(); mDirty =
false; }
180 void setBoolValue(
bool value ) { setValue( TQVariant( value ) ); }
181 void setStringValue(
const TQString & value ) { setValue( value ); }
182 void setIntValue(
int value ) { setValue( value ); }
183 void setUIntValue(
unsigned int value ) { setValue( value ); }
185 if (
argType() != ArgType_Path &&
argType()!= ArgType_DirPath ) setValue( value.url() );
186 else setValue( value.path() );
190 void setIntValueList(
const TQValueList<int> & l ) { setValue( from( l ) ); }
191 void setUIntValueList(
const TQValueList<unsigned int> & l ) { setValue( from( l ) ); }
192 void setURLValueList(
const KURL::List & l ) { setValue( from( l ) ); }
193 bool isDirty()
const {
return mDirty; }
195 TQVariant value()
const {
return mValue; }
197 void sync( TDEConfigBase * config ) {
201 config->writeEntry( kleo_chiasmus_config_entries[mIdx].name, mValue );
203 void read(
const TDEConfigBase * config ) {
205 mValue = config->readPropertyEntry( kleo_chiasmus_config_entries[mIdx].name, defaultValue() );
208 TQVariant defaultValue()
const;
209 void setValue(
const TQVariant & value ) { mValue = value; mDirty =
true; }
212 TQVariant ChiasmusConfigEntry::defaultValue()
const {
213 const kleo_chiasmus_config_data & data = kleo_chiasmus_config_entries[mIdx];
214 switch ( data.type ) {
219 return TQValueList<TQVariant>() << TQVariant( data.defaults.boolean.value );
221 return TQVariant( data.defaults.boolean.value );
224 return TQStringList( TQString::fromLatin1( data.defaults.string ) );
226 return TQString::fromLatin1( data.defaults.string );
229 return TQValueList<TQVariant>() << data.defaults.integer;
231 return data.defaults.integer;
234 return TQValueList<TQVariant>() << data.defaults.unsigned_integer;
236 return data.defaults.unsigned_integer;
238 case ArgType_DirPath:
240 return TQValueList<TQVariant>() << TQString::fromLatin1( data.defaults.path );
242 return TQString::fromLatin1( data.defaults.path );
244 case ArgType_LDAPURL:
246 return TQValueList<TQVariant>() << TQString::fromLatin1( data.defaults.url );
248 return TQString::fromLatin1( data.defaults.url );
253 mutable std::map<TQString,ChiasmusConfigEntry*> mCache;
254 mutable TDEConfig * mConfigObject;
256 ChiasmusGeneralGroup() : Kleo::CryptoConfigGroup(), mConfigObject( 0 ) {}
257 ~ChiasmusGeneralGroup() { clear();
delete mConfigObject; }
258 TQString
name()
const {
return "General"; }
259 TQString
iconName()
const {
return "chiasmus_chi"; }
260 TQString
description()
const {
return i18n(
"General" ); }
264 for (
unsigned int i = 0 ; i < kleo_chiasmus_config_entries_dim ; ++i )
265 result.push_back( kleo_chiasmus_config_entries[i].name );
269 if ( ChiasmusConfigEntry * entry = mCache[name] )
271 const TDEConfigGroup group( configObject(),
"Chiasmus" );
272 for (
unsigned int i = 0 ; i < kleo_chiasmus_config_entries_dim ; ++i )
273 if ( name == kleo_chiasmus_config_entries[i].name ) {
274 ChiasmusConfigEntry * entry =
new ChiasmusConfigEntry( i );
275 entry->read( &group );
282 TDEConfigGroup group( configObject(),
"Chiasmus" );
283 for ( std::map<TQString,ChiasmusConfigEntry*>::const_iterator it = mCache.begin(), end = mCache.end() ; it != end ; ++it )
284 it->second->sync( &group );
289 TDEConfig * configObject()
const {
290 if ( !mConfigObject )
292 mConfigObject =
new TDEConfig(
"chiasmusbackendrc" );
293 return mConfigObject;
296 for ( std::map<TQString,ChiasmusConfigEntry*>::const_iterator it = mCache.begin(), end = mCache.end() ; it != end ; ++it )
303 mutable ChiasmusGeneralGroup * mGeneralGroup;
305 ChiasmusComponent() : Kleo::CryptoConfigComponent(), mGeneralGroup( 0 ) {}
306 ~ChiasmusComponent() {
delete mGeneralGroup; }
310 mGeneralGroup->sync();
313 TQString
name()
const {
return "Chiasmus"; }
314 TQString
iconName()
const {
return "chiasmus_chi"; }
315 TQString
description()
const {
return i18n(
"Chiasmus" ); }
316 TQStringList
groupList()
const {
return TQStringList() <<
"General"; }
318 if ( name !=
"General" )
320 if ( !mGeneralGroup )
321 mGeneralGroup =
new ChiasmusGeneralGroup();
322 return mGeneralGroup;
329 mutable ChiasmusComponent * mComponent;
331 CryptoConfig() : Kleo::CryptoConfig(), mComponent( 0 ) {}
332 ~CryptoConfig() {
delete mComponent; }
334 TQStringList
componentList()
const {
return TQStringList() <<
"Chiasmus" ; }
335 ChiasmusComponent *
component(
const TQString & name )
const {
336 if ( name !=
"Chiasmus" )
339 mComponent =
new ChiasmusComponent();
346 void clear() {
delete mComponent; mComponent = 0; }
349 class Kleo::ChiasmusBackend::Protocol :
public Kleo::CryptoBackend::Protocol {
353 : Kleo::CryptoBackend::Protocol(), mCryptoConfig( config )
359 TQString name()
const {
return "Chiasmus"; }
360 TQString displayName()
const {
return i18n(
"Chiasmus command line tool" ); }
361 KeyListJob * keyListJob(
bool,
bool,
bool )
const {
return 0; }
362 EncryptJob * encryptJob(
bool,
bool )
const {
return 0; }
363 DecryptJob * decryptJob()
const {
return 0; }
364 SignJob * signJob(
bool,
bool )
const {
return 0; }
365 VerifyDetachedJob * verifyDetachedJob(
bool )
const {
return 0; }
366 VerifyOpaqueJob * verifyOpaqueJob(
bool )
const {
return 0; }
367 KeyGenerationJob * keyGenerationJob()
const {
return 0; }
368 ImportJob * importJob()
const {
return 0; }
369 ExportJob * publicKeyExportJob(
bool )
const {
return 0; }
370 ExportJob * secretKeyExportJob(
bool,
const TQString& )
const {
return 0; }
371 DownloadJob * downloadJob(
bool )
const {
return 0; }
372 DeleteJob * deleteJob()
const {
return 0; }
373 SignEncryptJob * signEncryptJob(
bool,
bool )
const {
return 0; }
374 DecryptVerifyJob * decryptVerifyJob(
bool )
const {
return 0; }
375 RefreshKeysJob * refreshKeysJob()
const {
return 0; }
377 SpecialJob * specialJob(
const char * type,
const TQStringVariantMap & args )
const {
378 if ( tqstricmp( type,
"x-obtain-keys" ) == 0 && args.size() == 0 )
379 return new ObtainKeysJob();
380 if ( tqstricmp( type,
"x-encrypt" ) == 0 && args.size() == 0 )
381 return new ChiasmusJob( ChiasmusJob::Encrypt );
382 if ( tqstricmp( type,
"x-decrypt" ) == 0 && args.size() == 0 )
383 return new ChiasmusJob( ChiasmusJob::Decrypt );
384 kdDebug(5150) <<
"ChiasmusBackend::Protocol: tried to instantiate unknown job type \""
385 << type <<
"\"" << endl;
391 Kleo::ChiasmusBackend * Kleo::ChiasmusBackend::self = 0;
393 Kleo::ChiasmusBackend::ChiasmusBackend()
394 : Kleo::CryptoBackend(),
401 Kleo::ChiasmusBackend::~ChiasmusBackend() {
403 delete mCryptoConfig;
407 TQString Kleo::ChiasmusBackend::name()
const {
411 TQString Kleo::ChiasmusBackend::displayName()
const {
412 return i18n(
"Chiasmus" );
416 if ( !mCryptoConfig )
417 mCryptoConfig =
new CryptoConfig();
418 return mCryptoConfig;
421 Kleo::CryptoBackend::Protocol * Kleo::ChiasmusBackend::protocol(
const char * name )
const {
422 if ( tqstricmp( name,
"Chiasmus" ) != 0 )
425 if ( checkForChiasmus() )
426 mProtocol =
new Protocol( config() );
430 bool Kleo::ChiasmusBackend::checkForOpenPGP( TQString * reason )
const {
432 *reason = i18n(
"Unsupported protocol \"%1\"" ).arg(
"OpenPGP" );
436 bool Kleo::ChiasmusBackend::checkForSMIME( TQString * reason )
const {
438 *reason = i18n(
"Unsupported protocol \"%1\"" ).arg(
"SMIME" );
442 bool Kleo::ChiasmusBackend::checkForChiasmus( TQString * reason )
const {
445 std::unique_ptr<Protocol> tmp( mProtocol );
448 const CryptoConfigEntry * path = config()->entry(
"Chiasmus",
"General",
"path" );
449 assert( path ); assert( path->argType() == CryptoConfigEntry::ArgType_Path );
450 const TQString chiasmus = path->urlValue().path();
451 const TQFileInfo fi( KShell::tildeExpand( chiasmus ) );
452 if ( !fi.isExecutable() ) {
454 *reason = i18n(
"File \"%1\" does not exist or is not executable." ).arg( chiasmus );
459 mProtocol = tmp.release();
463 bool Kleo::ChiasmusBackend::checkForProtocol(
const char * name, TQString * reason )
const {
464 if ( tqstricmp( name,
"Chiasmus" ) == 0 )
465 return checkForChiasmus( reason );
467 *reason = i18n(
"Unsupported protocol \"%1\"" ).arg( name );
471 bool Kleo::ChiasmusBackend::supportsProtocol(
const char * name )
const {
472 return tqstricmp( name,
"Chiasmus" ) == 0;
475 const char * Kleo::ChiasmusBackend::enumerateProtocols(
int i )
const {
476 return i == 0 ?
"Chiasmus" : 0 ;
Crypto config for one component (e.g.
virtual TQString name() const =0
Return the internal name of this component.
virtual TQString iconName() const =0
Return the name of the icon for this component.
virtual TQString description() const =0
Return user-visible description of this component.
virtual TQStringList groupList() const =0
Returns the list of groups that are known about.
virtual CryptoConfigGroup * group(const TQString &name) const =0
Description of a single option.
virtual void setBoolValue(bool)=0
Define whether the option is set or not (only allowed for ArgType_None)
virtual bool isList() const =0
virtual ArgType argType() const =0
Argument type.
virtual TQValueList< int > intValueList() const =0
Return value as a list of signed ints.
virtual TQStringList stringValueList() const =0
Return value as a list of strings (mostly meaningful for String, Path and URL argtypes,...
virtual void setNumberOfTimesSet(unsigned int)=0
Set the number of times the option is set (only valid for ArgType_None, if isList())
virtual void setUIntValueList(const TQValueList< unsigned int > &)=0
Set a new list of unsigned int values.
virtual bool isDirty() const =0
virtual void setStringValueList(const TQStringList &)=0
Set a new string-list value (only allowed for String, Path and URL argtypes, if isList())
virtual bool isOptional() const =0
virtual KURL::List urlValueList() const =0
Return value as a list of URLs (only meaningful for Path and URL argtypes, if isList())
virtual TQString name() const =0
Return the internal name of this entry.
virtual void setURLValue(const KURL &)=0
Set value as a URL (only meaningful for Path (if local) and URL argtypes)
virtual TQString stringValue() const =0
Return value as a string (available for all argtypes) The returned string can be empty (explicitely s...
virtual unsigned int uintValue() const =0
Return value as an unsigned int.
virtual void setUIntValue(unsigned int)=0
Set a new unsigned int value.
virtual Level level() const =0
User level.
virtual TQString description() const =0
virtual bool boolValue() const =0
Return value as a bool (only allowed for ArgType_None)
virtual int intValue() const =0
Return value as a signed int.
virtual KURL urlValue() const =0
Return value as a URL (only meaningful for Path and URL argtypes)
virtual TQValueList< unsigned int > uintValueList() const =0
Return value as a list of unsigned ints.
virtual unsigned int numberOfTimesSet() const =0
Return number of times the option is set (only valid for ArgType_None, if isList())
virtual bool isRuntime() const =0
virtual void setURLValueList(const KURL::List &)=0
Set value as a URL list (only meaningful for Path (if all URLs are local) and URL argtypes,...
virtual void setStringValue(const TQString &)=0
Set string value (allowed for all argtypes)
virtual void setIntValueList(const TQValueList< int > &)=0
Set a new list of signed int values.
virtual void resetToDefault()=0
Reset an option to its default value.
virtual bool isSet() const =0
Return true if the option is set, i.e.
virtual void setIntValue(int)=0
Set a new signed int value.
virtual bool isReadOnly() const =0
Group containing a set of config options.
virtual CryptoConfigEntry::Level level() const =0
User level.
virtual CryptoConfigEntry * entry(const TQString &name) const =0
virtual TQString description() const =0
virtual TQStringList entryList() const =0
Returns the list of entries that are known by this group.
virtual TQString iconName() const =0
Return the name of the icon for this group.
virtual TQString name() const =0
Return the internal name of this group.
Main interface to crypto configuration.
virtual void sync(bool runtime)=0
Write back changes.
virtual void clear()=0
Tells the CryptoConfig to discard any cached information, including all components,...
virtual TQStringList componentList() const =0
Returns the list of known components (e.g.
virtual CryptoConfigComponent * component(const TQString &name) const =0