certmanager/lib
qgpgmebackend.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifdef HAVE_CONFIG_H
00034 #include <config.h>
00035 #endif
00036
00037 #include "qgpgmebackend.h"
00038
00039 #include "qgpgmecryptoconfig.h"
00040 #include "cryptplugwrapper.h"
00041
00042 #include <gpgmepp/context.h>
00043 #include <gpgmepp/engineinfo.h>
00044
00045 #include <klocale.h>
00046 #include <kstandarddirs.h>
00047
00048 #include <qfile.h>
00049 #include <qstring.h>
00050
00051 Kleo::QGpgMEBackend::QGpgMEBackend()
00052 : Kleo::CryptoBackend(),
00053 mCryptoConfig( 0 ),
00054 mOpenPGPProtocol( 0 ),
00055 mSMIMEProtocol( 0 )
00056 {
00057
00058 }
00059
00060 Kleo::QGpgMEBackend::~QGpgMEBackend() {
00061 delete mCryptoConfig; mCryptoConfig = 0;
00062 delete mOpenPGPProtocol; mOpenPGPProtocol = 0;
00063 delete mSMIMEProtocol; mSMIMEProtocol = 0;
00064 }
00065
00066 QString Kleo::QGpgMEBackend::name() const {
00067 return "gpgme";
00068 }
00069
00070 QString Kleo::QGpgMEBackend::displayName() const {
00071 return i18n( "GpgME" );
00072 }
00073
00074 Kleo::CryptoConfig * Kleo::QGpgMEBackend::config() const {
00075 if ( !mCryptoConfig ) {
00076 static bool hasGpgConf = !KStandardDirs::findExe( "gpgconf" ).isEmpty();
00077 if ( hasGpgConf )
00078 mCryptoConfig = new QGpgMECryptoConfig();
00079 }
00080 return mCryptoConfig;
00081 }
00082
00083 static bool check( GpgME::Context::Protocol proto, QString * reason ) {
00084 if ( !GpgME::checkEngine( proto ) )
00085 return true;
00086 if ( !reason )
00087 return false;
00088
00089 const GpgME::EngineInfo ei = GpgME::engineInfo( proto );
00090 if ( ei.isNull() )
00091 *reason = i18n("GPGME was compiled without support for %1.").arg( proto == GpgME::Context::CMS ? "S/MIME" : "OpenPGP" );
00092 else if ( ei.fileName() && !ei.version() )
00093 *reason = i18n("Engine %1 is not installed properly.").arg( QFile::decodeName( ei.fileName() ) );
00094 else if ( ei.fileName() && ei.version() && ei.requiredVersion() )
00095 *reason = i18n("Engine %1 version %2 installed, "
00096 "but at least version %3 is required.")
00097 .arg( QFile::decodeName( ei.fileName() ), ei.version(), ei.requiredVersion() );
00098 else
00099 *reason = i18n("Unknown problem with engine for protocol %1.").arg( proto == GpgME::Context::CMS ? "S/MIME" : "OpenPGP" );
00100 return false;
00101 }
00102
00103 bool Kleo::QGpgMEBackend::checkForOpenPGP( QString * reason ) const {
00104 return check( GpgME::Context::OpenPGP, reason );
00105 }
00106
00107 bool Kleo::QGpgMEBackend::checkForSMIME( QString * reason ) const {
00108 return check( GpgME::Context::CMS, reason );
00109 }
00110
00111 bool Kleo::QGpgMEBackend::checkForProtocol( const char * name, QString * reason ) const {
00112 if ( qstricmp( name, OpenPGP ) == 0 )
00113 return check( GpgME::Context::OpenPGP, reason );
00114 if ( qstricmp( name, SMIME ) == 0 )
00115 return check( GpgME::Context::CMS, reason );
00116 if ( reason )
00117 *reason = i18n( "Unsupported protocol \"%1\"" ).arg( name );
00118 return false;
00119 }
00120
00121 Kleo::CryptoBackend::Protocol * Kleo::QGpgMEBackend::openpgp() const {
00122 if ( !mOpenPGPProtocol )
00123 if ( checkForOpenPGP() )
00124 mOpenPGPProtocol = new CryptPlugWrapper( "gpg", "openpgp" );
00125 return mOpenPGPProtocol;
00126 }
00127
00128 Kleo::CryptoBackend::Protocol * Kleo::QGpgMEBackend::smime() const {
00129 if ( !mSMIMEProtocol )
00130 if ( checkForSMIME() )
00131 mSMIMEProtocol = new CryptPlugWrapper( "gpgsm", "smime" );
00132 return mSMIMEProtocol;
00133 }
00134
00135 Kleo::CryptoBackend::Protocol * Kleo::QGpgMEBackend::protocol( const char * name ) const {
00136 if ( qstricmp( name, OpenPGP ) == 0 )
00137 return openpgp();
00138 if ( qstricmp( name, SMIME ) == 0 )
00139 return smime();
00140 return 0;
00141 }
00142
00143 bool Kleo::QGpgMEBackend::supportsProtocol( const char * name ) const {
00144 return qstricmp( name, OpenPGP ) == 0 || qstricmp( name, SMIME ) == 0;
00145 }
00146
00147 const char * Kleo::QGpgMEBackend::enumerateProtocols( int i ) const {
00148 switch ( i ) {
00149 case 0: return OpenPGP;
00150 case 1: return SMIME;
00151 default: return 0;
00152 }
00153 }
|