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; }
158 bool isReadOnly()
const {
return false; }
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(); }
168 KURL urlValue()
const {
169 if ( argType() != ArgType_Path && argType() != ArgType_DirPath )
return KURL( mValue.toString() );
170 KURL u; u.setPath( mValue.toString() );
return u;
172 unsigned int numberOfTimesSet()
const {
return 0; }
173 TQStringList stringValueList()
const {
return mValue.toStringList(); }
174 TQValueList<int> intValueList()
const {
return to< TQValueList<int> >( mValue ); }
175 TQValueList<unsigned int> uintValueList()
const {
return to< TQValueList<unsigned int> >( mValue ); }
176 KURL::List urlValueList()
const {
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 ); }
184 void setURLValue(
const KURL & value ) {
185 if ( argType() != ArgType_Path && argType()!= ArgType_DirPath ) setValue( value.url() );
186 else setValue( value.path() );
188 void setNumberOfTimesSet(
unsigned int ) {}
189 void setStringValueList(
const TQStringList & value ) { setValue( value ); }
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" ); }
262 TQStringList entryList()
const {
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 );
276 return mCache[name] = entry;
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 ;