akonadiconsole, DB Console tab: Ctrl + Return to execute query.
[kdepim.git] / libkleo / kleo / hierarchicalkeylistjob.cpp
blobe30d860436eb2eca05690d95eaacc96c9934d058
1 /*
2 hierarchicalkeylistjob.cpp
4 This file is part of libkleopatra, the KDE keymanagement library
5 Copyright (c) 2004 Klarälvdalens Datakonsult AB
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.
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.
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
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.
34 #include "hierarchicalkeylistjob.h"
35 #include "cryptobackend.h"
36 #include "keylistjob.h"
38 #include <klocale.h>
40 #include <QStringList>
42 #include <gpgme++/key.h>
43 #include <gpgme++/context.h>
44 #include <gpgme++/data.h>
46 #include <gpg-error.h>
48 #include <iterator>
49 #include <algorithm>
51 #include <assert.h>
53 Kleo::HierarchicalKeyListJob::HierarchicalKeyListJob( const CryptoBackend::Protocol * protocol,
54 bool remote, bool includeSigs, bool validating )
55 : KeyListJob( 0 ),
56 mProtocol( protocol ),
57 mRemote( remote ),
58 mIncludeSigs( includeSigs ),
59 mValidating( validating ),
60 mTruncated( false ),
61 mIntermediateResult(),
62 mJob( 0 )
64 assert( protocol );
67 Kleo::HierarchicalKeyListJob::~HierarchicalKeyListJob() {
71 GpgME::Error Kleo::HierarchicalKeyListJob::start( const QStringList & patterns, bool secretOnly ) {
72 if ( secretOnly || patterns.empty() )
73 return GpgME::Error::fromCode( GPG_ERR_UNSUPPORTED_OPERATION, GPG_ERR_SOURCE_GPGME );
74 qCopy( patterns.begin(), patterns.end(),
75 std::inserter( mNextSet, mNextSet.begin() ) );
76 const GpgME::Error err = startAJob();
77 if ( err )
78 deleteLater();
79 return err;
82 GpgME::KeyListResult Kleo::HierarchicalKeyListJob::exec( const QStringList &, bool,
83 std::vector<GpgME::Key> & keys ) {
84 keys.clear();
85 return GpgME::KeyListResult( GpgME::Error::fromCode( GPG_ERR_UNSUPPORTED_OPERATION, GPG_ERR_SOURCE_GPGME ) );
88 void Kleo::HierarchicalKeyListJob::slotNextKey( const GpgME::Key & key ) {
89 if ( const char * chain_id = key.chainID() )
90 mNextSet.insert( QLatin1String(chain_id) );
91 if ( const char * fpr = key.primaryFingerprint() )
92 if ( mSentSet.find( QLatin1String(fpr) ) == mSentSet.end() ) {
93 mSentSet.insert( QLatin1String(fpr) );
94 emit nextKey( key );
98 void Kleo::HierarchicalKeyListJob::slotCancel() {
99 if ( mJob ) mJob->slotCancel();
100 mNextSet.clear();
103 void Kleo::HierarchicalKeyListJob::slotResult( const GpgME::KeyListResult & res ) {
104 mJob = 0;
105 mIntermediateResult.mergeWith( res );
106 std::set<QString> tmp;
107 std::set_difference( mNextSet.begin(), mNextSet.end(),
108 mScheduledSet.begin(), mScheduledSet.end(),
109 std::inserter( tmp, tmp.begin() ) );
110 mNextSet.clear();
111 std::set_difference( tmp.begin(), tmp.end(),
112 mSentSet.begin(), mSentSet.end(),
113 std::inserter( mNextSet, mNextSet.begin() ) );
114 if ( mIntermediateResult.error() || mNextSet.empty() ) {
115 emit done();
116 emit result( mIntermediateResult );
117 deleteLater();
118 return;
120 if ( const GpgME::Error error = startAJob() ) { // error starting the job for next keys
121 mIntermediateResult.mergeWith( GpgME::KeyListResult( error ) );
122 emit done();
123 emit result( mIntermediateResult );
124 deleteLater();
125 return;
127 #if 0 // FIXME
128 const int current = mIt - mKeys.begin();
129 const int total = mKeys.size();
130 emit progress( i18nc("progress info: \"%1 of %2\"","%1/%2", current, total ), current, total );
131 #endif
134 GpgME::Error Kleo::HierarchicalKeyListJob::startAJob() {
135 if ( mNextSet.empty() )
136 return GpgME::Error(0);
137 mJob = mProtocol->keyListJob( mRemote, mIncludeSigs, mValidating );
138 assert( mJob ); // FIXME: we need a way to generate errors ourselves,
139 // but I don't like the dependency on gpg-error :/
141 connect( mJob, SIGNAL(nextKey(GpgME::Key)), SLOT(slotNextKey(GpgME::Key)) );
142 connect( mJob, SIGNAL(result(GpgME::KeyListResult)), SLOT(slotResult(GpgME::KeyListResult)) );
144 QStringList patterns;
145 for ( std::set<QString>::const_iterator it = mNextSet.begin() ; it != mNextSet.end() ; ++it )
146 patterns.push_back( *it );
148 mScheduledSet.insert( mNextSet.begin(), mNextSet.end() );
149 mNextSet.clear();
151 return mJob->start( patterns, false );
154 #include "moc_hierarchicalkeylistjob.cpp"