• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kleopatra

  • sources
  • kde-4.12
  • kdepim
  • kleopatra
  • crypto
recipient.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  crypto/recipient.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2009 Klarälvdalens Datakonsult AB
6 
7  Kleopatra is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Kleopatra 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 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.
31 */
32 
33 #include <config-kleopatra.h>
34 
35 #include "recipient.h"
36 
37 #include <models/predicates.h>
38 #include <models/keycache.h>
39 
40 #include <utils/kleo_assert.h>
41 #include <utils/cached.h>
42 
43 #include <kmime/kmime_header_parsing.h>
44 
45 #include <gpgme++/key.h>
46 
47 using namespace Kleo;
48 using namespace Kleo::Crypto;
49 using namespace KMime::Types;
50 using namespace GpgME;
51 using namespace boost;
52 
53 namespace KMime {
54 namespace Types {
55 static bool operator==( const AddrSpec & lhs, const AddrSpec & rhs ) {
56  return lhs.localPart == rhs.localPart
57  && lhs.domain == rhs.domain ;
58 }
59 
60 static bool operator==( const Mailbox & lhs, const Mailbox & rhs ) {
61  return lhs.name() == rhs.name()
62  && lhs.addrSpec() == rhs.addrSpec() ;
63 }
64 
65 static bool determine_ambiguous( const Mailbox & mb, const std::vector<Key> & keys ) {
66  Q_UNUSED( mb );
67  // ### really do check when we don't only show matching keys
68  return keys.size() != 1 ;
69 }
70 
71 } // namespace Types
72 } // namespace KMime
73 
74 class Recipient::Private {
75  friend class ::Kleo::Crypto::Recipient;
76 public:
77  explicit Private( const Mailbox & mb )
78  : mailbox( mb )
79  {
80  // ### also fill up to a certain number of keys with those
81  // ### that don't match, for the case where there's a low
82  // ### total number of keys
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 );
87  }
88 
89 private:
90  const Mailbox mailbox;
91  std::vector<Key> pgpEncryptionKeys, cmsEncryptionKeys;
92  Key cmsEncryptionKey;
93  UserID pgpEncryptionUid;
94  cached<bool> encryptionAmbiguous[2];
95 };
96 
97 Recipient::Recipient( const Mailbox & mb )
98  : d( new Private( mb ) )
99 {
100 
101 }
102 
103 void Recipient::detach() {
104  if ( d && !d.unique() )
105  d.reset( new Private( *d ) );
106 }
107 
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 )
116  ;
117 }
118 
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];
123 }
124 
125 const Mailbox & Recipient::mailbox() const {
126  return d->mailbox;
127 }
128 
129 const std::vector<Key> & Recipient::encryptionCertificateCandidates( GpgME::Protocol proto ) const {
130  if ( proto == OpenPGP )
131  return d->pgpEncryptionKeys;
132  if ( proto == CMS )
133  return d->cmsEncryptionKeys;
134  kleo_assert_fail( proto == OpenPGP || proto == CMS );
135 #if 0
136  return
137  proto == OpenPGP ? d->pgpEncryptionKeys :
138  proto == CMS ? d->cmsEncryptionKeys :
139  // even though gcc warns about this line, it's completely ok, promise:
140  kleo_assert_fail( proto == OpenPGP || proto == CMS ) ;
141 #endif
142 }
143 
144 void Recipient::setResolvedEncryptionKey( const Key & key ) {
145  if ( key.isNull() )
146  return;
147  const Protocol proto = key.protocol();
148  kleo_assert( proto == OpenPGP || proto == CMS );
149  detach();
150  if ( proto == OpenPGP )
151  d->pgpEncryptionUid = key.userID( 0 );
152  else
153  d->cmsEncryptionKey = key;
154  d->encryptionAmbiguous[proto] = false;
155 }
156 
157 Key Recipient::resolvedEncryptionKey( GpgME::Protocol proto ) const {
158  kleo_assert( proto == OpenPGP || proto == CMS );
159  if ( proto == OpenPGP )
160  return d->pgpEncryptionUid.parent();
161  else
162  return d->cmsEncryptionKey;
163 }
164 
165 void Recipient::setResolvedOpenPGPEncryptionUserID( const UserID & uid ) {
166  if ( uid.isNull() )
167  return;
168  detach();
169  d->pgpEncryptionUid = uid;
170 }
171 
172 UserID Recipient::resolvedOpenPGPEncryptionUserID() const {
173  return d->pgpEncryptionUid;
174 }
KMime::Types::operator==
static bool operator==(const Mailbox &lhs, const Mailbox &rhs)
Definition: recipient.cpp:60
kleo_assert.h
d
#define d
Definition: adduseridcommand.cpp:90
Kleo::Crypto::Recipient::mailbox
const KMime::Types::Mailbox & mailbox() const
Definition: recipient.cpp:125
Kleo::Crypto::Recipient::Recipient
Recipient()
Definition: recipient.h:58
cached.h
recipient.h
Kleo::Class::OpenPGP
Definition: classify.h:49
Kleo::Class::CMS
Definition: classify.h:48
kleo_assert_fail
#define kleo_assert_fail(cond)
Definition: kleo_assert.h:88
kleo_assert
#define kleo_assert(cond)
Definition: kleo_assert.h:84
Kleo::cached< bool >
predicates.h
Kleo::KeyCache::instance
static boost::shared_ptr< const KeyCache > instance()
Definition: keycache.cpp:189
keycache.h
KMime::Types::determine_ambiguous
static bool determine_ambiguous(const Mailbox &mb, const std::vector< Key > &keys)
Definition: recipient.cpp:65
Kleo::Crypto::Recipient
Definition: recipient.h:56
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:41 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kleopatra

Skip menu "kleopatra"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal