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

libkleo

  • sources
  • kde-4.14
  • kdepim
  • libkleo
  • backends
  • qgpgme
qgpgmekeylistjob.cpp
Go to the documentation of this file.
1 /*
2  qgpgmekeylistjob.cpp
3 
4  This file is part of libkleopatra, the KDE keymanagement library
5  Copyright (c) 2004,2008 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 "qgpgmekeylistjob.h"
34 
35 #include <gpgme++/key.h>
36 #include <gpgme++/context.h>
37 #include <gpgme++/keylistresult.h>
38 #include <gpg-error.h>
39 
40 #include <kmessagebox.h>
41 #include <klocale.h>
42 #include <kdebug.h>
43 
44 #include <QStringList>
45 
46 #include <algorithm>
47 
48 #include <cstdlib>
49 #include <cstring>
50 #include <cassert>
51 
52 using namespace Kleo;
53 using namespace GpgME;
54 using namespace boost;
55 
56 QGpgMEKeyListJob::QGpgMEKeyListJob( Context * context )
57  : mixin_type( context ),
58  mResult(), mSecretOnly( false )
59 {
60  lateInitialization();
61 }
62 
63 QGpgMEKeyListJob::~QGpgMEKeyListJob() {}
64 
65 static KeyListResult do_list_keys( Context * ctx, const QStringList & pats, std::vector<Key> & keys, bool secretOnly ) {
66 
67  const _detail::PatternConverter pc( pats );
68 
69  if ( const Error err = ctx->startKeyListing( pc.patterns(), secretOnly ) )
70  return KeyListResult( 0, err );
71 
72  Error err;
73  do
74  keys.push_back( ctx->nextKey( err ) );
75  while ( !err );
76 
77  keys.pop_back();
78 
79  const KeyListResult result = ctx->endKeyListing();
80  ctx->cancelPendingOperation();
81  return result;
82 }
83 
84 static QGpgMEKeyListJob::result_type list_keys( Context * ctx, QStringList pats, bool secretOnly ) {
85  if ( pats.size() < 2 ) {
86  std::vector<Key> keys;
87  const KeyListResult r = do_list_keys( ctx, pats, keys, secretOnly );
88  return boost::make_tuple( r, keys, QString(), Error() );
89  }
90 
91  // The communication channel between gpgme and gpgsm is limited in
92  // the number of patterns that can be transported, but they won't
93  // say to how much, so we need to find out ourselves if we get a
94  // LINE_TOO_LONG error back...
95 
96  // We could of course just feed them single patterns, and that would
97  // probably be easier, but the performance penalty would currently
98  // be noticeable.
99 
100  unsigned int chunkSize = pats.size();
101 retry:
102  std::vector<Key> keys;
103  keys.reserve( pats.size() );
104  KeyListResult result;
105  do {
106  const KeyListResult this_result = do_list_keys( ctx, pats.mid( 0, chunkSize ), keys, secretOnly );
107  if ( this_result.error().code() == GPG_ERR_LINE_TOO_LONG ) {
108  // got LINE_TOO_LONG, try a smaller chunksize:
109  chunkSize /= 2;
110  if ( chunkSize < 1 )
111  // chunks smaller than one can't be -> return the error.
112  return boost::make_tuple( this_result, keys, QString(), Error() );
113  else
114  goto retry;
115  } else if ( this_result.error().code() == GPG_ERR_EOF ) {
116  // early end of keylisting (can happen when ~/.gnupg doesn't
117  // exist). Fakeing an empty result:
118  return boost::make_tuple( KeyListResult(), std::vector<Key>(), QString(), Error() );
119  }
120  // ok, that seemed to work...
121  result.mergeWith( this_result );
122  if ( result.error().code() )
123  break;
124  pats = pats.mid( chunkSize );
125  } while ( !pats.empty() );
126  return boost::make_tuple( result, keys, QString(), Error() );
127 }
128 
129 Error QGpgMEKeyListJob::start( const QStringList & patterns, bool secretOnly ) {
130  mSecretOnly = secretOnly;
131  run( boost::bind( &list_keys, _1, patterns, secretOnly ) );
132  return Error();
133 }
134 
135 KeyListResult QGpgMEKeyListJob::exec( const QStringList & patterns, bool secretOnly, std::vector<Key> & keys ) {
136  mSecretOnly = secretOnly;
137  const result_type r = list_keys( context(), patterns, secretOnly );
138  resultHook( r );
139  keys = get<1>( r );
140  return get<0>( r );
141 }
142 
143 void QGpgMEKeyListJob::resultHook( const result_type & tuple ) {
144  mResult = get<0>( tuple );
145  Q_FOREACH( const Key & key, get<1>( tuple ) )
146  emit nextKey( key );
147 }
148 
149 void QGpgMEKeyListJob::showErrorDialog( QWidget * parent, const QString & caption ) const {
150  if ( !mResult.error() || mResult.error().isCanceled() )
151  return;
152  const QString msg = i18n( "<qt><p>An error occurred while fetching "
153  "the keys from the backend:</p>"
154  "<p><b>%1</b></p></qt>" ,
155  QString::fromLocal8Bit( mResult.error().asString() ) );
156  KMessageBox::error( parent, msg, caption );
157 }
158 
Kleo::QGpgMEKeyListJob::resultHook
void resultHook(const result_type &result)
Definition: qgpgmekeylistjob.cpp:143
Kleo::QGpgMEKeyListJob::showErrorDialog
void showErrorDialog(QWidget *parent, const QString &caption) const
Definition: qgpgmekeylistjob.cpp:149
QWidget
Kleo::QGpgMEKeyListJob::start
GpgME::Error start(const QStringList &patterns, bool secretOnly)
Definition: qgpgmekeylistjob.cpp:129
qgpgmekeylistjob.h
Kleo::_detail::PatternConverter
Definition: threadedjobmixin.h:64
Kleo::_detail::ThreadedJobMixin< KeyListJob, boost::tuple< GpgME::KeyListResult, std::vector< GpgME::Key >, QString, GpgME::Error > >::context
GpgME::Context * context() const
Definition: threadedjobmixin.h:178
QList::size
int size() const
QString::fromLocal8Bit
QString fromLocal8Bit(const char *str, int size)
QList::empty
bool empty() const
Kleo::QGpgMEKeyListJob::QGpgMEKeyListJob
QGpgMEKeyListJob(GpgME::Context *context)
Definition: qgpgmekeylistjob.cpp:56
Kleo::_detail::ThreadedJobMixin< KeyListJob, boost::tuple< GpgME::KeyListResult, std::vector< GpgME::Key >, QString, GpgME::Error > >::lateInitialization
void lateInitialization()
Definition: threadedjobmixin.h:146
Kleo::_detail::PatternConverter::patterns
const char ** patterns() const
Definition: threadedjobmixin.cpp:89
Kleo::QGpgMEKeyListJob::~QGpgMEKeyListJob
~QGpgMEKeyListJob()
Definition: qgpgmekeylistjob.cpp:63
do_list_keys
static KeyListResult do_list_keys(Context *ctx, const QStringList &pats, std::vector< Key > &keys, bool secretOnly)
Definition: qgpgmekeylistjob.cpp:65
QString
QStringList
Kleo::_detail::ThreadedJobMixin< KeyListJob, boost::tuple< GpgME::KeyListResult, std::vector< GpgME::Key >, QString, GpgME::Error > >::result_type
boost::tuple< GpgME::KeyListResult, std::vector< GpgME::Key >, QString, GpgME::Error > result_type
Definition: threadedjobmixin.h:117
Kleo::_detail::ThreadedJobMixin< KeyListJob, boost::tuple< GpgME::KeyListResult, std::vector< GpgME::Key >, QString, GpgME::Error > >::run
void run(const T_binder &func)
Definition: threadedjobmixin.h:153
QList::mid
QList< T > mid(int pos, int length) const
Kleo::QGpgMEKeyListJob::exec
GpgME::KeyListResult exec(const QStringList &patterns, bool secretOnly, std::vector< GpgME::Key > &keys)
Definition: qgpgmekeylistjob.cpp:135
list_keys
static QGpgMEKeyListJob::result_type list_keys(Context *ctx, QStringList pats, bool secretOnly)
Definition: qgpgmekeylistjob.cpp:84
Kleo::KeyListJob::nextKey
void nextKey(const GpgME::Key &key)
Kleo::_detail::ThreadedJobMixin< KeyListJob, boost::tuple< GpgME::KeyListResult, std::vector< GpgME::Key >, QString, GpgME::Error > >
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:38 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
  • pimprint

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