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
34 #include "hierarchicalkeylistjob.h"
35 #include "cryptobackend.h"
36 #include "keylistjob.h"
40 #include <QStringList>
42 #include <gpgme++/key.h>
43 #include <gpgme++/context.h>
44 #include <gpgme++/data.h>
46 #include <gpg-error.h>
53 Kleo::HierarchicalKeyListJob::HierarchicalKeyListJob( const CryptoBackend::Protocol
* protocol
,
54 bool remote
, bool includeSigs
, bool validating
)
56 mProtocol( protocol
),
58 mIncludeSigs( includeSigs
),
59 mValidating( validating
),
61 mIntermediateResult(),
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();
82 GpgME::KeyListResult
Kleo::HierarchicalKeyListJob::exec( const QStringList
&, bool,
83 std::vector
<GpgME::Key
> & keys
) {
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
) );
98 void Kleo::HierarchicalKeyListJob::slotCancel() {
99 if ( mJob
) mJob
->slotCancel();
103 void Kleo::HierarchicalKeyListJob::slotResult( const GpgME::KeyListResult
& res
) {
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() ) );
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() ) {
116 emit
result( mIntermediateResult
);
120 if ( const GpgME::Error error
= startAJob() ) { // error starting the job for next keys
121 mIntermediateResult
.mergeWith( GpgME::KeyListResult( error
) );
123 emit
result( mIntermediateResult
);
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
);
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() );
151 return mJob
->start( patterns
, false );
154 #include "moc_hierarchicalkeylistjob.cpp"