diff options
author | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
---|---|---|
committer | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
commit | 460c52653ab0dcca6f19a4f492ed2c5e4e963ab0 (patch) | |
tree | 67208f7c145782a7e90b123b982ca78d88cc2c87 /certmanager/lib/backends/qgpgme | |
download | tdepim-460c52653ab0dcca6f19a4f492ed2c5e4e963ab0.tar.gz tdepim-460c52653ab0dcca6f19a4f492ed2c5e4e963ab0.zip |
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdepim@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'certmanager/lib/backends/qgpgme')
41 files changed, 5127 insertions, 0 deletions
diff --git a/certmanager/lib/backends/qgpgme/Makefile.am b/certmanager/lib/backends/qgpgme/Makefile.am new file mode 100644 index 000000000..a7336a029 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/Makefile.am @@ -0,0 +1,34 @@ +#KDE_OPTIONS = nofinal + +INCLUDES = -I$(top_srcdir)/libkdenetwork \ + -I$(top_srcdir)/certmanager/lib \ + $(GPGME_CFLAGS) $(all_includes) + +noinst_LTLIBRARIES = libkleopatra_backend_qgpgme.la + +libkleopatra_backend_qgpgme_la_SOURCES = \ + gnupgprocessbase.cpp \ + qgpgmeprogresstokenmapper.cpp \ + \ + qgpgmebackend.cpp \ + \ + qgpgmejob.cpp \ + \ + qgpgmekeylistjob.cpp \ + qgpgmekeygenerationjob.cpp \ + qgpgmeimportjob.cpp \ + qgpgmeexportjob.cpp \ + qgpgmesecretkeyexportjob.cpp \ + qgpgmedownloadjob.cpp \ + qgpgmedeletejob.cpp \ + qgpgmeencryptjob.cpp \ + qgpgmedecryptjob.cpp \ + qgpgmesignjob.cpp \ + qgpgmeverifydetachedjob.cpp \ + qgpgmeverifyopaquejob.cpp \ + qgpgmesignencryptjob.cpp \ + qgpgmedecryptverifyjob.cpp \ + qgpgmecryptoconfig.cpp \ + qgpgmerefreshkeysjob.cpp + +METASOURCES = AUTO diff --git a/certmanager/lib/backends/qgpgme/gnupgprocessbase.cpp b/certmanager/lib/backends/qgpgme/gnupgprocessbase.cpp new file mode 100644 index 000000000..2d3ca7409 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/gnupgprocessbase.cpp @@ -0,0 +1,198 @@ +/* + gnupgprocessbase.cpp + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 "gnupgprocessbase.h" + +#include <kdebug.h> +#include <kurl.h> + +#include <qsocketnotifier.h> +#include <qtextcodec.h> +#include <qstringlist.h> + +#include <unistd.h> +#include <fcntl.h> +#include <errno.h> +#include <assert.h> + +struct Kleo::GnuPGProcessBase::Private { + Private() : useStatusFD( false ), statnot( 0 ) { + statusFD[0] = statusFD[1] = -1; + } + + bool useStatusFD; + int statusFD[2]; + QSocketNotifier * statnot; + QCString statusBuffer; +}; + + +Kleo::GnuPGProcessBase::GnuPGProcessBase( QObject * parent, const char * name ) + : KProcess( parent, name ) +{ + d = new Private(); +} + +Kleo::GnuPGProcessBase::~GnuPGProcessBase() { + delete d; d = 0; +} + +void Kleo::GnuPGProcessBase::setUseStatusFD( bool use ) { + assert( d ); + d->useStatusFD = use; +} + +bool Kleo::GnuPGProcessBase::start( RunMode runmode, Communication comm ) { + if ( d->useStatusFD ) { + // set up the status-fd. This should be in setupCommunication(), + // but then it's too late: we need the fd of the pipe to pass it + // as argument to the --status-fd option: + // PENDING(marc) find out why KProcess uses both pipe() and socketpair()... + if ( ::pipe( d->statusFD ) < 0 ) { + kdDebug( 5150 ) << "Kleo::GnuPGProcessBase::start: pipe(2) failed: " << perror << endl; + return false; + } + ::fcntl( d->statusFD[0], F_SETFD, FD_CLOEXEC ); + ::fcntl( d->statusFD[1], F_SETFD, FD_CLOEXEC ); + if ( !arguments.empty() ) { + QValueList<QCString>::iterator it = arguments.begin(); + ++it; + arguments.insert( it, "--status-fd" ); + char buf[25]; + sprintf( buf, "%d", d->statusFD[1] ); + arguments.insert( it, buf ); + arguments.insert( it, "--no-tty" ); + //arguments.insert( it, "--enable-progress-filter" ); // gpgsm doesn't know this + } + } + return KProcess::start( runmode, comm ); +} + +int Kleo::GnuPGProcessBase::setupCommunication( Communication comm ) { + if ( int ok = KProcess::setupCommunication( comm ) ) + return ok; + if ( d->useStatusFD ) { + // base class impl returned error, so close our fd's, too + ::close( d->statusFD[0] ); + ::close( d->statusFD[1] ); + d->statusFD[0] = d->statusFD[1] = -1; + } + return 0; // Error +} + +int Kleo::GnuPGProcessBase::commSetupDoneP() { + if ( d->useStatusFD ) { + ::close( d->statusFD[1] ); // close the input end of the pipe, we're the reader + d->statnot = new QSocketNotifier( d->statusFD[0], QSocketNotifier::Read, this ); + connect( d->statnot, SIGNAL(activated(int)), SLOT(slotChildStatus(int)) ); + } + return KProcess::commSetupDoneP(); +} + +int Kleo::GnuPGProcessBase::commSetupDoneC() { + if ( d->useStatusFD ) + ::fcntl( d->statusFD[1], F_SETFD, 0 ); + return KProcess::commSetupDoneC(); +} + +void Kleo::GnuPGProcessBase::slotChildStatus( int fd ) { + if ( !childStatus(fd) ) + closeStatus(); +} + +bool Kleo::GnuPGProcessBase::closeStatus() { + if ( !d->useStatusFD ) + return false; + d->useStatusFD = false; + delete d->statnot; d->statnot = 0; + ::close( d->statusFD[0] ); d->statusFD[0] = -1; + return true; +} + +int Kleo::GnuPGProcessBase::childStatus( int fd ) { + char buf[1024]; + const int len = ::read( fd, buf, sizeof(buf)-1 ); + if ( len > 0 ) { + buf[len] = 0; + d->statusBuffer += buf; + parseStatusOutput(); + } + return len; +} + +static QString fromHexEscapedUtf8( const QCString & str ) { + return KURL::decode_string( str.data(), 106 /* utf-8 */ ); +} + +void Kleo::GnuPGProcessBase::parseStatusOutput() { + static const char startToken[] = "[GNUPG:] "; + static const int startTokenLen = sizeof startToken / sizeof *startToken - 1; + + int lineStart = 0; + for ( int lineEnd = d->statusBuffer.find( '\n' ) ; lineEnd >= 0 ; lineEnd = d->statusBuffer.find( '\n', lineStart = lineEnd+1 ) ) { + // get next line: + const QCString line = d->statusBuffer.mid( lineStart, lineEnd - lineStart ).stripWhiteSpace(); + if ( line.isEmpty() ) + continue; + // check status token + if ( line.left( startTokenLen ) != startToken ) { + kdDebug( 5150 ) << "Kleo::GnuPGProcessBase::childStatus: status-fd protocol error: line doesn't begin with \"" + << startToken << "\"" << endl; + continue; + } + // remove status token: + const QCString command = line.mid( startTokenLen ).simplifyWhiteSpace() + ' '; + if ( command == " " ) { + kdDebug( 5150 ) << "Kleo::GnuPGProcessBase::childStatus: status-fd protocol error: line without content." << endl; + continue; + } + // split into base and args + QString cmd; + QStringList args; + int tagStart = 0; + for ( int tagEnd = command.find( ' ' ) ; tagEnd >= 0 ; tagEnd = command.find( ' ', tagStart = tagEnd+1 ) ) { + const QCString tag = command.mid( tagStart, tagEnd - tagStart ); + if ( cmd.isNull() ) + cmd = fromHexEscapedUtf8( tag ); + else + args.push_back( fromHexEscapedUtf8( tag ) ); + } + emit status( this, cmd, args ); + } + d->statusBuffer = d->statusBuffer.mid( lineStart ); +} + +void Kleo::GnuPGProcessBase::virtual_hook( int id, void * data ) { + KProcess::virtual_hook( id, data ); +} + +#include "gnupgprocessbase.moc" diff --git a/certmanager/lib/backends/qgpgme/gnupgprocessbase.h b/certmanager/lib/backends/qgpgme/gnupgprocessbase.h new file mode 100644 index 000000000..73e31e295 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/gnupgprocessbase.h @@ -0,0 +1,91 @@ +/* + gnupgprocessbase.h + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_GNUPGPROCESSBASE_H__ +#define __KLEO_GNUPGPROCESSBASE_H__ + +#include <kprocess.h> + +namespace Kleo { + + /** + * @short a base class for GPG and GPGSM processes. + * + * This KProcess subclass implements the status-fd handling common + * to GPG and GPGSM. + * + * @author Marc Mutz <mutz@kde.org> + */ + class GnuPGProcessBase : public KProcess { + Q_OBJECT + public: + GnuPGProcessBase( QObject * parent=0, const char * name=0 ); + ~GnuPGProcessBase(); + + void setUseStatusFD( bool use ); + + /*! reimplementation */ + bool start( RunMode runmode, Communication comm ); + + bool closeStatus(); + + signals: + void status( Kleo::GnuPGProcessBase * proc, const QString & type, const QStringList & args ); + + protected: + /*! reimplementation */ + int setupCommunication( Communication comm ); + /*! reimplementation */ + int commSetupDoneP(); + /*! reimplementation */ + int commSetupDoneC(); + + int childStatus( int fd ); + + + private slots: + void slotChildStatus( int fd ); + + private: + void parseStatusOutput(); + + private: + class Private; + Private * d; + protected: + /*! reimplementation */ + void virtual_hook( int id, void * data ); + }; + +} + +#endif // __KLEO_GNUPGPROCESSBASE_H__ diff --git a/certmanager/lib/backends/qgpgme/qgpgmebackend.cpp b/certmanager/lib/backends/qgpgme/qgpgmebackend.cpp new file mode 100644 index 000000000..8256522ae --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmebackend.cpp @@ -0,0 +1,153 @@ +/* -*- mode: C++; c-file-style: "gnu" -*- + qgpgmebackend.cpp + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004,2005 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "qgpgmebackend.h" + +#include "qgpgmecryptoconfig.h" +#include "cryptplugwrapper.h" + +#include <gpgmepp/context.h> +#include <gpgmepp/engineinfo.h> + +#include <klocale.h> +#include <kstandarddirs.h> + +#include <qfile.h> +#include <qstring.h> + +Kleo::QGpgMEBackend::QGpgMEBackend() + : Kleo::CryptoBackend(), + mCryptoConfig( 0 ), + mOpenPGPProtocol( 0 ), + mSMIMEProtocol( 0 ) +{ + GpgME::initializeLibrary(); +} + +Kleo::QGpgMEBackend::~QGpgMEBackend() { + delete mCryptoConfig; mCryptoConfig = 0; + delete mOpenPGPProtocol; mOpenPGPProtocol = 0; + delete mSMIMEProtocol; mSMIMEProtocol = 0; +} + +QString Kleo::QGpgMEBackend::name() const { + return "gpgme"; +} + +QString Kleo::QGpgMEBackend::displayName() const { + return i18n( "GpgME" ); +} + +Kleo::CryptoConfig * Kleo::QGpgMEBackend::config() const { + if ( !mCryptoConfig ) { + static bool hasGpgConf = !KStandardDirs::findExe( "gpgconf" ).isEmpty(); + if ( hasGpgConf ) + mCryptoConfig = new QGpgMECryptoConfig(); + } + return mCryptoConfig; +} + +static bool check( GpgME::Context::Protocol proto, QString * reason ) { + if ( !GpgME::checkEngine( proto ) ) + return true; + if ( !reason ) + return false; + // error, check why: + const GpgME::EngineInfo ei = GpgME::engineInfo( proto ); + if ( ei.isNull() ) + *reason = i18n("GPGME was compiled without support for %1.").arg( proto == GpgME::Context::CMS ? "S/MIME" : "OpenPGP" ); + else if ( ei.fileName() && !ei.version() ) + *reason = i18n("Engine %1 is not installed properly.").arg( QFile::decodeName( ei.fileName() ) ); + else if ( ei.fileName() && ei.version() && ei.requiredVersion() ) + *reason = i18n("Engine %1 version %2 installed, " + "but at least version %3 is required.") + .arg( QFile::decodeName( ei.fileName() ), ei.version(), ei.requiredVersion() ); + else + *reason = i18n("Unknown problem with engine for protocol %1.").arg( proto == GpgME::Context::CMS ? "S/MIME" : "OpenPGP" ); + return false; +} + +bool Kleo::QGpgMEBackend::checkForOpenPGP( QString * reason ) const { + return check( GpgME::Context::OpenPGP, reason ); +} + +bool Kleo::QGpgMEBackend::checkForSMIME( QString * reason ) const { + return check( GpgME::Context::CMS, reason ); +} + +bool Kleo::QGpgMEBackend::checkForProtocol( const char * name, QString * reason ) const { + if ( qstricmp( name, OpenPGP ) == 0 ) + return check( GpgME::Context::OpenPGP, reason ); + if ( qstricmp( name, SMIME ) == 0 ) + return check( GpgME::Context::CMS, reason ); + if ( reason ) + *reason = i18n( "Unsupported protocol \"%1\"" ).arg( name ); + return false; +} + +Kleo::CryptoBackend::Protocol * Kleo::QGpgMEBackend::openpgp() const { + if ( !mOpenPGPProtocol ) + if ( checkForOpenPGP() ) + mOpenPGPProtocol = new CryptPlugWrapper( "gpg", "openpgp" ); + return mOpenPGPProtocol; +} + +Kleo::CryptoBackend::Protocol * Kleo::QGpgMEBackend::smime() const { + if ( !mSMIMEProtocol ) + if ( checkForSMIME() ) + mSMIMEProtocol = new CryptPlugWrapper( "gpgsm", "smime" ); + return mSMIMEProtocol; +} + +Kleo::CryptoBackend::Protocol * Kleo::QGpgMEBackend::protocol( const char * name ) const { + if ( qstricmp( name, OpenPGP ) == 0 ) + return openpgp(); + if ( qstricmp( name, SMIME ) == 0 ) + return smime(); + return 0; +} + +bool Kleo::QGpgMEBackend::supportsProtocol( const char * name ) const { + return qstricmp( name, OpenPGP ) == 0 || qstricmp( name, SMIME ) == 0; +} + +const char * Kleo::QGpgMEBackend::enumerateProtocols( int i ) const { + switch ( i ) { + case 0: return OpenPGP; + case 1: return SMIME; + default: return 0; + } +} diff --git a/certmanager/lib/backends/qgpgme/qgpgmebackend.h b/certmanager/lib/backends/qgpgme/qgpgmebackend.h new file mode 100644 index 000000000..fae2dd68e --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmebackend.h @@ -0,0 +1,82 @@ +/* -*- mode: C++; c-file-style: "gnu" -*- + qgpgmebackend.h + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004,2005 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_QGPGMEBACKEND_H__ +#define __KLEO_QGPGMEBACKEND_H__ + +#include "kleo/cryptobackend.h" + +class CryptPlugWrapper; + +namespace Kleo { + class CryptoConfig; +} +class QGpgMECryptoConfig; +class QString; + +namespace Kleo { + + class QGpgMEBackend : public Kleo::CryptoBackend { + public: + QGpgMEBackend(); + ~QGpgMEBackend(); + + QString name() const; + QString displayName() const; + + CryptoConfig * config() const; + + Protocol * openpgp() const; + Protocol * smime() const; + Protocol * protocol( const char * name ) const; + + bool checkForOpenPGP( QString * reason=0 ) const; + bool checkForSMIME( QString * reason=0 ) const; + bool checkForProtocol( const char * name, QString * reason ) const; + + bool supportsOpenPGP() const { return true; } + bool supportsSMIME() const { return true; } + bool supportsProtocol( const char * name ) const; + + const char * enumerateProtocols( int i ) const; + + private: + mutable QGpgMECryptoConfig * mCryptoConfig; + mutable CryptPlugWrapper * mOpenPGPProtocol; + mutable CryptPlugWrapper * mSMIMEProtocol; + }; + +} + + +#endif // __KLEO_QGPGMEBACKEND_H__ diff --git a/certmanager/lib/backends/qgpgme/qgpgmecryptoconfig.cpp b/certmanager/lib/backends/qgpgme/qgpgmecryptoconfig.cpp new file mode 100644 index 000000000..86eab99b4 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmecryptoconfig.cpp @@ -0,0 +1,837 @@ +/* + qgpgmecryptoconfig.cpp + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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> +#include <qtextcodec.h> + +// 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; + +static const int GPGCONF_FLAG_GROUP = 1; +static const int GPGCONF_FLAG_OPTIONAL = 2; +static const int GPGCONF_FLAG_LIST = 4; +static const int GPGCONF_FLAG_RUNTIME = 8; +static const int GPGCONF_FLAG_DEFAULT = 16; // fixed default value available +static const int GPGCONF_FLAG_DEFAULT_DESC = 32; // runtime default value available +static const int GPGCONF_FLAG_NOARG_DESC = 64; // option with optional arg; special meaning if no arg set +static const int GPGCONF_FLAG_NO_CHANGE = 128; // readonly +// Change size of mFlags bitfield if adding new values here + +QGpgMECryptoConfig::QGpgMECryptoConfig() + : mComponents( 7 ), mParsed( false ) +{ + mComponents.setAutoDelete( true ); +} + +QGpgMECryptoConfig::~QGpgMECryptoConfig() +{ +} + +void QGpgMECryptoConfig::runGpgConf( bool showErrors ) +{ + // Run gpgconf --list-components to make the list of components + + KProcIO proc( QTextCodec::codecForName( "utf8" ) ); + 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() : -2 ; + + // handle errors, if any (and if requested) + if ( showErrors && rc != 0 ) { + QString wmsg = i18n("<qt>Failed to execute gpgconf:<br>%1</qt>"); + if ( rc == -1 ) + wmsg = wmsg.arg( i18n( "program not found" ) ); + else if ( rc == -2 ) + wmsg = wmsg.arg( i18n( "program cannot be executed" ) ); + else + wmsg = wmsg.arg( strerror(rc) ); + kdWarning(5150) << wmsg << endl; // to see it from test_cryptoconfig.cpp + 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( true ); + 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( QTextCodec::codecForName( "utf8" ) ); + proc << "gpgconf"; // must be in the PATH + proc << "--list-options"; + proc << mName; + + //kdDebug(5150) << "Running gpgconf --list-options " << mName << endl; + + 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 happen when using the wrong version of gpg... + kdWarning(5150) << "Running 'gpgconf --list-options " << mName << "' failed. " << strerror( rc ) << ", but try that command to see the real output" << endl; + else { + if ( mCurrentGroup && !mCurrentGroup->mEntries.isEmpty() ) // only add non-empty groups + mGroups.insert( mCurrentGroupName, mCurrentGroup ); + } +} + +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 + const QStringList lst = QStringList::split( ':', line, true ); + if ( lst.count() >= 10 ) { + const int flags = lst[1].toInt(); + const 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[0], lst[3], level ); + mCurrentGroupName = lst[0]; + } else { + // normal entry + if ( !mCurrentGroup ) { // first toplevel entry -> create toplevel group + mCurrentGroup = new QGpgMECryptoConfigGroup( "<nogroup>", 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 ); + + QValueList<QGpgMECryptoConfigEntry *> dirtyEntries; + + // Collect all dirty entries + QDictIterator<QGpgMECryptoConfigGroup> groupit( mGroups ); + for( ; groupit.current(); ++groupit ) { + QDictIterator<QGpgMECryptoConfigEntry> it( groupit.current()->mEntries ); + for( ; it.current(); ++it ) { + if ( it.current()->isDirty() ) { + // 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.utf8(); // encode with utf8, and KProcIO uses utf8 when reading. + tmpFile.file()->writeBlock( line8bit.data(), line8bit.size()-1 /*no 0*/ ); + dirtyEntries.append( it.current() ); + } + } + } + tmpFile.close(); + if ( dirtyEntries.isEmpty() ) + 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 ; + + if ( rc == -1 ) + { + QString wmsg = i18n( "Could not start gpgconf\nCheck that gpgconf is in the PATH and that it can be started" ); + kdWarning(5150) << wmsg << endl; + KMessageBox::error(0, wmsg); + } + else if( rc != 0 ) // Happens due to bugs in gpgconf (e.g. issues 104/115) + { + QString wmsg = i18n( "Error from gpgconf while saving configuration: %1" ).arg( QString::fromLocal8Bit( strerror( rc ) ) ); + kdWarning(5150) << k_funcinfo << ":" << strerror( rc ) << endl; + KMessageBox::error(0, wmsg); + } + else + { + QValueList<QGpgMECryptoConfigEntry *>::Iterator it = dirtyEntries.begin(); + for( ; it != dirtyEntries.end(); ++it ) { + (*it)->setDirty( false ); + } + } +} + +//// + +QGpgMECryptoConfigGroup::QGpgMECryptoConfigGroup( const QString & name, const QString& description, int level ) + : mEntries( 29 ), + mName( name ), + 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, 106 ); +} + +static QString gpgconf_escape( const QString& str ) +{ + // Escape special chars (including ':' and '%') + QString enc = KURL::encode_string( str, 106 ); // and convert to utf8 first (to get %12%34 for one special char) + // Also encode commas, for lists. + enc.replace( ',', "%2c" ); + return enc; +} + +static QString urlpart_encode( const QString& str ) +{ + QString enc( str ); + enc.replace( '%', "%25" ); // first! + enc.replace( ':', "%3a" ); + //kdDebug() << " urlpart_encode: " << str << " -> " << enc << endl; + return enc; +} + +static QString urlpart_decode( const QString& str ) +{ + return KURL::decode_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_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(); + mName = *it++; + 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 ) { + value = *it; // get default value + mDefaultValue = stringToValue( value, true ); + } + ++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, true ); + } + else { + mValue = mDefaultValue; + } + + mDirty = false; +} + +QVariant QGpgMECryptoConfigEntry::stringToValue( const QString& str, bool unescape ) const +{ + bool isString = isStringType(); + + if ( isList() ) { + QValueList<QVariant> lst; + QStringList items = QStringList::split( ',', str ); + for( QStringList::const_iterator valit = items.begin(); valit != items.end(); ++valit ) { + QString val = *valit; + if ( isString ) { + if ( val.isEmpty() ) { + lst << QString::null; + continue; + } + else if ( unescape ) { + if( val[0] != '"' ) // see README.gpgconf + kdWarning(5150) << "String value should start with '\"' : " << val << endl; + val = val.mid( 1 ); + } + } + lst << QVariant( unescape ? gpgconf_unescape( val ) : val ); + } + return lst; + } else { // not a list + QString val( str ); + if ( isString ) { + if ( val.isEmpty() ) + return QVariant( QString::null ); // not set [ok with lists too?] + else if ( unescape ) { + Q_ASSERT( val[0] == '"' ); // see README.gpgconf + val = val.mid( 1 ); + } + } + return QVariant( unescape ? gpgconf_unescape( val ) : val ); + } +} + +QGpgMECryptoConfigEntry::~QGpgMECryptoConfigEntry() +{ +#ifndef NDEBUG + if ( !s_duringClear && mDirty ) + kdWarning(5150) << "Deleting a QGpgMECryptoConfigEntry that was modified (" << mDescription << ")\n" + << "You forgot to call sync() (to commit) or clear() (to discard)" << endl; +#endif +} + +bool QGpgMECryptoConfigEntry::isOptional() const +{ + return mFlags & GPGCONF_FLAG_OPTIONAL; +} + +bool QGpgMECryptoConfigEntry::isReadOnly() const +{ + return mFlags & GPGCONF_FLAG_NO_CHANGE; +} + +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 +{ + return toString( false ); +} + +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( urlpart_decode( *it++ ) ); + url.setPort( (*it++).toInt() ); + url.setPath( "/" ); // workaround KURL parsing bug + url.setUser( urlpart_decode( *it++ ) ); + url.setPass( urlpart_decode( *it++ ) ); + url.setQuery( urlpart_decode( *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 + Q_ASSERT( url.protocol() == "ldap" ); + return urlpart_encode( url.host() ) + ":" + + QString::number( url.port() ) + ":" + + urlpart_encode( url.user() ) + ":" + + urlpart_encode( url.pass() ) + ":" + + // KURL automatically encoded the query (e.g. for spaces inside it), + // so decode it before writing it out to gpgconf (issue119) + urlpart_encode( KURL::decode_string( url.query().mid(1) ) ); + } + return url.path(); +} + +KURL QGpgMECryptoConfigEntry::urlValue() const +{ + Q_ASSERT( mArgType == ArgType_Path || mArgType == ArgType_URL || mArgType == ArgType_LDAPURL ); + Q_ASSERT( !isList() ); + QString str = mValue.toString(); + 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( 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 || 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_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() ); + // A "no arg" option is either set or not set. + // Being set means mSet==true + mValue==true, being unset means resetToDefault(), i.e. both false + mValue = b; + mSet = b; + mDirty = true; +} + +void QGpgMECryptoConfigEntry::setStringValue( const QString& str ) +{ + mValue = stringToValue( str, false ); + // When setting a string to empty (and there's no default), we need to act like resetToDefault + // Otherwise we try e.g. "ocsp-responder:0:" and gpgconf answers: + // "gpgconf: argument required for option ocsp-responder" + if ( str.isEmpty() && !isOptional() ) + mSet = false; + else + 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 ); + if ( str.isEmpty() && !isOptional() ) + mSet = false; + else + mSet = true; + mValue = str; + 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; + if ( lst.isEmpty() && !isOptional() ) + mSet = false; + else + 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; + if ( ret.isEmpty() && !isOptional() ) + mSet = false; + else + 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 ); + } + if ( ret.isEmpty() && !isOptional() ) + mSet = false; + else + mSet = true; + mValue = ret; + 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; + if ( lst.isEmpty() && !isOptional() ) + mSet = false; + else + mSet = true; + mDirty = true; +} + +QString QGpgMECryptoConfigEntry::toString( bool escape ) const +{ + // Basically the opposite of stringToValue + if ( isStringType() ) { + if ( mValue.isNull() ) + return QString::null; + else if ( isList() ) { // string list + QStringList lst = mValue.toStringList(); + if ( escape ) { + for( QStringList::iterator it = lst.begin(); it != lst.end(); ++it ) { + if ( !(*it).isNull() ) + *it = gpgconf_escape( *it ).prepend( "\"" ); + } + } + QString res = lst.join( "," ); + kdDebug(5150) << "toString: " << res << endl; + return res; + } else { // normal string + QString res = mValue.toString(); + if ( escape ) + res = gpgconf_escape( res ).prepend( "\"" ); + return res; + } + } + 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 (of other types than strings) + 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( "," ); +} + +QString QGpgMECryptoConfigEntry::outputString() const +{ + Q_ASSERT( mSet ); + return toString( true ); +} + +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 ); +} + +void QGpgMECryptoConfigEntry::setDirty( bool b ) +{ + mDirty = b; +} + +#include "qgpgmecryptoconfig.moc" diff --git a/certmanager/lib/backends/qgpgme/qgpgmecryptoconfig.h b/certmanager/lib/backends/qgpgme/qgpgmecryptoconfig.h new file mode 100644 index 000000000..bfcb8e5ca --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmecryptoconfig.h @@ -0,0 +1,186 @@ +/* + qgpgmecryptoconfig.h + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 + */ + QGpgMECryptoConfig(); + 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: + /// @param showErrors if true, a messagebox will be shown if e.g. gpgconf wasn't found + 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 ); + ~QGpgMECryptoConfigComponent(); + + QString name() const { return mName; } + QString iconName() const { return mName; } + QString description() const { return mDescription; } + QStringList groupList() const; + 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 & name, const QString& description, int level ); + ~QGpgMECryptoConfigGroup() {} + + QString name() const { return mName; } + QString iconName() const { return QString::null; } + QString description() const { return mDescription; } + Kleo::CryptoConfigEntry::Level level() const { return mLevel; } + QStringList entryList() const; + Kleo::CryptoConfigEntry* entry( const QString& name ) const; + +private: + friend class QGpgMECryptoConfigComponent; // it adds the entries + QDict<QGpgMECryptoConfigEntry> mEntries; + QString mName; + QString mDescription; + Kleo::CryptoConfigEntry::Level mLevel; +}; + +class QGpgMECryptoConfigEntry : public Kleo::CryptoConfigEntry { +public: + QGpgMECryptoConfigEntry( const QStringList& parsedLine ); + ~QGpgMECryptoConfigEntry(); + + QString name() const { return mName; } + QString description() const { return mDescription; } + bool isOptional() const; + bool isReadOnly() const; + bool isList() const; + bool isRuntime() const; + Level level() const { return static_cast<Level>( mLevel ); } + ArgType argType() const { return static_cast<ArgType>( mArgType ); } + bool isSet() const; + bool boolValue() const; + QString stringValue() const; + int intValue() const; + unsigned int uintValue() const; + KURL urlValue() const; + unsigned int numberOfTimesSet() const; + QStringList stringValueList() const; + QValueList<int> intValueList() const; + QValueList<unsigned int> uintValueList() const; + KURL::List urlValueList() const; + void resetToDefault(); + void setBoolValue( bool ); + void setStringValue( const QString& ); + void setIntValue( int ); + void setUIntValue( unsigned int ); + void setURLValue( const KURL& ); + void setNumberOfTimesSet( unsigned int ); + void setStringValueList( const QStringList& ); + void setIntValueList( const QValueList<int>& ); + void setUIntValueList( const QValueList<unsigned int>& ); + void setURLValueList( const KURL::List& ); + bool isDirty() const { return mDirty; } + + void setDirty( bool b ); + QString outputString() const; + +protected: + bool isStringType() const; + QVariant stringToValue( const QString& value, bool unescape ) const; + QString toString( bool escape ) const; +private: + QString mName; + QString mDescription; + QVariant mDefaultValue; + QVariant mValue; + uint mFlags : 8; // bitfield with 8 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 6 (ArgType enum) -> 3 bits; + uint mDirty : 1; + uint mSet : 1; +}; + +#endif /* KLEO_QGPGMECRYPTOCONFIG_H */ diff --git a/certmanager/lib/backends/qgpgme/qgpgmedecryptjob.cpp b/certmanager/lib/backends/qgpgme/qgpgmedecryptjob.cpp new file mode 100644 index 000000000..17905555e --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmedecryptjob.cpp @@ -0,0 +1,91 @@ +/* + qgpgmedecryptjob.cpp + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "qgpgmedecryptjob.h" + +#include <qgpgme/eventloopinteractor.h> +#include <qgpgme/dataprovider.h> + +#include <gpgmepp/context.h> +#include <gpgmepp/decryptionresult.h> +#include <gpgmepp/data.h> + +#include <assert.h> + +Kleo::QGpgMEDecryptJob::QGpgMEDecryptJob( GpgME::Context * context ) + : DecryptJob( QGpgME::EventLoopInteractor::instance(), "Kleo::QGpgMEDecryptJob" ), + QGpgMEJob( this, context ) +{ + assert( context ); +} + +Kleo::QGpgMEDecryptJob::~QGpgMEDecryptJob() { +} + +void Kleo::QGpgMEDecryptJob::setup( const QByteArray & cipherText ) { + assert( !mInData ); + assert( !mOutData ); + + createInData( cipherText ); + createOutData(); +} + +GpgME::Error Kleo::QGpgMEDecryptJob::start( const QByteArray & cipherText ) { + setup( cipherText ); + + hookupContextToEventLoopInteractor(); + + const GpgME::Error err = mCtx->startDecryption( *mInData, *mOutData ); + + if ( err ) + deleteLater(); + return err; +} + +GpgME::DecryptionResult Kleo::QGpgMEDecryptJob::exec( const QByteArray & cipherText, + QByteArray & plainText ) { + setup( cipherText ); + const GpgME::DecryptionResult result = mCtx->decrypt( *mInData, *mOutData ); + plainText = mOutDataDataProvider->data(); + getAuditLog(); + return result; +} + +void Kleo::QGpgMEDecryptJob::doOperationDoneEvent( const GpgME::Error & ) { + emit result( mCtx->decryptionResult(), mOutDataDataProvider->data() ); +} + +#include "qgpgmedecryptjob.moc" diff --git a/certmanager/lib/backends/qgpgme/qgpgmedecryptjob.h b/certmanager/lib/backends/qgpgme/qgpgmedecryptjob.h new file mode 100644 index 000000000..7fc89dff2 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmedecryptjob.h @@ -0,0 +1,74 @@ +/* + qgpgmedecryptjob.h + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_QGPGMEDECRYPTJOB_H__ +#define __KLEO_QGPGMEDECRYPTJOB_H__ + +#include <kleo/decryptjob.h> + +#include "qgpgmejob.h" + +#include <qcstring.h> + +namespace GpgME { + class Error; + class Context; +} + +namespace Kleo { + + class QGpgMEDecryptJob : public DecryptJob, private QGpgMEJob { + Q_OBJECT QGPGME_JOB + public: + QGpgMEDecryptJob( GpgME::Context * context ); + ~QGpgMEDecryptJob(); + + /*! \reimp from DecryptJob */ + GpgME::Error start( const QByteArray & cipherText ); + + /*! \reimp from DecryptJob */ + GpgME::DecryptionResult exec( const QByteArray & cipherText, + QByteArray & plainText ); + + private slots: + void slotOperationDoneEvent( GpgME::Context * context, const GpgME::Error & e ) { + QGpgMEJob::doSlotOperationDoneEvent( context, e ); + } + + private: + void doOperationDoneEvent( const GpgME::Error & e ); + void setup( const QByteArray & cipherText ); + }; + +} + +#endif // __KLEO_QGPGMEDECRYPTJOB_H__ diff --git a/certmanager/lib/backends/qgpgme/qgpgmedecryptverifyjob.cpp b/certmanager/lib/backends/qgpgme/qgpgmedecryptverifyjob.cpp new file mode 100644 index 000000000..02cf1ef28 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmedecryptverifyjob.cpp @@ -0,0 +1,95 @@ +/* + qgpgmedecryptverifyjob.cpp + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "qgpgmedecryptverifyjob.h" + +#include <qgpgme/eventloopinteractor.h> +#include <qgpgme/dataprovider.h> + +#include <gpgmepp/context.h> +#include <gpgmepp/decryptionresult.h> +#include <gpgmepp/verificationresult.h> +#include <gpgmepp/data.h> + +#include <assert.h> + +Kleo::QGpgMEDecryptVerifyJob::QGpgMEDecryptVerifyJob( GpgME::Context * context ) + : DecryptVerifyJob( QGpgME::EventLoopInteractor::instance(), "Kleo::QGpgMEDecryptVerifyJob" ), + QGpgMEJob( this, context ) +{ + assert( context ); +} + +Kleo::QGpgMEDecryptVerifyJob::~QGpgMEDecryptVerifyJob() { +} + +void Kleo::QGpgMEDecryptVerifyJob::setup( const QByteArray & cipherText ) { + assert( !mInData ); + assert( !mOutData ); + + createInData( cipherText ); + createOutData(); +} + +GpgME::Error Kleo::QGpgMEDecryptVerifyJob::start( const QByteArray & cipherText ) { + setup( cipherText ); + + hookupContextToEventLoopInteractor(); + + const GpgME::Error err = mCtx->startCombinedDecryptionAndVerification( *mInData, *mOutData ); + + if ( err ) + deleteLater(); + return err; +} + +std::pair<GpgME::DecryptionResult,GpgME::VerificationResult> +Kleo::QGpgMEDecryptVerifyJob::exec( const QByteArray & cipherText, QByteArray & plainText ) { + setup( cipherText ); + const std::pair<GpgME::DecryptionResult,GpgME::VerificationResult> result = + mCtx->decryptAndVerify( *mInData, *mOutData ); + plainText = mOutDataDataProvider->data(); + getAuditLog(); + return result; +} + +void Kleo::QGpgMEDecryptVerifyJob::doOperationDoneEvent( const GpgME::Error & ) { + emit result( mCtx->decryptionResult(), + mCtx->verificationResult(), + mOutDataDataProvider->data() ); +} + +#include "qgpgmedecryptverifyjob.moc" diff --git a/certmanager/lib/backends/qgpgme/qgpgmedecryptverifyjob.h b/certmanager/lib/backends/qgpgme/qgpgmedecryptverifyjob.h new file mode 100644 index 000000000..a39188746 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmedecryptverifyjob.h @@ -0,0 +1,75 @@ +/* + qgpgmedecryptverifyjob.h + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_QGPGMEDECRYPTVERIFYJOB_H__ +#define __KLEO_QGPGMEDECRYPTVERIFYJOB_H__ + +#include <kleo/decryptverifyjob.h> + +#include "qgpgmejob.h" + +#include <qcstring.h> +#include <kdepimmacros.h> + +namespace GpgME { + class Error; + class Context; +} + +namespace Kleo { + + class KDE_EXPORT QGpgMEDecryptVerifyJob : public DecryptVerifyJob, private QGpgMEJob { + Q_OBJECT QGPGME_JOB + public: + QGpgMEDecryptVerifyJob( GpgME::Context * context ); + ~QGpgMEDecryptVerifyJob(); + + /*! \reimp from DecryptVerifyJob */ + GpgME::Error start( const QByteArray & cipherText ); + + /*! \reimp from DecryptVerifyJob */ + std::pair<GpgME::DecryptionResult,GpgME::VerificationResult> + exec( const QByteArray & cipherText, QByteArray & plainText ); + + private slots: + void slotOperationDoneEvent( GpgME::Context * context, const GpgME::Error & e ) { + QGpgMEJob::doSlotOperationDoneEvent( context, e ); + } + + private: + void doOperationDoneEvent( const GpgME::Error & e ); + void setup( const QByteArray & ); + }; + +} + +#endif // __KLEO_QGPGMEDECRYPTVERIFYJOB_H__ diff --git a/certmanager/lib/backends/qgpgme/qgpgmedeletejob.cpp b/certmanager/lib/backends/qgpgme/qgpgmedeletejob.cpp new file mode 100644 index 000000000..c06907eea --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmedeletejob.cpp @@ -0,0 +1,70 @@ +/* + qgpgmedeletejob.cpp + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "qgpgmedeletejob.h" + +#include <qgpgme/eventloopinteractor.h> + +#include <gpgmepp/context.h> + +#include <assert.h> + +Kleo::QGpgMEDeleteJob::QGpgMEDeleteJob( GpgME::Context * context ) + : DeleteJob( QGpgME::EventLoopInteractor::instance(), "Kleo::QGpgMEDeleteJob" ), + QGpgMEJob( this, context ) +{ + assert( context ); +} + +Kleo::QGpgMEDeleteJob::~QGpgMEDeleteJob() { +} + +GpgME::Error Kleo::QGpgMEDeleteJob::start( const GpgME::Key & key, bool allowSecretKeyDeletion ) { + + hookupContextToEventLoopInteractor(); + + const GpgME::Error err = mCtx->startKeyDeletion( key, allowSecretKeyDeletion ); + + if ( err ) + deleteLater(); + return err; +} + +void Kleo::QGpgMEDeleteJob::doOperationDoneEvent( const GpgME::Error & error ) { + emit result( error ); +} + +#include "qgpgmedeletejob.moc" diff --git a/certmanager/lib/backends/qgpgme/qgpgmedeletejob.h b/certmanager/lib/backends/qgpgme/qgpgmedeletejob.h new file mode 100644 index 000000000..9dec128f7 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmedeletejob.h @@ -0,0 +1,68 @@ +/* + qgpgmedeletejob.h + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_QGPGMEDELETEJOB_H__ +#define __KLEO_QGPGMEDELETEJOB_H__ + +#include <kleo/deletejob.h> + +#include "qgpgmejob.h" + +namespace GpgME { + class Error; + class Context; + class Key; +} + +namespace Kleo { + + class QGpgMEDeleteJob : public DeleteJob, private QGpgMEJob { + Q_OBJECT QGPGME_JOB + public: + QGpgMEDeleteJob( GpgME::Context * context ); + ~QGpgMEDeleteJob(); + + /*! \reimp from DeleteJob */ + GpgME::Error start( const GpgME::Key & key, bool allowSecretKeyDeletion ); + + private slots: + void slotOperationDoneEvent( GpgME::Context * context, const GpgME::Error & e ) { + QGpgMEJob::doSlotOperationDoneEvent( context, e ); + } + + private: + void doOperationDoneEvent( const GpgME::Error & e ); + }; + +} + +#endif // __KLEO_QGPGMEDELETEJOB_H__ diff --git a/certmanager/lib/backends/qgpgme/qgpgmedownloadjob.cpp b/certmanager/lib/backends/qgpgme/qgpgmedownloadjob.cpp new file mode 100644 index 000000000..6ee771a04 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmedownloadjob.cpp @@ -0,0 +1,78 @@ +/* + qgpgmedownloadjob.cpp + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "qgpgmedownloadjob.h" + +#include <qgpgme/eventloopinteractor.h> +#include <qgpgme/dataprovider.h> + +#include <gpgmepp/context.h> +#include <gpgmepp/data.h> + +#include <qstringlist.h> + +#include <assert.h> + +Kleo::QGpgMEDownloadJob::QGpgMEDownloadJob( GpgME::Context * context ) + : DownloadJob( QGpgME::EventLoopInteractor::instance(), "Kleo::QGpgMEDownloadJob" ), + QGpgMEJob( this, context ) +{ + assert( context ); +} + +Kleo::QGpgMEDownloadJob::~QGpgMEDownloadJob() { +} + +GpgME::Error Kleo::QGpgMEDownloadJob::start( const QStringList & pats ) { + assert( !patterns() ); + assert( !mOutData ); + + createOutData(); + setPatterns( pats ); + hookupContextToEventLoopInteractor(); + + const GpgME::Error err = mCtx->startPublicKeyExport( patterns(), *mOutData ); + + if ( err ) + deleteLater(); + return err; +} + +void Kleo::QGpgMEDownloadJob::doOperationDoneEvent( const GpgME::Error & error ) { + emit result( error, mOutDataDataProvider->data() ); +} + +#include "qgpgmedownloadjob.moc" diff --git a/certmanager/lib/backends/qgpgme/qgpgmedownloadjob.h b/certmanager/lib/backends/qgpgme/qgpgmedownloadjob.h new file mode 100644 index 000000000..1402b127c --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmedownloadjob.h @@ -0,0 +1,67 @@ +/* + qgpgmedownloadjob.h + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_QGPGMEDOWNLOADJOB_H__ +#define __KLEO_QGPGMEDOWNLOADJOB_H__ + +#include <kleo/downloadjob.h> + +#include "qgpgmejob.h" + +namespace GpgME { + class Error; + class Context; +} + +namespace Kleo { + + class QGpgMEDownloadJob : public DownloadJob, private QGpgMEJob { + Q_OBJECT QGPGME_JOB + public: + QGpgMEDownloadJob( GpgME::Context * context ); + ~QGpgMEDownloadJob(); + + /*! \reimp from DownloadJob */ + GpgME::Error start( const QStringList & fingerprints ); + + private: + void doOperationDoneEvent( const GpgME::Error & e ); + + private slots: + void slotOperationDoneEvent( GpgME::Context * context, const GpgME::Error & e ) { + QGpgMEJob::doSlotOperationDoneEvent( context, e ); + } + }; + +} + +#endif // __KLEO_QGPGMEDOWNLOADJOB_H__ diff --git a/certmanager/lib/backends/qgpgme/qgpgmeencryptjob.cpp b/certmanager/lib/backends/qgpgme/qgpgmeencryptjob.cpp new file mode 100644 index 000000000..3f223fc56 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmeencryptjob.cpp @@ -0,0 +1,108 @@ +/* + qgpgmeencryptjob.cpp + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "qgpgmeencryptjob.h" + +#include "ui/messagebox.h" + +#include <qgpgme/eventloopinteractor.h> +#include <qgpgme/dataprovider.h> + +#include <gpgmepp/context.h> +#include <gpgmepp/encryptionresult.h> +#include <gpgmepp/data.h> + +#include <klocale.h> + +#include <assert.h> + +Kleo::QGpgMEEncryptJob::QGpgMEEncryptJob( GpgME::Context * context ) + : EncryptJob( QGpgME::EventLoopInteractor::instance(), "Kleo::QGpgMEEncryptJob" ), + QGpgMEJob( this, context ) +{ + assert( context ); +} + +Kleo::QGpgMEEncryptJob::~QGpgMEEncryptJob() { +} + +void Kleo::QGpgMEEncryptJob::setup( const QByteArray & plainText ) { + assert( !mInData ); + assert( !mOutData ); + + createInData( plainText ); + createOutData(); +} + +GpgME::Error Kleo::QGpgMEEncryptJob::start( const std::vector<GpgME::Key> & recipients, + const QByteArray & plainText, bool alwaysTrust ) { + setup( plainText ); + + hookupContextToEventLoopInteractor(); + + const GpgME::Context::EncryptionFlags flags = + alwaysTrust ? GpgME::Context::AlwaysTrust : GpgME::Context::None; + const GpgME::Error err = mCtx->startEncryption( recipients, *mInData, *mOutData, flags ); + + if ( err ) + deleteLater(); + mResult = GpgME::EncryptionResult( err ); + return err; +} + +GpgME::EncryptionResult Kleo::QGpgMEEncryptJob::exec( const std::vector<GpgME::Key> & recipients, + const QByteArray & plainText, + bool alwaysTrust, + QByteArray & ciphertext ) { + setup( plainText ); + const GpgME::Context::EncryptionFlags flags = + alwaysTrust ? GpgME::Context::AlwaysTrust : GpgME::Context::None; + mResult = mCtx->encrypt( recipients, *mInData, *mOutData, flags ); + ciphertext = mOutDataDataProvider->data(); + getAuditLog(); + return mResult; +} + +void Kleo::QGpgMEEncryptJob::doOperationDoneEvent( const GpgME::Error & ) { + emit result( mResult = mCtx->encryptionResult(), mOutDataDataProvider->data() ); +} + +void Kleo::QGpgMEEncryptJob::showErrorDialog( QWidget * parent, const QString & caption ) const { + if ( mResult.error() && !mResult.error().isCanceled() ) + Kleo::MessageBox::error( parent, mResult, this, caption ); +} + +#include "qgpgmeencryptjob.moc" diff --git a/certmanager/lib/backends/qgpgme/qgpgmeencryptjob.h b/certmanager/lib/backends/qgpgme/qgpgmeencryptjob.h new file mode 100644 index 000000000..362fb4e06 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmeencryptjob.h @@ -0,0 +1,86 @@ +/* + qgpgmeencryptjob.h + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_QGPGMEENCRYPTJOB_H__ +#define __KLEO_QGPGMEENCRYPTJOB_H__ + +#include <kleo/encryptjob.h> + +#include "qgpgmejob.h" + +#include <gpgmepp/encryptionresult.h> + +#include <qcstring.h> + +namespace GpgME { + class Error; + class Context; + class Key; +} + +namespace Kleo { + + class QGpgMEEncryptJob : public EncryptJob, private QGpgMEJob { + Q_OBJECT QGPGME_JOB + public: + QGpgMEEncryptJob( GpgME::Context * context ); + ~QGpgMEEncryptJob(); + + /*! \reimp from EncryptJob */ + GpgME::Error start( const std::vector<GpgME::Key> & recipients, + const QByteArray & plainText, bool alwaysTrust ); + + /*! \reimp from EncryptJob */ + GpgME::EncryptionResult exec( const std::vector<GpgME::Key> & recipients, + const QByteArray & plainText, bool alwaysTrust, + QByteArray & cipherText ); + + /*! \reimp from Job */ + void showErrorDialog( QWidget * parent, const QString & caption ) const; + + private slots: + void slotOperationDoneEvent( GpgME::Context * context, const GpgME::Error & e ) { + QGpgMEJob::doSlotOperationDoneEvent( context, e ); + } + + + private: + void doOperationDoneEvent( const GpgME::Error & e ); + void setup( const QByteArray & ); + + private: + GpgME::EncryptionResult mResult; + }; + +} + +#endif // __KLEO_QGPGMEENCRYPTJOB_H__ diff --git a/certmanager/lib/backends/qgpgme/qgpgmeexportjob.cpp b/certmanager/lib/backends/qgpgme/qgpgmeexportjob.cpp new file mode 100644 index 000000000..c9274f3b7 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmeexportjob.cpp @@ -0,0 +1,78 @@ +/* + qgpgmeexportjob.cpp + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "qgpgmeexportjob.h" + +#include <qgpgme/eventloopinteractor.h> +#include <qgpgme/dataprovider.h> + +#include <gpgmepp/context.h> +#include <gpgmepp/data.h> + +#include <qstringlist.h> + +#include <assert.h> + +Kleo::QGpgMEExportJob::QGpgMEExportJob( GpgME::Context * context ) + : ExportJob( QGpgME::EventLoopInteractor::instance(), "Kleo::QGpgMEExportJob" ), + QGpgMEJob( this, context ) +{ + assert( context ); +} + +Kleo::QGpgMEExportJob::~QGpgMEExportJob() { +} + +GpgME::Error Kleo::QGpgMEExportJob::start( const QStringList & pats ) { + assert( !patterns() ); + assert( !mOutData ); + + createOutData(); + setPatterns( pats ); + hookupContextToEventLoopInteractor(); + + const GpgME::Error err = mCtx->startPublicKeyExport( patterns(), *mOutData ); + + if ( err ) + deleteLater(); + return err; +} + +void Kleo::QGpgMEExportJob::doOperationDoneEvent( const GpgME::Error & error ) { + emit result( error, mOutDataDataProvider->data() ); +} + +#include "qgpgmeexportjob.moc" diff --git a/certmanager/lib/backends/qgpgme/qgpgmeexportjob.h b/certmanager/lib/backends/qgpgme/qgpgmeexportjob.h new file mode 100644 index 000000000..8b799b583 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmeexportjob.h @@ -0,0 +1,69 @@ +/* + qgpgmeexportjob.h + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_QGPGMEEXPORTJOB_H__ +#define __KLEO_QGPGMEEXPORTJOB_H__ + +#include <kleo/exportjob.h> + +#include "qgpgmejob.h" + +#include <qcstring.h> + +namespace GpgME { + class Error; + class Context; +} + +namespace Kleo { + + class QGpgMEExportJob : public ExportJob, private QGpgMEJob { + Q_OBJECT QGPGME_JOB + public: + QGpgMEExportJob( GpgME::Context * context ); + ~QGpgMEExportJob(); + + /*! \reimp from ExportJob */ + GpgME::Error start( const QStringList & patterns ); + + private slots: + void slotOperationDoneEvent( GpgME::Context * context, const GpgME::Error & e ) { + QGpgMEJob::doSlotOperationDoneEvent( context, e ); + } + + private: + void doOperationDoneEvent( const GpgME::Error & e ); + }; + +} + +#endif // __KLEO_QGPGMEEXPORTJOB_H__ diff --git a/certmanager/lib/backends/qgpgme/qgpgmeimportjob.cpp b/certmanager/lib/backends/qgpgme/qgpgmeimportjob.cpp new file mode 100644 index 000000000..fd1251b65 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmeimportjob.cpp @@ -0,0 +1,86 @@ +/* + qgpgmeimportjob.cpp + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "qgpgmeimportjob.h" + +#include <qgpgme/eventloopinteractor.h> +#include <qgpgme/dataprovider.h> + +#include <gpgmepp/context.h> +#include <gpgmepp/importresult.h> +#include <gpgmepp/data.h> + +#include <assert.h> + +Kleo::QGpgMEImportJob::QGpgMEImportJob( GpgME::Context * context ) + : ImportJob( QGpgME::EventLoopInteractor::instance(), "Kleo::QGpgMEImportJob" ), + QGpgMEJob( this, context ) +{ + assert( context ); +} + +Kleo::QGpgMEImportJob::~QGpgMEImportJob() { +} + +void Kleo::QGpgMEImportJob::setup( const QByteArray & keyData ) { + assert( !mInData ); + + createInData( keyData ); +} + +GpgME::Error Kleo::QGpgMEImportJob::start( const QByteArray & keyData ) { + setup( keyData ); + + hookupContextToEventLoopInteractor(); + + const GpgME::Error err = mCtx->startKeyImport( *mInData ); + + if ( err ) + deleteLater(); + return err; +} + +GpgME::ImportResult Kleo::QGpgMEImportJob::exec( const QByteArray & keyData ) { + setup( keyData ); + return mCtx->importKeys( *mInData ); +} + +void Kleo::QGpgMEImportJob::doOperationDoneEvent( const GpgME::Error & ) { + emit result( mCtx->importResult() ); +} + + +#include "qgpgmeimportjob.moc" diff --git a/certmanager/lib/backends/qgpgme/qgpgmeimportjob.h b/certmanager/lib/backends/qgpgme/qgpgmeimportjob.h new file mode 100644 index 000000000..8c2ee1ad4 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmeimportjob.h @@ -0,0 +1,73 @@ +/* + qgpgmeimportjob.h + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_QGPGMEIMPORTJOB_H__ +#define __KLEO_QGPGMEIMPORTJOB_H__ + +#include <kleo/importjob.h> + +#include "qgpgmejob.h" + +#include <qcstring.h> + +namespace GpgME { + class Error; + class Context; +} + +namespace Kleo { + + class QGpgMEImportJob : public ImportJob, private QGpgMEJob { + Q_OBJECT QGPGME_JOB + public: + QGpgMEImportJob( GpgME::Context * context ); + ~QGpgMEImportJob(); + + /*! \reimp from ImportJob */ + GpgME::Error start( const QByteArray & keyData ); + + /*! \reimp from ImportJob */ + GpgME::ImportResult exec( const QByteArray & keyData ); + + private slots: + void slotOperationDoneEvent( GpgME::Context * context, const GpgME::Error & e ) { + QGpgMEJob::doSlotOperationDoneEvent( context, e ); + } + + private: + void doOperationDoneEvent( const GpgME::Error & e ); + void setup( const QByteArray & ); + }; + +} + +#endif // __KLEO_QGPGMEIMPORTJOB_H__ diff --git a/certmanager/lib/backends/qgpgme/qgpgmejob.cpp b/certmanager/lib/backends/qgpgme/qgpgmejob.cpp new file mode 100644 index 000000000..897e5ff7c --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmejob.cpp @@ -0,0 +1,304 @@ +/* + qgpgmejob.cpp + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "qgpgmejob.h" +#include "qgpgmeprogresstokenmapper.h" + +#include <kleo/job.h> +#include <ui/passphrasedialog.h> + +#include <qgpgme/eventloopinteractor.h> +#include <qgpgme/dataprovider.h> + +#include <gpgmepp/context.h> +#include <gpgmepp/data.h> + +#include <klocale.h> +#include <kstandarddirs.h> + +#include <qstring.h> +#include <qstringlist.h> + +#include <algorithm> + +#include <assert.h> +#include <string.h> + +namespace { + class InvarianceChecker { + public: +#ifdef NDEBUG + InvarianceChecker( const Kleo::QGpgMEJob * ) {} +#else + InvarianceChecker( const Kleo::QGpgMEJob * job ) + : _this( job ) + { + assert( _this ); + _this->checkInvariants(); + } + ~InvarianceChecker() { + _this->checkInvariants(); + } + private: + const Kleo::QGpgMEJob * _this; +#endif + }; +} + +Kleo::QGpgMEJob::QGpgMEJob( Kleo::Job * _this, GpgME::Context * context ) + : GpgME::ProgressProvider(), + GpgME::PassphraseProvider(), + mThis( _this ), + mCtx( context ), + mInData( 0 ), + mInDataDataProvider( 0 ), + mOutData( 0 ), + mOutDataDataProvider( 0 ), + mPatterns( 0 ), + mReplacedPattern( 0 ), + mNumPatterns( 0 ), + mChunkSize( 1024 ), + mPatternStartIndex( 0 ), mPatternEndIndex( 0 ) +{ + InvarianceChecker check( this ); + assert( context ); + QObject::connect( QGpgME::EventLoopInteractor::instance(), SIGNAL(aboutToDestroy()), + _this, SLOT(slotCancel()) ); + context->setProgressProvider( this ); + // (mmutz) work around a gpgme bug in versions at least <= 0.9.0. + // These versions will return GPG_ERR_NOT_IMPLEMENTED from + // a CMS sign operation when a passphrase callback is set. + if ( context->protocol() == GpgME::Context::OpenPGP ) + context->setPassphraseProvider( this ); +} + +void Kleo::QGpgMEJob::checkInvariants() const { +#ifndef NDEBUG + if ( mPatterns ) { + assert( mPatterns[mNumPatterns] == 0 ); + if ( mPatternEndIndex > 0 ) { + assert( mPatternEndIndex > mPatternStartIndex ); + assert( mPatternEndIndex - mPatternStartIndex == mChunkSize ); + } else { + assert( mPatternEndIndex == mPatternStartIndex ); + } + if ( mPatternEndIndex < mNumPatterns ) { + assert( mPatterns[mPatternEndIndex] == 0 ); + assert( mReplacedPattern != 0 ); + } else { + assert( mReplacedPattern == 0 ); + } + } else { + assert( mNumPatterns == 0 ); + assert( mPatternStartIndex == 0 ); + assert( mPatternEndIndex == 0 ); + assert( mReplacedPattern == 0 ); + } +#endif +} + +Kleo::QGpgMEJob::~QGpgMEJob() { + InvarianceChecker check( this ); + delete mCtx; mCtx = 0; + delete mInData; mInData = 0; + delete mInDataDataProvider; mInDataDataProvider = 0; + delete mOutData; mOutData = 0; + delete mOutDataDataProvider; mOutDataDataProvider = 0; + deleteAllPatterns(); +} + +void Kleo::QGpgMEJob::deleteAllPatterns() { + if ( mPatterns ) + for ( unsigned int i = 0 ; i < mNumPatterns ; ++i ) + free( (void*)mPatterns[i] ); + free( (void*)mReplacedPattern ); mReplacedPattern = 0; + delete[] mPatterns; mPatterns = 0; + mPatternEndIndex = mPatternStartIndex = mNumPatterns = 0; +} + +void Kleo::QGpgMEJob::hookupContextToEventLoopInteractor() { + mCtx->setManagedByEventLoopInteractor( true ); + QObject::connect( QGpgME::EventLoopInteractor::instance(), + SIGNAL(operationDoneEventSignal(GpgME::Context*,const GpgME::Error&)), + mThis, SLOT(slotOperationDoneEvent(GpgME::Context*,const GpgME::Error&)) ); +} + +void Kleo::QGpgMEJob::setPatterns( const QStringList & sl, bool allowEmpty ) { + InvarianceChecker check( this ); + deleteAllPatterns(); + // create a new null-terminated C array of char* from patterns: + mPatterns = new const char*[ sl.size() + 1 ]; + const char* * pat_it = mPatterns; + mNumPatterns = 0; + for ( QStringList::const_iterator it = sl.begin() ; it != sl.end() ; ++it ) { + if ( (*it).isNull() ) + continue; + if ( (*it).isEmpty() && !allowEmpty ) + continue; + *pat_it++ = strdup( (*it).utf8().data() ); + ++mNumPatterns; + } + *pat_it++ = 0; + mReplacedPattern = 0; + mPatternEndIndex = mChunkSize = mNumPatterns; +} + +void Kleo::QGpgMEJob::setChunkSize( unsigned int chunksize ) { + InvarianceChecker check( this ); + if ( mReplacedPattern ) { + mPatterns[mPatternEndIndex] = mReplacedPattern; + mReplacedPattern = 0; + } + mChunkSize = std::min( chunksize, mNumPatterns ); + mPatternStartIndex = 0; + mPatternEndIndex = mChunkSize; + mReplacedPattern = mPatterns[mPatternEndIndex]; + mPatterns[mPatternEndIndex] = 0; +} + +const char* * Kleo::QGpgMEJob::nextChunk() { + InvarianceChecker check( this ); + if ( mReplacedPattern ) { + mPatterns[mPatternEndIndex] = mReplacedPattern; + mReplacedPattern = 0; + } + mPatternStartIndex += mChunkSize; + mPatternEndIndex += mChunkSize; + if ( mPatternEndIndex < mNumPatterns ) { // could safely be <=, but the last entry is NULL anyway + mReplacedPattern = mPatterns[mPatternEndIndex]; + mPatterns[mPatternEndIndex] = 0; + } + return patterns(); +} + +const char* * Kleo::QGpgMEJob::patterns() const { + InvarianceChecker check( this ); + if ( mPatternStartIndex < mNumPatterns ) + return mPatterns + mPatternStartIndex; + return 0; +} + +GpgME::Error Kleo::QGpgMEJob::setSigningKeys( const std::vector<GpgME::Key> & signers ) { + mCtx->clearSigningKeys(); + for ( std::vector<GpgME::Key>::const_iterator it = signers.begin() ; it != signers.end() ; ++it ) { + if ( (*it).isNull() ) + continue; + if ( const GpgME::Error err = mCtx->addSigningKey( *it ) ) + return err; + } + return 0; +} + +void Kleo::QGpgMEJob::createInData( const QByteArray & in ) { + mInDataDataProvider = new QGpgME::QByteArrayDataProvider( in ); + mInData = new GpgME::Data( mInDataDataProvider ); + assert( !mInData->isNull() ); +} + +void Kleo::QGpgMEJob::createOutData() { + mOutDataDataProvider = new QGpgME::QByteArrayDataProvider(); + mOutData = new GpgME::Data( mOutDataDataProvider ); + assert( !mOutData->isNull() ); +} + +static const unsigned int GetAuditLogFlags = GpgME::Context::AuditLogWithHelp|GpgME::Context::HtmlAuditLog; + +static QString audit_log_as_html( GpgME::Context * ctx ) { + if ( !ctx ) + return QString(); + QGpgME::QByteArrayDataProvider dp; + GpgME::Data data( &dp ); + assert( !data.isNull() ); + if ( const GpgME::Error err = ctx->getAuditLog( data, GetAuditLogFlags ) ) + return QString(); + else + return QString::fromUtf8( dp.data().data() ); +} + +void Kleo::QGpgMEJob::doSlotOperationDoneEvent( GpgME::Context * context, const GpgME::Error & e ) { + if ( context == mCtx ) { + getAuditLog(); + doEmitDoneSignal(); + doOperationDoneEvent( e ); + mThis->deleteLater(); + } +} + +void Kleo::QGpgMEJob::getAuditLog() { + mAuditLogAsHtml = audit_log_as_html( mCtx ); +} + +void Kleo::QGpgMEJob::doSlotCancel() { + mCtx->cancelPendingOperation(); +} + +void Kleo::QGpgMEJob::showProgress( const char * what, int type, int current, int total ) { + doEmitProgressSignal( QGpgMEProgressTokenMapper::instance()->map( what, type, current, total ), current, total ); +} + +char * Kleo::QGpgMEJob::getPassphrase( const char * useridHint, const char * /*description*/, + bool previousWasBad, bool & canceled ) { + // DF: here, description is the key fingerprint, twice, then "17 0". Not really descriptive. + // So I'm ignoring QString::fromLocal8Bit( description ) ) + QString msg = previousWasBad ? + i18n( "You need a passphrase to unlock the secret key for user:<br/> %1 (retry)" ) : + i18n( "You need a passphrase to unlock the secret key for user:<br/> %1" ); + msg = msg.arg( QString::fromUtf8( useridHint ) ) + "<br/><br/>"; + msg.prepend( "<qt>" ); + msg += i18n( "This dialog will reappear every time the passphrase is needed. For a more secure solution that also allows caching the passphrase, use gpg-agent." ) + "<br/>"; + const QString gpgAgent = KStandardDirs::findExe( "gpg-agent" ); + if ( !gpgAgent.isEmpty() ) { + msg += i18n( "gpg-agent was found in %1, but does not appear to be running." ) + .arg( gpgAgent ); + } else { + msg += i18n( "gpg-agent is part of gnupg-%1, which you can download from %2" ) + .arg( "1.9" ) + .arg( "http://www.gnupg.org/download" ); // add #gnupg2 if you can make this a real link + } + msg += "<br/>"; + msg += i18n( "For information on how to set up gpg-agent, see %1" ) + .arg( "http://kmail.kde.org/kmail-pgpmime-howto.html" ); + msg += "<br/><br/>"; + msg += i18n( "Enter passphrase:" ); + Kleo::PassphraseDialog dlg( msg, i18n("Passphrase Dialog") ); + if ( dlg.exec() != QDialog::Accepted ) { + canceled = true; + return 0; + } + canceled = false; + // gpgme++ free()s it, and we need to copy as long as dlg isn't deleted :o + return strdup( dlg.passphrase() ); +} diff --git a/certmanager/lib/backends/qgpgme/qgpgmejob.h b/certmanager/lib/backends/qgpgme/qgpgmejob.h new file mode 100644 index 000000000..da7acf1c9 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmejob.h @@ -0,0 +1,160 @@ +/* + qgpgmejob.h + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_QGPGMEJOB_H__ +#define __KLEO_QGPGMEJOB_H__ + +#include <gpgmepp/interfaces/progressprovider.h> +#include <gpgmepp/interfaces/passphraseprovider.h> + +#include <gpgmepp/key.h> + +#include <qcstring.h> +#include <qstring.h> + +#include <vector> +#include <kdepimmacros.h> + +namespace GpgME { + class Error; + class Context; + class Data; +} + +namespace Kleo { + class Job; +} + +namespace QGpgME { + class QByteArrayDataProvider; +} + +class QString; +class QStringList; + +namespace Kleo { + + /** This is a hackish helper class to avoid code duplication in this + backend's Kleo::Job subclasses. It contains several workarounds + for moc/signal/slot shortcomings, most of which the author of + this thinks are Qt bugs (lazy implementations), first and + foremost the inability of moc to handle inheritance from + multiple QObject-derived subclasses. + + To use it, inherit from the Job-subclass, then from this class, + add QGPGME_JOB to just after Q OBJECT and implement + doOperationDoneEvent() by emitting your variant of the result() + signal there. Pass "this" as the first argument this QGpgMEJOb's + ctor. The rest is dealt with automatically. + */ + class KDE_EXPORT QGpgMEJob : public GpgME::ProgressProvider, public GpgME::PassphraseProvider { + public: + QGpgMEJob( Kleo::Job * _this, GpgME::Context * context ); + ~QGpgMEJob(); + + protected: + /*! Called on operation-done events, between emitting done() and + calling deleteLater(). You should emit your result signal here. */ + virtual void doOperationDoneEvent( const GpgME::Error & e ) = 0; + /*! Hooks up mCtx to be managed by the event loop interactor */ + void hookupContextToEventLoopInteractor(); + /*! Fills mPatterns from the stringlist, resets chunking to the full list */ + void setPatterns( const QStringList & sl, bool allowEmpty=false ); + /*! Returnes the number of patterns set */ + unsigned int numPatterns() const { return mNumPatterns; } + /*! Skips to the next chunk of patterns. @return patterns() */ + const char* * nextChunk(); + /*! @return patterns, offset by the current chunk */ + const char* * patterns() const; + /*! Set the current pattern chunksize to size and reset the chunk index to zero */ + void setChunkSize( unsigned int size ); + /*! @return current chunksize */ + unsigned int chunkSize() const { return mChunkSize; } + /*! Creates an empty GpgME::Data/QGpgME::QByteArrayDataProvider pair */ + void createOutData(); + /*! Creates a GpgME::Data/QGpgME::QByteArrayDataProvider pair, + filled with the contents of \a in */ + void createInData( const QByteArray & in ); + /*! Sets the list of signing keys */ + GpgME::Error setSigningKeys( const std::vector<GpgME::Key> & signers ); + /*! Call this to implement a slotOperationDoneEvent() */ + void doSlotOperationDoneEvent( GpgME::Context * context, const GpgME::Error & e ); + /*! Call this to extract the audit log from mCtx */ + void getAuditLog(); + + // + // only boring stuff below this line... + // + + protected: + virtual void doEmitProgressSignal( const QString & what, int current, int total ) = 0; + virtual void doEmitDoneSignal() = 0; + void doSlotCancel(); + QString auditLogAsHtml() const { return mAuditLogAsHtml; } + + private: + /*! \reimp from GpgME::ProgressProvider */ + void showProgress( const char * what, int type, int current, int total ); + char * getPassphrase( const char * useridHint, const char * description, + bool previousWasBad, bool & canceled ); + void deleteAllPatterns(); + + public: + void checkInvariants() const; + + protected: + Kleo::Job * mThis; + GpgME::Context * mCtx; + GpgME::Data * mInData; + QGpgME::QByteArrayDataProvider * mInDataDataProvider; + GpgME::Data * mOutData; + QGpgME::QByteArrayDataProvider * mOutDataDataProvider; + private: + const char* * mPatterns; + // holds the entry - if any - in mPattern that was replaced with + // NULL to create a temporary end-of-array marker for gpgme: + const char * mReplacedPattern; + unsigned int mNumPatterns; + unsigned int mChunkSize; + unsigned int mPatternStartIndex, mPatternEndIndex; + QString mAuditLogAsHtml; + }; + +} + +#define make_slot_cancel private: void slotCancel() { QGpgMEJob::doSlotCancel(); } +#define make_progress_emitter private: void doEmitProgressSignal( const QString & what, int cur, int tot ) { emit progress( what, cur, tot ); } +#define make_done_emitter private: void doEmitDoneSignal() { emit done(); } +#define make_auditLogAsHtml private: QString auditLogAsHtml() const { return QGpgMEJob::auditLogAsHtml(); } +#define QGPGME_JOB make_slot_cancel make_progress_emitter make_done_emitter make_auditLogAsHtml + +#endif // __KLEO_QGPGMEJOB_H__ diff --git a/certmanager/lib/backends/qgpgme/qgpgmekeygenerationjob.cpp b/certmanager/lib/backends/qgpgme/qgpgmekeygenerationjob.cpp new file mode 100644 index 000000000..a0ab78f23 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmekeygenerationjob.cpp @@ -0,0 +1,86 @@ +/* + qgpgmekeygenerationjob.cpp + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "qgpgmekeygenerationjob.h" + +#include <qgpgme/dataprovider.h> +#include <qgpgme/eventloopinteractor.h> + +#include <gpgmepp/context.h> +#include <gpgmepp/keygenerationresult.h> +#include <gpgmepp/data.h> + +#include <assert.h> + +Kleo::QGpgMEKeyGenerationJob::QGpgMEKeyGenerationJob( GpgME::Context * context ) + : KeyGenerationJob( QGpgME::EventLoopInteractor::instance(), "Kleo::QGpgMEKeyGenerationJob" ), + QGpgMEJob( this, context ), + mPubKeyDataProvider( 0 ), + mPubKey( 0 ) +{ + assert( context ); +} + +Kleo::QGpgMEKeyGenerationJob::~QGpgMEKeyGenerationJob() { + delete mPubKey; mPubKey = 0; + delete mPubKeyDataProvider; mPubKeyDataProvider = 0; +} + +GpgME::Error Kleo::QGpgMEKeyGenerationJob::start( const QString & parameters ) { + assert( !mPubKey ); + + // set up empty data object for the public key data + if ( mCtx->protocol() == GpgME::Context::CMS ) { + mPubKeyDataProvider = new QGpgME::QByteArrayDataProvider(); + mPubKey = new GpgME::Data( mPubKeyDataProvider ); + assert( !mPubKey->isNull() ); + } + + hookupContextToEventLoopInteractor(); + + const GpgME::Error err = + mCtx->startKeyGeneration( parameters.utf8().data(), mPubKey ? *mPubKey : GpgME::Data::null ); + + if ( err ) + deleteLater(); + return err; +} + +void Kleo::QGpgMEKeyGenerationJob::doOperationDoneEvent( const GpgME::Error & ) { + emit result( mCtx->keyGenerationResult(), mPubKeyDataProvider ? mPubKeyDataProvider->data() : QByteArray() ); +} + +#include "qgpgmekeygenerationjob.moc" diff --git a/certmanager/lib/backends/qgpgme/qgpgmekeygenerationjob.h b/certmanager/lib/backends/qgpgme/qgpgmekeygenerationjob.h new file mode 100644 index 000000000..973aeb12d --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmekeygenerationjob.h @@ -0,0 +1,78 @@ +/* + qgpgmekeygenerationjob.h + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_QGPGMEKEYGENERATIONJOB_H__ +#define __KLEO_QGPGMEKEYGENERATIONJOB_H__ + +#include <kleo/keygenerationjob.h> + +#include "qgpgmejob.h" +#include <kdepimmacros.h> + +namespace GpgME { + class Error; + class Context; + class Key; + class Data; +} + +namespace QGpgME { + class QByteArrayDataProvider; +} + +namespace Kleo { + + class KDE_EXPORT QGpgMEKeyGenerationJob : public KeyGenerationJob, private QGpgMEJob { + Q_OBJECT QGPGME_JOB + public: + QGpgMEKeyGenerationJob( GpgME::Context * context ); + ~QGpgMEKeyGenerationJob(); + + /*! \reimp from KeygenerationJob */ + GpgME::Error start( const QString & parameters ); + + private slots: + void slotOperationDoneEvent( GpgME::Context * context, const GpgME::Error & error ) { + QGpgMEJob::doSlotOperationDoneEvent( context, error ); + } + + private: + void doOperationDoneEvent( const GpgME::Error & e ); + + private: + QGpgME::QByteArrayDataProvider * mPubKeyDataProvider; + GpgME::Data * mPubKey; + }; + +} + +#endif // __KLEO_QGPGMEKEYGENERATIONJOB_H__ diff --git a/certmanager/lib/backends/qgpgme/qgpgmekeylistjob.cpp b/certmanager/lib/backends/qgpgme/qgpgmekeylistjob.cpp new file mode 100644 index 000000000..9c87e3ef6 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmekeylistjob.cpp @@ -0,0 +1,187 @@ +/* + qgpgmekeylistjob.cpp + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "qgpgmekeylistjob.h" + +#include <qgpgme/eventloopinteractor.h> + +#include <gpgmepp/key.h> +#include <gpgmepp/context.h> +#include <gpgmepp/keylistresult.h> +#include <gpg-error.h> + +#include <kmessagebox.h> +#include <klocale.h> +#include <kdebug.h> + +#include <qstringlist.h> + +#include <algorithm> + +#include <stdlib.h> +#include <string.h> +#include <assert.h> + +Kleo::QGpgMEKeyListJob::QGpgMEKeyListJob( GpgME::Context * context ) + : KeyListJob( QGpgME::EventLoopInteractor::instance(), "Kleo::QGpgMEKeyListJob" ), + QGpgMEJob( this, context ), + mResult(), mSecretOnly( false ) +{ + assert( context ); +} + +Kleo::QGpgMEKeyListJob::~QGpgMEKeyListJob() { +} + +void Kleo::QGpgMEKeyListJob::setup( const QStringList & pats, bool secretOnly ) { + assert( !patterns() ); + + mSecretOnly = secretOnly; + setPatterns( pats ); +} + +GpgME::Error Kleo::QGpgMEKeyListJob::start( const QStringList & pats, bool secretOnly ) { + setup( pats, secretOnly ); + + hookupContextToEventLoopInteractor(); + connect( QGpgME::EventLoopInteractor::instance(), + SIGNAL(nextKeyEventSignal(GpgME::Context*,const GpgME::Key&)), + SLOT(slotNextKeyEvent(GpgME::Context*,const GpgME::Key&)) ); + + // The communication channel between gpgme and gpgsm is limited in + // the number of patterns that can be transported, but they won't + // say to how much, so we need to find out ourselves if we get a + // LINE_TOO_LONG error back... + + // We could of course just feed them single patterns, and that would + // probably be easier, but the performance penalty would currently + // be noticable. + + while ( const GpgME::Error err = mCtx->startKeyListing( patterns(), mSecretOnly ) ) { + if ( err.code() == GPG_ERR_LINE_TOO_LONG ) { + setChunkSize( chunkSize()/2 ); + if ( chunkSize() >= 1 ) { + kdDebug(5150) << "QGpgMEKeyListJob::start(): retrying keylisting with chunksize " << chunkSize() << endl; + continue; + } + } + deleteLater(); + mResult = GpgME::KeyListResult( 0, err ); + return err; + } + mResult = GpgME::KeyListResult( 0, 0 ); + return 0; +} + +GpgME::KeyListResult Kleo::QGpgMEKeyListJob::exec( const QStringList & pats, bool secretOnly, std::vector<GpgME::Key> & keys ) { + setup( pats, secretOnly ); + + // The communication channel between gpgme and gpgsm is limited in + // the number of patterns that can be transported, but they won't + // say to how much, so we need to find out ourselves if we get a + // LINE_TOO_LONG error back... + + // We could of course just feed them single patterns, and that would + // probably be easier, but the performance penalty would currently + // be noticable. + + for (;;) { + keys.clear(); + mResult = attemptSyncKeyListing( keys ); + if ( !mResult.error() || mResult.error().code() != GPG_ERR_LINE_TOO_LONG ) + return mResult; + // got LINE_TOO_LONG, try a smaller chunksize: + setChunkSize( chunkSize()/2 ); + if ( chunkSize() < 1 ) + // chunks smaller than one can't be -> return the error. + return mResult; + kdDebug(5150) << "QGpgMEKeyListJob::exec(): retrying keylisting with chunksize " << chunkSize() << endl; + } + kdFatal(5150) << "QGpgMEKeyListJob::exec(): Oops, this is not supposed to happen!" << endl; + return GpgME::KeyListResult(); +} + +GpgME::KeyListResult Kleo::QGpgMEKeyListJob::attemptSyncKeyListing( std::vector<GpgME::Key> & keys ) { + GpgME::KeyListResult result; + for ( const char* * chunk = patterns() ; chunk ; chunk = nextChunk() ) { + + if ( const GpgME::Error err = mCtx->startKeyListing( chunk, mSecretOnly ) ) + return GpgME::KeyListResult( 0, err ); + + GpgME::Error err; + do + keys.push_back( mCtx->nextKey( err ) ); + while ( !err ); + keys.pop_back(); + result.mergeWith( mCtx->endKeyListing() ); + if ( result.error() ) + break; + } + return result; +} + +void Kleo::QGpgMEKeyListJob::slotNextKeyEvent( GpgME::Context * context, const GpgME::Key & key ) { + if ( context == mCtx ) + emit nextKey( key ); +} + +void Kleo::QGpgMEKeyListJob::slotOperationDoneEvent( GpgME::Context * context, const GpgME::Error & ) { + if ( context != mCtx ) + return; + mResult.mergeWith( mCtx->keyListResult() ); + if ( !mResult.error() ) + if ( const char* * chunk = nextChunk() ) { + if ( const GpgME::Error err = mCtx->startKeyListing( chunk, mSecretOnly ) ) + mResult.mergeWith( GpgME::KeyListResult( 0, err ) ); + else + return; + } + emit done(); + emit result( mResult ); + deleteLater(); +} + +void Kleo::QGpgMEKeyListJob::showErrorDialog( QWidget * parent, const QString & caption ) const { + if ( !mResult.error() || mResult.error().isCanceled() ) + return; + const QString msg = i18n( "<qt><p>An error occurred while fetching " + "the keys from the backend:</p>" + "<p><b>%1</b></p></qt>" ) + .arg( QString::fromLocal8Bit( mResult.error().asString() ) ); + KMessageBox::error( parent, msg, caption ); +} + +#include "qgpgmekeylistjob.moc" diff --git a/certmanager/lib/backends/qgpgme/qgpgmekeylistjob.h b/certmanager/lib/backends/qgpgme/qgpgmekeylistjob.h new file mode 100644 index 000000000..8320e0134 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmekeylistjob.h @@ -0,0 +1,81 @@ +/* + qgpgmekeylistjob.h + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_QGPGMEKEYLISTJOB_H__ +#define __KLEO_QGPGMEKEYLISTJOB_H__ + +#include <kleo/keylistjob.h> + +#include <gpgmepp/keylistresult.h> + +#include "qgpgmejob.h" + +namespace GpgME { + class Error; + class Context; + class Key; +} + +namespace Kleo { + + class QGpgMEKeyListJob : public KeyListJob, private QGpgMEJob { + Q_OBJECT QGPGME_JOB + public: + QGpgMEKeyListJob( GpgME::Context * context ); + ~QGpgMEKeyListJob(); + + /*! \reimp from KeyListJob */ + GpgME::Error start( const QStringList & patterns, bool secretOnly ); + + /*! \reimp from KeyListJob */ + GpgME::KeyListResult exec( const QStringList & patterns, bool secretOnly, std::vector<GpgME::Key> & keys ); + + /*! \reimp from Job */ + void showErrorDialog( QWidget * parent, const QString & caption ) const; + + private slots: + void slotNextKeyEvent( GpgME::Context * context, const GpgME::Key & key ); + void slotOperationDoneEvent( GpgME::Context * context, const GpgME::Error & e ); + + private: + void doOperationDoneEvent( const GpgME::Error &) {} // unused, we implement slotOperationDoneEvent ourselves. + void setup( const QStringList &, bool ); + GpgME::KeyListResult attemptSyncKeyListing( std::vector<GpgME::Key> & ); + + private: + GpgME::KeyListResult mResult; + bool mSecretOnly; + }; + +} + +#endif // __KLEO_QGPGMEKEYLISTJOB_H__ diff --git a/certmanager/lib/backends/qgpgme/qgpgmeprogresstokenmapper.cpp b/certmanager/lib/backends/qgpgme/qgpgmeprogresstokenmapper.cpp new file mode 100644 index 000000000..3b6ae35eb --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmeprogresstokenmapper.cpp @@ -0,0 +1,159 @@ +/* -*- mode: C++; c-file-style: "gnu" -*- + qgpgmeprogresstokenmapper.cpp + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "qgpgmeprogresstokenmapper.h" + +#include <klocale.h> + +#include <qstring.h> + +#include <assert.h> +#include <map> + +struct Desc { + int type; // 0 == fallback + const char * display; // add %1 for useCur ^ useTot and %1 %2 for useCur == useTot == true + bool useCur : 1; + bool useTot : 1; +}; + +static const struct Desc pk_dsa[] = { + { 0, I18N_NOOP("Generating DSA key..."), false, false } +}; + +static const struct Desc pk_elg[] = { + { 0, I18N_NOOP("Generating ElGamal key..."), false, false } +}; + +static const struct Desc primegen[] = { + // FIXME: add all type's? + { 0, I18N_NOOP("Searching for a large prime number..."), false, false } +}; + +static const struct Desc need_entropy[] = { + { 0, I18N_NOOP("Waiting for new entropy from random number generator (you might want to exercise the harddisks or move the mouse)..."), false, false } +}; + +static const struct Desc tick[] = { + { 0, I18N_NOOP("Please wait..."), false, false } +}; + +static const struct Desc starting_agent[] = { + { 0, I18N_NOOP("Starting gpg-agent (you should consider starting a global instance instead)..."), false, false } +}; + +static const struct { + const char * token; + const Desc * desc; + unsigned int numDesc; +} tokens[] = { +#define make_token(x) { #x, x, sizeof(x) / sizeof(*x) } + make_token(pk_dsa), + make_token(pk_elg), + make_token(primegen), + make_token(need_entropy), + make_token(tick), + make_token(starting_agent) +#undef make_token +}; + + + +Kleo::QGpgMEProgressTokenMapper * Kleo::QGpgMEProgressTokenMapper::mSelf = 0; + +const Kleo::QGpgMEProgressTokenMapper * Kleo::QGpgMEProgressTokenMapper::instance() { + if ( !mSelf ) + (void) new QGpgMEProgressTokenMapper(); + return mSelf; +} + +Kleo::QGpgMEProgressTokenMapper::QGpgMEProgressTokenMapper() { + mSelf = this; +} + +Kleo::QGpgMEProgressTokenMapper::~QGpgMEProgressTokenMapper() { + mSelf = 0; +} + +typedef std::map< QString, std::map<int,Desc> > Map; + +static const Map & makeMap() { // return a reference to a static to avoid copying + static Map map; + for ( unsigned int i = 0 ; i < sizeof tokens / sizeof *tokens ; ++i ) { + assert( tokens[i].token ); + const QString token = QString::fromLatin1( tokens[i].token ).lower(); + for ( unsigned int j = 0 ; j < tokens[i].numDesc ; ++j ) { + const Desc & desc = tokens[i].desc[j]; + assert( desc.display ); + map[ token ][ desc.type ] = desc; + } + } + return map; +} + +QString Kleo::QGpgMEProgressTokenMapper::map( const char * tokenUtf8, int subtoken, int cur, int tot ) const { + if ( !tokenUtf8 || !*tokenUtf8 ) + return QString::null; + + if ( qstrcmp( tokenUtf8, "file:" ) == 0 ) + return QString::null; // gpgme's job + + return map( QString::fromUtf8( tokenUtf8 ), subtoken, cur, tot ); +} + +QString Kleo::QGpgMEProgressTokenMapper::map( const QString & token, int subtoken, int cur, int tot ) const { + if ( token.startsWith( "file:" ) ) + return QString::null; // gpgme's job + + static const Map & tokenMap = makeMap(); + + const Map::const_iterator it1 = tokenMap.find( token.lower() ); + if ( it1 == tokenMap.end() ) + return token; + std::map<int,Desc>::const_iterator it2 = it1->second.find( subtoken ); + if ( it2 == it1->second.end() ) + it2 = it1->second.find( 0 ); + if ( it2 == it1->second.end() ) + return token; + const Desc & desc = it2->second; + QString result = i18n( desc.display ); + if ( desc.useCur ) + result = result.arg( cur ); + if ( desc.useTot ) + result = result.arg( tot ); + return result; +} + diff --git a/certmanager/lib/backends/qgpgme/qgpgmeprogresstokenmapper.h b/certmanager/lib/backends/qgpgme/qgpgmeprogresstokenmapper.h new file mode 100644 index 000000000..d0a8da6c3 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmeprogresstokenmapper.h @@ -0,0 +1,58 @@ +/* + qgpgmeprogresstokenmapper.h + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_QGPGMEPROGRESSTOKENMAPPER_H__ +#define __KLEO_QGPGMEPROGRESSTOKENMAPPER_H__ + + +class QString; + +namespace Kleo { + + class QGpgMEProgressTokenMapper { + QGpgMEProgressTokenMapper(); + ~QGpgMEProgressTokenMapper(); + public: + static const QGpgMEProgressTokenMapper * instance(); + + QString map( const char * token, int subtoken, int current, int total ) const; + QString map( const QString & token, int subtoken, int current, int total ) const; + + private: + static QGpgMEProgressTokenMapper * mSelf; + }; + +} + + +#endif // __KLEO_QGPGMEPROGRESSTOKENMAPPER_H__ diff --git a/certmanager/lib/backends/qgpgme/qgpgmerefreshkeysjob.cpp b/certmanager/lib/backends/qgpgme/qgpgmerefreshkeysjob.cpp new file mode 100644 index 000000000..325306e8c --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmerefreshkeysjob.cpp @@ -0,0 +1,207 @@ +/* + qgpgmerefreshkeysjob.cpp + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "qgpgmerefreshkeysjob.h" + +#include "gnupgprocessbase.h" +#include "qgpgmeprogresstokenmapper.h" + +#include <kdebug.h> + +#include <gpgmepp/context.h> + +#include <qgpgme/eventloopinteractor.h> + +#include <qstringlist.h> + +#include <gpg-error.h> + +#include <assert.h> + +Kleo::QGpgMERefreshKeysJob::QGpgMERefreshKeysJob() + : RefreshKeysJob( QGpgME::EventLoopInteractor::instance(), "Kleo::QGpgMERefreshKeysJob" ), + mProcess( 0 ), + mError( 0 ) +{ + +} + +Kleo::QGpgMERefreshKeysJob::~QGpgMERefreshKeysJob() { + +} + +GpgME::Error Kleo::QGpgMERefreshKeysJob::start( const QStringList & patterns ) { + assert( mPatternsToDo.empty() ); + + mPatternsToDo = patterns; + if ( mPatternsToDo.empty() ) + mPatternsToDo.push_back( " " ); // empty list means all -> mae + // sure to fail the first + // startAProcess() guard clause + + return startAProcess(); +} + +#if MAX_CMD_LENGTH < 65 + 128 +#error MAX_CMD_LENGTH is too low +#endif + +GpgME::Error Kleo::QGpgMERefreshKeysJob::startAProcess() { + if ( mPatternsToDo.empty() ) + return 0; + // create and start gpgsm process: + mProcess = new GnuPGProcessBase( this, "gpgsm -k --with-validation --force-crl-refresh --enable-crl-checks" ); + + // FIXME: obbtain the path to gpgsm from gpgme, so we use the same instance. + *mProcess << "gpgsm" << "-k" << "--with-validation" << "--force-crl-refresh" + << "--enable-crl-checks"; + unsigned int commandLineLength = MAX_CMD_LENGTH; + commandLineLength -= + strlen("gpgsm") + 1 + strlen("-k") + 1 + + strlen("--with-validation") + 1 + strlen("--force-crl-refresh") + 1 + + strlen("--enable-crl-checks") + 1; + while ( !mPatternsToDo.empty() ) { + const QCString pat = mPatternsToDo.front().utf8().stripWhiteSpace(); + const unsigned int patLength = pat.length(); + if ( patLength >= commandLineLength ) + break; + mPatternsToDo.pop_front(); + if ( pat.isEmpty() ) + continue; + *mProcess << pat; + commandLineLength -= patLength + 1; + } + + mProcess->setUseStatusFD( true ); + + connect( mProcess, SIGNAL(processExited(KProcess*)), + SLOT(slotProcessExited(KProcess*)) ); + connect( mProcess, SIGNAL(receivedStderr(KProcess*,char*,int)), + SLOT(slotStderr(KProcess*,char*,int)) ); + connect( mProcess, SIGNAL(status(Kleo::GnuPGProcessBase*,const QString&,const QStringList&)), + SLOT(slotStatus(Kleo::GnuPGProcessBase*,const QString&,const QStringList&)) ); + + if ( !mProcess->start( KProcess::NotifyOnExit, KProcess::Stderr ) ) { + mError = gpg_err_make( GPG_ERR_SOURCE_GPGSM, GPG_ERR_ENOENT ); // what else? + deleteLater(); + return mError; + } else + return 0; +} + +void Kleo::QGpgMERefreshKeysJob::slotCancel() { + if ( mProcess ) + mProcess->kill(); + mProcess = 0; + mError = gpg_err_make( GPG_ERR_SOURCE_GPGSM, GPG_ERR_CANCELED ); +} + +void Kleo::QGpgMERefreshKeysJob::slotStatus( GnuPGProcessBase * proc, const QString & type, const QStringList & args ) { + if ( proc != mProcess ) + return; + QStringList::const_iterator it = args.begin(); + bool ok = false; + + if ( type == "ERROR" ) { + + + if ( args.size() < 2 ) { + kdDebug( 5150 ) << "Kleo::QGpgMERefreshKeysJob::slotStatus() not recognising ERROR with < 2 args!" << endl; + return; + } + const int source = (*++it).toInt( &ok ); + if ( !ok ) { + kdDebug( 5150 ) << "Kleo::QGpgMERefreshKeysJob::slotStatus() expected number for first ERROR arg, got something else" << endl; + return; + } + ok = false; + const int code = (*++it).toInt( &ok ); + if ( !ok ) { + kdDebug( 5150 ) << "Kleo::QGpgMERefreshKeysJob::slotStatus() expected number for second ERROR arg, got something else" << endl; + return; + } + mError = gpg_err_make( (gpg_err_source_t)source, (gpg_err_code_t)code ); + + + } else if ( type == "PROGRESS" ) { + + + if ( args.size() < 4 ) { + kdDebug( 5150 ) << "Kleo::QGpgMERefreshKeysJob::slotStatus() not recognising PROGRESS with < 4 args!" << endl; + return; + } + const QString what = *++it; + ++it; // don't use "type"... + const int cur = (*++it).toInt( &ok ); + if ( !ok ) { + kdDebug( 5150 ) << "Kleo::QGpgMERefreshKeysJob::slotStatus() expected number for \"cur\", got something else" << endl; + return; + } + ok = false; + const int total = (*++it).toInt( &ok ); + if ( !ok ) { + kdDebug( 5150 ) << "Kleo::QGpgMERefreshKeysJob::slotStatus() expected number for \"total\", got something else" << endl; + return; + } + emit progress( QGpgMEProgressTokenMapper::instance()->map( what, 0, cur, total ), cur, total ); + + + } +} + +void Kleo::QGpgMERefreshKeysJob::slotStderr( KProcess *, char *, int ) { + // implement? or not? +} + +void Kleo::QGpgMERefreshKeysJob::slotProcessExited( KProcess * proc ) { + if ( proc != mProcess ) + return; + + if ( !mError && !mPatternsToDo.empty() ) + if ( const GpgME::Error err = startAProcess() ) + mError = err; + else + return; + + emit done(); + if ( !mError && + ( !mProcess->normalExit() || mProcess->exitStatus() != 0 ) ) + mError = gpg_err_make( GPG_ERR_SOURCE_GPGSM, GPG_ERR_GENERAL ); + emit result( mError ); + deleteLater(); +} + +#include "qgpgmerefreshkeysjob.moc" diff --git a/certmanager/lib/backends/qgpgme/qgpgmerefreshkeysjob.h b/certmanager/lib/backends/qgpgme/qgpgmerefreshkeysjob.h new file mode 100644 index 000000000..a0132f223 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmerefreshkeysjob.h @@ -0,0 +1,80 @@ +/* + qgpgmerefreshkeysjob.h + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_QGPGMEREFRESHKEYSJOB_H__ +#define __KLEO_QGPGMEREFRESHKEYSJOB_H__ + +#include <kleo/refreshkeysjob.h> + +#include <qstringlist.h> + +namespace Kleo { + class GnuPGProcessBase; +} + +namespace GpgME { + class Error; +} + +class KProcess; + +namespace Kleo { + + class QGpgMERefreshKeysJob : public RefreshKeysJob { + Q_OBJECT + public: + QGpgMERefreshKeysJob(); + ~QGpgMERefreshKeysJob(); + + /*! \reimp from RefreshKeysJob */ + GpgME::Error start( const QStringList & patterns ); + + private slots: + /*! \reimp from Job */ + void slotCancel(); + + void slotStatus( Kleo::GnuPGProcessBase *, const QString &, const QStringList & ); + void slotStderr( KProcess *, char *, int ); + void slotProcessExited( KProcess * ); + + private: + GpgME::Error startAProcess(); + + private: + GnuPGProcessBase * mProcess; + int mError; + QStringList mPatternsToDo; + }; + +} + +#endif // __KLEO_QGPGMEREFRESHKEYSJOB_H__ diff --git a/certmanager/lib/backends/qgpgme/qgpgmesecretkeyexportjob.cpp b/certmanager/lib/backends/qgpgme/qgpgmesecretkeyexportjob.cpp new file mode 100644 index 000000000..0ce543cf9 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmesecretkeyexportjob.cpp @@ -0,0 +1,196 @@ +/* + qgpgmesecretexportjob.cpp + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "qgpgmesecretkeyexportjob.h" + +#include "gnupgprocessbase.h" +#include "qgpgmeprogresstokenmapper.h" + +#include <kdebug.h> + +#include <gpgmepp/context.h> +#include <gpgmepp/data.h> + +#include <qgpgme/eventloopinteractor.h> + +#include <qstringlist.h> + +#include <gpg-error.h> + +#include <string.h> +#include <assert.h> + +Kleo::QGpgMESecretKeyExportJob::QGpgMESecretKeyExportJob( bool armour, const QString& charset ) + : ExportJob( QGpgME::EventLoopInteractor::instance(), "Kleo::QGpgMESecretKeyExportJob" ), + mProcess( 0 ), + mError( 0 ), + mArmour( armour ), + mCharset( charset ) +{ + +} + +Kleo::QGpgMESecretKeyExportJob::~QGpgMESecretKeyExportJob() { + +} + +GpgME::Error Kleo::QGpgMESecretKeyExportJob::start( const QStringList & patterns ) { + assert( mKeyData.isEmpty() ); + + if ( patterns.size() != 1 || patterns.front().isEmpty() ) { + deleteLater(); + return mError = gpg_err_make( GPG_ERR_SOURCE_GPGSM, GPG_ERR_INV_VALUE ); + } + + // create and start gpgsm process: + mProcess = new GnuPGProcessBase( this, "gpgsm --export-secret-key-p12" ); + + // FIXME: obtain the path to gpgsm from gpgme, so we use the same instance. + *mProcess << "gpgsm" << "--export-secret-key-p12"; + if ( mArmour ) + *mProcess << "--armor"; + if ( !mCharset.isEmpty() ) + *mProcess << "--p12-charset" << mCharset; + *mProcess << patterns.front().utf8(); + + mProcess->setUseStatusFD( true ); + + connect( mProcess, SIGNAL(processExited(KProcess*)), + SLOT(slotProcessExited(KProcess*)) ); + connect( mProcess, SIGNAL(receivedStdout(KProcess*,char*,int)), + SLOT(slotStdout(KProcess*,char*,int)) ); + connect( mProcess, SIGNAL(receivedStderr(KProcess*,char*,int)), + SLOT(slotStderr(KProcess*,char*,int)) ); + connect( mProcess, SIGNAL(status(Kleo::GnuPGProcessBase*,const QString&,const QStringList&)), + SLOT(slotStatus(Kleo::GnuPGProcessBase*,const QString&,const QStringList&)) ); + + if ( !mProcess->start( KProcess::NotifyOnExit, KProcess::AllOutput ) ) { + mError = gpg_err_make( GPG_ERR_SOURCE_GPGSM, GPG_ERR_ENOENT ); // what else? + deleteLater(); + return mError; + } else + return 0; +} + +void Kleo::QGpgMESecretKeyExportJob::slotCancel() { + if ( mProcess ) + mProcess->kill(); + mProcess = 0; + mError = gpg_err_make( GPG_ERR_SOURCE_GPGSM, GPG_ERR_CANCELED ); +} + +void Kleo::QGpgMESecretKeyExportJob::slotStatus( GnuPGProcessBase * proc, const QString & type, const QStringList & args ) { + if ( proc != mProcess ) + return; + QStringList::const_iterator it = args.begin(); + bool ok = false; + + if ( type == "ERROR" ) { + + + if ( args.size() < 2 ) { + kdDebug( 5150 ) << "Kleo::QGpgMESecretKeyExportJob::slotStatus() not recognising ERROR with < 2 args!" << endl; + return; + } + const int source = (*++it).toInt( &ok ); + if ( !ok ) { + kdDebug( 5150 ) << "Kleo::QGpgMESecretKeyExportJob::slotStatus() expected number for first ERROR arg, got something else" << endl; + return; + } + ok = false; + const int code = (*++it).toInt( &ok ); + if ( !ok ) { + kdDebug( 5150 ) << "Kleo::QGpgMESecretKeyExportJob::slotStatus() expected number for second ERROR arg, got something else" << endl; + return; + } + mError = gpg_err_make( (gpg_err_source_t)source, (gpg_err_code_t)code ); + + + } else if ( type == "PROGRESS" ) { + + + if ( args.size() < 4 ) { + kdDebug( 5150 ) << "Kleo::QGpgMESecretKeyExportJob::slotStatus() not recognising PROGRESS with < 4 args!" << endl; + return; + } + const QString what = *++it; + ++it; // don't use "type"... + const int cur = (*++it).toInt( &ok ); + if ( !ok ) { + kdDebug( 5150 ) << "Kleo::QGpgMESecretKeyExportJob::slotStatus() expected number for \"cur\", got something else" << endl; + return; + } + ok = false; + const int total = (*++it).toInt( &ok ); + if ( !ok ) { + kdDebug( 5150 ) << "Kleo::QGpgMESecretKeyExportJob::slotStatus() expected number for \"total\", got something else" << endl; + return; + } + emit progress( QGpgMEProgressTokenMapper::instance()->map( what, 0, cur, total ), cur, total ); + + + } +} + +void Kleo::QGpgMESecretKeyExportJob::slotStdout( KProcess * proc, char * buf, int buflen ) { + if ( proc != mProcess ) + return; + if ( buflen <= 0 ) + return; + if ( !buf ) + return; + const unsigned int oldlen = mKeyData.size(); + mKeyData.resize( oldlen + buflen ); + memcpy( mKeyData.data() + oldlen, buf, buflen ); +} + +void Kleo::QGpgMESecretKeyExportJob::slotStderr( KProcess *, char *, int ) { + // implement? or not? +} + +void Kleo::QGpgMESecretKeyExportJob::slotProcessExited( KProcess * proc ) { + if ( proc != mProcess ) + return; + + emit done(); + if ( !mError && + ( !mProcess->normalExit() || mProcess->exitStatus() != 0 ) ) + mError = gpg_err_make( GPG_ERR_SOURCE_GPGSM, GPG_ERR_GENERAL ); + emit result( mError, mKeyData ); + deleteLater(); +} + +#include "qgpgmesecretkeyexportjob.moc" diff --git a/certmanager/lib/backends/qgpgme/qgpgmesecretkeyexportjob.h b/certmanager/lib/backends/qgpgme/qgpgmesecretkeyexportjob.h new file mode 100644 index 000000000..5f5e3b69c --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmesecretkeyexportjob.h @@ -0,0 +1,85 @@ +/* + qgpgmesecretkeyexportjob.h + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_QGPGMESECRETKEYEXPORTJOB_H__ +#define __KLEO_QGPGMESECRETKEYEXPORTJOB_H__ + +#include <kleo/exportjob.h> + +#include <qcstring.h> + +namespace Kleo { + class GnuPGProcessBase; +} + +namespace GpgME { + class Error; + class Data; +} + +namespace QGpgME { + class QByteArrayDataProvider; +} + +class KProcess; + +namespace Kleo { + + class QGpgMESecretKeyExportJob : public ExportJob { + Q_OBJECT + public: + QGpgMESecretKeyExportJob( bool armour, const QString& charset ); + ~QGpgMESecretKeyExportJob(); + + /*! \reimp from ExportJob */ + GpgME::Error start( const QStringList & patterns ); + + private slots: + /*! \reimp from Job */ + void slotCancel(); + + void slotStatus( Kleo::GnuPGProcessBase *, const QString &, const QStringList & ); + void slotStdout( KProcess *, char *, int ); + void slotStderr( KProcess *, char *, int ); + void slotProcessExited( KProcess * ); + + private: + GnuPGProcessBase * mProcess; + QByteArray mKeyData; + int mError; + bool mArmour; + QString mCharset; + }; + +} + +#endif // __KLEO_QGPGMESECRETKEYEXPORTJOB_H__ diff --git a/certmanager/lib/backends/qgpgme/qgpgmesignencryptjob.cpp b/certmanager/lib/backends/qgpgme/qgpgmesignencryptjob.cpp new file mode 100644 index 000000000..fae748478 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmesignencryptjob.cpp @@ -0,0 +1,121 @@ +/* + qgpgmesignencryptjob.cpp + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "qgpgmesignencryptjob.h" + +#include "ui/messagebox.h" + +#include <qgpgme/eventloopinteractor.h> +#include <qgpgme/dataprovider.h> + +#include <gpgmepp/context.h> +#include <gpgmepp/data.h> +#include <gpgmepp/key.h> + +#include <klocale.h> + +#include <assert.h> + +Kleo::QGpgMESignEncryptJob::QGpgMESignEncryptJob( GpgME::Context * context ) + : SignEncryptJob( QGpgME::EventLoopInteractor::instance(), "Kleo::QGpgMESignEncryptJob" ), + QGpgMEJob( this, context ) +{ + assert( context ); +} + +Kleo::QGpgMESignEncryptJob::~QGpgMESignEncryptJob() { +} + +GpgME::Error Kleo::QGpgMESignEncryptJob::setup( const std::vector<GpgME::Key> & signers, + const QByteArray & plainText ) { + assert( !mInData ); + assert( !mOutData ); + + createInData( plainText ); + createOutData(); + + return setSigningKeys( signers ); +} + +GpgME::Error Kleo::QGpgMESignEncryptJob::start( const std::vector<GpgME::Key> & signers, + const std::vector<GpgME::Key> & recipients, + const QByteArray & plainText, bool alwaysTrust ) { + if ( const GpgME::Error error = setup( signers, plainText ) ) { + deleteLater(); + return error; + } + + hookupContextToEventLoopInteractor(); + + const GpgME::Context::EncryptionFlags flags = + alwaysTrust ? GpgME::Context::AlwaysTrust : GpgME::Context::None ; + const GpgME::Error err = mCtx->startCombinedSigningAndEncryption( recipients, *mInData, *mOutData, flags ); + + if ( err ) + deleteLater(); + mResult.first = GpgME::SigningResult( err ); + mResult.second = GpgME::EncryptionResult( err ); + return err; +} + +std::pair<GpgME::SigningResult,GpgME::EncryptionResult> +Kleo::QGpgMESignEncryptJob::exec( const std::vector<GpgME::Key> & signers, + const std::vector<GpgME::Key> & recipients, + const QByteArray & plainText, bool alwaysTrust, + QByteArray & cipherText ) { + if ( GpgME::Error err = setup( signers, plainText ) ) + return std::make_pair( GpgME::SigningResult( 0, err ), GpgME::EncryptionResult() ); + const GpgME::Context::EncryptionFlags flags = + alwaysTrust ? GpgME::Context::AlwaysTrust : GpgME::Context::None ; + mResult = mCtx->signAndEncrypt( recipients, *mInData, *mOutData, flags ); + cipherText = mOutDataDataProvider->data(); + getAuditLog(); + return mResult; +} + +void Kleo::QGpgMESignEncryptJob::doOperationDoneEvent( const GpgME::Error & ) { + mResult.first = mCtx->signingResult(); + mResult.second = mCtx->encryptionResult(); + emit result( mResult.first, mResult.second, mOutDataDataProvider->data() ); +} + +void Kleo::QGpgMESignEncryptJob::showErrorDialog( QWidget * parent, const QString & caption ) const { + if ( mResult.first.error() && !mResult.first.error().isCanceled() || + mResult.second.error() && !mResult.second.error().isCanceled() ) + Kleo::MessageBox::error( parent, mResult.first, mResult.second, this, caption ); +} + +#include "qgpgmesignencryptjob.moc" diff --git a/certmanager/lib/backends/qgpgme/qgpgmesignencryptjob.h b/certmanager/lib/backends/qgpgme/qgpgmesignencryptjob.h new file mode 100644 index 000000000..a826edcbf --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmesignencryptjob.h @@ -0,0 +1,91 @@ +/* + qgpgmesignencryptjob.h + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_QGPGMESIGNENCRYPTJOB_H__ +#define __KLEO_QGPGMESIGNENCRYPTJOB_H__ + +#include <kleo/signencryptjob.h> + +#include "qgpgmejob.h" + +#include <gpgmepp/signingresult.h> +#include <gpgmepp/encryptionresult.h> + +#include <qcstring.h> + +#include <utility> + +#include <kdepimmacros.h> +namespace GpgME { + class Error; + class Context; + class Key; +} + +namespace Kleo { + + class KDE_EXPORT QGpgMESignEncryptJob : public SignEncryptJob, private QGpgMEJob { + Q_OBJECT QGPGME_JOB + public: + QGpgMESignEncryptJob( GpgME::Context * context ); + ~QGpgMESignEncryptJob(); + + /*! \reimp from SignEncryptJob */ + GpgME::Error start( const std::vector<GpgME::Key> & signers, + const std::vector<GpgME::Key> & recipients, + const QByteArray & plainText, bool alwaysTrust ); + + std::pair<GpgME::SigningResult,GpgME::EncryptionResult> + exec( const std::vector<GpgME::Key> & signers, + const std::vector<GpgME::Key> & recipients, + const QByteArray & plainText, bool alwaysTrust, + QByteArray & cipherText ); + + /*! \reimp from Job */ + void showErrorDialog( QWidget * parent, const QString & caption ) const; + + private slots: + void slotOperationDoneEvent( GpgME::Context * context, const GpgME::Error & e ) { + QGpgMEJob::doSlotOperationDoneEvent( context, e ); + } + + private: + void doOperationDoneEvent( const GpgME::Error & e ); + GpgME::Error setup( const std::vector<GpgME::Key> &, + const QByteArray & ); + private: + std::pair<GpgME::SigningResult,GpgME::EncryptionResult> mResult; + }; + +} + +#endif // __KLEO_QGPGMESIGNENCRYPTJOB_H__ diff --git a/certmanager/lib/backends/qgpgme/qgpgmesignjob.cpp b/certmanager/lib/backends/qgpgme/qgpgmesignjob.cpp new file mode 100644 index 000000000..84485ad5b --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmesignjob.cpp @@ -0,0 +1,113 @@ +/* + qgpgmesignjob.cpp + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "qgpgmesignjob.h" + +#include "ui/messagebox.h" + +#include <qgpgme/eventloopinteractor.h> +#include <qgpgme/dataprovider.h> + +#include <gpgmepp/context.h> +#include <gpgmepp/signingresult.h> +#include <gpgmepp/data.h> +#include <gpgmepp/key.h> + +#include <klocale.h> + +#include <assert.h> + +Kleo::QGpgMESignJob::QGpgMESignJob( GpgME::Context * context ) + : SignJob( QGpgME::EventLoopInteractor::instance(), "Kleo::QGpgMESignJob" ), + QGpgMEJob( this, context ) +{ + assert( context ); +} + +Kleo::QGpgMESignJob::~QGpgMESignJob() { +} + +GpgME::Error Kleo::QGpgMESignJob::setup( const std::vector<GpgME::Key> & signers, + const QByteArray & plainText ) { + assert( !mInData ); + assert( !mOutData ); + + createInData( plainText ); + createOutData(); + + return setSigningKeys( signers ); +} + +GpgME::Error Kleo::QGpgMESignJob::start( const std::vector<GpgME::Key> & signers, + const QByteArray & plainText, + GpgME::Context::SignatureMode mode ) { + if ( const GpgME::Error error = setup( signers, plainText ) ) { + deleteLater(); + return error; + } + + hookupContextToEventLoopInteractor(); + + const GpgME::Error err = mCtx->startSigning( *mInData, *mOutData, mode ); + + if ( err ) + deleteLater(); + mResult = GpgME::SigningResult( err ); + return err; +} + +GpgME::SigningResult Kleo::QGpgMESignJob::exec( const std::vector<GpgME::Key> & signers, + const QByteArray & plainText, + GpgME::Context::SignatureMode mode, + QByteArray & signature ) { + if ( const GpgME::Error err = setup( signers, plainText ) ) + return mResult = GpgME::SigningResult( 0, err ); + mResult = mCtx->sign( *mInData, *mOutData, mode ); + signature = mOutDataDataProvider->data(); + getAuditLog(); + return mResult; +} + +void Kleo::QGpgMESignJob::doOperationDoneEvent( const GpgME::Error & ) { + emit result( mResult = mCtx->signingResult(), mOutDataDataProvider->data() ); +} + +void Kleo::QGpgMESignJob::showErrorDialog( QWidget * parent, const QString & caption ) const { + if ( mResult.error() && !mResult.error().isCanceled() ) + Kleo::MessageBox::error( parent, mResult, this, caption ); +} + +#include "qgpgmesignjob.moc" diff --git a/certmanager/lib/backends/qgpgme/qgpgmesignjob.h b/certmanager/lib/backends/qgpgme/qgpgmesignjob.h new file mode 100644 index 000000000..c2589b477 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmesignjob.h @@ -0,0 +1,87 @@ +/* + qgpgmesignjob.h + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_QGPGMESIGNJOB_H__ +#define __KLEO_QGPGMESIGNJOB_H__ + +#include <kleo/signjob.h> + +#include "qgpgmejob.h" + +#include <gpgmepp/signingresult.h> + +#include <qcstring.h> + +namespace GpgME { + class Error; + class Context; + class Key; +} + +namespace Kleo { + + class QGpgMESignJob : public SignJob, private QGpgMEJob { + Q_OBJECT QGPGME_JOB + public: + QGpgMESignJob( GpgME::Context * context ); + ~QGpgMESignJob(); + + /*! \reimp from SignJob */ + GpgME::Error start( const std::vector<GpgME::Key> & signers, + const QByteArray & plainText, + GpgME::Context::SignatureMode mode ); + + /*! \reimp from SignJob */ + GpgME::SigningResult exec( const std::vector<GpgME::Key> & signers, + const QByteArray & plainText, + GpgME::Context::SignatureMode mode, + QByteArray & signature ); + + /*! \reimp from Job */ + void showErrorDialog( QWidget * parent, const QString & caption ) const; + + private slots: + void slotOperationDoneEvent( GpgME::Context * context, const GpgME::Error & e ) { + QGpgMEJob::doSlotOperationDoneEvent( context, e ); + } + + private: + void doOperationDoneEvent( const GpgME::Error & e ); + GpgME::Error setup( const std::vector<GpgME::Key> &, const QByteArray & ); + + private: + GpgME::SigningResult mResult; + }; + +} + +#endif // __KLEO_QGPGMESIGNJOB_H__ diff --git a/certmanager/lib/backends/qgpgme/qgpgmeverifydetachedjob.cpp b/certmanager/lib/backends/qgpgme/qgpgmeverifydetachedjob.cpp new file mode 100644 index 000000000..cc1186373 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmeverifydetachedjob.cpp @@ -0,0 +1,96 @@ +/* + qgpgmeverifydetachedjob.cpp + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "qgpgmeverifydetachedjob.h" + +#include <qgpgme/eventloopinteractor.h> +#include <qgpgme/dataprovider.h> + +#include <gpgmepp/context.h> +#include <gpgmepp/verificationresult.h> +#include <gpgmepp/data.h> + +#include <assert.h> + +Kleo::QGpgMEVerifyDetachedJob::QGpgMEVerifyDetachedJob( GpgME::Context * context ) + : VerifyDetachedJob( QGpgME::EventLoopInteractor::instance(), "Kleo::QGpgMEVerifyDetachedJob" ), + QGpgMEJob( this, context ) +{ + assert( context ); +} + +Kleo::QGpgMEVerifyDetachedJob::~QGpgMEVerifyDetachedJob() { +} + +void Kleo::QGpgMEVerifyDetachedJob::setup( const QByteArray & signature, const QByteArray & signedData ) { + assert( !mInData ); + assert( !mOutData ); + + createInData( signature ); + + // two "in" data objects - (mis|re)use the "out" data object for the second... + mOutDataDataProvider = new QGpgME::QByteArrayDataProvider( signedData ); + mOutData = new GpgME::Data( mOutDataDataProvider ); + assert( !mOutData->isNull() ); +} + +GpgME::Error Kleo::QGpgMEVerifyDetachedJob::start( const QByteArray & signature, + const QByteArray & signedData ) { + setup( signature, signedData ); + + hookupContextToEventLoopInteractor(); + + const GpgME::Error err = mCtx->startDetachedSignatureVerification( *mInData, *mOutData ); + + if ( err ) + deleteLater(); + return err; +} + +GpgME::VerificationResult Kleo::QGpgMEVerifyDetachedJob::exec( const QByteArray & signature, + const QByteArray & signedData ) { + setup( signature, signedData ); + const GpgME::VerificationResult r = mCtx->verifyDetachedSignature( *mInData, *mOutData ); + getAuditLog(); + return r; +} + +void Kleo::QGpgMEVerifyDetachedJob::doOperationDoneEvent( const GpgME::Error & ) { + emit result( mCtx->verificationResult() ); +} + + +#include "qgpgmeverifydetachedjob.moc" diff --git a/certmanager/lib/backends/qgpgme/qgpgmeverifydetachedjob.h b/certmanager/lib/backends/qgpgme/qgpgmeverifydetachedjob.h new file mode 100644 index 000000000..76141fb80 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmeverifydetachedjob.h @@ -0,0 +1,75 @@ +/* + qgpgmeverifydetachedjob.h + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_QGPGMEVERIFYDETACHEDJOB_H__ +#define __KLEO_QGPGMEVERIFYDETACHEDJOB_H__ + +#include <kleo/verifydetachedjob.h> + +#include "qgpgmejob.h" + +#include <qcstring.h> + +namespace GpgME { + class Error; + class Context; +} + +namespace Kleo { + + class QGpgMEVerifyDetachedJob : public VerifyDetachedJob, private QGpgMEJob { + Q_OBJECT QGPGME_JOB + public: + QGpgMEVerifyDetachedJob( GpgME::Context * context ); + ~QGpgMEVerifyDetachedJob(); + + /*! \reimp from VerifyDetachedJob */ + GpgME::Error start( const QByteArray & signature, + const QByteArray & signedData ); + + /*! \reimp from VerifyDetachedJob */ + GpgME::VerificationResult exec( const QByteArray & signature, + const QByteArray & signedData ); + + private slots: + void slotOperationDoneEvent( GpgME::Context * context, const GpgME::Error & e ) { + QGpgMEJob::doSlotOperationDoneEvent( context, e ); + } + + private: + void doOperationDoneEvent( const GpgME::Error & e ); + void setup( const QByteArray &, const QByteArray & ); + }; + +} + +#endif // __KLEO_QGPGMEVERIFYDETACHEDJOB_H__ diff --git a/certmanager/lib/backends/qgpgme/qgpgmeverifyopaquejob.cpp b/certmanager/lib/backends/qgpgme/qgpgmeverifyopaquejob.cpp new file mode 100644 index 000000000..15f075963 --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmeverifyopaquejob.cpp @@ -0,0 +1,91 @@ +/* + qgpgmeverifyopaquejob.cpp + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "qgpgmeverifyopaquejob.h" + +#include <qgpgme/eventloopinteractor.h> +#include <qgpgme/dataprovider.h> + +#include <gpgmepp/context.h> +#include <gpgmepp/verificationresult.h> +#include <gpgmepp/data.h> + +#include <assert.h> + +Kleo::QGpgMEVerifyOpaqueJob::QGpgMEVerifyOpaqueJob( GpgME::Context * context ) + : VerifyOpaqueJob( QGpgME::EventLoopInteractor::instance(), "Kleo::QGpgMEVerifyOpaqueJob" ), + QGpgMEJob( this, context ) +{ + assert( context ); +} + +Kleo::QGpgMEVerifyOpaqueJob::~QGpgMEVerifyOpaqueJob() { +} + +void Kleo::QGpgMEVerifyOpaqueJob::setup( const QByteArray & signedData ) { + assert( !mInData ); + assert( !mOutData ); + + createInData( signedData ); + createOutData(); +} + +GpgME::Error Kleo::QGpgMEVerifyOpaqueJob::start( const QByteArray & signedData ) { + setup( signedData ); + + hookupContextToEventLoopInteractor(); + + const GpgME::Error err = mCtx->startOpaqueSignatureVerification( *mInData, *mOutData ); + + if ( err ) + deleteLater(); + return err; +} + +GpgME::VerificationResult Kleo::QGpgMEVerifyOpaqueJob::exec( const QByteArray & signedData, QByteArray & plainText ) { + setup( signedData ); + const GpgME::VerificationResult res = mCtx->verifyOpaqueSignature( *mInData, *mOutData ); + plainText = mOutDataDataProvider->data(); + getAuditLog(); + return res; +} + +void Kleo::QGpgMEVerifyOpaqueJob::doOperationDoneEvent( const GpgME::Error & ) { + emit result( mCtx->verificationResult(), mOutDataDataProvider->data() ); +} + + +#include "qgpgmeverifyopaquejob.moc" diff --git a/certmanager/lib/backends/qgpgme/qgpgmeverifyopaquejob.h b/certmanager/lib/backends/qgpgme/qgpgmeverifyopaquejob.h new file mode 100644 index 000000000..2c167fb1d --- /dev/null +++ b/certmanager/lib/backends/qgpgme/qgpgmeverifyopaquejob.h @@ -0,0 +1,73 @@ +/* + qgpgmeverifyopaquejob.h + + This file is part of libkleopatra, the KDE keymanagement library + Copyright (c) 2004 Klarälvdalens 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_QGPGMEVERIFYOPAQUEJOB_H__ +#define __KLEO_QGPGMEVERIFYOPAQUEJOB_H__ + +#include <kleo/verifyopaquejob.h> + +#include "qgpgmejob.h" + +#include <qcstring.h> + +namespace GpgME { + class Error; + class Context; +} + +namespace Kleo { + + class QGpgMEVerifyOpaqueJob : public VerifyOpaqueJob, private QGpgMEJob { + Q_OBJECT QGPGME_JOB + public: + QGpgMEVerifyOpaqueJob( GpgME::Context * context ); + ~QGpgMEVerifyOpaqueJob(); + + /*! \reimp from VerifyOpaqueJob */ + GpgME::Error start( const QByteArray & signedData ); + + /*! \reimp form VerifyOpaqueJob */ + GpgME::VerificationResult exec( const QByteArray & signedData, QByteArray & plainData ); + + private slots: + void slotOperationDoneEvent( GpgME::Context * context, const GpgME::Error & e ) { + QGpgMEJob::doSlotOperationDoneEvent( context, e ); + } + + private: + void doOperationDoneEvent( const GpgME::Error & e ); + void setup( const QByteArray & ); + }; + +} + +#endif // __KLEO_QGPGMEVERIFYOPAQUEJOB_H__ |