qgpgmebackend.cpp
1 /*
2  qgpgmebackend.cpp
3 
4  This file is part of libkleopatra, the KDE keymanagement library
5  Copyright (c) 2004,2005 Klarälvdalens Datakonsult AB
6 
7  Libkleopatra is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of the
10  License, or (at your option) any later version.
11 
12  Libkleopatra is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the TQt library by Trolltech AS, Norway (or with modified versions
24  of TQt that use the same license as TQt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  TQt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36 
37 #include "qgpgmebackend.h"
38 
39 #include "qgpgmecryptoconfig.h"
40 #include "cryptplugwrapper.h"
41 
42 #include <gpgmepp/context.h>
43 #include <gpgmepp/engineinfo.h>
44 
45 #include <tdelocale.h>
46 #include <kstandarddirs.h>
47 
48 #include <tqfile.h>
49 #include <tqstring.h>
50 
51 Kleo::QGpgMEBackend::QGpgMEBackend()
52  : Kleo::CryptoBackend(),
53  mCryptoConfig( 0 ),
54  mOpenPGPProtocol( 0 ),
55  mSMIMEProtocol( 0 )
56 {
57  GpgME::initializeLibrary();
58 }
59 
60 Kleo::QGpgMEBackend::~QGpgMEBackend() {
61  delete mCryptoConfig; mCryptoConfig = 0;
62  delete mOpenPGPProtocol; mOpenPGPProtocol = 0;
63  delete mSMIMEProtocol; mSMIMEProtocol = 0;
64 }
65 
66 TQString Kleo::QGpgMEBackend::name() const {
67  return "gpgme";
68 }
69 
70 TQString Kleo::QGpgMEBackend::displayName() const {
71  return i18n( "GpgME" );
72 }
73 
74 Kleo::CryptoConfig * Kleo::QGpgMEBackend::config() const {
75  if ( !mCryptoConfig ) {
76  static bool hasGpgConf = !TDEStandardDirs::findExe( "gpgconf" ).isEmpty();
77  if ( hasGpgConf )
78  mCryptoConfig = new QGpgMECryptoConfig();
79  }
80  return mCryptoConfig;
81 }
82 
83 static bool check( GpgME::Context::Protocol proto, TQString * reason ) {
84  if ( !GpgME::checkEngine( proto ) )
85  return true;
86  if ( !reason )
87  return false;
88  // error, check why:
89  const GpgME::EngineInfo ei = GpgME::engineInfo( proto );
90  if ( ei.isNull() )
91  *reason = i18n("GPGME was compiled without support for %1.").arg( proto == GpgME::Context::CMS ? "S/MIME" : "OpenPGP" );
92  else if ( ei.fileName() && !ei.version() )
93  *reason = i18n("Engine %1 is not installed properly.").arg( TQFile::decodeName( ei.fileName() ) );
94  else if ( ei.fileName() && ei.version() && ei.requiredVersion() )
95  *reason = i18n("Engine %1 version %2 installed, "
96  "but at least version %3 is required.")
97  .arg( TQFile::decodeName( ei.fileName() ), ei.version(), ei.requiredVersion() );
98  else
99  *reason = i18n("Unknown problem with engine for protocol %1.").arg( proto == GpgME::Context::CMS ? "S/MIME" : "OpenPGP" );
100  return false;
101 }
102 
103 bool Kleo::QGpgMEBackend::checkForOpenPGP( TQString * reason ) const {
104  return check( GpgME::Context::OpenPGP, reason );
105 }
106 
107 bool Kleo::QGpgMEBackend::checkForSMIME( TQString * reason ) const {
108  return check( GpgME::Context::CMS, reason );
109 }
110 
111 bool Kleo::QGpgMEBackend::checkForProtocol( const char * name, TQString * reason ) const {
112  if ( tqstricmp( name, OpenPGP ) == 0 )
113  return check( GpgME::Context::OpenPGP, reason );
114  if ( tqstricmp( name, SMIME ) == 0 )
115  return check( GpgME::Context::CMS, reason );
116  if ( reason )
117  *reason = i18n( "Unsupported protocol \"%1\"" ).arg( name );
118  return false;
119 }
120 
121 Kleo::CryptoBackend::Protocol * Kleo::QGpgMEBackend::openpgp() const {
122  if ( !mOpenPGPProtocol )
123  if ( checkForOpenPGP() )
124  mOpenPGPProtocol = new CryptPlugWrapper( "gpg", "openpgp" );
125  return mOpenPGPProtocol;
126 }
127 
128 Kleo::CryptoBackend::Protocol * Kleo::QGpgMEBackend::smime() const {
129  if ( !mSMIMEProtocol )
130  if ( checkForSMIME() )
131  mSMIMEProtocol = new CryptPlugWrapper( "gpgsm", "smime" );
132  return mSMIMEProtocol;
133 }
134 
135 Kleo::CryptoBackend::Protocol * Kleo::QGpgMEBackend::protocol( const char * name ) const {
136  if ( tqstricmp( name, OpenPGP ) == 0 )
137  return openpgp();
138  if ( tqstricmp( name, SMIME ) == 0 )
139  return smime();
140  return 0;
141 }
142 
143 bool Kleo::QGpgMEBackend::supportsProtocol( const char * name ) const {
144  return tqstricmp( name, OpenPGP ) == 0 || tqstricmp( name, SMIME ) == 0;
145 }
146 
147 const char * Kleo::QGpgMEBackend::enumerateProtocols( int i ) const {
148  switch ( i ) {
149  case 0: return OpenPGP;
150  case 1: return SMIME;
151  default: return 0;
152  }
153 }
This class provides C++ access to the CRYPTPLUG API.
Main interface to crypto configuration.
Definition: cryptoconfig.h:334
CryptoConfig implementation around the gpgconf command-line tool For method docu, see kleo/cryptoconf...
C++ wrapper for the CRYPTPLUG library API.