33 #include <config-kleopatra.h>
43 #include <kmime/kmime_header_parsing.h>
45 #include <gpgme++/key.h>
48 using namespace Kleo::Crypto;
49 using namespace KMime::Types;
50 using namespace GpgME;
51 using namespace boost;
55 static bool operator==(
const AddrSpec & lhs,
const AddrSpec & rhs ) {
56 return lhs.localPart == rhs.localPart
57 && lhs.domain == rhs.domain ;
60 static bool operator==(
const Mailbox & lhs,
const Mailbox & rhs ) {
61 return lhs.name() == rhs.name()
62 && lhs.addrSpec() == rhs.addrSpec() ;
68 return keys.size() != 1 ;
74 class Recipient::Private {
75 friend class ::Kleo::Crypto::Recipient;
77 explicit Private(
const Mailbox & mb )
83 const std::vector<Key> encrypt =
KeyCache::instance()->findEncryptionKeysByMailbox( mb );
84 kdtools::separate_if( encrypt,
85 std::back_inserter( pgpEncryptionKeys ), std::back_inserter( cmsEncryptionKeys ),
86 boost::bind( &Key::protocol, _1 ) ==
OpenPGP );
90 const Mailbox mailbox;
91 std::vector<Key> pgpEncryptionKeys, cmsEncryptionKeys;
93 UserID pgpEncryptionUid;
98 :
d( new Private( mb ) )
103 void Recipient::detach() {
104 if (
d && !
d.unique() )
105 d.reset(
new Private( *
d ) );
108 bool Recipient::deepEquals(
const Recipient & other )
const {
109 static const _detail::ByFingerprint<std::equal_to> compare = {};
110 return mailbox() == other.
mailbox()
111 && compare(
d->cmsEncryptionKey, other.d->cmsEncryptionKey )
112 && compare(
d->pgpEncryptionUid.parent(), other.d->pgpEncryptionUid.parent() )
113 && strcmp(
d->pgpEncryptionUid.id(), other.d->pgpEncryptionUid.id() )
114 && kdtools::equal(
d->pgpEncryptionKeys, other.d->pgpEncryptionKeys, compare )
115 && kdtools::equal(
d->cmsEncryptionKeys, other.d->cmsEncryptionKeys, compare )
119 bool Recipient::isEncryptionAmbiguous( GpgME::Protocol proto )
const {
120 if (
d->encryptionAmbiguous[proto].dirty() )
121 d->encryptionAmbiguous[proto] =
determine_ambiguous(
d->mailbox, encryptionCertificateCandidates( proto ) );
122 return d->encryptionAmbiguous[proto];
125 const Mailbox & Recipient::mailbox()
const {
129 const std::vector<Key> & Recipient::encryptionCertificateCandidates( GpgME::Protocol proto )
const {
131 return d->pgpEncryptionKeys;
133 return d->cmsEncryptionKeys;
137 proto ==
OpenPGP ?
d->pgpEncryptionKeys :
138 proto ==
CMS ?
d->cmsEncryptionKeys :
144 void Recipient::setResolvedEncryptionKey(
const Key & key ) {
147 const Protocol proto = key.protocol();
151 d->pgpEncryptionUid = key.userID( 0 );
153 d->cmsEncryptionKey = key;
154 d->encryptionAmbiguous[proto] =
false;
157 Key Recipient::resolvedEncryptionKey( GpgME::Protocol proto )
const {
160 return d->pgpEncryptionUid.parent();
162 return d->cmsEncryptionKey;
165 void Recipient::setResolvedOpenPGPEncryptionUserID(
const UserID & uid ) {
169 d->pgpEncryptionUid = uid;
172 UserID Recipient::resolvedOpenPGPEncryptionUserID()
const {
173 return d->pgpEncryptionUid;
static bool operator==(const Mailbox &lhs, const Mailbox &rhs)
const KMime::Types::Mailbox & mailbox() const
#define kleo_assert_fail(cond)
#define kleo_assert(cond)
static boost::shared_ptr< const KeyCache > instance()
static bool determine_ambiguous(const Mailbox &mb, const std::vector< Key > &keys)