Page MenuHome GnuPG

No OneTemporary

This document is not UTF8. It was detected as Shift JIS and converted to UTF8 for display.
diff --git a/src/cryptoconfig.h b/src/cryptoconfig.h
index fb7445a..a3e8d30 100644
--- a/src/cryptoconfig.h
+++ b/src/cryptoconfig.h
@@ -1,347 +1,346 @@
/*
cryptoconfig.h
This file is part of libkleopatra, the KDE keymanagement library
Copyright (c) 2004 Klar舁vdalens Datakonsult AB
Libkleopatra is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
Libkleopatra is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
In addition, as a special exception, the copyright holders give
permission to link the code of this program with any edition of
the Qt library by Trolltech AS, Norway (or with modified versions
of Qt that use the same license as Qt), and distribute linked
combinations including the two. You must obey the GNU General
Public License in all respects for all of the code used other than
Qt. If you modify this file, you may extend this exception to
your version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from
your version.
*/
#ifndef CRYPTOCONFIG_H
#define CRYPTOCONFIG_H
#include <kurl.h>
// Start reading this file from the bottom up :)
namespace Kleo {
/**
* Description of a single option
*/
class CryptoConfigEntry {
public:
/**
@li basic This option should always be offered to the user.
@li advanced This option may be offered to advanced users.
@li expert This option should only be offered to expert users.
- @li invisible This option should normally never be displayed,
- not even to expert users.
- @li internal This option is for internal use only. Ignore it.
- // #### should we even have internal in the API, then?
*/
enum Level { Level_Basic = 0,
Level_Advanced = 1,
- Level_Expert = 2,
- Level_Invisible = 3,
- Level_Internal = 4 };
+ Level_Expert = 2 };
/**
Type of the argument
@li ArgType_None The option is set or not set, but no argument.
@li ArgType_String An unformatted string.
@li ArgType_Int A signed integer number.
@li ArgType_UInt An unsigned integer number.
@li ArgType_Path A string that describes the pathname of a file.
The file does not necessarily need to exist.
Separated from string so that e.g. a KURLRequester can be used.
@li ArgType_URL A URL
+ @li ArgType_LDAPURL A LDAP URL
+ Separated from URL so that a more specific widget can be shown, hiding the url syntax
*/
enum ArgType { ArgType_None = 0,
ArgType_String = 1,
ArgType_Int = 2,
ArgType_UInt = 3,
ArgType_Path = 4,
- ArgType_URL = 5 };
+ ArgType_URL = 5,
+ ArgType_LDAPURL = 6 };
virtual ~CryptoConfigEntry() {}
/**
* @return user-visible description of this entry
*/
virtual QString description() const = 0;
/**
* @return true if the argument is optional
*/
virtual bool isOptional() const = 0;
/**
* @return true if the argument can be given multiple times
*/
virtual bool isList() const = 0;
/**
* @return true if the argument can be changed at runtime
*/
virtual bool isRuntime() const = 0;
/**
* User level
*/
virtual Level level() const = 0;
/**
* Argument type
*/
virtual ArgType argType() const = 0;
/**
* Return true if the option is set, i.e. different from default
*/
virtual bool isSet() const = 0;
/**
* Return value as a bool (only allowed for ArgType_None)
*/
virtual bool boolValue() const = 0;
/**
* Return value as a string (mostly meaningful for String, Path and URL argtypes)
* The returned string can be empty (explicitely set to empty) or null (not set).
*/
virtual QString stringValue() const = 0;
/**
* Return value as a signed int
*/
virtual int intValue() const = 0;
/**
* Return value as an unsigned int
*/
virtual unsigned int uintValue() const = 0;
/**
* Return value as a URL (only meaningful for Path and URL argtypes)
*/
virtual KURL urlValue() const = 0;
/**
* Return number of times the option is set (only valid for ArgType_None, if isList())
*/
virtual unsigned int numberOfTimesSet() const = 0;
/**
* Return value as a list of strings (mostly meaningful for String, Path and URL argtypes, if isList())
*/
virtual QStringList stringValueList() const = 0;
/**
* Return value as a list of signed ints
*/
virtual QValueList<int> intValueList() const = 0;
/**
* Return value as a list of unsigned ints
*/
virtual QValueList<unsigned int> uintValueList() const = 0;
/**
* Return value as a list of URLs (only meaningful for Path and URL argtypes, if isList())
*/
virtual KURL::List urlValueList() const = 0;
/**
* Reset an option to its default value
*/
virtual void resetToDefault() = 0;
/**
* Define whether the option is set or not (only allowed for ArgType_None)
* #### TODO: and for options with optional args
*/
virtual void setBoolValue( bool ) = 0;
/**
* Set string value (only allowed for String, Path and URL argtypes)
*/
virtual void setStringValue( const QString& ) = 0;
/**
* Set a new signed int value
*/
virtual void setIntValue( int ) = 0;
/**
* Set a new unsigned int value
*/
virtual void setUIntValue( unsigned int ) = 0;
/**
* Set value as a URL (only meaningful for Path (if local) and URL argtypes)
*/
virtual void setURLValue( const KURL& ) = 0;
/**
* Set the number of times the option is set (only valid for ArgType_None, if isList())
*/
virtual void setNumberOfTimesSet( unsigned int ) = 0;
/**
* Set a new string-list value (only allowed for String, Path and URL argtypes, if isList())
*/
virtual void setStringValueList( const QStringList& ) = 0;
/**
* Set a new list of signed int values
*/
virtual void setIntValueList( const QValueList<int>& ) = 0;
/**
* Set a new list of unsigned int values
*/
virtual void setUIntValueList( const QValueList<unsigned int>& ) = 0;
/**
* Set value as a URL list (only meaningful for Path (if all URLs are local) and URL argtypes, if isList())
*/
virtual void setURLValueList( const KURL::List& ) = 0;
/**
* @return true if the value was changed
*/
virtual bool isDirty() const = 0;
};
/**
* Group containing a set of config options
*/
class CryptoConfigGroup {
public:
virtual ~CryptoConfigGroup() {}
/**
* @return user-visible description of this entry
*/
virtual QString description() const = 0;
/**
* User level
*/
virtual CryptoConfigEntry::Level level() const = 0;
/**
* Returns the list of entries that are known by this group.
*
* @return list of group entry names.
**/
virtual QStringList entryList() const = 0;
/**
* @return the configuration object for a given entry in this group
* The object is owned by CryptoConfigGroup, don't delete it.
* Groups cannot be nested, so all entries returned here are pure entries, no groups.
*/
virtual CryptoConfigEntry* entry( const QString& name ) const = 0;
};
/**
* Crypto config for one component (e.g. gpg-agent, dirmngr etc.)
*/
class CryptoConfigComponent {
public:
virtual ~CryptoConfigComponent() {}
/**
* Return user-visible description of this component
*/
virtual QString description() const = 0;
/**
* Returns the list of groups that are known about.
*
* @return list of group names. One of them can be "<nogroup>", which is the group where all
* "toplevel" options (belonging to no group) are.
*/
virtual QStringList groupList() const = 0;
/**
* @return the configuration object for a given group
* The object is owned by CryptoConfigComponent, don't delete it.
*/
virtual CryptoConfigGroup* group( const QString& name ) const = 0;
};
/**
* Main interface to crypto configuration.
*/
class CryptoConfig {
public:
virtual ~CryptoConfig() {}
/**
* Returns the list of known components (e.g. "gpg-agent", "dirmngr" etc.).
* Use @ref component() to retrieve more information about each one.
* @return list of component names.
**/
virtual QStringList componentList() const = 0;
/**
* @return the configuration object for a given component
* The object is owned by CryptoConfig, don't delete it.
*/
virtual CryptoConfigComponent* component( const QString& name ) const = 0;
/**
* Convenience method to get hold of a single configuration entry when
* its component, group and name are known. This can be used to read
* the value and/or to set a value to it.
*
* @return the configuration object for a single configuration entry, 0 if not found.
* The object is owned by CryptoConfig, don't delete it.
*/
CryptoConfigEntry* entry( const QString& componentName, const QString& groupName, const QString& entryName ) const {
const Kleo::CryptoConfigComponent* comp = component( componentName );
const Kleo::CryptoConfigGroup* group = comp ? comp->group( groupName ) : 0;
return group ? group->entry( entryName ) : 0;
}
/**
* Write back changes
*
* @param runtime If this option is set, the changes will take effect at run-time, as
* far as this is possible. Otherwise, they will take effect at the next
* start of the respective backend programs.
*/
virtual void sync( bool runtime ) = 0;
/**
- * Tells the CryptoConfig to discard any cached information.
+ * Tells the CryptoConfig to discard any cached information, including
+ * all components, groups and entries.
* Call this to free some memory when you won't be using the object
* for some time.
+ * DON'T call this if you're holding pointers to components, groups or entries.
*/
virtual void clear() = 0;
};
};
#endif /* CRYPTOCONFIG_H */
diff --git a/src/qgpgmecryptoconfig.cpp b/src/qgpgmecryptoconfig.cpp
index 0dd5907..3271dc0 100644
--- a/src/qgpgmecryptoconfig.cpp
+++ b/src/qgpgmecryptoconfig.cpp
@@ -1,702 +1,738 @@
/*
qgpgmecryptoconfig.cpp
This file is part of libkleopatra, the KDE keymanagement library
Copyright (c) 2004 Klar舁vdalens Datakonsult AB
Libkleopatra is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
Libkleopatra is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
In addition, as a special exception, the copyright holders give
permission to link the code of this program with any edition of
the Qt library by Trolltech AS, Norway (or with modified versions
of Qt that use the same license as Qt), and distribute linked
combinations including the two. You must obey the GNU General
Public License in all respects for all of the code used other than
Qt. If you modify this file, you may extend this exception to
your version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from
your version.
*/
#include "qgpgmecryptoconfig.h"
#include <kdebug.h>
#include <kprocio.h>
#include <errno.h>
#include <kmessagebox.h>
#include <klocale.h>
#include <assert.h>
#include <ktempfile.h>
#include <qfile.h>
#include <stdlib.h>
-// TODO: runtime changes from other apps? Is that possible to support (other than manual clear()?)
+// Just for the Q_ASSERT in the dtor. Not thread-safe, but who would
+// have 2 threads talking to gpgconf anyway? :)
+static bool s_duringClear = false;
#define GPGCONF_FLAG_GROUP 1
#define GPGCONF_FLAG_OPTIONAL 2
#define GPGCONF_FLAG_LIST 4
#define GPGCONF_FLAG_RUNTIME 8
#define GPGCONF_FLAG_DEFAULT 16 // fixed default value available
#define GPGCONF_FLAG_DEFAULT_DESC 32 // runtime default value available
#define GPGCONF_FLAG_NOARG_DESC 64 // option with optional arg; special meaning if no arg set
+// Change size of mFlags bitfield if adding new values here
QGpgMECryptoConfig::QGpgMECryptoConfig( bool showErrors )
: mComponents( 7 ), mParsed( false )
{
mComponents.setAutoDelete( true );
runGpgConf( showErrors );
}
QGpgMECryptoConfig::~QGpgMECryptoConfig()
{
}
void QGpgMECryptoConfig::runGpgConf( bool showErrors )
{
// Run gpgconf --list-components to make the list of components
KProcIO proc;
proc << "gpgconf"; // must be in the PATH
proc << "--list-components";
QObject::connect( &proc, SIGNAL( readReady(KProcIO*) ),
this, SLOT( slotCollectStdOut(KProcIO*) ) );
// run the process:
int rc = 0;
if ( !proc.start( KProcess::Block ) )
rc = -1;
else
rc = ( proc.normalExit() ) ? proc.exitStatus() : -1 ;
// handle errors, if any (and if requested)
if ( showErrors && rc != 0 ) {
QString wmsg = i18n("<qt>Failed to execute gpgconf:<br>%1</qt>").arg( strerror(rc) );
KMessageBox::error(0, wmsg);
}
mParsed = true;
}
void QGpgMECryptoConfig::slotCollectStdOut( KProcIO* proc )
{
QString line;
int result;
while( ( result = proc->readln(line) ) != -1 ) {
//kdDebug(5150) << "GOT LINE:" << line << endl;
// Format: NAME:DESCRIPTION
QStringList lst = QStringList::split( ':', line, true );
if ( lst.count() >= 2 ) {
mComponents.insert( lst[0], new QGpgMECryptoConfigComponent( this, lst[0], lst[1] ) );
} else {
kdWarning(5150) << "Parse error on gpgconf --list-components output: " << line << endl;
}
}
}
QStringList QGpgMECryptoConfig::componentList() const
{
if ( !mParsed )
const_cast<QGpgMECryptoConfig*>( this )->runGpgConf( false );
QDictIterator<QGpgMECryptoConfigComponent> it( mComponents );
QStringList names;
for( ; it.current(); ++it )
names.push_back( it.currentKey() );
return names;
}
Kleo::CryptoConfigComponent* QGpgMECryptoConfig::component( const QString& name ) const
{
if ( !mParsed )
const_cast<QGpgMECryptoConfig*>( this )->runGpgConf( false );
return mComponents.find( name );
}
void QGpgMECryptoConfig::sync( bool runtime )
{
QDictIterator<QGpgMECryptoConfigComponent> it( mComponents );
for( ; it.current(); ++it )
it.current()->sync( runtime );
}
void QGpgMECryptoConfig::clear()
{
+ s_duringClear = true;
mComponents.clear();
+ s_duringClear = false;
mParsed = false; // next call to componentList/component will need to run gpgconf again
}
////
QGpgMECryptoConfigComponent::QGpgMECryptoConfigComponent( QGpgMECryptoConfig*, const QString& name, const QString& description )
: mGroups( 7 ), mName( name ), mDescription( description )
{
mGroups.setAutoDelete( true );
runGpgConf();
}
QGpgMECryptoConfigComponent::~QGpgMECryptoConfigComponent()
{
}
void QGpgMECryptoConfigComponent::runGpgConf()
{
// Run gpgconf --list-options <component>, and create all groups and entries for that component
KProcIO proc;
proc << "gpgconf"; // must be in the PATH
proc << "--list-options";
proc << mName;
QObject::connect( &proc, SIGNAL( readReady(KProcIO*) ),
this, SLOT( slotCollectStdOut(KProcIO*) ) );
mCurrentGroup = 0;
// run the process:
int rc = 0;
if ( !proc.start( KProcess::Block ) )
rc = -1;
else
rc = ( proc.normalExit() ) ? proc.exitStatus() : -1 ;
if( rc != 0 ) // Can it really be non-0, when gpg-config --list-components worked?
kdWarning(5150) << k_funcinfo << ":" << strerror( rc ) << endl;
}
void QGpgMECryptoConfigComponent::slotCollectStdOut( KProcIO* proc )
{
QString line;
int result;
while( ( result = proc->readln(line) ) != -1 ) {
//kdDebug(5150) << "GOT LINE:" << line << endl;
// Format: NAME:FLAGS:LEVEL:DESCRIPTION:TYPE:ALT-TYPE:ARGNAME:DEFAULT:ARGDEF:VALUE
QStringList lst = QStringList::split( ':', line, true );
if ( lst.count() >= 10 ) {
int flags = lst[1].toInt();
int level = lst[2].toInt();
+ if ( level > 2 ) // invisible or internal -> skip it;
+ continue;
if ( flags & GPGCONF_FLAG_GROUP ) {
if ( mCurrentGroup && !mCurrentGroup->mEntries.isEmpty() ) // only add non-empty groups
mGroups.insert( mCurrentGroupName, mCurrentGroup );
//else
// kdDebug(5150) << "Discarding empty group " << mCurrentGroupName << endl;
mCurrentGroup = new QGpgMECryptoConfigGroup( lst[3], level );
mCurrentGroupName = lst[0];
} else {
// normal entry
if ( !mCurrentGroup ) { // first toplevel entry -> create toplevel group
mCurrentGroup = new QGpgMECryptoConfigGroup( QString::null, 0 );
mCurrentGroupName = "<nogroup>";
}
mCurrentGroup->mEntries.insert( lst[0], new QGpgMECryptoConfigEntry( lst ) );
}
} else {
// This happens on lines like
// dirmngr[31465]: error opening `/home/dfaure/.gnupg/dirmngr_ldapservers.conf': No such file or directory
// so let's not bother the user with it.
//kdWarning(5150) << "Parse error on gpgconf --list-options output: " << line << endl;
}
}
}
QStringList QGpgMECryptoConfigComponent::groupList() const
{
QDictIterator<QGpgMECryptoConfigGroup> it( mGroups );
QStringList names;
for( ; it.current(); ++it )
names.push_back( it.currentKey() );
return names;
}
Kleo::CryptoConfigGroup* QGpgMECryptoConfigComponent::group(const QString& name ) const
{
return mGroups.find( name );
}
void QGpgMECryptoConfigComponent::sync( bool runtime )
{
KTempFile tmpFile;
tmpFile.setAutoDelete( true );
// Collect all dirty entries
bool foundOne = false;
QDictIterator<QGpgMECryptoConfigGroup> groupit( mGroups );
for( ; groupit.current(); ++groupit ) {
QDictIterator<QGpgMECryptoConfigEntry> it( groupit.current()->mEntries );
for( ; it.current(); ++it ) {
if ( it.current()->isDirty() ) {
foundOne = true;
// OK, we can set it.currentKey() to it.current()->outputString()
QString line = it.currentKey();
if ( it.current()->isSet() ) { // set option
line += ":0:";
line += it.current()->outputString();
} else { // unset option
line += ":16:";
}
line += '\n';
QCString line8bit = line.latin1(); // latin1 is correct here, it's all escaped (and KProcIO uses latin1 when reading).
tmpFile.file()->writeBlock( line8bit.data(), line8bit.size()-1 /*no 0*/ );
it.current()->setDirty( false ); // ### move to after running gpgconf, on success only?
}
}
}
tmpFile.close();
if ( !foundOne )
return;
// Call gpgconf --change-options <component>
QString commandLine = "gpgconf";
if ( runtime )
commandLine += " --runtime";
commandLine += " --change-options ";
commandLine += KProcess::quote( mName );
commandLine += " < ";
commandLine += KProcess::quote( tmpFile.name() );
//kdDebug(5150) << commandLine << endl;
//system( QCString( "cat " ) + tmpFile.name().latin1() ); // DEBUG
KProcess proc;
proc.setUseShell( true );
proc << commandLine;
// run the process:
int rc = 0;
if ( !proc.start( KProcess::Block ) )
rc = -1;
else
rc = ( proc.normalExit() ) ? proc.exitStatus() : -1 ;
// ####### TODO error handling (message box).
if( rc != 0 ) // Happens due to bugs in gpgconf (e.g. issue104)
kdWarning(5150) << k_funcinfo << ":" << strerror( rc ) << endl;
}
////
QGpgMECryptoConfigGroup::QGpgMECryptoConfigGroup( const QString& description, int level )
: mEntries( 29 ),
mDescription( description ),
mLevel( static_cast<Kleo::CryptoConfigEntry::Level>( level ) )
{
mEntries.setAutoDelete( true );
}
QStringList QGpgMECryptoConfigGroup::entryList() const
{
QDictIterator<QGpgMECryptoConfigEntry> it( mEntries );
QStringList names;
for( ; it.current(); ++it )
names.push_back( it.currentKey() );
return names;
}
Kleo::CryptoConfigEntry* QGpgMECryptoConfigGroup::entry( const QString& name ) const
{
return mEntries.find( name );
}
////
static QString gpgconf_unescape( const QString& str )
{
// Looks like it's the same rules as KURL.
return KURL::decode_string( str );
}
static QString gpgconf_escape( const QString& str )
{
// ####### FIXME: this should escape ":" and "," in lists, but not much more?
return KURL::encode_string( str );
}
// gpgconf arg type number -> CryptoConfigEntry arg type enum mapping
static Kleo::CryptoConfigEntry::ArgType knownArgType( int argType, bool& ok ) {
ok = true;
switch( argType ) {
case 0: // none
return Kleo::CryptoConfigEntry::ArgType_None;
case 1: // string
return Kleo::CryptoConfigEntry::ArgType_String;
case 2: // int32
return Kleo::CryptoConfigEntry::ArgType_Int;
case 3: // uint32
return Kleo::CryptoConfigEntry::ArgType_UInt;
case 32: // pathname
return Kleo::CryptoConfigEntry::ArgType_Path;
case 33: // ldap server
- return Kleo::CryptoConfigEntry::ArgType_URL;
+ return Kleo::CryptoConfigEntry::ArgType_LDAPURL;
default:
ok = false;
return Kleo::CryptoConfigEntry::ArgType_None;
}
}
QGpgMECryptoConfigEntry::QGpgMECryptoConfigEntry( const QStringList& parsedLine )
{
// Format: NAME:FLAGS:LEVEL:DESCRIPTION:TYPE:ALT-TYPE:ARGNAME:DEFAULT:ARGDEF:VALUE
assert( parsedLine.count() >= 10 ); // called checked for it already
QStringList::const_iterator it = parsedLine.begin();
++it; // skip name, stored in group
mFlags = (*it++).toInt();
mLevel = (*it++).toInt();
mDescription = (*it++);
bool ok;
// we keep the real (int) arg type, since it influences the parsing (e.g. for ldap urls)
mRealArgType = (*it++).toInt();
mArgType = knownArgType( mRealArgType, ok );
if ( !ok && !(*it).isEmpty() ) {
// use ALT-TYPE
mRealArgType = (*it).toInt();
mArgType = knownArgType( mRealArgType, ok );
}
if ( !ok )
kdWarning(5150) << "Unsupported datatype: " << parsedLine[4] << " : " << *it << " for " << parsedLine[0] << endl;
++it; // done with alt-type
++it; // skip argname (not useful in GUIs)
mSet = false;
QString value;
- if ( mFlags & GPGCONF_FLAG_DEFAULT )
+ if ( mFlags & GPGCONF_FLAG_DEFAULT ) {
value = *it; // get default value
+ mDefaultValue = stringToValue( value );
+ }
++it; // done with DEFAULT
++it; // ### skip ARGDEF for now. It's only for options with an "optional arg"
//kdDebug(5150) << "Entry " << parsedLine[0] << " val=" << *it << endl;
if ( !(*it).isEmpty() ) { // a real value was set
mSet = true;
value = *it;
+ mValue = stringToValue( value );
+ }
+ else {
+ mValue = mDefaultValue;
}
- bool isString = ( mArgType == Kleo::CryptoConfigEntry::ArgType_String
- || mArgType == Kleo::CryptoConfigEntry::ArgType_Path
- || mArgType == Kleo::CryptoConfigEntry::ArgType_URL );
+ mDirty = false;
+}
+
+QVariant QGpgMECryptoConfigEntry::stringToValue( const QString& value ) const
+{
+ bool isString = isStringType();
if ( isList() ) {
QValueList<QVariant> lst;
QStringList items = QStringList::split( ',', value );
for( QStringList::const_iterator valit = items.begin(); valit != items.end(); ++valit ) {
QString val = *valit;
if ( isString ) {
if ( val.isEmpty() )
lst << QString::null;
else {
Q_ASSERT( val[0] == '"' ); // see README.gpgconf
lst << QVariant( gpgconf_unescape( val.mid( 1 ) ) );
}
} else
lst << QVariant( gpgconf_unescape( val ) );
}
- mValue = lst;
+ return lst;
} else { // not a list
if ( isString ) {
if ( value.isEmpty() )
- mValue = QVariant( QString::null ); // not set [ok with lists too?]
+ return QVariant( QString::null ); // not set [ok with lists too?]
else {
Q_ASSERT( value[0] == '"' ); // see README.gpgconf
- mValue = QVariant( gpgconf_unescape( value.mid( 1 ) ) );
+ return QVariant( gpgconf_unescape( value.mid( 1 ) ) );
}
} else {
- mValue = QVariant( gpgconf_unescape( value ) );
+ return QVariant( gpgconf_unescape( value ) );
}
}
- mDirty = false;
}
QGpgMECryptoConfigEntry::~QGpgMECryptoConfigEntry()
{
- Q_ASSERT( !mDirty ); // forgot to call sync()? :)
+#ifndef NDEBUG
+ if ( !s_duringClear && !mDirty )
+ kdWarning(5150) << "Deleting a QGpgMECryptoConfigEntry that was modified. You forgot to call sync() (to commit) or clear() (to discard)" << endl;
+#endif
}
bool QGpgMECryptoConfigEntry::isOptional() const
{
return mFlags & GPGCONF_FLAG_OPTIONAL;
}
bool QGpgMECryptoConfigEntry::isList() const
{
return mFlags & GPGCONF_FLAG_LIST;
}
bool QGpgMECryptoConfigEntry::isRuntime() const
{
return mFlags & GPGCONF_FLAG_RUNTIME;
}
bool QGpgMECryptoConfigEntry::isSet() const
{
return mSet;
}
bool QGpgMECryptoConfigEntry::boolValue() const
{
Q_ASSERT( mArgType == ArgType_None );
Q_ASSERT( !isList() );
return mValue.toBool();
}
QString QGpgMECryptoConfigEntry::stringValue() const
{
- Q_ASSERT( mArgType == ArgType_String || mArgType == ArgType_Path || mArgType == ArgType_URL );
+ Q_ASSERT( isStringType() );
Q_ASSERT( !isList() );
return mValue.toString();
}
int QGpgMECryptoConfigEntry::intValue() const
{
Q_ASSERT( mArgType == ArgType_Int );
Q_ASSERT( !isList() );
return mValue.toInt();
}
unsigned int QGpgMECryptoConfigEntry::uintValue() const
{
Q_ASSERT( mArgType == ArgType_UInt );
Q_ASSERT( !isList() );
return mValue.toUInt();
}
static KURL parseURL( int mRealArgType, const QString& str )
{
if ( mRealArgType == 33 ) { // LDAP server
// The format is HOSTNAME:PORT:USERNAME:PASSWORD:BASE_DN
QStringList items = QStringList::split( ':', str, true );
if ( items.count() == 5 ) {
QStringList::const_iterator it = items.begin();
KURL url;
url.setProtocol( "ldap" );
url.setHost( *it++ );
url.setPort( (*it++).toInt() );
url.setUser( *it++ );
url.setPass( *it++ );
url.setQuery( *it );
return url;
} else
kdWarning(5150) << "parseURL: malformed LDAP server: " << str << endl;
}
// other URLs : assume wellformed URL syntax.
return KURL( str );
}
// The opposite of parseURL
static QString splitURL( int mRealArgType, const KURL& url )
{
if ( mRealArgType == 33 ) { // LDAP server
// The format is HOSTNAME:PORT:USERNAME:PASSWORD:BASE_DN
// Not sure about encoding/escaping here. It will all be escaped when passing to gpgconf, but what if the pass contains ':'?
return url.host() + ":" + QString::number( url.port() ) + ":" + url.user() + ":" + url.pass() + ":" + url.query().mid(1);
}
return url.path();
}
KURL QGpgMECryptoConfigEntry::urlValue() const
{
- Q_ASSERT( mArgType == ArgType_Path || mArgType == ArgType_URL );
+ Q_ASSERT( mArgType == ArgType_Path || mArgType == ArgType_URL || mArgType == ArgType_LDAPURL );
Q_ASSERT( !isList() );
QString str = mValue.toString();
- if ( mArgType == ArgType_URL )
- return parseURL( mRealArgType, str );
- KURL url;
- url.setPath( str );
- return url;
+ if ( mArgType == ArgType_Path )
+ {
+ KURL url;
+ url.setPath( str );
+ return url;
+ }
+ return parseURL( mRealArgType, str );
}
unsigned int QGpgMECryptoConfigEntry::numberOfTimesSet() const
{
Q_ASSERT( mArgType == ArgType_None );
Q_ASSERT( isList() );
return mValue.toUInt();
}
QStringList QGpgMECryptoConfigEntry::stringValueList() const
{
- Q_ASSERT( mArgType == ArgType_String || mArgType == ArgType_Path || mArgType == ArgType_URL );
+ Q_ASSERT( isStringType() );
Q_ASSERT( isList() );
return mValue.toStringList();
}
QValueList<int> QGpgMECryptoConfigEntry::intValueList() const
{
Q_ASSERT( mArgType == ArgType_Int );
Q_ASSERT( isList() );
QValueList<int> ret;
QValueList<QVariant> lst = mValue.toList();
for( QValueList<QVariant>::const_iterator it = lst.begin(); it != lst.end(); ++it ) {
ret.append( (*it).toInt() );
}
return ret;
}
QValueList<unsigned int> QGpgMECryptoConfigEntry::uintValueList() const
{
Q_ASSERT( mArgType == ArgType_UInt );
Q_ASSERT( isList() );
QValueList<unsigned int> ret;
QValueList<QVariant> lst = mValue.toList();
for( QValueList<QVariant>::const_iterator it = lst.begin(); it != lst.end(); ++it ) {
ret.append( (*it).toUInt() );
}
return ret;
}
KURL::List QGpgMECryptoConfigEntry::urlValueList() const
{
- Q_ASSERT( mArgType == ArgType_Path || mArgType == ArgType_URL );
+ Q_ASSERT( mArgType == ArgType_Path || mArgType == ArgType_URL || mArgType == ArgType_LDAPURL );
Q_ASSERT( isList() );
QStringList lst = mValue.toStringList();
KURL::List ret;
for( QStringList::const_iterator it = lst.begin(); it != lst.end(); ++it ) {
- if ( mArgType == ArgType_URL )
- ret << parseURL( mRealArgType, *it );
- else {
+ if ( mArgType == ArgType_Path ) {
KURL url;
url.setPath( *it );
ret << url;
+ } else {
+ ret << parseURL( mRealArgType, *it );
}
}
return ret;
}
void QGpgMECryptoConfigEntry::resetToDefault()
{
mSet = false;
mDirty = true;
+ if ( mFlags & GPGCONF_FLAG_DEFAULT )
+ mValue = mDefaultValue;
+ else if ( mArgType == ArgType_None )
+ mValue = false;
}
void QGpgMECryptoConfigEntry::setBoolValue( bool b )
{
Q_ASSERT( mArgType == ArgType_None );
Q_ASSERT( !isList() );
mValue = b;
mSet = true;
mDirty = true;
}
void QGpgMECryptoConfigEntry::setStringValue( const QString& str )
{
- Q_ASSERT( mArgType == ArgType_String || mArgType == ArgType_Path || mArgType == ArgType_URL );
+ Q_ASSERT( isStringType() );
Q_ASSERT( !isList() );
mValue = str;
mSet = true;
mDirty = true;
}
void QGpgMECryptoConfigEntry::setIntValue( int i )
{
Q_ASSERT( mArgType == ArgType_Int );
Q_ASSERT( !isList() );
mValue = i;
mSet = true;
mDirty = true;
}
void QGpgMECryptoConfigEntry::setUIntValue( unsigned int i )
{
mValue = i;
mSet = true;
mDirty = true;
}
void QGpgMECryptoConfigEntry::setURLValue( const KURL& url )
{
QString str = splitURL( mRealArgType, url );
mValue = str;
mSet = true;
mDirty = true;
}
void QGpgMECryptoConfigEntry::setNumberOfTimesSet( unsigned int i )
{
Q_ASSERT( mArgType == ArgType_None );
Q_ASSERT( isList() );
setUIntValue( i );
}
void QGpgMECryptoConfigEntry::setStringValueList( const QStringList& lst )
{
mValue = lst;
mSet = true;
mDirty = true;
}
void QGpgMECryptoConfigEntry::setIntValueList( const QValueList<int>& lst )
{
QValueList<QVariant> ret;
for( QValueList<int>::const_iterator it = lst.begin(); it != lst.end(); ++it ) {
ret << QVariant( *it );
}
mValue = ret;
mSet = true;
mDirty = true;
}
void QGpgMECryptoConfigEntry::setUIntValueList( const QValueList<unsigned int>& lst )
{
QValueList<QVariant> ret;
for( QValueList<unsigned int>::const_iterator it = lst.begin(); it != lst.end(); ++it ) {
ret << QVariant( *it );
}
mValue = ret;
mSet = true;
mDirty = true;
}
void QGpgMECryptoConfigEntry::setURLValueList( const KURL::List& urls )
{
QStringList lst;
for( KURL::List::const_iterator it = urls.begin(); it != urls.end(); ++it ) {
lst << splitURL( mRealArgType, *it );
}
mValue = lst;
mSet = true;
mDirty = true;
}
QString QGpgMECryptoConfigEntry::outputString() const
{
Q_ASSERT( mSet );
// Basically the opposite of the constructor
- bool isString = ( mArgType == Kleo::CryptoConfigEntry::ArgType_String
- || mArgType == Kleo::CryptoConfigEntry::ArgType_Path
- || mArgType == Kleo::CryptoConfigEntry::ArgType_URL );
- if ( isString ) {
+ if ( isStringType() ) {
if ( mValue.isNull() )
return QString::null;
else if ( isList() ) { // string list
QStringList lst = mValue.toStringList();
for( QStringList::iterator it = lst.begin(); it != lst.end(); ++it ) {
if ( !(*it).isNull() )
*it = gpgconf_escape( *it ).prepend( "\"" );
}
QString res = lst.join( "," );
kdDebug(5150) << res << endl;
return res;
} else // normal string
return gpgconf_escape( mValue.toString() ).prepend( "\"" );
}
- if ( !isList() )
- return mValue.toString(); // works for ints and bools
+ if ( !isList() ) // non-list non-string
+ {
+ if ( mArgType == ArgType_None ) {
+ return mValue.toBool() ? QString::fromLatin1( "1" ) : QString::null;
+ } else { // some int
+ Q_ASSERT( mArgType == ArgType_Int || mArgType == ArgType_UInt );
+ return mValue.toString(); // int to string conversion
+ }
+ }
// Lists
if ( mArgType == ArgType_None )
return QString::number( numberOfTimesSet() );
QStringList ret;
QValueList<QVariant> lst = mValue.toList();
for( QValueList<QVariant>::const_iterator it = lst.begin(); it != lst.end(); ++it ) {
ret << (*it).toString(); // QVariant does the conversion
}
return ret.join( "," );
}
+bool QGpgMECryptoConfigEntry::isStringType() const
+{
+ return ( mArgType == Kleo::CryptoConfigEntry::ArgType_String
+ || mArgType == Kleo::CryptoConfigEntry::ArgType_Path
+ || mArgType == Kleo::CryptoConfigEntry::ArgType_URL
+ || mArgType == Kleo::CryptoConfigEntry::ArgType_LDAPURL );
+}
+
#include "qgpgmecryptoconfig.moc"
diff --git a/src/qgpgmecryptoconfig.h b/src/qgpgmecryptoconfig.h
index e50956c..996beb3 100644
--- a/src/qgpgmecryptoconfig.h
+++ b/src/qgpgmecryptoconfig.h
@@ -1,172 +1,176 @@
/*
qgpgmecryptoconfig.h
This file is part of libkleopatra, the KDE keymanagement library
Copyright (c) 2004 Klar舁vdalens Datakonsult AB
Libkleopatra is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
Libkleopatra is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
In addition, as a special exception, the copyright holders give
permission to link the code of this program with any edition of
the Qt library by Trolltech AS, Norway (or with modified versions
of Qt that use the same license as Qt), and distribute linked
combinations including the two. You must obey the GNU General
Public License in all respects for all of the code used other than
Qt. If you modify this file, you may extend this exception to
your version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from
your version.
*/
#ifndef KLEO_QGPGMECRYPTOCONFIG_H
#define KLEO_QGPGMECRYPTOCONFIG_H
#include <kleo/cryptoconfig.h>
#include <qdict.h>
#include <qstringlist.h>
#include <qobject.h>
#include <qvariant.h>
class KProcIO;
class QGpgMECryptoConfigComponent;
class QGpgMECryptoConfigEntry;
/**
* CryptoConfig implementation around the gpgconf command-line tool
* For method docu, see kleo/cryptoconfig.h
*/
class QGpgMECryptoConfig : public QObject, public Kleo::CryptoConfig {
Q_OBJECT
public:
/**
* Constructor
* @param showErrors if true, a messagebox will be shown if e.g. gpgconf wasn't found
*/
QGpgMECryptoConfig( bool showErrors = true );
virtual ~QGpgMECryptoConfig();
virtual QStringList componentList() const;
virtual Kleo::CryptoConfigComponent* component( const QString& name ) const;
virtual void clear();
virtual void sync( bool runtime );
private slots:
void slotCollectStdOut( KProcIO* proc );
private:
void runGpgConf( bool showErrors );
private:
QDict<QGpgMECryptoConfigComponent> mComponents;
bool mParsed;
};
class QGpgMECryptoConfigGroup;
/// For docu, see kleo/cryptoconfig.h
class QGpgMECryptoConfigComponent : public QObject, public Kleo::CryptoConfigComponent {
Q_OBJECT
public:
QGpgMECryptoConfigComponent( QGpgMECryptoConfig*, const QString& name, const QString& description );
virtual ~QGpgMECryptoConfigComponent();
virtual QString description() const { return mDescription; }
virtual QStringList groupList() const;
virtual Kleo::CryptoConfigGroup* group( const QString& name ) const;
void sync( bool runtime );
private slots:
void slotCollectStdOut( KProcIO* proc );
private:
void runGpgConf();
private:
QDict<QGpgMECryptoConfigGroup> mGroups;
QString mName;
QString mDescription;
QGpgMECryptoConfigGroup* mCurrentGroup; // during parsing
QString mCurrentGroupName; // during parsing
};
class QGpgMECryptoConfigGroup : public Kleo::CryptoConfigGroup {
public:
QGpgMECryptoConfigGroup( const QString& description, int level );
virtual ~QGpgMECryptoConfigGroup() {}
virtual QString description() const { return mDescription; }
virtual Kleo::CryptoConfigEntry::Level level() const { return mLevel; }
virtual QStringList entryList() const;
virtual Kleo::CryptoConfigEntry* entry( const QString& name ) const;
private:
friend class QGpgMECryptoConfigComponent; // it adds the entries
QDict<QGpgMECryptoConfigEntry> mEntries;
QString mDescription;
Kleo::CryptoConfigEntry::Level mLevel;
};
class QGpgMECryptoConfigEntry : public Kleo::CryptoConfigEntry {
public:
QGpgMECryptoConfigEntry( const QStringList& parsedLine );
virtual ~QGpgMECryptoConfigEntry();
virtual QString description() const { return mDescription; }
virtual bool isOptional() const;
virtual bool isList() const;
virtual bool isRuntime() const;
virtual Level level() const { return static_cast<Level>( mLevel ); }
virtual ArgType argType() const { return static_cast<ArgType>( mArgType ); }
virtual bool isSet() const;
virtual bool boolValue() const;
virtual QString stringValue() const;
virtual int intValue() const;
virtual unsigned int uintValue() const;
virtual KURL urlValue() const;
virtual unsigned int numberOfTimesSet() const;
virtual QStringList stringValueList() const;
virtual QValueList<int> intValueList() const;
virtual QValueList<unsigned int> uintValueList() const;
virtual KURL::List urlValueList() const;
virtual void resetToDefault();
virtual void setBoolValue( bool );
virtual void setStringValue( const QString& );
virtual void setIntValue( int );
virtual void setUIntValue( unsigned int );
virtual void setURLValue( const KURL& );
virtual void setNumberOfTimesSet( unsigned int );
virtual void setStringValueList( const QStringList& );
virtual void setIntValueList( const QValueList<int>& );
virtual void setUIntValueList( const QValueList<unsigned int>& );
virtual void setURLValueList( const KURL::List& );
virtual bool isDirty() const { return mDirty; }
void setDirty( bool b ) { mDirty = b; }
QString outputString() const;
+protected:
+ bool isStringType() const;
+ QVariant stringToValue( const QString& value ) const;
private:
QString mDescription;
+ QVariant mDefaultValue;
QVariant mValue;
- uint mFlags : 4; // bitfield with 4 bits
- uint mLevel : 3; // max is 4 -> 3 bits
+ uint mFlags : 7; // bitfield with 7 bits
+ uint mLevel : 3; // max is 4 (2, in fact) -> 3 bits
uint mRealArgType : 6; // max is 33 -> 6 bits
- uint mArgType : 3; // max is 5 (ArgType enum) -> 3 bits;
+ uint mArgType : 3; // max is 6 (ArgType enum) -> 3 bits;
uint mDirty : 1;
uint mSet : 1;
};
#endif /* KLEO_QGPGMECRYPTOCONFIG_H */

File Metadata

Mime Type
text/x-diff
Expires
Sat, Dec 6, 10:57 PM (1 d, 14 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
7b/eb/ab5812a6f5e8d7c2649e115e4cb6

Event Timeline