37 #include "kpgpkeylistjob.h"
42 #include <gpgmepp/key.h>
43 #include <gpgmepp/keylistresult.h>
53 Kleo::KpgpKeyListJob::KpgpKeyListJob( Kpgp::Base * pgpBase )
54 : KeyListJob( 0,
"Kleo::KpgpKeyListJob" ),
59 Kleo::KpgpKeyListJob::~KpgpKeyListJob() {
64 set_user_id_part (
char *tail,
const char *buf,
size_t len)
66 while (len && (buf[len - 1] ==
' ' || buf[len - 1] ==
'\t'))
76 parse_user_id (
char *src,
char **name,
char **email,
77 char **comment,
char *tail)
79 const char *start = NULL;
93 if (!--in_email && !*email)
96 tail = set_user_id_part (tail, start, src - start);
104 else if (*src ==
')')
106 if (!--in_comment && !*comment)
109 tail = set_user_id_part (tail, start, src - start);
113 else if (*src ==
'<')
120 tail = set_user_id_part (tail, start, src - start);
127 else if (*src ==
'(')
134 tail = set_user_id_part (tail, start, src - start);
141 else if (!in_name && *src !=
' ' && *src !=
'\t')
154 tail = set_user_id_part (tail, start, src - start);
168 gpgme_user_id_t KpgpUserID2GPGMEUserID(
const Kpgp::UserID * kUserId )
172 const TQCString text = kUserId->text().utf8();
173 const int src_len = text.length();
178 uid = (gpgme_user_id_t) malloc(
sizeof( *uid ) + 2 * src_len + 3 );
179 memset( uid, 0,
sizeof *uid );
180 uid->revoked = kUserId->revoked();
181 uid->invalid = kUserId->invalid();
182 uid->validity = (gpgme_validity_t) kUserId->validity();
184 uid->uid = ((
char *) uid) +
sizeof (*uid);
185 char *dst = uid->uid;
186 memcpy( dst, text.data(), src_len + 1 );
189 parse_user_id( uid->uid, &uid->name, &uid->email,
190 &uid->comment, dst );
195 gpgme_subkey_t KpgpSubkey2GPGMESubKey(
const Kpgp::Subkey * kSubkey )
197 gpgme_subkey_t subkey;
199 const TQCString fpr = kSubkey->fingerprint();
200 const unsigned int fpr_len = fpr.length();
201 const TQCString keyId = kSubkey->longKeyID();
203 subkey = (gpgme_subkey_t) calloc( 1,
sizeof( *subkey ) + fpr_len + 1 );
204 subkey->revoked = kSubkey->revoked();
205 subkey->expired = kSubkey->expired();
206 subkey->disabled = kSubkey->disabled();
207 subkey->invalid = kSubkey->invalid();
208 subkey->can_encrypt = kSubkey->canEncrypt();
209 subkey->can_sign = kSubkey->canSign();
210 subkey->can_certify = kSubkey->canCertify();
211 subkey->secret = kSubkey->secret();
212 subkey->pubkey_algo = (gpgme_pubkey_algo_t) kSubkey->keyAlgorithm();
213 subkey->length = kSubkey->keyLength();
214 subkey->keyid = subkey->_keyid;
215 memcpy( subkey->_keyid, keyId.data(), keyId.length() + 1 );
216 subkey->fpr = ((
char *) subkey) +
sizeof( *subkey );
217 memcpy( subkey->fpr, fpr.data(), fpr_len + 1 );
218 subkey->timestamp = kSubkey->creationDate();
219 subkey->expires = kSubkey->expirationDate();
224 gpgme_key_t KpgpKey2gpgme_key(
const Kpgp::Key * kKey )
228 key = (gpgme_key_t) calloc( 1,
sizeof( *key ) );
229 key->revoked = kKey->revoked();
230 key->expired = kKey->expired();
231 key->disabled = kKey->disabled();
232 key->invalid = kKey->invalid();
233 key->can_encrypt = kKey->canEncrypt();
234 key->can_sign = kKey->canSign();
235 key->can_certify = kKey->canCertify();
236 key->secret = kKey->secret();
237 key->protocol = GPGME_PROTOCOL_OpenPGP;
238 key->owner_trust = GPGME_VALIDITY_UNKNOWN;
240 Kpgp::UserIDList kUserIDs = kKey->userIDs();
241 for ( Kpgp::UserIDListIterator it( kUserIDs ); it.current(); ++it ) {
242 gpgme_user_id_t uid = KpgpUserID2GPGMEUserID( *it );
245 if ( key->_last_uid )
246 key->_last_uid->next = uid;
247 key->_last_uid = uid;
250 Kpgp::SubkeyList kSubkeys = kKey->subkeys();
251 for ( Kpgp::SubkeyListIterator it( kSubkeys ); it.current(); ++it ) {
252 gpgme_subkey_t subkey = KpgpSubkey2GPGMESubKey( *it );
254 key->subkeys = subkey;
255 if (key->_last_subkey)
256 key->_last_subkey->next = subkey;
257 key->_last_subkey = subkey;
263 GpgME::Error Kleo::KpgpKeyListJob::start(
const TQStringList & patterns,
265 mPatterns = patterns;
266 mSecretOnly = secretOnly;
267 TQTimer::singleShot( 0,
this, TQ_SLOT( slotDoIt() ) );
268 return GpgME::Error( 0 );
271 void Kleo::KpgpKeyListJob::slotDoIt() {
272 std::vector<GpgME::Key> keys;
273 GpgME::KeyListResult res = exec( mPatterns, mSecretOnly, keys );
274 for ( std::vector<GpgME::Key>::const_iterator it = keys.begin();
275 it != keys.end(); ++it )
282 GpgME::KeyListResult Kleo::KpgpKeyListJob::exec(
const TQStringList & patterns,
284 std::vector<GpgME::Key> & keys ) {
287 kKeys = mPgpBase->secretKeys( patterns );
289 kKeys = mPgpBase->publicKeys( patterns );
292 for ( Kpgp::KeyListIterator it( kKeys ); it.current(); ++it ) {
293 keys.push_back( GpgME::Key( KpgpKey2gpgme_key(*it),
true ) );
296 _gpgme_op_keylist_result res;
300 return GpgME::KeyListResult( GpgME::Error( 0 ), res );
303 #include "kpgpkeylistjob.moc"