certmanager/lib

hierarchicalkeylistjob.cpp
1 /*
2  hierarchicalkeylistjob.cpp
3 
4  This file is part of libkleopatra, the KDE keymanagement library
5  Copyright (c) 2004 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 "hierarchicalkeylistjob.h"
38 #include "cryptobackend.h"
39 #include "keylistjob.h"
40 
41 #include <tdelocale.h>
42 
43 #include <tqstringlist.h>
44 #include <tqtl.h>
45 
46 #include <gpgmepp/key.h>
47 #include <gpgmepp/context.h>
48 #include <gpgmepp/data.h>
49 
50 #include <gpg-error.h>
51 
52 #include <iterator>
53 #include <algorithm>
54 
55 #include <assert.h>
56 
57 Kleo::HierarchicalKeyListJob::HierarchicalKeyListJob( const CryptoBackend::Protocol * protocol,
58  bool remote, bool includeSigs, bool validating )
59  : KeyListJob( 0, "Kleo::HierarchicalKeyListJob" ),
60  mProtocol( protocol ),
61  mRemote( remote ),
62  mIncludeSigs( includeSigs ),
63  mValidating( validating ),
64  mTruncated( false ),
65  mIntermediateResult(),
66  mJob( 0 )
67 {
68  assert( protocol );
69 }
70 
71 Kleo::HierarchicalKeyListJob::~HierarchicalKeyListJob() {
72 
73 }
74 
75 GpgME::Error Kleo::HierarchicalKeyListJob::start( const TQStringList & patterns, bool secretOnly ) {
76  if ( secretOnly || patterns.empty() )
77  return gpg_err_make( GPG_ERR_SOURCE_GPGME, GPG_ERR_UNSUPPORTED_OPERATION );
78  tqCopy( patterns.begin(), patterns.end(),
79  std::inserter( mNextSet, mNextSet.begin() ) );
80  const GpgME::Error err = startAJob();
81  if ( err )
82  deleteLater();
83  return err;
84 }
85 
86 GpgME::KeyListResult Kleo::HierarchicalKeyListJob::exec( const TQStringList &, bool,
87  std::vector<GpgME::Key> & keys ) {
88  keys.clear();
89  return GpgME::KeyListResult( gpg_err_make( GPG_ERR_SOURCE_GPGME, GPG_ERR_UNSUPPORTED_OPERATION ) );
90 }
91 
92 void Kleo::HierarchicalKeyListJob::slotNextKey( const GpgME::Key & key ) {
93  if ( const char * chain_id = key.chainID() )
94  mNextSet.insert( chain_id );
95  if ( const char * fpr = key.primaryFingerprint() )
96  if ( mSentSet.find( fpr ) == mSentSet.end() ) {
97  mSentSet.insert( fpr );
98  emit nextKey( key );
99  }
100 }
101 
102 void Kleo::HierarchicalKeyListJob::slotCancel() {
103  if ( mJob ) mJob->slotCancel();
104  mNextSet.clear();
105 }
106 
107 void Kleo::HierarchicalKeyListJob::slotResult( const GpgME::KeyListResult & res ) {
108  mJob = 0;
109  mIntermediateResult.mergeWith( res );
110  std::set<TQString> tmp;
111  std::set_difference( mNextSet.begin(), mNextSet.end(),
112  mScheduledSet.begin(), mScheduledSet.end(),
113  std::inserter( tmp, tmp.begin() ) );
114  mNextSet.clear();
115  std::set_difference( tmp.begin(), tmp.end(),
116  mSentSet.begin(), mSentSet.end(),
117  std::inserter( mNextSet, mNextSet.begin() ) );
118  if ( mIntermediateResult.error() || mNextSet.empty() ) {
119  emit done();
120  emit result( mIntermediateResult );
121  deleteLater();
122  return;
123  }
124  if ( const GpgME::Error error = startAJob() ) { // error starting the job for next keys
125  mIntermediateResult.mergeWith( GpgME::KeyListResult( error ) );
126  emit done();
127  emit result( mIntermediateResult );
128  deleteLater();
129  return;
130  }
131 #if 0 // FIXME
132  const int current = mIt - mKeys.begin();
133  const int total = mKeys.size();
134  emit progress( i18n("progress info: \"%1 of %2\"","%1/%2").arg( current ).arg( total ), current, total );
135 #endif
136 }
137 
138 GpgME::Error Kleo::HierarchicalKeyListJob::startAJob() {
139  if ( mNextSet.empty() )
140  return 0;
141  mJob = mProtocol->keyListJob( mRemote, mIncludeSigs, mValidating );
142  assert( mJob ); // FIXME: we need a way to generate errors ourselves,
143  // but I don't like the dependency on gpg-error :/
144 
145  connect( mJob, TQ_SIGNAL(nextKey(const GpgME::Key&)), TQ_SLOT(slotNextKey(const GpgME::Key&)) );
146  connect( mJob, TQ_SIGNAL(result(const GpgME::KeyListResult&)), TQ_SLOT(slotResult(const GpgME::KeyListResult&)) );
147 
148  TQStringList patterns;
149  for ( std::set<TQString>::const_iterator it = mNextSet.begin() ; it != mNextSet.end() ; ++it )
150  patterns.push_back( *it );
151 
152  mScheduledSet.insert( mNextSet.begin(), mNextSet.end() );
153  mNextSet.clear();
154 
155  return mJob->start( patterns, false );
156 }
157 
158 #include "hierarchicalkeylistjob.moc"
GpgME::Error start(const TQStringList &patterns, bool secretOnly=false)
Starts the keylist operation.