certmanager/lib

qgpgmebackend.cpp

00001 /*  -*- mode: C++; c-file-style: "gnu" -*-
00002     qgpgmebackend.cpp
00003 
00004     This file is part of libkleopatra, the KDE keymanagement library
00005     Copyright (c) 2004,2005 Klarälvdalens Datakonsult AB
00006 
00007     Libkleopatra is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU General Public License as
00009     published by the Free Software Foundation; either version 2 of the
00010     License, or (at your option) any later version.
00011 
00012     Libkleopatra is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021     In addition, as a special exception, the copyright holders give
00022     permission to link the code of this program with any edition of
00023     the Qt library by Trolltech AS, Norway (or with modified versions
00024     of Qt that use the same license as Qt), and distribute linked
00025     combinations including the two.  You must obey the GNU General
00026     Public License in all respects for all of the code used other than
00027     Qt.  If you modify this file, you may extend this exception to
00028     your version of the file, but you are not obligated to do so.  If
00029     you do not wish to do so, delete this exception statement from
00030     your version.
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   // error, check why:
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 }
KDE Home | KDE Accessibility Home | Description of Access Keys