• 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
  • backends
  • qgpgme
qgpgmelistallkeysjob.cpp
Go to the documentation of this file.
1 /*
2  qgpgmelistallkeysjob.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 "qgpgmelistallkeysjob.h"
34 
35 #include "kleopatra/models/predicates.h"
36 
37 #include <gpgme++/key.h>
38 #include <gpgme++/context.h>
39 #include <gpgme++/keylistresult.h>
40 #include <gpg-error.h>
41 
42 #include <kmessagebox.h>
43 #include <klocale.h>
44 #include <kdebug.h>
45 
46 #include <QStringList>
47 
48 #include <algorithm>
49 
50 #include <cstdlib>
51 #include <cstring>
52 #include <cassert>
53 
54 using namespace Kleo;
55 using namespace GpgME;
56 using namespace boost;
57 
58 QGpgMEListAllKeysJob::QGpgMEListAllKeysJob( Context * context )
59  : mixin_type( context ),
60  mResult()
61 {
62  lateInitialization();
63 }
64 
65 QGpgMEListAllKeysJob::~QGpgMEListAllKeysJob() {}
66 
67 static KeyListResult do_list_keys( Context * ctx, std::vector<Key> & keys, bool secretOnly ) {
68 
69  const char ** pat = 0;
70  if ( const Error err = ctx->startKeyListing( pat, secretOnly ) )
71  return KeyListResult( 0, err );
72 
73  Error err;
74  do
75  keys.push_back( ctx->nextKey( err ) );
76  while ( !err );
77 
78  keys.pop_back();
79 
80  const KeyListResult result = ctx->endKeyListing();
81  ctx->cancelPendingOperation();
82  return result;
83 }
84 
85 namespace {
86 
87  template <typename ForwardIterator, typename BinaryPredicate>
88  ForwardIterator unique_by_merge( ForwardIterator first, ForwardIterator last, BinaryPredicate pred ) {
89  first = std::adjacent_find( first, last, pred );
90  if ( first == last )
91  return last;
92 
93  ForwardIterator dest = first;
94  dest->mergeWith( *++first );
95  while ( ++first != last )
96  if ( pred( *dest, *first ) )
97  dest->mergeWith( *first );
98  else
99  *++dest = *first;
100  return ++dest;
101  }
102 
103 }
104 
105 static void merge_keys( std::vector<Key> & merged, std::vector<Key> & pub, std::vector<Key> & sec ) {
106  merged.reserve( pub.size() + sec.size() );
107 
108  std::merge( pub.begin(), pub.end(),
109  sec.begin(), sec.end(),
110  std::back_inserter( merged ),
111  _detail::ByFingerprint<std::less>() );
112 
113  merged.erase( unique_by_merge( merged.begin(), merged.end(), _detail::ByFingerprint<std::equal_to>() ),
114  merged.end() );
115 }
116 
117 static QGpgMEListAllKeysJob::result_type list_keys( Context * ctx, bool mergeKeys ) {
118  std::vector<Key> pub, sec, merged;
119  KeyListResult r;
120 
121  r.mergeWith( do_list_keys( ctx, pub, false ) );
122  std::sort( pub.begin(), pub.end(), _detail::ByFingerprint<std::less>() );
123 
124  r.mergeWith( do_list_keys( ctx, sec, true ) );
125  std::sort( sec.begin(), sec.end(), _detail::ByFingerprint<std::less>() );
126 
127  if ( mergeKeys )
128  merge_keys( merged, pub, sec );
129  else
130  merged.swap( pub );
131  return make_tuple( r, merged, sec, QString(), Error() );
132 }
133 
134 Error QGpgMEListAllKeysJob::start( bool mergeKeys ) {
135  run( boost::bind( &list_keys, _1, mergeKeys ) );
136  return Error();
137 }
138 
139 KeyListResult QGpgMEListAllKeysJob::exec( std::vector<Key> & pub, std::vector<Key> & sec, bool mergeKeys ) {
140  const result_type r = list_keys( context(), mergeKeys );
141  resultHook( r );
142  pub = get<1>( r );
143  sec = get<2>( r );
144  return get<0>( r );
145 }
146 
147 void QGpgMEListAllKeysJob::resultHook( const result_type & tuple ) {
148  mResult = get<0>( tuple );
149 }
150 
151 void QGpgMEListAllKeysJob::showErrorDialog( QWidget * parent, const QString & caption ) const {
152  if ( !mResult.error() || mResult.error().isCanceled() )
153  return;
154  const QString msg = i18n( "<qt><p>An error occurred while fetching "
155  "the keys from the backend:</p>"
156  "<p><b>%1</b></p></qt>" ,
157  QString::fromLocal8Bit( mResult.error().asString() ) );
158  KMessageBox::error( parent, msg, caption );
159 }
160 
161 #include "qgpgmelistallkeysjob.moc"
do_list_keys
static KeyListResult do_list_keys(Context *ctx, std::vector< Key > &keys, bool secretOnly)
Definition: qgpgmelistallkeysjob.cpp:67
Kleo::QGpgMEListAllKeysJob::showErrorDialog
void showErrorDialog(QWidget *parent, const QString &caption) const
Definition: qgpgmelistallkeysjob.cpp:151
Kleo::QGpgMEListAllKeysJob::resultHook
void resultHook(const result_type &result)
Definition: qgpgmelistallkeysjob.cpp:147
Kleo::QGpgMEListAllKeysJob::exec
GpgME::KeyListResult exec(std::vector< GpgME::Key > &pub, std::vector< GpgME::Key > &sec, bool mergeKeys)
Definition: qgpgmelistallkeysjob.cpp:139
Kleo::_detail::ThreadedJobMixin< ListAllKeysJob, boost::tuple< GpgME::KeyListResult, std::vector< GpgME::Key >, std::vector< GpgME::Key >, QString, GpgME::Error > >::context
GpgME::Context * context() const
Definition: threadedjobmixin.h:176
QWidget
QString
Kleo::QGpgMEListAllKeysJob::QGpgMEListAllKeysJob
QGpgMEListAllKeysJob(GpgME::Context *context)
Definition: qgpgmelistallkeysjob.cpp:58
qgpgmelistallkeysjob.h
Kleo::_detail::ThreadedJobMixin< ListAllKeysJob, boost::tuple< GpgME::KeyListResult, std::vector< GpgME::Key >, std::vector< GpgME::Key >, QString, GpgME::Error > >::lateInitialization
void lateInitialization()
Definition: threadedjobmixin.h:144
merge_keys
static void merge_keys(std::vector< Key > &merged, std::vector< Key > &pub, std::vector< Key > &sec)
Definition: qgpgmelistallkeysjob.cpp:105
Kleo::_detail::ThreadedJobMixin< ListAllKeysJob, boost::tuple< GpgME::KeyListResult, std::vector< GpgME::Key >, std::vector< GpgME::Key >, QString, GpgME::Error > >::result_type
boost::tuple< GpgME::KeyListResult, std::vector< GpgME::Key >, std::vector< GpgME::Key >, QString, GpgME::Error > result_type
Definition: threadedjobmixin.h:115
Kleo::_detail::ThreadedJobMixin< ListAllKeysJob, boost::tuple< GpgME::KeyListResult, std::vector< GpgME::Key >, std::vector< GpgME::Key >, QString, GpgME::Error > >::run
void run(const T_binder &func)
Definition: threadedjobmixin.h:151
Kleo::QGpgMEListAllKeysJob::start
GpgME::Error start(bool mergeKeys)
Definition: qgpgmelistallkeysjob.cpp:134
Kleo::QGpgMEListAllKeysJob::~QGpgMEListAllKeysJob
~QGpgMEListAllKeysJob()
Definition: qgpgmelistallkeysjob.cpp:65
kdtools::sort
void sort(C &c)
Definition: stl_util.h:520
list_keys
static QGpgMEListAllKeysJob::result_type list_keys(Context *ctx, bool mergeKeys)
Definition: qgpgmelistallkeysjob.cpp:117
Kleo::_detail::ThreadedJobMixin< ListAllKeysJob, boost::tuple< GpgME::KeyListResult, std::vector< GpgME::Key >, std::vector< GpgME::Key >, QString, GpgME::Error > >
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:57:49 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