• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

libkleo

  • sources
  • kde-4.12
  • kdepim
  • libkleo
  • kleo
cryptobackendfactory.cpp
Go to the documentation of this file.
1 /*
2  cryptobackendfactory.cpp
3 
4  This file is part of libkleopatra, the KDE key management library
5  Copyright (c) 2001,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 Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), 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  Qt. 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 #include "cryptobackendfactory.h"
34 
35 #include "libkleo/backends/qgpgme/qgpgmebackend.h"
36 #if 0 // disabled for kde-3.3
37 #include "libkleo/backends/kpgp/pgp2backend.h"
38 #include "libkleo/backends/kpgp/pgp5backend.h"
39 #include "libkleo/backends/kpgp/pgp6backend.h"
40 #include "libkleo/backends/kpgp/gpg1backend.h"
41 #endif
42 
43 #ifndef KDEPIM_ONLY_KLEO
44 # include "libkleo/backends/chiasmus/chiasmusbackend.h"
45 #endif
46 
47 #include <kconfig.h>
48 #include <klocale.h>
49 #include <kdebug.h>
50 #include <kmessagebox.h>
51 #include <kconfiggroup.h>
52 
53 #include <QApplication>
54 #include <iterator>
55 #include <algorithm>
56 
57 #include <cassert>
58 
59 Kleo::CryptoBackendFactory * Kleo::CryptoBackendFactory::mSelf = 0;
60 
61 static const char * availableProtocols[] = {
62 #ifndef KDEPIM_ONLY_KLEO
63  "Chiasmus",
64 #endif
65  "OpenPGP", "SMIME",
66 };
67 static const unsigned int numAvailableProtocols = sizeof availableProtocols / sizeof *availableProtocols;
68 
69 Kleo::CryptoBackendFactory::CryptoBackendFactory()
70  : QObject( qApp ),
71  mConfigObject( 0 ),
72  mAvailableProtocols( availableProtocols, availableProtocols + numAvailableProtocols )
73 {
74  setObjectName(QLatin1String("CryptoBackendFactory::instance()"));
75  mBackendList.push_back( new QGpgMEBackend() );
76 #if 0 // disabled for kde-3.3
77  mBackendList.push_back( new PGP2Backend() );
78  mBackendList.push_back( new PGP5Backend() );
79  mBackendList.push_back( new PGP6Backend() );
80  mBackendList.push_back( new GPG1Backend() );
81 #endif
82 #ifndef KDEPIM_ONLY_KLEO
83  mBackendList.push_back( new ChiasmusBackend() );
84 #endif
85  scanForBackends();
86  readConfig();
87 
88  mSelf = this; // last!
89 }
90 
91 Kleo::CryptoBackendFactory::~CryptoBackendFactory() {
92  mSelf = 0; // first!
93 
94  for ( std::vector<CryptoBackend*>::iterator it = mBackendList.begin() ; it != mBackendList.end() ; ++it ) {
95  delete *it;
96  *it = 0;
97  }
98  delete mConfigObject;
99  mConfigObject = 0;
100 }
101 
102 Kleo::CryptoBackendFactory * Kleo::CryptoBackendFactory::instance() {
103  if ( !mSelf )
104  mSelf = new CryptoBackendFactory();
105  return mSelf;
106 }
107 
108 
109 // const Kleo::CryptoBackend* Kleo::CryptoBackendFactory::smimeBackend() const {
110 // return mSMIMEBackend;
111 // }
112 
113 // const Kleo::CryptoBackend* Kleo::CryptoBackendFactory::openpgpBackend() const {
114 // return mOpenPGPBackend;
115 // }
116 
117 const Kleo::CryptoBackend::Protocol * Kleo::CryptoBackendFactory::smime() const {
118  const BackendMap::const_iterator it = mBackends.find( "SMIME" );
119  if ( it == mBackends.end() )
120  return 0;
121  if ( !it->second )
122  return 0;
123  return it->second->smime();
124 }
125 
126 const Kleo::CryptoBackend::Protocol * Kleo::CryptoBackendFactory::openpgp() const {
127  const BackendMap::const_iterator it = mBackends.find( "OpenPGP" );
128  if ( it == mBackends.end() )
129  return 0;
130  if ( !it->second )
131  return 0;
132  return it->second->openpgp();
133 }
134 
135 const Kleo::CryptoBackend::Protocol * Kleo::CryptoBackendFactory::protocol( const char * name ) const {
136  const BackendMap::const_iterator it = mBackends.find( name );
137  if ( it == mBackends.end() )
138  return 0;
139  if ( !it->second )
140  return 0;
141  return it->second->protocol( name );
142 }
143 
144 const Kleo::CryptoBackend::Protocol * Kleo::CryptoBackendFactory::protocol( GpgME::Protocol proto ) const {
145  if ( proto == GpgME::OpenPGP )
146  return openpgp();
147  else if ( proto == GpgME::CMS )
148  return smime();
149  else
150  return 0;
151 }
152 
153 Kleo::CryptoConfig * Kleo::CryptoBackendFactory::config() const {
154  // ## should we use mSMIMEBackend? mOpenPGPBackend? backend(0) i.e. always qgpgme?
155  return backend( 0 ) ? backend( 0 )->config() : 0;
156 }
157 
158 bool Kleo::CryptoBackendFactory::hasBackends() const {
159  return !mBackendList.empty();
160 }
161 
162 void Kleo::CryptoBackendFactory::scanForBackends( QStringList * reasons ) {
163  for ( std::vector<CryptoBackend*>::const_iterator it = mBackendList.begin() ; it != mBackendList.end() ; ++it ) {
164  assert( *it );
165  for ( int i = 0 ;; ++i ) {
166  const char * protocol = (*it)->enumerateProtocols( i );
167  if ( !protocol )
168  break;
169  QString reason;
170  if ( (*it)->supportsProtocol( protocol ) && !(*it)->checkForProtocol( protocol, &reason ) ) {
171  if ( reasons ) {
172  reasons->push_back( i18n("While scanning for %1 support in backend %2:",
173  QLatin1String(protocol), (*it)->displayName() ) );
174  reasons->push_back( QLatin1String(" ") + reason );
175  }
176  }
177  }
178  }
179 }
180 
181 const Kleo::CryptoBackend * Kleo::CryptoBackendFactory::backend( unsigned int idx ) const {
182  return ( idx < mBackendList.size() ) ? mBackendList[idx] : 0 ;
183 }
184 
185 const Kleo::CryptoBackend * Kleo::CryptoBackendFactory::backendByName( const QString& name ) const {
186  for ( std::vector<CryptoBackend*>::const_iterator it = mBackendList.begin() ; it != mBackendList.end() ; ++it ) {
187  if ( (*it)->name() == name )
188  return *it;
189  }
190  return 0;
191 }
192 
193 KConfig* Kleo::CryptoBackendFactory::configObject() const {
194  if ( !mConfigObject )
195  // this is unsafe. We're a lib, used by concurrent apps.
196  mConfigObject = new KConfig( QLatin1String("libkleopatrarc") );
197  return mConfigObject;
198 }
199 
200 void Kleo::CryptoBackendFactory::setSMIMEBackend( const CryptoBackend* backend ) {
201  setProtocolBackend( "SMIME", backend );
202 }
203 
204 void Kleo::CryptoBackendFactory::setOpenPGPBackend( const CryptoBackend* backend ) {
205  setProtocolBackend( "OpenPGP", backend );
206 }
207 
208 void Kleo::CryptoBackendFactory::setProtocolBackend( const char * protocol, const CryptoBackend * backend ) {
209  const QString name = backend ? backend->name() : QString() ;
210  KConfigGroup group( configObject(), "Backends" );
211  group.writeEntry( protocol, name );
212  configObject()->sync();
213  mBackends[protocol] = backend;
214 }
215 
216 static const char * defaultBackend( const char * proto ) {
217  static const struct {
218  const char * proto;
219  const char * backend;
220  } defaults[] = {
221  { "OpenPGP", "gpgme" },
222  { "SMIME", "gpgme" },
223 #ifndef KDEPIM_ONLY_KLEO
224  { "Chiasmus", "chiasmus" },
225 #endif
226  };
227  for ( unsigned int i = 0 ; i < sizeof defaults / sizeof *defaults ; ++i )
228  if ( qstricmp( proto, defaults[i].proto ) == 0 )
229  return defaults[i].backend;
230  return 0;
231 }
232 
233 void Kleo::CryptoBackendFactory::readConfig() {
234  mBackends.clear();
235  const KConfigGroup group( configObject(), "Backends" );
236  for ( ProtocolSet::const_iterator it = mAvailableProtocols.begin(), end = mAvailableProtocols.end() ; it != end ; ++it ) {
237  const QString backend = group.readEntry( *it );
238  mBackends[*it] = backendByName( backend.isEmpty() ? QString::fromLatin1( defaultBackend( *it ) ) : backend );
239  }
240 }
241 
242 const char * Kleo::CryptoBackendFactory::enumerateProtocols( int i ) const {
243  if ( i < 0 || static_cast<unsigned int>( i ) >= mAvailableProtocols.size() )
244  return 0;
245  return mAvailableProtocols[i];
246 }
247 
248 namespace {
249  class CaseInsensitiveString {
250  const char * m;
251  public:
252  CaseInsensitiveString( const char * s ) : m( s ) {}
253 #define make_operator( op ) \
254  bool operator op( const CaseInsensitiveString & other ) const { \
255  return qstricmp( m, other.m ) op 0; \
256  } \
257  bool operator op( const char * other ) const { \
258  return qstricmp( m, other ) op 0; \
259  }
260  make_operator( == )
261  make_operator( != )
262  make_operator( < )
263  make_operator( > )
264  make_operator( <= )
265  make_operator( >= )
266 #undef make_operator
267  operator const char *() const { return m; }
268  };
269 #define make_ext_operator( op, inv_op ) \
270  inline bool operator op( const char * lhs, const CaseInsensitiveString & rhs ) { \
271  return rhs.operator inv_op( lhs ); \
272  }
273  make_ext_operator( ==, == )
274  make_ext_operator( !=, != )
275  make_ext_operator( <, > )
276  make_ext_operator( >, < )
277  make_ext_operator( <=, >= )
278  make_ext_operator( >=, <= )
279 #undef make_ext_operator
280 
281 }
282 
283 bool Kleo::CryptoBackendFactory::knowsAboutProtocol( const char * name ) const {
284  return std::find( mAvailableProtocols.begin(), mAvailableProtocols.end(),
285  CaseInsensitiveString( name ) ) != mAvailableProtocols.end();
286 }
287 
288 #include "cryptobackendfactory.moc"
defaultBackend
static const char * defaultBackend(const char *proto)
Definition: cryptobackendfactory.cpp:216
Kleo::CryptoBackendFactory::scanForBackends
void scanForBackends(QStringList *reasons=0)
Definition: cryptobackendfactory.cpp:162
name
const char * name
Definition: kconfigbasedkeyfilter.cpp:124
Kleo::CryptoBackendFactory::config
CryptoConfig * config() const
Definition: cryptobackendfactory.cpp:153
Kleo::CryptoBackendFactory::mBackendList
std::vector< CryptoBackend * > mBackendList
Definition: cryptobackendfactory.h:96
Kleo::CryptoBackendFactory::smime
const CryptoBackend::Protocol * smime() const
Definition: cryptobackendfactory.cpp:117
Kleo::QGpgMEBackend
Definition: qgpgmebackend.h:47
availableProtocols
static const char * availableProtocols[]
Definition: cryptobackendfactory.cpp:61
Kleo::CryptoBackendFactory::setProtocolBackend
void setProtocolBackend(const char *name, const CryptoBackend *backend)
Definition: cryptobackendfactory.cpp:208
QString
QObject
Kleo::CryptoBackendFactory::instance
static CryptoBackendFactory * instance()
Definition: cryptobackendfactory.cpp:102
Kleo::CryptoBackendFactory::backend
const CryptoBackend * backend(unsigned int idx) const
Definition: cryptobackendfactory.cpp:181
Kleo::CryptoBackend
Definition: cryptobackend.h:70
Kleo::CryptoBackendFactory::setSMIMEBackend
void setSMIMEBackend(const CryptoBackend *backend)
Definition: cryptobackendfactory.cpp:200
Kleo::CryptoBackendFactory::configObject
KConfig * configObject() const
Definition: cryptobackendfactory.cpp:193
cryptobackendfactory.h
Kleo::CryptoBackendFactory::protocol
const CryptoBackend::Protocol * protocol(const char *name) const
Definition: cryptobackendfactory.cpp:135
Kleo::CryptoBackendFactory::enumerateProtocols
const char * enumerateProtocols(int i) const
Definition: cryptobackendfactory.cpp:242
Kleo::CryptoBackendFactory::CryptoBackendFactory
CryptoBackendFactory()
Definition: cryptobackendfactory.cpp:69
Kleo::CryptoBackendFactory::~CryptoBackendFactory
~CryptoBackendFactory()
Definition: cryptobackendfactory.cpp:91
Kleo::CryptoBackend::name
virtual QString name() const =0
Kleo::CryptoBackendFactory::knowsAboutProtocol
bool knowsAboutProtocol(const char *name) const
Definition: cryptobackendfactory.cpp:283
make_operator
#define make_operator(op)
Definition: cryptobackendfactory.cpp:253
Kleo::CryptoBackendFactory::hasBackends
bool hasBackends() const
Definition: cryptobackendfactory.cpp:158
Kleo::ChiasmusBackend
Definition: chiasmusbackend.h:47
kdtools::find
boost::range_iterator< C >::type find(C &c, const V &v)
Definition: stl_util.h:322
make_ext_operator
#define make_ext_operator(op, inv_op)
Definition: cryptobackendfactory.cpp:269
chiasmusbackend.h
numAvailableProtocols
static const unsigned int numAvailableProtocols
Definition: cryptobackendfactory.cpp:67
qgpgmebackend.h
Kleo::CryptoBackendFactory
Definition: cryptobackendfactory.h:57
Kleo::CryptoBackendFactory::setOpenPGPBackend
void setOpenPGPBackend(const CryptoBackend *backend)
Definition: cryptobackendfactory.cpp:204
Kleo::CryptoBackend::Protocol
Definition: cryptobackend.h:99
Kleo::CryptoBackendFactory::openpgp
const CryptoBackend::Protocol * openpgp() const
Definition: cryptobackendfactory.cpp:126
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:57:48 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkleo

Skip menu "libkleo"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal