45 #include <kconfiggroup.h>
47 #include <QStringList>
68 template <
typename T>
class to {};
70 #define MAKE_TO( type, func ) \
75 to( const QVariant & v ) : m( v.func() ) {} \
76 operator type() const { return m; } \
80 MAKE_TO(
unsigned int, toUInt );
86 to(
const QVariant & v ) {
87 m.setPath( v.toString() );
89 operator KUrl()
const {
return m; }
93 class to< QList<T> > {
96 to(
const QVariant & v ) {
97 const QList<QVariant> vl = v.toList();
98 for ( QList<QVariant>::const_iterator it = vl.begin(), end = vl.end() ; it != end ; ++it )
99 m.push_back( to<T>( *it ) );
101 operator QList<T> ()
const {
return m; }
104 template <
typename T>
105 class to< std::vector<T> > {
108 to(
const QVariant & v ) {
109 const QList<QVariant> vl = v.toList();
110 m.reserve( m.size() + vl.size() );
111 for ( QList<QVariant>::const_iterator it = vl.begin(), end = vl.end() ; it != end ; ++it )
112 m.push_back( to<T>( *it ) );
114 operator std::vector<T> ()
const {
return m; }
118 class to<KUrl::List> {
121 to(
const QVariant & v ) {
123 m += to< QList<KUrl> >( v );
125 operator KUrl::List()
const {
return m; }
131 template <
typename T>
132 struct from_helper :
public QVariant {
133 from_helper(
const T & t ) : QVariant( t ) {}
136 template <
typename T>
137 QVariant from(
const T & t ) {
138 return from_helper<T>( t );
142 template <>
struct from_helper<bool> :
public QVariant {
143 from_helper(
bool b ) : QVariant( b ) {}
145 template <>
struct from_helper<KUrl> :
public QVariant {
146 from_helper(
const KUrl & url ) : QVariant( url.path() ) {}
148 template <
typename T>
struct from_helper< QList<T> > :
public QVariant {
149 from_helper(
const QList<T> & l ) {
150 QList<QVariant> result;
151 for (
typename QList<T>::const_iterator it = l.begin(), end = l.end() ; it != end ; ++it )
152 result.push_back( from( *it ) );
153 QVariant::operator=( result );
156 template <
typename T>
struct from_helper< std::vector<T> > :
public QVariant {
157 from_helper(
const std::vector<T> & l ) {
158 QList<QVariant> result;
159 for (
typename std::vector<T>::const_iterator it = l.begin(), end = l.end() ; it != end ; ++it )
160 result.push_back( from( *it ) );
161 QVariant::operator=( result );
164 template <>
struct from_helper<KUrl::List> :
public from_helper< QList<KUrl> > {
165 from_helper(
const KUrl::List & l ) : from_helper< QList<KUrl> >( l ) {}
168 class ChiasmusConfigEntry :
public Kleo::CryptoConfigEntry {
173 ChiasmusConfigEntry(
unsigned int i )
174 : Kleo::CryptoConfigEntry(),
175 mIdx( i ), mValue( defaultValue() ), mDirty( false )
183 bool isReadOnly()
const {
return false; }
188 bool isSet()
const {
return mValue != defaultValue(); }
189 bool boolValue()
const {
return mValue.toBool(); }
190 QString stringValue()
const {
return mValue.toString(); }
191 int intValue()
const {
return mValue.toInt(); }
192 unsigned int uintValue()
const {
return mValue.toUInt(); }
193 KUrl urlValue()
const {
195 KUrl u; u.setPath( mValue.toString() );
return u;
197 unsigned int numberOfTimesSet()
const {
return 0; }
198 QStringList stringValueList()
const {
return mValue.toStringList(); }
199 std::vector<int> intValueList()
const {
return to< std::vector<int> >( mValue ); }
200 std::vector<unsigned int> uintValueList()
const {
return to< std::vector<unsigned int> >( mValue ); }
201 KUrl::List urlValueList()
const {
203 else return to<KUrl::List>( mValue ); }
204 void resetToDefault() { mValue = defaultValue(); mDirty =
false; }
205 void setBoolValue(
bool value ) { setValue( QVariant( value ) ); }
206 void setStringValue(
const QString & value ) { setValue( value ); }
207 void setIntValue(
int value ) { setValue( value ); }
208 void setUIntValue(
unsigned int value ) { setValue( value ); }
209 void setURLValue(
const KUrl & value ) {
211 else setValue( value.path() );
213 void setNumberOfTimesSet(
unsigned int ) {}
214 void setStringValueList(
const QStringList & value ) { setValue( value ); }
215 void setIntValueList(
const std::vector<int> & l ) { setValue( from( l ) ); }
216 void setUIntValueList(
const std::vector<unsigned int> & l ) { setValue( from( l ) ); }
217 void setURLValueList(
const KUrl::List & l ) { setValue( from( l ) ); }
218 bool isDirty()
const {
return mDirty; }
220 QVariant value()
const {
return mValue; }
222 void sync( KConfigGroup config ) {
228 void read(
const KConfigGroup & config ) {
233 QVariant defaultValue()
const;
234 void setValue(
const QVariant & value ) { mValue = value; mDirty =
true; }
237 QVariant ChiasmusConfigEntry::defaultValue()
const {
239 switch ( data.
type ) {
249 return QStringList( QString::fromLatin1( data.
defaults.
string ) );
265 return QList<QVariant>() << QString::fromLatin1( data.
defaults.
path );
271 return QList<QVariant>() << QString::fromLatin1( data.
defaults.
url );
277 class ChiasmusGeneralGroup :
public Kleo::CryptoConfigGroup {
278 mutable std::map<QString,ChiasmusConfigEntry*> mCache;
279 mutable KConfig * mConfigObject;
281 ChiasmusGeneralGroup() : Kleo::CryptoConfigGroup(), mConfigObject( 0 ) {}
282 ~ChiasmusGeneralGroup() { clear();
delete mConfigObject; }
283 QString name()
const {
return QLatin1String(
"General"); }
284 QString iconName()
const {
return QLatin1String(
"chiasmus_chi"); }
286 QString description()
const {
return i18n(
"General" ); }
288 QStringList entryList()
const {
294 Kleo::CryptoConfigEntry * entry(
const QString &
name )
const {
295 if ( ChiasmusConfigEntry * entry = mCache[name] )
297 const KConfigGroup group( configObject(),
"Chiasmus" );
300 ChiasmusConfigEntry * entry =
new ChiasmusConfigEntry( i );
301 entry->read( group );
302 return mCache[
name] = entry;
308 KConfigGroup group( configObject(),
"Chiasmus" );
309 for ( std::map<QString,ChiasmusConfigEntry*>::const_iterator it = mCache.begin(), end = mCache.end() ; it != end ; ++it )
310 it->second->sync( group );
315 KConfig * configObject()
const {
316 if ( !mConfigObject )
318 mConfigObject =
new KConfig( QLatin1String(
"chiasmusbackendrc") );
319 return mConfigObject;
322 for ( std::map<QString,ChiasmusConfigEntry*>::const_iterator it = mCache.begin(), end = mCache.end() ; it != end ; ++it )
328 class ChiasmusComponent :
public Kleo::CryptoConfigComponent {
329 mutable ChiasmusGeneralGroup * mGeneralGroup;
331 ChiasmusComponent() : Kleo::CryptoConfigComponent(), mGeneralGroup( 0 ) {}
332 ~ChiasmusComponent() {
delete mGeneralGroup; }
336 mGeneralGroup->sync();
339 QString name()
const {
return QLatin1String(
"Chiasmus"); }
340 QString iconName()
const {
return QLatin1String(
"chiasmus_chi"); }
341 QString description()
const {
return i18n(
"Chiasmus" ); }
342 QStringList groupList()
const {
return QStringList() << QLatin1String(
"General"); }
343 Kleo::CryptoConfigGroup * group(
const QString & name )
const {
344 if ( name != QLatin1String(
"General") )
346 if ( !mGeneralGroup )
347 mGeneralGroup =
new ChiasmusGeneralGroup();
348 return mGeneralGroup;
354 class Kleo::ChiasmusBackend::CryptoConfig :
public Kleo::CryptoConfig {
355 mutable ChiasmusComponent * mComponent;
357 CryptoConfig() : Kleo::CryptoConfig(), mComponent( 0 ) {}
358 ~CryptoConfig() {
delete mComponent; }
360 QStringList componentList()
const {
return QStringList() << QLatin1String(
"Chiasmus") ; }
361 ChiasmusComponent * component(
const QString & name )
const {
362 if ( name != QLatin1String(
"Chiasmus") )
365 mComponent =
new ChiasmusComponent();
372 void clear() {
delete mComponent; mComponent = 0; }
376 Kleo::CryptoConfig * mCryptoConfig;
378 Protocol( Kleo::CryptoConfig * config )
379 : Kleo::CryptoBackend::Protocol(), mCryptoConfig( config )
385 QString name()
const {
return QLatin1String(
"Chiasmus"); }
387 KeyListJob *
keyListJob(
bool,
bool,
bool )
const {
return 0; }
389 EncryptJob *
encryptJob(
bool,
bool )
const {
return 0; }
391 SignJob *
signJob(
bool,
bool )
const {
return 0; }
396 ImportJob *
importJob()
const {
return 0; }
399 DownloadJob *
downloadJob(
bool )
const {
return 0; }
400 DeleteJob *
deleteJob()
const {
return 0; }
406 if ( qstricmp( type,
"x-obtain-keys" ) == 0 && args.size() == 0 )
407 return new ObtainKeysJob();
408 if ( qstricmp( type,
"x-encrypt" ) == 0 && args.size() == 0 )
410 if ( qstricmp( type,
"x-decrypt" ) == 0 && args.size() == 0 )
412 kDebug(5150) <<
"ChiasmusBackend::Protocol: tried to instantiate unknown job type \""
431 delete mCryptoConfig;
436 return QLatin1String(
"Chiasmus");
440 return i18n(
"Chiasmus" );
444 if ( !mCryptoConfig )
445 mCryptoConfig =
new CryptoConfig();
446 return mCryptoConfig;
450 if ( qstricmp( name,
"Chiasmus" ) != 0 )
453 if ( checkForChiasmus() )
454 mProtocol =
new Protocol( config() );
460 *reason = i18n(
"Unsupported protocol \"%1\"", QLatin1String(
"OpenPGP") );
466 *reason = i18n(
"Unsupported protocol \"%1\"", QLatin1String(
"SMIME") );
473 std::auto_ptr<Protocol> tmp( mProtocol );
476 const CryptoConfigEntry * path = config()->entry( QLatin1String(
"Chiasmus"), QLatin1String(
"General"), QLatin1String(
"path") );
478 const QString chiasmus = path->urlValue().path();
479 const QFileInfo fi( KShell::tildeExpand( chiasmus ) );
480 if ( !fi.isExecutable() ) {
482 *reason = i18n(
"File \"%1\" does not exist or is not executable.", chiasmus );
487 mProtocol = tmp.release();
492 if ( qstricmp( name,
"Chiasmus" ) == 0 )
493 return checkForChiasmus( reason );
495 *reason = i18n(
"Unsupported protocol \"%1\"", QLatin1String(name) );
500 return qstricmp( name,
"Chiasmus" ) == 0;
504 return i == 0 ?
"Chiasmus" : 0 ;
struct kleo_chiasmus_config_data::@0::@1 boolean
virtual VerifyDetachedJob * verifyDetachedJob(bool textmode=false) const =0
#define MAKE_TO(type, func)
virtual KeyGenerationJob * keyGenerationJob() const =0
virtual QString displayName() const =0
virtual DeleteJob * deleteJob() const =0
virtual ImportJob * importJob() const =0
virtual RefreshKeysJob * refreshKeysJob() const =0
virtual DecryptJob * decryptJob() const =0
Kleo::CryptoConfig * config() const
unsigned int unsigned_integer
virtual VerifyOpaqueJob * verifyOpaqueJob(bool textmode=false) const =0
Kleo::CryptoBackend::Protocol * protocol(const char *name) const
bool checkForOpenPGP(QString *reason=0) const
union kleo_chiasmus_config_data::@0 defaults
virtual SignJob * signJob(bool armor=false, bool textMode=false) const =0
virtual ImportFromKeyserverJob * importFromKeyserverJob() const =0
virtual SpecialJob * specialJob(const char *type, const QMap< QString, QVariant > &args) const =0
virtual ExportJob * publicKeyExportJob(bool armor=false) const =0
const unsigned int kleo_chiasmus_config_entries_dim
virtual SignEncryptJob * signEncryptJob(bool armor=false, bool textMode=false) const =0
bool checkForSMIME(QString *reason=0) const
const struct kleo_chiasmus_config_data kleo_chiasmus_config_entries[]
ArgType
Type of the argument.
virtual DownloadJob * downloadJob(bool armor=false) const =0
virtual DecryptVerifyJob * decryptVerifyJob(bool textmode=false) const =0
virtual ExportJob * secretKeyExportJob(bool armor=false, const QString &charset=QString()) const =0
virtual QString name() const =0
virtual KeyListJob * keyListJob(bool remote=false, bool includeSigs=false, bool validate=false) const =0
const char * enumerateProtocols(int i) const
bool checkForChiasmus(QString *reason=0) const
QString displayName() const
bool supportsProtocol(const char *name) const
virtual EncryptJob * encryptJob(bool armor=false, bool textmode=false) const =0
virtual ListAllKeysJob * listAllKeysJob(bool includeSigs=false, bool validate=false) const =0
bool checkForProtocol(const char *name, QString *reason=0) const